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     std::unique_ptr<MemoryBuffer> &BufferPtr = BufferOrErr.get();
40     if (std::error_code  ec = BufferOrErr.getError())
41       ErrorMessage = ec.message();
42     else {
43       ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
44           parseBitcodeFile(BufferPtr.get()->getMemBufferRef(), Context);
45       if (std::error_code ec = ModuleOrErr.getError())
46         ErrorMessage = ec.message();
47 
48       M = ModuleOrErr.get().release();
49     }
50   }
51 
52   if (!M) {
53     errs() << argv[0] << ": ";
54     if (ErrorMessage.size())
55       errs() << ErrorMessage << "\n";
56     else
57       errs() << "bitcode didn't read correctly.\n";
58     return 1;
59   }
60 
61   // Strip the OpenCL version metadata. There are a lot of linked
62   // modules in the library build, each spamming the same
63   // version. This may also report a different version than the user
64   // program is using. This should probably be uniqued when linking.
65   if (NamedMDNode *OCLVersion = M->getNamedMetadata("opencl.ocl.version"))
66       M->eraseNamedMetadata(OCLVersion);
67 
68   // Set linkage of every external definition to linkonce_odr.
69   for (Module::iterator i = M->begin(), e = M->end(); i != e; ++i) {
70     if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage)
71       i->setLinkage(GlobalValue::LinkOnceODRLinkage);
72   }
73 
74   for (Module::global_iterator i = M->global_begin(), e = M->global_end();
75        i != e; ++i) {
76     if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage)
77       i->setLinkage(GlobalValue::LinkOnceODRLinkage);
78   }
79 
80   if (OutputFilename.empty()) {
81     errs() << "no output file\n";
82     return 1;
83   }
84 
85   std::error_code EC;
86   std::unique_ptr<tool_output_file> Out
87   (new tool_output_file(OutputFilename, EC, sys::fs::F_None));
88   if (EC) {
89     errs() << EC.message() << '\n';
90     exit(1);
91   }
92 
93   WriteBitcodeToFile(M, Out->os());
94 
95   // Declare success.
96   Out->keep();
97   return 0;
98 }
99 
100