Skip to content

Commit 75f6761

Browse files
Remove auto, add log-range test, and end-to-end test for switch flat op
1 parent 803abd7 commit 75f6761

File tree

5 files changed

+134
-12
lines changed

5 files changed

+134
-12
lines changed

clang/include/clang/CIR/Dialect/IR/CIROps.td

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -981,15 +981,15 @@ def SwitchFlatOp : CIR_Op<"switch.flat", [AttrSizedOperandSegments,
981981
let description = [{
982982
The `cir.switch.flat` operation is a region-less and simplified
983983
version of the `cir.switch`.
984-
It's representation is closer to LLVM IR dialect
984+
Its representation is closer to LLVM IR dialect
985985
than the C/C++ language feature.
986986
}];
987987

988988
let arguments = (ins
989989
CIR_IntType:$condition,
990990
Variadic<AnyType>:$defaultOperands,
991991
VariadicOfVariadic<AnyType, "case_operand_segments">:$caseOperands,
992-
ArrayAttr:$case_values,
992+
ArrayAttr:$caseValues,
993993
DenseI32ArrayAttr:$case_operand_segments
994994
);
995995

@@ -1001,7 +1001,7 @@ def SwitchFlatOp : CIR_Op<"switch.flat", [AttrSizedOperandSegments,
10011001
let assemblyFormat = [{
10021002
$condition `:` type($condition) `,`
10031003
$defaultDestination (`(` $defaultOperands^ `:` type($defaultOperands) `)`)?
1004-
custom<SwitchFlatOpCases>(ref(type($condition)), $case_values,
1004+
custom<SwitchFlatOpCases>(ref(type($condition)), $caseValues,
10051005
$caseDestinations, $caseOperands,
10061006
type($caseOperands))
10071007
attr-dict

clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ class CIRSwitchOpFlattening : public mlir::OpRewritePattern<cir::SwitchOp> {
247247
{
248248
cir::YieldOp switchYield = nullptr;
249249
// Clear switch operation.
250-
for (auto &block : llvm::make_early_inc_range(op.getBody().getBlocks()))
250+
for (mlir::Block &block :
251+
llvm::make_early_inc_range(op.getBody().getBlocks()))
251252
if (auto yieldOp = dyn_cast<cir::YieldOp>(block.getTerminator()))
252253
switchYield = yieldOp;
253254

@@ -279,7 +280,7 @@ class CIRSwitchOpFlattening : public mlir::OpRewritePattern<cir::SwitchOp> {
279280
mlir::ValueRange defaultOperands = exitBlock->getArguments();
280281

281282
// Digest the case statements values and bodies.
282-
for (auto caseOp : cases) {
283+
for (cir::CaseOp caseOp : cases) {
283284
mlir::Region &region = caseOp.getCaseRegion();
284285

285286
// Found default case: save destination and operands.
@@ -300,7 +301,7 @@ class CIRSwitchOpFlattening : public mlir::OpRewritePattern<cir::SwitchOp> {
300301
case cir::CaseOpKind::Anyof:
301302
case cir::CaseOpKind::Equal:
302303
// AnyOf cases kind can have multiple values, hence the loop below.
303-
for (auto &value : caseOp.getValue()) {
304+
for (const mlir::Attribute &value : caseOp.getValue()) {
304305
caseValues.push_back(cast<cir::IntAttr>(value).getValue());
305306
caseDestinations.push_back(&region.front());
306307
caseOperands.push_back(caseDestinations.back()->getArguments());
@@ -319,7 +320,7 @@ class CIRSwitchOpFlattening : public mlir::OpRewritePattern<cir::SwitchOp> {
319320
});
320321

321322
// Track fallthrough in cases.
322-
for (auto &blk : region.getBlocks()) {
323+
for (mlir::Block &blk : region.getBlocks()) {
323324
if (blk.getNumSuccessors())
324325
continue;
325326

@@ -349,7 +350,7 @@ class CIRSwitchOpFlattening : public mlir::OpRewritePattern<cir::SwitchOp> {
349350
}
350351

351352
// Remove all cases since we've inlined the regions.
352-
for (auto caseOp : cases) {
353+
for (cir::CaseOp caseOp : cases) {
353354
mlir::Block *caseBlock = caseOp->getBlock();
354355
// Erase the block with no predecessors here to make the generated code
355356
// simpler a little bit.
@@ -359,9 +360,9 @@ class CIRSwitchOpFlattening : public mlir::OpRewritePattern<cir::SwitchOp> {
359360
rewriter.eraseOp(caseOp);
360361
}
361362

362-
for (size_t index = 0; index < rangeValues.size(); ++index) {
363-
APInt lowerBound = rangeValues[index].first;
364-
APInt upperBound = rangeValues[index].second;
363+
for (auto [index, rangeVal] : llvm::enumerate(rangeValues)) {
364+
APInt lowerBound = rangeVal.first;
365+
APInt upperBound = rangeVal.second;
365366

366367
// The case range is unreachable, skip it.
367368
if (lowerBound.sgt(upperBound))
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
2+
// RUN: cir-opt --mlir-print-ir-before=cir-flatten-cfg --cir-flatten-cfg %t.cir -o %t.flattened.before.cir 2> %t.before
3+
// RUN: FileCheck --input-file=%t.before %s --check-prefix=BEFORE
4+
// RUN: cir-opt --mlir-print-ir-after=cir-flatten-cfg --cir-flatten-cfg %t.cir -o %t.flattened.after.cir 2> %t.after
5+
// RUN: FileCheck --input-file=%t.after %s --check-prefix=AFTER
6+
7+
8+
9+
10+
11+
void swf(int a) {
12+
switch (int b = 3; a) {
13+
case 3:
14+
b = b * 2;
15+
break;
16+
case 4 ... 5:
17+
b = b * 3;
18+
break;
19+
default:
20+
break;
21+
}
22+
23+
}
24+
25+
// BEFORE: cir.func @_Z3swfi
26+
// BEFORE: %[[VAR_B:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["b", init] {alignment = 4 : i64}
27+
// BEFORE: %[[CONST_3:.*]] = cir.const #cir.int<3> : !s32i
28+
// BEFORE: cir.switch (%[[COND:.*]] : !s32i) {
29+
// BEFORE: cir.case(equal, [#cir.int<3> : !s32i]) {
30+
// BEFORE: %[[LOAD_B_EQ:.*]] = cir.load %[[VAR_B]] : !cir.ptr<!s32i>, !s32i
31+
// BEFORE: %[[CONST_2:.*]] = cir.const #cir.int<2> : !s32i
32+
// BEFORE: %[[MUL_EQ:.*]] = cir.binop(mul, %[[LOAD_B_EQ]], %[[CONST_2]]) nsw : !s32i
33+
// BEFORE: cir.store %[[MUL_EQ]], %[[VAR_B]] : !s32i, !cir.ptr<!s32i>
34+
// BEFORE: cir.break
35+
// BEFORE: }
36+
// BEFORE: cir.case(range, [#cir.int<4> : !s32i, #cir.int<5> : !s32i]) {
37+
// BEFORE: %[[LOAD_B_RANGE:.*]] = cir.load %[[VAR_B]] : !cir.ptr<!s32i>, !s32i
38+
// BEFORE: %[[CONST_3_RANGE:.*]] = cir.const #cir.int<3> : !s32i
39+
// BEFORE: %[[MUL_RANGE:.*]] = cir.binop(mul, %[[LOAD_B_RANGE]], %[[CONST_3_RANGE]]) nsw : !s32i
40+
// BEFORE: cir.store %[[MUL_RANGE]], %[[VAR_B]] : !s32i, !cir.ptr<!s32i>
41+
// BEFORE: cir.break
42+
// BEFORE: }
43+
// BEFORE: cir.case(default, []) {
44+
// BEFORE: cir.break
45+
// BEFORE: }
46+
// BEFORE: cir.yield
47+
// BEFORE: }
48+
// BEFORE: }
49+
// BEFORE: cir.return
50+
51+
// AFTER: cir.func @_Z3swfi
52+
// AFTER: %[[VAR_A:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["a", init] {alignment = 4 : i64}
53+
// AFTER: cir.store %arg0, %[[VAR_A]] : !s32i, !cir.ptr<!s32i>
54+
// AFTER: %[[VAR_B:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["b", init] {alignment = 4 : i64}
55+
// AFTER: %[[CONST_3:.*]] = cir.const #cir.int<3> : !s32i
56+
// AFTER: cir.store %[[CONST_3]], %[[VAR_B]] : !s32i, !cir.ptr<!s32i>
57+
// AFTER: cir.switch.flat %[[COND:.*]] : !s32i, ^bb[[#BB6:]] [
58+
// AFTER: 3: ^bb[[#BB4:]],
59+
// AFTER: 4: ^bb[[#BB5:]],
60+
// AFTER: 5: ^bb[[#BB5:]]
61+
// AFTER: ]
62+
// AFTER: ^bb[[#BB4]]:
63+
// AFTER: %[[LOAD_B_EQ:.*]] = cir.load %[[VAR_B]] : !cir.ptr<!s32i>, !s32i
64+
// AFTER: %[[CONST_2:.*]] = cir.const #cir.int<2> : !s32i
65+
// AFTER: %[[MUL_EQ:.*]] = cir.binop(mul, %[[LOAD_B_EQ]], %[[CONST_2]]) nsw : !s32i
66+
// AFTER: cir.store %[[MUL_EQ]], %[[VAR_B]] : !s32i, !cir.ptr<!s32i>
67+
// AFTER: cir.br ^bb[[#BB7:]]
68+
// AFTER: ^bb[[#BB5]]:
69+
// AFTER: %[[LOAD_B_RANGE:.*]] = cir.load %[[VAR_B]] : !cir.ptr<!s32i>, !s32i
70+
// AFTER: %[[CONST_3_AGAIN:.*]] = cir.const #cir.int<3> : !s32i
71+
// AFTER: %[[MUL_RANGE:.*]] = cir.binop(mul, %[[LOAD_B_RANGE]], %[[CONST_3_AGAIN]]) nsw : !s32i
72+
// AFTER: cir.store %[[MUL_RANGE]], %[[VAR_B]] : !s32i, !cir.ptr<!s32i>
73+
// AFTER: cir.br ^bb[[#BB7]]
74+
// AFTER: ^bb[[#BB6]]:
75+
// AFTER: cir.br ^bb[[#BB7]]
76+
// AFTER: ^bb[[#BB7]]:
77+
// AFTER: cir.br ^bb[[#BB8:]]
78+
// AFTER: ^bb[[#BB8]]:
79+
// AFTER: cir.return
80+
// AFTER: }
81+

clang/test/CIR/IR/switch-flat.cir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ cir.func @FlatSwitchWithoutDefault(%arg0: !s32i) {
1717
// CHECK-NEXT: ^bb1:
1818
// CHECK-NEXT: cir.br ^bb2
1919
// CHECK-NEXT: ^bb2:
20-
//CHECK-NEXT: cir.return
20+
// CHECK-NEXT: cir.return
2121

2222
cir.func @FlatSwitchWithDefault(%arg0: !s32i) {
2323
cir.switch.flat %arg0 : !s32i, ^bb2 [

clang/test/CIR/Transforms/switch.cir

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,4 +275,44 @@ module {
275275
// CHECK: cir.return
276276
// CHECK: }
277277

278+
cir.func @_Z8bigRangei(%arg0: !s32i) {
279+
%0 = cir.alloca !s32i, !cir.ptr<!s32i>, ["a", init] {alignment = 4 : i64}
280+
cir.store %arg0, %0 : !s32i, !cir.ptr<!s32i>
281+
cir.scope {
282+
%1 = cir.load %0 : !cir.ptr<!s32i>, !s32i
283+
cir.switch (%1 : !s32i) {
284+
cir.case(range, [#cir.int<3> : !s32i, #cir.int<100> : !s32i]) {
285+
cir.break
286+
}
287+
cir.case(default, []) {
288+
cir.break
289+
}
290+
cir.yield
291+
}
292+
}
293+
cir.return
294+
}
295+
296+
// CHECK: cir.func @_Z8bigRangei(%arg0: !s32i) {
297+
// CHECK: cir.switch.flat %[[COND:.*]] : !s32i, ^bb[[#RANGE_BR:]] [
298+
// CHECK: ]
299+
// CHECK: ^bb[[#NO_PRED_BB:]]: // no predecessors
300+
// CHECK: cir.br ^bb[[#DEFAULT_BB:]]
301+
// CHECK: ^bb[[#DEFAULT_BB]]: // 2 preds: ^bb[[#NO_PRED_BB]], ^bb[[#RANGE_BR]]
302+
// CHECK: cir.br ^bb[[#EXIT:]]
303+
// CHECK: ^bb[[#RANGE_BR]]: // pred: ^bb[[#BB2:]]
304+
// CHECK: %[[CONST97:.*]] = cir.const #cir.int<97> : !s32i
305+
// CHECK: %[[CONST3:.*]] = cir.const #cir.int<3> : !s32i
306+
// CHECK: %[[SUB:.*]] = cir.binop(sub, %[[COND]], %[[CONST3]]) : !s32i
307+
// CHECK: %[[CAST1:.*]] = cir.cast(integral, %[[SUB]] : !s32i), !u32i
308+
// CHECK: %[[CAST2:.*]] = cir.cast(integral, %[[CONST97]] : !s32i), !u32i
309+
// CHECK: %[[CMP:.*]] = cir.cmp(le, %[[CAST1]], %[[CAST2]]) : !u32i, !cir.bool
310+
// CHECK: cir.brcond %7 ^bb[[#DEFAULT_BB]], ^bb[[#RANGE_BB:]]
311+
// CHECK: ^bb[[#RANGE_BB]]: // pred: ^bb[[#RANGE_BR]]
312+
// CHECK: cir.br ^bb[[#EXIT]]
313+
// CHECK: ^bb[[#EXIT]]: // 2 preds: ^bb[[#DEFAULT_BB]], ^bb[[#RANGE_BB]]
314+
// CHECK: cir.br ^bb[[#RET_BB:]]
315+
// CHECK: ^bb[[#RET_BB]]: // pred: ^bb[[#EXIT]]
316+
// CHECK: cir.return
317+
// CHECK: }
278318
}

0 commit comments

Comments
 (0)