blob: de1e804d6fcf9efde5cd8c5b43580042db0935d2 [file] [log] [blame]
Jan Veselyce29e8c2017-09-29 19:06:41 +00001#if HAVE_LLVM > 0x0390
Tom Stellard088faab42016-11-11 21:34:47 +00002#include "llvm/Bitcode/BitcodeReader.h"
3#include "llvm/Bitcode/BitcodeWriter.h"
Jan Veselyce29e8c2017-09-29 19:06:41 +00004#else
5#include "llvm/Bitcode/ReaderWriter.h"
6#endif
7
Tom Stellard976577e2013-06-26 18:20:32 +00008#include "llvm/IR/Function.h"
9#include "llvm/IR/GlobalVariable.h"
10#include "llvm/IR/LLVMContext.h"
11#include "llvm/IR/Module.h"
Peter Collingbourned5395fb2012-01-08 22:09:58 +000012#include "llvm/Support/CommandLine.h"
13#include "llvm/Support/ManagedStatic.h"
14#include "llvm/Support/MemoryBuffer.h"
Tom Stellard98dccb12014-04-30 18:35:20 +000015#include "llvm/Support/FileSystem.h"
Peter Collingbourned5395fb2012-01-08 22:09:58 +000016#include "llvm/Support/raw_ostream.h"
Tom Stellard8a63b152014-01-20 20:28:48 +000017#include "llvm/Support/ErrorOr.h"
Peter Collingbourned5395fb2012-01-08 22:09:58 +000018#include "llvm/Support/ToolOutputFile.h"
Niels Ole Salscheider109ce692014-08-22 22:24:28 +000019#include "llvm/Config/llvm-config.h"
Peter Collingbourned5395fb2012-01-08 22:09:58 +000020
Jeroen Ketemad253e662014-06-21 09:20:31 +000021#include <system_error>
22
Jeroen Ketemad253e662014-06-21 09:20:31 +000023using namespace llvm;
24
Peter Collingbourned5395fb2012-01-08 22:09:58 +000025static cl::opt<std::string>
26InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
27
28static cl::opt<std::string>
29OutputFilename("o", cl::desc("Output filename"),
30 cl::value_desc("filename"));
31
32int main(int argc, char **argv) {
Tom Stellard6cb18a02016-04-15 14:18:58 +000033 LLVMContext Context;
Peter Collingbourned5395fb2012-01-08 22:09:58 +000034 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
35
36 cl::ParseCommandLineOptions(argc, argv, "libclc builtin preparation tool\n");
37
38 std::string ErrorMessage;
Jeroen Ketema2697d2a2015-06-27 12:35:54 +000039 Module *M = nullptr;
Peter Collingbourned5395fb2012-01-08 22:09:58 +000040
41 {
Tom Stellard8d92c392014-07-07 17:46:45 +000042 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
43 MemoryBuffer::getFile(InputFilename);
Jeroen Ketema80d2e8f2017-02-12 21:33:49 +000044 if (std::error_code ec = BufferOrErr.getError()) {
Peter Collingbourned5395fb2012-01-08 22:09:58 +000045 ErrorMessage = ec.message();
Jeroen Ketema80d2e8f2017-02-12 21:33:49 +000046 } else {
47 std::unique_ptr<MemoryBuffer> &BufferPtr = BufferOrErr.get();
Jeroen Ketemad9157392015-08-07 08:31:37 +000048 ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
Jan Veselyce29e8c2017-09-29 19:06:41 +000049#if HAVE_LLVM > 0x0390
Tom Stellardd83eb342016-11-14 16:06:33 +000050 expectedToErrorOrAndEmitErrors(Context,
51 parseBitcodeFile(BufferPtr.get()->getMemBufferRef(), Context));
Jan Veselyce29e8c2017-09-29 19:06:41 +000052#else
53 parseBitcodeFile(BufferPtr.get()->getMemBufferRef(), Context);
54#endif
Tom Stellard1d770712014-12-31 15:27:53 +000055 if (std::error_code ec = ModuleOrErr.getError())
Tom Stellard8a63b152014-01-20 20:28:48 +000056 ErrorMessage = ec.message();
Jeroen Ketemad9157392015-08-07 08:31:37 +000057
Tom Stellard4957e402015-06-24 17:03:50 +000058 M = ModuleOrErr.get().release();
Tom Stellard8a63b152014-01-20 20:28:48 +000059 }
Peter Collingbourned5395fb2012-01-08 22:09:58 +000060 }
61
Tom Stellard4957e402015-06-24 17:03:50 +000062 if (!M) {
Peter Collingbourned5395fb2012-01-08 22:09:58 +000063 errs() << argv[0] << ": ";
64 if (ErrorMessage.size())
65 errs() << ErrorMessage << "\n";
66 else
67 errs() << "bitcode didn't read correctly.\n";
68 return 1;
69 }
70
Matt Arsenault7ef7e6a2016-08-25 00:25:10 +000071 // Strip the OpenCL version metadata. There are a lot of linked
72 // modules in the library build, each spamming the same
73 // version. This may also report a different version than the user
74 // program is using. This should probably be uniqued when linking.
75 if (NamedMDNode *OCLVersion = M->getNamedMetadata("opencl.ocl.version"))
76 M->eraseNamedMetadata(OCLVersion);
77
Peter Collingbourned5395fb2012-01-08 22:09:58 +000078 // Set linkage of every external definition to linkonce_odr.
79 for (Module::iterator i = M->begin(), e = M->end(); i != e; ++i) {
80 if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage)
81 i->setLinkage(GlobalValue::LinkOnceODRLinkage);
82 }
83
84 for (Module::global_iterator i = M->global_begin(), e = M->global_end();
85 i != e; ++i) {
86 if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage)
87 i->setLinkage(GlobalValue::LinkOnceODRLinkage);
88 }
89
90 if (OutputFilename.empty()) {
91 errs() << "no output file\n";
92 return 1;
93 }
94
Michel Danzera10b4922014-08-28 06:19:33 +000095 std::error_code EC;
Jan Vesely76005202017-09-25 16:04:37 +000096#if HAVE_LLVM >= 0x0600
Reid Kleckner3fc649c2017-09-23 01:03:17 +000097 std::unique_ptr<ToolOutputFile> Out(
98 new ToolOutputFile(OutputFilename, EC, sys::fs::F_None));
Jan Vesely76005202017-09-25 16:04:37 +000099#else
100 std::unique_ptr<tool_output_file> Out(
101 new tool_output_file(OutputFilename, EC, sys::fs::F_None));
102#endif
Michel Danzera10b4922014-08-28 06:19:33 +0000103 if (EC) {
104 errs() << EC.message() << '\n';
105 exit(1);
106 }
Peter Collingbourned5395fb2012-01-08 22:09:58 +0000107
Jan Vesely86db4302018-02-23 07:37:03 +0000108#if HAVE_LLVM >= 0x0700
109 WriteBitcodeToFile(*M, Out->os());
110#else
Tom Stellard4957e402015-06-24 17:03:50 +0000111 WriteBitcodeToFile(M, Out->os());
Jan Vesely86db4302018-02-23 07:37:03 +0000112#endif
Peter Collingbourned5395fb2012-01-08 22:09:58 +0000113
114 // Declare success.
115 Out->keep();
116 return 0;
117}
118