Skip to content

Commit be8ad03

Browse files
committed
Address feedback
1 parent 6c6d77f commit be8ad03

File tree

3 files changed

+105
-2
lines changed

3 files changed

+105
-2
lines changed

mlir/docs/Dialects/SPIR-V.md

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ merge block.
566566
For example, for the given function
567567

568568
```c++
569-
void loop(bool cond) {
569+
void if(bool cond) {
570570
int x = 0;
571571
if (cond) {
572572
x = 1;
@@ -605,6 +605,62 @@ func.func @selection(%cond: i1) -> () {
605605
}
606606
```
607607

608+
Similarly, for the give function with a `switch` statement
609+
610+
```c++
611+
void switch(int selector) {
612+
int x = 0;
613+
switch (selector) {
614+
case 0:
615+
x = 2;
616+
break;
617+
case 1:
618+
x = 3;
619+
break;
620+
default:
621+
x = 1;
622+
break;
623+
}
624+
// ...
625+
}
626+
```
627+
628+
It will be represented as
629+
630+
```mlir
631+
func.func @selection(%selector: i32) -> () {
632+
%zero = spirv.Constant 0: i32
633+
%one = spirv.Constant 1: i32
634+
%two = spirv.Constant 2: i32
635+
%three = spirv.Constant 3: i32
636+
%var = spirv.Variable init(%zero) : !spirv.ptr<i32, Function>
637+
638+
spirv.mlir.selection {
639+
spirv.Switch %selector : i32, [
640+
default: ^default,
641+
0: ^case0,
642+
1: ^case1
643+
]
644+
^default:
645+
spirv.Store "Function" %var, %one : i32
646+
spirv.Branch ^merge
647+
648+
^case0:
649+
spirv.Store "Function" %var, %two : i32
650+
spirv.Branch ^merge
651+
652+
^case1:
653+
spirv.Store "Function" %var, %three : i32
654+
spirv.Branch ^merge
655+
656+
^merge:
657+
spirv.mlir.merge
658+
}
659+
660+
// ...
661+
}
662+
```
663+
608664
The selection can return values by yielding them with `spirv.mlir.merge`. This
609665
mechanism allows values defined within the selection region to be used outside of it.
610666
Without this, values that were sunk into the selection region, but used outside, would

mlir/lib/Dialect/SPIRV/IR/ControlFlowOps.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ Block *SwitchOp::getSuccessorForOperands(ArrayRef<Attribute> operands) {
293293
return getDefaultTarget();
294294

295295
SuccessorRange targets = getTargets();
296-
if (auto value = llvm::dyn_cast_or_null<IntegerAttr>(operands.front())) {
296+
if (auto value = dyn_cast_or_null<IntegerAttr>(operands.front())) {
297297
for (auto [index, literal] : llvm::enumerate(literals->getValues<APInt>()))
298298
if (literal == value.getValue())
299299
return targets[index];

mlir/test/Dialect/SPIRV/IR/control-flow-ops.mlir

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,53 @@ func.func @selection(%cond: i1) -> () {
795795

796796
// -----
797797

798+
func.func @selection_switch(%selector: i32) -> () {
799+
%zero = spirv.Constant 0: i32
800+
%one = spirv.Constant 1: i32
801+
%two = spirv.Constant 2: i32
802+
%three = spirv.Constant 3: i32
803+
%var = spirv.Variable init(%zero) : !spirv.ptr<i32, Function>
804+
805+
// CHECK: spirv.mlir.selection {
806+
spirv.mlir.selection {
807+
// CHECK-NEXT: spirv.Switch {{%.*}} : i32, [
808+
// CHECK-NEXT: default: ^bb1,
809+
// CHECK-NEXT: 0: ^bb2,
810+
// CHECK-NEXT: 1: ^bb3
811+
spirv.Switch %selector : i32, [
812+
default: ^default,
813+
0: ^case0,
814+
1: ^case1
815+
]
816+
// CHECK: ^bb1
817+
^default:
818+
spirv.Store "Function" %var, %one : i32
819+
// CHECK: spirv.Branch ^bb4
820+
spirv.Branch ^merge
821+
822+
// CHECK: ^bb2
823+
^case0:
824+
spirv.Store "Function" %var, %two : i32
825+
// CHECK: spirv.Branch ^bb4
826+
spirv.Branch ^merge
827+
828+
// CHECK: ^bb3
829+
^case1:
830+
spirv.Store "Function" %var, %three : i32
831+
// CHECK: spirv.Branch ^bb4
832+
spirv.Branch ^merge
833+
834+
// CHECK: ^bb4
835+
^merge:
836+
// CHECK-NEXT: spirv.mlir.merge
837+
spirv.mlir.merge
838+
}
839+
840+
spirv.Return
841+
}
842+
843+
// -----
844+
798845
// CHECK-LABEL: @empty_region
799846
func.func @empty_region() -> () {
800847
// CHECK: spirv.mlir.selection

0 commit comments

Comments
 (0)