[llvm] [IPO] Added attributor for identifying invariant loads (PR #141800)
Krzysztof Drewniak via llvm-commits
llvm-commits at lists.llvm.org
Wed May 28 13:22:00 PDT 2025
================
@@ -0,0 +1,220 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -passes=attributor %s -S | FileCheck %s
+
+ at G = global i32 zeroinitializer, align 4
+
+declare ptr @get_ptr()
+declare noalias ptr @get_noalias_ptr()
+
+define i32 @test_plain(ptr %ptr) {
+; CHECK-LABEL: define i32 @test_plain(
+; CHECK-SAME: ptr nofree noundef nonnull readonly align 4 captures(none) dereferenceable(4) [[PTR:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT: [[VAL:%.*]] = load i32, ptr [[PTR]], align 4
+; CHECK-NEXT: ret i32 [[VAL]]
+;
+ %val = load i32, ptr %ptr, align 4
+ ret i32 %val
+}
+
+define i32 @test_noalias_ptr(ptr noalias %ptr) {
+; CHECK-LABEL: define i32 @test_noalias_ptr(
+; CHECK-SAME: ptr noalias nofree noundef nonnull readonly align 4 captures(none) dereferenceable(4) [[PTR:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[VAL:%.*]] = load i32, ptr [[PTR]], align 4, !invariant.load [[META0:![0-9]+]]
----------------
krzysz00 wrote:
So I went digging, and the atomics guid says
> Alias analysis: Note that AA will return ModRef for anything Acquire or Release, and for the address accessed by any Monotonic operation.
So I'm pretty sure it's true than an acquire or stronger atomic - which is what you'd need for any sort of "wait for someone to modify this" - should block `readonly`
That is, it seems true that acquire semantics on a load (or stronger) make the loaded-from location not `readonly` in the LLVM sense.
And for weaker semantics - `monotonic`, say, - you're dealing with things like atomic counters where you can reorder things however you'd like so long as you don't lose writes.
That is, I claim that
```llvm
define void @f1(ptr addrspace(1) readonly noalias noundef %flag, ...) {
; ..., no uses of %flag
spin:
%can.proceed = load i1, ptr addrspace(1) %flag, acquire
br i1 %can.proceed, label %cont, label %spin
cont:
; ..., more things, don't modify %flag
ret void
}
```
is ill-formed and if the attributor *adds* the `readonly` flag to code like this it's wrong
https://github.com/llvm/llvm-project/pull/141800
More information about the llvm-commits
mailing list