@@ -3002,6 +3002,88 @@ mlir::LogicalResult CIRToLLVMAllocExceptionOpLowering::matchAndRewrite(
30023002 return mlir::success ();
30033003}
30043004
3005+ static mlir::LLVM::LLVMStructType
3006+ getLLVMLandingPadStructTy (mlir::ConversionPatternRewriter &rewriter) {
3007+ // Create the landing pad type: struct { ptr, i32 }
3008+ mlir::MLIRContext *ctx = rewriter.getContext ();
3009+ auto llvmPtr = mlir::LLVM::LLVMPointerType::get (ctx);
3010+ llvm::SmallVector<mlir::Type> structFields = {llvmPtr, rewriter.getI32Type ()};
3011+ return mlir::LLVM::LLVMStructType::getLiteral (ctx, structFields);
3012+ }
3013+
3014+ mlir::LogicalResult CIRToLLVMEhInflightOpLowering::matchAndRewrite (
3015+ cir::EhInflightOp op, OpAdaptor adaptor,
3016+ mlir::ConversionPatternRewriter &rewriter) const {
3017+ auto llvmFn = op->getParentOfType <mlir::LLVM::LLVMFuncOp>();
3018+ assert (llvmFn && " expected LLVM function parent" );
3019+ mlir::Block *entryBlock = &llvmFn.getRegion ().front ();
3020+ assert (entryBlock->isEntryBlock ());
3021+
3022+ mlir::ArrayAttr symListAttr = op.getSymTypeListAttr ();
3023+ mlir::SmallVector<mlir::Value, 4 > symAddrs;
3024+
3025+ auto llvmPtrTy = mlir::LLVM::LLVMPointerType::get (rewriter.getContext ());
3026+ mlir::Location loc = op.getLoc ();
3027+
3028+ // %landingpad = landingpad { ptr, i32 }
3029+ // Note that since llvm.landingpad has to be the first operation on the
3030+ // block, any needed value for its operands has to be added somewhere else.
3031+ if (symListAttr) {
3032+ // catch ptr @_ZTIi
3033+ // catch ptr @_ZTIPKc
3034+ for (mlir::Attribute attr : symListAttr) {
3035+ auto symAttr = cast<mlir::FlatSymbolRefAttr>(attr);
3036+ // Generate `llvm.mlir.addressof` for each symbol, and place those
3037+ // operations in the LLVM function entry basic block.
3038+ mlir::OpBuilder::InsertionGuard guard (rewriter);
3039+ rewriter.setInsertionPointToStart (entryBlock);
3040+ mlir::Value addrOp = mlir::LLVM::AddressOfOp::create (
3041+ rewriter, loc, llvmPtrTy, symAttr.getValue ());
3042+ symAddrs.push_back (addrOp);
3043+ }
3044+ } else if (!op.getCleanup ()) {
3045+ // catch ptr null
3046+ mlir::OpBuilder::InsertionGuard guard (rewriter);
3047+ rewriter.setInsertionPointToStart (entryBlock);
3048+ mlir::Value nullOp = mlir::LLVM::ZeroOp::create (rewriter, loc, llvmPtrTy);
3049+ symAddrs.push_back (nullOp);
3050+ }
3051+
3052+ // %slot = extractvalue { ptr, i32 } %x, 0
3053+ // %selector = extractvalue { ptr, i32 } %x, 1
3054+ mlir::LLVM::LLVMStructType llvmLandingPadStructTy =
3055+ getLLVMLandingPadStructTy (rewriter);
3056+ auto landingPadOp = mlir::LLVM::LandingpadOp::create (
3057+ rewriter, loc, llvmLandingPadStructTy, symAddrs);
3058+
3059+ if (op.getCleanup ())
3060+ landingPadOp.setCleanup (true );
3061+
3062+ mlir::Value slot =
3063+ mlir::LLVM::ExtractValueOp::create (rewriter, loc, landingPadOp, 0 );
3064+ mlir::Value selector =
3065+ mlir::LLVM::ExtractValueOp::create (rewriter, loc, landingPadOp, 1 );
3066+ rewriter.replaceOp (op, mlir::ValueRange{slot, selector});
3067+
3068+ // Landing pads are required to be in LLVM functions with personality
3069+ // attribute.
3070+ // TODO(cir): for now hardcode personality creation in order to start
3071+ // adding exception tests, once we annotate CIR with such information,
3072+ // change it to be in FuncOp lowering instead.
3073+ mlir::OpBuilder::InsertionGuard guard (rewriter);
3074+ // Insert personality decl before the current function.
3075+ rewriter.setInsertionPoint (llvmFn);
3076+ auto personalityFnTy =
3077+ mlir::LLVM::LLVMFunctionType::get (rewriter.getI32Type (), {},
3078+ /* isVarArg=*/ true );
3079+ // Get or create `__gxx_personality_v0`
3080+ const StringRef fnName = " __gxx_personality_v0" ;
3081+ createLLVMFuncOpIfNotExist (rewriter, op, fnName, personalityFnTy);
3082+ llvmFn.setPersonality (fnName);
3083+
3084+ return mlir::success ();
3085+ }
3086+
30053087mlir::LogicalResult CIRToLLVMTrapOpLowering::matchAndRewrite (
30063088 cir::TrapOp op, OpAdaptor adaptor,
30073089 mlir::ConversionPatternRewriter &rewriter) const {
0 commit comments