Skip to content
This repository has been archived by the owner on Jan 1, 2023. It is now read-only.

Commit

Permalink
[ThinLTO] Parse module summary index from assembly
Browse files Browse the repository at this point in the history
Summary:
Adds assembly parsing support for the module summary index (follow on
to r333335 which added the assembly writing support).

I added support to llvm-as to invoke the index parsing, so that it can
create either a bitcode file with a Module and a per-module index, or
a combined index without a Module.

I will send follow on patches soon to do the following:
- add support to tools such as llvm-lto2 to parse the per-module indexes
from assembly instead of bitcode when testing the thin link.
- verification support.

Depends on D47844 and D47842.

Reviewers: pcc, dexonsmith, mehdi_amini

Subscribers: inglorion, eraman, steven_wu, llvm-commits

Differential Revision: https://reviews.llvm.org/D47905

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@335602 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
teresajohnson committed Jun 26, 2018
1 parent a83173f commit edb2621
Show file tree
Hide file tree
Showing 20 changed files with 1,773 additions and 48 deletions.
76 changes: 73 additions & 3 deletions include/llvm/AsmParser/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ namespace llvm {
class Constant;
class LLVMContext;
class Module;
class ModuleSummaryIndex;
struct SlotMapping;
class SMDiagnostic;
class Type;

/// This function is the main interface to the LLVM Assembly Parser. It parses
/// This function is a main interface to the LLVM Assembly Parser. It parses
/// an ASCII file that (presumably) contains LLVM Assembly code. It returns a
/// Module (intermediate representation) with the corresponding features. Note
/// that this does not verify that the generated Module is valid, so you should
Expand Down Expand Up @@ -67,6 +68,46 @@ std::unique_ptr<Module> parseAssemblyString(StringRef AsmString,
bool UpgradeDebugInfo = true,
StringRef DataLayoutString = "");

/// Holds the Module and ModuleSummaryIndex returned by the interfaces
/// that parse both.
struct ParsedModuleAndIndex {
std::unique_ptr<Module> Mod;
std::unique_ptr<ModuleSummaryIndex> Index;
};

/// This function is a main interface to the LLVM Assembly Parser. It parses
/// an ASCII file that (presumably) contains LLVM Assembly code, including
/// a module summary. It returns a Module (intermediate representation) and
/// a ModuleSummaryIndex with the corresponding features. Note that this does
/// not verify that the generated Module or Index are valid, so you should
/// run the verifier after parsing the file to check that they are okay.
/// Parse LLVM Assembly from a file
/// \param Filename The name of the file to parse
/// \param Error Error result info.
/// \param Context Context in which to allocate globals info.
/// \param Slots The optional slot mapping that will be initialized during
/// parsing.
/// \param UpgradeDebugInfo Run UpgradeDebugInfo, which runs the Verifier.
/// This option should only be set to false by llvm-as
/// for use inside the LLVM testuite!
/// \param DataLayoutString Override datalayout in the llvm assembly.
ParsedModuleAndIndex
parseAssemblyFileWithIndex(StringRef Filename, SMDiagnostic &Error,
LLVMContext &Context, SlotMapping *Slots = nullptr,
bool UpgradeDebugInfo = true,
StringRef DataLayoutString = "");

/// This function is a main interface to the LLVM Assembly Parser. It parses
/// an ASCII file that (presumably) contains LLVM Assembly code for a module
/// summary. It returns a a ModuleSummaryIndex with the corresponding features.
/// Note that this does not verify that the generated Index is valid, so you
/// should run the verifier after parsing the file to check that it is okay.
/// Parse LLVM Assembly Index from a file
/// \param Filename The name of the file to parse
/// \param Error Error result info.
std::unique_ptr<ModuleSummaryIndex>
parseSummaryIndexAssemblyFile(StringRef Filename, SMDiagnostic &Error);

/// parseAssemblyFile and parseAssemblyString are wrappers around this function.
/// Parse LLVM Assembly from a MemoryBuffer.
/// \param F The MemoryBuffer containing assembly
Expand All @@ -83,13 +124,42 @@ std::unique_ptr<Module> parseAssembly(MemoryBufferRef F, SMDiagnostic &Err,
bool UpgradeDebugInfo = true,
StringRef DataLayoutString = "");

/// Parse LLVM Assembly including the summary index from a MemoryBuffer.
///
/// \param F The MemoryBuffer containing assembly with summary
/// \param Err Error result info.
/// \param Slots The optional slot mapping that will be initialized during
/// parsing.
/// \param UpgradeDebugInfo Run UpgradeDebugInfo, which runs the Verifier.
/// This option should only be set to false by llvm-as
/// for use inside the LLVM testuite!
/// \param DataLayoutString Override datalayout in the llvm assembly.
///
/// parseAssemblyFileWithIndex is a wrapper around this function.
ParsedModuleAndIndex parseAssemblyWithIndex(MemoryBufferRef F,
SMDiagnostic &Err,
LLVMContext &Context,
SlotMapping *Slots = nullptr,
bool UpgradeDebugInfo = true,
StringRef DataLayoutString = "");

/// Parse LLVM Assembly for summary index from a MemoryBuffer.
///
/// \param F The MemoryBuffer containing assembly with summary
/// \param Err Error result info.
///
/// parseSummaryIndexAssemblyFile is a wrapper around this function.
std::unique_ptr<ModuleSummaryIndex>
parseSummaryIndexAssembly(MemoryBufferRef F, SMDiagnostic &Err);

/// This function is the low-level interface to the LLVM Assembly Parser.
/// This is kept as an independent function instead of being inlined into
/// parseAssembly for the convenience of interactive users that want to add
/// recently parsed bits to an existing module.
///
/// \param F The MemoryBuffer containing assembly
/// \param M The module to add data to.
/// \param Index The index to add data to.
/// \param Err Error result info.
/// \param Slots The optional slot mapping that will be initialized during
/// parsing.
Expand All @@ -98,8 +168,8 @@ std::unique_ptr<Module> parseAssembly(MemoryBufferRef F, SMDiagnostic &Err,
/// This option should only be set to false by llvm-as
/// for use inside the LLVM testuite!
/// \param DataLayoutString Override datalayout in the llvm assembly.
bool parseAssemblyInto(MemoryBufferRef F, Module &M, SMDiagnostic &Err,
SlotMapping *Slots = nullptr,
bool parseAssemblyInto(MemoryBufferRef F, Module *M, ModuleSummaryIndex *Index,
SMDiagnostic &Err, SlotMapping *Slots = nullptr,
bool UpgradeDebugInfo = true,
StringRef DataLayoutString = "");

Expand Down
2 changes: 2 additions & 0 deletions include/llvm/IR/ModuleSummaryIndex.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,8 @@ class AliasSummary : public GlobalValueSummary {
void setAliasee(GlobalValueSummary *Aliasee) { AliaseeSummary = Aliasee; }
void setAliaseeGUID(GlobalValue::GUID GUID) { AliaseeGUID = GUID; }

bool hasAliasee() const { return !!AliaseeSummary; }

const GlobalValueSummary &getAliasee() const {
assert(AliaseeSummary && "Unexpected missing aliasee summary");
return *AliaseeSummary;
Expand Down
77 changes: 74 additions & 3 deletions lib/AsmParser/LLLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ static const char *isLabelTail(const char *CurPtr) {

LLLexer::LLLexer(StringRef StartBuf, SourceMgr &sm, SMDiagnostic &Err,
LLVMContext &C)
: CurBuf(StartBuf), ErrorInfo(Err), SM(sm), Context(C), APFloatVal(0.0) {
: CurBuf(StartBuf), ErrorInfo(Err), SM(sm), Context(C), APFloatVal(0.0),
IgnoreColonInIdentifiers(false) {
CurPtr = CurBuf.begin();
}

Expand Down Expand Up @@ -221,6 +222,8 @@ lltok::Kind LLLexer::LexToken() {
case '!': return LexExclaim();
case '^':
return LexCaret();
case ':':
return lltok::colon;
case '#': return LexHash();
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
Expand Down Expand Up @@ -461,8 +464,9 @@ lltok::Kind LLLexer::LexIdentifier() {
KeywordEnd = CurPtr;
}

// If we stopped due to a colon, this really is a label.
if (*CurPtr == ':') {
// If we stopped due to a colon, unless we were directed to ignore it,
// this really is a label.
if (!IgnoreColonInIdentifiers && *CurPtr == ':') {
StrVal.assign(StartChar-1, CurPtr++);
return lltok::LabelStr;
}
Expand Down Expand Up @@ -715,6 +719,73 @@ lltok::Kind LLLexer::LexIdentifier() {
KEYWORD(catch);
KEYWORD(filter);

// Summary index keywords.
KEYWORD(path);
KEYWORD(hash);
KEYWORD(gv);
KEYWORD(guid);
KEYWORD(name);
KEYWORD(summaries);
KEYWORD(flags);
KEYWORD(linkage);
KEYWORD(notEligibleToImport);
KEYWORD(live);
KEYWORD(dsoLocal);
KEYWORD(function);
KEYWORD(insts);
KEYWORD(funcFlags);
KEYWORD(readNone);
KEYWORD(readOnly);
KEYWORD(noRecurse);
KEYWORD(returnDoesNotAlias);
KEYWORD(calls);
KEYWORD(callee);
KEYWORD(hotness);
KEYWORD(unknown);
KEYWORD(hot);
KEYWORD(critical);
KEYWORD(relbf);
KEYWORD(variable);
KEYWORD(aliasee);
KEYWORD(refs);
KEYWORD(typeIdInfo);
KEYWORD(typeTests);
KEYWORD(typeTestAssumeVCalls);
KEYWORD(typeCheckedLoadVCalls);
KEYWORD(typeTestAssumeConstVCalls);
KEYWORD(typeCheckedLoadConstVCalls);
KEYWORD(vFuncId);
KEYWORD(offset);
KEYWORD(args);
KEYWORD(typeid);
KEYWORD(summary);
KEYWORD(typeTestRes);
KEYWORD(kind);
KEYWORD(unsat);
KEYWORD(byteArray);
KEYWORD(inline);
KEYWORD(single);
KEYWORD(allOnes);
KEYWORD(sizeM1BitWidth);
KEYWORD(alignLog2);
KEYWORD(sizeM1);
KEYWORD(bitMask);
KEYWORD(inlineBits);
KEYWORD(wpdResolutions);
KEYWORD(wpdRes);
KEYWORD(indir);
KEYWORD(singleImpl);
KEYWORD(branchFunnel);
KEYWORD(singleImplName);
KEYWORD(resByArg);
KEYWORD(byArg);
KEYWORD(uniformRetVal);
KEYWORD(uniqueRetVal);
KEYWORD(virtualConstProp);
KEYWORD(info);
KEYWORD(byte);
KEYWORD(bit);

#undef KEYWORD

// Keywords for types.
Expand Down
7 changes: 7 additions & 0 deletions lib/AsmParser/LLLexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ namespace llvm {
APFloat APFloatVal;
APSInt APSIntVal;

// When false (default), an identifier ending in ':' is a label token.
// When true, the ':' is treated as a separate token.
bool IgnoreColonInIdentifiers;

public:
explicit LLLexer(StringRef StartBuf, SourceMgr &SM, SMDiagnostic &,
LLVMContext &C);
Expand All @@ -59,6 +63,9 @@ namespace llvm {
const APSInt &getAPSIntVal() const { return APSIntVal; }
const APFloat &getAPFloatVal() const { return APFloatVal; }

void setIgnoreColonInIdentifiers(bool val) {
IgnoreColonInIdentifiers = val;
}

bool Error(LocTy L, const Twine &Msg) const;
bool Error(const Twine &Msg) const { return Error(getLoc(), Msg); }
Expand Down
Loading

0 comments on commit edb2621

Please sign in to comment.