Skip to content

Commit 1576fa1

Browse files
authored
[ctxprof] Extend the notion of "cannot return" (#135651)
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 #135650
1 parent 80c19b3 commit 1576fa1

File tree

3 files changed

+40
-17
lines changed

3 files changed

+40
-17
lines changed

llvm/lib/Transforms/Instrumentation/PGOCtxProfLowering.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "llvm/Transforms/Instrumentation/PGOCtxProfLowering.h"
1111
#include "llvm/ADT/STLExtras.h"
12+
#include "llvm/Analysis/CFG.h"
1213
#include "llvm/Analysis/CtxProfAnalysis.h"
1314
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
1415
#include "llvm/IR/Analysis.h"
@@ -105,6 +106,12 @@ std::pair<uint32_t, uint32_t> getNumCountersAndCallsites(const Function &F) {
105106
}
106107
return {NumCounters, NumCallsites};
107108
}
109+
110+
void emitUnsupportedRootError(const Function &F, StringRef Reason) {
111+
F.getContext().emitError("[ctxprof] The function " + F.getName() +
112+
" was indicated as context root but " + Reason +
113+
", which is not supported.");
114+
}
108115
} // namespace
109116

110117
// set up tie-in with compiler-rt.
@@ -164,12 +171,8 @@ CtxInstrumentationLowerer::CtxInstrumentationLowerer(Module &M,
164171
for (const auto &BB : *F)
165172
for (const auto &I : BB)
166173
if (const auto *CB = dyn_cast<CallBase>(&I))
167-
if (CB->isMustTailCall()) {
168-
M.getContext().emitError("The function " + Fname +
169-
" was indicated as a context root, "
170-
"but it features musttail "
171-
"calls, which is not supported.");
172-
}
174+
if (CB->isMustTailCall())
175+
emitUnsupportedRootError(*F, "it features musttail calls");
173176
}
174177
}
175178

@@ -230,11 +233,13 @@ bool CtxInstrumentationLowerer::lowerFunction(Function &F) {
230233

231234
// Probably pointless to try to do anything here, unlikely to be
232235
// performance-affecting.
233-
if (F.doesNotReturn()) {
236+
if (!llvm::canReturn(F)) {
234237
for (auto &BB : F)
235238
for (auto &I : make_early_inc_range(BB))
236239
if (isa<InstrProfCntrInstBase>(&I))
237240
I.eraseFromParent();
241+
if (ContextRootSet.contains(&F))
242+
emitUnsupportedRootError(F, "it does not return");
238243
return true;
239244
}
240245

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1-
; RUN: not opt -passes=ctx-instr-gen,ctx-instr-lower -profile-context-root=good \
2-
; RUN: -profile-context-root=bad \
3-
; RUN: -S < %s 2>&1 | FileCheck %s
1+
; RUN: split-file %s %t
2+
; RUN: not opt -passes=ctx-instr-gen,ctx-instr-lower -profile-context-root=the_func -S %t/musttail.ll -o - 2>&1 | FileCheck %s
3+
; RUN: not opt -passes=ctx-instr-gen,ctx-instr-lower -profile-context-root=the_func -S %t/unreachable.ll -o - 2>&1 | FileCheck %s
4+
; RUN: not opt -passes=ctx-instr-gen,ctx-instr-lower -profile-context-root=the_func -S %t/noreturn.ll -o - 2>&1 | FileCheck %s
45

6+
;--- musttail.ll
57
declare void @foo()
68

7-
define void @good() {
8-
call void @foo()
9-
ret void
10-
}
11-
12-
define void @bad() {
9+
define void @the_func() {
1310
musttail call void @foo()
1411
ret void
1512
}
13+
;--- unreachable.ll
14+
define void @the_func() {
15+
unreachable
16+
}
17+
;--- noreturn.ll
18+
define void @the_func() noreturn {
19+
unreachable
20+
}
1621

17-
; CHECK: error: The function bad was indicated as a context root, but it features musttail calls, which is not supported.
22+
; CHECK: error: [ctxprof] The function the_func was indicated as context root

llvm/test/Transforms/PGOProfile/ctx-instrumentation.ll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,18 @@ define void @does_not_return() noreturn {
323323
;
324324
unreachable
325325
}
326+
327+
define void @unreachable() {
328+
; INSTRUMENT-LABEL: define void @unreachable() {
329+
; INSTRUMENT-NEXT: call void @llvm.instrprof.increment(ptr @unreachable, i64 742261418966908927, i32 1, i32 0)
330+
; INSTRUMENT-NEXT: unreachable
331+
;
332+
; LOWERING-LABEL: define void @unreachable(
333+
; LOWERING-SAME: ) !guid [[META9:![0-9]+]] {
334+
; LOWERING-NEXT: unreachable
335+
;
336+
unreachable
337+
}
326338
;.
327339
; LOWERING: attributes #[[ATTR0]] = { noreturn }
328340
; LOWERING: attributes #[[ATTR1:[0-9]+]] = { nounwind }
@@ -340,4 +352,5 @@ define void @does_not_return() noreturn {
340352
; LOWERING: [[META6]] = !{i64 -3771893999295659109}
341353
; LOWERING: [[META7]] = !{i64 -4680624981836544329}
342354
; LOWERING: [[META8]] = !{i64 5519225910966780583}
355+
; LOWERING: [[META9]] = !{i64 -565652589829076809}
343356
;.

0 commit comments

Comments
 (0)