diff --git a/llvm/include/llvm/Passes/PassBuilder.h b/llvm/include/llvm/Passes/PassBuilder.h --- a/llvm/include/llvm/Passes/PassBuilder.h +++ b/llvm/include/llvm/Passes/PassBuilder.h @@ -127,6 +127,9 @@ /// Tuning option to enable/disable function merging. Its default value is /// false. bool MergeFunctions; + + /// Uniquefy function linkage name. Its default value is false. + bool UniqueLinkageNames; }; /// This class provides access to building LLVM's passes. diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp --- a/llvm/lib/Passes/PassBuilder.cpp +++ b/llvm/lib/Passes/PassBuilder.cpp @@ -285,6 +285,7 @@ LicmMssaNoAccForPromotionCap = SetLicmMssaNoAccForPromotionCap; CallGraphProfile = true; MergeFunctions = false; + UniqueLinkageNames = false; } extern cl::opt EnableConstraintElimination; @@ -1003,6 +1004,11 @@ ThinLTOPhase Phase) { ModulePassManager MPM(DebugLogging); + // Add UniqueInternalLinkageNames Pass which renames internal linkage + // symbols with unique names. + if (PTO.UniqueLinkageNames) + MPM.addPass(UniqueInternalLinkageNamesPass()); + // Place pseudo probe instrumentation as the first pass of the pipeline to // minimize the impact of optimization changes. if (PGOOpt && PGOOpt->PseudoProbeForProfiling && @@ -1773,6 +1779,11 @@ ModulePassManager MPM(DebugLogging); + // Add UniqueInternalLinkageNames Pass which renames internal linkage + // symbols with unique names. + if (PTO.UniqueLinkageNames) + MPM.addPass(UniqueInternalLinkageNamesPass()); + if (PGOOpt && (PGOOpt->Action == PGOOptions::IRInstr || PGOOpt->Action == PGOOptions::IRUse)) addPGOInstrPassesForO0( diff --git a/llvm/test/Other/new-pm-pseudo-probe.ll b/llvm/test/Other/new-pm-pseudo-probe.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Other/new-pm-pseudo-probe.ll @@ -0,0 +1,12 @@ +; RUN: opt -S -passes='default' -new-pm-pseudo-probe-for-profiling -debug-pass-manager < %s 2>&1 | FileCheck %s +; RUN: opt -S -passes='default' -new-pm-pseudo-probe-for-profiling -debug-pass-manager < %s 2>&1 | FileCheck %s +; RUN: opt -S -passes='thinlto-pre-link' -new-pm-pseudo-probe-for-profiling -debug-pass-manager < %s 2>&1 | FileCheck %s +; RUN: opt -S -passes='thinlto-pre-link' -new-pm-pseudo-probe-for-profiling -debug-pass-manager < %s 2>&1 | FileCheck %s + +define void @foo() { + ret void +} + +;; Check the SampleProfileProbePass is enabled under the -new-pm-pseudo-probe-for-profiling switch. +;; The switch can be used to test a specific pass order in a particular setup, e.g, in unique-internal-linkage-names.ll +; CHECK: Running pass: SampleProfileProbePass diff --git a/llvm/test/Transforms/UniqueLinkageNames/unique-internal-linkage-names.ll b/llvm/test/Transforms/UniqueLinkageNames/unique-internal-linkage-names.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/UniqueLinkageNames/unique-internal-linkage-names.ll @@ -0,0 +1,24 @@ +; RUN: opt -S -passes='default' -new-pm-pseudo-probe-for-profiling -new-pm-unique-internal-linkage-names -debug-pass-manager < %s 2>&1 | FileCheck %s --check-prefix=O0 --check-prefix=UNIQUE +; RUN: opt -S -passes='default' -new-pm-pseudo-probe-for-profiling -new-pm-unique-internal-linkage-names -debug-pass-manager < %s 2>&1 | FileCheck %s --check-prefix=O2 --check-prefix=UNIQUE +; RUN: opt -S -passes='default' -new-pm-pseudo-probe-for-profiling -new-pm-unique-internal-linkage-names -debug-pass-manager < %s 2>&1 | FileCheck %s --check-prefix=O2 --check-prefix=UNIQUE +; RUN: opt -S -passes='thinlto-pre-link' -new-pm-pseudo-probe-for-profiling -new-pm-unique-internal-linkage-names -debug-pass-manager < %s 2>&1 | FileCheck %s --check-prefix=O2 --check-prefix=UNIQUE +; RUN: opt -S -passes='thinlto-pre-link' -new-pm-pseudo-probe-for-profiling -new-pm-unique-internal-linkage-names -debug-pass-manager < %s 2>&1 | FileCheck %s --check-prefix=O2 --check-prefix=UNIQUE + +define internal i32 @foo() { +entry: + ret i32 0 +} + +define dso_local i32 (...)* @bar() { +entry: + ret i32 (...)* bitcast (i32 ()* @foo to i32 (...)*) +} + +; O0: Running pass: UniqueInternalLinkageNamesPass + +;; Check UniqueInternalLinkageNamesPass is scheduled before SampleProfileProbePass. +; O2: Running pass: UniqueInternalLinkageNamesPass +; O2: Running pass: SampleProfileProbePass + +; UNIQUE: define internal i32 @foo.__uniq.{{[0-9a-f]+}}() +; UNIQUE: ret {{.*}} @foo.__uniq.{{[0-9a-f]+}} {{.*}} diff --git a/llvm/tools/opt/NewPMDriver.cpp b/llvm/tools/opt/NewPMDriver.cpp --- a/llvm/tools/opt/NewPMDriver.cpp +++ b/llvm/tools/opt/NewPMDriver.cpp @@ -134,6 +134,13 @@ static cl::opt DebugInfoForProfiling( "new-pm-debug-info-for-profiling", cl::init(false), cl::Hidden, cl::desc("Emit special debug info to enable PGO profile generation.")); +static cl::opt PseudoProbeForProfiling( + "new-pm-pseudo-probe-for-profiling", cl::init(false), cl::Hidden, + cl::desc("Emit pseudo probes to enable PGO profile generation.")); +static cl::opt UniqueInternalLinkageNames( + "new-pm-unique-internal-linkage-names", cl::init(false), cl::Hidden, + cl::desc("Uniqueify Internal Linkage Symbol Names by appending the MD5 " + "hash of the module path.")); /// @}} template @@ -247,6 +254,9 @@ if (DebugInfoForProfiling) P = PGOOptions("", "", "", PGOOptions::NoAction, PGOOptions::NoCSAction, true); + else if (PseudoProbeForProfiling) + P = PGOOptions("", "", "", PGOOptions::NoAction, PGOOptions::NoCSAction, + false, true); else P = None; } @@ -282,6 +292,7 @@ // option has been enabled. PTO.LoopUnrolling = !DisableLoopUnrolling; PTO.Coroutines = Coroutines; + PTO.UniqueLinkageNames = UniqueInternalLinkageNames; PassBuilder PB(DebugPM, TM, PTO, P, &PIC); registerEPCallbacks(PB);