Skip to content

Attributor: Propagate align to atomicrmw instructions #134837

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

arsenm
Copy link
Contributor

@arsenm arsenm commented Apr 8, 2025

Partially fixes #134480

Copy link
Contributor Author

arsenm commented Apr 8, 2025

@arsenm arsenm requested review from jdoerfert and shiltian April 8, 2025 10:52
@arsenm arsenm marked this pull request as ready for review April 8, 2025 10:52
@llvmbot
Copy link
Member

llvmbot commented Apr 8, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Matt Arsenault (arsenm)

Changes

Partially fixes #134480


Full diff: https://github.com/llvm/llvm-project/pull/134837.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/IPO/AttributorAttributes.cpp (+15-5)
  • (modified) llvm/test/Transforms/Attributor/align-atomic.ll (+1-1)
diff --git a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp
index a477c90bb4f45..717ba7f688548 100644
--- a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp
+++ b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp
@@ -5283,7 +5283,7 @@ struct AAAlignImpl : AAAlign {
 
   /// See AbstractAttribute::manifest(...).
   ChangeStatus manifest(Attributor &A) override {
-    ChangeStatus LoadStoreChanged = ChangeStatus::UNCHANGED;
+    ChangeStatus InstrChanged = ChangeStatus::UNCHANGED;
 
     // Check for users that allow alignment annotations.
     Value &AssociatedValue = getAssociatedValue();
@@ -5297,7 +5297,7 @@ struct AAAlignImpl : AAAlign {
             STATS_DECLTRACK(AAAlign, Store,
                             "Number of times alignment added to a store");
             SI->setAlignment(getAssumedAlign());
-            LoadStoreChanged = ChangeStatus::CHANGED;
+            InstrChanged = ChangeStatus::CHANGED;
           }
       } else if (auto *LI = dyn_cast<LoadInst>(U.getUser())) {
         if (LI->getPointerOperand() == &AssociatedValue)
@@ -5305,8 +5305,18 @@ struct AAAlignImpl : AAAlign {
             LI->setAlignment(getAssumedAlign());
             STATS_DECLTRACK(AAAlign, Load,
                             "Number of times alignment added to a load");
-            LoadStoreChanged = ChangeStatus::CHANGED;
+            InstrChanged = ChangeStatus::CHANGED;
           }
+      } else if (auto *RMW = dyn_cast<AtomicRMWInst>(U.getUser())) {
+        if (RMW->getPointerOperand() == &AssociatedValue) {
+          if (RMW->getAlign() < getAssumedAlign()) {
+            STATS_DECLTRACK(AAAlign, AtomicRMW,
+                            "Number of times alignment added to atomicrmw");
+
+            RMW->setAlignment(getAssumedAlign());
+            InstrChanged = ChangeStatus::CHANGED;
+          }
+        }
       }
     }
 
@@ -5315,8 +5325,8 @@ struct AAAlignImpl : AAAlign {
     Align InheritAlign =
         getAssociatedValue().getPointerAlignment(A.getDataLayout());
     if (InheritAlign >= getAssumedAlign())
-      return LoadStoreChanged;
-    return Changed | LoadStoreChanged;
+      return InstrChanged;
+    return Changed | InstrChanged;
   }
 
   // TODO: Provide a helper to determine the implied ABI alignment and check in
diff --git a/llvm/test/Transforms/Attributor/align-atomic.ll b/llvm/test/Transforms/Attributor/align-atomic.ll
index 764ed7419a079..0931c14685a87 100644
--- a/llvm/test/Transforms/Attributor/align-atomic.ll
+++ b/llvm/test/Transforms/Attributor/align-atomic.ll
@@ -16,7 +16,7 @@ define i32 @atomicrmw_add_no_op(ptr align 4 %ptr, i32 %val) {
 define i32 @atomicrmw_add_propagate(ptr align 8 %ptr, i32 %val) {
 ; CHECK-LABEL: define i32 @atomicrmw_add_propagate(
 ; CHECK-SAME: ptr nofree noundef nonnull align 8 captures(none) dereferenceable(4) [[PTR:%.*]], i32 [[VAL:%.*]]) #[[ATTR0]] {
-; CHECK-NEXT:    [[RESULT:%.*]] = atomicrmw add ptr [[PTR]], i32 [[VAL]] seq_cst, align 2
+; CHECK-NEXT:    [[RESULT:%.*]] = atomicrmw add ptr [[PTR]], i32 [[VAL]] seq_cst, align 8
 ; CHECK-NEXT:    ret i32 [[RESULT]]
 ;
   %result = atomicrmw add ptr %ptr, i32 %val seq_cst, align 2

Copy link
Contributor Author

arsenm commented Apr 8, 2025

Merge activity

  • Apr 8, 11:01 AM EDT: A user started a stack merge that includes this pull request via Graphite.
  • Apr 8, 11:08 AM EDT: Graphite rebased this pull request as part of a merge.
  • Apr 8, 11:12 AM EDT: A user merged this pull request with Graphite.

@arsenm arsenm force-pushed the users/arsenm/attributor/add-baseline-tests-atomic-align-propagate branch 2 times, most recently from 5fa7aac to 639fefd Compare April 8, 2025 15:06
Base automatically changed from users/arsenm/attributor/add-baseline-tests-atomic-align-propagate to main April 8, 2025 15:08
@arsenm arsenm force-pushed the users/arsenm/attributor/propagate-align-to-atomicrmw-instructions branch from 396afde to 6fa8d75 Compare April 8, 2025 15:08
@arsenm arsenm merged commit 66f0343 into main Apr 8, 2025
6 of 10 checks passed
@arsenm arsenm deleted the users/arsenm/attributor/propagate-align-to-atomicrmw-instructions branch April 8, 2025 15:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AAAlign only handles load and store uses
3 participants