Skip to content

Commit c8d4b0f

Browse files
bcardosolopeslanza
authored andcommitted
[CIR][LowerToLLVM][NFC] Exceptions: use getOrCreateLLVMFuncOp to create personality functions
While here, cleanup getOrCreateLLVMFuncOp usaga a bit.
1 parent 762752b commit c8d4b0f

File tree

1 file changed

+24
-31
lines changed

1 file changed

+24
-31
lines changed

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,22 @@ lowerCIRVisibilityToLLVMVisibility(mlir::cir::VisibilityKind visibilityKind) {
161161
return ::mlir::LLVM::Visibility::Protected;
162162
}
163163
}
164+
165+
// Make sure the LLVM function we are about to create a call for actually
166+
// exists, if not create one. Returns a function
167+
void getOrCreateLLVMFuncOp(mlir::ConversionPatternRewriter &rewriter,
168+
mlir::Operation *srcOp, llvm::StringRef fnName,
169+
mlir::Type fnTy) {
170+
auto modOp = srcOp->getParentOfType<mlir::ModuleOp>();
171+
auto enclosingFnOp = srcOp->getParentOfType<mlir::LLVM::LLVMFuncOp>();
172+
auto *sourceSymbol = mlir::SymbolTable::lookupSymbolIn(modOp, fnName);
173+
if (!sourceSymbol) {
174+
mlir::OpBuilder::InsertionGuard guard(rewriter);
175+
rewriter.setInsertionPoint(enclosingFnOp);
176+
rewriter.create<mlir::LLVM::LLVMFuncOp>(srcOp->getLoc(), fnName, fnTy);
177+
}
178+
}
179+
164180
} // namespace
165181

166182
//===----------------------------------------------------------------------===//
@@ -1041,9 +1057,10 @@ class CIREhInflightOpLowering
10411057
auto personalityFnTy =
10421058
mlir::LLVM::LLVMFunctionType::get(rewriter.getI32Type(), {},
10431059
/*isVarArg=*/true);
1044-
auto personalityFn = rewriter.create<mlir::LLVM::LLVMFuncOp>(
1045-
loc, "__gxx_personality_v0", personalityFnTy);
1046-
llvmFn.setPersonality(personalityFn.getName());
1060+
// Get or create `__gxx_personality_v0`
1061+
StringRef fnName = "__gxx_personality_v0";
1062+
getOrCreateLLVMFuncOp(rewriter, op, fnName, personalityFnTy);
1063+
llvmFn.setPersonality(fnName);
10471064
}
10481065
return mlir::success();
10491066
}
@@ -3640,20 +3657,6 @@ class CIREhTypeIdOpLowering
36403657
}
36413658
};
36423659

3643-
// Make sure the LLVM function we are about to create a call for actually
3644-
// exists, if not create one. Returns a function
3645-
void getOrCreateLLVMFuncOp(mlir::ConversionPatternRewriter &rewriter,
3646-
mlir::Location loc, mlir::ModuleOp mod,
3647-
mlir::LLVM::LLVMFuncOp enclosingfnOp,
3648-
llvm::StringRef fnName, mlir::Type fnTy) {
3649-
auto *sourceSymbol = mlir::SymbolTable::lookupSymbolIn(mod, fnName);
3650-
if (!sourceSymbol) {
3651-
mlir::OpBuilder::InsertionGuard guard(rewriter);
3652-
rewriter.setInsertionPoint(enclosingfnOp);
3653-
rewriter.create<mlir::LLVM::LLVMFuncOp>(loc, fnName, fnTy);
3654-
}
3655-
}
3656-
36573660
class CIRCatchParamOpLowering
36583661
: public mlir::OpConversionPattern<mlir::cir::CatchParamOp> {
36593662
public:
@@ -3662,16 +3665,13 @@ class CIRCatchParamOpLowering
36623665
mlir::LogicalResult
36633666
matchAndRewrite(mlir::cir::CatchParamOp op, OpAdaptor adaptor,
36643667
mlir::ConversionPatternRewriter &rewriter) const override {
3665-
auto modOp = op->getParentOfType<mlir::ModuleOp>();
3666-
auto enclosingFnOp = op->getParentOfType<mlir::LLVM::LLVMFuncOp>();
36673668
if (op.isBegin()) {
36683669
// Get or create `declare ptr @__cxa_begin_catch(ptr)`
36693670
StringRef fnName = "__cxa_begin_catch";
36703671
auto llvmPtrTy = mlir::LLVM::LLVMPointerType::get(rewriter.getContext());
36713672
auto fnTy = mlir::LLVM::LLVMFunctionType::get(llvmPtrTy, {llvmPtrTy},
36723673
/*isVarArg=*/false);
3673-
getOrCreateLLVMFuncOp(rewriter, op.getLoc(), modOp, enclosingFnOp, fnName,
3674-
fnTy);
3674+
getOrCreateLLVMFuncOp(rewriter, op, fnName, fnTy);
36753675
rewriter.replaceOpWithNewOp<mlir::LLVM::CallOp>(
36763676
op, mlir::TypeRange{llvmPtrTy}, fnName,
36773677
mlir::ValueRange{adaptor.getExceptionPtr()});
@@ -3681,8 +3681,7 @@ class CIRCatchParamOpLowering
36813681
auto fnTy = mlir::LLVM::LLVMFunctionType::get(
36823682
mlir::LLVM::LLVMVoidType::get(rewriter.getContext()), {},
36833683
/*isVarArg=*/false);
3684-
getOrCreateLLVMFuncOp(rewriter, op.getLoc(), modOp, enclosingFnOp, fnName,
3685-
fnTy);
3684+
getOrCreateLLVMFuncOp(rewriter, op, fnName, fnTy);
36863685
rewriter.create<mlir::LLVM::CallOp>(op.getLoc(), mlir::TypeRange{},
36873686
fnName, mlir::ValueRange{});
36883687
rewriter.eraseOp(op);
@@ -3730,14 +3729,11 @@ class CIRAllocExceptionOpLowering
37303729
mlir::ConversionPatternRewriter &rewriter) const override {
37313730
// Get or create `declare ptr @__cxa_allocate_exception(i64)`
37323731
StringRef fnName = "__cxa_allocate_exception";
3733-
auto modOp = op->getParentOfType<mlir::ModuleOp>();
3734-
auto enclosingFnOp = op->getParentOfType<mlir::LLVM::LLVMFuncOp>();
37353732
auto llvmPtrTy = mlir::LLVM::LLVMPointerType::get(rewriter.getContext());
37363733
auto int64Ty = mlir::IntegerType::get(rewriter.getContext(), 64);
37373734
auto fnTy = mlir::LLVM::LLVMFunctionType::get(llvmPtrTy, {int64Ty},
37383735
/*isVarArg=*/false);
3739-
getOrCreateLLVMFuncOp(rewriter, op.getLoc(), modOp, enclosingFnOp, fnName,
3740-
fnTy);
3736+
getOrCreateLLVMFuncOp(rewriter, op, fnName, fnTy);
37413737
auto size = rewriter.create<mlir::LLVM::ConstantOp>(op.getLoc(),
37423738
adaptor.getSizeAttr());
37433739
rewriter.replaceOpWithNewOp<mlir::LLVM::CallOp>(
@@ -3756,15 +3752,12 @@ class CIRThrowOpLowering
37563752
mlir::ConversionPatternRewriter &rewriter) const override {
37573753
// Get or create `declare void @__cxa_throw(ptr, ptr, ptr)`
37583754
StringRef fnName = "__cxa_throw";
3759-
auto modOp = op->getParentOfType<mlir::ModuleOp>();
3760-
auto enclosingFnOp = op->getParentOfType<mlir::LLVM::LLVMFuncOp>();
37613755
auto llvmPtrTy = mlir::LLVM::LLVMPointerType::get(rewriter.getContext());
37623756
auto voidTy = mlir::LLVM::LLVMVoidType::get(rewriter.getContext());
37633757
auto fnTy = mlir::LLVM::LLVMFunctionType::get(
37643758
voidTy, {llvmPtrTy, llvmPtrTy, llvmPtrTy},
37653759
/*isVarArg=*/false);
3766-
getOrCreateLLVMFuncOp(rewriter, op.getLoc(), modOp, enclosingFnOp, fnName,
3767-
fnTy);
3760+
getOrCreateLLVMFuncOp(rewriter, op, fnName, fnTy);
37683761
mlir::Value typeInfo = rewriter.create<mlir::LLVM::AddressOfOp>(
37693762
op.getLoc(), mlir::LLVM::LLVMPointerType::get(rewriter.getContext()),
37703763
adaptor.getTypeInfoAttr());

0 commit comments

Comments
 (0)