Skip to content

Propagate attributes from the llvm dialect during cf canonicalization #2

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions mlir/lib/Dialect/ControlFlow/IR/ControlFlowOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/Bufferization/IR/BufferDeallocationOpInterface.h"
#include "mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h"
#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/IR/AffineExpr.h"
#include "mlir/IR/AffineMap.h"
#include "mlir/IR/Builders.h"
Expand Down Expand Up @@ -265,9 +267,20 @@ struct SimplifyPassThroughCondBranch : public OpRewritePattern<CondBranchOp> {
return failure();

// Create a new branch with the collapsed successors.
rewriter.replaceOpWithNewOp<CondBranchOp>(condbr, condbr.getCondition(),
trueDest, trueDestOperands,
falseDest, falseDestOperands);
auto newCondbr = rewriter.replaceOpWithNewOp<CondBranchOp>(
condbr, condbr.getCondition(), trueDest, trueDestOperands, falseDest,
falseDestOperands);

// Ensure loop metadata is copied into the new op
rewriter.modifyOpInPlace(newCondbr, [&]() {
SmallVector<NamedAttribute> llvmAttrs;
llvm::copy_if(
condbr->getAttrs(), std::back_inserter(llvmAttrs), [](auto attr) {
return isa<mlir::LLVM::LLVMDialect>(attr.getValue().getDialect());
});
newCondbr->setDiscardableAttrs(llvmAttrs);
});

return success();
}
};
Expand Down
25 changes: 25 additions & 0 deletions mlir/test/Dialect/ControlFlow/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,31 @@ func.func @cond_br_passthrough(%arg0 : i32, %arg1 : i32, %arg2 : i32, %cond : i1
return %arg4, %arg5 : i32, i32
}

/// Test that pass-through simplification does not lose metadata
#loop_unroll = #llvm.loop_unroll<disable = false, full = true>
#loop_annotation = #llvm.loop_annotation<unroll = #loop_unroll>
func.func @cond_br_preserve_loop_md(%arg0 : i32, %arg1 : i32, %arg2 : i32, %cond : i1) {
// CHECK-LABEL: @cond_br_preserve_loop_md(
// CHECK: cf.cond_br
// CHECK-SAME: loop_annotation = #loop_annotation
cf.cond_br %cond, ^bb1, ^bb2 {loop_annotation = #loop_annotation}

// CHECK-NEXT: ^bb1:
// CHECK-NEXT: foo.op
// CHECK-NEXT: cf.br
// CHECK-NEXT: ^bb2:
// CHECK-NEXT: return
^bb1:
cf.br ^bb3

^bb2:
"foo.op"() : () -> ()
cf.br ^bb3

^bb3:
return
}

/// Test the failure modes of collapsing CondBranchOp pass-throughs successors.

// CHECK-LABEL: func @cond_br_pass_through_fail(
Expand Down