Skip to content

[ctxprof] Extend the notion of "cannot return" #135651

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

Merged

Conversation

mtrofin
Copy link
Member

@mtrofin mtrofin commented Apr 14, 2025

No description provided.

Copy link
Member Author

mtrofin commented Apr 14, 2025

@mtrofin mtrofin force-pushed the users/mtrofin/04-14-_ctxprof_extend_the_notion_of_cannot_return_ branch from 4154007 to e88504d Compare April 14, 2025 17:28
@mtrofin mtrofin force-pushed the users/mtrofin/04-14-_nfc_expose_canreturn_from_functionattrs branch from c58b167 to 8c0f413 Compare April 14, 2025 17:28
@mtrofin mtrofin marked this pull request as ready for review April 14, 2025 17:30
@llvmbot llvmbot added PGO Profile Guided Optimizations llvm:transforms labels Apr 14, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 14, 2025

@llvm/pr-subscribers-pgo

@llvm/pr-subscribers-llvm-transforms

Author: Mircea Trofin (mtrofin)

Changes

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

3 Files Affected:

  • (modified) llvm/lib/Transforms/Instrumentation/PGOCtxProfLowering.cpp (+12-7)
  • (modified) llvm/test/Transforms/PGOProfile/ctx-instrumentation-invalid-roots.ll (+15-10)
  • (modified) llvm/test/Transforms/PGOProfile/ctx-instrumentation.ll (+13)
diff --git a/llvm/lib/Transforms/Instrumentation/PGOCtxProfLowering.cpp b/llvm/lib/Transforms/Instrumentation/PGOCtxProfLowering.cpp
index f99d7b9d03e02..136225ab27cdc 100644
--- a/llvm/lib/Transforms/Instrumentation/PGOCtxProfLowering.cpp
+++ b/llvm/lib/Transforms/Instrumentation/PGOCtxProfLowering.cpp
@@ -9,6 +9,7 @@
 
 #include "llvm/Transforms/Instrumentation/PGOCtxProfLowering.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/Analysis/CFG.h"
 #include "llvm/Analysis/CtxProfAnalysis.h"
 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
 #include "llvm/IR/Analysis.h"
@@ -105,6 +106,12 @@ std::pair<uint32_t, uint32_t> getNumCountersAndCallsites(const Function &F) {
   }
   return {NumCounters, NumCallsites};
 }
+
+void emitUnsupportedRoot(const Function &F, StringRef Reason) {
+  F.getContext().emitError("[ctxprof] The function " + F.getName() +
+                           " was indicated as context root but " + Reason +
+                           ", which is not supported.");
+}
 } // namespace
 
 // set up tie-in with compiler-rt.
@@ -164,12 +171,8 @@ CtxInstrumentationLowerer::CtxInstrumentationLowerer(Module &M,
       for (const auto &BB : *F)
         for (const auto &I : BB)
           if (const auto *CB = dyn_cast<CallBase>(&I))
-            if (CB->isMustTailCall()) {
-              M.getContext().emitError("The function " + Fname +
-                                       " was indicated as a context root, "
-                                       "but it features musttail "
-                                       "calls, which is not supported.");
-            }
+            if (CB->isMustTailCall())
+              emitUnsupportedRoot(*F, "it features musttail calls");
     }
   }
 
@@ -230,11 +233,13 @@ bool CtxInstrumentationLowerer::lowerFunction(Function &F) {
 
   // Probably pointless to try to do anything here, unlikely to be
   // performance-affecting.
-  if (F.doesNotReturn()) {
+  if (!llvm::canReturn(F)) {
     for (auto &BB : F)
       for (auto &I : make_early_inc_range(BB))
         if (isa<InstrProfCntrInstBase>(&I))
           I.eraseFromParent();
+    if (ContextRootSet.contains(&F))
+      emitUnsupportedRoot(F, "it does not return");
     return true;
   }
 
diff --git a/llvm/test/Transforms/PGOProfile/ctx-instrumentation-invalid-roots.ll b/llvm/test/Transforms/PGOProfile/ctx-instrumentation-invalid-roots.ll
index 454780153b823..b5ceb4602c60b 100644
--- a/llvm/test/Transforms/PGOProfile/ctx-instrumentation-invalid-roots.ll
+++ b/llvm/test/Transforms/PGOProfile/ctx-instrumentation-invalid-roots.ll
@@ -1,17 +1,22 @@
-; RUN: not opt -passes=ctx-instr-gen,ctx-instr-lower -profile-context-root=good \
-; RUN:   -profile-context-root=bad \
-; RUN:   -S < %s 2>&1 | FileCheck %s
+; RUN: split-file %s %t
+; RUN: not opt -passes=ctx-instr-gen,ctx-instr-lower -profile-context-root=the_func -S %t/musttail.ll -o - 2>&1 | FileCheck %s
+; RUN: not opt -passes=ctx-instr-gen,ctx-instr-lower -profile-context-root=the_func -S %t/unreachable.ll -o - 2>&1 | FileCheck %s
+; RUN: not opt -passes=ctx-instr-gen,ctx-instr-lower -profile-context-root=the_func -S %t/noreturn.ll -o - 2>&1 | FileCheck %s
 
+;--- musttail.ll
 declare void @foo()
 
-define void @good() {
-  call void @foo()
-  ret void
-}
-
-define void @bad() {
+define void @the_func() {
   musttail call void @foo()
   ret void
 }
+;--- unreachable.ll
+define void @the_func() {
+  unreachable
+}
+;--- noreturn.ll
+define void @the_func() noreturn {
+  unreachable
+}
 
-; CHECK: error: The function bad was indicated as a context root, but it features musttail calls, which is not supported.
+; CHECK: error: [ctxprof] The function the_func was indicated as context root
diff --git a/llvm/test/Transforms/PGOProfile/ctx-instrumentation.ll b/llvm/test/Transforms/PGOProfile/ctx-instrumentation.ll
index 6b2f25a585ec3..71d54f98d26e1 100644
--- a/llvm/test/Transforms/PGOProfile/ctx-instrumentation.ll
+++ b/llvm/test/Transforms/PGOProfile/ctx-instrumentation.ll
@@ -323,6 +323,18 @@ define void @does_not_return() noreturn {
 ;
   unreachable
 }
+
+define void @unreachable() {
+; INSTRUMENT-LABEL: define void @unreachable() {
+; INSTRUMENT-NEXT:    call void @llvm.instrprof.increment(ptr @unreachable, i64 742261418966908927, i32 1, i32 0)
+; INSTRUMENT-NEXT:    unreachable
+;
+; LOWERING-LABEL: define void @unreachable(
+; LOWERING-SAME: ) !guid [[META9:![0-9]+]] {
+; LOWERING-NEXT:    unreachable
+;
+  unreachable
+}
 ;.
 ; LOWERING: attributes #[[ATTR0]] = { noreturn }
 ; LOWERING: attributes #[[ATTR1:[0-9]+]] = { nounwind }
@@ -340,4 +352,5 @@ define void @does_not_return() noreturn {
 ; LOWERING: [[META6]] = !{i64 -3771893999295659109}
 ; LOWERING: [[META7]] = !{i64 -4680624981836544329}
 ; LOWERING: [[META8]] = !{i64 5519225910966780583}
+; LOWERING: [[META9]] = !{i64 -565652589829076809}
 ;.

@mtrofin mtrofin force-pushed the users/mtrofin/04-14-_ctxprof_extend_the_notion_of_cannot_return_ branch 2 times, most recently from d02d00b to 9642055 Compare April 14, 2025 18:27
@mtrofin mtrofin mentioned this pull request Apr 14, 2025
@mtrofin mtrofin force-pushed the users/mtrofin/04-14-_ctxprof_extend_the_notion_of_cannot_return_ branch from 9642055 to ea62923 Compare April 14, 2025 18:30
@mtrofin mtrofin force-pushed the users/mtrofin/04-14-_nfc_expose_canreturn_from_functionattrs branch from 8c0f413 to 57e7e32 Compare April 14, 2025 18:30
Base automatically changed from users/mtrofin/04-14-_nfc_expose_canreturn_from_functionattrs to main April 15, 2025 20:55
@mtrofin mtrofin force-pushed the users/mtrofin/04-14-_ctxprof_extend_the_notion_of_cannot_return_ branch 4 times, most recently from c43aeb1 to 6cc7a0c Compare April 16, 2025 01:03
Copy link
Contributor

@snehasish snehasish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mtrofin mtrofin force-pushed the users/mtrofin/04-14-_ctxprof_extend_the_notion_of_cannot_return_ branch 2 times, most recently from 5f26ea3 to ab506b5 Compare April 16, 2025 14:56
@mtrofin mtrofin force-pushed the users/mtrofin/04-14-_ctxprof_extend_the_notion_of_cannot_return_ branch from ab506b5 to 928963c Compare April 16, 2025 15:08
Copy link
Member Author

mtrofin commented Apr 16, 2025

Merge activity

  • Apr 16, 1:38 PM EDT: A user started a stack merge that includes this pull request via Graphite.
  • Apr 16, 1:39 PM EDT: A user merged this pull request with Graphite.

@mtrofin mtrofin merged commit 1576fa1 into main Apr 16, 2025
9 of 11 checks passed
@mtrofin mtrofin deleted the users/mtrofin/04-14-_ctxprof_extend_the_notion_of_cannot_return_ branch April 16, 2025 17:39
var-const pushed a commit to ldionne/llvm-project that referenced this pull request Apr 17, 2025
At the time of instrumentation (and instrumentation lowering), `noreturn` is not applied uniformously. Rather than running `FunctionAttrs` pass, we just need to use `llvm::canReturn` exposed in PR llvm#135650
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:transforms PGO Profile Guided Optimizations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants