Skip to content

[TRITONGPU] FuseNestedLoops: only trust llvm.assume that dominates the loop - #11521

Merged
peterbell10 merged 3 commits into
triton-lang:mainfrom
LiRunGuo:fix-fuse-nested-loops-assume-dominance
Sep 8, 2026
Merged

peterbell10 merged 3 commits into
triton-lang:mainfrom
LiRunGuo:fix-fuse-nested-loops-assume-dominance

Conversation

@LiRunGuo

@LiRunGuo LiRunGuo commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

[TRITONGPU] FuseNestedLoops: only trust llvm.assume that dominates the loop

Fixes #11519.

Problem

tritongpu-fuse-nested-loops proves an inner loop's trip count positive by
matching an llvm.assume(ub > lb) anywhere in the function — no dominance
or reachability query. When the assume sits on a branch the loop nest is not
reachable from (e.g. a shape-specialization branch that asserts something
about another parameter), the pass marks the inner loop as must-execute and
drops the zero-trip version split it would otherwise emit.

On a fully legal call (m=4, n=0 in the reproducer) the fused loop then runs
max(n, 1) * m times: the kernel returns a value the source program cannot
produce, and stores into a buffer the source program never writes (silent
memory corruption, no diagnostic).

The assumption is not being weighed and found probable: it is being read from
a program point the code under transformation cannot reach, which no
assumption-consuming analysis in LLVM or MLIR would consider valid
(llvm::isValidAssumeForContext is dominance-gated).

Fix

Thread DominanceInfo (already held by the pass and used two functions above
in the same file) into matchPositiveTripCount and require the matched
llvm.assume to properly dominate the loop. Otherwise fall through to the
existing version-split path, which is what every kernel without an in-scope
assume gets today. The in-tree _p_matmul idiom (tl.assume(loop_bound > 0)
immediately before the loop) keeps the fast path — nothing is lost there.

Verification

  • Reproduced on NVIDIA H200 (sm_90) with the reproducer from [FuseNestedLoops] matchPositiveTripCount accepts an llvm.assume without checking it dominates the loop, and miscompiles #11519: before
    the fix out[:4] = [4.0, 8.0, 12.0, 16.0] and 256 spurious sink writes;
    after the fix out stays [0.0, 0.0, 0.0, 0.0] and sink is untouched.
  • The structural half (no GPU): before the fix the emitted LLVM IR for the
    assume kernel contains no zero-trip test (icmp eq i32 %4, 0 absent); after
    the fix it is present, matching the assume-free control kernel.
  • Lit test test/TritonGPU/fuse-nested-loops.mlir: new @assume_not_dominating_loop
    case asserts the zero-trip guard (scf.if (ub == 0)) is still emitted and no
    arith.constant true must-execute guard appears.

Tests

  • /test for lit tests
  • /python/test for end-to-end tests

New contributor declaration

  • I am not making a trivial change, such as fixing a typo in a comment.
  • I have written a PR description following these
    rules.
  • I have run pre-commit run --from-ref origin/main --to-ref HEAD
    (the expand-yaml-anchors hook was skipped: it only applies to
    .github/workflows/integration-tests.yml and needs a network fetch of yq).

…e loop

tritongpu-fuse-nested-loops proved an inner loop's trip count positive by
matching an llvm.assume(ub > lb) anywhere in the function, without checking
that the assume dominates the loop. When the assume sat on a branch the loop
nest was not reachable from (e.g. a shape-specialization branch), the pass
dropped the zero-trip version split and marked the inner loop as
must-execute, so a legal call with n == 0 ran the fused loop max(n, 1) * m
times, returned a value the source cannot produce, and stored into memory
the source never writes.

Only trust the assume when it properly dominates the loop; otherwise fall
through to the existing version-split path, which is what every kernel
without an in-scope assume gets today. The in-tree _p_matmul idiom (assume
immediately before the loop) keeps the fast path.

Fixes triton-lang#11519
@LiRunGuo
LiRunGuo requested a review from ptillet as a code owner August 31, 2026 16:08
LiRunGuo and others added 2 commits August 31, 2026 20:23
- Remove the four-line comment in matchPositiveTripCount; the
  properlyDominates check is clear from the code.
- Remove the python regression test; the lit test in
  test/TritonGPU/fuse-nested-loops.mlir already covers the fix.
@LiRunGuo

LiRunGuo commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author

Hi @ThomasRaoux, thanks for the review!

I've addressed both comments in commit b4c81ae:

  • Removed the Python regression test in python/test/regression/test_functional_regressions.py — the lit test test/TritonGPU/fuse-nested-loops.mlir already covers the fix.
  • Removed the verbose comment in matchPositiveTripCount in FuseNestedLoops.cpp.

Could you take another look? Thanks!

@LiRunGuo

LiRunGuo commented Sep 8, 2026

Copy link
Copy Markdown
Contributor Author

Hi @ThomasRaoux,

The PR is approved and in sync with main, but it looks like the CI workflow hasn't run yet — all three "Integration Tests" runs for this branch are stuck in action_required (latest run: https://github.com/triton-lang/triton/actions/runs/33461670728).

Could you (or another maintainer) please approve the workflow so the checks can run? Thanks a lot!

@peterbell10
peterbell10 enabled auto-merge (squash) September 8, 2026 16:15
@peterbell10
peterbell10 merged commit 8f80860 into triton-lang:main Sep 8, 2026
18 of 20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FuseNestedLoops] matchPositiveTripCount accepts an llvm.assume without checking it dominates the loop, and miscompiles

4 participants