-
Notifications
You must be signed in to change notification settings - Fork 10.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Disable PGO instrumentation on naked function #75224
Conversation
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 llvm#74573
@llvm/pr-subscribers-llvm-transforms @llvm/pr-subscribers-pgo Author: None (serge-sans-paille) ChangesWe 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:
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 }
|
@llvm/pr-subscribers-clang Author: None (serge-sans-paille) ChangesWe 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:
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 }
|
@@ -892,6 +892,10 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy, | |||
} | |||
} | |||
|
|||
if (FD->hasAttr<NakedAttr>()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this change needed given the separate change in LLVM skipPGOGen?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should not be needed. The llvm change is sufficient to work with all frontends including clang.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not at all, that was a leftover, sorry about that.
@@ -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)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. Similar to https://reviews.llvm.org/D149721
@@ -892,6 +892,10 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy, | |||
} | |||
} | |||
|
|||
if (FD->hasAttr<NakedAttr>()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should not be needed. The llvm change is sufficient to work with all frontends including clang.
Thanks @MaskRay & @teresajohnson for the quick review o/ |
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