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

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


https://github.com/serge-sans-paille created https://github.com/llvm/llvm-project/pull/75224

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

>From 08c69a8c2c8b813130dcd68ac6d616ec7e0dc474 Mon Sep 17 00:00:00 2001
From: serge-sans-paille <sguelton at mozilla.com>
Date: Tue, 12 Dec 2023 18:23:23 +0100
Subject: [PATCH] Disable PGO instrumentation on naked function

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
---
 clang/lib/CodeGen/CodeGenFunction.cpp               |  4 ++++
 .../Instrumentation/PGOInstrumentation.cpp          |  2 ++
 llvm/test/Transforms/PGOProfile/timestamp.ll        | 13 ++++++++++++-
 3 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp
index 2199d7b58fb96e..6689ddc19cb1ce 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 4a5a0b25bebbaf..57ff1648788f9a 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 8d6095a031a66b..d5d1233070f7fa 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 }



More information about the cfe-commits mailing list