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