Jan Vesely | ce29e8c | 2017-09-29 19:06:41 +0000 | [diff] [blame] | 1 | #if HAVE_LLVM > 0x0390 |
Tom Stellard | 088faab4 | 2016-11-11 21:34:47 +0000 | [diff] [blame] | 2 | #include "llvm/Bitcode/BitcodeReader.h" |
| 3 | #include "llvm/Bitcode/BitcodeWriter.h" |
Jan Vesely | ce29e8c | 2017-09-29 19:06:41 +0000 | [diff] [blame] | 4 | #else |
| 5 | #include "llvm/Bitcode/ReaderWriter.h" |
| 6 | #endif |
| 7 | |
Tom Stellard | 976577e | 2013-06-26 18:20:32 +0000 | [diff] [blame] | 8 | #include "llvm/IR/Function.h" |
| 9 | #include "llvm/IR/GlobalVariable.h" |
| 10 | #include "llvm/IR/LLVMContext.h" |
| 11 | #include "llvm/IR/Module.h" |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 +0000 | [diff] [blame] | 12 | #include "llvm/Support/CommandLine.h" |
| 13 | #include "llvm/Support/ManagedStatic.h" |
| 14 | #include "llvm/Support/MemoryBuffer.h" |
Tom Stellard | 98dccb1 | 2014-04-30 18:35:20 +0000 | [diff] [blame] | 15 | #include "llvm/Support/FileSystem.h" |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 +0000 | [diff] [blame] | 16 | #include "llvm/Support/raw_ostream.h" |
Tom Stellard | 8a63b15 | 2014-01-20 20:28:48 +0000 | [diff] [blame] | 17 | #include "llvm/Support/ErrorOr.h" |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ToolOutputFile.h" |
Niels Ole Salscheider | 109ce69 | 2014-08-22 22:24:28 +0000 | [diff] [blame] | 19 | #include "llvm/Config/llvm-config.h" |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 +0000 | [diff] [blame] | 20 | |
Jeroen Ketema | d253e66 | 2014-06-21 09:20:31 +0000 | [diff] [blame] | 21 | #include <system_error> |
| 22 | |
Jeroen Ketema | d253e66 | 2014-06-21 09:20:31 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 +0000 | [diff] [blame] | 25 | static cl::opt<std::string> |
| 26 | InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-")); |
| 27 | |
| 28 | static cl::opt<std::string> |
| 29 | OutputFilename("o", cl::desc("Output filename"), |
| 30 | cl::value_desc("filename")); |
| 31 | |
| 32 | int main(int argc, char **argv) { |
Tom Stellard | 6cb18a0 | 2016-04-15 14:18:58 +0000 | [diff] [blame] | 33 | LLVMContext Context; |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 +0000 | [diff] [blame] | 34 | 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 Ketema | 2697d2a | 2015-06-27 12:35:54 +0000 | [diff] [blame] | 39 | Module *M = nullptr; |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 +0000 | [diff] [blame] | 40 | |
| 41 | { |
Tom Stellard | 8d92c39 | 2014-07-07 17:46:45 +0000 | [diff] [blame] | 42 | ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr = |
| 43 | MemoryBuffer::getFile(InputFilename); |
Jeroen Ketema | 80d2e8f | 2017-02-12 21:33:49 +0000 | [diff] [blame] | 44 | if (std::error_code ec = BufferOrErr.getError()) { |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 +0000 | [diff] [blame] | 45 | ErrorMessage = ec.message(); |
Jeroen Ketema | 80d2e8f | 2017-02-12 21:33:49 +0000 | [diff] [blame] | 46 | } else { |
| 47 | std::unique_ptr<MemoryBuffer> &BufferPtr = BufferOrErr.get(); |
Jeroen Ketema | d915739 | 2015-08-07 08:31:37 +0000 | [diff] [blame] | 48 | ErrorOr<std::unique_ptr<Module>> ModuleOrErr = |
Jan Vesely | ce29e8c | 2017-09-29 19:06:41 +0000 | [diff] [blame] | 49 | #if HAVE_LLVM > 0x0390 |
Tom Stellard | d83eb34 | 2016-11-14 16:06:33 +0000 | [diff] [blame] | 50 | expectedToErrorOrAndEmitErrors(Context, |
| 51 | parseBitcodeFile(BufferPtr.get()->getMemBufferRef(), Context)); |
Jan Vesely | ce29e8c | 2017-09-29 19:06:41 +0000 | [diff] [blame] | 52 | #else |
| 53 | parseBitcodeFile(BufferPtr.get()->getMemBufferRef(), Context); |
| 54 | #endif |
Tom Stellard | 1d77071 | 2014-12-31 15:27:53 +0000 | [diff] [blame] | 55 | if (std::error_code ec = ModuleOrErr.getError()) |
Tom Stellard | 8a63b15 | 2014-01-20 20:28:48 +0000 | [diff] [blame] | 56 | ErrorMessage = ec.message(); |
Jeroen Ketema | d915739 | 2015-08-07 08:31:37 +0000 | [diff] [blame] | 57 | |
Tom Stellard | 4957e40 | 2015-06-24 17:03:50 +0000 | [diff] [blame] | 58 | M = ModuleOrErr.get().release(); |
Tom Stellard | 8a63b15 | 2014-01-20 20:28:48 +0000 | [diff] [blame] | 59 | } |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Tom Stellard | 4957e40 | 2015-06-24 17:03:50 +0000 | [diff] [blame] | 62 | if (!M) { |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 +0000 | [diff] [blame] | 63 | 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 Arsenault | 7ef7e6a | 2016-08-25 00:25:10 +0000 | [diff] [blame] | 71 | // 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 Collingbourne | d5395fb | 2012-01-08 22:09:58 +0000 | [diff] [blame] | 78 | // 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 Danzer | a10b492 | 2014-08-28 06:19:33 +0000 | [diff] [blame] | 95 | std::error_code EC; |
Jan Vesely | 7600520 | 2017-09-25 16:04:37 +0000 | [diff] [blame] | 96 | #if HAVE_LLVM >= 0x0600 |
Reid Kleckner | 3fc649c | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 97 | std::unique_ptr<ToolOutputFile> Out( |
| 98 | new ToolOutputFile(OutputFilename, EC, sys::fs::F_None)); |
Jan Vesely | 7600520 | 2017-09-25 16:04:37 +0000 | [diff] [blame] | 99 | #else |
| 100 | std::unique_ptr<tool_output_file> Out( |
| 101 | new tool_output_file(OutputFilename, EC, sys::fs::F_None)); |
| 102 | #endif |
Michel Danzer | a10b492 | 2014-08-28 06:19:33 +0000 | [diff] [blame] | 103 | if (EC) { |
| 104 | errs() << EC.message() << '\n'; |
| 105 | exit(1); |
| 106 | } |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 +0000 | [diff] [blame] | 107 | |
Jan Vesely | 86db430 | 2018-02-23 07:37:03 +0000 | [diff] [blame] | 108 | #if HAVE_LLVM >= 0x0700 |
| 109 | WriteBitcodeToFile(*M, Out->os()); |
| 110 | #else |
Tom Stellard | 4957e40 | 2015-06-24 17:03:50 +0000 | [diff] [blame] | 111 | WriteBitcodeToFile(M, Out->os()); |
Jan Vesely | 86db430 | 2018-02-23 07:37:03 +0000 | [diff] [blame] | 112 | #endif |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 +0000 | [diff] [blame] | 113 | |
| 114 | // Declare success. |
| 115 | Out->keep(); |
| 116 | return 0; |
| 117 | } |
| 118 | |