1 #include "llvm/Bitcode/BitcodeReader.h" 2 #include "llvm/Bitcode/BitcodeWriter.h" 3 #include "llvm/IR/Function.h" 4 #include "llvm/IR/GlobalVariable.h" 5 #include "llvm/IR/LLVMContext.h" 6 #include "llvm/IR/Module.h" 7 #include "llvm/Support/CommandLine.h" 8 #include "llvm/Support/ManagedStatic.h" 9 #include "llvm/Support/MemoryBuffer.h" 10 #include "llvm/Support/FileSystem.h" 11 #include "llvm/Support/raw_ostream.h" 12 #include "llvm/Support/ErrorOr.h" 13 #include "llvm/Support/ToolOutputFile.h" 14 #include "llvm/Config/llvm-config.h" 15 16 #include <system_error> 17 18 using namespace llvm; 19 20 static cl::opt<std::string> 21 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-")); 22 23 static cl::opt<std::string> 24 OutputFilename("o", cl::desc("Output filename"), 25 cl::value_desc("filename")); 26 27 int main(int argc, char **argv) { 28 LLVMContext Context; 29 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; 34 Module *M = nullptr; 35 36 { 37 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr = 38 MemoryBuffer::getFile(InputFilename); 39 if (std::error_code ec = BufferOrErr.getError()) { 40 ErrorMessage = ec.message(); 41 } else { 42 std::unique_ptr<MemoryBuffer> &BufferPtr = BufferOrErr.get(); 43 ErrorOr<std::unique_ptr<Module>> ModuleOrErr = 44 expectedToErrorOrAndEmitErrors(Context, 45 parseBitcodeFile(BufferPtr.get()->getMemBufferRef(), Context)); 46 if (std::error_code ec = ModuleOrErr.getError()) 47 ErrorMessage = ec.message(); 48 49 M = ModuleOrErr.get().release(); 50 } 51 } 52 53 if (!M) { 54 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 62 // 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 69 // 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 86 std::error_code EC; 87 std::unique_ptr<tool_output_file> Out 88 (new tool_output_file(OutputFilename, EC, sys::fs::F_None)); 89 if (EC) { 90 errs() << EC.message() << '\n'; 91 exit(1); 92 } 93 94 WriteBitcodeToFile(M, Out->os()); 95 96 // Declare success. 97 Out->keep(); 98 return 0; 99 } 100 101