Skip to content

Commit

Permalink
Canonicalize air.execute returned value if its yielded value isn't de…
Browse files Browse the repository at this point in the history
…fined within the execute (Xilinx#871)
  • Loading branch information
erwei-xilinx authored Jan 21, 2025
1 parent 0061fad commit cf4230d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
16 changes: 10 additions & 6 deletions mlir/lib/Dialect/AIR/IR/AIRDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1181,17 +1181,21 @@ static LogicalResult FoldExecute(ExecuteOp op, PatternRewriter &rewriter) {
return success();
}

// replace returns of constants with the constant
// replace returns of (1) constants with the constant, and (2) values not
// defined within the execute with its original value
int idx = 0;
for (auto v : et->getOperands()) {
idx++;
if (op.getResult(idx).use_empty())
continue;
auto o = v.getDefiningOp();
if (!o)
continue;
if (isa<arith::ConstantOp>(o)) {
op.getResult(idx).replaceAllUsesWith(rewriter.clone(*o)->getResult(0));
if (!op.getRegion().isAncestor(v.getParentRegion())) {
rewriter.replaceAllUsesWith(op.getResult(idx), v);
return success();
}
if (auto constOp =
dyn_cast_if_present<arith::ConstantOp>(v.getDefiningOp())) {
rewriter.replaceAllUsesWith(op.getResult(idx),
rewriter.clone(*constOp)->getResult(0));
return success();
}
}
Expand Down
25 changes: 25 additions & 0 deletions mlir/test/Dialect/AIR/air_canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,31 @@ func.func @execute_4() -> (memref<1xi32>, !air.async.token) {
return %results, %t : memref<1xi32>, !air.async.token
}

// CHECK-LABEL: execute_5
// CHECK: scf.for {{.*}} {
// CHECK: air.execute {{.*}} {
// CHECK-NEXT: memref.store
// CHECK-NEXT: }
// CHECK: scf.yield
// CHECK-NEXT: }
func.func @execute_5(%alloc : memref<4xi32>) -> (!air.async.token) {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%c4 = arith.constant 4 : index
%cst = arith.constant 2 : i32
%t = air.wait_all async
%0 = scf.for %arg0 = %c0 to %c4 step %c1 iter_args(%arg1 = %t) -> (!air.async.token) {
%async_token_0, %results_1 = air.execute [%arg1] -> (index) {
air.execute_terminator %arg0 : index
}
%async_token_1 = air.execute [%async_token_0] {
memref.store %cst, %alloc[%results_1] : memref<4xi32>
}
scf.yield %async_token_1 : !air.async.token
}
return %0 :!air.async.token
}

// CHECK: func.func @chan_0
// CHECK: %[[TOKEN0:.*]] = air.channel.get async @channel_0
// CHECK: %[[TOKEN1:.*]] = air.channel.get async @channel_1
Expand Down

0 comments on commit cf4230d

Please sign in to comment.