1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 18:54:02 +01:00

[ThinLTO] Make a copy of buffer identifier in ThinLTOCodeGenerator

We can't assume that the `const char *` provided through libLTO has a
lifetime that expands beyond the codegenerator itself.

llvm-svn: 295018
This commit is contained in:
Mehdi Amini 2017-02-14 02:20:51 +00:00
parent 88e492b37d
commit 5cb64b18b7
2 changed files with 35 additions and 15 deletions

View File

@ -31,6 +31,23 @@ class StringRef;
class LLVMContext;
class TargetMachine;
/// Wrapper around MemoryBufferRef, owning the identifier
class ThinLTOBuffer {
std::string OwnedIdentifier;
StringRef Buffer;
public:
ThinLTOBuffer(StringRef Buffer, StringRef Identifier)
: OwnedIdentifier(Identifier), Buffer(Buffer) {}
MemoryBufferRef getMemBuffer() const {
return MemoryBufferRef(Buffer,
{OwnedIdentifier.c_str(), OwnedIdentifier.size()});
}
StringRef getBuffer() const { return Buffer; }
StringRef getBufferIdentifier() const { return OwnedIdentifier; }
};
/// Helper to gather options relevant to the target machine creation
struct TargetMachineBuilder {
Triple TheTriple;
@ -280,7 +297,7 @@ private:
/// Vector holding the input buffers containing the bitcode modules to
/// process.
std::vector<MemoryBufferRef> Modules;
std::vector<ThinLTOBuffer> Modules;
/// Set of symbols that need to be preserved outside of the set of bitcode
/// files.

View File

@ -129,13 +129,13 @@ static void computePrevailingCopies(
}
static StringMap<MemoryBufferRef>
generateModuleMap(const std::vector<MemoryBufferRef> &Modules) {
generateModuleMap(const std::vector<ThinLTOBuffer> &Modules) {
StringMap<MemoryBufferRef> ModuleMap;
for (auto &ModuleBuffer : Modules) {
assert(ModuleMap.find(ModuleBuffer.getBufferIdentifier()) ==
ModuleMap.end() &&
"Expect unique Buffer Identifier");
ModuleMap[ModuleBuffer.getBufferIdentifier()] = ModuleBuffer;
ModuleMap[ModuleBuffer.getBufferIdentifier()] = ModuleBuffer.getMemBuffer();
}
return ModuleMap;
}
@ -501,13 +501,13 @@ static void initTMBuilder(TargetMachineBuilder &TMBuilder,
} // end anonymous namespace
void ThinLTOCodeGenerator::addModule(StringRef Identifier, StringRef Data) {
MemoryBufferRef Buffer(Data, Identifier);
ThinLTOBuffer Buffer(Data, Identifier);
if (Modules.empty()) {
// First module added, so initialize the triple and some options
LLVMContext Context;
StringRef TripleStr;
ErrorOr<std::string> TripleOrErr =
expectedToErrorOrAndEmitErrors(Context, getBitcodeTargetTriple(Buffer));
ErrorOr<std::string> TripleOrErr = expectedToErrorOrAndEmitErrors(
Context, getBitcodeTargetTriple(Buffer.getMemBuffer()));
if (TripleOrErr)
TripleStr = *TripleOrErr;
Triple TheTriple(TripleStr);
@ -517,8 +517,8 @@ void ThinLTOCodeGenerator::addModule(StringRef Identifier, StringRef Data) {
else {
LLVMContext Context;
StringRef TripleStr;
ErrorOr<std::string> TripleOrErr =
expectedToErrorOrAndEmitErrors(Context, getBitcodeTargetTriple(Buffer));
ErrorOr<std::string> TripleOrErr = expectedToErrorOrAndEmitErrors(
Context, getBitcodeTargetTriple(Buffer.getMemBuffer()));
if (TripleOrErr)
TripleStr = *TripleOrErr;
assert(TMBuilder.TheTriple.str() == TripleStr &&
@ -567,7 +567,8 @@ std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() {
uint64_t NextModuleId = 0;
for (auto &ModuleBuffer : Modules) {
Expected<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr =
object::ModuleSummaryIndexObjectFile::create(ModuleBuffer);
object::ModuleSummaryIndexObjectFile::create(
ModuleBuffer.getMemBuffer());
if (!ObjOrErr) {
// FIXME diagnose
logAllUnhandledErrors(
@ -831,8 +832,9 @@ void ThinLTOCodeGenerator::run() {
Context.setDiscardValueNames(LTODiscardValueNames);
// Parse module now
auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false,
/*IsImporting*/ false);
auto TheModule =
loadModuleFromBuffer(ModuleBuffer.getMemBuffer(), Context, false,
/*IsImporting*/ false);
// CodeGen
auto OutputBuffer = codegen(*TheModule);
@ -922,8 +924,8 @@ void ThinLTOCodeGenerator::run() {
std::iota(ModulesOrdering.begin(), ModulesOrdering.end(), 0);
std::sort(ModulesOrdering.begin(), ModulesOrdering.end(),
[&](int LeftIndex, int RightIndex) {
auto LSize = Modules[LeftIndex].getBufferSize();
auto RSize = Modules[RightIndex].getBufferSize();
auto LSize = Modules[LeftIndex].getBuffer().size();
auto RSize = Modules[RightIndex].getBuffer().size();
return LSize > RSize;
});
@ -976,8 +978,9 @@ void ThinLTOCodeGenerator::run() {
}
// Parse module now
auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false,
/*IsImporting*/ false);
auto TheModule =
loadModuleFromBuffer(ModuleBuffer.getMemBuffer(), Context, false,
/*IsImporting*/ false);
// Save temps: original file.
saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");