[llvm] [clang] Disable PGO instrumentation on naked function (PR #75224)

via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 12 09:34:23 PST 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: None (serge-sans-paille)

<details>
<summary>Changes</summary>

We only allow for assembly code in naked function, and PGO instrumentation (esp. temporal instrumentation that introduces a function call) can wreak havoc in this.

Fix #<!-- -->74573

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


3 Files Affected:

- (modified) clang/lib/CodeGen/CodeGenFunction.cpp (+4) 
- (modified) llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp (+2) 
- (modified) llvm/test/Transforms/PGOProfile/timestamp.ll (+12-1) 


``````````diff
diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp
index 2199d7b58fb96..6689ddc19cb1c 100644
--- a/clang/lib/CodeGen/CodeGenFunction.cpp
+++ b/clang/lib/CodeGen/CodeGenFunction.cpp
@@ -892,6 +892,10 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
     }
   }
 
+  if (FD->hasAttr<NakedAttr>()) {
+    Fn->addFnAttr(llvm::Attribute::NoProfile);
+  }
+
   unsigned Count, Offset;
   if (const auto *Attr =
           D ? D->getAttr<PatchableFunctionEntryAttr>() : nullptr) {
diff --git a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
index 4a5a0b25bebba..57ff1648788f9 100644
--- a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
+++ b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
@@ -1783,6 +1783,8 @@ static bool skipPGOUse(const Function &F) {
 static bool skipPGOGen(const Function &F) {
   if (skipPGOUse(F))
     return true;
+  if (F.hasFnAttribute(llvm::Attribute::Naked))
+    return true;
   if (F.hasFnAttribute(llvm::Attribute::NoProfile))
     return true;
   if (F.hasFnAttribute(llvm::Attribute::SkipProfile))
diff --git a/llvm/test/Transforms/PGOProfile/timestamp.ll b/llvm/test/Transforms/PGOProfile/timestamp.ll
index 8d6095a031a66..d5d1233070f7f 100644
--- a/llvm/test/Transforms/PGOProfile/timestamp.ll
+++ b/llvm/test/Transforms/PGOProfile/timestamp.ll
@@ -3,10 +3,21 @@
 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
 target triple = "x86_64-unknown-linux-gnu"
 
+; CHECK-LABEL: define void @foo(
 define void @foo() {
 entry:
   ; CHECK: call void @llvm.instrprof.timestamp({{.*}})
   ret void
 }
 
-; CHECK: declare void @llvm.instrprof.timestamp(
+; CHECK-LABEL: define void @bar(
+define void @bar() #0 {
+entry:
+  ; CHECK-NOT: call void @llvm.instrprof.timestamp({{.*}})
+  call void asm sideeffect "retq;", "~{dirflag},~{fpsr},~{flags}"()
+  unreachable
+}
+
+; CHECK-LABEL: declare void @llvm.instrprof.timestamp(
+
+attributes #0 = { naked }

``````````

</details>


https://github.com/llvm/llvm-project/pull/75224


More information about the cfe-commits mailing list