2017-09-30 03:06:41 +08:00
|
|
|
#if HAVE_LLVM > 0x0390
|
2016-11-12 05:34:47 +08:00
|
|
|
#include "llvm/Bitcode/BitcodeReader.h"
|
|
|
|
#include "llvm/Bitcode/BitcodeWriter.h"
|
2017-09-30 03:06:41 +08:00
|
|
|
#else
|
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
|
|
|
#endif
|
|
|
|
|
2013-06-27 02:20:32 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/GlobalVariable.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2012-01-09 06:09:58 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2014-05-01 02:35:20 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2012-01-09 06:09:58 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-01-21 04:28:48 +08:00
|
|
|
#include "llvm/Support/ErrorOr.h"
|
2012-01-09 06:09:58 +08:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2014-08-23 06:24:28 +08:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2012-01-09 06:09:58 +08:00
|
|
|
|
2014-06-21 17:20:31 +08:00
|
|
|
#include <system_error>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2012-01-09 06:09:58 +08:00
|
|
|
static cl::opt<std::string>
|
|
|
|
InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
|
|
|
|
|
|
|
|
static cl::opt<std::string>
|
|
|
|
OutputFilename("o", cl::desc("Output filename"),
|
|
|
|
cl::value_desc("filename"));
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2016-04-15 22:18:58 +08:00
|
|
|
LLVMContext Context;
|
2012-01-09 06:09:58 +08:00
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
|
|
|
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, "libclc builtin preparation tool\n");
|
|
|
|
|
|
|
|
std::string ErrorMessage;
|
2015-06-27 20:35:54 +08:00
|
|
|
Module *M = nullptr;
|
2012-01-09 06:09:58 +08:00
|
|
|
|
|
|
|
{
|
2014-07-08 01:46:45 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
|
|
|
|
MemoryBuffer::getFile(InputFilename);
|
2017-02-13 05:33:49 +08:00
|
|
|
if (std::error_code ec = BufferOrErr.getError()) {
|
2012-01-09 06:09:58 +08:00
|
|
|
ErrorMessage = ec.message();
|
2017-02-13 05:33:49 +08:00
|
|
|
} else {
|
|
|
|
std::unique_ptr<MemoryBuffer> &BufferPtr = BufferOrErr.get();
|
2015-08-07 16:31:37 +08:00
|
|
|
ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
|
2017-09-30 03:06:41 +08:00
|
|
|
#if HAVE_LLVM > 0x0390
|
2016-11-15 00:06:33 +08:00
|
|
|
expectedToErrorOrAndEmitErrors(Context,
|
|
|
|
parseBitcodeFile(BufferPtr.get()->getMemBufferRef(), Context));
|
2017-09-30 03:06:41 +08:00
|
|
|
#else
|
|
|
|
parseBitcodeFile(BufferPtr.get()->getMemBufferRef(), Context);
|
|
|
|
#endif
|
2014-12-31 23:27:53 +08:00
|
|
|
if (std::error_code ec = ModuleOrErr.getError())
|
2014-01-21 04:28:48 +08:00
|
|
|
ErrorMessage = ec.message();
|
2015-08-07 16:31:37 +08:00
|
|
|
|
2015-06-25 01:03:50 +08:00
|
|
|
M = ModuleOrErr.get().release();
|
2014-01-21 04:28:48 +08:00
|
|
|
}
|
2012-01-09 06:09:58 +08:00
|
|
|
}
|
|
|
|
|
2015-06-25 01:03:50 +08:00
|
|
|
if (!M) {
|
2012-01-09 06:09:58 +08:00
|
|
|
errs() << argv[0] << ": ";
|
|
|
|
if (ErrorMessage.size())
|
|
|
|
errs() << ErrorMessage << "\n";
|
|
|
|
else
|
|
|
|
errs() << "bitcode didn't read correctly.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-08-25 08:25:10 +08:00
|
|
|
// Strip the OpenCL version metadata. There are a lot of linked
|
|
|
|
// modules in the library build, each spamming the same
|
|
|
|
// version. This may also report a different version than the user
|
|
|
|
// program is using. This should probably be uniqued when linking.
|
|
|
|
if (NamedMDNode *OCLVersion = M->getNamedMetadata("opencl.ocl.version"))
|
|
|
|
M->eraseNamedMetadata(OCLVersion);
|
|
|
|
|
2012-01-09 06:09:58 +08:00
|
|
|
// Set linkage of every external definition to linkonce_odr.
|
|
|
|
for (Module::iterator i = M->begin(), e = M->end(); i != e; ++i) {
|
|
|
|
if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage)
|
|
|
|
i->setLinkage(GlobalValue::LinkOnceODRLinkage);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Module::global_iterator i = M->global_begin(), e = M->global_end();
|
|
|
|
i != e; ++i) {
|
|
|
|
if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage)
|
|
|
|
i->setLinkage(GlobalValue::LinkOnceODRLinkage);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (OutputFilename.empty()) {
|
|
|
|
errs() << "no output file\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-08-28 14:19:33 +08:00
|
|
|
std::error_code EC;
|
2017-09-26 00:04:37 +08:00
|
|
|
#if HAVE_LLVM >= 0x0600
|
2017-09-23 09:03:17 +08:00
|
|
|
std::unique_ptr<ToolOutputFile> Out(
|
|
|
|
new ToolOutputFile(OutputFilename, EC, sys::fs::F_None));
|
2017-09-26 00:04:37 +08:00
|
|
|
#else
|
|
|
|
std::unique_ptr<tool_output_file> Out(
|
|
|
|
new tool_output_file(OutputFilename, EC, sys::fs::F_None));
|
|
|
|
#endif
|
2014-08-28 14:19:33 +08:00
|
|
|
if (EC) {
|
|
|
|
errs() << EC.message() << '\n';
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-01-09 06:09:58 +08:00
|
|
|
|
2018-02-23 15:37:03 +08:00
|
|
|
#if HAVE_LLVM >= 0x0700
|
|
|
|
WriteBitcodeToFile(*M, Out->os());
|
|
|
|
#else
|
2015-06-25 01:03:50 +08:00
|
|
|
WriteBitcodeToFile(M, Out->os());
|
2018-02-23 15:37:03 +08:00
|
|
|
#endif
|
2012-01-09 06:09:58 +08:00
|
|
|
|
|
|
|
// Declare success.
|
|
|
|
Out->keep();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|