Skip to content

[ThinLTO]Record import type in GlobalValueSummary::GVFlags #87597

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
merged 9 commits into from
Apr 11, 2024
1 change: 1 addition & 0 deletions llvm/include/llvm/AsmParser/LLToken.h
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ enum Kind {
kw_live,
kw_dsoLocal,
kw_canAutoHide,
kw_importAsDec,
kw_function,
kw_insts,
kw_funcFlags,
Expand Down
14 changes: 11 additions & 3 deletions llvm/include/llvm/IR/ModuleSummaryIndex.h
Original file line number Diff line number Diff line change
Expand Up @@ -472,14 +472,20 @@ class GlobalValueSummary {
/// means the symbol was externally visible.
unsigned CanAutoHide : 1;

/// This field is written by the ThinLTO indexing step to postlink
/// per-module summary. It indicates whether a global value (function or
/// function alias, etc) should be imported as a definition or declaration.
unsigned ImportAsDec : 1;

/// Convenience Constructors
explicit GVFlags(GlobalValue::LinkageTypes Linkage,
GlobalValue::VisibilityTypes Visibility,
bool NotEligibleToImport, bool Live, bool IsLocal,
bool CanAutoHide)
bool CanAutoHide, bool ImportAsDec)
: Linkage(Linkage), Visibility(Visibility),
NotEligibleToImport(NotEligibleToImport), Live(Live),
DSOLocal(IsLocal), CanAutoHide(CanAutoHide) {}
DSOLocal(IsLocal), CanAutoHide(CanAutoHide),
ImportAsDec(ImportAsDec) {}
};

private:
Expand Down Expand Up @@ -564,6 +570,8 @@ class GlobalValueSummary {

bool canAutoHide() const { return Flags.CanAutoHide; }

bool shouldImportAsDec() const { return Flags.ImportAsDec; }

GlobalValue::VisibilityTypes getVisibility() const {
return (GlobalValue::VisibilityTypes)Flags.Visibility;
}
Expand Down Expand Up @@ -813,7 +821,7 @@ class FunctionSummary : public GlobalValueSummary {
GlobalValue::LinkageTypes::AvailableExternallyLinkage,
GlobalValue::DefaultVisibility,
/*NotEligibleToImport=*/true, /*Live=*/true, /*IsLocal=*/false,
/*CanAutoHide=*/false),
/*CanAutoHide=*/false, /*ImportAsDec=*/false),
/*NumInsts=*/0, FunctionSummary::FFlags{}, /*EntryCount=*/0,
std::vector<ValueInfo>(), std::move(Edges),
std::vector<GlobalValue::GUID>(),
Expand Down
7 changes: 4 additions & 3 deletions llvm/include/llvm/IR/ModuleSummaryIndexYAML.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ template <> struct MappingTraits<TypeIdSummary> {

struct FunctionSummaryYaml {
unsigned Linkage, Visibility;
bool NotEligibleToImport, Live, IsLocal, CanAutoHide;
bool NotEligibleToImport, Live, IsLocal, CanAutoHide, ImportAsDec;
std::vector<uint64_t> Refs;
std::vector<uint64_t> TypeTests;
std::vector<FunctionSummary::VFuncId> TypeTestAssumeVCalls,
Expand Down Expand Up @@ -227,7 +227,7 @@ template <> struct CustomMappingTraits<GlobalValueSummaryMapTy> {
static_cast<GlobalValue::LinkageTypes>(FSum.Linkage),
static_cast<GlobalValue::VisibilityTypes>(FSum.Visibility),
FSum.NotEligibleToImport, FSum.Live, FSum.IsLocal,
FSum.CanAutoHide),
FSum.CanAutoHide, FSum.ImportAsDec),
/*NumInsts=*/0, FunctionSummary::FFlags{}, /*EntryCount=*/0, Refs,
ArrayRef<FunctionSummary::EdgeTy>{}, std::move(FSum.TypeTests),
std::move(FSum.TypeTestAssumeVCalls),
Expand All @@ -251,7 +251,8 @@ template <> struct CustomMappingTraits<GlobalValueSummaryMapTy> {
static_cast<bool>(FSum->flags().NotEligibleToImport),
static_cast<bool>(FSum->flags().Live),
static_cast<bool>(FSum->flags().DSOLocal),
static_cast<bool>(FSum->flags().CanAutoHide), Refs,
static_cast<bool>(FSum->flags().CanAutoHide),
static_cast<bool>(FSum->flags().ImportAsDec), Refs,
FSum->type_tests(), FSum->type_test_assume_vcalls(),
FSum->type_checked_load_vcalls(),
FSum->type_test_assume_const_vcalls(),
Expand Down
12 changes: 8 additions & 4 deletions llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,8 @@ static void computeFunctionSummary(
HasIndirBranchToBlockAddress || HasIFuncCall;
GlobalValueSummary::GVFlags Flags(
F.getLinkage(), F.getVisibility(), NotEligibleForImport,
/* Live = */ false, F.isDSOLocal(), F.canBeOmittedFromSymbolTable());
/* Live = */ false, F.isDSOLocal(), F.canBeOmittedFromSymbolTable(),
/*ImportAsDec=*/false);
FunctionSummary::FFlags FunFlags{
F.doesNotAccessMemory(), F.onlyReadsMemory() && !F.doesNotAccessMemory(),
F.hasFnAttribute(Attribute::NoRecurse), F.returnDoesNotAlias(),
Expand Down Expand Up @@ -761,7 +762,8 @@ static void computeVariableSummary(ModuleSummaryIndex &Index,
bool NonRenamableLocal = isNonRenamableLocal(V);
GlobalValueSummary::GVFlags Flags(
V.getLinkage(), V.getVisibility(), NonRenamableLocal,
/* Live = */ false, V.isDSOLocal(), V.canBeOmittedFromSymbolTable());
/* Live = */ false, V.isDSOLocal(), V.canBeOmittedFromSymbolTable(),
/* ImportAsDec= */ false);

VTableFuncList VTableFuncs;
// If splitting is not enabled, then we compute the summary information
Expand Down Expand Up @@ -807,7 +809,8 @@ static void computeAliasSummary(ModuleSummaryIndex &Index, const GlobalAlias &A,
bool NonRenamableLocal = isNonRenamableLocal(A);
GlobalValueSummary::GVFlags Flags(
A.getLinkage(), A.getVisibility(), NonRenamableLocal,
/* Live = */ false, A.isDSOLocal(), A.canBeOmittedFromSymbolTable());
/* Live = */ false, A.isDSOLocal(), A.canBeOmittedFromSymbolTable(),
/* ImportAsDec= */ false);
auto AS = std::make_unique<AliasSummary>(Flags);
auto AliaseeVI = Index.getValueInfo(Aliasee->getGUID());
assert(AliaseeVI && "Alias expects aliasee summary to be available");
Expand Down Expand Up @@ -887,7 +890,8 @@ ModuleSummaryIndex llvm::buildModuleSummaryIndex(
GlobalValue::InternalLinkage, GlobalValue::DefaultVisibility,
/* NotEligibleToImport = */ true,
/* Live = */ true,
/* Local */ GV->isDSOLocal(), GV->canBeOmittedFromSymbolTable());
/* Local */ GV->isDSOLocal(), GV->canBeOmittedFromSymbolTable(),
/* ImportAsDec= */ false);
CantBePromoted.insert(GV->getGUID());
// Create the appropriate summary type.
if (Function *F = dyn_cast<Function>(GV)) {
Expand Down
15 changes: 12 additions & 3 deletions llvm/lib/AsmParser/LLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9215,7 +9215,8 @@ bool LLParser::parseFunctionSummary(std::string Name, GlobalValue::GUID GUID,
GlobalValueSummary::GVFlags GVFlags = GlobalValueSummary::GVFlags(
GlobalValue::ExternalLinkage, GlobalValue::DefaultVisibility,
/*NotEligibleToImport=*/false,
/*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false);
/*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false,
/*ImportAsDec=*/false);
unsigned InstCount;
std::vector<FunctionSummary::EdgeTy> Calls;
FunctionSummary::TypeIdInfo TypeIdInfo;
Expand Down Expand Up @@ -9302,7 +9303,8 @@ bool LLParser::parseVariableSummary(std::string Name, GlobalValue::GUID GUID,
GlobalValueSummary::GVFlags GVFlags = GlobalValueSummary::GVFlags(
GlobalValue::ExternalLinkage, GlobalValue::DefaultVisibility,
/*NotEligibleToImport=*/false,
/*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false);
/*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false,
/*ImportAsDec*/ false);
GlobalVarSummary::GVarFlags GVarFlags(/*ReadOnly*/ false,
/* WriteOnly */ false,
/* Constant */ false,
Expand Down Expand Up @@ -9360,7 +9362,8 @@ bool LLParser::parseAliasSummary(std::string Name, GlobalValue::GUID GUID,
GlobalValueSummary::GVFlags GVFlags = GlobalValueSummary::GVFlags(
GlobalValue::ExternalLinkage, GlobalValue::DefaultVisibility,
/*NotEligibleToImport=*/false,
/*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false);
/*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false,
/*ImportAsDec=*/false);
if (parseToken(lltok::colon, "expected ':' here") ||
parseToken(lltok::lparen, "expected '(' here") ||
parseModuleReference(ModulePath) ||
Expand Down Expand Up @@ -10146,6 +10149,12 @@ bool LLParser::parseGVFlags(GlobalValueSummary::GVFlags &GVFlags) {
return true;
GVFlags.CanAutoHide = Flag;
break;
case lltok::kw_importAsDec:
Lex.Lex();
if (parseToken(lltok::colon, "expected ':'") || parseFlag(Flag))
return true;
GVFlags.ImportAsDec = Flag;
break;
default:
return error(Lex.getLoc(), "expected gv flag type");
}
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Bitcode/Reader/BitcodeReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,7 @@ static GlobalValueSummary::GVFlags getDecodedGVSummaryFlags(uint64_t RawFlags,
// to getDecodedLinkage() will need to be taken into account here as above.
auto Linkage = GlobalValue::LinkageTypes(RawFlags & 0xF); // 4 bits
auto Visibility = GlobalValue::VisibilityTypes((RawFlags >> 8) & 3); // 2 bits
bool ImportAsDec = ((RawFlags >> 10) & 1);
RawFlags = RawFlags >> 4;
bool NotEligibleToImport = (RawFlags & 0x1) || Version < 3;
// The Live flag wasn't introduced until version 3. For dead stripping
Expand All @@ -1143,7 +1144,7 @@ static GlobalValueSummary::GVFlags getDecodedGVSummaryFlags(uint64_t RawFlags,
bool AutoHide = (RawFlags & 0x8);

return GlobalValueSummary::GVFlags(Linkage, Visibility, NotEligibleToImport,
Live, Local, AutoHide);
Live, Local, AutoHide, ImportAsDec);
}

// Decode the flags for GlobalVariable in the summary
Expand Down
5 changes: 4 additions & 1 deletion llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,8 @@ static uint64_t getEncodedFFlags(FunctionSummary::FFlags Flags) {

// Decode the flags for GlobalValue in the summary. See getDecodedGVSummaryFlags
// in BitcodeReader.cpp.
static uint64_t getEncodedGVSummaryFlags(GlobalValueSummary::GVFlags Flags) {
static uint64_t getEncodedGVSummaryFlags(GlobalValueSummary::GVFlags Flags,
bool ImportAsDec = false) {
uint64_t RawFlags = 0;

RawFlags |= Flags.NotEligibleToImport; // bool
Expand All @@ -1217,6 +1218,8 @@ static uint64_t getEncodedGVSummaryFlags(GlobalValueSummary::GVFlags Flags) {

RawFlags |= (Flags.Visibility << 8); // 2 bits

RawFlags |= ((ImportAsDec) & (1 << 10)); // 1 bit

return RawFlags;
}

Expand Down