Skip to content

Commit c47ec75

Browse files
Kureelanza
authored andcommitted
[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 86f0e23 commit c47ec75

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
@@ -527,14 +527,35 @@ class CIRScopeOpLowering
527527
}
528528
};
529529

530+
struct CIRBrCondOpLowering
531+
: public mlir::OpConversionPattern<mlir::cir::BrCondOp> {
532+
using mlir::OpConversionPattern<mlir::cir::BrCondOp>::OpConversionPattern;
533+
534+
mlir::LogicalResult
535+
matchAndRewrite(mlir::cir::BrCondOp brOp, OpAdaptor adaptor,
536+
mlir::ConversionPatternRewriter &rewriter) const override {
537+
538+
auto condition = adaptor.getCond();
539+
auto i1Condition = rewriter.create<mlir::arith::TruncIOp>(
540+
brOp.getLoc(), rewriter.getI1Type(), condition);
541+
rewriter.replaceOpWithNewOp<mlir::cf::CondBranchOp>(
542+
brOp, i1Condition.getResult(), brOp.getDestTrue(),
543+
adaptor.getDestOperandsTrue(), brOp.getDestFalse(),
544+
adaptor.getDestOperandsFalse());
545+
546+
return mlir::success();
547+
}
548+
};
549+
530550
void populateCIRToMLIRConversionPatterns(mlir::RewritePatternSet &patterns,
531551
mlir::TypeConverter &converter) {
532552
patterns.add<CIRReturnLowering, CIRBrOpLowering>(patterns.getContext());
533553

534554
patterns.add<CIRCmpOpLowering, CIRCallLowering, CIRUnaryOpLowering,
535555
CIRBinOpLowering, CIRLoadLowering, CIRConstantLowering,
536556
CIRStoreLowering, CIRAllocaLowering, CIRFuncLowering,
537-
CIRScopeOpLowering>(converter, patterns.getContext());
557+
CIRScopeOpLowering, CIRBrCondOpLowering>(converter,
558+
patterns.getContext());
538559
}
539560

540561
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)