Skip to content

[Codegen] Dynamic matmul causes a segfault when scf.forall uses its own result as an initializer #25156

Open

Description

@quiet-maple-47

What happened?

iree-compile segfaults in TileAndDistributeToWorkgroupsUsingForallOpPass when compiling a dynamic linalg.matmul whose LHS and accumulator are produced by consecutive linalg.fill operations. Both llvm-cpu and vmvx are affected.

func.func @main(%a: tensor<?x?xf32>) -> tensor<?x?xf32> {
  %c0 = arith.constant 0 : index
  %c1 = arith.constant 1 : index
  %n = tensor.dim %a, %c0 : tensor<?x?xf32>
  %m = tensor.dim %a, %c1 : tensor<?x?xf32>
  %e = tensor.empty(%n, %m) : tensor<?x?xf32>
  %one = arith.constant 1.0 : f32
  %zero = arith.constant 0.0 : f32
  %v = linalg.fill ins(%one : f32) outs(%e : tensor<?x?xf32>) -> tensor<?x?xf32>
  %f = linalg.fill ins(%zero : f32) outs(%v : tensor<?x?xf32>) -> tensor<?x?xf32>
  %w = linalg.matmul ins(%v, %a : tensor<?x?xf32>, tensor<?x?xf32>) outs(%f : tensor<?x?xf32>) -> tensor<?x?xf32>
  return %w : tensor<?x?xf32>
}

Steps to reproduce your issue

Save the function above as input.mlir and run:

iree-compile --iree-hal-target-backends=llvm-cpu input.mlir -o module.vmfb

The compiler segfaults and emits a crash diagnostic requesting a bug report.

The failure can also be reproduced directly with the affected pass.

pass.mlir:

func.func @f(%a: tensor<?x?xf32>, %n: index, %m: index) -> tensor<?x?xf32> {
  %one = arith.constant 1.0 : f32
  %zero = arith.constant 0.0 : f32
  %e = tensor.empty(%n, %m) : tensor<?x?xf32>
  %v = linalg.fill ins(%one : f32) outs(%e : tensor<?x?xf32>) -> tensor<?x?xf32>
  %f = linalg.fill ins(%zero : f32) outs(%v : tensor<?x?xf32>) -> tensor<?x?xf32>
  %w = linalg.matmul {lowering_config = #iree_codegen.lowering_config<tile_sizes = [[64, 0, 0]]>}
      ins(%v, %a : tensor<?x?xf32>, tensor<?x?xf32>) outs(%f : tensor<?x?xf32>) -> tensor<?x?xf32>
  return %w : tensor<?x?xf32>
}
iree-opt --pass-pipeline="builtin.module(func.func(iree-codegen-tile-and-distribute-to-workgroups-using-forall-op))" pass.mlir

The pass segfaults. Relevant stack trace from the Release build with assertions enabled at 5240b82 (no assertion fires first):

#4 mlir::detail::OperandStorage::setOperands(mlir::Operation*, unsigned int, unsigned int, mlir::ValueRange)
#5 mlir::MutableOperandRange::assign(mlir::ValueRange)
#6 (anonymous namespace)::ForallOpIterArgsFolder::matchAndRewrite(mlir::scf::ForallOp, mlir::PatternRewriter&) const
...
#11 mlir::applyPatternsGreedily(mlir::Region&, mlir::FrozenRewritePatternSet const&, mlir::GreedyRewriteConfig, bool*)
#12 mlir::iree_compiler::(anonymous namespace)::TileAndDistributeToWorkgroupsUsingForallOpPass::runOnOperation()

What component(s) does this issue relate to?

Compiler

Version information

Reproduced on Linux x86_64:

  • iree-base-compiler 3.13.0 (3.13.0rc20261016 @ 89ece87b89852100c0a4ac3d515e14842d090967)
  • Nightly 3.14.0rc20261024 (5240b82af62635c93016bc22d835544e22bc787c)

Additional context

Immediately before ForallOpIterArgsFolder, the generated scf.forall contains a self-reference:

%20:4 = "scf.forall"(%13, %20#3, %4, %4, %4) ...
//                          ^^^^^
//                          The loop's own result

The apparent cause is an interaction between dynamic shape queries and result replacement in TileDispatchUsingForall.cpp:

  1. tensor.dim users cause intermediate fills to be included in yieldReplacementsFor (source).
  2. After destination fusion, replaceUsesWithIf replaces a fill result with a yielded loop result, including its use as the same loop's initializer (source).
  3. ForallOpIterArgsFolder later erases the old loop and attempts to assign an operand referencing one of its erased results, causing the segfault.

The replacement predicate checks dominanceInfo.dominates(replacementCopy, user), which does not exclude the operation defining replacementCopy itself (MLIR Dominance.h).

Potential fix: Excluding tensor.dim users when determining yieldReplacementsFor prevents the crash. With this change, the pass and both targets compile, and llvm-cpu produces the same values as the equivalent program with separate fills.

Changing dominates to properlyDominates alone prevents the segfault but leaves incorrect fill placement, resulting in:

'linalg.fill' op write affecting operations on global resources are restricted to workgroup distributed contexts

This investigation and write-up were AI-assisted. I've reviewed and verified everything above, and I'm happy to answer questions.

Assisted-by: Claude Opus 5.5

Activity

  1. added
    bug 🐞
    on Oct 29, 2026
  2. github-actions commented on Oct 29, 2026

    @github-actions

    Hello @quiet-maple-47 👋

    Thank you for submitting an issue to IREE! It looks like it's your first one.

    The volume of issues is huge, and triage times may vary. If you need quicker assistance on the issue, consider posting it on IREE Discord and providing short context there.
    Feel free to identify the most frequent contributors to the corresponding part of the codebase via Git history/CODEOWNERS and (gently!) tag them below.


    Action required: acknowledge IREE project policies

    IREE is a Linux Foundation project. All participants are expected to follow the LF Projects Code of Conduct.

    Please note that all contributions to IREE, including GitHub issues, must follow our IREE AI Tool Use Policy. This means:

    • Issue reporters must fully understand the primary issue and the impact of the problem.
      For bug reports, specifics of the use-case, workflow and/or crash scenario usually suffice.
      For improvement suggestions, this usually implies deeper understanding of suboptimal code, IR design choices, assumed performance tradeoffs, etc.
    • Contributors must write the summary of the issue themselves, so that the exact impact and scenario is clear to other participants.
    • Issue authors are expected to review and understand all LLM/AI-assisted segments of the report, if any. E.g.: CLI commands and IR reproducers, model/framework details, lowering pipeline details, assumptions about problematic behaviors of IREE components.
    • Substantial use of LLM/generative AI tools must be disclaimed in the header or footer of the issue description.

    Before the issue can be triaged, we kindly ask you to reply to this message and confirm that you understand and accept the proposed policy. As required, please adjust your initial issue description.


    If you have any questions, feel free to leave a comment here, or ask away on IREE Discord.

    Thank you,
    The IREE Community

  3. quiet-maple-47 commented on Oct 29, 2026

    @quiet-maple-47
    Author

    Hello @quiet-maple-47 👋

    Thank you for submitting an issue to IREE! It looks like it's your first one.

    The volume of issues is huge, and triage times may vary. If you need quicker assistance on the issue, consider posting it on IREE Discord and providing short context there. Feel free to identify the most frequent contributors to the corresponding part of the codebase via Git history/CODEOWNERS and (gently!) tag them below.

    Action required: acknowledge IREE project policies

    IREE is a Linux Foundation project. All participants are expected to follow the LF Projects Code of Conduct.

    Please note that all contributions to IREE, including GitHub issues, must follow our IREE AI Tool Use Policy. This means:

    • Issue reporters must fully understand the primary issue and the impact of the problem.
      For bug reports, specifics of the use-case, workflow and/or crash scenario usually suffice.
      For improvement suggestions, this usually implies deeper understanding of suboptimal code, IR design choices, assumed performance tradeoffs, etc.
    • Contributors must write the summary of the issue themselves, so that the exact impact and scenario is clear to other participants.
    • Issue authors are expected to review and understand all LLM/AI-assisted segments of the report, if any. E.g.: CLI commands and IR reproducers, model/framework details, lowering pipeline details, assumptions about problematic behaviors of IREE components.
    • Substantial use of LLM/generative AI tools must be disclaimed in the header or footer of the issue description.

    Before the issue can be triaged, we kindly ask you to reply to this message and confirm that you understand and accept the proposed policy. As required, please adjust your initial issue description.

    If you have any questions, feel free to leave a comment here, or ask away on IREE Discord.

    Thank you, The IREE Community

    ack

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐞

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions