Skip to content

Commit 384ec29

Browse files
Added lowering logic for switch and appropriate tests
1 parent 3def997 commit 384ec29

File tree

4 files changed

+655
-1
lines changed

4 files changed

+655
-1
lines changed

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,37 @@ mlir::LogicalResult CIRToLLVMGlobalOpLowering::matchAndRewrite(
10381038
return mlir::success();
10391039
}
10401040

1041+
mlir::LogicalResult CIRToLLVMSwitchFlatOpLowering::matchAndRewrite(
1042+
cir::SwitchFlatOp op, OpAdaptor adaptor,
1043+
mlir::ConversionPatternRewriter &rewriter) const {
1044+
1045+
llvm::SmallVector<mlir::APInt, 8> caseValues;
1046+
if (op.getCaseValues()) {
1047+
for (mlir::Attribute val : op.getCaseValues()) {
1048+
auto intAttr = dyn_cast<cir::IntAttr>(val);
1049+
caseValues.push_back(intAttr.getValue());
1050+
}
1051+
}
1052+
1053+
llvm::SmallVector<mlir::Block *, 8> caseDestinations;
1054+
llvm::SmallVector<mlir::ValueRange, 8> caseOperands;
1055+
1056+
for (mlir::Block *x : op.getCaseDestinations()) {
1057+
caseDestinations.push_back(x);
1058+
}
1059+
1060+
for (mlir::OperandRange x : op.getCaseOperands()) {
1061+
caseOperands.push_back(x);
1062+
}
1063+
1064+
// Set switch op to branch to the newly created blocks.
1065+
rewriter.setInsertionPoint(op);
1066+
rewriter.replaceOpWithNewOp<mlir::LLVM::SwitchOp>(
1067+
op, adaptor.getCondition(), op.getDefaultDestination(),
1068+
op.getDefaultOperands(), caseValues, caseDestinations, caseOperands);
1069+
return mlir::success();
1070+
}
1071+
10411072
mlir::LogicalResult CIRToLLVMUnaryOpLowering::matchAndRewrite(
10421073
cir::UnaryOp op, OpAdaptor adaptor,
10431074
mlir::ConversionPatternRewriter &rewriter) const {
@@ -1641,6 +1672,7 @@ void ConvertCIRToLLVMPass::runOnOperation() {
16411672
CIRToLLVMGetGlobalOpLowering,
16421673
CIRToLLVMGetMemberOpLowering,
16431674
CIRToLLVMSelectOpLowering,
1675+
CIRToLLVMSwitchFlatOpLowering,
16441676
CIRToLLVMShiftOpLowering,
16451677
CIRToLLVMStackSaveOpLowering,
16461678
CIRToLLVMStackRestoreOpLowering,

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,16 @@ class CIRToLLVMFuncOpLowering : public mlir::OpConversionPattern<cir::FuncOp> {
149149
mlir::ConversionPatternRewriter &) const override;
150150
};
151151

152+
class CIRToLLVMSwitchFlatOpLowering
153+
: public mlir::OpConversionPattern<cir::SwitchFlatOp> {
154+
public:
155+
using mlir::OpConversionPattern<cir::SwitchFlatOp>::OpConversionPattern;
156+
157+
mlir::LogicalResult
158+
matchAndRewrite(cir::SwitchFlatOp op, OpAdaptor,
159+
mlir::ConversionPatternRewriter &) const override;
160+
};
161+
152162
class CIRToLLVMGetGlobalOpLowering
153163
: public mlir::OpConversionPattern<cir::GetGlobalOp> {
154164
public:

0 commit comments

Comments
 (0)