[llvm] [LFI][AArch64] Add +4 size overestimate for control flow instructions in getLFIInstSizeInBytes (PR #224791)

Sharjeel Khan via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 20 22:49:49 PDT 2026


https://github.com/Sharjeel-Khan updated https://github.com/llvm/llvm-project/pull/224791

>From a4ddad294f5e2e153f0cfe84aee4ce9c270fee58 Mon Sep 17 00:00:00 2001
From: Sharjeel Khan <sharjeelkhan at google.com>
Date: Fri, 18 Sep 2026 19:18:44 -0700
Subject: [PATCH 1/6] [LFI][AArch64] Add +4 size overestimate for control flow
 instructions in getLFIInstSizeInBytes

In AArch64MCLFIRewriter, any control flow instruction (branches, calls,
returns) can trigger flushing of an active DeferredLRGuard, emitting a
4-byte link-register mask (add x30, x27, w30, uxtw) immediately before
the instruction.

Update getLFIInstSizeInBytes to add a 4-byte overestimate to control flow
instructions (BR, BLR, BRA*, BLRA*, and any other branch or call via
isBranch() / isCall()). This prevents instruction size assertion failures
in +assertions builds.

Assisted-by: Gemini
---
 llvm/lib/Target/AArch64/AArch64InstrInfo.cpp | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp b/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
index 2e482b38f72f1..7baee9adf4f77 100644
--- a/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
+++ b/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
@@ -126,8 +126,9 @@ static std::optional<unsigned> getLFIInstSizeInBytes(const MachineInstr &MI) {
     return 16;
   case AArch64::BR:
   case AArch64::BLR:
-    // Indirect branches/calls expand to 2 instructions (guard + br/blr).
-    return 8;
+    // Indirect branches/calls expand to 2 instructions (guard + br/blr),
+    // plus up to 1 instruction for a flushed deferred LR guard.
+    return 12;
   case AArch64::RET:
     // RET through another register expands to 2 instructions (guard + ret).
     // RET through LR may also expand to 2 instructions if a deferred LR guard
@@ -136,7 +137,7 @@ static std::optional<unsigned> getLFIInstSizeInBytes(const MachineInstr &MI) {
   case AArch64::RETAA:
   case AArch64::RETAB:
     // Authenticated returns expand to 3 instructions (authenticate + guard +
-    // ret).
+    // ret). Any deferred LR guard is discarded.
     return 12;
   case AArch64::BRAA:
   case AArch64::BRAAZ:
@@ -147,8 +148,8 @@ static std::optional<unsigned> getLFIInstSizeInBytes(const MachineInstr &MI) {
   case AArch64::BLRAB:
   case AArch64::BLRABZ:
     // Authenticated branches/calls expand to 3 instructions (authenticate +
-    // guard + branch).
-    return 12;
+    // guard + branch), plus up to 1 instruction for a flushed deferred LR guard.
+    return 16;
   case AArch64::AUTIASP:
   case AArch64::AUTIBSP:
   case AArch64::AUTIAZ:
@@ -194,6 +195,11 @@ static std::optional<unsigned> getLFIInstSizeInBytes(const MachineInstr &MI) {
   if (ModifiesSP || ModifiesLR)
     return 8;
 
+  // Any branch or call instruction may have a deferred LR guard flushed
+  // before it (+4 bytes).
+  if (MI.isBranch() || MI.isCall())
+    return 8;
+
   // Default case: instructions that don't cause expansion.
   // - TP accesses in LFI are a single load/store, so no expansion.
   // - All remaining instructions are not rewritten.

>From fea5275e61258488e7c4da79c43e923b7c344db2 Mon Sep 17 00:00:00 2001
From: Sharjeel Khan <sharjeelkhan at google.com>
Date: Fri, 18 Sep 2026 19:51:04 -0700
Subject: [PATCH 2/6] Fix code formatting

---
 llvm/lib/Target/AArch64/AArch64InstrInfo.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp b/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
index 7baee9adf4f77..301368f80e7fb 100644
--- a/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
+++ b/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
@@ -148,7 +148,8 @@ static std::optional<unsigned> getLFIInstSizeInBytes(const MachineInstr &MI) {
   case AArch64::BLRAB:
   case AArch64::BLRABZ:
     // Authenticated branches/calls expand to 3 instructions (authenticate +
-    // guard + branch), plus up to 1 instruction for a flushed deferred LR guard.
+    // guard + branch), plus up to 1 instruction for a flushed deferred LR
+    // guard.
     return 16;
   case AArch64::AUTIASP:
   case AArch64::AUTIBSP:

>From 83e8a3a5141ffb7bc37293e0cecca0dbaf770fe5 Mon Sep 17 00:00:00 2001
From: Sharjeel Khan <sharjeelkhan at google.com>
Date: Fri, 18 Sep 2026 20:15:33 -0700
Subject: [PATCH 3/6] [LFI][AArch64] Add unit tests for control flow
 instruction sizing

---
 llvm/unittests/Target/AArch64/InstSizes.cpp | 50 ++++++++++++++++++++-
 1 file changed, 48 insertions(+), 2 deletions(-)

diff --git a/llvm/unittests/Target/AArch64/InstSizes.cpp b/llvm/unittests/Target/AArch64/InstSizes.cpp
index 898e3dae167b3..90e14a26339c5 100644
--- a/llvm/unittests/Target/AArch64/InstSizes.cpp
+++ b/llvm/unittests/Target/AArch64/InstSizes.cpp
@@ -12,8 +12,9 @@
 using namespace llvm;
 
 namespace {
-std::unique_ptr<TargetMachine> createTargetMachine() {
-  Triple TT("aarch64--");
+std::unique_ptr<TargetMachine>
+createTargetMachine(StringRef TripleStr = "aarch64--") {
+  Triple TT(TripleStr);
   std::string CPU("generic");
   std::string FS("+pauth,+mops,+mte");
 
@@ -370,3 +371,48 @@ TEST(InstSizes, MOPSMemoryPseudos) {
               EXPECT_EQ(12u, II.getInstSizeInBytes(*I));
             });
 }
+
+TEST(InstSizes, LFIControlFlow) {
+  std::unique_ptr<TargetMachine> TM = createTargetMachine("aarch64_lfi--");
+  auto [ST, II] = createInstrInfo(TM.get());
+
+  runChecks(TM.get(), II.get(), "", "  BR $x0\n",
+            [](AArch64InstrInfo &II, MachineFunction &MF) {
+              auto I = MF.begin()->begin();
+              EXPECT_EQ(12u, II.getInstSizeInBytes(*I)); // BR (8 + 4)
+            });
+
+  runChecks(TM.get(), II.get(), "", "  BLR $x0\n",
+            [](AArch64InstrInfo &II, MachineFunction &MF) {
+              auto I = MF.begin()->begin();
+              EXPECT_EQ(12u, II.getInstSizeInBytes(*I)); // BLR (8 + 4)
+            });
+
+  runChecks(TM.get(), II.get(), "", "  BLRAA $x10, $x9\n",
+            [](AArch64InstrInfo &II, MachineFunction &MF) {
+              auto I = MF.begin()->begin();
+              EXPECT_EQ(16u, II.getInstSizeInBytes(*I)); // BLRAA (12 + 4)
+            });
+
+  runChecks(TM.get(), II.get(), "", "  B %bb.0\n",
+            [](AArch64InstrInfo &II, MachineFunction &MF) {
+              auto I = MF.begin()->begin();
+              EXPECT_EQ(8u, II.getInstSizeInBytes(*I)); // B (4 + 4)
+            });
+
+  runChecks(TM.get(), II.get(), "",
+            "  CBZX $x0, %bb.0\n"
+            "  B %bb.0\n",
+            [](AArch64InstrInfo &II, MachineFunction &MF) {
+              auto I = MF.begin()->begin();
+              EXPECT_EQ(8u, II.getInstSizeInBytes(*I)); // CBZX (4 + 4)
+            });
+
+  runChecks(TM.get(), II.get(), "",
+            "  BL @sizes, csr_aarch64_aapcs\n"
+            "  B %bb.0\n",
+            [](AArch64InstrInfo &II, MachineFunction &MF) {
+              auto I = MF.begin()->begin();
+              EXPECT_EQ(8u, II.getInstSizeInBytes(*I)); // BL (4 + 4)
+            });
+}

>From 0ca9da4f24f23737c765960316a3f4af1d0530a3 Mon Sep 17 00:00:00 2001
From: Sharjeel Khan <sharjeelkhan at google.com>
Date: Fri, 18 Sep 2026 20:18:07 -0700
Subject: [PATCH 4/6] [LFI][AArch64] Use dedicated createLFITargetMachine
 helper in InstSizes.cpp

---
 llvm/unittests/Target/AArch64/InstSizes.cpp | 24 +++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/llvm/unittests/Target/AArch64/InstSizes.cpp b/llvm/unittests/Target/AArch64/InstSizes.cpp
index 90e14a26339c5..80d2dc06e7150 100644
--- a/llvm/unittests/Target/AArch64/InstSizes.cpp
+++ b/llvm/unittests/Target/AArch64/InstSizes.cpp
@@ -12,9 +12,8 @@
 using namespace llvm;
 
 namespace {
-std::unique_ptr<TargetMachine>
-createTargetMachine(StringRef TripleStr = "aarch64--") {
-  Triple TT(TripleStr);
+std::unique_ptr<TargetMachine> createTargetMachine() {
+  Triple TT("aarch64--");
   std::string CPU("generic");
   std::string FS("+pauth,+mops,+mte");
 
@@ -372,8 +371,25 @@ TEST(InstSizes, MOPSMemoryPseudos) {
             });
 }
 
+static std::unique_ptr<TargetMachine> createLFITargetMachine() {
+  Triple TT("aarch64_lfi--");
+  std::string CPU("generic");
+  std::string FS("+pauth,+mops,+mte");
+
+  LLVMInitializeAArch64TargetInfo();
+  LLVMInitializeAArch64Target();
+  LLVMInitializeAArch64TargetMC();
+
+  std::string Error;
+  const Target *TheTarget = TargetRegistry::lookupTarget(TT, Error);
+
+  return std::unique_ptr<TargetMachine>(
+      TheTarget->createTargetMachine(TT, CPU, FS, TargetOptions(), std::nullopt,
+                                     std::nullopt, CodeGenOptLevel::Default));
+}
+
 TEST(InstSizes, LFIControlFlow) {
-  std::unique_ptr<TargetMachine> TM = createTargetMachine("aarch64_lfi--");
+  std::unique_ptr<TargetMachine> TM = createLFITargetMachine();
   auto [ST, II] = createInstrInfo(TM.get());
 
   runChecks(TM.get(), II.get(), "", "  BR $x0\n",

>From f5056ecec7a029cb7e72ceed2298b632ddd2d067 Mon Sep 17 00:00:00 2001
From: Sharjeel Khan <sharjeelkhan at google.com>
Date: Fri, 18 Sep 2026 20:30:46 -0700
Subject: [PATCH 5/6] [LFI][AArch64] Remove unnecessary +mops and +mte features
 from createLFITargetMachine

---
 llvm/unittests/Target/AArch64/InstSizes.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/unittests/Target/AArch64/InstSizes.cpp b/llvm/unittests/Target/AArch64/InstSizes.cpp
index 80d2dc06e7150..ba10012764279 100644
--- a/llvm/unittests/Target/AArch64/InstSizes.cpp
+++ b/llvm/unittests/Target/AArch64/InstSizes.cpp
@@ -374,7 +374,7 @@ TEST(InstSizes, MOPSMemoryPseudos) {
 static std::unique_ptr<TargetMachine> createLFITargetMachine() {
   Triple TT("aarch64_lfi--");
   std::string CPU("generic");
-  std::string FS("+pauth,+mops,+mte");
+  std::string FS("+pauth");
 
   LLVMInitializeAArch64TargetInfo();
   LLVMInitializeAArch64Target();

>From 01b7f936cca7b8e5e2fbff53b40d5488fc35cae9 Mon Sep 17 00:00:00 2001
From: Sharjeel Khan <sharjeelkhan at google.com>
Date: Fri, 18 Sep 2026 20:41:30 -0700
Subject: [PATCH 6/6] [LFI][AArch64] Add unit tests for BRAA, RET, and RETAA
 sizing

---
 llvm/unittests/Target/AArch64/InstSizes.cpp | 52 ++++++++++++++-------
 1 file changed, 35 insertions(+), 17 deletions(-)

diff --git a/llvm/unittests/Target/AArch64/InstSizes.cpp b/llvm/unittests/Target/AArch64/InstSizes.cpp
index ba10012764279..6394899b3d24f 100644
--- a/llvm/unittests/Target/AArch64/InstSizes.cpp
+++ b/llvm/unittests/Target/AArch64/InstSizes.cpp
@@ -29,6 +29,23 @@ std::unique_ptr<TargetMachine> createTargetMachine() {
                                      std::nullopt, CodeGenOptLevel::Default));
 }
 
+std::unique_ptr<TargetMachine> createLFITargetMachine() {
+  Triple TT("aarch64_lfi--");
+  std::string CPU("generic");
+  std::string FS("+pauth");
+
+  LLVMInitializeAArch64TargetInfo();
+  LLVMInitializeAArch64Target();
+  LLVMInitializeAArch64TargetMC();
+
+  std::string Error;
+  const Target *TheTarget = TargetRegistry::lookupTarget(TT, Error);
+
+  return std::unique_ptr<TargetMachine>(
+      TheTarget->createTargetMachine(TT, CPU, FS, TargetOptions(), std::nullopt,
+                                     std::nullopt, CodeGenOptLevel::Default));
+}
+
 std::pair<std::unique_ptr<AArch64Subtarget>, std::unique_ptr<AArch64InstrInfo>>
 createInstrInfo(TargetMachine *TM) {
   auto ST = std::make_unique<AArch64Subtarget>(
@@ -371,23 +388,6 @@ TEST(InstSizes, MOPSMemoryPseudos) {
             });
 }
 
-static std::unique_ptr<TargetMachine> createLFITargetMachine() {
-  Triple TT("aarch64_lfi--");
-  std::string CPU("generic");
-  std::string FS("+pauth");
-
-  LLVMInitializeAArch64TargetInfo();
-  LLVMInitializeAArch64Target();
-  LLVMInitializeAArch64TargetMC();
-
-  std::string Error;
-  const Target *TheTarget = TargetRegistry::lookupTarget(TT, Error);
-
-  return std::unique_ptr<TargetMachine>(
-      TheTarget->createTargetMachine(TT, CPU, FS, TargetOptions(), std::nullopt,
-                                     std::nullopt, CodeGenOptLevel::Default));
-}
-
 TEST(InstSizes, LFIControlFlow) {
   std::unique_ptr<TargetMachine> TM = createLFITargetMachine();
   auto [ST, II] = createInstrInfo(TM.get());
@@ -404,12 +404,30 @@ TEST(InstSizes, LFIControlFlow) {
               EXPECT_EQ(12u, II.getInstSizeInBytes(*I)); // BLR (8 + 4)
             });
 
+  runChecks(TM.get(), II.get(), "", "  BRAA $x10, $x9\n",
+            [](AArch64InstrInfo &II, MachineFunction &MF) {
+              auto I = MF.begin()->begin();
+              EXPECT_EQ(16u, II.getInstSizeInBytes(*I)); // BRAA (12 + 4)
+            });
+
   runChecks(TM.get(), II.get(), "", "  BLRAA $x10, $x9\n",
             [](AArch64InstrInfo &II, MachineFunction &MF) {
               auto I = MF.begin()->begin();
               EXPECT_EQ(16u, II.getInstSizeInBytes(*I)); // BLRAA (12 + 4)
             });
 
+  runChecks(TM.get(), II.get(), "", "  RET undef $lr\n",
+            [](AArch64InstrInfo &II, MachineFunction &MF) {
+              auto I = MF.begin()->begin();
+              EXPECT_EQ(8u, II.getInstSizeInBytes(*I)); // RET (4 + 4)
+            });
+
+  runChecks(TM.get(), II.get(), "", "  RETAA implicit $lr, implicit $sp\n",
+            [](AArch64InstrInfo &II, MachineFunction &MF) {
+              auto I = MF.begin()->begin();
+              EXPECT_EQ(12u, II.getInstSizeInBytes(*I)); // RETAA (12)
+            });
+
   runChecks(TM.get(), II.get(), "", "  B %bb.0\n",
             [](AArch64InstrInfo &II, MachineFunction &MF) {
               auto I = MF.begin()->begin();



More information about the llvm-commits mailing list