Skip to content

[CIR][ThroughMLIR] Support lowering cir.condition and cir.while to scf.condition, scf.while #636

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

Merged
merged 15 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 69 additions & 15 deletions clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,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"
Expand All @@ -42,8 +46,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"
Copy link
Contributor

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?

Copy link
Contributor Author

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

Copy link
Contributor Author

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?

Copy link
Contributor

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?


using namespace cir;
using namespace llvm;
Expand Down Expand Up @@ -460,7 +470,6 @@ class CIRFuncOpLowering : public mlir::OpConversionPattern<mlir::cir::FuncOp> {
return mlir::failure();

rewriter.eraseOp(op);

return mlir::LogicalResult::success();
}
};
Expand Down Expand Up @@ -785,7 +794,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);
Expand Down Expand Up @@ -858,7 +866,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();
Expand All @@ -867,6 +875,51 @@ class CIRYieldOpLowering
}
};

class CIRConditionOpLowering
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 CIRWhileOpLowering
: public mlir::OpConversionPattern<mlir::cir::WhileOp> {
public:
using OpConversionPattern<mlir::cir::WhileOp>::OpConversionPattern;

mlir::LogicalResult
matchAndRewrite(mlir::cir::WhileOp whileOp, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const override {
auto newWhile = rewriter.create<mlir::scf::WhileOp>(
whileOp->getLoc(), whileOp->getResultTypes(), adaptor.getOperands());
rewriter.createBlock(&newWhile.getBefore());
rewriter.createBlock(&newWhile.getAfter());

rewriter.cloneRegionBefore(whileOp.getCond(), &newWhile.getBefore().back());
rewriter.eraseBlock(&newWhile.getBefore().back());

rewriter.cloneRegionBefore(whileOp.getBody(), &newWhile.getAfter().back());
rewriter.eraseBlock(&newWhile.getAfter().back());

rewriter.replaceOp(whileOp, newWhile->getResults());
return mlir::success();
}
};

class CIRGlobalOpLowering
: public mlir::OpConversionPattern<mlir::cir::GlobalOp> {
public:
Expand Down Expand Up @@ -1158,18 +1211,19 @@ void populateCIRToMLIRConversionPatterns(mlir::RewritePatternSet &patterns,
mlir::TypeConverter &converter) {
patterns.add<CIRReturnLowering, CIRBrOpLowering>(patterns.getContext());

patterns.add<CIRCmpOpLowering, CIRCallOpLowering, CIRUnaryOpLowering,
CIRBinOpLowering, CIRLoadOpLowering, CIRConstantOpLowering,
CIRStoreOpLowering, CIRAllocaOpLowering, CIRFuncOpLowering,
CIRScopeOpLowering, CIRBrCondOpLowering, CIRTernaryOpLowering,
CIRYieldOpLowering, CIRCosOpLowering, CIRGlobalOpLowering,
CIRGetGlobalOpLowering, CIRCastOpLowering,
CIRPtrStrideOpLowering, CIRSqrtOpLowering, CIRCeilOpLowering,
CIRExp2OpLowering, CIRExpOpLowering, CIRFAbsOpLowering,
CIRFloorOpLowering, CIRLog10OpLowering, CIRLog2OpLowering,
CIRLogOpLowering, CIRRoundOpLowering, CIRPtrStrideOpLowering,
CIRSinOpLowering, CIRShiftOpLowering>(converter,
patterns.getContext());
patterns
.add<CIRCmpOpLowering, CIRCallOpLowering, CIRUnaryOpLowering,
CIRBinOpLowering, CIRLoadOpLowering, CIRConstantOpLowering,
CIRStoreOpLowering, CIRAllocaOpLowering, CIRFuncOpLowering,
CIRScopeOpLowering, CIRBrCondOpLowering, CIRTernaryOpLowering,
CIRYieldOpLowering, CIRCosOpLowering, CIRGlobalOpLowering,
CIRGetGlobalOpLowering, CIRCastOpLowering, CIRPtrStrideOpLowering,
CIRSqrtOpLowering, CIRCeilOpLowering, CIRExp2OpLowering,
CIRExpOpLowering, CIRFAbsOpLowering, CIRFloorOpLowering,
CIRLog10OpLowering, CIRLog2OpLowering, CIRLogOpLowering,
CIRRoundOpLowering, CIRPtrStrideOpLowering, CIRSinOpLowering,
CIRShiftOpLowering, CIRConditionOpLowering, CIRWhileOpLowering>(
converter, patterns.getContext());
}

static mlir::TypeConverter prepareTypeConverter() {
Expand Down
48 changes: 48 additions & 0 deletions clang/test/CIR/Lowering/ThroughMLIR/while.cir
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 {
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will add some comment and test case

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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:}
Loading