Skip to content

[MLIR] Allow llvm.resume with non-landingpad input #8590

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

Merged
merged 3 commits into from
Mar 10, 2023
Merged
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
17 changes: 15 additions & 2 deletions mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1551,8 +1551,21 @@ LogicalResult ReturnOp::verify() {
//===----------------------------------------------------------------------===//

LogicalResult ResumeOp::verify() {
if (!getValue().getDefiningOp<LandingpadOp>())
return emitOpError("expects landingpad value as operand");
auto parentFunc = getOperation()->getParentOfType<FunctionOpInterface>();
assert(parentFunc && "Expecting parent function");
const auto ty = getOperand().getType();
if (!parentFunc
->walk([ty](LandingpadOp landingpad) {
return landingpad.getType() == ty
? WalkResult::interrupt() // Just an operation needed
: WalkResult::advance();
})
.wasInterrupted()) {
// No operation was found: emit error
return emitOpError("expects landingpad operation in the same function and "
"with the same type as this operation's operand");
}

// No check for personality of function - landingpad op verifies it.
return success();
}
Expand Down
13 changes: 12 additions & 1 deletion mlir/test/Dialect/LLVMIR/invalid.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,18 @@ llvm.func @caller(%arg0: i32) -> i32 attributes { personality = @__gxx_personali
llvm.return %0 : i32
^bb2: // pred: ^bb0
%2 = llvm.landingpad cleanup : !llvm.struct<(ptr<i8>, i32)>
// expected-error@+1 {{'llvm.resume' op expects landingpad value as operand}}
// expected-error@+1 {{'llvm.resume' op expects landingpad operation in the same function and with the same type as this operation's operand}}
llvm.resume %0 : i32
}

// -----

llvm.func @foo(i32) -> i32
llvm.func @__gxx_personality_v0(...) -> i32

llvm.func @caller(%arg0: i32) -> i32 attributes { personality = @__gxx_personality_v0 } {
%0 = llvm.mlir.constant(1 : i32) : i32
// expected-error@+1 {{'llvm.resume' op expects landingpad operation in the same function and with the same type as this operation's operand}}
llvm.resume %0 : i32
}

Expand Down
21 changes: 21 additions & 0 deletions mlir/test/Dialect/LLVMIR/roundtrip.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,27 @@ llvm.func @invokeLandingpad() -> i32 attributes { personality = @__gxx_personali
llvm.return %0 : i32
}

llvm.func @foo2() -> !llvm.struct<(ptr<i8>, i32)>

// CHECK-LABEL: llvm.func @resumeNoLandingpadValue(
// CHECK-SAME: %[[ARG:.*]]: i32)
llvm.func @resumeNoLandingpadValue(%arg0: i32) -> i32 attributes { personality = @__gxx_personality_v0 } {
// CHECK: %[[VAL_0:.*]] = llvm.call @foo2() : () -> !llvm.struct<(ptr<i8>, i32)>
// CHECK: %[[VAL_1:.*]] = llvm.invoke @foo(%[[ARG]]) to ^[[BB1:.*]] unwind ^[[BB2:.*]] : (i32) -> i32
%0 = llvm.call @foo2() : () -> !llvm.struct<(ptr<i8>, i32)>
%1 = llvm.invoke @foo(%arg0) to ^bb1 unwind ^bb2 : (i32) -> i32
// CHECK: ^[[BB1]]:
// CHECK: llvm.return %[[VAL_1]] : i32
^bb1: // pred: ^bb0
llvm.return %1 : i32
// CHECK: ^[[BB2]]:
// CHECK: %[[VAL_2:.*]] = llvm.landingpad cleanup : !llvm.struct<(ptr<i8>, i32)>
// CHECK: llvm.resume %[[VAL_0]] : !llvm.struct<(ptr<i8>, i32)>
^bb2: // pred: ^bb0
%2 = llvm.landingpad cleanup : !llvm.struct<(ptr<i8>, i32)>
llvm.resume %0 : !llvm.struct<(ptr<i8>, i32)>
}

// CHECK-LABEL: @useFreezeOp
func.func @useFreezeOp(%arg0: i32) {
// CHECK: = llvm.freeze %[[ARG0:.*]] : i32
Expand Down