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

[BOLT][AArch64] Reduce the number of ADR relaxations #111577

Merged
merged 3 commits into from
Oct 8, 2024

Conversation

maksfb
Copy link
Contributor

@maksfb maksfb commented Oct 8, 2024

If ADR instruction references the same function, we can skip relaxation even if the function is split, but ADR is in the main fragment.

If ADR instruction references the same function, we can skip relaxation
even if the function is split, but ADR is in the main fragment.
@llvmbot
Copy link
Collaborator

llvmbot commented Oct 8, 2024

@llvm/pr-subscribers-bolt

Author: Maksim Panchenko (maksfb)

Changes

If ADR instruction references the same function, we can skip relaxation even if the function is split, but ADR is in the main fragment.


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

2 Files Affected:

  • (modified) bolt/lib/Passes/ADRRelaxationPass.cpp (+4-3)
  • (added) bolt/test/AArch64/adr-relaxation.s (+31)
diff --git a/bolt/lib/Passes/ADRRelaxationPass.cpp b/bolt/lib/Passes/ADRRelaxationPass.cpp
index 256034a841c706..52811edcb82731 100644
--- a/bolt/lib/Passes/ADRRelaxationPass.cpp
+++ b/bolt/lib/Passes/ADRRelaxationPass.cpp
@@ -56,13 +56,14 @@ void ADRRelaxationPass::runOnFunction(BinaryFunction &BF) {
           continue;
       }
 
-      // Don't relax adr if it points to the same function and it is not split
-      // and BF initial size is < 1MB.
+      // Don't relax ADR if it points to the same function and is in the main
+      // fragment and BF initial size is < 1MB.
       const unsigned OneMB = 0x100000;
       if (BF.getSize() < OneMB) {
         BinaryFunction *TargetBF = BC.getFunctionForSymbol(Symbol);
-        if (TargetBF == &BF && !BF.isSplit())
+        if (TargetBF == &BF && !BB.isSplit())
           continue;
+
         // No relaxation needed if ADR references a basic block in the same
         // fragment.
         if (BinaryBasicBlock *TargetBB = BF.getBasicBlockForLabel(Symbol))
diff --git a/bolt/test/AArch64/adr-relaxation.s b/bolt/test/AArch64/adr-relaxation.s
new file mode 100644
index 00000000000000..55b7d80edaab49
--- /dev/null
+++ b/bolt/test/AArch64/adr-relaxation.s
@@ -0,0 +1,31 @@
+## Check that llvm-bolt will unnecessarily relax ADR instruction.
+## ADR below references containing function that is split. But ADR is always
+## in the main fragment, thus there is no need to relax it.
+
+# RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown %s -o %t.o
+# RUN: %clang %cflags %t.o -o %t.exe -Wl,-q -static
+# RUN: llvm-bolt %t.exe -o %t.bolt --split-functions --split-strategy=randomN \
+# RUN:   2>&1 | FileCheck %s
+# RUN: llvm-objdump -d --disassemble-symbols=_start %t.bolt | FileCheck %s
+
+# CHECK-NOT: adrp
+
+	.text
+  .globl _start
+  .type _start, %function
+_start:
+	.cfi_startproc
+  adr x1, _start
+	cmp	x1, x11
+	b.hi	.L1
+
+	mov	x0, #0x0
+
+.L1:
+	ret	x30
+
+	.cfi_endproc
+.size _start, .-_start
+
+## Force relocation mode.
+  .reloc 0, R_AARCH64_NONE

Copy link
Member

@dcci dcci left a comment

Choose a reason for hiding this comment

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

Thanks for working on this. Some comments/questions.

@@ -0,0 +1,31 @@
## Check that llvm-bolt will unnecessarily relax ADR instruction.
Copy link
Member

Choose a reason for hiding this comment

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

you mean "will not relax" ?

# RUN: 2>&1 | FileCheck %s
# RUN: llvm-objdump -d --disassemble-symbols=_start %t.bolt | FileCheck %s

# CHECK-NOT: adrp
Copy link
Member

Choose a reason for hiding this comment

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

can we dump the whole function (or some context?) that way we can make the test more robust.

const unsigned OneMB = 0x100000;
if (BF.getSize() < OneMB) {
BinaryFunction *TargetBF = BC.getFunctionForSymbol(Symbol);
if (TargetBF == &BF && !BF.isSplit())
if (TargetBF == &BF && !BB.isSplit())
Copy link
Member

Choose a reason for hiding this comment

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

This looks fine to me. Can we add a negative test? (e.g. one where the func is not in the main fragment to show the adrp is still there?)

Copy link
Member

@yota9 yota9 left a comment

Choose a reason for hiding this comment

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

Thanks!

_start:
# CHECK: <_start>:
.cfi_startproc
adr x1, _start
Copy link
Member

Choose a reason for hiding this comment

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

Indentation

Copy link
Member

@dcci dcci left a comment

Choose a reason for hiding this comment

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

LGTM

@maksfb maksfb merged commit 0e86e52 into llvm:main Oct 8, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants