Skip to content

Commit 4fd974f

Browse files
kumasentoivanradanov
authored andcommitted
[FoldSCFIf] fixed issues when no else
1 parent 6e35949 commit 4fd974f

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

tools/polymer/lib/Transforms/FoldSCFIf.cc

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,17 @@ static void foldSCFIf(scf::IfOp ifOp, FuncOp f, OpBuilder &b) {
5353
};
5454

5555
cloneAfter(ifOp.thenBlock(), thenResults);
56-
cloneAfter(ifOp.elseBlock(), elseResults);
5756

58-
for (auto ifResult : enumerate(ifOp.getResults())) {
59-
Value newResult =
60-
b.create<SelectOp>(loc, ifOp.condition(), thenResults[ifResult.index()],
61-
elseResults[ifResult.index()]);
62-
ifResult.value().replaceAllUsesWith(newResult);
57+
// Only an if op can have results when an else block is present.
58+
if (ifOp.elseBlock()) {
59+
cloneAfter(ifOp.elseBlock(), elseResults);
60+
61+
for (auto ifResult : enumerate(ifOp.getResults())) {
62+
Value newResult = b.create<SelectOp>(loc, ifOp.condition(),
63+
thenResults[ifResult.index()],
64+
elseResults[ifResult.index()]);
65+
ifResult.value().replaceAllUsesWith(newResult);
66+
}
6367
}
6468

6569
ifOp.erase();
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: polymer-opt %s -fold-scf-if | FileCheck %s
2+
3+
func @foo(%a: f32, %b: f32, %c: i1) {
4+
scf.if %c {
5+
%0 = arith.addf %a, %b : f32
6+
}
7+
return
8+
}
9+
10+
// CHECK: func @foo
11+
// CHECK-NEXT: arith.addf
12+
// CHECK-NEXT: return
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: polymer-opt %s -fold-scf-if | FileCheck %s
2+
func @foo(%a: f32, %b: f32, %c: i1) {
3+
scf.if %c {
4+
%0 = arith.addf %a, %b : f32
5+
} else {
6+
%0 = arith.mulf %a, %b : f32
7+
}
8+
9+
return
10+
}
11+
12+
// CHECK: func @foo
13+
// CHECK-NEXT: arith.addf
14+
// CHECK-NEXT: arith.mulf
15+
// CHECK-NEXT: return

0 commit comments

Comments
 (0)