llvm.org GIT mirror llvm / 6163b4a
Bitcode: Add a string table to the bitcode format. Add a top-level STRTAB block containing a string table blob, and start storing strings for module codes FUNCTION, GLOBALVAR, ALIAS, IFUNC and COMDAT in the string table. This change allows us to share names between globals and comdats as well as between modules, and improves the efficiency of loading bitcode files by no longer using a bit encoding for symbol names. Once we start writing the irsymtab to the bitcode file we will also be able to share strings between it and the module. On my machine, link time for Chromium for Linux with ThinLTO decreases by about 7% for no-op incremental builds or about 1% for full builds. Total bitcode file size decreases by about 3%. As discussed on llvm-dev: http://lists.llvm.org/pipermail/llvm-dev/2017-April/111732.html Differential Revision: https://reviews.llvm.org/D31838 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@300464 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Collingbourne 3 years ago
21 changed file(s) with 677 addition(s) and 442 deletion(s). Raw diff Collapse all Expand all
549549
550550 * 17 --- `TYPE_BLOCK`_ --- This describes all of the types in the module.
551551
552 * 23 --- `STRTAB_BLOCK`_ --- The bitcode file's string table.
553
552554 .. _MODULE_BLOCK:
553555
554556 MODULE_BLOCK Contents
576578 ``[VERSION, version#]``
577579
578580 The ``VERSION`` record (code 1) contains a single value indicating the format
579 version. Versions 0 and 1 are supported at this time. The difference between
581 version. Versions 0, 1 and 2 are supported at this time. The difference between
580582 version 0 and 1 is in the encoding of instruction operands in
581583 each `FUNCTION_BLOCK`_.
582584
619621 case of phi instructions. For phi instructions, operands are encoded as
620622 `Signed VBRs`_ to deal with forward references.
621623
624 In version 2, the meaning of module records ``FUNCTION``, ``GLOBALVAR``,
625 ``ALIAS``, ``IFUNC`` and ``COMDAT`` change such that the first two operands
626 specify an offset and size of a string in a string table (see `STRTAB_BLOCK
627 Contents`_), the function name is removed from the ``FNENTRY`` record in the
628 value symbol table, and the top-level ``VALUE_SYMTAB_BLOCK`` may only contain
629 ``FNENTRY`` records.
622630
623631 MODULE_CODE_TRIPLE Record
624632 ^^^^^^^^^^^^^^^^^^^^^^^^^
672680 MODULE_CODE_GLOBALVAR Record
673681 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
674682
675 ``[GLOBALVAR, pointer type, isconst, initid, linkage, alignment, section, visibility, threadlocal, unnamed_addr, externally_initialized, dllstorageclass, comdat]``
683 ``[GLOBALVAR, strtab offset, strtab size, pointer type, isconst, initid, linkage, alignment, section, visibility, threadlocal, unnamed_addr, externally_initialized, dllstorageclass, comdat]``
676684
677685 The ``GLOBALVAR`` record (code 7) marks the declaration or definition of a
678686 global variable. The operand fields are:
687
688 * *strtab offset*, *strtab size*: Specifies the name of the global variable.
689 See `STRTAB_BLOCK Contents`_.
679690
680691 * *pointer type*: The type index of the pointer type used to point to this
681692 global variable
754765 MODULE_CODE_FUNCTION Record
755766 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
756767
757 ``[FUNCTION, type, callingconv, isproto, linkage, paramattr, alignment, section, visibility, gc, prologuedata, dllstorageclass, comdat, prefixdata, personalityfn]``
768 ``[FUNCTION, strtab offset, strtab size, type, callingconv, isproto, linkage, paramattr, alignment, section, visibility, gc, prologuedata, dllstorageclass, comdat, prefixdata, personalityfn]``
758769
759770 The ``FUNCTION`` record (code 8) marks the declaration or definition of a
760771 function. The operand fields are:
772
773 * *strtab offset*, *strtab size*: Specifies the name of the function.
774 See `STRTAB_BLOCK Contents`_.
761775
762776 * *type*: The type index of the function type describing this function
763777
816830 MODULE_CODE_ALIAS Record
817831 ^^^^^^^^^^^^^^^^^^^^^^^^
818832
819 ``[ALIAS, alias type, aliasee val#, linkage, visibility, dllstorageclass, threadlocal, unnamed_addr]``
833 ``[ALIAS, strtab offset, strtab size, alias type, aliasee val#, linkage, visibility, dllstorageclass, threadlocal, unnamed_addr]``
820834
821835 The ``ALIAS`` record (code 9) marks the definition of an alias. The operand
822836 fields are
837
838 * *strtab offset*, *strtab size*: Specifies the name of the alias.
839 See `STRTAB_BLOCK Contents`_.
823840
824841 * *alias type*: The type index of the alias
825842
12991316 ----------------------------
13001317
13011318 The ``METADATA_ATTACHMENT`` block (id 16) ...
1319
1320 .. _STRTAB_BLOCK:
1321
1322 STRTAB_BLOCK Contents
1323 ---------------------
1324
1325 The ``STRTAB`` block (id 23) contains a single record (``STRTAB_BLOB``, id 1)
1326 with a single blob operand containing the bitcode file's string table.
1327
1328 Strings in the string table are not null terminated. A record's *strtab
1329 offset* and *strtab size* operands specify the byte offset and size of a
1330 string within the string table.
1331
1332 The string table is used by all preceding blocks in the bitcode file that are
1333 not succeeded by another intervening ``STRTAB`` block. Normally a bitcode
1334 file will have a single string table, but it may have more than one if it
1335 was created by binary concatenation of multiple bitcode files.
4545 ArrayRef Buffer;
4646 StringRef ModuleIdentifier;
4747
48 // The string table used to interpret this module.
49 StringRef Strtab;
50
4851 // The bitstream location of the IDENTIFICATION_BLOCK.
4952 uint64_t IdentificationBit;
5053
6972 StringRef getBuffer() const {
7073 return StringRef((const char *)Buffer.begin(), Buffer.size());
7174 }
75 StringRef getStrtab() const { return Strtab; }
7276
7377 StringRef getModuleIdentifier() const { return ModuleIdentifier; }
7478
1414 #define LLVM_BITCODE_BITCODEWRITER_H
1515
1616 #include "llvm/IR/ModuleSummaryIndex.h"
17 #include "llvm/MC/StringTableBuilder.h"
1718 #include
1819
1920 namespace llvm {
2526 SmallVectorImpl &Buffer;
2627 std::unique_ptr Stream;
2728
29 StringTableBuilder StrtabBuilder{StringTableBuilder::RAW};
30 bool WroteStrtab = false;
31
32 void writeBlob(unsigned Block, unsigned Record, StringRef Blob);
33
2834 public:
2935 /// Create a BitcodeWriter that writes to Buffer.
3036 BitcodeWriter(SmallVectorImpl &Buffer);
3137
3238 ~BitcodeWriter();
39
40 /// Write the bitcode file's string table. This must be called exactly once
41 /// after all modules have been written.
42 void writeStrtab();
43
44 /// Copy the string table for another module into this bitcode file. This
45 /// should be called after copying the module itself into the bitcode file.
46 void copyStrtab(StringRef Strtab);
3347
3448 /// Write the specified module to the buffer specified at construction time.
3549 ///
2121
2222 namespace llvm {
2323 namespace bitc {
24 // The only top-level block type defined is for a module.
24 // The only top-level block types are MODULE, IDENTIFICATION and STRTAB.
2525 enum BlockIDs {
2626 // Blocks
2727 MODULE_BLOCK_ID = FIRST_APPLICATION_BLOCKID,
5151
5252 OPERAND_BUNDLE_TAGS_BLOCK_ID,
5353
54 METADATA_KIND_BLOCK_ID
54 METADATA_KIND_BLOCK_ID,
55
56 STRTAB_BLOCK_ID,
5557 };
5658
5759 /// Identification block contains a string that describes the producer details,
231233 // llvm.type.checked.load intrinsic with all constant integer arguments.
232234 // [typeid, offset, n x arg]
233235 FS_TYPE_CHECKED_LOAD_CONST_VCALL = 15,
236 // Assigns a GUID to a value ID. This normally appears only in combined
237 // summaries, but it can also appear in per-module summaries for PGO data.
238 // [valueid, guid]
239 FS_VALUE_GUID = 16,
234240 };
235241
236242 enum MetadataCodes {
549555 COMDAT_SELECTION_KIND_SAME_SIZE = 5,
550556 };
551557
558 enum StrtabCodes {
559 STRTAB_BLOB = 1,
560 };
561
552562 } // End bitc namespace
553563 } // End llvm namespace
554564
371371
372372 class BitcodeReaderBase {
373373 protected:
374 BitcodeReaderBase(BitstreamCursor Stream) : Stream(std::move(Stream)) {
374 BitcodeReaderBase(BitstreamCursor Stream, StringRef Strtab)
375 : Stream(std::move(Stream)), Strtab(Strtab) {
375376 this->Stream.setBlockInfo(&BlockInfo);
376377 }
377378
378379 BitstreamBlockInfo BlockInfo;
379380 BitstreamCursor Stream;
381 StringRef Strtab;
382
383 /// In version 2 of the bitcode we store names of global values and comdats in
384 /// a string table rather than in the VST.
385 bool UseStrtab = false;
380386
381387 Expected parseVersionRecord(ArrayRef Record);
388
389 /// If this module uses a string table, pop the reference to the string table
390 /// and return the referenced string and the rest of the record. Otherwise
391 /// just return the record itself.
392 std::pair>
393 readNameFromStrtab(ArrayRef Record);
382394
383395 bool readBlockInfo();
384396
401413 if (Record.size() < 1)
402414 return error("Invalid record");
403415 unsigned ModuleVersion = Record[0];
404 if (ModuleVersion > 1)
416 if (ModuleVersion > 2)
405417 return error("Invalid value");
418 UseStrtab = ModuleVersion >= 2;
406419 return ModuleVersion;
420 }
421
422 std::pair>
423 BitcodeReaderBase::readNameFromStrtab(ArrayRef Record) {
424 if (!UseStrtab)
425 return {"", Record};
426 // Invalid reference. Let the caller complain about the record being empty.
427 if (Record[0] + Record[1] > Strtab.size())
428 return {"", {}};
429 return {StringRef(Strtab.data() + Record[0], Record[1]), Record.slice(2)};
407430 }
408431
409432 class BitcodeReader : public BitcodeReaderBase, public GVMaterializer {
491514 std::vector BundleTags;
492515
493516 public:
494 BitcodeReader(BitstreamCursor Stream, StringRef ProducerIdentification,
495 LLVMContext &Context);
517 BitcodeReader(BitstreamCursor Stream, StringRef Strtab,
518 StringRef ProducerIdentification, LLVMContext &Context);
496519
497520 Error materializeForwardReferencedFunctions();
498521
627650
628651 Expected recordValue(SmallVectorImpl &Record,
629652 unsigned NameIndex, Triple &TT);
653 void setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta, Function *F,
654 ArrayRef Record);
630655 Error parseValueSymbolTable(uint64_t Offset = 0);
656 Error parseGlobalValueSymbolTable();
631657 Error parseConstants();
632658 Error rememberAndSkipFunctionBodies();
633659 Error rememberAndSkipFunctionBody();
680706 std::string SourceFileName;
681707
682708 public:
683 ModuleSummaryIndexBitcodeReader(
684 BitstreamCursor Stream, ModuleSummaryIndex &TheIndex);
709 ModuleSummaryIndexBitcodeReader(BitstreamCursor Stream, StringRef Strtab,
710 ModuleSummaryIndex &TheIndex);
685711
686712 Error parseModule(StringRef ModulePath);
687713
688714 private:
715 void setValueGUID(uint64_t ValueID, StringRef ValueName,
716 GlobalValue::LinkageTypes Linkage,
717 StringRef SourceFileName);
689718 Error parseValueSymbolTable(
690719 uint64_t Offset,
691720 DenseMap &ValueIdToLinkageMap);
715744 return std::error_code();
716745 }
717746
718 BitcodeReader::BitcodeReader(BitstreamCursor Stream,
747 BitcodeReader::BitcodeReader(BitstreamCursor Stream, StringRef Strtab,
719748 StringRef ProducerIdentification,
720749 LLVMContext &Context)
721 : BitcodeReaderBase(std::move(Stream)), Context(Context),
750 : BitcodeReaderBase(std::move(Stream), Strtab), Context(Context),
722751 ValueList(Context) {
723752 this->ProducerIdentification = ProducerIdentification;
724753 }
17481777 return CurrentBit;
17491778 }
17501779
1780 void BitcodeReader::setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta,
1781 Function *F,
1782 ArrayRef Record) {
1783 // Note that we subtract 1 here because the offset is relative to one word
1784 // before the start of the identification or module block, which was
1785 // historically always the start of the regular bitcode header.
1786 uint64_t FuncWordOffset = Record[1] - 1;
1787 uint64_t FuncBitOffset = FuncWordOffset * 32;
1788 DeferredFunctionInfo[F] = FuncBitOffset + FuncBitcodeOffsetDelta;
1789 // Set the LastFunctionBlockBit to point to the last function block.
1790 // Later when parsing is resumed after function materialization,
1791 // we can simply skip that last function block.
1792 if (FuncBitOffset > LastFunctionBlockBit)
1793 LastFunctionBlockBit = FuncBitOffset;
1794 }
1795
1796 /// Read a new-style GlobalValue symbol table.
1797 Error BitcodeReader::parseGlobalValueSymbolTable() {
1798 unsigned FuncBitcodeOffsetDelta =
1799 Stream.getAbbrevIDWidth() + bitc::BlockIDWidth;
1800
1801 if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
1802 return error("Invalid record");
1803
1804 SmallVector Record;
1805 while (true) {
1806 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1807
1808 switch (Entry.Kind) {
1809 case BitstreamEntry::SubBlock:
1810 case BitstreamEntry::Error:
1811 return error("Malformed block");
1812 case BitstreamEntry::EndBlock:
1813 return Error::success();
1814 case BitstreamEntry::Record:
1815 break;
1816 }
1817
1818 Record.clear();
1819 switch (Stream.readRecord(Entry.ID, Record)) {
1820 case bitc::VST_CODE_FNENTRY: // [valueid, offset]
1821 setDeferredFunctionInfo(FuncBitcodeOffsetDelta,
1822 cast(ValueList[Record[0]]), Record);
1823 break;
1824 }
1825 }
1826 }
1827
17511828 /// Parse the value symbol table at either the current parsing location or
17521829 /// at the given bit offset if provided.
17531830 Error BitcodeReader::parseValueSymbolTable(uint64_t Offset) {
17551832 // Pass in the Offset to distinguish between calling for the module-level
17561833 // VST (where we want to jump to the VST offset) and the function-level
17571834 // VST (where we don't).
1758 if (Offset > 0)
1835 if (Offset > 0) {
17591836 CurrentBit = jumpToValueSymbolTable(Offset, Stream);
1837 // If this module uses a string table, read this as a module-level VST.
1838 if (UseStrtab) {
1839 if (Error Err = parseGlobalValueSymbolTable())
1840 return Err;
1841 Stream.JumpToBit(CurrentBit);
1842 return Error::success();
1843 }
1844 // Otherwise, the VST will be in a similar format to a function-level VST,
1845 // and will contain symbol names.
1846 }
17601847
17611848 // Compute the delta between the bitcode indices in the VST (the word offset
17621849 // to the word-aligned ENTER_SUBBLOCK for the function block, and that
18171904 return Err;
18181905 Value *V = ValOrErr.get();
18191906
1820 auto *F = dyn_cast(V);
18211907 // Ignore function offsets emitted for aliases of functions in older
18221908 // versions of LLVM.
1823 if (!F)
1824 break;
1825
1826 // Note that we subtract 1 here because the offset is relative to one word
1827 // before the start of the identification or module block, which was
1828 // historically always the start of the regular bitcode header.
1829 uint64_t FuncWordOffset = Record[1] - 1;
1830 uint64_t FuncBitOffset = FuncWordOffset * 32;
1831 DeferredFunctionInfo[F] = FuncBitOffset + FuncBitcodeOffsetDelta;
1832 // Set the LastFunctionBlockBit to point to the last function block.
1833 // Later when parsing is resumed after function materialization,
1834 // we can simply skip that last function block.
1835 if (FuncBitOffset > LastFunctionBlockBit)
1836 LastFunctionBlockBit = FuncBitOffset;
1909 if (auto *F = dyn_cast(V))
1910 setDeferredFunctionInfo(FuncBitcodeOffsetDelta, F, Record);
18371911 break;
18381912 }
18391913 case bitc::VST_CODE_BBENTRY: {
26252699 }
26262700
26272701 Error BitcodeReader::parseComdatRecord(ArrayRef Record) {
2628 // [selection_kind, name]
2629 if (Record.size() < 2)
2702 // v1: [selection_kind, name]
2703 // v2: [strtab_offset, strtab_size, selection_kind]
2704 StringRef Name;
2705 std::tie(Name, Record) = readNameFromStrtab(Record);
2706
2707 if (Record.size() < 1)
26302708 return error("Invalid record");
26312709 Comdat::SelectionKind SK = getDecodedComdatSelectionKind(Record[0]);
2632 std::string Name;
2633 unsigned ComdatNameSize = Record[1];
2634 Name.reserve(ComdatNameSize);
2635 for (unsigned i = 0; i != ComdatNameSize; ++i)
2636 Name += (char)Record[2 + i];
2710 std::string OldFormatName;
2711 if (!UseStrtab) {
2712 if (Record.size() < 2)
2713 return error("Invalid record");
2714 unsigned ComdatNameSize = Record[1];
2715 OldFormatName.reserve(ComdatNameSize);
2716 for (unsigned i = 0; i != ComdatNameSize; ++i)
2717 OldFormatName += (char)Record[2 + i];
2718 Name = OldFormatName;
2719 }
26372720 Comdat *C = TheModule->getOrInsertComdat(Name);
26382721 C->setSelectionKind(SK);
26392722 ComdatList.push_back(C);
26412724 }
26422725
26432726 Error BitcodeReader::parseGlobalVarRecord(ArrayRef Record) {
2644 // [pointer type, isconst, initid, linkage, alignment, section,
2727 // v1: [pointer type, isconst, initid, linkage, alignment, section,
26452728 // visibility, threadlocal, unnamed_addr, externally_initialized,
2646 // dllstorageclass, comdat]
2729 // dllstorageclass, comdat] (name in VST)
2730 // v2: [strtab_offset, strtab_size, v1]
2731 StringRef Name;
2732 std::tie(Name, Record) = readNameFromStrtab(Record);
2733
26472734 if (Record.size() < 6)
26482735 return error("Invalid record");
26492736 Type *Ty = getTypeByID(Record[0]);
26912778 ExternallyInitialized = Record[9];
26922779
26932780 GlobalVariable *NewGV =
2694 new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, "",
2781 new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, Name,
26952782 nullptr, TLM, AddressSpace, ExternallyInitialized);
26962783 NewGV->setAlignment(Alignment);
26972784 if (!Section.empty())
27232810 }
27242811
27252812 Error BitcodeReader::parseFunctionRecord(ArrayRef Record) {
2726 // [type, callingconv, isproto, linkage, paramattr, alignment, section,
2813 // v1: [type, callingconv, isproto, linkage, paramattr, alignment, section,
27272814 // visibility, gc, unnamed_addr, prologuedata, dllstorageclass, comdat,
2728 // prefixdata]
2815 // prefixdata] (name in VST)
2816 // v2: [strtab_offset, strtab_size, v1]
2817 StringRef Name;
2818 std::tie(Name, Record) = readNameFromStrtab(Record);
2819
27292820 if (Record.size() < 8)
27302821 return error("Invalid record");
27312822 Type *Ty = getTypeByID(Record[0]);
27412832 return error("Invalid calling convention ID");
27422833
27432834 Function *Func =
2744 Function::Create(FTy, GlobalValue::ExternalLinkage, "", TheModule);
2835 Function::Create(FTy, GlobalValue::ExternalLinkage, Name, TheModule);
27452836
27462837 Func->setCallingConv(CC);
27472838 bool isProto = Record[2];
28092900
28102901 Error BitcodeReader::parseGlobalIndirectSymbolRecord(
28112902 unsigned BitCode, ArrayRef Record) {
2812 // ALIAS_OLD: [alias type, aliasee val#, linkage]
2813 // ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility,
2814 // dllstorageclass]
2815 // IFUNC: [alias type, addrspace, aliasee val#, linkage,
2816 // visibility, dllstorageclass]
2903 // v1 ALIAS_OLD: [alias type, aliasee val#, linkage] (name in VST)
2904 // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility,
2905 // dllstorageclass] (name in VST)
2906 // v1 IFUNC: [alias type, addrspace, aliasee val#, linkage,
2907 // visibility, dllstorageclass] (name in VST)
2908 // v2: [strtab_offset, strtab_size, v1]
2909 StringRef Name;
2910 std::tie(Name, Record) = readNameFromStrtab(Record);
2911
28172912 bool NewRecord = BitCode != bitc::MODULE_CODE_ALIAS_OLD;
28182913 if (Record.size() < (3 + (unsigned)NewRecord))
28192914 return error("Invalid record");
28382933 GlobalIndirectSymbol *NewGA;
28392934 if (BitCode == bitc::MODULE_CODE_ALIAS ||
28402935 BitCode == bitc::MODULE_CODE_ALIAS_OLD)
2841 NewGA = GlobalAlias::create(Ty, AddrSpace, getDecodedLinkage(Linkage), "",
2936 NewGA = GlobalAlias::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name,
28422937 TheModule);
28432938 else
2844 NewGA = GlobalIFunc::create(Ty, AddrSpace, getDecodedLinkage(Linkage), "",
2939 NewGA = GlobalIFunc::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name,
28452940 nullptr, TheModule);
28462941 // Old bitcode files didn't have visibility field.
28472942 // Local linkage must have default visibility.
45694664 }
45704665
45714666 ModuleSummaryIndexBitcodeReader::ModuleSummaryIndexBitcodeReader(
4572 BitstreamCursor Cursor, ModuleSummaryIndex &TheIndex)
4573 : BitcodeReaderBase(std::move(Cursor)), TheIndex(TheIndex) {}
4667 BitstreamCursor Cursor, StringRef Strtab, ModuleSummaryIndex &TheIndex)
4668 : BitcodeReaderBase(std::move(Cursor), Strtab), TheIndex(TheIndex) {}
45744669
45754670 std::pair
45764671 ModuleSummaryIndexBitcodeReader::getGUIDFromValueId(unsigned ValueId) {
45774672 auto VGI = ValueIdToCallGraphGUIDMap.find(ValueId);
45784673 assert(VGI != ValueIdToCallGraphGUIDMap.end());
45794674 return VGI->second;
4675 }
4676
4677 void ModuleSummaryIndexBitcodeReader::setValueGUID(
4678 uint64_t ValueID, StringRef ValueName, GlobalValue::LinkageTypes Linkage,
4679 StringRef SourceFileName) {
4680 std::string GlobalId =
4681 GlobalValue::getGlobalIdentifier(ValueName, Linkage, SourceFileName);
4682 auto ValueGUID = GlobalValue::getGUID(GlobalId);
4683 auto OriginalNameID = ValueGUID;
4684 if (GlobalValue::isLocalLinkage(Linkage))
4685 OriginalNameID = GlobalValue::getGUID(ValueName);
4686 if (PrintSummaryGUIDs)
4687 dbgs() << "GUID " << ValueGUID << "(" << OriginalNameID << ") is "
4688 << ValueName << "\n";
4689 ValueIdToCallGraphGUIDMap[ValueID] =
4690 std::make_pair(ValueGUID, OriginalNameID);
45804691 }
45814692
45824693 // Specialized value symbol table parser used when reading module index
45854696 Error ModuleSummaryIndexBitcodeReader::parseValueSymbolTable(
45864697 uint64_t Offset,
45874698 DenseMap &ValueIdToLinkageMap) {
4699 // With a strtab the VST is not required to parse the summary.
4700 if (UseStrtab)
4701 return Error::success();
4702
45884703 assert(Offset > 0 && "Expected non-zero VST offset");
45894704 uint64_t CurrentBit = jumpToValueSymbolTable(Offset, Stream);
45904705
46264741 assert(VLI != ValueIdToLinkageMap.end() &&
46274742 "No linkage found for VST entry?");
46284743 auto Linkage = VLI->second;
4629 std::string GlobalId =
4630 GlobalValue::getGlobalIdentifier(ValueName, Linkage, SourceFileName);
4631 auto ValueGUID = GlobalValue::getGUID(GlobalId);
4632 auto OriginalNameID = ValueGUID;
4633 if (GlobalValue::isLocalLinkage(Linkage))
4634 OriginalNameID = GlobalValue::getGUID(ValueName);
4635 if (PrintSummaryGUIDs)
4636 dbgs() << "GUID " << ValueGUID << "(" << OriginalNameID << ") is "
4637 << ValueName << "\n";
4638 ValueIdToCallGraphGUIDMap[ValueID] =
4639 std::make_pair(ValueGUID, OriginalNameID);
4744 setValueGUID(ValueID, ValueName, Linkage, SourceFileName);
46404745 ValueName.clear();
46414746 break;
46424747 }
46504755 assert(VLI != ValueIdToLinkageMap.end() &&
46514756 "No linkage found for VST entry?");
46524757 auto Linkage = VLI->second;
4653 std::string FunctionGlobalId = GlobalValue::getGlobalIdentifier(
4654 ValueName, VLI->second, SourceFileName);
4655 auto FunctionGUID = GlobalValue::getGUID(FunctionGlobalId);
4656 auto OriginalNameID = FunctionGUID;
4657 if (GlobalValue::isLocalLinkage(Linkage))
4658 OriginalNameID = GlobalValue::getGUID(ValueName);
4659 if (PrintSummaryGUIDs)
4660 dbgs() << "GUID " << FunctionGUID << "(" << OriginalNameID << ") is "
4661 << ValueName << "\n";
4662 ValueIdToCallGraphGUIDMap[ValueID] =
4663 std::make_pair(FunctionGUID, OriginalNameID);
4664
4758 setValueGUID(ValueID, ValueName, Linkage, SourceFileName);
46654759 ValueName.clear();
46664760 break;
46674761 }
47484842 switch (BitCode) {
47494843 default:
47504844 break; // Default behavior, ignore unknown content.
4845 case bitc::MODULE_CODE_VERSION: {
4846 if (Error Err = parseVersionRecord(Record).takeError())
4847 return Err;
4848 break;
4849 }
47514850 /// MODULE_CODE_SOURCE_FILENAME: [namechar x N]
47524851 case bitc::MODULE_CODE_SOURCE_FILENAME: {
47534852 SmallString<128> ValueName;
47824881 // was historically always the start of the regular bitcode header.
47834882 VSTOffset = Record[0] - 1;
47844883 break;
4785 // GLOBALVAR: [pointer type, isconst, initid, linkage, ...]
4786 // FUNCTION: [type, callingconv, isproto, linkage, ...]
4787 // ALIAS: [alias type, addrspace, aliasee val#, linkage, ...]
4884 // v1 GLOBALVAR: [pointer type, isconst, initid, linkage, ...]
4885 // v1 FUNCTION: [type, callingconv, isproto, linkage, ...]
4886 // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, ...]
4887 // v2: [strtab offset, strtab size, v1]
47884888 case bitc::MODULE_CODE_GLOBALVAR:
47894889 case bitc::MODULE_CODE_FUNCTION:
47904890 case bitc::MODULE_CODE_ALIAS: {
4791 if (Record.size() <= 3)
4891 StringRef Name;
4892 ArrayRef GVRecord;
4893 std::tie(Name, GVRecord) = readNameFromStrtab(Record);
4894 if (GVRecord.size() <= 3)
47924895 return error("Invalid record");
4793 uint64_t RawLinkage = Record[3];
4896 uint64_t RawLinkage = GVRecord[3];
47944897 GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage);
4795 ValueIdToLinkageMap[ValueId++] = Linkage;
4898 if (!UseStrtab) {
4899 ValueIdToLinkageMap[ValueId++] = Linkage;
4900 break;
4901 }
4902
4903 setValueGUID(ValueId++, Name, Linkage, SourceFileName);
47964904 break;
47974905 }
47984906 }
49035011 switch (BitCode) {
49045012 default: // Default behavior: ignore.
49055013 break;
5014 case bitc::FS_VALUE_GUID: { // [valueid, refguid]
5015 uint64_t ValueID = Record[0];
5016 GlobalValue::GUID RefGUID = Record[1];
5017 ValueIdToCallGraphGUIDMap[ValueID] = std::make_pair(RefGUID, RefGUID);
5018 break;
5019 }
49065020 // FS_PERMODULE: [valueid, flags, instcount, numrefs, numrefs x valueid,
49075021 // n x (valueid)]
49085022 // FS_PERMODULE_PROFILE: [valueid, flags, instcount, numrefs,
52075321 return *ErrorCategory;
52085322 }
52095323
5324 static Expected readStrtab(BitstreamCursor &Stream) {
5325 if (Stream.EnterSubBlock(bitc::STRTAB_BLOCK_ID))
5326 return error("Invalid record");
5327
5328 StringRef Strtab;
5329 while (1) {
5330 BitstreamEntry Entry = Stream.advance();
5331 switch (Entry.Kind) {
5332 case BitstreamEntry::EndBlock:
5333 return Strtab;
5334
5335 case BitstreamEntry::Error:
5336 return error("Malformed block");
5337
5338 case BitstreamEntry::SubBlock:
5339 if (Stream.SkipBlock())
5340 return error("Malformed block");
5341 break;
5342
5343 case BitstreamEntry::Record:
5344 StringRef Blob;
5345 SmallVector Record;
5346 if (Stream.readRecord(Entry.ID, Record, &Blob) == bitc::STRTAB_BLOB)
5347 Strtab = Blob;
5348 break;
5349 }
5350 }
5351 }
5352
52105353 //===----------------------------------------------------------------------===//
52115354 // External interface
52125355 //===----------------------------------------------------------------------===//
52565399 BCBegin, Stream.getCurrentByteNo() - BCBegin),
52575400 Buffer.getBufferIdentifier(), IdentificationBit,
52585401 ModuleBit});
5402 continue;
5403 }
5404
5405 if (Entry.ID == bitc::STRTAB_BLOCK_ID) {
5406 Expected Strtab = readStrtab(Stream);
5407 if (!Strtab)
5408 return Strtab.takeError();
5409 // This string table is used by every preceding bitcode module that does
5410 // not have its own string table. A bitcode file may have multiple
5411 // string tables if it was created by binary concatenation, for example
5412 // with "llvm-cat -b".
5413 for (auto I = Modules.rbegin(), E = Modules.rend(); I != E; ++I) {
5414 if (!I->Strtab.empty())
5415 break;
5416 I->Strtab = *Strtab;
5417 }
52595418 continue;
52605419 }
52615420
52955454 }
52965455
52975456 Stream.JumpToBit(ModuleBit);
5298 auto *R =
5299 new BitcodeReader(std::move(Stream), ProducerIdentification, Context);
5457 auto *R = new BitcodeReader(std::move(Stream), Strtab, ProducerIdentification,
5458 Context);
53005459
53015460 std::unique_ptr M =
53025461 llvm::make_unique(ModuleIdentifier, Context);
53315490 Stream.JumpToBit(ModuleBit);
53325491
53335492 auto Index = llvm::make_unique();
5334 ModuleSummaryIndexBitcodeReader R(std::move(Stream), *Index);
5493 ModuleSummaryIndexBitcodeReader R(std::move(Stream), Strtab, *Index);
53355494
53365495 if (Error Err = R.parseModule(ModuleIdentifier))
53375496 return std::move(Err);
2727 #include "llvm/IR/Operator.h"
2828 #include "llvm/IR/UseListOrder.h"
2929 #include "llvm/IR/ValueSymbolTable.h"
30 #include "llvm/MC/StringTableBuilder.h"
3031 #include "llvm/Support/ErrorHandling.h"
3132 #include "llvm/Support/MathExtras.h"
3233 #include "llvm/Support/Program.h"
7576 /// The stream created and owned by the client.
7677 BitstreamWriter &Stream;
7778
78 /// Saves the offset of the VSTOffset record that must eventually be
79 /// backpatched with the offset of the actual VST.
80 uint64_t VSTOffsetPlaceholder = 0;
81
8279 public:
8380 /// Constructs a BitcodeWriterBase object that writes to the provided
8481 /// \p Stream.
8582 BitcodeWriterBase(BitstreamWriter &Stream) : Stream(Stream) {}
8683
8784 protected:
88 bool hasVSTOffsetPlaceholder() { return VSTOffsetPlaceholder != 0; }
89 void writeValueSymbolTableForwardDecl();
9085 void writeBitcodeHeader();
86 void writeModuleVersion();
9187 };
88
89 void BitcodeWriterBase::writeModuleVersion() {
90 // VERSION: [version#]
91 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, ArrayRef{2});
92 }
9293
9394 /// Class to manage the bitcode writing for a module.
9495 class ModuleBitcodeWriter : public BitcodeWriterBase {
9596 /// Pointer to the buffer allocated by caller for bitcode writing.
9697 const SmallVectorImpl &Buffer;
98
99 StringTableBuilder &StrtabBuilder;
97100
98101 /// The Module to write to bitcode.
99102 const Module &M;
126129 /// Tracks the last value id recorded in the GUIDToValueMap.
127130 unsigned GlobalValueId;
128131
132 /// Saves the offset of the VSTOffset record that must eventually be
133 /// backpatched with the offset of the actual VST.
134 uint64_t VSTOffsetPlaceholder = 0;
135
129136 public:
130137 /// Constructs a ModuleBitcodeWriter object for the given Module,
131138 /// writing to the provided \p Buffer.
132139 ModuleBitcodeWriter(const Module *M, SmallVectorImpl &Buffer,
140 StringTableBuilder &StrtabBuilder,
133141 BitstreamWriter &Stream, bool ShouldPreserveUseListOrder,
134142 const ModuleSummaryIndex *Index, bool GenerateHash,
135143 ModuleHash *ModHash = nullptr)
136 : BitcodeWriterBase(Stream), Buffer(Buffer), M(*M),
137 VE(*M, ShouldPreserveUseListOrder), Index(Index),
144 : BitcodeWriterBase(Stream), Buffer(Buffer), StrtabBuilder(StrtabBuilder),
145 M(*M), VE(*M, ShouldPreserveUseListOrder), Index(Index),
138146 GenerateHash(GenerateHash), ModHash(ModHash),
139147 BitcodeStartBit(Stream.GetCurrentBitNo()) {
140148 // Assign ValueIds to any callee values in the index that came from
168176 void writeAttributeTable();
169177 void writeTypeTable();
170178 void writeComdats();
179 void writeValueSymbolTableForwardDecl();
171180 void writeModuleInfo();
172181 void writeValueAsMetadata(const ValueAsMetadata *MD,
173182 SmallVectorImpl &Record);
260269 SmallVectorImpl &Vals);
261270 void writeInstruction(const Instruction &I, unsigned InstID,
262271 SmallVectorImpl &Vals);
263 void writeValueSymbolTable(
264 const ValueSymbolTable &VST, bool IsModuleLevel = false,
265 DenseMap *FunctionToBitcodeIndex = nullptr);
272 void writeFunctionLevelValueSymbolTable(const ValueSymbolTable &VST);
273 void writeGlobalValueSymbolTable(
274 DenseMap &FunctionToBitcodeIndex);
266275 void writeUseList(UseListOrder &&Order);
267276 void writeUseListBlock(const Function *F);
268277 void
476485
477486 private:
478487 void writeModStrings();
479 void writeCombinedValueSymbolTable();
480488 void writeCombinedGlobalValueSummary();
481489
482490 /// Indicates whether the provided \p ModulePath should be written into
491499 const auto &VMI = GUIDToValueIdMap.find(ValGUID);
492500 return VMI != GUIDToValueIdMap.end();
493501 }
502 void assignValueId(GlobalValue::GUID ValGUID) {
503 unsigned &ValueId = GUIDToValueIdMap[ValGUID];
504 if (ValueId == 0)
505 ValueId = ++GlobalValueId;
506 }
494507 unsigned getValueId(GlobalValue::GUID ValGUID) {
495 const auto &VMI = GUIDToValueIdMap.find(ValGUID);
496 // If this GUID doesn't have an entry, assign one.
497 if (VMI == GUIDToValueIdMap.end()) {
498 GUIDToValueIdMap[ValGUID] = ++GlobalValueId;
499 return GlobalValueId;
500 } else {
501 return VMI->second;
502 }
508 auto VMI = GUIDToValueIdMap.find(ValGUID);
509 assert(VMI != GUIDToValueIdMap.end());
510 return VMI->second;
503511 }
504512 std::map &valueIds() { return GUIDToValueIdMap; }
505513 };
10461054 void ModuleBitcodeWriter::writeComdats() {
10471055 SmallVector Vals;
10481056 for (const Comdat *C : VE.getComdats()) {
1049 // COMDAT: [selection_kind, name]
1057 // COMDAT: [strtab offset, strtab size, selection_kind]
1058 Vals.push_back(StrtabBuilder.add(C->getName()));
1059 Vals.push_back(C->getName().size());
10501060 Vals.push_back(getEncodedComdatSelectionKind(*C));
1051 size_t Size = C->getName().size();
1052 assert(isUInt<32>(Size));
1053 Vals.push_back(Size);
1054 for (char Chr : C->getName())
1055 Vals.push_back((unsigned char)Chr);
10561061 Stream.EmitRecord(bitc::MODULE_CODE_COMDAT, Vals, /*AbbrevToUse=*/0);
10571062 Vals.clear();
10581063 }
10611066 /// Write a record that will eventually hold the word offset of the
10621067 /// module-level VST. For now the offset is 0, which will be backpatched
10631068 /// after the real VST is written. Saves the bit offset to backpatch.
1064 void BitcodeWriterBase::writeValueSymbolTableForwardDecl() {
1069 void ModuleBitcodeWriter::writeValueSymbolTableForwardDecl() {
10651070 // Write a placeholder value in for the offset of the real VST,
10661071 // which is written after the function blocks so that it can include
10671072 // the offset of each function. The placeholder offset will be
11641169 // Add an abbrev for common globals with no visibility or thread localness.
11651170 auto Abbv = std::make_shared();
11661171 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
1172 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1173 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
11671174 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
11681175 Log2_32_Ceil(MaxGlobalType+1)));
11691176 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // AddrSpace << 2
11871194 SimpleGVarAbbrev = Stream.EmitAbbrev(std::move(Abbv));
11881195 }
11891196
1197 SmallVector Vals;
1198 // Emit the module's source file name.
1199 {
1200 StringEncoding Bits = getStringEncoding(M.getSourceFileName().data(),
1201 M.getSourceFileName().size());
1202 BitCodeAbbrevOp AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8);
1203 if (Bits == SE_Char6)
1204 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Char6);
1205 else if (Bits == SE_Fixed7)
1206 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7);
1207
1208 // MODULE_CODE_SOURCE_FILENAME: [namechar x N]
1209 auto Abbv = std::make_shared();
1210 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_SOURCE_FILENAME));
1211 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1212 Abbv->Add(AbbrevOpToUse);
1213 unsigned FilenameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1214
1215 for (const auto P : M.getSourceFileName())
1216 Vals.push_back((unsigned char)P);
1217
1218 // Emit the finished record.
1219 Stream.EmitRecord(bitc::MODULE_CODE_SOURCE_FILENAME, Vals, FilenameAbbrev);
1220 Vals.clear();
1221 }
1222
11901223 // Emit the global variable information.
1191 SmallVector Vals;
11921224 for (const GlobalVariable &GV : M.globals()) {
11931225 unsigned AbbrevToUse = 0;
11941226
1195 // GLOBALVAR: [type, isconst, initid,
1227 // GLOBALVAR: [strtab offset, strtab size, type, isconst, initid,
11961228 // linkage, alignment, section, visibility, threadlocal,
11971229 // unnamed_addr, externally_initialized, dllstorageclass,
11981230 // comdat]
1231 Vals.push_back(StrtabBuilder.add(GV.getName()));
1232 Vals.push_back(GV.getName().size());
11991233 Vals.push_back(VE.getTypeID(GV.getValueType()));
12001234 Vals.push_back(GV.getType()->getAddressSpace() << 2 | 2 | GV.isConstant());
12011235 Vals.push_back(GV.isDeclaration() ? 0 :
12251259
12261260 // Emit the function proto information.
12271261 for (const Function &F : M) {
1228 // FUNCTION: [type, callingconv, isproto, linkage, paramattrs, alignment,
1229 // section, visibility, gc, unnamed_addr, prologuedata,
1230 // dllstorageclass, comdat, prefixdata, personalityfn]
1262 // FUNCTION: [strtab offset, strtab size, type, callingconv, isproto,
1263 // linkage, paramattrs, alignment, section, visibility, gc,
1264 // unnamed_addr, prologuedata, dllstorageclass, comdat,
1265 // prefixdata, personalityfn]
1266 Vals.push_back(StrtabBuilder.add(F.getName()));
1267 Vals.push_back(F.getName().size());
12311268 Vals.push_back(VE.getTypeID(F.getFunctionType()));
12321269 Vals.push_back(F.getCallingConv());
12331270 Vals.push_back(F.isDeclaration());
12541291
12551292 // Emit the alias information.
12561293 for (const GlobalAlias &A : M.aliases()) {
1257 // ALIAS: [alias type, aliasee val#, linkage, visibility, dllstorageclass,
1258 // threadlocal, unnamed_addr]
1294 // ALIAS: [strtab offset, strtab size, alias type, aliasee val#, linkage,
1295 // visibility, dllstorageclass, threadlocal, unnamed_addr]
1296 Vals.push_back(StrtabBuilder.add(A.getName()));
1297 Vals.push_back(A.getName().size());
12591298 Vals.push_back(VE.getTypeID(A.getValueType()));
12601299 Vals.push_back(A.getType()->getAddressSpace());
12611300 Vals.push_back(VE.getValueID(A.getAliasee()));
12711310
12721311 // Emit the ifunc information.
12731312 for (const GlobalIFunc &I : M.ifuncs()) {
1274 // IFUNC: [ifunc type, address space, resolver val#, linkage, visibility]
1313 // IFUNC: [strtab offset, strtab size, ifunc type, address space, resolver
1314 // val#, linkage, visibility]
1315 Vals.push_back(StrtabBuilder.add(I.getName()));
1316 Vals.push_back(I.getName().size());
12751317 Vals.push_back(VE.getTypeID(I.getValueType()));
12761318 Vals.push_back(I.getType()->getAddressSpace());
12771319 Vals.push_back(VE.getValueID(I.getResolver()));
12811323 Vals.clear();
12821324 }
12831325
1284 // Emit the module's source file name.
1285 {
1286 StringEncoding Bits = getStringEncoding(M.getSourceFileName().data(),
1287 M.getSourceFileName().size());
1288 BitCodeAbbrevOp AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8);
1289 if (Bits == SE_Char6)
1290 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Char6);
1291 else if (Bits == SE_Fixed7)
1292 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7);
1293
1294 // MODULE_CODE_SOURCE_FILENAME: [namechar x N]
1295 auto Abbv = std::make_shared();
1296 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_SOURCE_FILENAME));
1297 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1298 Abbv->Add(AbbrevOpToUse);
1299 unsigned FilenameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1300
1301 for (const auto P : M.getSourceFileName())
1302 Vals.push_back((unsigned char)P);
1303
1304 // Emit the finished record.
1305 Stream.EmitRecord(bitc::MODULE_CODE_SOURCE_FILENAME, Vals, FilenameAbbrev);
1306 Vals.clear();
1307 }
1308
1309 // If we have a VST, write the VSTOFFSET record placeholder.
1310 if (M.getValueSymbolTable().empty())
1311 return;
13121326 writeValueSymbolTableForwardDecl();
13131327 }
13141328
28382852 Vals.clear();
28392853 }
28402854
2841 /// Emit names for globals/functions etc. \p IsModuleLevel is true when
2842 /// we are writing the module-level VST, where we are including a function
2843 /// bitcode index and need to backpatch the VST forward declaration record.
2844 void ModuleBitcodeWriter::writeValueSymbolTable(
2845 const ValueSymbolTable &VST, bool IsModuleLevel,
2846 DenseMap *FunctionToBitcodeIndex) {
2847 if (VST.empty()) {
2848 // writeValueSymbolTableForwardDecl should have returned early as
2849 // well. Ensure this handling remains in sync by asserting that
2850 // the placeholder offset is not set.
2851 assert(!IsModuleLevel || !hasVSTOffsetPlaceholder());
2852 return;
2853 }
2854
2855 if (IsModuleLevel && hasVSTOffsetPlaceholder()) {
2856 // Get the offset of the VST we are writing, and backpatch it into
2857 // the VST forward declaration record.
2858 uint64_t VSTOffset = Stream.GetCurrentBitNo();
2859 // The BitcodeStartBit was the stream offset of the identification block.
2860 VSTOffset -= bitcodeStartBit();
2861 assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned");
2855 /// Write a GlobalValue VST to the module. The purpose of this data structure is
2856 /// to allow clients to efficiently find the function body.
2857 void ModuleBitcodeWriter::writeGlobalValueSymbolTable(
2858 DenseMap &FunctionToBitcodeIndex) {
2859 // Get the offset of the VST we are writing, and backpatch it into
2860 // the VST forward declaration record.
2861 uint64_t VSTOffset = Stream.GetCurrentBitNo();
2862 // The BitcodeStartBit was the stream offset of the identification block.
2863 VSTOffset -= bitcodeStartBit();
2864 assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned");
2865 // Note that we add 1 here because the offset is relative to one word
2866 // before the start of the identification block, which was historically
2867 // always the start of the regular bitcode header.
2868 Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32 + 1);
2869
2870 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
2871
2872 auto Abbv = std::make_shared();
2873 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
2874 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2875 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
2876 unsigned FnEntryAbbrev = Stream.EmitAbbrev(std::move(Abbv));
2877
2878 for (const Function &F : M) {
2879 uint64_t Record[2];
2880
2881 if (F.isDeclaration())
2882 continue;
2883
2884 Record[0] = VE.getValueID(&F);
2885
2886 // Save the word offset of the function (from the start of the
2887 // actual bitcode written to the stream).
2888 uint64_t BitcodeIndex = FunctionToBitcodeIndex[&F] - bitcodeStartBit();
2889 assert((BitcodeIndex & 31) == 0 && "function block not 32-bit aligned");
28622890 // Note that we add 1 here because the offset is relative to one word
28632891 // before the start of the identification block, which was historically
28642892 // always the start of the regular bitcode header.
2865 Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32 + 1);
2866 }
2893 Record[1] = BitcodeIndex / 32 + 1;
2894
2895 Stream.EmitRecord(bitc::VST_CODE_FNENTRY, Record, FnEntryAbbrev);
2896 }
2897
2898 Stream.ExitBlock();
2899 }
2900
2901 /// Emit names for arguments, instructions and basic blocks in a function.
2902 void ModuleBitcodeWriter::writeFunctionLevelValueSymbolTable(
2903 const ValueSymbolTable &VST) {
2904 if (VST.empty())
2905 return;
28672906
28682907 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
2869
2870 // For the module-level VST, add abbrev Ids for the VST_CODE_FNENTRY
2871 // records, which are not used in the per-function VSTs.
2872 unsigned FnEntry8BitAbbrev;
2873 unsigned FnEntry7BitAbbrev;
2874 unsigned FnEntry6BitAbbrev;
2875 unsigned GUIDEntryAbbrev;
2876 if (IsModuleLevel && hasVSTOffsetPlaceholder()) {
2877 // 8-bit fixed-width VST_CODE_FNENTRY function strings.
2878 auto Abbv = std::make_shared();
2879 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
2880 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2881 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
2882 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2883 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
2884 FnEntry8BitAbbrev = Stream.EmitAbbrev(std::move(Abbv));
2885
2886 // 7-bit fixed width VST_CODE_FNENTRY function strings.
2887 Abbv = std::make_shared();
2888 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
2889 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2890 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
2891 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2892 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
2893 FnEntry7BitAbbrev = Stream.EmitAbbrev(std::move(Abbv));
2894
2895 // 6-bit char6 VST_CODE_FNENTRY function strings.
2896 Abbv = std::make_shared();
2897 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
2898 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2899 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
2900 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2901 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
2902 FnEntry6BitAbbrev = Stream.EmitAbbrev(std::move(Abbv));
2903
2904 // FIXME: Change the name of this record as it is now used by
2905 // the per-module index as well.
2906 Abbv = std::make_shared();
2907 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_COMBINED_ENTRY));
2908 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
2909 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // refguid
2910 GUIDEntryAbbrev = Stream.EmitAbbrev(std::move(Abbv));
2911 }
29122908
29132909 // FIXME: Set up the abbrev, we know how many values there are!
29142910 // FIXME: We know if the type names can use 7-bit ascii.
29222918 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
29232919 NameVals.push_back(VE.getValueID(Name.getValue()));
29242920
2925 Function *F = dyn_cast(Name.getValue());
2926
29272921 // VST_CODE_ENTRY: [valueid, namechar x N]
2928 // VST_CODE_FNENTRY: [valueid, funcoffset, namechar x N]
29292922 // VST_CODE_BBENTRY: [bbid, namechar x N]
29302923 unsigned Code;
29312924 if (isa(Name.getValue())) {
29322925 Code = bitc::VST_CODE_BBENTRY;
29332926 if (Bits == SE_Char6)
29342927 AbbrevToUse = VST_BBENTRY_6_ABBREV;
2935 } else if (F && !F->isDeclaration()) {
2936 // Must be the module-level VST, where we pass in the Index and
2937 // have a VSTOffsetPlaceholder. The function-level VST should not
2938 // contain any Function symbols.
2939 assert(FunctionToBitcodeIndex);
2940 assert(hasVSTOffsetPlaceholder());
2941
2942 // Save the word offset of the function (from the start of the
2943 // actual bitcode written to the stream).
2944 uint64_t BitcodeIndex = (*FunctionToBitcodeIndex)[F] - bitcodeStartBit();
2945 assert((BitcodeIndex & 31) == 0 && "function block not 32-bit aligned");
2946 // Note that we add 1 here because the offset is relative to one word
2947 // before the start of the identification block, which was historically
2948 // always the start of the regular bitcode header.
2949 NameVals.push_back(BitcodeIndex / 32 + 1);
2950
2951 Code = bitc::VST_CODE_FNENTRY;
2952 AbbrevToUse = FnEntry8BitAbbrev;
2953 if (Bits == SE_Char6)
2954 AbbrevToUse = FnEntry6BitAbbrev;
2955 else if (Bits == SE_Fixed7)
2956 AbbrevToUse = FnEntry7BitAbbrev;
29572928 } else {
29582929 Code = bitc::VST_CODE_ENTRY;
29592930 if (Bits == SE_Char6)
29692940 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
29702941 NameVals.clear();
29712942 }
2972 // Emit any GUID valueIDs created for indirect call edges into the
2973 // module-level VST.
2974 if (IsModuleLevel && hasVSTOffsetPlaceholder())
2975 for (const auto &GI : valueIds()) {
2976 NameVals.push_back(GI.second);
2977 NameVals.push_back(GI.first);
2978 Stream.EmitRecord(bitc::VST_CODE_COMBINED_ENTRY, NameVals,
2979 GUIDEntryAbbrev);
2980 NameVals.clear();
2981 }
2982 Stream.ExitBlock();
2983 }
2984
2985 /// Emit function names and summary offsets for the combined index
2986 /// used by ThinLTO.
2987 void IndexBitcodeWriter::writeCombinedValueSymbolTable() {
2988 assert(hasVSTOffsetPlaceholder() && "Expected non-zero VSTOffsetPlaceholder");
2989 // Get the offset of the VST we are writing, and backpatch it into
2990 // the VST forward declaration record.
2991 uint64_t VSTOffset = Stream.GetCurrentBitNo();
2992 assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned");
2993 Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32);
2994
2995 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
2996
2997 auto Abbv = std::make_shared();
2998 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_COMBINED_ENTRY));
2999 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
3000 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // refguid
3001 unsigned EntryAbbrev = Stream.EmitAbbrev(std::move(Abbv));
3002
3003 SmallVector NameVals;
3004 for (const auto &GVI : valueIds()) {
3005 // VST_CODE_COMBINED_ENTRY: [valueid, refguid]
3006 NameVals.push_back(GVI.second);
3007 NameVals.push_back(GVI.first);
3008
3009 // Emit the finished record.
3010 Stream.EmitRecord(bitc::VST_CODE_COMBINED_ENTRY, NameVals, EntryAbbrev);
3011 NameVals.clear();
3012 }
2943
30132944 Stream.ExitBlock();
30142945 }
30152946
31133044
31143045 // Emit names for all the instructions etc.
31153046 if (auto *Symtab = F.getValueSymbolTable())
3116 writeValueSymbolTable(*Symtab);
3047 writeFunctionLevelValueSymbolTable(*Symtab);
31173048
31183049 if (NeedsMetadataAttachment)
31193050 writeFunctionMetadataAttachment(F);
34993430 if (Index->begin() == Index->end()) {
35003431 Stream.ExitBlock();
35013432 return;
3433 }
3434
3435 for (const auto &GVI : valueIds()) {
3436 Stream.EmitRecord(bitc::FS_VALUE_GUID,
3437 ArrayRef{GVI.second, GVI.first});
35023438 }
35033439
35043440 // Abbrev for FS_PERMODULE.
35923528 void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
35933529 Stream.EnterSubblock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID, 3);
35943530 Stream.EmitRecord(bitc::FS_VERSION, ArrayRef{INDEX_VERSION});
3531
3532 // Create value IDs for undefined references.
3533 for (const auto &I : *this) {
3534 if (auto *VS = dyn_cast(I.second)) {
3535 for (auto &RI : VS->refs())
3536 assignValueId(RI.getGUID());
3537 continue;
3538 }
3539
3540 auto *FS = dyn_cast(I.second);
3541 if (!FS)
3542 continue;
3543 for (auto &RI : FS->refs())
3544 assignValueId(RI.getGUID());
3545
3546 for (auto &EI : FS->calls()) {
3547 GlobalValue::GUID GUID = EI.first.getGUID();
3548 if (!hasValueId(GUID)) {
3549 // For SamplePGO, the indirect call targets for local functions will
3550 // have its original name annotated in profile. We try to find the
3551 // corresponding PGOFuncName as the GUID.
3552 GUID = Index.getGUIDFromOriginalID(GUID);
3553 if (GUID == 0 || !hasValueId(GUID))
3554 continue;
3555 }
3556 assignValueId(GUID);
3557 }
3558 }
3559
3560 for (const auto &GVI : valueIds()) {
3561 Stream.EmitRecord(bitc::FS_VALUE_GUID,
3562 ArrayRef{GVI.second, GVI.first});
3563 }
35953564
35963565 // Abbrev for FS_COMBINED.
35973566 auto Abbv = std::make_shared();
38073776 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
38083777 size_t BlockStartPos = Buffer.size();
38093778
3810 SmallVector Vals;
3811 unsigned CurVersion = 1;
3812 Vals.push_back(CurVersion);
3813 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
3779 writeModuleVersion();
38143780
38153781 // Emit blockinfo, which defines the standard abbreviations etc.
38163782 writeBlockInfo();
38563822 if (Index)
38573823 writePerModuleGlobalValueSummary();
38583824
3859 writeValueSymbolTable(M.getValueSymbolTable(),
3860 /* IsModuleLevel */ true, &FunctionToBitcodeIndex);
3825 writeGlobalValueSymbolTable(FunctionToBitcodeIndex);
38613826
38623827 writeModuleHash(BlockStartPos);
38633828
39453910 writeBitcodeHeader(*Stream);
39463911 }
39473912
3948 BitcodeWriter::~BitcodeWriter() = default;
3913 BitcodeWriter::~BitcodeWriter() { assert(WroteStrtab); }
3914
3915 void BitcodeWriter::writeBlob(unsigned Block, unsigned Record, StringRef Blob) {
3916 Stream->EnterSubblock(Block, 3);
3917
3918 auto Abbv = std::make_shared();
3919 Abbv->Add(BitCodeAbbrevOp(Record));
3920 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3921 auto AbbrevNo = Stream->EmitAbbrev(std::move(Abbv));
3922
3923 Stream->EmitRecordWithBlob(AbbrevNo, ArrayRef{Record}, Blob);
3924
3925 Stream->ExitBlock();
3926 }
3927
3928 void BitcodeWriter::writeStrtab() {
3929 assert(!WroteStrtab);
3930
3931 std::vector Strtab;
3932 StrtabBuilder.finalizeInOrder();
3933 Strtab.resize(StrtabBuilder.getSize());
3934 StrtabBuilder.write((uint8_t *)Strtab.data());
3935
3936 writeBlob(bitc::STRTAB_BLOCK_ID, bitc::STRTAB_BLOB,
3937 {Strtab.data(), Strtab.size()});
3938
3939 WroteStrtab = true;
3940 }
3941
3942 void BitcodeWriter::copyStrtab(StringRef Strtab) {
3943 writeBlob(bitc::STRTAB_BLOCK_ID, bitc::STRTAB_BLOB, Strtab);
3944 WroteStrtab = true;
3945 }
39493946
39503947 void BitcodeWriter::writeModule(const Module *M,
39513948 bool ShouldPreserveUseListOrder,
39523949 const ModuleSummaryIndex *Index,
39533950 bool GenerateHash, ModuleHash *ModHash) {
3954 ModuleBitcodeWriter ModuleWriter(M, Buffer, *Stream,
3951 ModuleBitcodeWriter ModuleWriter(M, Buffer, StrtabBuilder, *Stream,
39553952 ShouldPreserveUseListOrder, Index,
39563953 GenerateHash, ModHash);
39573954 ModuleWriter.write();
39753972 BitcodeWriter Writer(Buffer);
39763973 Writer.writeModule(M, ShouldPreserveUseListOrder, Index, GenerateHash,
39773974 ModHash);
3975 Writer.writeStrtab();
39783976
39793977 if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
39803978 emitDarwinBCHeaderAndTrailer(Buffer, TT);
39863984 void IndexBitcodeWriter::write() {
39873985 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
39883986
3989 SmallVector Vals;
3990 unsigned CurVersion = 1;
3991 Vals.push_back(CurVersion);
3992 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
3993
3994 // If we have a VST, write the VSTOFFSET record placeholder.
3995 writeValueSymbolTableForwardDecl();
3987 writeModuleVersion();
39963988
39973989 // Write the module paths in the combined index.
39983990 writeModStrings();
39993991
40003992 // Write the summary combined index records.
40013993 writeCombinedGlobalValueSummary();
4002
4003 // Need a special VST writer for the combined index (we don't have a
4004 // real VST and real values when this is invoked).
4005 writeCombinedValueSymbolTable();
40063994
40073995 Stream.ExitBlock();
40083996 }
362362 W.writeModule(&M, /*ShouldPreserveUseListOrder=*/false, &Index,
363363 /*GenerateHash=*/true, &ModHash);
364364 W.writeModule(MergedM.get());
365 W.writeStrtab();
365366 OS << Buffer;
366367
367368 // If a minimized bitcode module was requested for the thin link,
374375 W2.writeModule(&M, /*ShouldPreserveUseListOrder=*/false, &Index,
375376 /*GenerateHash=*/false, &ModHash);
376377 W2.writeModule(MergedM.get());
378 W2.writeStrtab();
377379 *ThinLinkOS << Buffer;
378380 }
379381 }
44 ; RUN: llvm-lto -thinlto -o %t3 %t.o %t2.o
55 ; RUN: llvm-bcanalyzer -dump %t3.thinlto.bc | FileCheck %s --check-prefix=COMBINED
66
7 ; CHECK:
8 ; "main"
9 ; CHECK-NEXT:
10 ; "analias"
11 ; CHECK-NEXT:
712 ; CHECK:
813 ; CHECK-NEXT:
9 ; See if the call to func is registered, using the expected callsite count
10 ; and value id matching the subsequent value symbol table.
11 ; CHECK-NEXT:
14 ; See if the call to func is registered.
15 ; The value id 1 matches the second FUNCTION record above.
16 ; CHECK-NEXT:
1217 ; CHECK-NEXT:
13 ; CHECK-NEXT:
14 ; CHECK-NEXT:
15 ; External function analias should have entry with value id FUNCID
16 ; CHECK-NEXT:
17 ; CHECK-NEXT:
18
19 ; CHECK:
20 ; CHECK-NEXT: blob data = 'mainanalias'
1821
1922 ; COMBINED:
2023 ; COMBINED-NEXT:
21 ; See if the call to analias is registered, using the expected callsite count
22 ; and value id matching the subsequent value symbol table.
23 ; COMBINED-NEXT:
24 ; Followed by the alias and aliasee
24 ; See if the call to analias is registered, using the expected value id.
25 ; COMBINED-NEXT:
26 ; COMBINED-NEXT:
27 ; COMBINED-NEXT:
28 ; COMBINED-NEXT:
2529 ; COMBINED-NEXT:
26 ; COMBINED-NEXT: :[0-9]+]]
30 ; COMBINED-NEXT: ]]
2731 ; COMBINED-NEXT:
28 ; COMBINED-NEXT:
29 ; Entry for function func should have entry with value id ALIASID
30 ; COMBINED-NEXT:
31 ; COMBINED-NEXT:
32 ; COMBINED-NEXT:
33 ; COMBINED-NEXT:
3432
3533 ; ModuleID = 'thinlto-function-summary-callgraph.ll'
3634 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
99 ; RUN: llvm-lto -thinlto-index-stats %p/Inputs/thinlto-function-summary-callgraph-pgo.1.bc | FileCheck %s --check-prefix=OLD
1010 ; RUN: llvm-lto -thinlto-index-stats %p/Inputs/thinlto-function-summary-callgraph-pgo-combined.1.bc | FileCheck %s --check-prefix=OLD-COMBINED
1111
12 ; CHECK:
13 ; CHECK-NEXT:
14 ; "func"
15 ; CHECK-NEXT:
1216 ; CHECK:
1317 ; CHECK-NEXT:
14 ; See if the call to func is registered, using the expected callsite count
15 ; and hotness type, with value id matching the subsequent value symbol table.
16 ; CHECK-NEXT:
18 ; See if the call to func is registered, using the expected hotness type.
19 ; CHECK-NEXT:
1720 ; CHECK-NEXT:
18 ; CHECK-NEXT:
19 ; CHECK-NEXT:
20 ; External function func should have entry with value id FUNCID
21 ; CHECK-NEXT:
22 ; CHECK-NEXT:
21 ; CHECK:
22 ; CHECK-NEXT: blob data = 'mainfunc'
2323
2424 ; COMBINED:
2525 ; COMBINED-NEXT:
26 ; COMBINED-NEXT:
27 ; COMBINED-NEXT:
2628 ; COMBINED-NEXT:
27 ; See if the call to func is registered, using the expected callsite count
28 ; and hotness type, with value id matching the subsequent value symbol table.
29 ; See if the call to func is registered, using the expected hotness type.
2930 ; op6=2 which is hotnessType::None.
30 ; COMBINED-NEXT: :[0-9]+]] op6=2/>
31 ; COMBINED-NEXT: ]] op6=2/>
3132 ; COMBINED-NEXT:
32 ; COMBINED-NEXT:
33 ; Entry for function func should have entry with value id FUNCID
34 ; COMBINED-NEXT:
35 ; COMBINED-NEXT:
36 ; COMBINED-NEXT:
3733
3834 ; ModuleID = 'thinlto-function-summary-callgraph.ll'
3935 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
55 ; RUN: llvm-bcanalyzer -dump %t3.thinlto.bc | FileCheck %s --check-prefix=COMBINED
66
77
8 ; CHECK:
9 ; "hot_function"
10 ; CHECK-NEXT:
11 ; "hot1"
12 ; CHECK-NEXT:
13 ; "hot2"
14 ; CHECK-NEXT:
15 ; "hot3"
16 ; CHECK-NEXT:
17 ; "hot4"
18 ; CHECK-NEXT:
19 ; "cold"
20 ; CHECK-NEXT:
21 ; "none1"
22 ; CHECK-NEXT:
23 ; "none2"
24 ; CHECK-NEXT:
25 ; "none3"
26 ; CHECK-NEXT:
827 ; CHECK-LABEL:
928 ; CHECK-NEXT:
10 ; See if the call to func is registered, using the expected callsite count
11 ; and profile count, with value id matching the subsequent value symbol table.
12 ; CHECK-NEXT: <PERMODULE_PROFILE {{.*}} op4=[[HOT1:.*]] op5=3 op6=[[COLD:.*]] op7=1 op8=[[HOT2:.*]] op9=3 op10=[[HOT4:.*]] op11=3 op12=[[NONE1:.*]] op13=2 op14=[[HOT3:.*]] op15=3 op16=[[NONE2:.*]] op17=2 op18=[[NONE3:.*]] op19=2 op20=[[LEGACY:.*]] op21=3/>
29 ; CHECK-NEXT: <VALUE_GUID op0=25 op1=123/>
30 ; op4=hot1 op6=cold op8=hot2 op10=hot4 op12=none1 op14=hot3 op16=none2 op18=none3 op20=123
31 ; CHECK-NEXT:
1332 ; CHECK-NEXT:
14 ; CHECK-LABEL:
15 ; CHECK-NEXT:
17 ; CHECK-DAG:
18 ; CHECK-DAG:
19 ; CHECK-DAG:
20 ; CHECK-DAG:
21 ; CHECK-DAG:
22 ; CHECK-DAG:
23 ; CHECK-DAG:
24 ; CHECK-DAG:
25 ; CHECK-LABEL:
33
34 ; CHECK:
35 ; CHECK-NEXT: blob data = 'hot_functionhot1hot2hot3hot4coldnone1none2none3'
2636
2737 ; COMBINED:
2838 ; COMBINED-NEXT:
39 ; COMBINED-NEXT:
40 ; COMBINED-NEXT:
41 ; COMBINED-NEXT:
42 ; COMBINED-NEXT:
43 ; COMBINED-NEXT:
44 ; COMBINED-NEXT:
45 ; COMBINED-NEXT:
46 ; COMBINED-NEXT:
2947 ; COMBINED-NEXT:
3048 ; COMBINED-NEXT:
3149 ; COMBINED-NEXT:
99 ; RUN: llvm-lto -thinlto-index-stats %p/Inputs/thinlto-function-summary-callgraph.1.bc | FileCheck %s --check-prefix=OLD
1010 ; RUN: llvm-lto -thinlto-index-stats %p/Inputs/thinlto-function-summary-callgraph-combined.1.bc | FileCheck %s --check-prefix=OLD-COMBINED
1111
12 ; CHECK:
13 ; CHECK-NEXT:
14 ; "func"
15 ; CHECK-NEXT:
1216 ; CHECK:
1317 ; CHECK-NEXT:
14 ; See if the call to func is registered, using the expected callsite count
15 ; and value id matching the subsequent value symbol table.
16 ; CHECK-NEXT:
18 ; See if the call to func is registered.
19 ; CHECK-NEXT:
1720 ; CHECK-NEXT:
18 ; CHECK-NEXT:
19 ; CHECK-NEXT:
20 ; External function func should have entry with value id FUNCID
21 ; CHECK-NEXT:
22 ; CHECK-NEXT:
21 ; CHECK:
22 ; CHECK-NEXT: blob data = 'mainfunc'
23
2324
2425 ; COMBINED:
2526 ; COMBINED-NEXT:
27 ; COMBINED-NEXT:
28 ; COMBINED-NEXT:
2629 ; COMBINED-NEXT:
27 ; See if the call to func is registered, using the expected callsite count
28 ; and value id matching the subsequent value symbol table.
29 ; COMBINED-NEXT:
30 ; See if the call to func is registered.
31 ; COMBINED-NEXT:
3032 ; COMBINED-NEXT:
31 ; COMBINED-NEXT:
32 ; Entry for function func should have entry with value id FUNCID
33 ; COMBINED-NEXT:
34 ; COMBINED-NEXT:
35 ; COMBINED-NEXT:
3633
3734 ; ModuleID = 'thinlto-function-summary-callgraph.ll'
3835 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
4845 declare void @func(...) #1
4946
5047 ; OLD: Index {{.*}} contains 1 nodes (1 functions, 0 alias, 0 globals) and 1 edges (0 refs and 1 calls)
51 ; OLD-COMBINED: Index {{.*}} contains 2 nodes (2 functions, 0 alias, 0 globals) and 1 edges (0 refs and 1 calls)
48 ; OLD-COMBINED: Index {{.*}} contains 2 nodes (2 functions, 0 alias, 0 globals) and 1 edges (0 refs and 1 calls)
44
55 ; COMBINED:
66 ; COMBINED-NEXT:
7 ; COMBINED-NEXT:
8 ; COMBINED-NEXT:
9 ; COMBINED-NEXT:
710 ; COMBINED-DAG:
811 ; COMBINED-DAG:
912 ; COMBINED-DAG:
1114 ; COMBINED-DAG:
1215 ; COMBINED-DAG:
1316 ; COMBINED-NEXT:
14 ; COMBINED-NEXT:
15 ; COMBINED-NEXT:
16 ; COMBINED-NEXT:
17 ; COMBINED-NEXT:
18 ; COMBINED-NEXT:
1917
2018 source_filename = "/path/to/source.c"
2119
0 ; Test to check both the callgraph and refgraph in summary
11 ; RUN: opt -module-summary %s -o %t.o
22 ; RUN: llvm-bcanalyzer -dump %t.o | FileCheck %s
3
4 ; CHECK:
5 ; "bar"
6 ; CHECK-NEXT:
7 ; "globalvar"
8 ; CHECK-NEXT:
9 ; "func"
10 ; CHECK-NEXT:
11 ; "func2"
12 ; CHECK-NEXT:
13 ; "foo"
14 ; CHECK-NEXT:
15 ; "func3"
16 ; CHECK-NEXT:
17 ; "W"
18 ; CHECK-NEXT:
19 ; "X"
20 ; CHECK-NEXT:
21 ; "Y"
22 ; CHECK-NEXT:
23 ; "Z"
24 ; CHECK-NEXT:
25 ; "llvm.ctpop.i8"
26 ; CHECK-NEXT:
27 ; "main"
28 ; CHECK-NEXT:
329
430 ; See if the calls and other references are recorded properly using the
531 ; expected value id and other information as appropriate (callsite cout
1036 ; llvm.ctpop.i8.
1137 ; CHECK:
1238 ; Function main contains call to func, as well as address reference to func:
13 ; CHECK-DAG:
39 ; op0=main op4=func op5=func
40 ; CHECK-DAG:
1441 ; Function W contains a call to func3 as well as a reference to globalvar:
15 ; CHECK-DAG:
42 ; op0=W op4=globalvar op5=func3
43 ; CHECK-DAG:
1644 ; Function X contains call to foo, as well as address reference to foo
1745 ; which is in the same instruction as the call:
18 ; CHECK-DAG:
46 ; op0=X op4=foo op5=foo
47 ; CHECK-DAG:
1948 ; Function Y contains call to func2, and ensures we don't incorrectly add
2049 ; a reference to it when reached while earlier analyzing the phi using its
2150 ; return value:
22 ; CHECK-DAG:
51 ; op0=Y op4=func2
52 ; CHECK-DAG:
2353 ; Function Z contains call to func2, and ensures we don't incorrectly add
2454 ; a reference to it when reached while analyzing subsequent use of its return
2555 ; value:
26 ; CHECK-DAG:
56 ; op0=Z op4=func2
57 ; CHECK-DAG:
2758 ; Variable bar initialization contains address reference to func:
28 ; CHECK-DAG:
59 ; op0=bar op2=func
60 ; CHECK-DAG:
2961 ; CHECK:
3062
31 ; CHECK-NEXT:
32 ; CHECK-DAG:
33 ; CHECK-DAG:
34 ; CHECK-DAG:
35 ; CHECK-DAG:
36 ; CHECK-DAG:
37 ; CHECK-DAG:
38 ; CHECK-DAG:
39 ; CHECK-DAG:
40 ; CHECK-DAG:
41 ; CHECK-DAG:
42 ; CHECK-DAG:
43 ; CHECK:
63 ; CHECK:
64 ; CHECK-NEXT: blob data = 'barglobalvarfuncfunc2foofunc3WXYZllvm.ctpop.i8main'
4465
4566 ; ModuleID = 'thinlto-function-summary-refgraph.ll'
4667 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
11 ; RUN: opt -passes=name-anon-globals -module-summary < %s | llvm-bcanalyzer -dump | FileCheck %s -check-prefix=BC
22 ; Check for summary block/records.
33
4 ; Check the value ids in the summary entries against the
5 ; same in the ValueSumbolTable, to ensure the ordering is stable.
6 ; Also check the linkage field on the summary entries.
4 ; BC:
5 ; "h"
6 ; BC-NEXT:
7 ; "foo"
8 ; BC-NEXT:
9 ; "bar"
10 ; BC-NEXT:
11 ; "anon.[32 chars].0"
12 ; BC-NEXT:
13 ; "variadic"
14 ; BC-NEXT:
15 ; "f"
16 ; BC-NEXT:
717 ; BC:
818 ; BC-NEXT:
919 ; BC-NEXT:
1222 ; BC-NEXT:
1323 ; BC-NEXT:
1424 ; BC-NEXT:
15 ; BC-NEXT:
16 ; BC-NEXT: record string = 'variadic'
17 ; BC-NEXT: record string = 'foo'
18 ; BC-NEXT: record string = 'bar'
19 ; BC-NEXT: record string = 'f'
20 ; BC-NEXT:
21 ; BC-NEXT: record string = 'anon.
25 ; BC:
26 ; BC-NEXT: blob data = 'hfoobaranon.{{................................}}.0variadicf'
2227
2328
2429 ; RUN: opt -name-anon-globals -module-summary < %s | llvm-dis | FileCheck %s
88 ; RUN: -import=globalfunc1:%p/Inputs/autoupgrade.bc %t.bc \
99 ; RUN: | llvm-bcanalyzer -dump | FileCheck %s
1010
11
12 ; CHECK-NOT: 'llvm.invariant.start'
13 ; CHECK: record string = 'llvm.invariant.start.p0i8'
14 ; CHECK-NOT: 'llvm.invariant.start'
11 ; CHECK:
12 ; CHECK-NEXT: blob data = 'mainglobalfunc1llvm.invariant.start.p0i8'
1513
1614 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
1715 target triple = "x86_64-apple-macosx10.11.0"
1212 ; BACKEND1-NEXT:
1313 ; BACKEND1-NEXT:
1414 ; BACKEND1-NEXT:
15 ; BACKEND1-NEXT:
16 ; BACKEND1-NEXT:
1517 ; BACKEND1-NEXT:
1618 ; BACKEND1-NEXT:
1719 ; BACKEND1-NEXT:
18 ; BACKEND1-NEXT:
19 ; Check that the format is: op0=valueid, op1=offset, op2=funcguid,
20 ; where funcguid is the lower 64 bits of the function name MD5.
21 ; BACKEND1-NEXT:
22 ; BACKEND1-NEXT:
23 ; BACKEND1-NEXT:
2420
2521 ; The backend index for Input/distributed_indexes.ll contains summaries from
2622 ; itself only, as it does not import anything.
2925 ; BACKEND2-NEXT:
3026 ; BACKEND2-NEXT:
3127 ; BACKEND2-NEXT:
28 ; BACKEND2-NEXT:
3229 ; BACKEND2-NEXT:
3330 ; BACKEND2-NEXT:
34 ; BACKEND2-NEXT:
35 ; Check that the format is: op0=valueid, op1=offset, op2=funcguid,
36 ; where funcguid is the lower 64 bits of the function name MD5.
37 ; BACKEND2-NEXT:
38 ; BACKEND2-NEXT:
3931
4032 declare void @g(...)
4133
8181 ; BACKEND1-NEXT:
8282 ; BACKEND1-NEXT:
8383 ; BACKEND1-NEXT:
84 ; BACKEND1-NEXT:
85 ; BACKEND1-NEXT:
8486 ; BACKEND1-NEXT:
8587 ; BACKEND1-NEXT:
8688 ; BACKEND1-NEXT:
87 ; BACKEND1-NEXT:
88 ; Check that the format is: op0=valueid, op1=offset, op2=funcguid,
89 ; where funcguid is the lower 64 bits of the function name MD5.
90 ; BACKEND1-NEXT:
91 ; BACKEND1-NEXT:
92 ; BACKEND1-NEXT:
9389
9490 ; The backend index for Input/thinlto.ll contains summaries from itself only,
9591 ; as it does not import anything.
9894 ; BACKEND2-NEXT:
9995 ; BACKEND2-NEXT:
10096 ; BACKEND2-NEXT:
97 ; BACKEND2-NEXT:
10198 ; BACKEND2-NEXT:
10299 ; BACKEND2-NEXT:
103 ; BACKEND2-NEXT:
104 ; Check that the format is: op0=valueid, op1=offset, op2=funcguid,
105 ; where funcguid is the lower 64 bits of the function name MD5.
106 ; BACKEND2-NEXT:
107 ; BACKEND2-NEXT:
108100
109101 ; COMBINED:
110102 ; COMBINED-NEXT:
112104 ; COMBINED-NEXT:
113105 ; COMBINED-NEXT:
114106 ; COMBINED-NEXT:
107 ; COMBINED-NEXT:
108 ; COMBINED-NEXT:
115109 ; COMBINED-NEXT:
116110 ; COMBINED-NEXT:
117111 ; COMBINED-NEXT:
118 ; COMBINED-NEXT:
119 ; Check that the format is: op0=valueid, op1=offset, op2=funcguid,
120 ; where funcguid is the lower 64 bits of the function name MD5.
121 ; COMBINED-NEXT:
122 ; COMBINED-NEXT:
123 ; COMBINED-NEXT:
124112
125113 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
126114 target triple = "x86_64-unknown-linux-gnu"
1010 ; COMBINED-NEXT:
1111 ; COMBINED-NEXT:
1212 ; COMBINED-NEXT:
13 ; COMBINED-NEXT:
14 ; COMBINED-NEXT:
1315 ; COMBINED-NEXT:
1416 ; COMBINED-NEXT:
1517 ; COMBINED-NEXT:
16 ; COMBINED-NEXT:
17 ; Check that the format is: op0=valueid, op1=offset, op2=funcguid,
18 ; where funcguid is the lower 64 bits of the function name MD5.
19 ; COMBINED-NEXT:
20 ; COMBINED-NEXT:
21 ; COMBINED-NEXT:
2218
2319 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
2420
121121 case bitc::GLOBALVAL_SUMMARY_BLOCK_ID:
122122 return "GLOBALVAL_SUMMARY_BLOCK";
123123 case bitc::MODULE_STRTAB_BLOCK_ID: return "MODULE_STRTAB_BLOCK";
124 case bitc::STRTAB_BLOCK_ID: return "STRTAB_BLOCK";
124125 }
125126 }
126127
314315 STRINGIFY_CODE(FS, TYPE_CHECKED_LOAD_VCALLS)
315316 STRINGIFY_CODE(FS, TYPE_TEST_ASSUME_CONST_VCALL)
316317 STRINGIFY_CODE(FS, TYPE_CHECKED_LOAD_CONST_VCALL)
318 STRINGIFY_CODE(FS, VALUE_GUID)
317319 }
318320 case bitc::METADATA_ATTACHMENT_ID:
319321 switch(CodeID) {
380382 default: return nullptr;
381383 case bitc::OPERAND_BUNDLE_TAG: return "OPERAND_BUNDLE_TAG";
382384 }
385 case bitc::STRTAB_BLOCK_ID:
386 switch(CodeID) {
387 default: return nullptr;
388 case bitc::STRTAB_BLOB: return "BLOB";
389 }
383390 }
384391 #undef STRINGIFY_CODE
385392 }
4343 std::unique_ptr MB = ExitOnErr(
4444 errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename)));
4545 std::vector Mods = ExitOnErr(getBitcodeModuleList(*MB));
46 for (auto &BitcodeMod : Mods)
46 for (auto &BitcodeMod : Mods) {
4747 Buffer.insert(Buffer.end(), BitcodeMod.getBuffer().begin(),
4848 BitcodeMod.getBuffer().end());
49 Writer.copyStrtab(BitcodeMod.getStrtab());
50 }
4951 }
5052 } else {
53 // The string table does not own strings added to it, some of which are
54 // owned by the modules; keep them alive until we write the string table.
55 std::vector> OwnedMods;
5156 for (const auto &InputFilename : InputFilenames) {
5257 SMDiagnostic Err;
5358 std::unique_ptr M = parseIRFile(InputFilename, Err, Context);
5661 return 1;
5762 }
5863 Writer.writeModule(M.get());
64 OwnedMods.push_back(std::move(M));
5965 }
66 Writer.writeStrtab();
6067 }
6168
6269 std::error_code EC;
5858 ExitOnErr(errorCodeToError(EC));
5959
6060 if (BinaryExtract) {
61 SmallVector Header;
62 BitcodeWriter Writer(Header);
63 Out->os() << Header << Ms[ModuleIndex].getBuffer();
61 SmallVector Result;
62 BitcodeWriter Writer(Result);
63 Result.append(Ms[ModuleIndex].getBuffer().begin(),
64 Ms[ModuleIndex].getBuffer().end());
65 Writer.copyStrtab(Ms[ModuleIndex].getStrtab());
66 Out->os() << Result;
6467 Out->keep();
6568 return 0;
6669 }