Skip to content

Commit

Permalink
[CIR][NFC] Create LLVM intrinsic calls through `createCallLLVMIntrins…
Browse files Browse the repository at this point in the history
…icOp` (llvm#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](llvm#556 (comment))
in llvm#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.
  • Loading branch information
Lancern authored and lanza committed Jun 20, 2024
1 parent adc96e8 commit b1f7552
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
26 changes: 14 additions & 12 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<mlir::LLVM::ConstantOp>(
loc, rewriter.getI1Type(), false);
auto trueValue = rewriter.create<mlir::LLVM::ConstantOp>(
loc, rewriter.getI1Type(), true);

rewriter.replaceOpWithNewOp<mlir::LLVM::CallIntrinsicOp>(
op, llvmResTy, llvmIntrinNameAttr,
replaceOpWithCallLLVMIntrinsicOp(
rewriter, op, "llvm.objectsize", llvmResTy,
mlir::ValueRange{adaptor.getPtr(),
kindInfo == mlir::cir::SizeInfoType::max ? falseValue
: trueValue,
Expand Down Expand Up @@ -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<mlir::LLVM::CallIntrinsicOp>(
op, resTy, llvmIntrinNameAttr, adaptor.getInput());
rewriter.replaceOpWithNewOp<mlir::LLVM::ByteSwapOp>(op, adaptor.getInput());

return mlir::LogicalResult::success();
}
Expand Down Expand Up @@ -2682,10 +2687,7 @@ class CIRTrapLowering : public mlir::OpConversionPattern<mlir::cir::TrapOp> {
auto loc = op->getLoc();
rewriter.eraseOp(op);

auto llvmTrapIntrinsicType =
mlir::LLVM::LLVMVoidType::get(op->getContext());
rewriter.create<mlir::LLVM::CallIntrinsicOp>(
loc, llvmTrapIntrinsicType, "llvm.trap", mlir::ValueRange{});
rewriter.create<mlir::LLVM::Trap>(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
Expand Down
2 changes: 1 addition & 1 deletion clang/test/CIR/Lowering/bswap.cir
Original file line number Diff line number Diff line change
Expand Up @@ -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: }

Expand Down
2 changes: 1 addition & 1 deletion clang/test/CIR/Lowering/intrinsics.cir
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit b1f7552

Please sign in to comment.