[clang] [CIR] Add CIRGen for cir.unreachable and cir.trap (PR #151363)
Sirui Mu via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 30 10:10:25 PDT 2025
https://github.com/Lancern created https://github.com/llvm/llvm-project/pull/151363
This patch adds CIRGen support for `cir.unreachable` and `cir.trap`. It also adds missing LLVM lowering code for `cir.unreachable`.
>From 22fd4328f0b5695de47e001f135a92416c38bfbf Mon Sep 17 00:00:00 2001
From: Sirui Mu <msrlancern at gmail.com>
Date: Thu, 31 Jul 2025 01:08:22 +0800
Subject: [PATCH] [CIR] Add CIRGen for cir.unreachable and cir.trap
---
clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp | 17 +++++++++++++
clang/lib/CIR/CodeGen/CIRGenExpr.cpp | 5 ++++
clang/lib/CIR/CodeGen/CIRGenFunction.h | 4 ++++
.../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 10 +++++++-
.../CIR/Lowering/DirectToLLVM/LowerToLLVM.h | 10 ++++++++
clang/test/CIR/CodeGen/builtin_call.cpp | 24 +++++++++++++++++++
6 files changed, 69 insertions(+), 1 deletion(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
index 9049a016b2b9b..735b2b0a1627d 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
@@ -21,6 +21,7 @@
#include "mlir/Support/LLVM.h"
#include "clang/AST/Expr.h"
#include "clang/AST/GlobalDecl.h"
+#include "clang/Basic/Builtins.h"
#include "clang/CIR/MissingFeatures.h"
#include "llvm/Support/ErrorHandling.h"
@@ -269,6 +270,22 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID,
case Builtin::BI__builtin_rotateright32:
case Builtin::BI__builtin_rotateright64:
return emitRotate(e, /*isRotateLeft=*/false);
+
+ case Builtin::BI__builtin_trap: {
+ builder.create<cir::TrapOp>(loc);
+ // Note that cir.trap is a terminator so we need to start a new dummy block
+ // to preserve the builder's insertion point.
+ builder.createBlock(builder.getBlock()->getParent());
+ return RValue::get(nullptr);
+ }
+
+ case Builtin::BI__builtin_unreachable: {
+ emitUnreachable(e->getExprLoc());
+ // Note that cir.unreachable is a terminator so we need to start a new dummy
+ // block to preserve the builder's insertion point.
+ builder.createBlock(builder.getBlock()->getParent());
+ return RValue::get(nullptr);
+ }
}
// If this is an alias for a lib function (e.g. __builtin_sin), emit
diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
index c18498f80e99f..ae8da71219bf3 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
@@ -1780,6 +1780,11 @@ LValue CIRGenFunction::emitLoadOfReferenceLValue(Address refAddr,
pointeeBaseInfo);
}
+void CIRGenFunction::emitUnreachable(clang::SourceLocation loc) {
+ assert(!cir::MissingFeatures::sanitizers());
+ builder.create<cir::UnreachableOp>(getLoc(loc));
+}
+
mlir::Value CIRGenFunction::createDummyValue(mlir::Location loc,
clang::QualType qt) {
mlir::Type t = convertType(qt);
diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.h b/clang/lib/CIR/CodeGen/CIRGenFunction.h
index 603f75078c519..01049424f90cb 100644
--- a/clang/lib/CIR/CodeGen/CIRGenFunction.h
+++ b/clang/lib/CIR/CodeGen/CIRGenFunction.h
@@ -1201,6 +1201,10 @@ class CIRGenFunction : public CIRGenTypeCache {
LValue emitUnaryOpLValue(const clang::UnaryOperator *e);
+ /// Emit a reached-unreachable diagnostic if \p loc is valid and runtime
+ /// checking is enabled. Otherwise, just emit an unreachable instruction.
+ void emitUnreachable(clang::SourceLocation loc);
+
/// This method handles emission of any variable declaration
/// inside a function, including static vars etc.
void emitVarDecl(const clang::VarDecl &d);
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index 957a51ab334aa..ce245681e4443 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -2214,7 +2214,8 @@ void ConvertCIRToLLVMPass::runOnOperation() {
CIRToLLVMVecShuffleDynamicOpLowering,
CIRToLLVMVecShuffleOpLowering,
CIRToLLVMVecSplatOpLowering,
- CIRToLLVMVecTernaryOpLowering
+ CIRToLLVMVecTernaryOpLowering,
+ CIRToLLVMUnreachableOpLowering
// clang-format on
>(converter, patterns.getContext());
@@ -2270,6 +2271,13 @@ mlir::LogicalResult CIRToLLVMGetMemberOpLowering::matchAndRewrite(
}
}
+mlir::LogicalResult CIRToLLVMUnreachableOpLowering::matchAndRewrite(
+ cir::UnreachableOp op, OpAdaptor adaptor,
+ mlir::ConversionPatternRewriter &rewriter) const {
+ rewriter.replaceOpWithNewOp<mlir::LLVM::UnreachableOp>(op);
+ return mlir::success();
+}
+
mlir::LogicalResult CIRToLLVMTrapOpLowering::matchAndRewrite(
cir::TrapOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const {
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h
index f339d4310ae0c..c5106cb33f452 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h
@@ -402,6 +402,16 @@ class CIRToLLVMGetMemberOpLowering
mlir::ConversionPatternRewriter &) const override;
};
+class CIRToLLVMUnreachableOpLowering
+ : public mlir::OpConversionPattern<cir::UnreachableOp> {
+public:
+ using mlir::OpConversionPattern<cir::UnreachableOp>::OpConversionPattern;
+
+ mlir::LogicalResult
+ matchAndRewrite(cir::UnreachableOp op, OpAdaptor,
+ mlir::ConversionPatternRewriter &) const override;
+};
+
class CIRToLLVMTrapOpLowering : public mlir::OpConversionPattern<cir::TrapOp> {
public:
using mlir::OpConversionPattern<cir::TrapOp>::OpConversionPattern;
diff --git a/clang/test/CIR/CodeGen/builtin_call.cpp b/clang/test/CIR/CodeGen/builtin_call.cpp
index d9a70683a4dbc..8465ea83d7875 100644
--- a/clang/test/CIR/CodeGen/builtin_call.cpp
+++ b/clang/test/CIR/CodeGen/builtin_call.cpp
@@ -166,3 +166,27 @@ void expect_prob(int x, int y) {
// LLVM-NEXT: %[[Y_LONG:.+]] = sext i32 %[[Y]] to i64
// LLVM-NEXT: %{{.+}} = call i64 @llvm.expect.with.probability.i64(i64 %[[X_LONG]], i64 %[[Y_LONG]], double 2.500000e-01)
// LLVM: }
+
+void unreachable() {
+ __builtin_unreachable();
+}
+
+// CIR-LABEL: @_Z11unreachablev
+// CIR: cir.unreachable
+// CIR: }
+
+// LLVM-LABEL: @_Z11unreachablev
+// LLVM: unreachable
+// LLVM: }
+
+void trap() {
+ __builtin_trap();
+}
+
+// CIR-LABEL: @_Z4trapv
+// CIR: cir.trap
+// CIR: }
+
+// LLVM-LABEL: @_Z4trapv
+// LLVM: call void @llvm.trap()
+// LLVM: }
More information about the cfe-commits
mailing list