-
Notifications
You must be signed in to change notification settings - Fork 15k
[CIR] Upstream Exception EhInflight op #165621
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
Open
AmrDeveloper
wants to merge
1
commit into
llvm:main
Choose a base branch
from
AmrDeveloper:cir_exception_eh_inflight_op
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
+126
−0
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
@llvm/pr-subscribers-clang @llvm/pr-subscribers-clangir Author: Amr Hesham (AmrDeveloper) ChangesUpstream Exception EhInflight op as a prerequisite for full catch handlers implementation Issue #154992 Full diff: https://github.com/llvm/llvm-project/pull/165621.diff 3 Files Affected:
diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index 2b361ed0982c6..0497d97239000 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -4490,6 +4490,35 @@ def CIR_TryOp : CIR_Op<"try",[
let hasLLVMLowering = false;
}
+//===----------------------------------------------------------------------===//
+// Exception related: EhInflightOp
+//===----------------------------------------------------------------------===//
+
+def CIR_EhInflightOp : CIR_Op<"eh.inflight_exception"> {
+ let summary = "Materialize the catch clause formal parameter";
+ let description = [{
+ `cir.eh.inflight_exception` returns two values:
+ - `exception_ptr`: The exception pointer for the inflight exception
+ - `type_id`: pointer to the exception object
+ This operation is expected to be the first one basic blocks on the
+ exception path out of `cir.try_call` operations.
+
+ The `cleanup` attribute indicates that clean up code might run before the
+ values produced by this operation are used to gather exception information.
+ This helps CIR to pass down more accurate information for LLVM lowering
+ to landingpads.
+ }];
+
+ let arguments = (ins UnitAttr:$cleanup,
+ OptionalAttr<FlatSymbolRefArrayAttr>:$sym_type_list);
+ let results = (outs CIR_VoidPtrType:$exception_ptr, CIR_UInt32:$type_id);
+ let assemblyFormat = [{
+ (`cleanup` $cleanup^)?
+ ($sym_type_list^)?
+ attr-dict
+ }];
+}
+
//===----------------------------------------------------------------------===//
// Atomic operations
//===----------------------------------------------------------------------===//
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index 5a6193fa8d840..0bf5eb740096f 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -3002,6 +3002,88 @@ mlir::LogicalResult CIRToLLVMAllocExceptionOpLowering::matchAndRewrite(
return mlir::success();
}
+static mlir::LLVM::LLVMStructType
+getLLVMLandingPadStructTy(mlir::ConversionPatternRewriter &rewriter) {
+ // Create the landing pad type: struct { ptr, i32 }
+ mlir::MLIRContext *ctx = rewriter.getContext();
+ auto llvmPtr = mlir::LLVM::LLVMPointerType::get(ctx);
+ llvm::SmallVector<mlir::Type> structFields = {llvmPtr, rewriter.getI32Type()};
+ return mlir::LLVM::LLVMStructType::getLiteral(ctx, structFields);
+}
+
+mlir::LogicalResult CIRToLLVMEhInflightOpLowering::matchAndRewrite(
+ cir::EhInflightOp op, OpAdaptor adaptor,
+ mlir::ConversionPatternRewriter &rewriter) const {
+ auto llvmFn = op->getParentOfType<mlir::LLVM::LLVMFuncOp>();
+ assert(llvmFn && "expected LLVM function parent");
+ mlir::Block *entryBlock = &llvmFn.getRegion().front();
+ assert(entryBlock->isEntryBlock());
+
+ mlir::ArrayAttr symListAttr = op.getSymTypeListAttr();
+ mlir::SmallVector<mlir::Value, 4> symAddrs;
+
+ auto llvmPtrTy = mlir::LLVM::LLVMPointerType::get(rewriter.getContext());
+ mlir::Location loc = op.getLoc();
+
+ // %landingpad = landingpad { ptr, i32 }
+ // Note that since llvm.landingpad has to be the first operation on the
+ // block, any needed value for its operands has to be added somewhere else.
+ if (symListAttr) {
+ // catch ptr @_ZTIi
+ // catch ptr @_ZTIPKc
+ for (mlir::Attribute attr : symListAttr) {
+ auto symAttr = cast<mlir::FlatSymbolRefAttr>(attr);
+ // Generate `llvm.mlir.addressof` for each symbol, and place those
+ // operations in the LLVM function entry basic block.
+ mlir::OpBuilder::InsertionGuard guard(rewriter);
+ rewriter.setInsertionPointToStart(entryBlock);
+ mlir::Value addrOp = mlir::LLVM::AddressOfOp::create(
+ rewriter, loc, llvmPtrTy, symAttr.getValue());
+ symAddrs.push_back(addrOp);
+ }
+ } else if (!op.getCleanup()) {
+ // catch ptr null
+ mlir::OpBuilder::InsertionGuard guard(rewriter);
+ rewriter.setInsertionPointToStart(entryBlock);
+ mlir::Value nullOp = mlir::LLVM::ZeroOp::create(rewriter, loc, llvmPtrTy);
+ symAddrs.push_back(nullOp);
+ }
+
+ // %slot = extractvalue { ptr, i32 } %x, 0
+ // %selector = extractvalue { ptr, i32 } %x, 1
+ mlir::LLVM::LLVMStructType llvmLandingPadStructTy =
+ getLLVMLandingPadStructTy(rewriter);
+ auto landingPadOp = mlir::LLVM::LandingpadOp::create(
+ rewriter, loc, llvmLandingPadStructTy, symAddrs);
+
+ if (op.getCleanup())
+ landingPadOp.setCleanup(true);
+
+ mlir::Value slot =
+ mlir::LLVM::ExtractValueOp::create(rewriter, loc, landingPadOp, 0);
+ mlir::Value selector =
+ mlir::LLVM::ExtractValueOp::create(rewriter, loc, landingPadOp, 1);
+ rewriter.replaceOp(op, mlir::ValueRange{slot, selector});
+
+ // Landing pads are required to be in LLVM functions with personality
+ // attribute.
+ // TODO(cir): for now hardcode personality creation in order to start
+ // adding exception tests, once we annotate CIR with such information,
+ // change it to be in FuncOp lowering instead.
+ mlir::OpBuilder::InsertionGuard guard(rewriter);
+ // Insert personality decl before the current function.
+ rewriter.setInsertionPoint(llvmFn);
+ auto personalityFnTy =
+ mlir::LLVM::LLVMFunctionType::get(rewriter.getI32Type(), {},
+ /*isVarArg=*/true);
+ // Get or create `__gxx_personality_v0`
+ const StringRef fnName = "__gxx_personality_v0";
+ createLLVMFuncOpIfNotExist(rewriter, op, fnName, personalityFnTy);
+ llvmFn.setPersonality(fnName);
+
+ return mlir::success();
+}
+
mlir::LogicalResult CIRToLLVMTrapOpLowering::matchAndRewrite(
cir::TrapOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const {
diff --git a/clang/test/CIR/IR/eh-inflight.cir b/clang/test/CIR/IR/eh-inflight.cir
new file mode 100644
index 0000000000000..90a5f3f67b6fe
--- /dev/null
+++ b/clang/test/CIR/IR/eh-inflight.cir
@@ -0,0 +1,15 @@
+// RUN: cir-opt %s --verify-roundtrip | FileCheck %s
+
+module {
+
+cir.func dso_local @function_with_inflight_exception() {
+ %exception_ptr, %type_id = cir.eh.inflight_exception
+ cir.return
+}
+
+// CHECK: cir.func dso_local @function_with_inflight_exception() {
+// CHECK: %exception_ptr, %type_id = cir.eh.inflight_exception
+// CHECK: cir.return
+// CHECK: }
+
+}
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Upstream Exception EhInflight op as a prerequisite for full catch handlers implementation
Issue #154992