Skip to content

[MachineLICM] Don't allow hoisting invariant loads across mem barrier. #116987

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

Merged
merged 2 commits into from
Nov 21, 2024

Conversation

fhahn
Copy link
Contributor

@fhahn fhahn commented Nov 20, 2024

The improvements in 63917e1 / #70796 do not check for memory barriers/unmodelled sideeffects, which means we may incorrectly hoist loads across memory barriers.

Fix this by checking any machine instruction in the loop is a load-fold barrier.

@llvmbot
Copy link
Member

llvmbot commented Nov 20, 2024

@llvm/pr-subscribers-backend-aarch64

Author: Florian Hahn (fhahn)

Changes

The improvements in 63917e1 / #70796 do not check for memory barriers/unmodelled sideeffects, which means we may incorrectly hoist loads across memory barriers.

Fix this by checking any machine instruction in the loop is a load-fold barrier.


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

2 Files Affected:

  • (modified) llvm/lib/CodeGen/MachineLICM.cpp (+1-1)
  • (modified) llvm/test/CodeGen/AArch64/machine-licm-hoist-load.ll (+2-2)
diff --git a/llvm/lib/CodeGen/MachineLICM.cpp b/llvm/lib/CodeGen/MachineLICM.cpp
index 48c901b8d06d61..00e3b5def5d108 100644
--- a/llvm/lib/CodeGen/MachineLICM.cpp
+++ b/llvm/lib/CodeGen/MachineLICM.cpp
@@ -1501,7 +1501,7 @@ void MachineLICMImpl::InitializeLoadsHoistableLoops() {
       if (!AllowedToHoistLoads[Loop])
         continue;
       for (auto &MI : *MBB) {
-        if (!MI.mayStore() && !MI.isCall() &&
+        if (!MI.isLoadFoldBarrier() && !MI.mayStore() && !MI.isCall() &&
             !(MI.mayLoad() && MI.hasOrderedMemoryRef()))
           continue;
         for (MachineLoop *L = Loop; L != nullptr; L = L->getParentLoop())
diff --git a/llvm/test/CodeGen/AArch64/machine-licm-hoist-load.ll b/llvm/test/CodeGen/AArch64/machine-licm-hoist-load.ll
index 932a5af264a000..17f8263560430d 100644
--- a/llvm/test/CodeGen/AArch64/machine-licm-hoist-load.ll
+++ b/llvm/test/CodeGen/AArch64/machine-licm-hoist-load.ll
@@ -499,16 +499,16 @@ for.exit:                                 ; preds = %for.body
 
 @a = external local_unnamed_addr global i32, align 4
 
-; FIXME: Load hoisted out of the loop across memory barriers.
+; Make sure the load is not hoisted out of the loop across memory barriers.
 define i32 @load_between_memory_barriers() {
 ; CHECK-LABEL: load_between_memory_barriers:
 ; CHECK:       // %bb.0:
 ; CHECK-NEXT:    adrp x8, :got:a
 ; CHECK-NEXT:    ldr x8, [x8, :got_lo12:a]
-; CHECK-NEXT:    ldr w0, [x8]
 ; CHECK-NEXT:  .LBB8_1: // %loop
 ; CHECK-NEXT:    // =>This Inner Loop Header: Depth=1
 ; CHECK-NEXT:    //MEMBARRIER
+; CHECK-NEXT:    ldr w0, [x8]
 ; CHECK-NEXT:    //MEMBARRIER
 ; CHECK-NEXT:    cbz w0, .LBB8_1
 ; CHECK-NEXT:  // %bb.2: // %exit

Copy link
Contributor

@david-arm david-arm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

The improvements in 63917e1 / llvm#70796 do not check for memory
barriers/unmodelled sideeffects, which means we may incorrectly hoist
loads across memory barriers.

Fix this by checking any machine instruction in the loop is a load-fold
barrier.
@fhahn fhahn force-pushed the machinelicm-membarrier branch from d5b0f71 to b62f928 Compare November 20, 2024 19:02
@fhahn fhahn merged commit ef102b4 into llvm:main Nov 21, 2024
8 checks passed
@fhahn fhahn deleted the machinelicm-membarrier branch November 21, 2024 10:25
@fhahn
Copy link
Contributor Author

fhahn commented Nov 21, 2024

/cherry-pick a9b3ec1 ef102b4

@llvmbot
Copy link
Member

llvmbot commented Nov 21, 2024

/cherry-pick a9b3ec1 ef102b4

Error: Command failed due to missing milestone.

@fhahn fhahn added this to the LLVM 19.X Release milestone Nov 21, 2024
@fhahn
Copy link
Contributor Author

fhahn commented Nov 21, 2024

/cherry-pick a9b3ec1 ef102b4

@llvmbot
Copy link
Member

llvmbot commented Nov 21, 2024

/pull-request #117154

tru pushed a commit to llvmbot/llvm-project that referenced this pull request Nov 25, 2024
llvm#116987)

The improvements in 63917e1 / llvm#70796 do not check for memory
barriers/unmodelled sideeffects, which means we may incorrectly hoist
loads across memory barriers.

Fix this by checking any machine instruction in the loop is a load-fold
barrier.

PR: llvm#116987
(cherry picked from commit ef102b4)
fhahn added a commit to fhahn/llvm-project that referenced this pull request Jan 6, 2025
llvm#116987)

The improvements in 63917e1 / llvm#70796 do not check for memory
barriers/unmodelled sideeffects, which means we may incorrectly hoist
loads across memory barriers.

Fix this by checking any machine instruction in the loop is a load-fold
barrier.

PR: llvm#116987
(cherry picked from commit ef102b4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

3 participants