Skip to content

[LoongArch] Report correct instruction size for PATCHABLE_* - #188228

Merged
nikic merged 1 commit into
llvm:mainfrom
nikic:loongarch-instr-size
Mar 26, 2026
Merged

nikic merged 1 commit into
llvm:mainfrom
nikic:loongarch-instr-size

Conversation

@nikic

@nikic nikic commented Mar 24, 2026

Copy link
Copy Markdown
Contributor

This is either the number of nops provided by patchable-function-entry or the size of the xray sled.

See

void LoongArchAsmPrinter::LowerPATCHABLE_FUNCTION_ENTER(
for the corresponding lowering code.

This came up while working on #187703.

This is either the number of nops provided by
patchable-function-entry or the size of the xray sled.
@llvmbot

llvmbot commented Mar 24, 2026

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-backend-loongarch

Author: Nikita Popov (nikic)

Changes

This is either the number of nops provided by patchable-function-entry or the size of the xray sled.

See

void LoongArchAsmPrinter::LowerPATCHABLE_FUNCTION_ENTER(
for the corresponding lowering code.

This came up while working on #187703.


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

1 Files Affected:

  • (modified) llvm/lib/Target/LoongArch/LoongArchInstrInfo.cpp (+17)
diff --git a/llvm/lib/Target/LoongArch/LoongArchInstrInfo.cpp b/llvm/lib/Target/LoongArch/LoongArchInstrInfo.cpp
index 2b163d2d62d3a..4b5ce311f7d52 100644
--- a/llvm/lib/Target/LoongArch/LoongArchInstrInfo.cpp
+++ b/llvm/lib/Target/LoongArch/LoongArchInstrInfo.cpp
@@ -263,6 +263,23 @@ unsigned LoongArchInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
     if (NumBytes == 0)
       NumBytes = 4;
     break;
+  case TargetOpcode::PATCHABLE_FUNCTION_ENTER: {
+    const MachineFunction *MF = MI.getParent()->getParent();
+    const Function &F = MF->getFunction();
+    if (F.hasFnAttribute("patchable-function-entry")) {
+      unsigned Num;
+      if (F.getFnAttribute("patchable-function-entry")
+              .getValueAsString()
+              .getAsInteger(10, Num))
+        return 0;
+      return Num * 4;
+    }
+    [[fallthrough]];
+  }
+  case TargetOpcode::PATCHABLE_FUNCTION_EXIT:
+  case TargetOpcode::PATCHABLE_TAIL_CALL:
+    // Size of xray sled (branch + 11 nops).
+    return 12 * 4;
   }
   return NumBytes;
 }

@heiher heiher left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. It matches the lowering of these patchable nodes. Thanks!

@nikic
nikic merged commit f3e0f2b into llvm:main Mar 26, 2026
12 checks passed
@nikic
nikic deleted the loongarch-instr-size branch March 26, 2026 14:25
Aadarsh-Keshri pushed a commit to Aadarsh-Keshri/llvm-project that referenced this pull request Mar 28, 2026
)

This is either the number of nops provided by patchable-function-entry
or the size of the xray sled.
fzou1 pushed a commit to fzou1/llvm-project that referenced this pull request Mar 30, 2026
)

This is either the number of nops provided by patchable-function-entry
or the size of the xray sled.
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.

3 participants