-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[CIR] Upstream the CatchParamOp #165110
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?
[CIR] Upstream the CatchParamOp #165110
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 |
|---|---|---|
|
|
@@ -3096,6 +3096,23 @@ static mlir::ParseResult parseTryHandlerRegions( | |
| return mlir::success(); | ||
| } | ||
|
|
||
| //===----------------------------------------------------------------------===// | ||
| // CatchParamOp | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| LogicalResult cir::CatchParamOp::verify() { | ||
|
Contributor
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. Is it true that a begin param should always be post-dominated by an end param and every end param should be dominated by a begin param? Is there a practical way to verify that here? |
||
| std::optional<cir::CatchParamKind> kind = getKind(); | ||
| if (getExceptionPtr()) { | ||
| if (!kind || *kind != cir::CatchParamKind::Begin) | ||
| return emitOpError("with exception pointer must be of `begin` kind"); | ||
| return success(); | ||
| } | ||
|
|
||
| if (!kind && !(*this)->getParentOfType<cir::TryOp>()) | ||
| return emitOpError("without `kind` requires `cir.try` surrounding scope"); | ||
| return success(); | ||
| } | ||
|
|
||
| //===----------------------------------------------------------------------===// | ||
| // TableGen'd op method definitions | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -2981,6 +2981,35 @@ mlir::LogicalResult CIRToLLVMThrowOpLowering::matchAndRewrite( | |||||||
| return mlir::success(); | ||||||||
| } | ||||||||
|
|
||||||||
| mlir::LogicalResult CIRToLLVMCatchParamOpLowering::matchAndRewrite( | ||||||||
|
Contributor
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. Is this tested here? |
||||||||
| cir::CatchParamOp op, OpAdaptor adaptor, | ||||||||
| mlir::ConversionPatternRewriter &rewriter) const { | ||||||||
| std::optional<cir::CatchParamKind> kind = op.getKind(); | ||||||||
| if (!kind) | ||||||||
| llvm_unreachable("only begin/end supposed to make to lowering stage"); | ||||||||
|
Comment on lines
+2988
to
+2989
Contributor
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.
Suggested change
|
||||||||
|
|
||||||||
| if (kind == cir::CatchParamKind::Begin) { | ||||||||
| // Get or create `declare ptr @__cxa_begin_catch(ptr)` | ||||||||
| const llvm::StringRef fnName = "__cxa_begin_catch"; | ||||||||
| auto llvmPtrTy = mlir::LLVM::LLVMPointerType::get(rewriter.getContext()); | ||||||||
| auto fnTy = mlir::LLVM::LLVMFunctionType::get(llvmPtrTy, {llvmPtrTy}); | ||||||||
| createLLVMFuncOpIfNotExist(rewriter, op, fnName, fnTy); | ||||||||
| rewriter.replaceOpWithNewOp<mlir::LLVM::CallOp>( | ||||||||
| op, mlir::TypeRange{llvmPtrTy}, fnName, | ||||||||
| mlir::ValueRange{adaptor.getExceptionPtr()}); | ||||||||
| return mlir::success(); | ||||||||
| } | ||||||||
|
|
||||||||
| // Get or create `declare void @__cxa_end_catch()` | ||||||||
| const llvm::StringRef fnName = "__cxa_end_catch"; | ||||||||
| auto voidTy = mlir::LLVM::LLVMVoidType::get(rewriter.getContext()); | ||||||||
| auto fnTy = mlir::LLVM::LLVMFunctionType::get(voidTy, {}); | ||||||||
| createLLVMFuncOpIfNotExist(rewriter, op, fnName, fnTy); | ||||||||
| rewriter.replaceOpWithNewOp<mlir::LLVM::CallOp>(op, mlir::TypeRange{}, fnName, | ||||||||
| mlir::ValueRange{}); | ||||||||
| return mlir::success(); | ||||||||
| } | ||||||||
|
|
||||||||
| mlir::LogicalResult CIRToLLVMAllocExceptionOpLowering::matchAndRewrite( | ||||||||
| cir::AllocExceptionOp op, OpAdaptor adaptor, | ||||||||
| mlir::ConversionPatternRewriter &rewriter) const { | ||||||||
|
|
||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // RUN: cir-opt %s --verify-roundtrip | FileCheck %s | ||
|
|
||
| !s32i = !cir.int<s, 32> | ||
| !void = !cir.void | ||
|
|
||
| module { | ||
|
|
||
| cir.func dso_local @catch_param_inside_catch() { | ||
| cir.scope { | ||
| cir.try { | ||
| cir.yield | ||
| } catch all { | ||
| cir.catch_param : !cir.ptr<!void> | ||
| cir.yield | ||
| } | ||
| } | ||
| cir.return | ||
| } | ||
|
|
||
| // CHECK: cir.func dso_local @catch_param_inside_catch() { | ||
| // CHECK: cir.scope { | ||
| // CHECK: cir.try { | ||
| // CHECK: cir.yield | ||
| // CHECK: } catch all { | ||
| // CHECK: cir.catch_param : !cir.ptr<!void> | ||
| // CHECK: cir.yield | ||
| // CHECK: } | ||
| // CHECK: } | ||
| // CHECK: cir.return | ||
| // CHECK: } | ||
|
|
||
| cir.func dso_local @catch_begin_and_end() { | ||
| %exn_addr = cir.alloca !cir.ptr<!void>, !cir.ptr<!cir.ptr<!void>>, ["exn_addr"] | ||
| %tmp_exn_ptr = cir.load %exn_addr : !cir.ptr<!cir.ptr<!void>>, !cir.ptr<!void> | ||
| cir.br ^bb1(%tmp_exn_ptr : !cir.ptr<!void>) | ||
| ^bb1(%exn_ptr : !cir.ptr<!void>): | ||
| %begin = cir.catch_param begin %exn_ptr : !cir.ptr<!s32i> | ||
| cir.catch_param end | ||
| cir.br ^bb2 | ||
| ^bb2: | ||
| cir.return | ||
| } | ||
|
|
||
| // CHECK: cir.func dso_local @catch_begin_and_end() { | ||
| // CHECK: %[[EXN_ADDR:.*]] = cir.alloca !cir.ptr<!void>, !cir.ptr<!cir.ptr<!void>>, ["exn_addr"] | ||
| // CHECK: %[[TMP_EXN_PTR:.*]] = cir.load %[[EXN_ADDR]] : !cir.ptr<!cir.ptr<!void>>, !cir.ptr<!void> | ||
| // CHECK: cir.br ^bb1(%[[TMP_EXN_PTR]] : !cir.ptr<!void>) | ||
| // CHECK: ^bb1(%[[EXN_PTR:.*]]: !cir.ptr<!void>): | ||
| // CHECK: %[[BEGIN:.*]] = cir.catch_param begin %[[EXN_PTR]] : !cir.ptr<!s32i> | ||
| // CHECK: cir.catch_param end | ||
| // CHECK: cir.br ^bb2 | ||
| // CHECK: ^bb2: | ||
| // CHECK: cir.return | ||
| // CHECK: } | ||
|
|
||
| } | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| // RUN: cir-opt %s -verify-diagnostics -split-input-file | ||
|
|
||
AmrDeveloper marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| !s32i = !cir.int<s, 32> | ||
| !void = !cir.void | ||
|
|
||
| module { | ||
|
|
||
| cir.func dso_local @catch_param_without_kind_and_without_try_scope() { | ||
| // expected-error @below {{op without `kind` requires `cir.try` surrounding scope}} | ||
| %0 = cir.catch_param : !cir.ptr<!void> | ||
| cir.return | ||
| } | ||
|
|
||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| !s32i = !cir.int<s, 32> | ||
| !void = !cir.void | ||
|
|
||
| module { | ||
|
|
||
| cir.func private @division() -> !s32i | ||
| cir.func dso_local @catch_param_with_exception_ptr_but_without_kind() { | ||
| cir.scope { | ||
| cir.try { | ||
| %0 = cir.call @division() : () -> !s32i | ||
| cir.yield | ||
| } catch all { | ||
| %0 = cir.const #cir.ptr<null> : !cir.ptr<!void> | ||
| // expected-error @below {{op with exception pointer must be of `begin` kind}} | ||
| %1 = cir.catch_param %0 : !cir.ptr<!void> | ||
| cir.yield | ||
| } | ||
| } | ||
| cir.return | ||
| } | ||
|
|
||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| !s32i = !cir.int<s, 32> | ||
| !void = !cir.void | ||
|
|
||
| module { | ||
|
|
||
| cir.func private @division() -> !s32i | ||
| cir.func dso_local @catch_param_with_exception_ptr_but_with_end_kind() { | ||
| cir.scope { | ||
| cir.try { | ||
| %0 = cir.call @division() : () -> !s32i | ||
| cir.yield | ||
| } catch all { | ||
| %0 = cir.const #cir.ptr<null> : !cir.ptr<!void> | ||
| // expected-error @below {{'cir.catch_param' op with exception pointer must be of `begin` kind}} | ||
| %1 = cir.catch_param end %0 : !cir.ptr<!void> | ||
| cir.yield | ||
| } | ||
| } | ||
| cir.return | ||
| } | ||
|
|
||
| } | ||
|
|
||
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.
It might be worth expanding on how the two modes are used (one that assumes structured control and the other one for lower level lowering). An alternative design would be split this into two operations (let's see if others have any strong opinions here).
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.
Happy to approve this PR once this comment is addressed
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.
I'd prefer to have two different operations for the modes. They seem to mean two entirely different things.
Is the begin/end mode used on Windows?
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.
good point (I'm not sure)