Skip to content
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

[AArch64] Resolve FIXME: Use scavengeRegisterBackwards to find the best unused register #78910

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

AtariDreams
Copy link
Contributor

No description provided.

@llvmbot
Copy link
Collaborator

llvmbot commented Jan 21, 2024

@llvm/pr-subscribers-llvm-regalloc
@llvm/pr-subscribers-backend-amdgpu

@llvm/pr-subscribers-backend-aarch64

Author: AtariDreams (AtariDreams)

Changes

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

1 Files Affected:

  • (modified) llvm/lib/Target/AArch64/AArch64SpeculationHardening.cpp (+2-7)
diff --git a/llvm/lib/Target/AArch64/AArch64SpeculationHardening.cpp b/llvm/lib/Target/AArch64/AArch64SpeculationHardening.cpp
index a991d645eb6f405..86135ef39d82148 100644
--- a/llvm/lib/Target/AArch64/AArch64SpeculationHardening.cpp
+++ b/llvm/lib/Target/AArch64/AArch64SpeculationHardening.cpp
@@ -300,13 +300,8 @@ bool AArch64SpeculationHardening::instrumentControlFlow(
       RS.enterBasicBlock(MBB);
     else
       RS.backward(I);
-    // FIXME: The below just finds *a* unused register. Maybe code could be
-    // optimized more if this looks for the register that isn't used for the
-    // longest time around this place, to enable more scheduling freedom. Not
-    // sure if that would actually result in a big performance difference
-    // though. Maybe RegisterScavenger::findSurvivorBackwards has some logic
-    // already to do this - but it's unclear if that could easily be used here.
-    Register TmpReg = RS.FindUnusedReg(&AArch64::GPR64commonRegClass);
+    Register TmpReg = RS.scavengeRegisterBackwards(AArch64::GPR64commonRegClass,
+                                                   I, false, 0, false);
     LLVM_DEBUG(dbgs() << "RS finds "
                       << ((TmpReg == 0) ? "no register " : "register ");
                if (TmpReg != 0) dbgs() << printReg(TmpReg, TRI) << " ";

@AtariDreams AtariDreams marked this pull request as draft January 21, 2024 20:08
Copy link

github-actions bot commented Jan 22, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@AtariDreams AtariDreams marked this pull request as ready for review January 22, 2024 19:22
@AtariDreams AtariDreams changed the title Resolve FIXME: use RegScavenger to find the best unused register [Aarch64] Resolve FIXME: Use scavengeRegisterBackwards to find the best unused register Jan 22, 2024
@AtariDreams AtariDreams changed the title [Aarch64] Resolve FIXME: Use scavengeRegisterBackwards to find the best unused register [AArch64] Resolve FIXME: Use scavengeRegisterBackwards to find the best unused register Jan 22, 2024
@AtariDreams AtariDreams marked this pull request as draft January 22, 2024 20:10
@arsenm arsenm requested review from jayfoad, arsenm, MatzeB and qcolombet and removed request for jayfoad January 24, 2024 01:02
Comment on lines 300 to 308
if (I == MBB.begin()) {
RS.enterBasicBlock(MBB);
else
// Cannot scavenge backwards as we are at the start of the basic block
TmpReg = RS.FindUnusedReg(&AArch64::GPR64commonRegClass);
} else {
RS.backward(I);
// FIXME: The below just finds *a* unused register. Maybe code could be
// optimized more if this looks for the register that isn't used for the
// longest time around this place, to enable more scheduling freedom. Not
// sure if that would actually result in a big performance difference
// though. Maybe RegisterScavenger::findSurvivorBackwards has some logic
// already to do this - but it's unclear if that could easily be used here.
Register TmpReg = RS.FindUnusedReg(&AArch64::GPR64commonRegClass);
TmpReg = RS.scavengeRegisterBackwards(AArch64::GPR64commonRegClass, I,
false, 0, false);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can call scavengeRegisterBackwards even at the start of a block - can't you?

    if (I == MBB.begin())
      RS.enterBasicBlock(MBB);
    else
      RS.backward(I);
    TmpReg = RS.scavengeRegisterBackwards(AArch64::GPR64commonRegClass, I,
                                          false, 0, false);

The only reason this code was special casing I == MBB.begin() in the first place is for efficiency, not for correctness. The assumption is that enterBasicBlock is more efficient than backward which might have to step over arbitrarily many instructions.

@@ -11,34 +12,148 @@
# - other direct branches don't seem to be generated by the AArch64 codegen
--- |
define void @nobranch_fallthrough(i32 %a, i32 %b) speculative_load_hardening {
; CHECK-LABEL: nobranch_fallthrough:
Copy link
Collaborator

Choose a reason for hiding this comment

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

If you use the autogenerated test, make sure to clear the all check lines.
As is, there are labels that are repeated which may lead in weird failures with FileCheck. (e.g., the label nobranch_fallthrough appears twice.)

@@ -11,34 +12,148 @@
# - other direct branches don't seem to be generated by the AArch64 codegen
--- |
define void @nobranch_fallthrough(i32 %a, i32 %b) speculative_load_hardening {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you add a comment of what needs to happen or must not happen in the comment?

The manual checks were conveying this in a more direct way (but broken in its own way :P).

Copy link
Contributor

@arsenm arsenm left a comment

Choose a reason for hiding this comment

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

Previous comments

Comment on lines -303 to -308
// FIXME: The below just finds *a* unused register. Maybe code could be
// optimized more if this looks for the register that isn't used for the
// longest time around this place, to enable more scheduling freedom. Not
// sure if that would actually result in a big performance difference
// though. Maybe RegisterScavenger::findSurvivorBackwards has some logic
// already to do this - but it's unclear if that could easily be used here.
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment should probably be reserved, fundamentally the scavenge is still doing the same thing and not finding a "nicer" register

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.

None yet

5 participants