[Util] Fix crash in MergeIndexSwitchPattern when second op yields outer values (#25038)
In `MergeIndexSwitchPattern`, remapping operands of the second
`scf.index_switch`'s yields used `IRMapping::lookupOrNull`. When a
yielded value was defined outside the switch region (e.g., hoisted
constants), it was absent from the mapper, resulting in a null operand
passed to `scf::YieldOp::create` and causing a crash.
Switch to `lookupOrDefault` for the second op's yielded values, matching
how the first op's yields are handled. Cloned values and results of the
first op are still remapped correctly, while values defined in enclosing
scopes pass through unchanged.
### Reproducer
Fails in `MergeIndexSwitchPattern::matchAndRewrite` →
`scf::YieldOp::create`:
```mlir
func.func @main(%sel: i32) -> (i32, i32) {
%s = arith.index_cast %sel : i32 to index
%a = scf.index_switch %s -> i32
case 0 { %c = arith.constant 10 : i32
scf.yield %c : i32 }
case 1 { %c = arith.constant 11 : i32
scf.yield %c : i32 }
default { %c = arith.constant 12 : i32
scf.yield %c : i32 }
%b = scf.index_switch %s -> i32
case 0 { %c = arith.constant 20 : i32
scf.yield %c : i32 }
case 1 { %c = arith.constant 21 : i32
scf.yield %c : i32 }
default { %c = arith.constant 22 : i32
scf.yield %c : i32 }
return %a, %b : i32, i32
}
```
```shell
iree-compile repro.mlir --iree-hal-target-device=local \
--iree-hal-local-target-device-backends=vmvx -o /dev/null
```
### Changes
- Use `IRMapping::lookupOrDefault` when resolving yielded values from
the second switch.
- Add a regression test in `Dialect/Util/Transforms/test/patterns.mlir`
covering outer-scope yield values.
Assisted-by: Claude Code
Signed-off-by: jonathansong <7958352+jonathansong@users.noreply.github.com>
Co-authored-by: jonathansong <7958352+jonathansong@users.noreply.github.com>
diff --git a/compiler/src/iree/compiler/Dialect/Util/Transforms/Patterns.cpp b/compiler/src/iree/compiler/Dialect/Util/Transforms/Patterns.cpp
index 50541ca..6aee651 100644
--- a/compiler/src/iree/compiler/Dialect/Util/Transforms/Patterns.cpp
+++ b/compiler/src/iree/compiler/Dialect/Util/Transforms/Patterns.cpp
@@ -565,7 +565,7 @@
for (auto yieldValue : yieldB.getOperands()) {
// Track the new local values yielded by the region, ensuring we check
// what the first region may have produced and been captured implicitly.
- yieldValues.push_back(localMapping.lookupOrNull(yieldValue));
+ yieldValues.push_back(localMapping.lookupOrDefault(yieldValue));
}
// Add the merged yield containing results from both regions.
diff --git a/compiler/src/iree/compiler/Dialect/Util/Transforms/test/patterns.mlir b/compiler/src/iree/compiler/Dialect/Util/Transforms/test/patterns.mlir
index 26b2b37..702e882 100644
--- a/compiler/src/iree/compiler/Dialect/Util/Transforms/test/patterns.mlir
+++ b/compiler/src/iree/compiler/Dialect/Util/Transforms/test/patterns.mlir
@@ -258,6 +258,46 @@
// -----
+// Cases may yield values defined above both switches; they are used as-is in
+// the merged switch.
+
+// CHECK-LABEL: @mergeIndexSwitchesYieldingOuterValues
+// CHECK-SAME: (%[[CASE:.+]]: index, %[[X:.+]]: i32, %[[Y:.+]]: i32, %[[Z:.+]]: i32)
+util.func @mergeIndexSwitchesYieldingOuterValues(%case: index, %x: i32, %y: i32, %z: i32) -> (i32, i32) {
+ // CHECK: %[[RESULTS:.+]]:2 = scf.index_switch %[[CASE]] -> i32, i32
+ %result0 = scf.index_switch %case -> i32
+ // CHECK-NEXT: case 0 {
+ case 0 {
+ // CHECK-NEXT: scf.yield %[[X]], %[[Y]] : i32, i32
+ scf.yield %x : i32
+ }
+ // CHECK: case 1 {
+ case 1 {
+ // CHECK-NEXT: scf.yield %[[Y]], %[[Z]] : i32, i32
+ scf.yield %y : i32
+ }
+ // CHECK: default {
+ default {
+ // CHECK-NEXT: scf.yield %[[Z]], %[[X]] : i32, i32
+ scf.yield %z : i32
+ }
+ // CHECK-NOT: scf.index_switch
+ %result1 = scf.index_switch %case -> i32
+ case 0 {
+ scf.yield %y : i32
+ }
+ case 1 {
+ scf.yield %z : i32
+ }
+ default {
+ scf.yield %x : i32
+ }
+ // CHECK: util.return %[[RESULTS]]#0, %[[RESULTS]]#1
+ util.return %result0, %result1 : i32, i32
+}
+
+// -----
+
// CHECK-LABEL: @mergeIndexSwitchesIntoEmptyDefault
// CHECK-SAME: (%[[CASE:.+]]: index)
util.func @mergeIndexSwitchesIntoEmptyDefault(%case: index) {