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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions llvm/lib/Transforms/Instrumentation/PGOCtxProfLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -105,6 +106,12 @@ std::pair<uint32_t, uint32_t> getNumCountersAndCallsites(const Function &F) {
}
return {NumCounters, NumCallsites};
}

void emitUnsupportedRootError(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.
Expand Down Expand Up @@ -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())
emitUnsupportedRootError(*F, "it features musttail calls");
}
}

Expand Down Expand Up @@ -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))
emitUnsupportedRootError(F, "it does not return");
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions llvm/test/Transforms/PGOProfile/ctx-instrumentation.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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}
;.
Loading