blob: 9ca6e79a030d94ff588dd6bc9de7c6edb9e3c37c [file] [log] [blame]
Tom Stellard088faab42016-11-11 21:34:47 +00001#include "llvm/Bitcode/BitcodeReader.h"
2#include "llvm/Bitcode/BitcodeWriter.h"
Tom Stellard976577e2013-06-26 18:20:32 +00003#include "llvm/IR/Function.h"
4#include "llvm/IR/GlobalVariable.h"
5#include "llvm/IR/LLVMContext.h"
6#include "llvm/IR/Module.h"
Peter Collingbourned5395fb2012-01-08 22:09:58 +00007#include "llvm/Support/CommandLine.h"
8#include "llvm/Support/ManagedStatic.h"
9#include "llvm/Support/MemoryBuffer.h"
Tom Stellard98dccb12014-04-30 18:35:20 +000010#include "llvm/Support/FileSystem.h"
Peter Collingbourned5395fb2012-01-08 22:09:58 +000011#include "llvm/Support/raw_ostream.h"
Tom Stellard8a63b152014-01-20 20:28:48 +000012#include "llvm/Support/ErrorOr.h"
Peter Collingbourned5395fb2012-01-08 22:09:58 +000013#include "llvm/Support/ToolOutputFile.h"
Niels Ole Salscheider109ce692014-08-22 22:24:28 +000014#include "llvm/Config/llvm-config.h"
Peter Collingbourned5395fb2012-01-08 22:09:58 +000015
Jeroen Ketemad253e662014-06-21 09:20:31 +000016#include <system_error>
17
Jeroen Ketemad253e662014-06-21 09:20:31 +000018using namespace llvm;
19
Peter Collingbourned5395fb2012-01-08 22:09:58 +000020static cl::opt<std::string>
21InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
22
23static cl::opt<std::string>
24OutputFilename("o", cl::desc("Output filename"),
25 cl::value_desc("filename"));
26
27int main(int argc, char **argv) {
Tom Stellard6cb18a02016-04-15 14:18:58 +000028 LLVMContext Context;
Peter Collingbourned5395fb2012-01-08 22:09:58 +000029 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
30
31 cl::ParseCommandLineOptions(argc, argv, "libclc builtin preparation tool\n");
32
33 std::string ErrorMessage;
Jeroen Ketema2697d2a2015-06-27 12:35:54 +000034 Module *M = nullptr;
Peter Collingbourned5395fb2012-01-08 22:09:58 +000035
36 {
Tom Stellard8d92c392014-07-07 17:46:45 +000037 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
38 MemoryBuffer::getFile(InputFilename);
39 std::unique_ptr<MemoryBuffer> &BufferPtr = BufferOrErr.get();
40 if (std::error_code ec = BufferOrErr.getError())
Peter Collingbourned5395fb2012-01-08 22:09:58 +000041 ErrorMessage = ec.message();
Tom Stellard8a63b152014-01-20 20:28:48 +000042 else {
Jeroen Ketemad9157392015-08-07 08:31:37 +000043 ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
Tom Stellard4957e402015-06-24 17:03:50 +000044 parseBitcodeFile(BufferPtr.get()->getMemBufferRef(), Context);
Tom Stellard1d770712014-12-31 15:27:53 +000045 if (std::error_code ec = ModuleOrErr.getError())
Tom Stellard8a63b152014-01-20 20:28:48 +000046 ErrorMessage = ec.message();
Jeroen Ketemad9157392015-08-07 08:31:37 +000047
Tom Stellard4957e402015-06-24 17:03:50 +000048 M = ModuleOrErr.get().release();
Tom Stellard8a63b152014-01-20 20:28:48 +000049 }
Peter Collingbourned5395fb2012-01-08 22:09:58 +000050 }
51
Tom Stellard4957e402015-06-24 17:03:50 +000052 if (!M) {
Peter Collingbourned5395fb2012-01-08 22:09:58 +000053 errs() << argv[0] << ": ";
54 if (ErrorMessage.size())
55 errs() << ErrorMessage << "\n";
56 else
57 errs() << "bitcode didn't read correctly.\n";
58 return 1;
59 }
60
Matt Arsenault7ef7e6a2016-08-25 00:25:10 +000061 // Strip the OpenCL version metadata. There are a lot of linked
62 // modules in the library build, each spamming the same
63 // version. This may also report a different version than the user
64 // program is using. This should probably be uniqued when linking.
65 if (NamedMDNode *OCLVersion = M->getNamedMetadata("opencl.ocl.version"))
66 M->eraseNamedMetadata(OCLVersion);
67
Peter Collingbourned5395fb2012-01-08 22:09:58 +000068 // Set linkage of every external definition to linkonce_odr.
69 for (Module::iterator i = M->begin(), e = M->end(); i != e; ++i) {
70 if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage)
71 i->setLinkage(GlobalValue::LinkOnceODRLinkage);
72 }
73
74 for (Module::global_iterator i = M->global_begin(), e = M->global_end();
75 i != e; ++i) {
76 if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage)
77 i->setLinkage(GlobalValue::LinkOnceODRLinkage);
78 }
79
80 if (OutputFilename.empty()) {
81 errs() << "no output file\n";
82 return 1;
83 }
84
Michel Danzera10b4922014-08-28 06:19:33 +000085 std::error_code EC;
Tom Stellard1d770712014-12-31 15:27:53 +000086 std::unique_ptr<tool_output_file> Out
Michel Danzera10b4922014-08-28 06:19:33 +000087 (new tool_output_file(OutputFilename, EC, sys::fs::F_None));
88 if (EC) {
89 errs() << EC.message() << '\n';
90 exit(1);
91 }
Peter Collingbourned5395fb2012-01-08 22:09:58 +000092
Tom Stellard4957e402015-06-24 17:03:50 +000093 WriteBitcodeToFile(M, Out->os());
Peter Collingbourned5395fb2012-01-08 22:09:58 +000094
95 // Declare success.
96 Out->keep();
97 return 0;
98}
99