blob: 5cd32cdb24827e8714bea5cffc6bd67a869e6212 [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);
Jeroen Ketema80d2e8f2017-02-12 21:33:49 +000039 if (std::error_code ec = BufferOrErr.getError()) {
Peter Collingbourned5395fb2012-01-08 22:09:58 +000040 ErrorMessage = ec.message();
Jeroen Ketema80d2e8f2017-02-12 21:33:49 +000041 } else {
42 std::unique_ptr<MemoryBuffer> &BufferPtr = BufferOrErr.get();
Jeroen Ketemad9157392015-08-07 08:31:37 +000043 ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
Tom Stellardd83eb342016-11-14 16:06:33 +000044 expectedToErrorOrAndEmitErrors(Context,
45 parseBitcodeFile(BufferPtr.get()->getMemBufferRef(), Context));
Tom Stellard1d770712014-12-31 15:27:53 +000046 if (std::error_code ec = ModuleOrErr.getError())
Tom Stellard8a63b152014-01-20 20:28:48 +000047 ErrorMessage = ec.message();
Jeroen Ketemad9157392015-08-07 08:31:37 +000048
Tom Stellard4957e402015-06-24 17:03:50 +000049 M = ModuleOrErr.get().release();
Tom Stellard8a63b152014-01-20 20:28:48 +000050 }
Peter Collingbourned5395fb2012-01-08 22:09:58 +000051 }
52
Tom Stellard4957e402015-06-24 17:03:50 +000053 if (!M) {
Peter Collingbourned5395fb2012-01-08 22:09:58 +000054 errs() << argv[0] << ": ";
55 if (ErrorMessage.size())
56 errs() << ErrorMessage << "\n";
57 else
58 errs() << "bitcode didn't read correctly.\n";
59 return 1;
60 }
61
Matt Arsenault7ef7e6a2016-08-25 00:25:10 +000062 // Strip the OpenCL version metadata. There are a lot of linked
63 // modules in the library build, each spamming the same
64 // version. This may also report a different version than the user
65 // program is using. This should probably be uniqued when linking.
66 if (NamedMDNode *OCLVersion = M->getNamedMetadata("opencl.ocl.version"))
67 M->eraseNamedMetadata(OCLVersion);
68
Peter Collingbourned5395fb2012-01-08 22:09:58 +000069 // Set linkage of every external definition to linkonce_odr.
70 for (Module::iterator i = M->begin(), e = M->end(); i != e; ++i) {
71 if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage)
72 i->setLinkage(GlobalValue::LinkOnceODRLinkage);
73 }
74
75 for (Module::global_iterator i = M->global_begin(), e = M->global_end();
76 i != e; ++i) {
77 if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage)
78 i->setLinkage(GlobalValue::LinkOnceODRLinkage);
79 }
80
81 if (OutputFilename.empty()) {
82 errs() << "no output file\n";
83 return 1;
84 }
85
Michel Danzera10b4922014-08-28 06:19:33 +000086 std::error_code EC;
Jan Vesely76005202017-09-25 16:04:37 +000087#if HAVE_LLVM >= 0x0600
Reid Kleckner3fc649c2017-09-23 01:03:17 +000088 std::unique_ptr<ToolOutputFile> Out(
89 new ToolOutputFile(OutputFilename, EC, sys::fs::F_None));
Jan Vesely76005202017-09-25 16:04:37 +000090#else
91 std::unique_ptr<tool_output_file> Out(
92 new tool_output_file(OutputFilename, EC, sys::fs::F_None));
93#endif
Michel Danzera10b4922014-08-28 06:19:33 +000094 if (EC) {
95 errs() << EC.message() << '\n';
96 exit(1);
97 }
Peter Collingbourned5395fb2012-01-08 22:09:58 +000098
Tom Stellard4957e402015-06-24 17:03:50 +000099 WriteBitcodeToFile(M, Out->os());
Peter Collingbourned5395fb2012-01-08 22:09:58 +0000100
101 // Declare success.
102 Out->keep();
103 return 0;
104}
105