llvm.org GIT mirror llvm / a4799ad
Object: Teach irsymtab::read() to try to use the irsymtab that we wrote to disk. Fixes PR27551. Differential Revision: https://reviews.llvm.org/D33974 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@306488 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Collingbourne 3 years ago
6 changed file(s) with 90 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
110110
111111 struct BitcodeFileContents {
112112 std::vector Mods;
113 StringRef Symtab, StrtabForSymtab;
113114 };
114115
115 /// Returns the contents of a bitcode file.
116 /// Returns the contents of a bitcode file. This includes the raw contents of
117 /// the symbol table embedded in the bitcode file. Clients which require a
118 /// symbol table should prefer to use irsymtab::read instead of this function
119 /// because it creates a reader for the irsymtab and handles upgrading bitcode
120 /// files without a symbol table or with an old symbol table.
116121 Expected getBitcodeFileContents(MemoryBufferRef Buffer);
117122
118123 /// Returns a list of modules in the specified bitcode buffer.
254254 /// copied into an irsymtab::Symbol object.
255255 symbol_range symbols() const;
256256
257 size_t getNumModules() const { return Modules.size(); }
258
257259 /// Returns a slice of the symbol table for the I'th module in the file.
258260 /// The symbols enumerated by this method are ephemeral, but they can be
259261 /// copied into an irsymtab::Symbol object.
53595359 return *ErrorCategory;
53605360 }
53615361
5362 static Expected readStrtab(BitstreamCursor &Stream) {
5363 if (Stream.EnterSubBlock(bitc::STRTAB_BLOCK_ID))
5362 static Expected readBlobInRecord(BitstreamCursor &Stream,
5363 unsigned Block, unsigned RecordID) {
5364 if (Stream.EnterSubBlock(Block))
53645365 return error("Invalid record");
53655366
53665367 StringRef Strtab;
53815382 case BitstreamEntry::Record:
53825383 StringRef Blob;
53835384 SmallVector Record;
5384 if (Stream.readRecord(Entry.ID, Record, &Blob) == bitc::STRTAB_BLOB)
5385 if (Stream.readRecord(Entry.ID, Record, &Blob) == RecordID)
53855386 Strtab = Blob;
53865387 break;
53875388 }
54495450 }
54505451
54515452 if (Entry.ID == bitc::STRTAB_BLOCK_ID) {
5452 Expected Strtab = readStrtab(Stream);
5453 Expected Strtab =
5454 readBlobInRecord(Stream, bitc::STRTAB_BLOCK_ID, bitc::STRTAB_BLOB);
54535455 if (!Strtab)
54545456 return Strtab.takeError();
54555457 // This string table is used by every preceding bitcode module that does
54615463 break;
54625464 I->Strtab = *Strtab;
54635465 }
5466 // Similarly, the string table is used by every preceding symbol table;
5467 // normally there will be just one unless the bitcode file was created
5468 // by binary concatenation.
5469 if (!F.Symtab.empty() && F.StrtabForSymtab.empty())
5470 F.StrtabForSymtab = *Strtab;
5471 continue;
5472 }
5473
5474 if (Entry.ID == bitc::SYMTAB_BLOCK_ID) {
5475 Expected SymtabOrErr =
5476 readBlobInRecord(Stream, bitc::SYMTAB_BLOCK_ID, bitc::SYMTAB_BLOB);
5477 if (!SymtabOrErr)
5478 return SymtabOrErr.takeError();
5479
5480 // We can expect the bitcode file to have multiple symbol tables if it
5481 // was created by binary concatenation. In that case we silently
5482 // ignore any subsequent symbol tables, which is fine because this is a
5483 // low level function. The client is expected to notice that the number
5484 // of modules in the symbol table does not match the number of modules
5485 // in the input file and regenerate the symbol table.
5486 if (F.Symtab.empty())
5487 F.Symtab = *SymtabOrErr;
54645488 continue;
54655489 }
54665490
317317 return make_error("Bitcode file does not contain any modules",
318318 inconvertibleErrorCode());
319319
320 // Right now we have no on-disk representation of symbol tables, so we always
321 // upgrade.
322 return upgrade(BFC.Mods);
323 }
320 if (BFC.StrtabForSymtab.empty() ||
321 BFC.Symtab.size() < sizeof(storage::Header))
322 return upgrade(BFC.Mods);
323
324 // We cannot use the regular reader to read the version and producer, because
325 // it will expect the header to be in the current format. The only thing we
326 // can rely on is that the version and producer will be present as the first
327 // struct elements.
328 auto *Hdr = reinterpret_cast(BFC.Symtab.data());
329 unsigned Version = Hdr->Version;
330 StringRef Producer = Hdr->Producer.get(BFC.StrtabForSymtab);
331 if (Version != storage::Header::kCurrentVersion ||
332 Producer != kExpectedProducerName)
333 return upgrade(BFC.Mods);
334
335 FileContents FC;
336 FC.TheReader = {{BFC.Symtab.data(), BFC.Symtab.size()},
337 {BFC.StrtabForSymtab.data(), BFC.StrtabForSymtab.size()}};
338
339 // Finally, make sure that the number of modules in the symbol table matches
340 // the number of modules in the bitcode file. If they differ, it may mean that
341 // the bitcode file was created by binary concatenation, so we need to create
342 // a new symbol table from scratch.
343 if (FC.TheReader.getNumModules() != BFC.Mods.size())
344 return upgrade(std::move(BFC.Mods));
345
346 return std::move(FC);
347 }
0 ; RUN: env LLVM_OVERRIDE_PRODUCER=producer opt -o %t %s
11 ; RUN: llvm-bcanalyzer -dump -show-binary-blobs %t | FileCheck --check-prefix=BCA %s
2
3 ; Same producer, does not require upgrade.
4 ; RUN: env LLVM_OVERRIDE_PRODUCER=producer llvm-lto2 dump-symtab %t | FileCheck --check-prefix=SYMTAB %s
5
6 ; Different producer, requires upgrade.
7 ; RUN: env LLVM_OVERRIDE_PRODUCER=consumer llvm-lto2 dump-symtab %t | FileCheck --check-prefix=SYMTAB %s
28
39 ; BCA:
410 ; Version stored at offset 0.
713 ; BCA-NEXT:
814 ; BCA-NEXT: blob data = 'foobarproducerx86_64-unknown-linux-gnuirsymtab.ll'
915 ; BCA-NEXT:
16
17 ; SYMTAB: version: 0
18 ; SYMTAB-NEXT: producer: producer
19 ; SYMTAB-NEXT: target triple: x86_64-unknown-linux-gnu
20 ; SYMTAB-NEXT: source filename: irsymtab.ll
21 ; SYMTAB-NEXT: D------X foo
22 ; SYMTAB-NEXT: DU-----X bar
1023
1124 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
1225 target triple = "x86_64-unknown-linux-gnu"
1515 //
1616 //===----------------------------------------------------------------------===//
1717
18 #include "llvm/LTO/Caching.h"
18 #include "llvm/Bitcode/BitcodeReader.h"
1919 #include "llvm/CodeGen/CommandFlags.h"
2020 #include "llvm/IR/DiagnosticPrinter.h"
21 #include "llvm/LTO/Caching.h"
2122 #include "llvm/LTO/LTO.h"
2223 #include "llvm/Support/CommandLine.h"
2324 #include "llvm/Support/FileSystem.h"
297298 static int dumpSymtab(int argc, char **argv) {
298299 for (StringRef F : make_range(argv + 1, argv + argc)) {
299300 std::unique_ptr MB = check(MemoryBuffer::getFile(F), F);
301 BitcodeFileContents BFC = check(getBitcodeFileContents(*MB), F);
302
303 if (BFC.Symtab.size() >= sizeof(irsymtab::storage::Header)) {
304 auto *Hdr = reinterpret_cast(
305 BFC.Symtab.data());
306 outs() << "version: " << Hdr->Version << '\n';
307 if (Hdr->Version == irsymtab::storage::Header::kCurrentVersion)
308 outs() << "producer: " << Hdr->Producer.get(BFC.StrtabForSymtab)
309 << '\n';
310 }
311
300312 std::unique_ptr Input =
301313 check(InputFile::create(MB->getMemBufferRef()), F);
302314