Sink undesirable LTO functions into the old C API

We want to encourage users of the C++ LTO API to reuse memory buffers instead
of repeatedly opening and reading the same file contents.

This reverts commit r212305 and implements a tidier scheme.

llvm-svn: 212308
This commit is contained in:
Alp Toker 2014-07-04 00:58:41 +00:00
parent 44123517c9
commit a27c4dc68d
3 changed files with 20 additions and 40 deletions

View File

@ -70,13 +70,14 @@ public:
static bool isBitcodeFile(const void *mem, size_t length);
static bool isBitcodeFile(const char *path);
/// Returns 'true' if the file or memory contents is LLVM bitcode for the
/// specified triple.
static bool isBitcodeFileForTarget(const void *mem,
size_t length,
const char *triplePrefix);
static bool isBitcodeFileForTarget(const char *path,
const char *triplePrefix);
/// Returns 'true' if the memory buffer is LLVM bitcode for the specified
/// triple.
static bool isBitcodeForTarget(MemoryBuffer *memBuffer,
StringRef triplePrefix);
/// Create a MemoryBuffer from a memory range with an optional name.
static MemoryBuffer *makeBuffer(const void *mem, size_t length,
StringRef name = "");
/// Create an LTOModule. N.B. These methods take ownership of the buffer. The
/// caller must have initialized the Targets, the TargetMCs, the AsmPrinters,
@ -202,17 +203,10 @@ private:
/// Get string that the data pointer points to.
bool objcClassNameFromExpression(const Constant *c, std::string &name);
/// Returns 'true' if the bitcode BC is for the specified target triple.
static bool isTargetMatch(StringRef BC, const char *TriplePrefix);
/// Create an LTOModule (private version). N.B. This method takes ownership of
/// the buffer.
static LTOModule *makeLTOModule(std::unique_ptr<MemoryBuffer> Buffer,
TargetOptions options, std::string &errMsg);
/// Create a MemoryBuffer from a memory range with an optional name.
static MemoryBuffer *makeBuffer(const void *mem, size_t length,
StringRef name = "");
};
}
#endif // LTO_MODULE_H

View File

@ -68,30 +68,9 @@ bool LTOModule::isBitcodeFile(const char *path) {
return type == sys::fs::file_magic::bitcode;
}
/// isBitcodeFileForTarget - Returns 'true' if the file (or memory contents) is
/// LLVM bitcode for the specified triple.
bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length,
const char *triplePrefix) {
MemoryBuffer *buffer = makeBuffer(mem, length);
if (!buffer)
return false;
return isTargetMatch(StringRef((const char *)mem, length), triplePrefix);
}
bool LTOModule::isBitcodeFileForTarget(const char *path,
const char *triplePrefix) {
std::unique_ptr<MemoryBuffer> buffer;
if (MemoryBuffer::getFile(path, buffer))
return false;
return isTargetMatch(buffer->getBuffer(), triplePrefix);
}
/// Returns 'true' if the bitcode BC is for the specified target triple.
bool LTOModule::isTargetMatch(StringRef BC, const char *TriplePrefix) {
std::unique_ptr<MemoryBuffer> Buffer(
MemoryBuffer::getMemBuffer(BC, "", false));
std::string Triple = getBitcodeTargetTriple(Buffer.get(), getGlobalContext());
return strncmp(Triple.c_str(), TriplePrefix, strlen(TriplePrefix)) == 0;
bool LTOModule::isBitcodeForTarget(MemoryBuffer *buffer,
StringRef triplePrefix) {
return getBitcodeTargetTriple(buffer, getGlobalContext()) == triplePrefix;
}
LTOModule *LTOModule::createFromFile(const char *path, TargetOptions options,

View File

@ -16,6 +16,7 @@
#include "llvm/CodeGen/CommandFlags.h"
#include "llvm/LTO/LTOCodeGenerator.h"
#include "llvm/LTO/LTOModule.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/TargetSelect.h"
// extra command-line flags needed for LTOCodeGenerator
@ -87,7 +88,10 @@ bool lto_module_is_object_file(const char* path) {
bool lto_module_is_object_file_for_target(const char* path,
const char* target_triplet_prefix) {
return LTOModule::isBitcodeFileForTarget(path, target_triplet_prefix);
std::unique_ptr<MemoryBuffer> buffer;
if (MemoryBuffer::getFile(path, buffer))
return false;
return LTOModule::isBitcodeForTarget(buffer.get(), target_triplet_prefix);
}
bool lto_module_is_object_file_in_memory(const void* mem, size_t length) {
@ -98,7 +102,10 @@ bool
lto_module_is_object_file_in_memory_for_target(const void* mem,
size_t length,
const char* target_triplet_prefix) {
return LTOModule::isBitcodeFileForTarget(mem, length, target_triplet_prefix);
std::unique_ptr<MemoryBuffer> buffer(LTOModule::makeBuffer(mem, length));
if (!buffer)
return false;
return LTOModule::isBitcodeForTarget(buffer.get(), target_triplet_prefix);
}
lto_module_t lto_module_create(const char* path) {