Repository navigation
Description
Summary
On feature/togsim-cpp-trace, a bilinear F.interpolate emits invalid MLIR: a value defined
inside one gather's affine.for body is referenced from a later, sibling affine.for body.
Codegen aborts while the Python passes re-parse the kernel:
mlir._mlir_libs._site_initialize.<locals>.MLIRError: Unable to parse module assembly:
error: "-":186:42: use of undeclared SSA value name
Minimal repro
import torch
import torch.nn.functional as F
x = torch.randn(1, 8, 8, 8).to("npu:0")
f = torch.compile(dynamic=False)(
lambda t: F.interpolate(t, scale_factor=2, mode="bilinear", align_corners=False)
)
f(x) # -> MLIRError: use of undeclared SSA value nameWhat does and does not trigger it
| case | result |
|---|---|
x[i] (single gather) |
OK |
x[i0] - x[i1] (two gathers + sub) |
OK |
x[i0] + x[i1] |
OK |
F.interpolate(..., mode="nearest") |
OK |
F.interpolate(..., mode="bilinear") |
FAIL |
So two indirect gathers alone are fine. It needs the bilinear pattern, where several gathers of the
same tensor are blended and one gather's loaded value is consumed after a later gather has run.
Mechanism
Two things are broken at once. From the generated kernel:
119: "togsim.transfer"(%in_ptr0, ..., %spad4, ..., %spad3) {indirect = true} // gather #1 -> spad4
120: affine.for %compute_idx = 0 to 64 step 16 {
122: %tmp57 = affine.vector_load %spad4[...] // value A
...
} // <- scope closes
182: "togsim.transfer"(%in_ptr0, ..., %spad4, ..., %spad7) {indirect = true} // gather #2 -> SAME spad4
183: affine.for %compute_idx = 0 to 64 step 16 {
185: %tmp113 = affine.vector_load %spad4[...] // value B
186: %tmp114 = arith.subf %tmp57, %tmp113 // A - B
}- SSA scope violation.
%tmp57is defined in the first loop's region and used in a sibling
region, so the module does not parse. - Even with scoping fixed, the data would be wrong. Both indirect MVINs target the same
%spad4(only the index buffer differs:%spad3vs%spad7), so value A has already been
overwritten by gather Scalar #2 by the timearith.subfruns.
A correct lowering has to either give each gather its own scratchpad buffer (and hoist the loaded
value so it stays live), or fuse the gather loops so both loads happen in one region.
Impact
Blocks HuggingFace SegFormer (issue #254). Running SegFormer on this branch, 10 of 1170 generated
kernels fail to parse; every one of them has >= 2 indirect MVINs writing the same scratchpad. The
decode head's bilinear upsample is the source.
Note this is not the error originally reported in #254 (NotImplementedError: Not supporting format),
which no longer reproduces. SegFormer now clears codegen for 145 kernels and all their gem5 runs
(~197 s with the functional Spike pass off), then dies here.
Related but distinct: #284 (the trace path does not model indirect access). This issue is about the
frontend emitting unparseable MLIR in the first place.
Environment
- branch
feature/togsim-cpp-trace - fails at
PyTorchSimFrontend/mlir/passes/__init__.py:78(Module.parse) via
extension_codecache.load -> run_python_passes, i.e. before gem5/Spike, so it is pure frontend
codegen and independent of the simulator binaries.
Activity
YWHyuk commentedon Jul 9, 2026
Root cause
Two coupled defects. Both stem from gathers of the same tensor aliasing one scratchpad.
D1 — the scratchpad cache key ignores which index buffer the gather used
MLIRKernel.load()(mlir_codegen_backend.py) first factors the indirect part of the index outinto
offset_desc:and
allocate_sram_buffercaches the buffer by(dram_name, str(raw_index)):After
convert_indirect_indexing, the residualraw_indexforx[i0]andx[i1]is the samestring.
offset_desc— the thing that actually distinguishes the two gathers — is not part of thekey. So both gathers get the same
%spadN, eachtogsim.transfer {indirect = true}overwrites theprevious gather's data, and the emitted load text (
affine.vector_load %spadN[...]) is identical.D2 — values are referenced across step (region) boundaries without bridging
push_step()opens a newaffine.forcompute region:The dedup cache is made region-safe, but a
CSEVariableproduced in an earlier step is stillprinted verbatim when a later step consumes it. The backend already knows where that value came from
—
load()recordsself.spad_buffer_dict[str(out)] = [sram_var, ...]— so it could re-emit the loadin the consuming region. It does not. The "steps bridge via spad" intent is not implemented for this
path.
How the two defects produce two very different symptoms
(a) No step boundary between the gathers -> silent wrong answer.
Both loads land in one region, identical load text, CSE collapses them:
Verified numerically —
x[i0] - x[i1]returns all zeros on npu:Filed separately as a correctness bug, since a user hits it with no
interpolateinvolved.(b) A step boundary between the gathers -> invalid MLIR (this issue).
Bilinear interpolate needs its own MVIN per gather, so
push_step()runs between them. The laterregion then references
%tmp57from the closed region ->use of undeclared SSA value name.Why fixing only one defect is not enough
%tmp57from aclosed region -> still a parse error.
%spad4, which nowholds gather Scalar #2's data -> still wrong (
B - B == 0).Fix sketch
buffer. The load texts then differ (
%spad4vs%spad9) and the CSE distinguishes them naturally.CSEVariableproduced in an earlier step,re-emit the load from
spad_buffer_dict[var]inside the consuming region.Worth checking SPAD pressure afterwards: bilinear keeps up to 4 gathers live.