diff options
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/Analysis/CtxProfAnalysis.cpp | 42 | ||||
-rw-r--r-- | llvm/lib/IR/Globals.cpp | 28 | ||||
-rw-r--r-- | llvm/lib/Passes/PassBuilder.cpp | 1 | ||||
-rw-r--r-- | llvm/lib/Passes/PassBuilderPipelines.cpp | 15 | ||||
-rw-r--r-- | llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp | 7 | ||||
-rw-r--r-- | llvm/lib/Transforms/Instrumentation/PGOCtxProfFlattening.cpp | 4 | ||||
-rw-r--r-- | llvm/lib/Transforms/Instrumentation/PGOCtxProfLowering.cpp | 3 | ||||
-rw-r--r-- | llvm/lib/Transforms/Utils/AssignGUID.cpp | 31 | ||||
-rw-r--r-- | llvm/lib/Transforms/Utils/CMakeLists.txt | 1 | ||||
-rw-r--r-- | llvm/lib/Transforms/Utils/CallPromotionUtils.cpp | 5 | ||||
-rw-r--r-- | llvm/lib/Transforms/Utils/InlineFunction.cpp | 4 |
11 files changed, 90 insertions, 51 deletions
diff --git a/llvm/lib/Analysis/CtxProfAnalysis.cpp b/llvm/lib/Analysis/CtxProfAnalysis.cpp index a363bce..7e8cb8e 100644 --- a/llvm/lib/Analysis/CtxProfAnalysis.cpp +++ b/llvm/lib/Analysis/CtxProfAnalysis.cpp @@ -48,8 +48,6 @@ static cl::opt<bool> ForceIsInSpecializedModule( cl::desc("Treat the given module as-if it were containing the " "post-thinlink module containing the root")); -const char *AssignGUIDPass::GUIDMetadataName = "guid"; - namespace llvm { class ProfileAnnotatorImpl final { friend class ProfileAnnotator; @@ -418,33 +416,6 @@ bool ProfileAnnotator::getOutgoingBranchWeights( return MaxCount > 0; } -PreservedAnalyses AssignGUIDPass::run(Module &M, ModuleAnalysisManager &MAM) { - for (auto &F : M.functions()) { - if (F.isDeclaration()) - continue; - if (F.getMetadata(GUIDMetadataName)) - continue; - const GlobalValue::GUID GUID = F.getGUID(); - F.setMetadata(GUIDMetadataName, - MDNode::get(M.getContext(), - {ConstantAsMetadata::get(ConstantInt::get( - Type::getInt64Ty(M.getContext()), GUID))})); - } - return PreservedAnalyses::none(); -} - -GlobalValue::GUID AssignGUIDPass::getGUID(const Function &F) { - if (F.isDeclaration()) { - assert(GlobalValue::isExternalLinkage(F.getLinkage())); - return F.getGUID(); - } - auto *MD = F.getMetadata(GUIDMetadataName); - assert(MD && "guid not found for defined function"); - return cast<ConstantInt>(cast<ConstantAsMetadata>(MD->getOperand(0)) - ->getValue() - ->stripPointerCasts()) - ->getZExtValue(); -} AnalysisKey CtxProfAnalysis::Key; CtxProfAnalysis::CtxProfAnalysis(std::optional<StringRef> Profile) @@ -513,7 +484,7 @@ PGOContextualProfile CtxProfAnalysis::run(Module &M, for (const auto &F : M) { if (F.isDeclaration()) continue; - auto GUID = AssignGUIDPass::getGUID(F); + auto GUID = F.getGUID(); assert(GUID && "guid not found for defined function"); const auto &Entry = F.begin(); uint32_t MaxCounters = 0; // we expect at least a counter. @@ -547,13 +518,6 @@ PGOContextualProfile CtxProfAnalysis::run(Module &M, return Result; } -GlobalValue::GUID -PGOContextualProfile::getDefinedFunctionGUID(const Function &F) const { - if (auto It = FuncInfo.find(AssignGUIDPass::getGUID(F)); It != FuncInfo.end()) - return It->first; - return 0; -} - CtxProfAnalysisPrinterPass::CtxProfAnalysisPrinterPass(raw_ostream &OS) : OS(OS), Mode(PrintLevel) {} @@ -669,7 +633,7 @@ bool PGOContextualProfile::isInSpecializedModule() const { void PGOContextualProfile::update(Visitor V, const Function &F) { assert(isFunctionKnown(F)); - GlobalValue::GUID G = getDefinedFunctionGUID(F); + GlobalValue::GUID G = F.getGUID(); for (auto *Node = FuncInfo.find(G)->second.Index.Next; Node; Node = Node->Next) V(*reinterpret_cast<PGOCtxProfContext *>(Node)); @@ -680,7 +644,7 @@ void PGOContextualProfile::visit(ConstVisitor V, const Function *F) const { return preorderVisit<const PGOCtxProfContext::CallTargetMapTy, const PGOCtxProfContext>(Profiles.Contexts, V); assert(isFunctionKnown(*F)); - GlobalValue::GUID G = getDefinedFunctionGUID(*F); + GlobalValue::GUID G = F->getGUID(); for (const auto *Node = FuncInfo.find(G)->second.Index.Next; Node; Node = Node->Next) V(*reinterpret_cast<const PGOCtxProfContext *>(Node)); diff --git a/llvm/lib/IR/Globals.cpp b/llvm/lib/IR/Globals.cpp index 7b799c7..4a4b5bb 100644 --- a/llvm/lib/IR/Globals.cpp +++ b/llvm/lib/IR/Globals.cpp @@ -78,6 +78,34 @@ GlobalValue::getGUIDAssumingExternalLinkage(StringRef GlobalIdentifier) { return MD5Hash(GlobalIdentifier); } +void GlobalValue::assignGUID() { + if (getMetadata(LLVMContext::MD_unique_id) != nullptr) + return; + + const GUID G = + GlobalValue::getGUIDAssumingExternalLinkage(getGlobalIdentifier()); + setMetadata( + LLVMContext::MD_unique_id, + MDNode::get(getContext(), {ConstantAsMetadata::get(ConstantInt::get( + Type::getInt64Ty(getContext()), G))})); +} + +GlobalValue::GUID GlobalValue::getGUID() const { + if (isDeclaration()) { + return GlobalValue::getGUIDAssumingExternalLinkage(getGlobalIdentifier()); + } + if (isa<GlobalAlias>(this)) { + return GlobalValue::getGUIDAssumingExternalLinkage(getGlobalIdentifier()); + } + + auto *MD = getMetadata(LLVMContext::MD_unique_id); + assert(MD != nullptr && "GUID was not assigned before calling GetGUID()"); + return cast<ConstantInt>(cast<ConstantAsMetadata>(MD->getOperand(0)) + ->getValue() + ->stripPointerCasts()) + ->getZExtValue(); +} + void GlobalValue::removeFromParent() { switch (getValueID()) { #define HANDLE_GLOBAL_VALUE(NAME) \ diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp index f810368..bb696ba 100644 --- a/llvm/lib/Passes/PassBuilder.cpp +++ b/llvm/lib/Passes/PassBuilder.cpp @@ -338,6 +338,7 @@ #include "llvm/Transforms/Scalar/TailRecursionElimination.h" #include "llvm/Transforms/Scalar/WarnMissedTransforms.h" #include "llvm/Transforms/Utils/AddDiscriminators.h" +#include "llvm/Transforms/Utils/AssignGUID.h" #include "llvm/Transforms/Utils/AssumeBundleBuilder.h" #include "llvm/Transforms/Utils/BreakCriticalEdges.h" #include "llvm/Transforms/Utils/CanonicalizeAliases.h" diff --git a/llvm/lib/Passes/PassBuilderPipelines.cpp b/llvm/lib/Passes/PassBuilderPipelines.cpp index 98821bb..0585303 100644 --- a/llvm/lib/Passes/PassBuilderPipelines.cpp +++ b/llvm/lib/Passes/PassBuilderPipelines.cpp @@ -130,6 +130,7 @@ #include "llvm/Transforms/Scalar/TailRecursionElimination.h" #include "llvm/Transforms/Scalar/WarnMissedTransforms.h" #include "llvm/Transforms/Utils/AddDiscriminators.h" +#include "llvm/Transforms/Utils/AssignGUID.h" #include "llvm/Transforms/Utils/AssumeBundleBuilder.h" #include "llvm/Transforms/Utils/CanonicalizeAliases.h" #include "llvm/Transforms/Utils/CountVisits.h" @@ -786,6 +787,7 @@ PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level, void PassBuilder::addRequiredLTOPreLinkPasses(ModulePassManager &MPM) { MPM.addPass(CanonicalizeAliasesPass()); MPM.addPass(NameAnonGlobalPass()); + MPM.addPass(AssignGUIDPass()); } void PassBuilder::addPreInlinerPasses(ModulePassManager &MPM, @@ -1234,9 +1236,6 @@ PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level, // In pre-link, we just want the instrumented IR. We use the contextual // profile in the post-thinlink phase. // The instrumentation will be removed in post-thinlink after IPO. - // FIXME(mtrofin): move AssignGUIDPass if there is agreement to use this - // mechanism for GUIDs. - MPM.addPass(AssignGUIDPass()); if (IsCtxProfUse) { MPM.addPass(PGOCtxProfFlatteningPass(/*IsPreThinlink=*/true)); return MPM; @@ -1640,6 +1639,11 @@ PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level, ModulePassManager MPM; + /* + MPM.addPass(NameAnonGlobalPass()); + MPM.addPass(AssignGUIDPass()); + */ + // Convert @llvm.global.annotations to !annotation metadata. MPM.addPass(Annotation2MetadataPass()); @@ -2208,6 +2212,11 @@ PassBuilder::buildO0DefaultPipeline(OptimizationLevel Level, ModulePassManager MPM; + /* + MPM.addPass(NameAnonGlobalPass()); + MPM.addPass(AssignGUIDPass()); + */ + // Perform pseudo probe instrumentation in O0 mode. This is for the // consistency between different build modes. For example, a LTO build can be // mixed with an O0 prelink and an O2 postlink. Loading a sample profile in diff --git a/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp b/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp index aec484f8..99be710 100644 --- a/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp +++ b/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp @@ -91,6 +91,7 @@ #include "llvm/TargetParser/Triple.h" #include "llvm/Transforms/IPO.h" #include "llvm/Transforms/IPO/FunctionAttrs.h" +#include "llvm/Transforms/Utils/AssignGUID.h" #include "llvm/Transforms/Utils/BasicBlockUtils.h" #include "llvm/Transforms/Utils/CallPromotionUtils.h" #include "llvm/Transforms/Utils/Evaluator.h" @@ -860,6 +861,12 @@ void llvm::updateVCallVisibilityInModule( function_ref<bool(StringRef)> IsVisibleToRegularObj) { if (!hasWholeProgramVisibility(WholeProgramVisibilityEnabledInLTO)) return; + + // Manually assign GUIDs -- updateVCallVisibilityInModule accesses GUIDs, and + // there's no way to specify it in the pass pipeline since this runs before + // any pass given on the command line. + AssignGUIDPass::runOnModule(M); + for (GlobalVariable &GV : M.globals()) { // Add linkage unit visibility to any variable with type metadata, which are // the vtable definitions. We won't have an existing vcall_visibility diff --git a/llvm/lib/Transforms/Instrumentation/PGOCtxProfFlattening.cpp b/llvm/lib/Transforms/Instrumentation/PGOCtxProfFlattening.cpp index 6128581..d71079e 100644 --- a/llvm/lib/Transforms/Instrumentation/PGOCtxProfFlattening.cpp +++ b/llvm/lib/Transforms/Instrumentation/PGOCtxProfFlattening.cpp @@ -126,7 +126,7 @@ void annotateIndirectCalls(Module &M, const CtxProfAnalysis::Result &CtxProf) { for (auto &F : M) { if (F.isDeclaration()) continue; - auto FlatProfIter = FlatIndCalls.find(AssignGUIDPass::getGUID(F)); + auto FlatProfIter = FlatIndCalls.find(F.getGUID()); if (FlatProfIter == FlatIndCalls.end()) continue; const auto &FlatProf = FlatProfIter->second; @@ -179,7 +179,7 @@ PreservedAnalyses PGOCtxProfFlatteningPass::run(Module &M, "Function has unreacheable basic blocks. The expectation was that " "DCE was run before."); - auto It = FlattenedProfile.find(AssignGUIDPass::getGUID(F)); + auto It = FlattenedProfile.find(F.getGUID()); // If this function didn't appear in the contextual profile, it's cold. if (It == FlattenedProfile.end()) clearColdFunctionProfile(F); diff --git a/llvm/lib/Transforms/Instrumentation/PGOCtxProfLowering.cpp b/llvm/lib/Transforms/Instrumentation/PGOCtxProfLowering.cpp index d741695..142db91 100644 --- a/llvm/lib/Transforms/Instrumentation/PGOCtxProfLowering.cpp +++ b/llvm/lib/Transforms/Instrumentation/PGOCtxProfLowering.cpp @@ -280,8 +280,7 @@ bool CtxInstrumentationLowerer::lowerFunction(Function &F) { assert(Mark->getIndex()->isZero()); IRBuilder<> Builder(Mark); - Guid = Builder.getInt64( - AssignGUIDPass::getGUID(cast<Function>(*Mark->getNameValue()))); + Guid = Builder.getInt64(cast<Function>(*Mark->getNameValue()).getGUID()); // The type of the context of this function is now knowable since we have // NumCallsites and NumCounters. We delcare it here because it's more // convenient - we have the Builder. diff --git a/llvm/lib/Transforms/Utils/AssignGUID.cpp b/llvm/lib/Transforms/Utils/AssignGUID.cpp new file mode 100644 index 0000000..7a7dba1 --- /dev/null +++ b/llvm/lib/Transforms/Utils/AssignGUID.cpp @@ -0,0 +1,31 @@ +//===-- AssignGUID.cpp - Unique identifier assignment pass ------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file provides a pass which assigns a a GUID (globally unique identifier) +// to every GlobalValue in the module, according to its current name, linkage, +// and originating file. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Transforms/Utils/AssignGUID.h" +#include "llvm/Support/Debug.h" + +using namespace llvm; + +void AssignGUIDPass::runOnModule(Module &M) { + for (auto &GV : M.globals()) { + if (GV.isDeclaration()) + continue; + GV.assignGUID(); + } + for (auto &F : M.functions()) { + if (F.isDeclaration()) + continue; + F.assignGUID(); + } +}
\ No newline at end of file diff --git a/llvm/lib/Transforms/Utils/CMakeLists.txt b/llvm/lib/Transforms/Utils/CMakeLists.txt index a4fa0e2..f387509 100644 --- a/llvm/lib/Transforms/Utils/CMakeLists.txt +++ b/llvm/lib/Transforms/Utils/CMakeLists.txt @@ -2,6 +2,7 @@ add_llvm_component_library(LLVMTransformUtils AddDiscriminators.cpp AMDGPUEmitPrintf.cpp ASanStackFrameLayout.cpp + AssignGUID.cpp AssumeBundleBuilder.cpp BasicBlockUtils.cpp BreakCriticalEdges.cpp diff --git a/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp b/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp index f0f9add..6912338 100644 --- a/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp +++ b/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp @@ -616,11 +616,11 @@ CallBase *llvm::promoteCallWithIfThenElse(CallBase &CB, Function &Callee, IndirectBBIns->setIndex(IndirectID); IndirectBBIns->insertInto(&IndirectBB, IndirectBB.getFirstInsertionPt()); - const GlobalValue::GUID CalleeGUID = AssignGUIDPass::getGUID(Callee); + const GlobalValue::GUID CalleeGUID = Callee.getGUID(); const uint32_t NewCountersSize = IndirectID + 1; auto ProfileUpdater = [&](PGOCtxProfContext &Ctx) { - assert(Ctx.guid() == AssignGUIDPass::getGUID(Caller)); + assert(Ctx.guid() == Caller.getGUID()); assert(NewCountersSize - 2 == Ctx.counters().size()); // All the ctx-es belonging to a function must have the same size counters. Ctx.resizeCounters(NewCountersSize); @@ -655,7 +655,6 @@ CallBase *llvm::promoteCallWithIfThenElse(CallBase &CB, Function &Callee, // times, and the indirect BB, IndirectCount times Ctx.counters()[DirectID] = DirectCount; Ctx.counters()[IndirectID] = IndirectCount; - }; CtxProf.update(ProfileUpdater, Caller); return &DirectCall; diff --git a/llvm/lib/Transforms/Utils/InlineFunction.cpp b/llvm/lib/Transforms/Utils/InlineFunction.cpp index ed3dca2..049d70c 100644 --- a/llvm/lib/Transforms/Utils/InlineFunction.cpp +++ b/llvm/lib/Transforms/Utils/InlineFunction.cpp @@ -2378,7 +2378,7 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI, // Get some preliminary data about the callsite before it might get inlined. // Inlining shouldn't delete the callee, but it's cleaner (and low-cost) to // get this data upfront and rely less on InlineFunction's behavior. - const auto CalleeGUID = AssignGUIDPass::getGUID(Callee); + const auto CalleeGUID = Callee.getGUID(); auto *CallsiteIDIns = CtxProfAnalysis::getCallsiteInstrumentation(CB); const auto CallsiteID = static_cast<uint32_t>(CallsiteIDIns->getIndex()->getZExtValue()); @@ -2403,7 +2403,7 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI, const uint32_t NewCountersSize = CtxProf.getNumCounters(Caller); auto Updater = [&](PGOCtxProfContext &Ctx) { - assert(Ctx.guid() == AssignGUIDPass::getGUID(Caller)); + assert(Ctx.guid() == Caller.getGUID()); const auto &[CalleeCounterMap, CalleeCallsiteMap] = IndicesMaps; assert( (Ctx.counters().size() + |