[llvm] [BOLT] Ensure remember and restore CFIs are in the same list (PR #144348)

Asher Dobrescu via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 7 04:47:38 PDT 2025


https://github.com/Asher8118 updated https://github.com/llvm/llvm-project/pull/144348

>From 7c3246f5d928c6ab4d2bc11402cb30a408bbb6d3 Mon Sep 17 00:00:00 2001
From: Ash Dobrescu <ash.dobrescu at arm.com>
Date: Wed, 2 Jul 2025 11:35:00 +0000
Subject: [PATCH 1/4] Add pre-commit test

---
 bolt/test/AArch64/cfi-state-list.test | 69 +++++++++++++++++++++++++++
 1 file changed, 69 insertions(+)
 create mode 100644 bolt/test/AArch64/cfi-state-list.test

diff --git a/bolt/test/AArch64/cfi-state-list.test b/bolt/test/AArch64/cfi-state-list.test
new file mode 100644
index 0000000000000..73bd8563c83bf
--- /dev/null
+++ b/bolt/test/AArch64/cfi-state-list.test
@@ -0,0 +1,69 @@
+// This test checks that BOLT does not split remember and restore CFI states
+// into different lists, which would cause an assertion failure.
+
+# RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown %s -o %t.o
+# RUN: %clang %cflags %t.o -o %t.exe -Wl,-q
+# RUN: llvm-bolt %t.exe -o %t.bolt 2>&1 | FileCheck %s
+
+# CHECK: BOLT-INFO: Target architecture: aarch64
+# CHECK: BOLT-INFO: enabling relocation mode
+# CHECK-NOT: llvm-bolt:
+# CHECK: BOLT-INFO: Starting stub-insertion pass
+
+.text
+.global main
+.type main, %function
+
+main:
+.cfi_startproc
+.cfi_def_cfa_offset 16
+.cfi_offset x30, -8
+.cfi_remember_state
+mov	x9, #0x3ff0000000000000
+mov	x8, x0
+stp	x30, x9, [sp, #-0x10]!
+add	x3, sp, #0x8
+mov	x0, x1
+mov	x1, x2
+mov	x2, x8
+bl main
+fcmp	d0, #0.0
+b.ne main+0x34
+
+mov	w0, wzr
+ldr	x30, [sp], #0x10
+.cfi_def_cfa_offset 0
+.cfi_restore x30
+
+ret
+.cfi_restore_state
+.cfi_remember_state
+
+fmov	x8, d0
+mov	x9, #0x7ff0000000000000
+and	x8, x8, #0x7fffffffffffffff
+cmp	x8, x9
+b.lt main+0x5c
+fcmp	d0, #0.0
+mov	w8, #-0x1
+csinc	w0, w8, wzr, le
+ldr	x30, [sp], #0x10
+.cfi_def_cfa_offset 0
+.cfi_restore x30
+
+ret
+nop
+.cfi_restore_state
+
+ldr	d2, [sp, #0x8]
+mov	x8, #0x3cb0000000000000
+fabs	d1, d0
+fcmp	d0, #0.0
+fmov	d3, x8
+mov	w8, #-0x1
+csinc	w0, w8, wzr, le
+fmul	d2, d2, d3
+fcmp	d1, d2
+b.ls main+0x28
+b main+0x2c
+.cfi_endproc

>From 75cc116385baebf8e4be97c0729425b3bcdbbb63 Mon Sep 17 00:00:00 2001
From: Ash Dobrescu <ash.dobrescu at arm.com>
Date: Mon, 16 Jun 2025 13:05:02 +0000
Subject: [PATCH 2/4] [BOLT] Ensure remember and restore CFIs are in the same
 list

---
 bolt/include/bolt/Core/BinaryFunction.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/bolt/include/bolt/Core/BinaryFunction.h b/bolt/include/bolt/Core/BinaryFunction.h
index ca8b786f4ab69..828c56d818b09 100644
--- a/bolt/include/bolt/Core/BinaryFunction.h
+++ b/bolt/include/bolt/Core/BinaryFunction.h
@@ -1663,7 +1663,7 @@ class BinaryFunction {
       Offset = I->first;
     }
     assert(I->first == Offset && "CFI pointing to unknown instruction");
-    if (I == Instructions.begin()) {
+    if (I == Instructions.begin() && Inst.getOperation() != MCCFIInstruction::OpRememberState) {
       CIEFrameInstructions.emplace_back(std::forward<MCCFIInstruction>(Inst));
       return;
     }

>From 62b7cac7d418f964360319a7641df58e8e0458e8 Mon Sep 17 00:00:00 2001
From: Ash Dobrescu <ash.dobrescu at arm.com>
Date: Mon, 16 Jun 2025 13:27:01 +0000
Subject: [PATCH 3/4] Run clang-format

---
 bolt/include/bolt/Core/BinaryFunction.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/bolt/include/bolt/Core/BinaryFunction.h b/bolt/include/bolt/Core/BinaryFunction.h
index 828c56d818b09..af7149e49c840 100644
--- a/bolt/include/bolt/Core/BinaryFunction.h
+++ b/bolt/include/bolt/Core/BinaryFunction.h
@@ -1663,7 +1663,8 @@ class BinaryFunction {
       Offset = I->first;
     }
     assert(I->first == Offset && "CFI pointing to unknown instruction");
-    if (I == Instructions.begin() && Inst.getOperation() != MCCFIInstruction::OpRememberState) {
+    if (I == Instructions.begin() &&
+        Inst.getOperation() != MCCFIInstruction::OpRememberState) {
       CIEFrameInstructions.emplace_back(std::forward<MCCFIInstruction>(Inst));
       return;
     }

>From bc31f66f23f95b1dd03a2b7e0817306615edf6f0 Mon Sep 17 00:00:00 2001
From: Ash Dobrescu <ash.dobrescu at arm.com>
Date: Mon, 7 Jul 2025 11:44:35 +0000
Subject: [PATCH 4/4] Address review comments

---
 bolt/include/bolt/Core/BinaryFunction.h |  3 ++
 bolt/test/AArch64/cfi-state-list.test   | 69 ++++++-------------------
 2 files changed, 20 insertions(+), 52 deletions(-)

diff --git a/bolt/include/bolt/Core/BinaryFunction.h b/bolt/include/bolt/Core/BinaryFunction.h
index af7149e49c840..ae580520b9110 100644
--- a/bolt/include/bolt/Core/BinaryFunction.h
+++ b/bolt/include/bolt/Core/BinaryFunction.h
@@ -1663,6 +1663,9 @@ class BinaryFunction {
       Offset = I->first;
     }
     assert(I->first == Offset && "CFI pointing to unknown instruction");
+    // When dealing with RememberState, we place this CFI in FrameInstructions.
+    // We want to ensure RememberState and RestoreState CFIs are in the same
+    // list in order to properly populate the StateStack.
     if (I == Instructions.begin() &&
         Inst.getOperation() != MCCFIInstruction::OpRememberState) {
       CIEFrameInstructions.emplace_back(std::forward<MCCFIInstruction>(Inst));
diff --git a/bolt/test/AArch64/cfi-state-list.test b/bolt/test/AArch64/cfi-state-list.test
index 73bd8563c83bf..2109f912ce890 100644
--- a/bolt/test/AArch64/cfi-state-list.test
+++ b/bolt/test/AArch64/cfi-state-list.test
@@ -5,9 +5,6 @@
 # RUN: %clang %cflags %t.o -o %t.exe -Wl,-q
 # RUN: llvm-bolt %t.exe -o %t.bolt 2>&1 | FileCheck %s
 
-# CHECK: BOLT-INFO: Target architecture: aarch64
-# CHECK: BOLT-INFO: enabling relocation mode
-# CHECK-NOT: llvm-bolt:
 # CHECK: BOLT-INFO: Starting stub-insertion pass
 
 .text
@@ -16,54 +13,22 @@
 
 main:
 .cfi_startproc
-.cfi_def_cfa_offset 16
-.cfi_offset x30, -8
 .cfi_remember_state
-mov	x9, #0x3ff0000000000000
-mov	x8, x0
-stp	x30, x9, [sp, #-0x10]!
-add	x3, sp, #0x8
-mov	x0, x1
-mov	x1, x2
-mov	x2, x8
-bl main
-fcmp	d0, #0.0
-b.ne main+0x34
-
-mov	w0, wzr
-ldr	x30, [sp], #0x10
-.cfi_def_cfa_offset 0
-.cfi_restore x30
-
-ret
-.cfi_restore_state
-.cfi_remember_state
-
-fmov	x8, d0
-mov	x9, #0x7ff0000000000000
-and	x8, x8, #0x7fffffffffffffff
-cmp	x8, x9
-b.lt main+0x5c
-fcmp	d0, #0.0
-mov	w8, #-0x1
-csinc	w0, w8, wzr, le
-ldr	x30, [sp], #0x10
-.cfi_def_cfa_offset 0
-.cfi_restore x30
-
-ret
-nop
-.cfi_restore_state
-
-ldr	d2, [sp, #0x8]
-mov	x8, #0x3cb0000000000000
-fabs	d1, d0
-fcmp	d0, #0.0
-fmov	d3, x8
-mov	w8, #-0x1
-csinc	w0, w8, wzr, le
-fmul	d2, d2, d3
-fcmp	d1, d2
-b.ls main+0x28
-b main+0x2c
+  mov	w0, wzr
+  b.ne .L1
+.L0:
+  mov	w0, wzr
+.L1:
+  cmp	x0, #0
+  b.lt .L2
+.L2:
+  nop
+  .cfi_restore_state
+  mov	x8, xzr
+  b.ls .L0
+  ret
 .cfi_endproc
+    .size main, .-main
+
+## Force relocation mode.
+  .reloc 0, R_AARCH64_NONE



More information about the llvm-commits mailing list