blob: c8b1f446e35918e4eb2a8845debb48e8ebf59465 [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 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;
Tom Stellard1d770712014-12-31 15:27:53 +000087 std::unique_ptr<tool_output_file> Out
Michel Danzera10b4922014-08-28 06:19:33 +000088 (new tool_output_file(OutputFilename, EC, sys::fs::F_None));
89 if (EC) {
90 errs() << EC.message() << '\n';
91 exit(1);
92 }
Peter Collingbourned5395fb2012-01-08 22:09:58 +000093
Tom Stellard4957e402015-06-24 17:03:50 +000094 WriteBitcodeToFile(M, Out->os());
Peter Collingbourned5395fb2012-01-08 22:09:58 +000095
96 // Declare success.
97 Out->keep();
98 return 0;
99}
100