Skip to content

Commit a460ec5

Browse files
authored
[CIR][Lowering] Add cir.brcond lowering (#278)
This PR adds `cir.brcond` lowering, which more or less follows the one in LLVM dialect lowering.
1 parent 3a3e65d commit a460ec5

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,14 +515,35 @@ class CIRScopeOpLowering
515515
}
516516
};
517517

518+
struct CIRBrCondOpLowering
519+
: public mlir::OpConversionPattern<mlir::cir::BrCondOp> {
520+
using mlir::OpConversionPattern<mlir::cir::BrCondOp>::OpConversionPattern;
521+
522+
mlir::LogicalResult
523+
matchAndRewrite(mlir::cir::BrCondOp brOp, OpAdaptor adaptor,
524+
mlir::ConversionPatternRewriter &rewriter) const override {
525+
526+
auto condition = adaptor.getCond();
527+
auto i1Condition = rewriter.create<mlir::arith::TruncIOp>(
528+
brOp.getLoc(), rewriter.getI1Type(), condition);
529+
rewriter.replaceOpWithNewOp<mlir::cf::CondBranchOp>(
530+
brOp, i1Condition.getResult(), brOp.getDestTrue(),
531+
adaptor.getDestOperandsTrue(), brOp.getDestFalse(),
532+
adaptor.getDestOperandsFalse());
533+
534+
return mlir::success();
535+
}
536+
};
537+
518538
void populateCIRToMLIRConversionPatterns(mlir::RewritePatternSet &patterns,
519539
mlir::TypeConverter &converter) {
520540
patterns.add<CIRReturnLowering, CIRBrOpLowering>(patterns.getContext());
521541

522542
patterns.add<CIRCmpOpLowering, CIRCallLowering, CIRUnaryOpLowering,
523543
CIRBinOpLowering, CIRLoadLowering, CIRConstantLowering,
524544
CIRStoreLowering, CIRAllocaLowering, CIRFuncLowering,
525-
CIRScopeOpLowering>(converter, patterns.getContext());
545+
CIRScopeOpLowering, CIRBrCondOpLowering>(converter,
546+
patterns.getContext());
526547
}
527548

528549
static mlir::TypeConverter prepareTypeConverter() {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// RUN: cir-opt %s -cir-to-mlir | FileCheck %s -check-prefix=MLIR
2+
// RUN: cir-opt %s -cir-to-mlir -cir-mlir-to-llvm | mlir-translate -mlir-to-llvmir | FileCheck %s -check-prefix=LLVM
3+
4+
!s32i = !cir.int<s, 32>
5+
cir.func @foo(%arg0: !cir.bool) -> !s32i {
6+
cir.brcond %arg0 ^bb1, ^bb2
7+
^bb1:
8+
%0 = cir.const(#cir.int<1>: !s32i) : !s32i
9+
cir.return %0 : !s32i
10+
^bb2:
11+
%1 = cir.const(#cir.int<0>: !s32i) : !s32i
12+
cir.return %1 : !s32i
13+
}
14+
15+
// MLIR: module {
16+
// MLIR-NEXT: func.func @foo(%arg0: i8) -> i32
17+
// MLIR-NEXT: %0 = arith.trunci %arg0 : i8 to i1
18+
// MLIR-NEXT: cf.cond_br %0, ^bb1, ^bb2
19+
// MLIR-NEXT: ^bb1: // pred: ^bb0
20+
// MLIR-NEXT: %c1_i32 = arith.constant 1 : i32
21+
// MLIR-NEXT: return %c1_i32 : i32
22+
// MLIR-NEXT: ^bb2: // pred: ^bb0
23+
// MLIR-NEXT: %c0_i32 = arith.constant 0 : i32
24+
// MLIR-NEXT: return %c0_i32 : i32
25+
// MLIR-NEXT: }
26+
// MLIR-NEXT: }
27+
28+
// LLVM: define i32 @foo(i8 %0)
29+
// LLVM-NEXT: %2 = trunc i8 %0 to i1
30+
// LLVM-NEXT: br i1 %2, label %3, label %4
31+
// LLVM-EMPTY:
32+
// LLVM-NEXT: 3: ; preds = %1
33+
// LLVM-NEXT: ret i32 1
34+
// LLVM-EMPTY:
35+
// LLVM-NEXT: 4: ; preds = %1
36+
// LLVM-NEXT: ret i32 0
37+
// LLVM-NEXT: }

0 commit comments

Comments
 (0)