[PATCH] D149721: EntryExitInstrumenter: skip naked functions

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 2 21:08:46 PDT 2023

MaskRay created this revision.
MaskRay added reviewers: hans, pcc.
Herald added subscribers: hoy, hiraditya.
Herald added a project: All.
MaskRay requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Naked functions are currently implemented by -pg,
-finstrument-functions, and -finstrument-function-entry-bare.

The asm in a naked function may reasonably expect the argument registers and the
return address register (if present) to be live. A function call to
`mcount`/`__cyg_profile_func_enter_bare`/etc will clobber these registers.

It seems that the only one reasonable way to handle the combination
is to disable -pg/only one reasonable way instrumenting for naked functions.

Close https://github.com/llvm/llvm-project/issues/62504

  rG LLVM Github Monorepo



Index: llvm/test/Transforms/EntryExitInstrumenter/mcount.ll
--- llvm/test/Transforms/EntryExitInstrumenter/mcount.ll
+++ llvm/test/Transforms/EntryExitInstrumenter/mcount.ll
@@ -99,6 +99,13 @@
 ; CHECK: ret
+;; naked functions are not instrumented, otherwise the argument registers
+;; and the return address register (if present) would be clobbered.
+define void @naked() naked { entry: ret void }
+; CHECK-LABEL:      define void @naked(
+; CHECK-LABEL-NEXT:   ret void
 ; The attributes are "consumed" when the instrumentation is inserted.
 ; CHECK: attributes
 ; CHECK-NOT: instrument-function
Index: llvm/lib/Transforms/Utils/EntryExitInstrumenter.cpp
--- llvm/lib/Transforms/Utils/EntryExitInstrumenter.cpp
+++ llvm/lib/Transforms/Utils/EntryExitInstrumenter.cpp
@@ -80,6 +80,9 @@
 static bool runOnFunction(Function &F, bool PostInlining) {
+  if (F.hasFnAttribute(Attribute::Naked))
+    return false;
   StringRef EntryAttr = PostInlining ? "instrument-function-entry-inlined"
                                      : "instrument-function-entry";

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149721.518959.patch
Type: text/x-patch
Size: 1235 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230503/1397d9ae/attachment.bin>

More information about the llvm-commits mailing list