llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[MLIR][SCF][fuzzer-found] `scf-for-loop-specialization` crashes when an `scf.for` result is used

Open pingshiyu opened this issue 1 month ago • 2 comments

Reproduced with an LLVM/MLIR 23.1.4 Linux build.

For the following (valid) program repro.mlir:

module {
  func.func @f(%n: index) -> index {
    %c0 = arith.constant 0 : index
    %c1 = arith.constant 1 : index
    %ub = affine.min affine_map<(d0) -> (d0, 4)>(%n)
    %result = scf.for %iv = %c0 to %ub step %c1
        iter_args(%acc = %c0) -> (index) {
      scf.yield %acc : index
    }
    return %result : index
  }
}

Running the specialization pass crashes with SIGSEGV while verifying the transformed IR:

$ mlir-opt repro.mlir '--pass-pipeline=builtin.module(func.func(scf-for-loop-specialization))' --verify-each -o /dev/null
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and instructions to reproduce the bug.
Stack dump:
#3 (anonymous namespace)::OperationVerifier::verifyOpAndDominance(...)
Segmentation fault

Removing the use of %result makes the pass succeed.

LLM-Suggested Cause

specializeForLoopForUnrolling creates a resultless scf.if, clones the result-bearing scf.for into both branches, and erases the original loop without replacing its live results. The surviving return therefore refers to the erased loop result and verification crashes.

The replacement scf.if should have the original loop result types, each branch should yield its cloned loop's results, and the original result uses should be replaced with the scf.if results before erasing the loop.

LLM Declaration

LLM was used to prepare the bug report, and the bug itself was found and verified by a human.

pingshiyu avatar Oct 26 '26 01:10 pingshiyu

@llvm/issue-subscribers-mlir

Author: pingshiyu

Reproduced with an LLVM/MLIR 23.1.4 Linux build.

For the following (valid) program repro.mlir:

module {
  func.func @<!-- -->f(%n: index) -&gt; index {
    %c0 = arith.constant 0 : index
    %c1 = arith.constant 1 : index
    %ub = affine.min affine_map&lt;(d0) -&gt; (d0, 4)&gt;(%n)
    %result = scf.for %iv = %c0 to %ub step %c1
        iter_args(%acc = %c0) -&gt; (index) {
      scf.yield %acc : index
    }
    return %result : index
  }
}

Running the specialization pass crashes with SIGSEGV while verifying the transformed IR:

$ mlir-opt repro.mlir '--pass-pipeline=builtin.module(func.func(scf-for-loop-specialization))' --verify-each -o /dev/null
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and instructions to reproduce the bug.
Stack dump:
#<!-- -->3 (anonymous namespace)::OperationVerifier::verifyOpAndDominance(...)
Segmentation fault

Removing the use of %result makes the pass succeed.

LLM-Suggested Cause

specializeForLoopForUnrolling creates a resultless scf.if, clones the result-bearing scf.for into both branches, and erases the original loop without replacing its live results. The surviving return therefore refers to the erased loop result and verification crashes.

The replacement scf.if should have the original loop result types, each branch should yield its cloned loop's results, and the original result uses should be replaced with the scf.if results before erasing the loop.

LLM Declaration

LLM was used to prepare the bug report, and the bug itself was found and verified by a human.

looking into this

StarOne01 avatar Oct 26 '26 07:10 StarOne01