Skip to content

[ctxprof] don't inline weak symbols after instrumentation #128811

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
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
15 changes: 15 additions & 0 deletions llvm/include/llvm/Transforms/Instrumentation/PGOCtxProfLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,20 @@ class PGOCtxProfLoweringPass : public PassInfoMixin<PGOCtxProfLoweringPass> {

PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
};

// Utility pass blocking inlining for any function that may be overridden during
// linking by a prevailing copy.
// This avoids confusingly collecting profiles for the same GUID corresponding
// to different variants of the function. We could do like PGO and identify
// functions by a (GUID, Hash) tuple, but since the ctxprof "use" waits for
// thinlto to happen before performing any further optimizations, it's
// unnecessary to collect profiles for non-prevailing copies.
class NoinlineNonPrevailing : public PassInfoMixin<NoinlineNonPrevailing> {
public:
explicit NoinlineNonPrevailing() = default;

PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
};

} // namespace llvm
#endif
7 changes: 7 additions & 0 deletions llvm/lib/Passes/PassBuilderPipelines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,13 @@ PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
MPM.addPass(AssignGUIDPass());
if (IsCtxProfUse)
return MPM;
// Block further inlining in the instrumented ctxprof case. This avoids
// confusingly collecting profiles for the same GUID corresponding to
// different variants of the function. We could do like PGO and identify
// functions by a (GUID, Hash) tuple, but since the ctxprof "use" waits for
// thinlto to happen before performing any further optimizations, it's
// unnecessary to collect profiles for non-prevailing copies.
MPM.addPass(NoinlineNonPrevailing());
addPostPGOLoopRotation(MPM, Level);
MPM.addPass(PGOCtxProfLoweringPass());
} else if (IsColdFuncOnlyInstrGen) {
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Passes/PassRegistry.def
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ MODULE_PASS("cross-dso-cfi", CrossDSOCFIPass())
MODULE_PASS("ctx-instr-gen",
PGOInstrumentationGen(PGOInstrumentationType::CTXPROF))
MODULE_PASS("ctx-prof-flatten", PGOCtxProfFlatteningPass())
MODULE_PASS("noinline-nonprevailing", NoinlineNonPrevailing())
MODULE_PASS("deadargelim", DeadArgumentEliminationPass())
MODULE_PASS("debugify", NewPMDebugifyPass())
MODULE_PASS("dfsan", DataFlowSanitizerPass())
Expand Down
22 changes: 22 additions & 0 deletions llvm/lib/Transforms/Instrumentation/PGOCtxProfLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,3 +351,25 @@ bool CtxInstrumentationLowerer::lowerFunction(Function &F) {
F.getName());
return true;
}

PreservedAnalyses NoinlineNonPrevailing::run(Module &M,
ModuleAnalysisManager &MAM) {
bool Changed = false;
for (auto &F : M) {
if (F.isDeclaration())
continue;
if (F.hasFnAttribute(Attribute::NoInline))
continue;
if (!F.isWeakForLinker())
continue;

if (F.hasFnAttribute(Attribute::AlwaysInline))
F.removeFnAttr(Attribute::AlwaysInline);

F.addFnAttr(Attribute::NoInline);
Changed = true;
}
if (Changed)
return PreservedAnalyses::none();
return PreservedAnalyses::all();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
; RUN: opt -passes=noinline-nonprevailing -S < %s 2>&1 | FileCheck %s

define void @a() {
ret void
}

define void @b() #0 {
ret void
}

define weak_odr void @c() {
ret void
}

define weak_odr void @d() #0{
ret void
}

attributes #0 = { alwaysinline }

; CHECK: void @a() {
; CHECK: void @b() #0
; CHECK: void @c() #1
; CHECK: void @d() #1
; CHECK: attributes #1 = { noinline }
Loading