Repository navigation
Description
What happened?
llvm/llvm-project#133231 seems to be breaking our subbyte emulation tests.
A simple repro is https://gist.github.com/raikonenfnu/1ea07b7e231d8997bfa1c29502df637d
Based on the above test, currently IREE is generating code that has different trailing dim and "virtually"/fake non constant offset into the store vector.store %0[%2] where %2 is actually a constant, but since it is inside a branch region, %c0 becomes %2 which is a region argument and is now a non-constant.
The condition above leads our program into this code path where it cannot determine !foldedNumFrontPadElems and fails this lowering. as seen in:
https://github.com/llvm/llvm-project/blob/2de936b6eb38e7a37224a97c2a22aa79b9dfb9dc/mlir/lib/Dialect/Vector/Transforms/VectorEmulateNarrowType.cpp#L619-L629
IIUC, that PR is somewhat correct, as long as the trailing dim does not match, we may need partial stores.
I don't think I have enough context on the best way to solve this. We can:
- Fold away that region since the condition to the regions are constants or somehow constant fold S.T we have constant offset in vector.store
- Add some code in vector dialect's EmulateNarrowType vector::StoreOp conversion to handle non constant cases through series of bitcasting and memref.generic_atomic_rmws. (this seems much harder and require some more thinking)
Steps to reproduce your issue
-
wget https://gist.githubusercontent.com/raikonenfnu/1ea07b7e231d8997bfa1c29502df637d/raw/6e3311f6794dd5afeaeac0f82422cbe498d67272/emulate_failure.mlir
-
iree-opt --iree-codegen-emulate-narrow-type emulate_failure.mlir -o out.mlir
emulate.mlir:18:3: error: failed to legalize operation 'vector.store' that was explicitly marked illegal
What component(s) does this issue relate to?
No response
Version information
No response
Additional context
No response
Activity
Integrate llvm/llvm-project@1c8e5e2
Integrate llvm/llvm-project@1c8e5e2 (#20646)
krzysz00 commentedon Apr 26, 2025
So, I think what should be done there is that the slow path should be guarded by a runtime condition as in llvm/llvm-project#135014
But, in addition, the narrow type emulation should be moved before scf-to-cf - heck, before OptimizeIntArithmetic - so that we can take advantage of divisibility analysis
lialan commentedon Apr 28, 2025
That is the way to go. Currently the transformation is also unable to handle some other cases, all needs runtime check.
krzysz00 commentedon Apr 28, 2025
To clarify, which "other cases"?
lialan commentedon Apr 29, 2025
All the cases with a non-constant storing index.
lialan commentedon Apr 29, 2025
@raikonenfnu Was the emulation in the case of non-constant/dynamic indexing already working before? I think it was not working as intended even before this problematic patch.
krzysz00 commentedon Apr 29, 2025
I do think the patch was fixing a real correctness issue, but didn't do it in a performant way
Integrate llvm/llvm-project@5953f19 (#20657)
banach-space commentedon Apr 30, 2025
Sorry that my patch is causing this issue - I'd like to see if I can help resolve it.
To start, I’m trying to better understand the underlying problem. When I lower the reproducer (with my patch reverted), I get the following IR:
What stands out to me is that
%2(the branch+load/store index) doesn’t appear to be used to mask the input of thevector.store. I’m not yet sure what this constant (that's used for masking) represents ...... but it doesn't look correct. So, it feels like the failing test is exercising an incorrect lowering path in upstream MLIR? And my patch effectively disables that.
Does this agree with your understanding?
Integrate llvm/llvm-project@7b70fc7 (#20674)
Integrate llvm/llvm-project@1c8e5e2 (iree-org#20646)
Integrate llvm/llvm-project@5953f19 (iree-org#20657)
10 remaining items
krzysz00 commentedon May 30, 2025
We could also get around to adding
align = Ntovector.loadandvector.storeand such, which is @efric (and then an absentalign =or analign = 0would mean we need the unaligned case)krzysz00 commentedon May 30, 2025
Another thing that can be done upstream is to put the slow and the fast cases in two arms of an if statement and let integer range analysis clear off the slow path.
This is what got done for the buffer OOB stuff
hanhanW commentedon May 30, 2025
I'd prefer not using this approach atm, because we can get all the needed information from IR. I can be convinced if there are other use cases.
I'm not sure how integrate range analysis works with upstream patterns. It seems like the analysis should only be run once because of efficiency. Then we collect the ops and apply the transform. Also, the analysis is only available in IREE, so I'm not sure how they can be connected.
Anyway, I think we have a solid plan, and we can fix it if someone pick this up.
benvanik commentedon May 30, 2025
Agreed RE upstream analysis interaction - I think we could provide interface implementations if upstream uses them that distills the info (I don't think we do). The particular benefit of the
util.assume.intop is the correlation between independent SSA values ("if A is udiv=4 then B is udiv=8" or "if A is umin=8 then B is umin=16"). We can do a lot more with that (@qedawkins specialization is an example - specializing just for the sets of assumptions vs the full combinatorial explosion of them). We should probably plan on keeping anything important relying on analysis on the IREE side but exposing what we can (the union/intersection of all our assume pairs exposed as a single value range).krzysz00 commentedon May 30, 2025
Integer range analysis lives upstream - the only thing that doesn't is divisibility analysis, and that should be fairly easily upstreamable
And even if integer range analysis doesn't work, having the pattern gener9ate
instead of just generating
slowPathin cases where the divisibility isn't statically obvious will allow LLVM to take care of it[Integrate] Mirror and prioritize the old ConvertVectorStore pattern. (…
Integrate llvm/llvm-project@1c8e5e2 (#20646)
Integrate llvm/llvm-project@5953f19 (#20657)
Integrate llvm/llvm-project@7b70fc7 (#20674)
[Integrate] Mirror and prioritize the old ConvertVectorStore pattern. (…
[Codegen][NFC] Drop the workaround from EmulateNarrowType pass. (iree…
[Codegen][NFC] Drop the workaround from EmulateNarrowType pass. (#23319)
[Codegen][NFC] Drop the workaround from EmulateNarrowType pass. (iree…