-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[MLIR][Affine] Fix memref replacement in affine-data-copy-generate #139016
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,13 +115,16 @@ static bool doubleBuffer(Value oldMemRef, AffineForOp forOp) { | |
|
||
// replaceAllMemRefUsesWith will succeed unless the forOp body has | ||
// non-dereferencing uses of the memref (dealloc's are fine though). | ||
if (failed(replaceAllMemRefUsesWith( | ||
oldMemRef, newMemRef, | ||
/*extraIndices=*/{ivModTwoOp}, | ||
/*indexRemap=*/AffineMap(), | ||
/*extraOperands=*/{}, | ||
/*symbolOperands=*/{}, | ||
/*domOpFilter=*/&*forOp.getBody()->begin()))) { | ||
auto userFilterFn = [&](Operation *user) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we compute dominance info inside a static utility? It is expensive, and I thought the convention is to compute it once in the entry function of a pass, and pass it around. |
||
auto domInfo = std::make_unique<DominanceInfo>( | ||
forOp->getParentOfType<FunctionOpInterface>()); | ||
return domInfo->dominates(&*forOp.getBody()->begin(), user); | ||
}; | ||
if (failed(replaceAllMemRefUsesWith(oldMemRef, newMemRef, | ||
/*extraIndices=*/{ivModTwoOp}, | ||
/*indexRemap=*/AffineMap(), | ||
/*extraOperands=*/{}, | ||
/*symbolOperands=*/{}, userFilterFn))) { | ||
LLVM_DEBUG( | ||
forOp.emitError("memref replacement for double buffering failed")); | ||
ivModTwoOp.erase(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -447,3 +447,51 @@ func.func @memref_def_inside(%arg0: index) { | |
// LIMITED-MEM-NEXT: memref.dealloc %{{.*}} : memref<1xf32> | ||
return | ||
} | ||
|
||
// Test with uses across multiple blocks. | ||
|
||
memref.global "private" constant @__constant_1x2x1xi32_1 : memref<1x2x1xi32> = dense<0> {alignment = 64 : i64} | ||
|
||
// CHECK-LABEL: func @multiple_blocks | ||
func.func @multiple_blocks(%arg0: index) -> memref<1x2x1xi32> { | ||
%c1_i32 = arith.constant 1 : i32 | ||
%c3_i32 = arith.constant 3 : i32 | ||
%0 = memref.get_global @__constant_1x2x1xi32_1 : memref<1x2x1xi32> | ||
%alloc = memref.alloc() {alignment = 64 : i64} : memref<1x2x1xi32> | ||
memref.copy %0, %alloc : memref<1x2x1xi32> to memref<1x2x1xi32> | ||
cf.br ^bb1(%alloc : memref<1x2x1xi32>) | ||
^bb1(%1: memref<1x2x1xi32>): // 2 preds: ^bb0, ^bb2 | ||
// CHECK: ^bb1(%[[MEM:.*]]: memref<1x2x1xi32>): | ||
%alloc_0 = memref.alloc() {alignment = 64 : i64} : memref<1x2x1xi1> | ||
// CHECK: %[[BUF:.*]] = memref.alloc() : memref<1x2x1xi32> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A question for my understanding -- Why is the fast buffer not allocated in shared memory space? |
||
affine.for %arg1 = 0 to 1 { | ||
affine.for %arg2 = 0 to 2 { | ||
affine.for %arg3 = 0 to 1 { | ||
// CHECK: affine.load %[[BUF]] | ||
%3 = affine.load %1[%arg1, %arg2, %arg3] : memref<1x2x1xi32> | ||
%4 = arith.cmpi slt, %3, %c3_i32 : i32 | ||
affine.store %4, %alloc_0[%arg1, %arg2, %arg3] : memref<1x2x1xi1> | ||
} | ||
} | ||
} | ||
// CHECK: memref.dealloc %[[BUF]] | ||
%2 = memref.load %alloc_0[%arg0, %arg0, %arg0] : memref<1x2x1xi1> | ||
cf.cond_br %2, ^bb2, ^bb3 | ||
^bb2: // pred: ^bb1 | ||
// CHECK: ^bb2 | ||
%alloc_1 = memref.alloc() {alignment = 64 : i64} : memref<1x2x1xi32> | ||
affine.for %arg1 = 0 to 1 { | ||
affine.for %arg2 = 0 to 2 { | ||
affine.for %arg3 = 0 to 1 { | ||
// Ensure that this reference isn't replaced. | ||
%3 = affine.load %1[%arg1, %arg2, %arg3] : memref<1x2x1xi32> | ||
// CHECK: affine.load %[[MEM]] | ||
%4 = arith.addi %3, %c1_i32 : i32 | ||
affine.store %4, %alloc_1[%arg1, %arg2, %arg3] : memref<1x2x1xi32> | ||
} | ||
} | ||
} | ||
cf.br ^bb1(%alloc_1 : memref<1x2x1xi32>) | ||
^bb3: // pred: ^bb1 | ||
return %1 : memref<1x2x1xi32> | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we compute dominance info inside a static utility? It is expensive, and I thought the convention is to compute it once in the entry function of a pass, and pass it around.