-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CIR][ThroughMLIR] Support lowering cir.condition and cir.while to scf.condition, scf.while #636
Changes from 10 commits
e29afd2
32375b4
e882b20
7bedd35
6c92cbd
ec2b5b2
43a17e4
bd8109b
00a8ce4
cc56b3d
584cced
592ac85
901b838
7b17ea5
45c9da1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,10 @@ | |
#include "mlir/Dialect/SCF/Transforms/Passes.h" | ||
#include "mlir/IR/BuiltinDialect.h" | ||
#include "mlir/IR/BuiltinTypes.h" | ||
#include "mlir/IR/Operation.h" | ||
#include "mlir/IR/Region.h" | ||
#include "mlir/IR/TypeRange.h" | ||
#include "mlir/IR/ValueRange.h" | ||
#include "mlir/Pass/Pass.h" | ||
#include "mlir/Pass/PassManager.h" | ||
#include "mlir/Support/LogicalResult.h" | ||
|
@@ -43,8 +47,14 @@ | |
#include "clang/CIR/Dialect/IR/CIRTypes.h" | ||
#include "clang/CIR/LowerToMLIR.h" | ||
#include "clang/CIR/Passes.h" | ||
#include "llvm/ADT/STLExtras.h" | ||
#include "llvm/ADT/Sequence.h" | ||
#include "llvm/ADT/SmallVector.h" | ||
#include "llvm/ADT/TypeSwitch.h" | ||
#include "llvm/Analysis/RegionInfo.h" | ||
#include "llvm/IR/Type.h" | ||
#include "llvm/IR/Value.h" | ||
#include "llvm/Support/Casting.h" | ||
|
||
using namespace cir; | ||
using namespace llvm; | ||
|
@@ -558,7 +568,6 @@ class CIRFuncOpLowering : public mlir::OpConversionPattern<mlir::cir::FuncOp> { | |
return mlir::failure(); | ||
|
||
rewriter.eraseOp(op); | ||
|
||
return mlir::LogicalResult::success(); | ||
} | ||
}; | ||
|
@@ -883,7 +892,6 @@ class CIRScopeOpLowering | |
if (mlir::failed(getTypeConverter()->convertTypes(scopeOp->getResultTypes(), | ||
mlirResultTypes))) | ||
return mlir::LogicalResult::failure(); | ||
|
||
rewriter.setInsertionPoint(scopeOp); | ||
auto newScopeOp = rewriter.create<mlir::memref::AllocaScopeOp>( | ||
scopeOp.getLoc(), mlirResultTypes); | ||
|
@@ -956,7 +964,7 @@ class CIRYieldOpLowering | |
mlir::ConversionPatternRewriter &rewriter) const override { | ||
auto *parentOp = op->getParentOp(); | ||
return llvm::TypeSwitch<mlir::Operation *, mlir::LogicalResult>(parentOp) | ||
.Case<mlir::scf::IfOp, mlir::scf::ForOp>([&](auto) { | ||
.Case<mlir::scf::IfOp, mlir::scf::ForOp, mlir::scf::WhileOp>([&](auto) { | ||
rewriter.replaceOpWithNewOp<mlir::scf::YieldOp>( | ||
op, adaptor.getOperands()); | ||
return mlir::success(); | ||
|
@@ -965,6 +973,27 @@ class CIRYieldOpLowering | |
} | ||
}; | ||
|
||
class CIRConditionOpLowering | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems ConditionOp only be used for loops. Should we move to LowerCIRLoopToSCF.cpp? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I move the CIRConditionOpLowering to LoweriCIRLoopToSCF |
||
: public mlir::OpConversionPattern<mlir::cir::ConditionOp> { | ||
public: | ||
using OpConversionPattern<mlir::cir::ConditionOp>::OpConversionPattern; | ||
mlir::LogicalResult | ||
matchAndRewrite(mlir::cir::ConditionOp op, OpAdaptor adaptor, | ||
mlir::ConversionPatternRewriter &rewriter) const override { | ||
auto *parentOp = op->getParentOp(); | ||
return llvm::TypeSwitch<mlir::Operation *, mlir::LogicalResult>(parentOp) | ||
.Case<mlir::scf::WhileOp>([&](auto) { | ||
auto condition = adaptor.getCondition(); | ||
auto i1Condition = rewriter.create<mlir::arith::TruncIOp>( | ||
op.getLoc(), rewriter.getI1Type(), condition); | ||
rewriter.replaceOpWithNewOp<mlir::scf::ConditionOp>( | ||
op, i1Condition, parentOp->getOperands()); | ||
return mlir::success(); | ||
}) | ||
.Default([](auto) { return mlir::failure(); }); | ||
} | ||
}; | ||
|
||
class CIRGlobalOpLowering | ||
: public mlir::OpConversionPattern<mlir::cir::GlobalOp> { | ||
public: | ||
|
@@ -1268,8 +1297,8 @@ void populateCIRToMLIRConversionPatterns(mlir::RewritePatternSet &patterns, | |
CIRLogOpLowering, CIRRoundOpLowering, CIRPtrStrideOpLowering, | ||
CIRSinOpLowering, CIRShiftOpLowering, CIRBitClzOpLowering, | ||
CIRBitCtzOpLowering, CIRBitPopcountOpLowering, CIRBitClrsbOpLowering, | ||
CIRBitFfsOpLowering, CIRBitParityOpLowering>(converter, | ||
patterns.getContext()); | ||
CIRBitFfsOpLowering, CIRBitParityOpLowering, CIRConditionOpLowering>( | ||
converter, patterns.getContext()); | ||
} | ||
|
||
static mlir::TypeConverter prepareTypeConverter() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fno-clangir-direct-lowering -emit-mlir %s -o %t.mlir | ||
// RUN: FileCheck --input-file=%t.mlir %s | ||
|
||
void foo() { | ||
int a = 0; | ||
while(a < 2) { | ||
a++; | ||
} | ||
} | ||
|
||
//CHECK: func.func @foo() { | ||
//CHECK: %alloca = memref.alloca() {alignment = 4 : i64} : memref<i32> | ||
//CHECK: %c0_i32 = arith.constant 0 : i32 | ||
//CHECK: memref.store %c0_i32, %alloca[] : memref<i32> | ||
//CHECK: memref.alloca_scope { | ||
//CHECK: scf.while : () -> () { | ||
//CHECK: %0 = memref.load %alloca[] : memref<i32> | ||
//CHECK: %c2_i32 = arith.constant 2 : i32 | ||
//CHECK: %1 = arith.cmpi ult, %0, %c2_i32 : i32 | ||
//CHECK: %2 = arith.extui %1 : i1 to i32 | ||
//CHECK: %c0_i32_0 = arith.constant 0 : i32 | ||
//CHECK: %3 = arith.cmpi ne, %2, %c0_i32_0 : i32 | ||
//CHECK: %4 = arith.extui %3 : i1 to i8 | ||
//CHECK: %5 = arith.trunci %4 : i8 to i1 | ||
//CHECK: scf.condition(%5) | ||
//CHECK: } do { | ||
//CHECK: %0 = memref.load %alloca[] : memref<i32> | ||
//CHECK: %c1_i32 = arith.constant 1 : i32 | ||
//CHECK: %1 = arith.addi %0, %c1_i32 : i32 | ||
//CHECK: memref.store %1, %alloca[] : memref<i32> | ||
//CHECK: scf.yield | ||
//CHECK: } | ||
//CHECK: } | ||
//CHECK: return | ||
//CHECK: } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// RUN: cir-opt %s -cir-to-mlir -o %t.mlir | ||
// RUN: FileCheck %s --input-file %t.mlir | ||
|
||
!s32i = !cir.int<s, 32> | ||
module { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please either add a comment with the C/C++ code we could use to regen this test in case CIR changes significantly or add a test coming from C/C++ source. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I will add some comment and test case There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hello, I add a test coming from C source , can you review it? |
||
cir.func @foo() { | ||
%0 = cir.alloca !s32i, !cir.ptr<!s32i>, ["__retval"] {alignment = 4 : i64} | ||
%1 = cir.alloca !s32i, !cir.ptr<!s32i>, ["a", init] {alignment = 4 : i64} | ||
%2 = cir.const #cir.int<0> : !s32i | ||
cir.store %2, %1 : !s32i, !cir.ptr<!s32i> | ||
cir.while { | ||
%3 = cir.load %1 : !cir.ptr<!s32i>, !s32i | ||
%4 = cir.const #cir.int<2> : !s32i | ||
%5 = cir.cmp(lt, %3, %4) : !s32i, !cir.bool | ||
cir.condition(%5) | ||
} do { | ||
%3 = cir.load %1 : !cir.ptr<!s32i>, !s32i | ||
%4 = cir.unary(inc, %3) : !s32i, !s32i | ||
cir.store %4, %1 : !s32i, !cir.ptr<!s32i> | ||
cir.yield | ||
} | ||
cir.return | ||
} | ||
} | ||
|
||
//CHECK: module { | ||
//CHECK-NEXT: func.func @foo() { | ||
//CHECK-NEXT: %alloca = memref.alloca() {alignment = 4 : i64} : memref<i32> | ||
//CHECK-NEXT: %alloca_0 = memref.alloca() {alignment = 4 : i64} : memref<i32> | ||
//CHECK-NEXT: %c0_i32 = arith.constant 0 : i32 | ||
//CHECK-NEXT: memref.store %c0_i32, %alloca_0[] : memref<i32> | ||
//CHECK-NEXT: scf.while : () -> () { | ||
//CHECK-NEXT: %0 = memref.load %alloca_0[] : memref<i32> | ||
//CHECK-NEXT: %c2_i32 = arith.constant 2 : i32 | ||
//CHECK-NEXT: %1 = arith.cmpi ult, %0, %c2_i32 : i32 | ||
//CHECK-NEXT: %2 = arith.extui %1 : i1 to i8 | ||
//CHECK-NEXT: %3 = arith.trunci %2 : i8 to i1 | ||
//CHECK-NEXT: scf.condition(%3) | ||
//CHECK-NEXT: } do { | ||
//CHECK-NEXT: %0 = memref.load %alloca_0[] : memref<i32> | ||
//CHECK-NEXT: %c1_i32 = arith.constant 1 : i32 | ||
//CHECK-NEXT: %1 = arith.addi %0, %c1_i32 : i32 | ||
//CHECK-NEXT: memref.store %1, %alloca_0[] : memref<i32> | ||
//CHECK-NEXT: scf.yield | ||
//CHECK-NEXT: } | ||
//CHECK-NEXT: return | ||
//CHECK-NEXT: } | ||
//CHECK-NEXT:} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: does the new adding include lines are required?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These files are generated by clangd’s auto-completion, and I believe they are not necessary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello, I have moved the CIRWhileOpLowering to LoweringSCFLoop.cpp , can you review it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we remove the un-required including lines?