Skip to content

Commit 4c4a762

Browse files
terapines open source contributor 2AdUhTkJm
andauthored
[CIR][ThroughMLIR] Lower TrapOp (#1561)
`cir.trap` corresponds to two operations, `call @llvm.trap` and `unreachable`. See the test case `Lowering/intrinsics.cir`. Co-authored-by: Yue Huang <[email protected]>
1 parent 90833a4 commit 4c4a762

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,12 +1248,28 @@ class CIRUnreachableOpLowering
12481248
mlir::LogicalResult
12491249
matchAndRewrite(cir::UnreachableOp op, OpAdaptor adaptor,
12501250
mlir::ConversionPatternRewriter &rewriter) const override {
1251-
// match and rewrite.
12521251
rewriter.replaceOpWithNewOp<mlir::LLVM::UnreachableOp>(op);
12531252
return mlir::success();
12541253
}
12551254
};
12561255

1256+
class CIRTrapOpLowering : public mlir::OpConversionPattern<cir::TrapOp> {
1257+
public:
1258+
using OpConversionPattern<cir::TrapOp>::OpConversionPattern;
1259+
1260+
mlir::LogicalResult
1261+
matchAndRewrite(cir::TrapOp op, OpAdaptor adaptor,
1262+
mlir::ConversionPatternRewriter &rewriter) const override {
1263+
rewriter.setInsertionPointAfter(op);
1264+
auto trapIntrinsicName = rewriter.getStringAttr("llvm.trap");
1265+
rewriter.create<mlir::LLVM::CallIntrinsicOp>(op.getLoc(), trapIntrinsicName,
1266+
/*args=*/mlir::ValueRange());
1267+
rewriter.create<mlir::LLVM::UnreachableOp>(op.getLoc());
1268+
rewriter.eraseOp(op);
1269+
return mlir::success();
1270+
}
1271+
};
1272+
12571273
void populateCIRToMLIRConversionPatterns(mlir::RewritePatternSet &patterns,
12581274
mlir::TypeConverter &converter) {
12591275
patterns.add<CIRReturnLowering, CIRBrOpLowering>(patterns.getContext());
@@ -1274,8 +1290,8 @@ void populateCIRToMLIRConversionPatterns(mlir::RewritePatternSet &patterns,
12741290
CIRBitClrsbOpLowering, CIRBitFfsOpLowering, CIRBitParityOpLowering,
12751291
CIRIfOpLowering, CIRVectorCreateLowering, CIRVectorInsertLowering,
12761292
CIRVectorExtractLowering, CIRVectorCmpOpLowering, CIRACosOpLowering,
1277-
CIRASinOpLowering, CIRUnreachableOpLowering, CIRTanOpLowering>(
1278-
converter, patterns.getContext());
1293+
CIRASinOpLowering, CIRUnreachableOpLowering, CIRTanOpLowering,
1294+
CIRTrapOpLowering>(converter, patterns.getContext());
12791295
}
12801296

12811297
static mlir::TypeConverter prepareTypeConverter() {

clang/test/CIR/Lowering/ThroughMLIR/unreachable.cir

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,13 @@ module {
77

88
// MLIR: func.func @test_unreachable()
99
// MLIR-NEXT: llvm.unreachable
10+
11+
cir.func @test_trap() {
12+
cir.trap
13+
}
14+
15+
// MLIR: func.func @test_trap() {
16+
// MLIR-NEXT: llvm.call_intrinsic "llvm.trap"() : () -> ()
17+
// MLIR-NEXT: llvm.unreachable
18+
// MLIR-NEXT: }
1019
}

0 commit comments

Comments
 (0)