[llvm] r304958 - Object: Factor out the code for creating the irsymtab for an arbitrary bitcode file.
Peter Collingbourne via llvm-commits
llvm-commits at lists.llvm.org
Wed Jun 7 18:26:15 PDT 2017
Author: pcc
Date: Wed Jun 7 20:26:14 2017
New Revision: 304958
URL: http://llvm.org/viewvc/llvm-project?rev=304958&view=rev
Log:
Object: Factor out the code for creating the irsymtab for an arbitrary bitcode file.
This code now lives in lib/Object. The idea is that it can now be reused by
IRObjectFile among other things.
Differential Revision: https://reviews.llvm.org/D31921
Modified:
llvm/trunk/include/llvm/Object/IRObjectFile.h
llvm/trunk/include/llvm/Object/IRSymtab.h
llvm/trunk/lib/LTO/LTO.cpp
llvm/trunk/lib/Object/IRObjectFile.cpp
llvm/trunk/lib/Object/IRSymtab.cpp
Modified: llvm/trunk/include/llvm/Object/IRObjectFile.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/IRObjectFile.h?rev=304958&r1=304957&r2=304958&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/IRObjectFile.h (original)
+++ llvm/trunk/include/llvm/Object/IRObjectFile.h Wed Jun 7 20:26:14 2017
@@ -15,6 +15,7 @@
#define LLVM_OBJECT_IROBJECTFILE_H
#include "llvm/ADT/PointerUnion.h"
+#include "llvm/Object/IRSymtab.h"
#include "llvm/Object/ModuleSymbolTable.h"
#include "llvm/Object/SymbolicFile.h"
@@ -61,7 +62,20 @@ public:
static Expected<std::unique_ptr<IRObjectFile>> create(MemoryBufferRef Object,
LLVMContext &Context);
};
+
+/// The contents of a bitcode file and its irsymtab. Any underlying data
+/// for the irsymtab are owned by Symtab and Strtab.
+struct IRSymtabFile {
+ std::vector<BitcodeModule> Mods;
+ SmallVector<char, 0> Symtab, Strtab;
+ irsymtab::Reader TheReader;
+};
+
+/// Reads a bitcode file, creating its irsymtab if necessary.
+Expected<IRSymtabFile> readIRSymtab(MemoryBufferRef MBRef);
+
}
+
}
#endif
Modified: llvm/trunk/include/llvm/Object/IRSymtab.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/IRSymtab.h?rev=304958&r1=304957&r2=304958&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/IRSymtab.h (original)
+++ llvm/trunk/include/llvm/Object/IRSymtab.h Wed Jun 7 20:26:14 2017
@@ -36,6 +36,9 @@
#include <vector>
namespace llvm {
+
+class BitcodeModule;
+
namespace irsymtab {
namespace storage {
@@ -314,6 +317,16 @@ inline Reader::symbol_range Reader::modu
SymbolRef(MEnd, MEnd, nullptr, this)};
}
+/// The contents of the irsymtab in a bitcode file. Any underlying data for the
+/// irsymtab are owned by Symtab and Strtab.
+struct FileContents {
+ SmallVector<char, 0> Symtab, Strtab;
+ Reader TheReader;
+};
+
+/// Reads the contents of a bitcode file, creating its irsymtab if necessary.
+Expected<FileContents> readBitcode(ArrayRef<BitcodeModule> Mods);
+
} // end namespace irsymtab
} // end namespace llvm
Modified: llvm/trunk/lib/LTO/LTO.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/LTO/LTO.cpp?rev=304958&r1=304957&r2=304958&view=diff
==============================================================================
--- llvm/trunk/lib/LTO/LTO.cpp (original)
+++ llvm/trunk/lib/LTO/LTO.cpp Wed Jun 7 20:26:14 2017
@@ -315,54 +315,19 @@ InputFile::~InputFile() = default;
Expected<std::unique_ptr<InputFile>> InputFile::create(MemoryBufferRef Object) {
std::unique_ptr<InputFile> File(new InputFile);
- ErrorOr<MemoryBufferRef> BCOrErr =
- IRObjectFile::findBitcodeInMemBuffer(Object);
- if (!BCOrErr)
- return errorCodeToError(BCOrErr.getError());
+ Expected<IRSymtabFile> FOrErr = readIRSymtab(Object);
+ if (!FOrErr)
+ return FOrErr.takeError();
+
+ File->TargetTriple = FOrErr->TheReader.getTargetTriple();
+ File->SourceFileName = FOrErr->TheReader.getSourceFileName();
+ File->COFFLinkerOpts = FOrErr->TheReader.getCOFFLinkerOpts();
+ File->ComdatTable = FOrErr->TheReader.getComdatTable();
- Expected<std::vector<BitcodeModule>> BMsOrErr =
- getBitcodeModuleList(*BCOrErr);
- if (!BMsOrErr)
- return BMsOrErr.takeError();
-
- if (BMsOrErr->empty())
- return make_error<StringError>("Bitcode file does not contain any modules",
- inconvertibleErrorCode());
-
- File->Mods = *BMsOrErr;
-
- LLVMContext Ctx;
- std::vector<Module *> Mods;
- std::vector<std::unique_ptr<Module>> OwnedMods;
- for (auto BM : *BMsOrErr) {
- Expected<std::unique_ptr<Module>> MOrErr =
- BM.getLazyModule(Ctx, /*ShouldLazyLoadMetadata*/ true,
- /*IsImporting*/ false);
- if (!MOrErr)
- return MOrErr.takeError();
-
- if ((*MOrErr)->getDataLayoutStr().empty())
- return make_error<StringError>("input module has no datalayout",
- inconvertibleErrorCode());
-
- Mods.push_back(MOrErr->get());
- OwnedMods.push_back(std::move(*MOrErr));
- }
-
- SmallVector<char, 0> Symtab;
- if (Error E = irsymtab::build(Mods, Symtab, File->Strtab))
- return std::move(E);
-
- irsymtab::Reader R({Symtab.data(), Symtab.size()},
- {File->Strtab.data(), File->Strtab.size()});
- File->TargetTriple = R.getTargetTriple();
- File->SourceFileName = R.getSourceFileName();
- File->COFFLinkerOpts = R.getCOFFLinkerOpts();
- File->ComdatTable = R.getComdatTable();
-
- for (unsigned I = 0; I != Mods.size(); ++I) {
+ for (unsigned I = 0; I != FOrErr->Mods.size(); ++I) {
size_t Begin = File->Symbols.size();
- for (const irsymtab::Reader::SymbolRef &Sym : R.module_symbols(I))
+ for (const irsymtab::Reader::SymbolRef &Sym :
+ FOrErr->TheReader.module_symbols(I))
// Skip symbols that are irrelevant to LTO. Note that this condition needs
// to match the one in Skip() in LTO::addRegularLTO().
if (Sym.isGlobal() && !Sym.isFormatSpecific())
@@ -370,6 +335,8 @@ Expected<std::unique_ptr<InputFile>> Inp
File->ModuleSymIndices.push_back({Begin, File->Symbols.size()});
}
+ File->Mods = FOrErr->Mods;
+ File->Strtab = std::move(FOrErr->Strtab);
return std::move(File);
}
Modified: llvm/trunk/lib/Object/IRObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/IRObjectFile.cpp?rev=304958&r1=304957&r2=304958&view=diff
==============================================================================
--- llvm/trunk/lib/Object/IRObjectFile.cpp (original)
+++ llvm/trunk/lib/Object/IRObjectFile.cpp Wed Jun 7 20:26:14 2017
@@ -139,3 +139,26 @@ IRObjectFile::create(MemoryBufferRef Obj
return std::unique_ptr<IRObjectFile>(
new IRObjectFile(*BCOrErr, std::move(Mods)));
}
+
+Expected<IRSymtabFile> object::readIRSymtab(MemoryBufferRef MBRef) {
+ IRSymtabFile F;
+ ErrorOr<MemoryBufferRef> BCOrErr =
+ IRObjectFile::findBitcodeInMemBuffer(MBRef);
+ if (!BCOrErr)
+ return errorCodeToError(BCOrErr.getError());
+
+ Expected<std::vector<BitcodeModule>> BMsOrErr =
+ getBitcodeModuleList(*BCOrErr);
+ if (!BMsOrErr)
+ return BMsOrErr.takeError();
+
+ Expected<irsymtab::FileContents> FCOrErr = irsymtab::readBitcode(*BMsOrErr);
+ if (!FCOrErr)
+ return FCOrErr.takeError();
+
+ F.Mods = std::move(*BMsOrErr);
+ F.Symtab = std::move(FCOrErr->Symtab);
+ F.Strtab = std::move(FCOrErr->Strtab);
+ F.TheReader = std::move(FCOrErr->TheReader);
+ return std::move(F);
+}
Modified: llvm/trunk/lib/Object/IRSymtab.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/IRSymtab.cpp?rev=304958&r1=304957&r2=304958&view=diff
==============================================================================
--- llvm/trunk/lib/Object/IRSymtab.cpp (original)
+++ llvm/trunk/lib/Object/IRSymtab.cpp Wed Jun 7 20:26:14 2017
@@ -23,7 +23,9 @@
#include "llvm/IR/Mangler.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Module.h"
+#include "llvm/Bitcode/BitcodeReader.h"
#include "llvm/MC/StringTableBuilder.h"
+#include "llvm/Object/IRObjectFile.h"
#include "llvm/Object/ModuleSymbolTable.h"
#include "llvm/Object/SymbolicFile.h"
#include "llvm/Support/Allocator.h"
@@ -259,3 +261,35 @@ Error irsymtab::build(ArrayRef<Module *>
SmallVector<char, 0> &Strtab) {
return Builder(Symtab, Strtab).build(Mods);
}
+
+Expected<FileContents> irsymtab::readBitcode(ArrayRef<BitcodeModule> BMs) {
+ FileContents FC;
+ if (BMs.empty())
+ return make_error<StringError>("Bitcode file does not contain any modules",
+ inconvertibleErrorCode());
+
+ LLVMContext Ctx;
+ std::vector<Module *> Mods;
+ std::vector<std::unique_ptr<Module>> OwnedMods;
+ for (auto BM : BMs) {
+ Expected<std::unique_ptr<Module>> MOrErr =
+ BM.getLazyModule(Ctx, /*ShouldLazyLoadMetadata*/ true,
+ /*IsImporting*/ false);
+ if (!MOrErr)
+ return MOrErr.takeError();
+
+ if ((*MOrErr)->getDataLayoutStr().empty())
+ return make_error<StringError>("input module has no datalayout",
+ inconvertibleErrorCode());
+
+ Mods.push_back(MOrErr->get());
+ OwnedMods.push_back(std::move(*MOrErr));
+ }
+
+ if (Error E = build(Mods, FC.Symtab, FC.Strtab))
+ return std::move(E);
+
+ FC.TheReader = {{FC.Symtab.data(), FC.Symtab.size()},
+ {FC.Strtab.data(), FC.Strtab.size()}};
+ return std::move(FC);
+}
More information about the llvm-commits
mailing list