Skip to content

Commit e37a973

Browse files
authored
[JTS][NFC] Optimize guid fetching (#161612)
It's unnecessary to build the whole symtable, and on top of everything, un-optimal to do so for every function. All we really need is the instrumented PGO name - considering also LTO-ness - and then we can compute the function name.
1 parent 3f3a20f commit e37a973

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

llvm/include/llvm/Transforms/Scalar/JumpTableToSwitch.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ namespace llvm {
1515

1616
class Function;
1717

18-
struct JumpTableToSwitchPass : PassInfoMixin<JumpTableToSwitchPass> {
18+
class JumpTableToSwitchPass : public PassInfoMixin<JumpTableToSwitchPass> {
19+
// Necessary until we switch to GUIDs as metadata, after which we can drop it.
20+
const bool InLTO;
21+
22+
public:
23+
explicit JumpTableToSwitchPass(bool InLTO = false) : InLTO(InLTO) {}
1924
/// Run the pass over the function.
2025
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
2126
};

llvm/lib/Passes/PassBuilderPipelines.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,9 @@ PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
611611

612612
// Jump table to switch conversion.
613613
if (EnableJumpTableToSwitch)
614-
FPM.addPass(JumpTableToSwitchPass());
614+
FPM.addPass(JumpTableToSwitchPass(
615+
/*InLTO=*/Phase == ThinOrFullLTOPhase::ThinLTOPostLink ||
616+
Phase == ThinOrFullLTOPhase::FullLTOPostLink));
615617

616618
FPM.addPass(
617619
SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true)));

llvm/lib/Transforms/Scalar/JumpTableToSwitch.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,12 @@ PreservedAnalyses JumpTableToSwitchPass::run(Function &F,
205205
PostDominatorTree *PDT = AM.getCachedResult<PostDominatorTreeAnalysis>(F);
206206
DomTreeUpdater DTU(DT, PDT, DomTreeUpdater::UpdateStrategy::Lazy);
207207
bool Changed = false;
208-
InstrProfSymtab Symtab;
209-
if (auto E = Symtab.create(*F.getParent()))
210-
F.getContext().emitError(
211-
"Could not create indirect call table, likely corrupted IR" +
212-
toString(std::move(E)));
213-
DenseMap<const Function *, GlobalValue::GUID> FToGuid;
214-
for (const auto &[G, FPtr] : Symtab.getIDToNameMap())
215-
FToGuid.insert({FPtr, G});
208+
auto FuncToGuid = [&](const Function &Fct) {
209+
if (Fct.getMetadata(AssignGUIDPass::GUIDMetadataName))
210+
return AssignGUIDPass::getGUID(Fct);
211+
212+
return Function::getGUIDAssumingExternalLinkage(getIRPGOFuncName(F, InLTO));
213+
};
216214

217215
for (BasicBlock &BB : make_early_inc_range(F)) {
218216
BasicBlock *CurrentBB = &BB;
@@ -234,12 +232,8 @@ PreservedAnalyses JumpTableToSwitchPass::run(Function &F,
234232
std::optional<JumpTableTy> JumpTable = parseJumpTable(GEP, PtrTy);
235233
if (!JumpTable)
236234
continue;
237-
SplittedOutTail = expandToSwitch(
238-
Call, *JumpTable, DTU, ORE, [&](const Function &Fct) {
239-
if (Fct.getMetadata(AssignGUIDPass::GUIDMetadataName))
240-
return AssignGUIDPass::getGUID(Fct);
241-
return FToGuid.lookup_or(&Fct, 0U);
242-
});
235+
SplittedOutTail =
236+
expandToSwitch(Call, *JumpTable, DTU, ORE, FuncToGuid);
243237
Changed = true;
244238
break;
245239
}

0 commit comments

Comments
 (0)