Skip to content

Commit cfa6a59

Browse files
authored
[ctxprof] Don't lower instrumentation for noreturn functions (llvm#134932)
`noreturn` functions are doubtfully interesting for performance optimization / profiling.
1 parent 76d7227 commit cfa6a59

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

llvm/lib/Transforms/Instrumentation/PGOCtxProfLowering.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//
99

1010
#include "llvm/Transforms/Instrumentation/PGOCtxProfLowering.h"
11+
#include "llvm/ADT/STLExtras.h"
1112
#include "llvm/Analysis/CtxProfAnalysis.h"
1213
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
1314
#include "llvm/IR/Analysis.h"
@@ -206,6 +207,17 @@ PreservedAnalyses PGOCtxProfLoweringPass::run(Module &M,
206207
bool CtxInstrumentationLowerer::lowerFunction(Function &F) {
207208
if (F.isDeclaration())
208209
return false;
210+
211+
// Probably pointless to try to do anything here, unlikely to be
212+
// performance-affecting.
213+
if (F.doesNotReturn()) {
214+
for (auto &BB : F)
215+
for (auto &I : make_early_inc_range(BB))
216+
if (isa<InstrProfCntrInstBase>(&I))
217+
I.eraseFromParent();
218+
return true;
219+
}
220+
209221
auto &FAM = MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
210222
auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(F);
211223

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,26 @@ define void @has_musttail_calls() {
310310
musttail call void @bar()
311311
ret void
312312
}
313+
314+
define void @does_not_return() noreturn {
315+
; INSTRUMENT-LABEL: define void @does_not_return(
316+
; INSTRUMENT-SAME: ) #[[ATTR0:[0-9]+]] {
317+
; INSTRUMENT-NEXT: call void @llvm.instrprof.increment(ptr @does_not_return, i64 742261418966908927, i32 1, i32 0)
318+
; INSTRUMENT-NEXT: unreachable
319+
;
320+
; LOWERING-LABEL: define void @does_not_return(
321+
; LOWERING-SAME: ) #[[ATTR0:[0-9]+]] !guid [[META8:![0-9]+]] {
322+
; LOWERING-NEXT: unreachable
323+
;
324+
unreachable
325+
}
313326
;.
314-
; LOWERING: attributes #[[ATTR0:[0-9]+]] = { nounwind }
315-
; LOWERING: attributes #[[ATTR1:[0-9]+]] = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
327+
; LOWERING: attributes #[[ATTR0]] = { noreturn }
328+
; LOWERING: attributes #[[ATTR1:[0-9]+]] = { nounwind }
329+
; LOWERING: attributes #[[ATTR2:[0-9]+]] = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
316330
;.
317-
; INSTRUMENT: attributes #[[ATTR0:[0-9]+]] = { nounwind }
331+
; INSTRUMENT: attributes #[[ATTR0]] = { noreturn }
332+
; INSTRUMENT: attributes #[[ATTR1:[0-9]+]] = { nounwind }
318333
;.
319334
; LOWERING: [[META0]] = !{i64 6699318081062747564}
320335
; LOWERING: [[META1]] = !{i64 4909520559318251808}
@@ -324,4 +339,5 @@ define void @has_musttail_calls() {
324339
; LOWERING: [[META5]] = !{i64 5458232184388660970}
325340
; LOWERING: [[META6]] = !{i64 -3771893999295659109}
326341
; LOWERING: [[META7]] = !{i64 -4680624981836544329}
342+
; LOWERING: [[META8]] = !{i64 5519225910966780583}
327343
;.

0 commit comments

Comments
 (0)