@@ -3065,6 +3065,88 @@ mlir::LogicalResult CIRToLLVMAllocExceptionOpLowering::matchAndRewrite(
30653065 return mlir::success ();
30663066}
30673067
3068+ static mlir::LLVM::LLVMStructType
3069+ getLLVMLandingPadStructTy (mlir::ConversionPatternRewriter &rewriter) {
3070+ // Create the landing pad type: struct { ptr, i32 }
3071+ mlir::MLIRContext *ctx = rewriter.getContext ();
3072+ auto llvmPtr = mlir::LLVM::LLVMPointerType::get (ctx);
3073+ llvm::SmallVector<mlir::Type> structFields = {llvmPtr, rewriter.getI32Type ()};
3074+ return mlir::LLVM::LLVMStructType::getLiteral (ctx, structFields);
3075+ }
3076+
3077+ mlir::LogicalResult CIRToLLVMEhInflightOpLowering::matchAndRewrite (
3078+ cir::EhInflightOp op, OpAdaptor adaptor,
3079+ mlir::ConversionPatternRewriter &rewriter) const {
3080+ auto llvmFn = op->getParentOfType <mlir::LLVM::LLVMFuncOp>();
3081+ assert (llvmFn && " expected LLVM function parent" );
3082+ mlir::Block *entryBlock = &llvmFn.getRegion ().front ();
3083+ assert (entryBlock->isEntryBlock ());
3084+
3085+ mlir::ArrayAttr symListAttr = op.getSymTypeListAttr ();
3086+ mlir::SmallVector<mlir::Value, 4 > symAddrs;
3087+
3088+ auto llvmPtrTy = mlir::LLVM::LLVMPointerType::get (rewriter.getContext ());
3089+ mlir::Location loc = op.getLoc ();
3090+
3091+ // %landingpad = landingpad { ptr, i32 }
3092+ // Note that since llvm.landingpad has to be the first operation on the
3093+ // block, any needed value for its operands has to be added somewhere else.
3094+ if (symListAttr) {
3095+ // catch ptr @_ZTIi
3096+ // catch ptr @_ZTIPKc
3097+ for (mlir::Attribute attr : symListAttr) {
3098+ auto symAttr = cast<mlir::FlatSymbolRefAttr>(attr);
3099+ // Generate `llvm.mlir.addressof` for each symbol, and place those
3100+ // operations in the LLVM function entry basic block.
3101+ mlir::OpBuilder::InsertionGuard guard (rewriter);
3102+ rewriter.setInsertionPointToStart (entryBlock);
3103+ mlir::Value addrOp = mlir::LLVM::AddressOfOp::create (
3104+ rewriter, loc, llvmPtrTy, symAttr.getValue ());
3105+ symAddrs.push_back (addrOp);
3106+ }
3107+ } else if (!op.getCleanup ()) {
3108+ // catch ptr null
3109+ mlir::OpBuilder::InsertionGuard guard (rewriter);
3110+ rewriter.setInsertionPointToStart (entryBlock);
3111+ mlir::Value nullOp = mlir::LLVM::ZeroOp::create (rewriter, loc, llvmPtrTy);
3112+ symAddrs.push_back (nullOp);
3113+ }
3114+
3115+ // %slot = extractvalue { ptr, i32 } %x, 0
3116+ // %selector = extractvalue { ptr, i32 } %x, 1
3117+ mlir::LLVM::LLVMStructType llvmLandingPadStructTy =
3118+ getLLVMLandingPadStructTy (rewriter);
3119+ auto landingPadOp = mlir::LLVM::LandingpadOp::create (
3120+ rewriter, loc, llvmLandingPadStructTy, symAddrs);
3121+
3122+ if (op.getCleanup ())
3123+ landingPadOp.setCleanup (true );
3124+
3125+ mlir::Value slot =
3126+ mlir::LLVM::ExtractValueOp::create (rewriter, loc, landingPadOp, 0 );
3127+ mlir::Value selector =
3128+ mlir::LLVM::ExtractValueOp::create (rewriter, loc, landingPadOp, 1 );
3129+ rewriter.replaceOp (op, mlir::ValueRange{slot, selector});
3130+
3131+ // Landing pads are required to be in LLVM functions with personality
3132+ // attribute.
3133+ // TODO(cir): for now hardcode personality creation in order to start
3134+ // adding exception tests, once we annotate CIR with such information,
3135+ // change it to be in FuncOp lowering instead.
3136+ mlir::OpBuilder::InsertionGuard guard (rewriter);
3137+ // Insert personality decl before the current function.
3138+ rewriter.setInsertionPoint (llvmFn);
3139+ auto personalityFnTy =
3140+ mlir::LLVM::LLVMFunctionType::get (rewriter.getI32Type (), {},
3141+ /* isVarArg=*/ true );
3142+ // Get or create `__gxx_personality_v0`
3143+ const StringRef fnName = " __gxx_personality_v0" ;
3144+ createLLVMFuncOpIfNotExist (rewriter, op, fnName, personalityFnTy);
3145+ llvmFn.setPersonality (fnName);
3146+
3147+ return mlir::success ();
3148+ }
3149+
30683150mlir::LogicalResult CIRToLLVMTrapOpLowering::matchAndRewrite (
30693151 cir::TrapOp op, OpAdaptor adaptor,
30703152 mlir::ConversionPatternRewriter &rewriter) const {
0 commit comments