Skip to content

Conversation

@yavtuk
Copy link
Contributor

@yavtuk yavtuk commented Oct 30, 2025

ldr reg, literal instruction is limited +/- 1MB range, emitCI put the constants by the end of function and the one is out of available range.

@llvmbot
Copy link
Member

llvmbot commented Oct 30, 2025

@llvm/pr-subscribers-bolt

Author: Alexey Moksyakov (yavtuk)

Changes

ldr reg, literal instruction is limited +/- 1MB range, emitCI put the constants by the end of function and the one is out of available range.


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

1 Files Affected:

  • (added) bolt/test/AArch64/materialize-constant.s (+74)
diff --git a/bolt/test/AArch64/materialize-constant.s b/bolt/test/AArch64/materialize-constant.s
new file mode 100644
index 0000000000000..1c15626b09594
--- /dev/null
+++ b/bolt/test/AArch64/materialize-constant.s
@@ -0,0 +1,74 @@
+// this test checks a load literal instructions changed to movk
+
+// REQUIRES: system-linux
+
+# RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown %s -o %t.o
+
+# RUN: link_fdata %s %t.o %t.fdata
+# RUN: %clang %cflags -pie %t.o -o %t.exe -Wl,-q -Wl,-z,relro -Wl,-z,now
+# RUN: llvm-bolt %t.exe -o %t.bolt -data %t.fdata \
+# RUN:    --keep-nops --eliminate-unreachable=false
+# RUN: llvm-objdump --disassemble-symbols=foo %t.bolt | FileCheck %s
+
+# CHECK: mov{{.*}} w19, #0
+# CHECK-NEXT: mov{{.*}} w22, #0
+# CHECK-NEXT: movk{{.*}} w23, #0, lsl #16
+# CHECK-NEXT: movk{{.*}} w23, #100
+# CHECK-NEXT: movk{{.*}} w24, #0, lsl #16
+# CHECK-NEXT: movk{{.*}} w24, #3
+
+  .text
+  .align 4
+  .local foo
+  .type foo, %function
+foo:
+# FDATA: 1 main 0 1 foo 0 0 10
+    stp x29, x30, [sp, #-32]!
+    stp x19, x20, [sp, #16]
+    mov x29, sp
+
+    mov w19, #0 // counter = 0
+    mov w22, #0 // result = 0
+
+    ldr w23, .Llimit
+    ldr w24, .LStep
+    b .LStub
+
+.LConstants:
+  .Llimit: .word 100
+  .LStep:  .word 3
+
+.LStub:
+.rep 0x100000
+    nop
+.endr
+    b .Lmain_loop
+
+.Lmain_loop:
+    madd w22, w19, w24, w22  // result += counter * increment
+
+    add w19, w19, #1
+    cmp w19, w23
+    b.lt .Lmain_loop
+
+    mov w0, w22
+
+    b .Lreturn_point
+
+.Lreturn_point:
+    ldp x19, x20, [sp, #16]
+    ldp x29, x30, [sp], #32
+    ret
+.size foo, .-foo
+
+
+  .global main
+  .type main, %function
+main:
+  mov x0, #0
+  bl foo
+  mov     x0, 0
+  mov     w8, #93
+  svc     #0
+
+  .size main, .-main

@yavtuk
Copy link
Contributor Author

yavtuk commented Oct 30, 2025

hi folks, I've faced with the issue related to ldr reg, literal instruction inside function with constant island. after emitCI the constants are placed by the end of the function and the distance to target for ldr is out of range.

I think such cases are possibly for ADR instruction either and also after long jump pass.
I found that this can be solved by functionality which we have for X86 in simplifyROdata pass.
I wrote quick pass for checking and replace ldr -> movk.
The case is not common that why how do you think should we add the separate pass for verification such cases or extend functionality for simplifyROdata for aarch64 and move the functionality from under the option to a permanent basis?

@yozhu
Copy link
Contributor

yozhu commented Oct 30, 2025

Thanks for the patch! Wondering if it would be useful to combine this "ldr->movk" transformation with some sort of ldr relaxation (as in #165787), particularly for cases where the constant island content has (dynamic) relocation and need to be fixed up?

@maksfb
Copy link
Contributor

maksfb commented Oct 30, 2025

@yavtuk, great idea! If this is integrated into SimplifyRODataLoads pass then it makes sense to run it by default when it's strictly beneficial to do so. I.e., I consider replacing LDR with one or two MOVK to be a performance benefit. For more instructions in case of 64-bit load it's more complicated. #137413 is relevant to immediate generation on AArch64.

Extra step on top will be elimination of the constant island that is no longer referenced.

Running LDR-relaxation pass afterwords will catch cases where the simplification is not possible or is not the best option. We will make use of both passes.

@paschalis-mpeis
Copy link
Member

It would be good to handle this in the suggested pass, also reporting how many times it was applied.
Eliminating any converted islands with no further users sounds like a nice addition on top!

From discussion, this doesn't appear to be a common case.
If it turns out to be more common than we now think, we could consider keeping the island close to its users, as per assembler's original intent, given it is feasible.

ldr reg, literal instruction is limited  +/- 1MB range,
emitCI put the constants by the end of function and the one is out of available range.
@yavtuk yavtuk force-pushed the check-load-literal branch from 82564f4 to 95e84b2 Compare November 24, 2025 15:38
@github-actions
Copy link

github-actions bot commented Nov 24, 2025

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

@github-actions
Copy link

github-actions bot commented Nov 24, 2025

🐧 Linux x64 Test Results

  • 582 tests passed
  • 37 tests skipped
  • 3 tests failed

Failed Tests

(click on a test name to see its output)

BOLT

BOLT.AArch64/materialize-constant.s
Exit Code: 1

Command Output (stdout):
--
# RUN: at line 5
rm -rf /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/test/AArch64/Output/materialize-constant.s.tmp && /home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/split-file /home/gha/actions-runner/_work/llvm-project/llvm-project/bolt/test/AArch64/materialize-constant.s /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/test/AArch64/Output/materialize-constant.s.tmp
# executed command: rm -rf /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/test/AArch64/Output/materialize-constant.s.tmp
# note: command had no output on stdout or stderr
# executed command: /home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/split-file /home/gha/actions-runner/_work/llvm-project/llvm-project/bolt/test/AArch64/materialize-constant.s /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/test/AArch64/Output/materialize-constant.s.tmp
# note: command had no output on stdout or stderr
# RUN: at line 7
/home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/llvm-mc -filetype=obj -triple aarch64-unknown-unknown     --defsym CIBIGFUNC=1     /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/test/AArch64/Output/materialize-constant.s.tmp/materialized-ci.s -o /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/test/AArch64/Output/materialize-constant.s.tmp/materialized-ci.o
# executed command: /home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/llvm-mc -filetype=obj -triple aarch64-unknown-unknown --defsym CIBIGFUNC=1 /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/test/AArch64/Output/materialize-constant.s.tmp/materialized-ci.s -o /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/test/AArch64/Output/materialize-constant.s.tmp/materialized-ci.o
# note: command had no output on stdout or stderr
# RUN: at line 10
/home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/clang /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/test/AArch64/Output/materialize-constant.s.tmp/materialized-ci.o -Wl,-q     -o /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/test/AArch64/Output/materialize-constant.s.tmp/materialized-ci.exe
# executed command: /home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/clang /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/test/AArch64/Output/materialize-constant.s.tmp/materialized-ci.o -Wl,-q -o /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/test/AArch64/Output/materialize-constant.s.tmp/materialized-ci.exe
# .---command stderr------------
# | /usr/bin/ld: unknown architecture of input file `/home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/test/AArch64/Output/materialize-constant.s.tmp/materialized-ci.o' is incompatible with i386:x86-64 output
# | /usr/bin/ld: warning: /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/test/AArch64/Output/materialize-constant.s.tmp/materialized-ci.o: missing .note.GNU-stack section implies executable stack
# | /usr/bin/ld: NOTE: This behaviour is deprecated and will be removed in a future version of the linker
# | clang: error: linker command failed with exit code 1 (use -v to see invocation)
# `-----------------------------
# error: command failed with exit status: 1

--

BOLT.X86/rodata-simpl-loads.test
Exit Code: -11

Command Output (stdout):
--
# RUN: at line 3
/home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/clang  --target=x86_64-unknown-linux-gnu -fPIE -fuse-ld=lld -Wl,--unresolved-symbols=ignore-all -Wl,--build-id=none -pie --target=x86_64-unknown-linux-gnu -nostdlib -mllvm -x86-asm-syntax=att /home/gha/actions-runner/_work/llvm-project/llvm-project/bolt/test/X86/Inputs/rodata_simpl_loads.s -o /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/test/X86/Output/rodata-simpl-loads.test.tmp.exe
# executed command: /home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/clang --target=x86_64-unknown-linux-gnu -fPIE -fuse-ld=lld -Wl,--unresolved-symbols=ignore-all -Wl,--build-id=none -pie --target=x86_64-unknown-linux-gnu -nostdlib -mllvm -x86-asm-syntax=att /home/gha/actions-runner/_work/llvm-project/llvm-project/bolt/test/X86/Inputs/rodata_simpl_loads.s -o /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/test/X86/Output/rodata-simpl-loads.test.tmp.exe
# .---command stderr------------
# | ld.lld: warning: cannot find entry symbol _start; not setting start address
# `-----------------------------
# RUN: at line 4
/home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/llvm-bolt --runtime-instrumentation-lib=/home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/bolt_rt-bins/lib/libbolt_rt_instr.a --runtime-hugify-lib=/home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/bolt_rt-bins/lib/libbolt_rt_hugify.a /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/test/X86/Output/rodata-simpl-loads.test.tmp.exe -o /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/test/X86/Output/rodata-simpl-loads.test.tmp --simplify-rodata-loads
# executed command: /home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/llvm-bolt --runtime-instrumentation-lib=/home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/bolt_rt-bins/lib/libbolt_rt_instr.a --runtime-hugify-lib=/home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/bolt_rt-bins/lib/libbolt_rt_hugify.a /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/test/X86/Output/rodata-simpl-loads.test.tmp.exe -o /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/test/X86/Output/rodata-simpl-loads.test.tmp --simplify-rodata-loads
# .---command stdout------------
# | BOLT-INFO: shared object or position-independent executable detected
# | BOLT-INFO: Target architecture: x86_64
# | BOLT-INFO: BOLT version: 9016d58b512b4857f5d3e85a9992769bb8f1e05e
# | BOLT-INFO: first alloc address is 0x0
# | BOLT-INFO: creating new program header table at address 0x200000, offset 0x200000
# | BOLT-INFO: enabling lite mode
# | BOLT-INFO: 0 out of 1 functions in the binary (0.0%) have non-empty execution profile
# `-----------------------------
# .---command stderr------------
# |  #0 0x0000000002f8ae38 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /home/gha/actions-runner/_work/llvm-project/llvm-project/llvm/lib/Support/Unix/Signals.inc:834:13
# |  #1 0x0000000002f8853c llvm::sys::RunSignalHandlers() /home/gha/actions-runner/_work/llvm-project/llvm-project/llvm/lib/Support/Signals.cpp:105:18
# |  #2 0x0000000002f8bbf1 SignalHandler(int, siginfo_t*, void*) /home/gha/actions-runner/_work/llvm-project/llvm-project/llvm/lib/Support/Unix/Signals.inc:426:38
# |  #3 0x00007976b2d86330 (/lib/x86_64-linux-gnu/libc.so.6+0x45330)
# |  #4 0x000000000360d7ae getOpcode /home/gha/actions-runner/_work/llvm-project/llvm-project/llvm/include/llvm/MC/MCInst.h:202:39
# |  #5 0x000000000360d7ae llvm::bolt::SimplifyRODataLoads::simplifyRODataLoads(llvm::bolt::BinaryFunction&) /home/gha/actions-runner/_work/llvm-project/llvm-project/bolt/lib/Passes/BinaryPasses.cpp:1192:30
# |  #6 0x000000000360de4f llvm::bolt::SimplifyRODataLoads::runOnFunctions(llvm::bolt::BinaryContext&) /home/gha/actions-runner/_work/llvm-project/llvm-project/bolt/lib/Passes/BinaryPasses.cpp:1294:34
# |  #7 0x0000000003001fa4 setChecked /home/gha/actions-runner/_work/llvm-project/llvm-project/llvm/include/llvm/Support/Error.h:306:13
# |  #8 0x0000000003001fa4 operator= /home/gha/actions-runner/_work/llvm-project/llvm-project/llvm/include/llvm/Support/Error.h:221:5
# |  #9 0x0000000003001fa4 Error /home/gha/actions-runner/_work/llvm-project/llvm-project/llvm/include/llvm/Support/Error.h:198:11
# | #10 0x0000000003001fa4 joinErrors /home/gha/actions-runner/_work/llvm-project/llvm-project/llvm/include/llvm/Support/Error.h:443:26
# | #11 0x0000000003001fa4 llvm::bolt::BinaryFunctionPassManager::runPasses()::$_0::operator()() const /home/gha/actions-runner/_work/llvm-project/llvm-project/bolt/lib/Rewrite/BinaryPassManager.cpp:320:15
# | #12 0x0000000002ffcc4a callWithDynoStats<(lambda at /home/gha/actions-runner/_work/llvm-project/llvm-project/bolt/lib/Rewrite/BinaryPassManager.cpp:319:9), std::map<unsigned long, llvm::bolt::BinaryFunction, std::less<unsigned long>, std::allocator<std::pair<const unsigned long, llvm::bolt::BinaryFunction> > > > /home/gha/actions-runner/_work/llvm-project/llvm-project/bolt/include/bolt/Core/DynoStats.h:0:3
# | #13 0x0000000002ffcc4a llvm::bolt::BinaryFunctionPassManager::runPasses() /home/gha/actions-runner/_work/llvm-project/llvm-project/bolt/lib/Rewrite/BinaryPassManager.cpp:317:5
# | #14 0x0000000003001c70 ~vector /usr/lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/stl_vector.h:735:30
# | #15 0x0000000003001c70 ~BinaryFunctionPassManager /home/gha/actions-runner/_work/llvm-project/llvm-project/bolt/include/bolt/Rewrite/BinaryPassManager.h:25:7
# | #16 0x0000000003001c70 llvm::bolt::BinaryFunctionPassManager::runAllPasses(llvm::bolt::BinaryContext&) /home/gha/actions-runner/_work/llvm-project/llvm-project/bolt/lib/Rewrite/BinaryPassManager.cpp:566:1
# | #17 0x0000000003024a92 llvm::bolt::RewriteInstance::runOptimizationPasses() /home/gha/actions-runner/_work/llvm-project/llvm-project/bolt/lib/Rewrite/RewriteInstance.cpp:3655:7
# | #18 0x000000000301a0f3 StringRef /home/gha/actions-runner/_work/llvm-project/llvm-project/llvm/include/llvm/ADT/StringRef.h:106:11
# | #19 0x000000000301a0f3 StringRef /home/gha/actions-runner/_work/llvm-project/llvm-project/llvm/include/llvm/ADT/StringRef.h:93:11
# | #20 0x000000000301a0f3 finalizeMetadataPreEmit /home/gha/actions-runner/_work/llvm-project/llvm-project/bolt/lib/Rewrite/RewriteInstance.cpp:3817:22
# | #21 0x000000000301a0f3 llvm::bolt::RewriteInstance::run() /home/gha/actions-runner/_work/llvm-project/llvm-project/bolt/lib/Rewrite/RewriteInstance.cpp:782:3
# | #22 0x00000000023dc9b9 getPtr /home/gha/actions-runner/_work/llvm-project/llvm-project/llvm/include/llvm/Support/Error.h:278:42
# | #23 0x00000000023dc9b9 operator bool /home/gha/actions-runner/_work/llvm-project/llvm-project/llvm/include/llvm/Support/Error.h:241:16
# | #24 0x00000000023dc9b9 main /home/gha/actions-runner/_work/llvm-project/llvm-project/bolt/tools/driver/llvm-bolt.cpp:267:17
# | #25 0x00007976b2d6b1ca (/lib/x86_64-linux-gnu/libc.so.6+0x2a1ca)
# | #26 0x00007976b2d6b28b __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28b)
# | #27 0x00000000023da725 _start (/home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/llvm-bolt+0x23da725)
# | PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and instructions to reproduce the bug.
# | Stack dump:
# | 0.	Program arguments: /home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/llvm-bolt --runtime-instrumentation-lib=/home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/bolt_rt-bins/lib/libbolt_rt_instr.a --runtime-hugify-lib=/home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/bolt_rt-bins/lib/libbolt_rt_hugify.a /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/test/X86/Output/rodata-simpl-loads.test.tmp.exe -o /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/test/X86/Output/rodata-simpl-loads.test.tmp --simplify-rodata-loads
# `-----------------------------
# error: command failed with exit status: -11

--

BOLT.X86/srol-bug.test
Exit Code: 1

Command Output (stdout):
--
# RUN: at line 1
/home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/yaml2obj /home/gha/actions-runner/_work/llvm-project/llvm-project/bolt/test/X86/Inputs/srol-bug-input.yaml &> /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/test/X86/Output/srol-bug.test.tmp.exe
# executed command: /home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/yaml2obj /home/gha/actions-runner/_work/llvm-project/llvm-project/bolt/test/X86/Inputs/srol-bug-input.yaml
# note: command had no output on stdout or stderr
# RUN: at line 2
/home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/llvm-bolt --runtime-instrumentation-lib=/home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/bolt_rt-bins/lib/libbolt_rt_instr.a --runtime-hugify-lib=/home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/bolt_rt-bins/lib/libbolt_rt_hugify.a /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/test/X86/Output/srol-bug.test.tmp.exe --simplify-rodata-loads --print-finalized --relocs=0     --print-disasm -o /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/test/X86/Output/srol-bug.test.tmp.out | /home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/FileCheck /home/gha/actions-runner/_work/llvm-project/llvm-project/bolt/test/X86/srol-bug.test
# executed command: /home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/llvm-bolt --runtime-instrumentation-lib=/home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/bolt_rt-bins/lib/libbolt_rt_instr.a --runtime-hugify-lib=/home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/bolt_rt-bins/lib/libbolt_rt_hugify.a /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/test/X86/Output/srol-bug.test.tmp.exe --simplify-rodata-loads --print-finalized --relocs=0 --print-disasm -o /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/bolt/test/X86/Output/srol-bug.test.tmp.out
# note: command had no output on stdout or stderr
# executed command: /home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/FileCheck /home/gha/actions-runner/_work/llvm-project/llvm-project/bolt/test/X86/srol-bug.test
# .---command stderr------------
# | /home/gha/actions-runner/_work/llvm-project/llvm-project/bolt/test/X86/srol-bug.test:27:8: error: CHECK: expected string not found in input
# | CHECK: 00000006: subl $0x20001, %edx
# |        ^
# | <stdin>:69:31: note: scanning from here
# |  00000000: addl $0x20001, %edx
# |                               ^
# | <stdin>:71:2: note: possible intended match here
# |  0000000c: andl $0x20001, %edx
# |  ^
# | 
# | Input file: <stdin>
# | Check file: /home/gha/actions-runner/_work/llvm-project/llvm-project/bolt/test/X86/srol-bug.test
# | 
# | -dump-input=help explains the following input dump.
# | 
# | Input was:
# | <<<<<<
# |             .
# |             .
# |             .
# |            64:  Hash : 72cee1a96986b338 
# |            65:  BB Layout : .LBB00 
# |            66: } 
# |            67: .LBB00 (20 instructions, align : 1) 
# |            68:  Entry Point 
# |            69:  00000000: addl $0x20001, %edx 
# | check:27'0                                   X error: no match found
# |            70:  00000006: subl mydata(%rip), %edx 
# | check:27'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |            71:  0000000c: andl $0x20001, %edx 
# | check:27'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | check:27'1      ?                              possible intended match
# |            72:  00000012: orl mydata(%rip), %edx 
# | check:27'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |            73:  00000018: xorl $0x20001, %edx 
# | check:27'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |            74:  0000001e: pushq %rax 
# | check:27'0     ~~~~~~~~~~~~~~~~~~~~~~
# |            75:  0000001f: movl $0x1, %edx 
# | check:27'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |            76:  00000024: movb mydata(%rip), %dl 
# | check:27'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |             .
# |             .
# |             .
# | >>>>>>
# `-----------------------------
# error: command failed with exit status: 1

--

If these failures are unrelated to your changes (for example tests are broken or flaky at HEAD), please open an issue at https://github.com/llvm/llvm-project/issues and add the infrastructure label.

@yavtuk yavtuk force-pushed the check-load-literal branch 2 times, most recently from ec9d1a5 to 54f11ca Compare November 25, 2025 07:56
@yavtuk yavtuk changed the title [bolt][aarch64] test to reproduce the issue with ldr reg, literal [bolt][aarch64] simplify rodata load for X86 & AArch64 Nov 25, 2025
@yavtuk yavtuk changed the title [bolt][aarch64] simplify rodata load for X86 & AArch64 [bolt][aarch64] simplify rodata/literal load for X86 & AArch64 Nov 25, 2025
@yavtuk yavtuk force-pushed the check-load-literal branch from 54f11ca to 4ef587a Compare November 25, 2025 08:42
@yavtuk
Copy link
Contributor Author

yavtuk commented Nov 25, 2025

@maksfb @yozhu @paschalis-mpeis please take a look when you have a time

uint32_t Offset = TargetAddress - DataSection->getAddress();
StringRef ConstantData = DataSection->getContents();
const InstructionListType Instrs =
MIB->materializeConstant(Inst, ConstantData, Offset);
Copy link
Contributor

Choose a reason for hiding this comment

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

For x86, could we keep the original code which directly update the Instr in place? The new code calling materializeConstant() will call replaceMemOperandWithImm(), create a new Instr and copy over the new instruction operand by operand, find the original instruction in basic block, and then replace it with the new instruction.

Copy link
Contributor Author

@yavtuk yavtuk Nov 27, 2025

Choose a reason for hiding this comment

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

I prefer to use extra wrapper to unify for all platform, replaceMemOperandWithImm is used in another optimisations passes related to X86

I = {8, 4, AArch64::MOVKXi};
break;
default:
llvm_unreachable("unexpected ldr instruction");
Copy link
Contributor

Choose a reason for hiding this comment

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

Could there be any other PC relative LDR instructions hit the default case, like byte or half-word load? Just in case, maybe replace llvm_unreachable() with a comment saying unsupported and then return Insts.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't find instructions related to byte or half wold, now only 2 instructions are handled.
we can analyse the apps to figure out which and how much are used, I faced only 4 and 8 bytes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we will be able to add extra logic as necessary

b .LStub

// CI moved by emitCI function to the end of the function
// without materialization CI is outside available range (+/-1MB)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: "without" -> "Without"


.size _start, .-_start

#--- materialize-ci-outside-func.s
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: this test case is almost the same as the previous one, except for the many nop's under .LStub. Could we keep the first copy and wrap the .LStub block with #if defined(...) and #endif?

@yozhu
Copy link
Contributor

yozhu commented Nov 27, 2025

@maksfb @yozhu @paschalis-mpeis please take a look when you have a time

Thanks for the patch, and sorry for the delay on review. I left some comments in the code; and overall LGTM.

@yavtuk yavtuk force-pushed the check-load-literal branch from 4ef587a to d80a5e6 Compare November 27, 2025 20:06
@yavtuk
Copy link
Contributor Author

yavtuk commented Nov 27, 2025

@yozhu do we need one more verification pass to check that the distance is valid for target?

@yavtuk yavtuk force-pushed the check-load-literal branch 2 times, most recently from b2d22b9 to e96818d Compare November 27, 2025 20:32
This patch fixed the issue related to load literal
for AArch64 (bolt/test/AArch64/materialize-constant.s),
address range for literal is limited  +/- 1MB,
emitCI puts the constants by the end of function and
the one is out of available range.

SimplifyRODataLoads is enabled by default for X86 & AArch64

Signed-off-by: Moksyakov Alexey <[email protected]>
@yavtuk yavtuk force-pushed the check-load-literal branch from e96818d to de57782 Compare November 27, 2025 21:06
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.

5 participants