[llvm] [dyndbg][AsmPrinter] Add tail padding attributes (PR #194852)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 29 06:12:28 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-x86
Author: Orlando Cazalet-Hyams (OCHyams)
<details>
<summary>Changes</summary>
"tail-pad-to-size"="n": Pad functions up to at least 'n' bytes.
"tail-pad-value"="n": Pad using the value 'n' (default 0).
This patch is to support dynamic debugging, RFC:
https://discourse.llvm.org/t/90113
In order to transfer control from an optimized to an unoptimized function a
debugger may patch the optimized version with a jump to the unoptimized one.
This patch adds a mechanism to ensure there's enough padding between functions
to avoid patches overwriting unrelated code. It adds padding to the end of
functions smaller then 'n' bytes.
The `.fill` directive expression was discussed here:
https://discourse.llvm.org/t/89658
---
It could make sense to introduce a module flag or something instead, but this approach is very small, easy to test and works well for our purposes. Plus if LTO becomes a supported dyndbg configuration then a module flag may not be flexible enough.
There was discussion on the RFC on re-using existing patch/pad mechanisms, e.g. `"patchable-function"="prologue-short-redirect"`. Adjusting `"patchable-function"="prologue-short-redirect"` to support 32 bit relative jumps too increases (one arbitrarily picked medium sized code base) .text size by 3.2%. Presumably the inclusion of nops at the start of functions has a minor performance implication too (though I haven't measured this and it may be within the noise).
If the addition of another padding/patching mechanism is undesirable we probably _can_ live with the costs of `"patchable-function"="prologue-short-redirect"`.
---
Full diff: https://github.com/llvm/llvm-project/pull/194852.diff
2 Files Affected:
- (modified) llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp (+32-1)
- (added) llvm/test/CodeGen/X86/attr-tail-pad.ll (+15)
``````````diff
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 5fdf88831892f..c95d265ee1fae 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -2452,6 +2452,36 @@ void AsmPrinter::emitFunctionBody() {
// Emit target-specific gunk after the function body.
emitFunctionBodyEnd();
+ // Tail-pad functions that want it.
+ if (F.hasFnAttribute("tail-pad-to-size")) {
+ auto *FnEndSym = createTempSymbol("tail_pad_start");
+ OutStreamer->emitLabel(FnEndSym);
+
+ uint64_t PadToSize;
+ if (F.getFnAttribute("tail-pad-to-size")
+ .getValueAsString()
+ .getAsInteger(10, PadToSize))
+ PadToSize = 0;
+ uint64_t FillValue;
+ if (!F.hasFnAttribute("tail-pad-value") ||
+ F.getFnAttribute("tail-pad-value")
+ .getValueAsString()
+ .getAsInteger(10, FillValue))
+ FillValue = 0;
+
+ // .fill ((PadToSize - FuncSize) & (PadToSize - FuncSize >= 0)) FillValue
+ const MCExpr *FuncSize = MCBinaryExpr::createSub(
+ MCSymbolRefExpr::create(FnEndSym, OutContext),
+ MCSymbolRefExpr::create(CurrentFnSymForSize, OutContext), OutContext);
+ const MCExpr *SizeConst = MCConstantExpr::create(PadToSize, OutContext);
+ const MCExpr *Zero = MCConstantExpr::create(0, OutContext);
+ const MCExpr *SubExpr =
+ MCBinaryExpr::createSub(SizeConst, FuncSize, OutContext);
+ const MCExpr *Cmp = MCBinaryExpr::createGTE(SubExpr, Zero, OutContext);
+ const MCExpr *FillExpr = MCBinaryExpr::createAnd(SubExpr, Cmp, OutContext);
+ OutStreamer->emitFill(*FillExpr, FillValue);
+ }
+
// Even though wasm supports .type and .size in general, function symbols
// are automatically sized.
bool EmitFunctionSize = MAI->hasDotTypeDotSizeDirective() && !TT.isWasm();
@@ -3220,7 +3250,8 @@ void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
MBBSectionRanges.clear();
MBBSectionExceptionSyms.clear();
bool NeedsLocalForSize = MAI->needsLocalForSize();
- if (F.hasFnAttribute("patchable-function-entry") ||
+ if (F.hasFnAttribute("tail-pad-to-size") ||
+ F.hasFnAttribute("patchable-function-entry") ||
F.hasFnAttribute("function-instrument") ||
F.hasFnAttribute("xray-instruction-threshold") ||
needFuncLabels(MF, *this) || NeedsLocalForSize ||
diff --git a/llvm/test/CodeGen/X86/attr-tail-pad.ll b/llvm/test/CodeGen/X86/attr-tail-pad.ll
new file mode 100644
index 0000000000000..4b3c6efd142d5
--- /dev/null
+++ b/llvm/test/CodeGen/X86/attr-tail-pad.ll
@@ -0,0 +1,15 @@
+; RUN: llc -mtriple=x86_64-unknown-linux %s -o - | FileCheck %s
+
+; CHECK: a: # @a
+; CHECK: retq
+; CHECK-NEXT: .Ltail_pad_start0:
+; CHECK-NEXT: .zero (5-(.Ltail_pad_start0-a))&((5-(.Ltail_pad_start0-a))>=0),144
+; CHECK-NEXT: .Lfunc_end0:
+; CHECK-NEXT: .size a, .Lfunc_end0-a
+
+define hidden void @a() #0 {
+entry:
+ ret void
+}
+
+attributes #0 = { "tail-pad-to-size"="5" "tail-pad-value"="144" }
``````````
</details>
https://github.com/llvm/llvm-project/pull/194852
More information about the llvm-commits
mailing list