[mlir][LLVM] Model side effects of volatile and atomic load-store #65730
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
According to the LLVM language reference, both volatile memory operations and atomic operations (except unordered) do not simply read memory but also perform write operations on arbitrary memory[0][1].
In the case of volatile memory operations, this is the case due to the read possibly having target specific properties. A common real-world situation where this happens is reading memory mapped registers on an MCU for example. Atomic operations are more special. They form a kind of memory barrier which from the perspective of the optimizer/lang-ref makes writes from other threads visible in the current thread. Any kind of synchronization can therefore conservatively be modeled as a write-effect.
This PR therefore adjusts the side effects of
llvm.load
andllvm.store
to add unknown global read and write effects if they are either atomic or volatile.Regarding testing: I am not sure how to best test this change for
llvm.store
and the "globalness" of the effect that isn't just a unit test checking that the output matches exactly. For the time being, I added a test making sure thatllvm.load
does not get DCEd in aforementioned cases.Related logic in LLVM proper:
llvm-project/llvm/lib/IR/Instruction.cpp
Lines 638 to 676 in 3398744
llvm-project/llvm/include/llvm/IR/Instructions.h
Lines 258 to 262 in 3398744
[0] https://llvm.org/docs/LangRef.html#volatile-memory-accesses
[1] https://llvm.org/docs/Atomics.html#monotonic