diff options
author | Vitaly Buka <vitalybuka@google.com> | 2020-05-31 23:49:57 -0700 |
---|---|---|
committer | Vitaly Buka <vitalybuka@google.com> | 2020-06-10 02:43:28 -0700 |
commit | 4666953ce229cd7a8b042e5f80ecc97b4eae3636 (patch) | |
tree | c96ad1e3ae01bf6d658a6a829319b82291173dd0 /llvm/lib | |
parent | 8fd2270370244f0e93b4fd9ac4e13473f3cd7dd7 (diff) | |
download | llvm-4666953ce229cd7a8b042e5f80ecc97b4eae3636.zip llvm-4666953ce229cd7a8b042e5f80ecc97b4eae3636.tar.gz llvm-4666953ce229cd7a8b042e5f80ecc97b4eae3636.tar.bz2 |
[StackSafety] Add info into function summary
Summary:
This patch adds optional field into function summary,
implements asm and bitcode serialization. YAML
serialization is omitted and can be added later if
needed.
This patch includes this information into summary only
if module contains at least one sanitize_memtag function.
In a near future MTE is the user of the analysis.
Later if needed we can provede more direct control
on when information is included into summary.
Reviewers: eugenis
Subscribers: hiraditya, steven_wu, dexonsmith, arphaman, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D80908
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/Analysis/ModuleSummaryAnalysis.cpp | 45 | ||||
-rw-r--r-- | llvm/lib/Analysis/StackSafetyAnalysis.cpp | 34 | ||||
-rw-r--r-- | llvm/lib/AsmParser/LLLexer.cpp | 2 | ||||
-rw-r--r-- | llvm/lib/AsmParser/LLParser.cpp | 136 | ||||
-rw-r--r-- | llvm/lib/AsmParser/LLParser.h | 6 | ||||
-rw-r--r-- | llvm/lib/AsmParser/LLToken.h | 2 | ||||
-rw-r--r-- | llvm/lib/Bitcode/Reader/BitcodeAnalyzer.cpp | 1 | ||||
-rw-r--r-- | llvm/lib/Bitcode/Reader/BitcodeReader.cpp | 48 | ||||
-rw-r--r-- | llvm/lib/Bitcode/Writer/BitcodeWriter.cpp | 23 | ||||
-rw-r--r-- | llvm/lib/IR/AsmWriter.cpp | 30 | ||||
-rw-r--r-- | llvm/lib/IR/ModuleSummaryIndex.cpp | 2 |
11 files changed, 312 insertions, 17 deletions
diff --git a/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp b/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp index c5e5e32..9950798 100644 --- a/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp +++ b/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp @@ -25,6 +25,7 @@ #include "llvm/Analysis/IndirectCallPromotionAnalysis.h" #include "llvm/Analysis/LoopInfo.h" #include "llvm/Analysis/ProfileSummaryInfo.h" +#include "llvm/Analysis/StackSafetyAnalysis.h" #include "llvm/Analysis/TypeMetadataUtils.h" #include "llvm/IR/Attributes.h" #include "llvm/IR/BasicBlock.h" @@ -238,12 +239,12 @@ static bool isNonVolatileStore(const Instruction *I) { return false; } -static void computeFunctionSummary(ModuleSummaryIndex &Index, const Module &M, - const Function &F, BlockFrequencyInfo *BFI, - ProfileSummaryInfo *PSI, DominatorTree &DT, - bool HasLocalsInUsedOrAsm, - DenseSet<GlobalValue::GUID> &CantBePromoted, - bool IsThinLTO) { +static void computeFunctionSummary( + ModuleSummaryIndex &Index, const Module &M, const Function &F, + BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI, DominatorTree &DT, + bool HasLocalsInUsedOrAsm, DenseSet<GlobalValue::GUID> &CantBePromoted, + bool IsThinLTO, + std::function<const StackSafetyInfo *(const Function &F)> GetSSICallback) { // Summary not currently supported for anonymous functions, they should // have been named. assert(F.hasName()); @@ -469,12 +470,15 @@ static void computeFunctionSummary(ModuleSummaryIndex &Index, const Module &M, // Don't try to import functions with noinline attribute. F.getAttributes().hasFnAttribute(Attribute::NoInline), F.hasFnAttribute(Attribute::AlwaysInline)}; + std::vector<FunctionSummary::ParamAccess> ParamAccesses; + if (auto *SSI = GetSSICallback(F)) + ParamAccesses = SSI->getParamAccesses(); auto FuncSummary = std::make_unique<FunctionSummary>( Flags, NumInsts, FunFlags, /*EntryCount=*/0, std::move(Refs), CallGraphEdges.takeVector(), TypeTests.takeVector(), TypeTestAssumeVCalls.takeVector(), TypeCheckedLoadVCalls.takeVector(), TypeTestAssumeConstVCalls.takeVector(), - TypeCheckedLoadConstVCalls.takeVector()); + TypeCheckedLoadConstVCalls.takeVector(), std::move(ParamAccesses)); if (NonRenamableLocal) CantBePromoted.insert(F.getGUID()); Index.addGlobalValueSummary(F, std::move(FuncSummary)); @@ -643,7 +647,8 @@ static void setLiveRoot(ModuleSummaryIndex &Index, StringRef Name) { ModuleSummaryIndex llvm::buildModuleSummaryIndex( const Module &M, std::function<BlockFrequencyInfo *(const Function &F)> GetBFICallback, - ProfileSummaryInfo *PSI) { + ProfileSummaryInfo *PSI, + std::function<const StackSafetyInfo *(const Function &F)> GetSSICallback) { assert(PSI); bool EnableSplitLTOUnit = false; if (auto *MD = mdconst::extract_or_null<ConstantInt>( @@ -716,7 +721,8 @@ ModuleSummaryIndex llvm::buildModuleSummaryIndex( ArrayRef<FunctionSummary::VFuncId>{}, ArrayRef<FunctionSummary::VFuncId>{}, ArrayRef<FunctionSummary::ConstVCall>{}, - ArrayRef<FunctionSummary::ConstVCall>{}); + ArrayRef<FunctionSummary::ConstVCall>{}, + ArrayRef<FunctionSummary::ParamAccess>{}); Index.addGlobalValueSummary(*GV, std::move(Summary)); } else { std::unique_ptr<GlobalVarSummary> Summary = @@ -756,7 +762,7 @@ ModuleSummaryIndex llvm::buildModuleSummaryIndex( computeFunctionSummary(Index, M, F, BFI, PSI, DT, !LocalsUsed.empty() || HasLocalInlineAsmSymbol, - CantBePromoted, IsThinLTO); + CantBePromoted, IsThinLTO, GetSSICallback); } // Compute summaries for all variables defined in module, and save in the @@ -838,13 +844,19 @@ ModuleSummaryIndex ModuleSummaryIndexAnalysis::run(Module &M, ModuleAnalysisManager &AM) { ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M); auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); + bool NeedSSI = needsParamAccessSummary(M); return buildModuleSummaryIndex( M, [&FAM](const Function &F) { return &FAM.getResult<BlockFrequencyAnalysis>( *const_cast<Function *>(&F)); }, - &PSI); + &PSI, + [&FAM, NeedSSI](const Function &F) -> const StackSafetyInfo * { + return NeedSSI ? &FAM.getResult<StackSafetyAnalysis>( + const_cast<Function &>(F)) + : nullptr; + }); } char ModuleSummaryIndexWrapperPass::ID = 0; @@ -853,6 +865,7 @@ INITIALIZE_PASS_BEGIN(ModuleSummaryIndexWrapperPass, "module-summary-analysis", "Module Summary Analysis", false, true) INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass) INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass) +INITIALIZE_PASS_DEPENDENCY(StackSafetyInfoWrapperPass) INITIALIZE_PASS_END(ModuleSummaryIndexWrapperPass, "module-summary-analysis", "Module Summary Analysis", false, true) @@ -867,6 +880,7 @@ ModuleSummaryIndexWrapperPass::ModuleSummaryIndexWrapperPass() bool ModuleSummaryIndexWrapperPass::runOnModule(Module &M) { auto *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI(); + bool NeedSSI = needsParamAccessSummary(M); Index.emplace(buildModuleSummaryIndex( M, [this](const Function &F) { @@ -874,7 +888,13 @@ bool ModuleSummaryIndexWrapperPass::runOnModule(Module &M) { *const_cast<Function *>(&F)) .getBFI()); }, - PSI)); + PSI, + [&](const Function &F) -> const StackSafetyInfo * { + return NeedSSI ? &getAnalysis<StackSafetyInfoWrapperPass>( + const_cast<Function &>(F)) + .getResult() + : nullptr; + })); return false; } @@ -887,4 +907,5 @@ void ModuleSummaryIndexWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { AU.setPreservesAll(); AU.addRequired<BlockFrequencyInfoWrapperPass>(); AU.addRequired<ProfileSummaryInfoWrapperPass>(); + AU.addRequired<StackSafetyInfoWrapperPass>(); } diff --git a/llvm/lib/Analysis/StackSafetyAnalysis.cpp b/llvm/lib/Analysis/StackSafetyAnalysis.cpp index 9d96e9d..b23a955c 100644 --- a/llvm/lib/Analysis/StackSafetyAnalysis.cpp +++ b/llvm/lib/Analysis/StackSafetyAnalysis.cpp @@ -665,6 +665,33 @@ const StackSafetyGlobalInfo::InfoTy &StackSafetyGlobalInfo::getInfo() const { return *Info; } +// Converts a StackSafetyFunctionInfo to the relevant FunctionSummary +// constructor fields +std::vector<FunctionSummary::ParamAccess> +StackSafetyInfo::getParamAccesses() const { + assert(needsParamAccessSummary(*F->getParent())); + + std::vector<FunctionSummary::ParamAccess> ParamAccesses; + for (const auto &KV : getInfo().Info.Params) { + auto &PS = KV.second; + if (PS.Range.isFullSet()) + continue; + + ParamAccesses.emplace_back(KV.first, PS.Range); + FunctionSummary::ParamAccess &Param = ParamAccesses.back(); + + Param.Calls.reserve(PS.Calls.size()); + for (auto &C : PS.Calls) { + if (C.Offset.isFullSet()) { + ParamAccesses.pop_back(); + break; + } + Param.Calls.emplace_back(C.ParamNo, C.Callee->getGUID(), C.Offset); + } + } + return ParamAccesses; +} + StackSafetyGlobalInfo::StackSafetyGlobalInfo() = default; StackSafetyGlobalInfo::StackSafetyGlobalInfo( @@ -785,6 +812,13 @@ bool StackSafetyGlobalInfoWrapperPass::runOnModule(Module &M) { return false; } +bool llvm::needsParamAccessSummary(const Module &M) { + for (auto &F : M.functions()) + if (F.hasFnAttribute(Attribute::SanitizeMemTag)) + return true; + return false; +} + static const char LocalPassArg[] = "stack-safety-local"; static const char LocalPassName[] = "Stack Safety Local Analysis"; INITIALIZE_PASS_BEGIN(StackSafetyInfoWrapperPass, LocalPassArg, LocalPassName, diff --git a/llvm/lib/AsmParser/LLLexer.cpp b/llvm/lib/AsmParser/LLLexer.cpp index eb1209a..2a39e19 100644 --- a/llvm/lib/AsmParser/LLLexer.cpp +++ b/llvm/lib/AsmParser/LLLexer.cpp @@ -758,6 +758,8 @@ lltok::Kind LLLexer::LexIdentifier() { KEYWORD(alwaysInline); KEYWORD(calls); KEYWORD(callee); + KEYWORD(params); + KEYWORD(param); KEYWORD(hotness); KEYWORD(unknown); KEYWORD(hot); diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp index fb6e61f..117ec2c 100644 --- a/llvm/lib/AsmParser/LLParser.cpp +++ b/llvm/lib/AsmParser/LLParser.cpp @@ -11,6 +11,8 @@ //===----------------------------------------------------------------------===// #include "LLParser.h" +#include "LLToken.h" +#include "llvm/ADT/APSInt.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/None.h" #include "llvm/ADT/STLExtras.h" @@ -22,6 +24,7 @@ #include "llvm/IR/BasicBlock.h" #include "llvm/IR/CallingConv.h" #include "llvm/IR/Comdat.h" +#include "llvm/IR/ConstantRange.h" #include "llvm/IR/Constants.h" #include "llvm/IR/DebugInfoMetadata.h" #include "llvm/IR/DerivedTypes.h" @@ -8209,7 +8212,8 @@ bool LLParser::ParseGVEntry(unsigned ID) { /// FunctionSummary /// ::= 'function' ':' '(' 'module' ':' ModuleReference ',' GVFlags /// ',' 'insts' ':' UInt32 [',' OptionalFFlags]? [',' OptionalCalls]? -/// [',' OptionalTypeIdInfo]? [',' OptionalRefs]? ')' +/// [',' OptionalTypeIdInfo]? [',' OptionalParamAccesses]? +/// [',' OptionalRefs]? ')' bool LLParser::ParseFunctionSummary(std::string Name, GlobalValue::GUID GUID, unsigned ID) { assert(Lex.getKind() == lltok::kw_function); @@ -8222,6 +8226,7 @@ bool LLParser::ParseFunctionSummary(std::string Name, GlobalValue::GUID GUID, unsigned InstCount; std::vector<FunctionSummary::EdgeTy> Calls; FunctionSummary::TypeIdInfo TypeIdInfo; + std::vector<FunctionSummary::ParamAccess> ParamAccesses; std::vector<ValueInfo> Refs; // Default is all-zeros (conservative values). FunctionSummary::FFlags FFlags = {}; @@ -8253,6 +8258,10 @@ bool LLParser::ParseFunctionSummary(std::string Name, GlobalValue::GUID GUID, if (ParseOptionalRefs(Refs)) return true; break; + case lltok::kw_params: + if (ParseOptionalParamAccesses(ParamAccesses)) + return true; + break; default: return Error(Lex.getLoc(), "expected optional function summary field"); } @@ -8267,7 +8276,8 @@ bool LLParser::ParseFunctionSummary(std::string Name, GlobalValue::GUID GUID, std::move(TypeIdInfo.TypeTestAssumeVCalls), std::move(TypeIdInfo.TypeCheckedLoadVCalls), std::move(TypeIdInfo.TypeTestAssumeConstVCalls), - std::move(TypeIdInfo.TypeCheckedLoadConstVCalls)); + std::move(TypeIdInfo.TypeCheckedLoadConstVCalls), + std::move(ParamAccesses)); FS->setModulePath(ModulePath); @@ -8616,13 +8626,133 @@ bool LLParser::ParseOptionalVTableFuncs(VTableFuncList &VTableFuncs) { return false; } +/// ParamNo := 'param' ':' UInt64 +bool LLParser::ParseParamNo(uint64_t &ParamNo) { + if (ParseToken(lltok::kw_param, "expected 'param' here") || + ParseToken(lltok::colon, "expected ':' here") || ParseUInt64(ParamNo)) + return true; + return false; +} + +/// ParamAccessOffset := 'offset' ':' '[' APSINTVAL ',' APSINTVAL ']' +bool LLParser::ParseParamAccessOffset(ConstantRange &Range) { + APSInt Lower; + APSInt Upper; + auto ParseAPSInt = [&](APSInt &Val) { + if (Lex.getKind() != lltok::APSInt) + return TokError("expected integer"); + Val = Lex.getAPSIntVal(); + Val = Val.extOrTrunc(FunctionSummary::ParamAccess::RangeWidth); + Val.setIsSigned(true); + Lex.Lex(); + return false; + }; + if (ParseToken(lltok::kw_offset, "expected 'offset' here") || + ParseToken(lltok::colon, "expected ':' here") || + ParseToken(lltok::lsquare, "expected '[' here") || ParseAPSInt(Lower) || + ParseToken(lltok::comma, "expected ',' here") || ParseAPSInt(Upper) || + ParseToken(lltok::rsquare, "expected ']' here")) + return true; + + ++Upper; + Range = + (Lower == Upper && !Lower.isMaxValue()) + ? ConstantRange::getEmpty(FunctionSummary::ParamAccess::RangeWidth) + : ConstantRange(Lower, Upper); + + return false; +} + +/// ParamAccessCall +/// := '(' 'callee' ':' GVReference ',' ParamNo ',' ParamAccessOffset ')' +bool LLParser::ParseParamAccessCall(FunctionSummary::ParamAccess::Call &Call) { + if (ParseToken(lltok::lparen, "expected '(' here") || + ParseToken(lltok::kw_callee, "expected 'callee' here") || + ParseToken(lltok::colon, "expected ':' here")) + return true; + + unsigned GVId; + ValueInfo VI; + if (ParseGVReference(VI, GVId)) + return true; + + Call.Callee = VI.getGUID(); + + if (ParseToken(lltok::comma, "expected ',' here") || + ParseParamNo(Call.ParamNo) || + ParseToken(lltok::comma, "expected ',' here") || + ParseParamAccessOffset(Call.Offsets)) + return true; + + if (ParseToken(lltok::rparen, "expected ')' here")) + return true; + + return false; +} + +/// ParamAccess +/// := '(' ParamNo ',' ParamAccessOffset [',' OptionalParamAccessCalls]? ')' +/// OptionalParamAccessCalls := '(' Call [',' Call]* ')' +bool LLParser::ParseParamAccess(FunctionSummary::ParamAccess &Param) { + if (ParseToken(lltok::lparen, "expected '(' here") || + ParseParamNo(Param.ParamNo) || + ParseToken(lltok::comma, "expected ',' here") || + ParseParamAccessOffset(Param.Use)) + return true; + + if (EatIfPresent(lltok::comma)) { + if (ParseToken(lltok::kw_calls, "expected 'calls' here") || + ParseToken(lltok::colon, "expected ':' here") || + ParseToken(lltok::lparen, "expected '(' here")) + return true; + do { + FunctionSummary::ParamAccess::Call Call; + if (ParseParamAccessCall(Call)) + return true; + Param.Calls.push_back(Call); + } while (EatIfPresent(lltok::comma)); + + if (ParseToken(lltok::rparen, "expected ')' here")) + return true; + } + + if (ParseToken(lltok::rparen, "expected ')' here")) + return true; + + return false; +} + +/// OptionalParamAccesses +/// := 'params' ':' '(' ParamAccess [',' ParamAccess]* ')' +bool LLParser::ParseOptionalParamAccesses( + std::vector<FunctionSummary::ParamAccess> &Params) { + assert(Lex.getKind() == lltok::kw_params); + Lex.Lex(); + + if (ParseToken(lltok::colon, "expected ':' here") || + ParseToken(lltok::lparen, "expected '(' here")) + return true; + + do { + FunctionSummary::ParamAccess ParamAccess; + if (ParseParamAccess(ParamAccess)) + return true; + Params.push_back(ParamAccess); + } while (EatIfPresent(lltok::comma)); + + if (ParseToken(lltok::rparen, "expected ')' here")) + return true; + + return false; +} + /// OptionalRefs /// := 'refs' ':' '(' GVReference [',' GVReference]* ')' bool LLParser::ParseOptionalRefs(std::vector<ValueInfo> &Refs) { assert(Lex.getKind() == lltok::kw_refs); Lex.Lex(); - if (ParseToken(lltok::colon, "expected ':' in refs") | + if (ParseToken(lltok::colon, "expected ':' in refs") || ParseToken(lltok::lparen, "expected '(' in refs")) return true; diff --git a/llvm/lib/AsmParser/LLParser.h b/llvm/lib/AsmParser/LLParser.h index be23ba4..cc3aae6 100644 --- a/llvm/lib/AsmParser/LLParser.h +++ b/llvm/lib/AsmParser/LLParser.h @@ -365,6 +365,12 @@ namespace llvm { bool ParseVFuncId(FunctionSummary::VFuncId &VFuncId, IdToIndexMapType &IdToIndexMap, unsigned Index); bool ParseOptionalVTableFuncs(VTableFuncList &VTableFuncs); + bool ParseOptionalParamAccesses( + std::vector<FunctionSummary::ParamAccess> &Params); + bool ParseParamNo(uint64_t &ParamNo); + bool ParseParamAccess(FunctionSummary::ParamAccess &Param); + bool ParseParamAccessCall(FunctionSummary::ParamAccess::Call &Call); + bool ParseParamAccessOffset(ConstantRange &range); bool ParseOptionalRefs(std::vector<ValueInfo> &Refs); bool ParseTypeIdEntry(unsigned ID); bool ParseTypeIdSummary(TypeIdSummary &TIS); diff --git a/llvm/lib/AsmParser/LLToken.h b/llvm/lib/AsmParser/LLToken.h index a8b4ad9..0c190bf 100644 --- a/llvm/lib/AsmParser/LLToken.h +++ b/llvm/lib/AsmParser/LLToken.h @@ -391,6 +391,8 @@ enum Kind { kw_alwaysInline, kw_calls, kw_callee, + kw_params, + kw_param, kw_hotness, kw_unknown, kw_hot, diff --git a/llvm/lib/Bitcode/Reader/BitcodeAnalyzer.cpp b/llvm/lib/Bitcode/Reader/BitcodeAnalyzer.cpp index a72a33a..64530f3 100644 --- a/llvm/lib/Bitcode/Reader/BitcodeAnalyzer.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeAnalyzer.cpp @@ -306,6 +306,7 @@ static Optional<const char *> GetCodeName(unsigned CodeID, unsigned BlockID, STRINGIFY_CODE(FS, TYPE_ID) STRINGIFY_CODE(FS, TYPE_ID_METADATA) STRINGIFY_CODE(FS, BLOCK_COUNT) + STRINGIFY_CODE(FS, PARAM_ACCESS) } case bitc::METADATA_ATTACHMENT_ID: switch (CodeID) { diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp index 615b646..2f9331e 100644 --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -5804,6 +5804,41 @@ static void parseTypeIdSummaryRecord(ArrayRef<uint64_t> Record, parseWholeProgramDevirtResolution(Record, Strtab, Slot, TypeId); } +static std::vector<FunctionSummary::ParamAccess> +parseParamAccesses(ArrayRef<uint64_t> Record) { + auto ReadRange = [&]() { + APInt Lower(FunctionSummary::ParamAccess::RangeWidth, + BitcodeReader::decodeSignRotatedValue(Record.front())); + Record = Record.drop_front(); + APInt Upper(FunctionSummary::ParamAccess::RangeWidth, + BitcodeReader::decodeSignRotatedValue(Record.front())); + Record = Record.drop_front(); + ConstantRange Range{Lower, Upper}; + assert(!Range.isFullSet()); + assert(!Range.isUpperSignWrapped()); + return Range; + }; + + std::vector<FunctionSummary::ParamAccess> PendingParamAccesses; + while (!Record.empty()) { + PendingParamAccesses.emplace_back(); + FunctionSummary::ParamAccess &ParamAccess = PendingParamAccesses.back(); + ParamAccess.ParamNo = Record.front(); + Record = Record.drop_front(); + ParamAccess.Use = ReadRange(); + ParamAccess.Calls.resize(Record.front()); + Record = Record.drop_front(); + for (auto &Call : ParamAccess.Calls) { + Call.ParamNo = Record.front(); + Record = Record.drop_front(); + Call.Callee = Record.front(); + Record = Record.drop_front(); + Call.Offsets = ReadRange(); + } + } + return PendingParamAccesses; +} + void ModuleSummaryIndexBitcodeReader::parseTypeIdCompatibleVtableInfo( ArrayRef<uint64_t> Record, size_t &Slot, TypeIdCompatibleVtableInfo &TypeId) { @@ -5881,6 +5916,7 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { PendingTypeCheckedLoadVCalls; std::vector<FunctionSummary::ConstVCall> PendingTypeTestAssumeConstVCalls, PendingTypeCheckedLoadConstVCalls; + std::vector<FunctionSummary::ParamAccess> PendingParamAccesses; while (true) { Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); @@ -5979,7 +6015,8 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { std::move(PendingTypeTestAssumeVCalls), std::move(PendingTypeCheckedLoadVCalls), std::move(PendingTypeTestAssumeConstVCalls), - std::move(PendingTypeCheckedLoadConstVCalls)); + std::move(PendingTypeCheckedLoadConstVCalls), + std::move(PendingParamAccesses)); auto VIAndOriginalGUID = getValueInfoFromValueId(ValueID); FS->setModulePath(getThisModule()->first()); FS->setOriginalName(VIAndOriginalGUID.second); @@ -6121,7 +6158,8 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { std::move(PendingTypeTestAssumeVCalls), std::move(PendingTypeCheckedLoadVCalls), std::move(PendingTypeTestAssumeConstVCalls), - std::move(PendingTypeCheckedLoadConstVCalls)); + std::move(PendingTypeCheckedLoadConstVCalls), + std::move(PendingParamAccesses)); LastSeenSummary = FS.get(); LastSeenGUID = VI.getGUID(); FS->setModulePath(ModuleIdMap[ModuleId]); @@ -6242,6 +6280,12 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { case bitc::FS_BLOCK_COUNT: TheIndex.addBlockCount(Record[0]); + break; + + case bitc::FS_PARAM_ACCESS: { + PendingParamAccesses = parseParamAccesses(Record); + break; + } } } llvm_unreachable("Exit infinite loop"); diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp index 992cdfc..653d7f9 100644 --- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -3576,6 +3576,29 @@ static void writeFunctionTypeMetadataRecords(BitstreamWriter &Stream, FS->type_test_assume_const_vcalls()); WriteConstVCallVec(bitc::FS_TYPE_CHECKED_LOAD_CONST_VCALL, FS->type_checked_load_const_vcalls()); + + auto WriteRange = [&](ConstantRange Range) { + Range = Range.sextOrTrunc(FunctionSummary::ParamAccess::RangeWidth); + assert(Range.getLower().getNumWords() == 1); + assert(Range.getUpper().getNumWords() == 1); + emitSignedInt64(Record, *Range.getLower().getRawData()); + emitSignedInt64(Record, *Range.getUpper().getRawData()); + }; + + if (!FS->paramAccesses().empty()) { + Record.clear(); + for (auto &Arg : FS->paramAccesses()) { + Record.push_back(Arg.ParamNo); + WriteRange(Arg.Use); + Record.push_back(Arg.Calls.size()); + for (auto &Call : Arg.Calls) { + Record.push_back(Call.ParamNo); + Record.push_back(Call.Callee); + WriteRange(Call.Offsets); + } + } + Stream.EmitRecord(bitc::FS_PARAM_ACCESS, Record); + } } /// Collect type IDs from type tests used by function. diff --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp index 0fc7d66..efaeadb 100644 --- a/llvm/lib/IR/AsmWriter.cpp +++ b/llvm/lib/IR/AsmWriter.cpp @@ -3083,6 +3083,36 @@ void AssemblyWriter::printFunctionSummary(const FunctionSummary *FS) { if (const auto *TIdInfo = FS->getTypeIdInfo()) printTypeIdInfo(*TIdInfo); + + auto PrintRange = [&](const ConstantRange &Range) { + Out << "[" << Range.getLower() << ", " << Range.getSignedMax() << "]"; + }; + + if (!FS->paramAccesses().empty()) { + Out << ", params: ("; + FieldSeparator IFS; + for (auto &PS : FS->paramAccesses()) { + Out << IFS; + Out << "(param: " << PS.ParamNo; + Out << ", offset: "; + PrintRange(PS.Use); + if (!PS.Calls.empty()) { + Out << ", calls: ("; + FieldSeparator IFS; + for (auto &Call : PS.Calls) { + Out << IFS; + Out << "(callee: ^" << Machine.getGUIDSlot(Call.Callee); + Out << ", param: " << Call.ParamNo; + Out << ", offset: "; + PrintRange(Call.Offsets); + Out << ")"; + } + Out << ")"; + } + Out << ")"; + } + Out << ")"; + } } void AssemblyWriter::printTypeIdInfo( diff --git a/llvm/lib/IR/ModuleSummaryIndex.cpp b/llvm/lib/IR/ModuleSummaryIndex.cpp index 158369a..91612ea 100644 --- a/llvm/lib/IR/ModuleSummaryIndex.cpp +++ b/llvm/lib/IR/ModuleSummaryIndex.cpp @@ -35,6 +35,8 @@ static cl::opt<bool> ImportConstantsWithRefs( "import-constants-with-refs", cl::init(true), cl::Hidden, cl::desc("Import constant global variables with references")); +constexpr uint32_t FunctionSummary::ParamAccess::RangeWidth; + FunctionSummary FunctionSummary::ExternalNode = FunctionSummary::makeDummyFunctionSummary({}); |