Repository navigation
Description
What happened?
I have a model where a call to stablehlo.reverse gives very wrong answers. Instead of reversing elements in a tensor, it generates new values that aren't in the original tensor. This doesn't look like a direct issue with that operation, however, since it works on its own; it only fails based on what happened before it.
Here is a simple excerpt (constants removed to keep things brief, full MLIR files are attached). The following works:
%1 = stablehlo.constant dense<...> : tensor<50x2x16xf32>
%2 = stablehlo.reverse %1, dims = [1] : tensor<50x2x16xf32>
However, if there are previous operations
%cst = stablehlo.constant dense<...> : tensor<50x3x32xf32>
%cst_0 = stablehlo.constant dense<...> : tensor<50x6x16xf32>
%c = stablehlo.constant dense<...> : tensor<50x8xi32>
%cst_1 = stablehlo.constant dense<...> : tensor<2x32xf32>
%0:6 = call @combine(%cst_1, %cst, %cst_0, %c) : (tensor<2x32xf32>, tensor<50x3x32xf32>, tensor<50x6x16xf32>, tensor<50x8xi32>) -> (tensor<50x3x32xf32>, tensor<50x6xf32>, tensor<50x6x16xf32>, tensor<50x6x16x8xf32>, tensor<50xi32>, tensor<2x32xf32>)
%1 = stablehlo.slice %0#2 [0:50, 4:6, 0:16] : (tensor<50x6x16xf32>) -> tensor<50x2x16xf32>
%2 = stablehlo.reverse %1, dims = [1] : tensor<50x2x16xf32>
then the answer is wrong. In the included models, I have made certain that the input to stablehlo.reverse is the same in both cases. The only difference is whether these previous operation are included.
If I isolate the stablehlo.reverse by putting in a barrier, then things work again
%1 = stablehlo.slice %0#2 [0:50, 4:6, 0:16] : (tensor<50x6x16xf32>) -> tensor<50x2x16xf32>
%2 = stablehlo.optimization_barrier %1 : tensor<50x2x16xf32>
%3 = stablehlo.reverse %2, dims = [1] : tensor<50x2x16xf32>
I have included 3 models:
- flip_good.mlir - simple model that just calls
stablehlo.reverse. - flip_bad.mlir - repro case that gets the wrong answer.
- flip_barrier.mlir - same as the bad case but with an optimization barrier that causes it to get the right answer again.
All three models output the input and output tensors of stablehlo.reverse so that it's easy to compare. This shows that the same input is used in all models, but that flip_bad.mlir gets the wrong result.
This was tested on CPU, Vulkan, and CUDA, and IREE behaves the same way in all cases.
Steps to reproduce your issue
- Compile
flip_good.mlir,flip_bad,mlirandflip_barrier.mlir. - Note that output 1 matches for all three models, but that
flip_bad.mlirhas an incorrect second output.
What component(s) does this issue relate to?
Compiler
Version information
Tested with 3.11.0rc20260224, but I've seen the issue as far back as 3.5.0
Additional context
No response
Activity
MaheshRavishankar commentedon Mar 3, 2026
@IanWood1 when you have some time can you triage this?
pstarkcdpr commentedon Mar 3, 2026
Okay, I've managed to reduce this even further. I'm including an updated model that has the issue, but only does 2 operations:
I've added updated models. The good model is the same. The bad and optimization barrier models have been simplified:
updated_models.zip
IanWood1 commentedon Mar 3, 2026
Yep, that makes sense. This is an issue with
vector.gatheroperating on strided memrefs. I'm trying to get a reproducer just before the pass generates the incorrect IR.IanWood1 commentedon Mar 4, 2026
It looks like the problem is that vectorization is trying to convert the following to a
vector.gather(https://github.com/llvm/llvm-project/blob/5e5f7efd77064a1b05b2596dfab39966ce828359/mlir/lib/Dialect/Linalg/Transforms/Vectorization.cpp#L1194-L1205)But this isn't actually representable as
vector.gather.indicesonly modifies the innermost index during the gather. The lowering tries to get around this restriction by overflowing the innermost index. This only works when the gather is on an identity stride memref which this is not. @Groverkss since I think you have the most context on this, does this make sense to you?Here's the gist with the logs
flip_bad1.mlirhttps://gist.github.com/IanWood1/e92ae5d6fd19226e72d49ed6c42e2bfbIanWood1 commentedon Mar 4, 2026
Looks like the same issue as #20011
pstarkcdpr commentedon Mar 4, 2026
Thanks @IanWood1. It looks like this will be non-trivial to fix? This issue is kind of blocking us, so I'm wondering if there are any workarounds or a way for the compiler to throw an error instead of getting the wrong answer (I'd prefer to know in advance if the model will fail rather than see unexpected behaviour)?
Based on the above failure case, I threw together a quick unit test in the style of
tests/e2e/stablehlo_opsthat reproduces the issue:MaheshRavishankar commentedon Mar 4, 2026
@hanhanW I am moving this issue to you. You might have more context on this (its the reverse operation). I think we used to have a dedicated reverse operation, but it is now removed, and might be causing issues.
hanhanW commentedon Mar 4, 2026
Let me take a look, I'll check if it is a pre-existing issue that @IanWood1 pointed out (which will be very useful).
pstarkcdpr commentedon Mar 4, 2026
Okay, now that we know the pattern, I think that we can work around this issue in JAX. Basically, whenever we have a
jnp.flipthat works on a slice, we can just reverse the operations (flip then slice). If there isn't a quick fix for the compiler, I'm wondering if there's an easy way to add a compile error when the bad situation arises. It doesn't need to be committed, we build a local compiler anyways. This would give us confidence that we've worked around all the instances of this issue.IanWood1 commentedon Mar 4, 2026
If you just need something hacky, this should identify when it will produce incorrect results:
hanhanW commentedon Mar 4, 2026
Just a heads-up that I got a fix (but I need to verify and review) from Claude:
Issue #23637 / #20011: vector.gather produces incorrect results on strided memrefs
Root cause chain:
Fix: Modified Gather1DToConditionalLoads in LowerVectorGather.cpp to detect non-contiguous strided memrefs and delinearize the 1D gather offset back into proper multi-dimensional load indices:
Verification:
Note: The root cause is in MLIR's calculateGatherOffset() which creates vector.gather with indices that overflow the innermost dimension — technically undefined behavior per the vector.gather spec. This should be fixed upstream.
The fix in Gather1DToConditionalLoads handles it at the lowering level for all backends that use populateVectorGatherToConditionalLoadPatterns (LLVMCPU, SPIRV).
hanhanW commentedon Mar 5, 2026
Here is the fix: llvm/llvm-project#184706
@IanWood1 feel free to review if you want to.
MaheshRavishankar commentedon Mar 5, 2026
Fast progress! Thanks @hanhanW
pstarkcdpr commentedon Mar 5, 2026
Thanks @hanhanW. I tried your fix in my local compiler build and, so far, it looks like that fixes a bunch of issues for us. JAX and IREE are matching now.
hanhanW commentedon Mar 13, 2026
The upstream change is landed. I'll close the issue after it propagates to IREE.
hanhanW commentedon Mar 17, 2026
Confirmed that it is fixed on ToT, closing the issue