From b1f7552288a3e98c934bb2c605f35e8e8cc439d6 Mon Sep 17 00:00:00 2001 From: Sirui Mu Date: Fri, 26 Apr 2024 06:09:16 +0800 Subject: [PATCH] [CIR][NFC] Create LLVM intrinsic calls through `createCallLLVMIntrinsicOp` (#564) This PR does not introduce any functional changes. It cleans up code in `LowerToLLVM.cpp` and creates all LLVM intrinsic calls through the unified `createCallLLVMIntrinsicOp` function, as suggested by [this comment](https://github.com/llvm/clangir/pull/556#discussion_r1575198259) in #556 . Some LLVM intrinsics already have specialized LLVMIR operations. CIR operations that depend on these intrinsics are lowered to those specialized operations rather than `llvm.call_intrinsic` operation. --- .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 26 ++++++++++--------- clang/test/CIR/Lowering/bswap.cir | 2 +- clang/test/CIR/Lowering/intrinsics.cir | 2 +- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 7a73c170b94f..d26faa2a4fb2 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -1995,6 +1995,16 @@ createCallLLVMIntrinsicOp(mlir::ConversionPatternRewriter &rewriter, loc, resultTy, intrinsicNameAttr, operands); } +static mlir::LLVM::CallIntrinsicOp replaceOpWithCallLLVMIntrinsicOp( + mlir::ConversionPatternRewriter &rewriter, mlir::Operation *op, + const llvm::Twine &intrinsicName, mlir::Type resultTy, + mlir::ValueRange operands) { + auto callIntrinOp = createCallLLVMIntrinsicOp( + rewriter, op->getLoc(), intrinsicName, resultTy, operands); + rewriter.replaceOp(op, callIntrinOp.getOperation()); + return callIntrinOp; +} + static mlir::Value createLLVMBitOp(mlir::Location loc, const llvm::Twine &llvmIntrinBaseName, mlir::Type resultTy, mlir::Value operand, @@ -2079,16 +2089,14 @@ class CIRObjSizeOpLowering auto llvmResTy = getTypeConverter()->convertType(op.getType()); auto loc = op->getLoc(); - auto llvmIntrinNameAttr = - mlir::StringAttr::get(rewriter.getContext(), "llvm.objectsize"); mlir::cir::SizeInfoType kindInfo = op.getKind(); auto falseValue = rewriter.create( loc, rewriter.getI1Type(), false); auto trueValue = rewriter.create( loc, rewriter.getI1Type(), true); - rewriter.replaceOpWithNewOp( - op, llvmResTy, llvmIntrinNameAttr, + replaceOpWithCallLLVMIntrinsicOp( + rewriter, op, "llvm.objectsize", llvmResTy, mlir::ValueRange{adaptor.getPtr(), kindInfo == mlir::cir::SizeInfoType::max ? falseValue : trueValue, @@ -2467,11 +2475,8 @@ class CIRByteswapOpLowering std::string llvmIntrinName = "llvm.bswap.i"; llvmIntrinName.append(std::to_string(resTy.getWidth())); - auto llvmIntrinNameAttr = - mlir::StringAttr::get(rewriter.getContext(), llvmIntrinName); - rewriter.replaceOpWithNewOp( - op, resTy, llvmIntrinNameAttr, adaptor.getInput()); + rewriter.replaceOpWithNewOp(op, adaptor.getInput()); return mlir::LogicalResult::success(); } @@ -2682,10 +2687,7 @@ class CIRTrapLowering : public mlir::OpConversionPattern { auto loc = op->getLoc(); rewriter.eraseOp(op); - auto llvmTrapIntrinsicType = - mlir::LLVM::LLVMVoidType::get(op->getContext()); - rewriter.create( - loc, llvmTrapIntrinsicType, "llvm.trap", mlir::ValueRange{}); + rewriter.create(loc); // Note that the call to llvm.trap is not a terminator in LLVM dialect. // So we must emit an additional llvm.unreachable to terminate the current diff --git a/clang/test/CIR/Lowering/bswap.cir b/clang/test/CIR/Lowering/bswap.cir index 7e778820a131..7733b4de1dae 100644 --- a/clang/test/CIR/Lowering/bswap.cir +++ b/clang/test/CIR/Lowering/bswap.cir @@ -9,7 +9,7 @@ cir.func @test(%arg0: !u32i) -> !u32i { } // MLIR: llvm.func @test(%arg0: i32) -> i32 -// MLIR-NEXT: %0 = llvm.call_intrinsic "llvm.bswap.i32"(%arg0) : (i32) -> i32 +// MLIR-NEXT: %0 = llvm.intr.bswap(%arg0) : (i32) -> i32 // MLIR-NEXT: llvm.return %0 : i32 // MLIR-NEXT: } diff --git a/clang/test/CIR/Lowering/intrinsics.cir b/clang/test/CIR/Lowering/intrinsics.cir index ddf8e0708ad4..25b0b34738bc 100644 --- a/clang/test/CIR/Lowering/intrinsics.cir +++ b/clang/test/CIR/Lowering/intrinsics.cir @@ -14,7 +14,7 @@ module { } // MLIR: llvm.func @test_trap() - // MLIR-NEXT: llvm.call_intrinsic "llvm.trap"() : () -> !llvm.void + // MLIR-NEXT: "llvm.intr.trap"() : () -> () // MLIR-NEXT: llvm.unreachable // LLVM: define void @test_trap()