Issue 235441
Summary [mlir][linalg] transform.structured.fuse_into_containing_op crashes when the containing op is scf.for and the producer is used after the loop
Labels mlir
Assignees
Reporter andy-thomason
    `transform.structured.fuse_into_containing_op` crashes when the containing op is an `scf.for` and the producer still has a use after the loop. The same input with `scf.forall` (`tile_using_forall`) works, and so does the `scf.for` case when the producer has no later use.

### Reproducer

`repro.mlir`:

```mlir
#map = affine_map<(d0) -> (d0)>
func.func @multi_use_producer(%in: tensor<64xf32>) -> (tensor<64xf32>, tensor<64xf32>) {
  %e = tensor.empty() : tensor<64xf32>
  %exp = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel"]}
      ins(%in : tensor<64xf32>) outs(%e : tensor<64xf32>) {
  ^bb0(%a: f32, %b: f32):
    %0 = math.exp %a : f32
    linalg.yield %0 : f32
  } -> tensor<64xf32>
  %neg = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel"]}
      ins(%exp : tensor<64xf32>) outs(%e : tensor<64xf32>) {
  ^bb0(%a: f32, %b: f32):
    %0 = arith.negf %a : f32
    linalg.yield %0 : f32
  } -> tensor<64xf32>
  return %neg, %exp : tensor<64xf32>, tensor<64xf32>
}

module attributes {transform.with_named_sequence} {
  transform.named_sequence @__transform_main(%root: !transform.any_op {transform.readonly}) {
    %g = transform.structured.match ops{["linalg.generic"]} in %root : (!transform.any_op) -> !transform.any_op
    %exp, %neg = transform.split_handle %g : (!transform.any_op) -> (!transform.any_op, !transform.any_op)
    %tiled, %loop = transform.structured.tile_using_for %neg tile_sizes [16]
      : (!transform.any_op) -> (!transform.any_op, !transform.any_op)
    %fused, %new_loop = transform.structured.fuse_into_containing_op %exp into %loop
      : (!transform.any_op, !transform.any_op) -> (!transform.any_op, !transform.any_op)
    transform.yield
  }
}
```

```
mlir-opt repro.mlir --transform-interpreter
```

**Expected:** `%exp` is cloned into the loop, and the original `%exp` stays for the use in `return`. That's what happens when the second use is before the loop, or when the loop is an `scf.forall`.

**Actual:** segmentation fault (exit code 139).

Variations I tried:

| containing op | producer used after the loop | result |
|---|---|---|
| `scf.for` from `tile_using_for` | yes (above) | segfault |
| `scf.forall` from `tile_using_forall` | yes | ok |
| `scf.for` from `tile_reduction_using_for` | yes | segfault |
| `scf.for` from `tile_reduction_using_for` | no | ok |

I first hit this fusing `exp(x - max)` into the reduction loop of a decomposed `linalg.softmax`, where the divide that follows is the later use.

### Version

LLVM 22.1.1 release build (`mlir-opt --version`: "LLVM version 22.1.1, Optimized build", no assertions), x86_64 Linux. Backtrace (later frames trimmed to symbol names):

```
Stack dump:
0.	Program arguments: mlir-opt repro.mlir --transform-interpreter
 #0 0x000057192bbfb277 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (mlir-opt+0x74c6277)
 #1 0x000057192bbfbd9f SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
 #2 0x000079f5b1245330 (/lib/x86_64-linux-gnu/libc.so.6+0x45330)
 #3 0x000057192c90d3e6 mlir::transform::FuseIntoContainingOp::apply(mlir::transform::TransformRewriter&, mlir::transform::TransformResults&, mlir::transform::TransformState&) (mlir-opt+0x81d83e6)
 #4 0x000057192c87904e mlir::transform::detail::TransformOpInterfaceInterfaceTraits::Model<mlir::transform::FuseIntoContainingOp>::apply(...) DialectExtension.cpp:0:0
 #5 mlir::transform::TransformState::applyTransform(mlir::transform::TransformOpInterface)
 #6 applySequenceBlock(...) TransformOps.cpp:0:0
 #7 mlir::transform::NamedSequenceOp::apply(...)
 ...
#12 (anonymous namespace)::InterpreterPass::runOnOperation() InterpreterPass.cpp:0:0
```

### Likely cause

`tileAndFuseFirstExtractUse` always calls `replaceForAllWithNewSignature`. When the producer has users outside the containing op that the containing op dominates, that function does

```cpp
auto forallOp = cast<scf::ForallOp>(containingOp);
```

([LinalgTransformOps.cpp#L869 on main](https://github.com/llvm/llvm-project/blob/1c7ca866e4a3fc1c983d055d4526edcc7871cedb/mlir/lib/Dialect/Linalg/TransformOps/LinalgTransformOps.cpp#L869)). For an `scf.for` that cast is invalid, which segfaults in a release build. I'd expect an assertion failure in an assertions build, but I haven't run one. I haven't run main either, but the unguarded cast is still there.

A possible fix is to return `nullptr` early from `replaceForAllWithNewSignature` when the containing op isn't an `scf.forall`. The fused clone then feeds the loop, and the original producer keeps serving the later users, as it already does when there are no such users. The alternative is to give the `scf.for` an extra iter_arg/result, as is done for `scf.forall`.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to