Skip to content

LiveRangeShrink: Early exit when encountering a code motion barrier. #136806

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

pcc
Copy link
Contributor

@pcc pcc commented Apr 23, 2025

Without this, we end up with quadratic behavior affecting functions with
large numbers of code motion barriers, such as CFI jump tables.

As a drive-by cleanup, remove a redundant store to SawStore in this
pass as it is also done by isSafeToMove.

pcc added 2 commits April 22, 2025 20:54
Created using spr 1.3.6-beta.1

[skip ci]
Created using spr 1.3.6-beta.1
@llvmbot
Copy link
Member

llvmbot commented Apr 23, 2025

@llvm/pr-subscribers-llvm-regalloc

Author: Peter Collingbourne (pcc)

Changes

Without this, we end up with quadratic behavior affecting functions with
large numbers of code motion barriers, such as CFI jump tables.


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

1 Files Affected:

  • (modified) llvm/lib/CodeGen/LiveRangeShrink.cpp (+12-3)
diff --git a/llvm/lib/CodeGen/LiveRangeShrink.cpp b/llvm/lib/CodeGen/LiveRangeShrink.cpp
index dae7e14e54aae..46e837c086c5d 100644
--- a/llvm/lib/CodeGen/LiveRangeShrink.cpp
+++ b/llvm/lib/CodeGen/LiveRangeShrink.cpp
@@ -95,14 +95,24 @@ static MachineInstr *FindDominatedInstruction(MachineInstr &New,
   return Old;
 }
 
+static bool isCodeMotionBarrier(MachineInstr &MI) {
+  return MI.hasUnmodeledSideEffects() && !MI.isPseudoProbe();
+}
+
 /// Builds Instruction to its dominating order number map \p M by traversing
 /// from instruction \p Start.
 static void BuildInstOrderMap(MachineBasicBlock::iterator Start,
                               InstOrderMap &M) {
   M.clear();
   unsigned i = 0;
-  for (MachineInstr &I : make_range(Start, Start->getParent()->end()))
+  bool SawStore = false;
+  for (MachineInstr &I : make_range(Start, Start->getParent()->end())) {
+    if (I.mayStore())
+      SawStore = true;
+    if (!I.isSafeToMove(SawStore) && isCodeMotionBarrier(I))
+      break;
     M[&I] = i++;
+  }
 }
 
 bool LiveRangeShrink::runOnMachineFunction(MachineFunction &MF) {
@@ -166,8 +176,7 @@ bool LiveRangeShrink::runOnMachineFunction(MachineFunction &MF) {
         // If MI has side effects, it should become a barrier for code motion.
         // IOM is rebuild from the next instruction to prevent later
         // instructions from being moved before this MI.
-        if (MI.hasUnmodeledSideEffects() && !MI.isPseudoProbe() &&
-            Next != MBB.end()) {
+        if (isCodeMotionBarrier(MI) && Next != MBB.end()) {
           BuildInstOrderMap(Next, IOM);
           SawStore = false;
         }

@pcc pcc requested a review from arsenm April 23, 2025 04:01
Comment on lines +98 to +100
static bool isCodeMotionBarrier(MachineInstr &MI) {
return MI.hasUnmodeledSideEffects() && !MI.isPseudoProbe();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Document this? What is the exact problematic condition? We have several different barrier concepts already spread in MachineInstr and TargetInstrInfo, can we use one of those

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a comment that describes the situation to the best of my understanding. I don't think the existing ones can be used because the condition is specific to what this pass is doing.

for (MachineInstr &I : make_range(Start, Start->getParent()->end())) {
if (I.mayStore())
SawStore = true;
if (!I.isSafeToMove(SawStore) && isCodeMotionBarrier(I))
Copy link
Contributor

Choose a reason for hiding this comment

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

The MI.hasUnmodeledSideEffects() is redundant with the isSafeToMove check?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct, updated this code to only call one.

While reading the code I observed that the code to update SawStore in this pass is redundant with what isSafeToMove is doing, so I removed it.

pcc added 2 commits April 23, 2025 16:07
Created using spr 1.3.6-beta.1

[skip ci]
Created using spr 1.3.6-beta.1
@pcc pcc changed the base branch from users/pcc/spr/main.liverangeshrink-early-exit-when-encountering-a-code-motion-barrier to main April 24, 2025 19:44
@pcc pcc merged commit 4ed8bfd into main Apr 24, 2025
13 of 14 checks passed
@pcc pcc deleted the users/pcc/spr/liverangeshrink-early-exit-when-encountering-a-code-motion-barrier branch April 24, 2025 19:44
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request May 6, 2025
…n barrier.

Without this, we end up with quadratic behavior affecting functions with
large numbers of code motion barriers, such as CFI jump tables.

As a drive-by cleanup, remove a redundant store to SawStore in this
pass as it is also done by isSafeToMove.

Reviewers: arsenm

Reviewed By: arsenm

Pull Request: llvm/llvm-project#136806
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Without this, we end up with quadratic behavior affecting functions with
large numbers of code motion barriers, such as CFI jump tables.

As a drive-by cleanup, remove a redundant store to SawStore in this
pass as it is also done by isSafeToMove.

Reviewers: arsenm

Reviewed By: arsenm

Pull Request: llvm#136806
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Without this, we end up with quadratic behavior affecting functions with
large numbers of code motion barriers, such as CFI jump tables.

As a drive-by cleanup, remove a redundant store to SawStore in this
pass as it is also done by isSafeToMove.

Reviewers: arsenm

Reviewed By: arsenm

Pull Request: llvm#136806
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Without this, we end up with quadratic behavior affecting functions with
large numbers of code motion barriers, such as CFI jump tables.

As a drive-by cleanup, remove a redundant store to SawStore in this
pass as it is also done by isSafeToMove.

Reviewers: arsenm

Reviewed By: arsenm

Pull Request: llvm#136806
Ankur-0429 pushed a commit to Ankur-0429/llvm-project that referenced this pull request May 9, 2025
Without this, we end up with quadratic behavior affecting functions with
large numbers of code motion barriers, such as CFI jump tables.

As a drive-by cleanup, remove a redundant store to SawStore in this
pass as it is also done by isSafeToMove.

Reviewers: arsenm

Reviewed By: arsenm

Pull Request: llvm#136806
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.

3 participants