[MLIR][SCF][fuzzer-found] `scf-for-loop-specialization` crashes when an `scf.for` result is used
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.
@llvm/issue-subscribers-mlir
Author: pingshiyu
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.
looking into this