[llvm] [llvm] Support invariant.load on readonly intrinsics (PR #205916)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Sat Jun 27 02:10:29 PDT 2026
dtcxzyw wrote:
> Mark all the memory locations loaded by the instruction as invariant.
I am not sure if we should treat it as a global side effect (i.e., add a shadow state tracking which byte is immutable) or use the "access through the pointer" semantics like `initializes`. The former one is simple, but the state can be erased by `llvm.invariant.end` like https://godbolt.org/z/9YYhrzh9q.
If we use the second interpretation, I would make it a parameter attribute rather than metadata. This avoids the definition of the behaviour against internal allocations. However, I don't know how this works with intrinsics that don't access memory via parameters (e.g., `llvm.read_register`).
```
%val1 = call i32 @readonly_call(ptr invariant(4) %p) ; First 4 bytes are immutable after the call returns. Use -1 to indicate that the whole region starting from %p to the end of the underlying object is immutable.
; some clobbers
%val2 = call i32 @readonly_call(ptr %p) ; can be CSEed (some additional attributes are required)
%load1 = load i32, ptr %p
%load2 = load i32, ptr %p ; can be CSEed
```
I think it's too early to generalize it to arbitrary calls. Unless we see more missing opportunities for optimization, I suggest limiting it to a few intrinsics instead of inferring from other fn attributes.
https://github.com/llvm/llvm-project/pull/205916
More information about the llvm-commits
mailing list