Zonr Chang | c383a50 | 2010-10-12 01:52:08 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Zonr Chang | a02010c | 2010-10-11 20:54:28 +0800 | [diff] [blame] | 17 | #include <list> |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 18 | #include <memory> |
| 19 | #include <string> |
| 20 | #include <vector> |
| 21 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame^] | 22 | #include "llvm/Analysis/Verifier.h" |
| 23 | |
| 24 | #include "llvm/Bitcode/ReaderWriter.h" |
| 25 | |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 26 | #include "llvm/Linker.h" |
| 27 | #include "llvm/LLVMContext.h" |
| 28 | #include "llvm/Metadata.h" |
| 29 | #include "llvm/Module.h" |
Zonr Chang | 7f2f385 | 2010-10-07 19:12:23 +0800 | [diff] [blame] | 30 | |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 31 | #include "llvm/Support/CommandLine.h" |
| 32 | #include "llvm/Support/ManagedStatic.h" |
| 33 | #include "llvm/Support/MemoryBuffer.h" |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 34 | #include "llvm/Support/StandardPasses.h" |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 35 | #include "llvm/Support/raw_ostream.h" |
Zonr Chang | 7f2f385 | 2010-10-07 19:12:23 +0800 | [diff] [blame] | 36 | |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 37 | #include "llvm/Target/TargetData.h" |
Zonr Chang | 7f2f385 | 2010-10-07 19:12:23 +0800 | [diff] [blame] | 38 | |
| 39 | #include "slang_rs_metadata.h" |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 40 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame^] | 41 | using llvm::errs; |
| 42 | using llvm::LLVMContext; |
| 43 | using llvm::MemoryBuffer; |
| 44 | using llvm::Module; |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 45 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame^] | 46 | static llvm::cl::list<std::string> |
| 47 | InputFilenames(llvm::cl::Positional, llvm::cl::OneOrMore, |
| 48 | llvm::cl::desc("<input bitcode files>")); |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 49 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame^] | 50 | static llvm::cl::list<std::string> |
| 51 | OutputFilenames("o", llvm::cl::desc("Override output filename"), |
| 52 | llvm::cl::value_desc("<output bitcode file>")); |
Zonr Chang | a02010c | 2010-10-11 20:54:28 +0800 | [diff] [blame] | 53 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame^] | 54 | static llvm::cl::opt<bool> |
| 55 | NoStdLib("nostdlib", llvm::cl::desc("Don't link RS default libraries")); |
Zonr Chang | a02010c | 2010-10-11 20:54:28 +0800 | [diff] [blame] | 56 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame^] | 57 | static llvm::cl::list<std::string> |
| 58 | AdditionalLibs("l", llvm::cl::Prefix, |
| 59 | llvm::cl::desc("Specify additional libraries to link to"), |
| 60 | llvm::cl::value_desc("<library bitcode>")); |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 61 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame^] | 62 | static bool GetExportSymbolNames(llvm::NamedMDNode *N, |
Zonr Chang | 7f2f385 | 2010-10-07 19:12:23 +0800 | [diff] [blame] | 63 | unsigned NameOpIdx, |
| 64 | std::vector<const char *> &Names) { |
| 65 | if (N == NULL) |
| 66 | return true; |
Shih-wei Liao | 9c9bbc8 | 2010-08-18 03:08:44 -0700 | [diff] [blame] | 67 | |
Zonr Chang | 7f2f385 | 2010-10-07 19:12:23 +0800 | [diff] [blame] | 68 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame^] | 69 | llvm::MDNode *V = N->getOperand(i); |
Zonr Chang | 7f2f385 | 2010-10-07 19:12:23 +0800 | [diff] [blame] | 70 | if (V == NULL) |
| 71 | continue; |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 72 | |
Zonr Chang | 7f2f385 | 2010-10-07 19:12:23 +0800 | [diff] [blame] | 73 | if (V->getNumOperands() < (NameOpIdx + 1)) { |
| 74 | errs() << "Invalid metadata spec of " << N->getName() |
| 75 | << " in RenderScript executable. (#op)\n"; |
| 76 | return false; |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame^] | 79 | llvm::MDString *Name = |
| 80 | llvm::dyn_cast<llvm::MDString>(V->getOperand(NameOpIdx)); |
Zonr Chang | 7f2f385 | 2010-10-07 19:12:23 +0800 | [diff] [blame] | 81 | if (Name == NULL) { |
| 82 | errs() << "Invalid metadata spec of " << N->getName() |
| 83 | << " in RenderScript executable. (#name)\n"; |
| 84 | return false; |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Zonr Chang | 7f2f385 | 2010-10-07 19:12:23 +0800 | [diff] [blame] | 87 | Names.push_back(Name->getString().data()); |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 88 | } |
Zonr Chang | 7f2f385 | 2010-10-07 19:12:23 +0800 | [diff] [blame] | 89 | return true; |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Zonr Chang | 7f2f385 | 2010-10-07 19:12:23 +0800 | [diff] [blame] | 92 | static bool GetExportSymbols(Module *M, std::vector<const char *> &Names) { |
| 93 | bool Result = true; |
| 94 | // Variables marked as export must be externally visible |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame^] | 95 | if (llvm::NamedMDNode *EV = M->getNamedMetadata(RS_EXPORT_VAR_MN)) |
Zonr Chang | 7f2f385 | 2010-10-07 19:12:23 +0800 | [diff] [blame] | 96 | Result |= GetExportSymbolNames(EV, RS_EXPORT_VAR_NAME, Names); |
| 97 | // So are those exported functions |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame^] | 98 | if (llvm::NamedMDNode *EF = M->getNamedMetadata(RS_EXPORT_FUNC_MN)) |
Zonr Chang | 7f2f385 | 2010-10-07 19:12:23 +0800 | [diff] [blame] | 99 | Result |= GetExportSymbolNames(EF, RS_EXPORT_FUNC_NAME, Names); |
| 100 | return Result; |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Zonr Chang | a02010c | 2010-10-11 20:54:28 +0800 | [diff] [blame] | 103 | static inline MemoryBuffer *LoadFileIntoMemory(const std::string &F) { |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 104 | std::string Err; |
Zonr Chang | a02010c | 2010-10-11 20:54:28 +0800 | [diff] [blame] | 105 | MemoryBuffer *MB = MemoryBuffer::getFile(F, &Err); |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 106 | |
Zonr Chang | a02010c | 2010-10-11 20:54:28 +0800 | [diff] [blame] | 107 | if (MB == NULL) |
| 108 | errs() << "Failed to load `" << F << "' (" << Err << ")\n"; |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 109 | |
Zonr Chang | a02010c | 2010-10-11 20:54:28 +0800 | [diff] [blame] | 110 | return MB; |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Zonr Chang | a02010c | 2010-10-11 20:54:28 +0800 | [diff] [blame] | 113 | static inline Module *ParseBitcodeFromMemoryBuffer(MemoryBuffer *MB, |
| 114 | LLVMContext& Context) { |
| 115 | std::string Err; |
| 116 | Module *M = ParseBitcodeFile(MB, Context, &Err); |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 117 | |
Zonr Chang | a02010c | 2010-10-11 20:54:28 +0800 | [diff] [blame] | 118 | if (M == NULL) |
| 119 | errs() << "Corrupted bitcode file `" << MB->getBufferIdentifier() |
| 120 | << "' (" << Err << ")\n"; |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 121 | |
Zonr Chang | a02010c | 2010-10-11 20:54:28 +0800 | [diff] [blame] | 122 | return M; |
| 123 | } |
| 124 | |
| 125 | // LoadBitcodeFile - Read the specified bitcode file in and return it. |
| 126 | static inline Module *LoadBitcodeFile(const std::string &F, |
| 127 | LLVMContext& Context) { |
| 128 | MemoryBuffer *MB = LoadFileIntoMemory(F); |
| 129 | if (MB == NULL) |
| 130 | return NULL; |
| 131 | |
| 132 | Module *M = ParseBitcodeFromMemoryBuffer(MB, Context); |
| 133 | if (M == NULL) |
| 134 | delete MB; |
| 135 | |
| 136 | return M; |
| 137 | } |
| 138 | |
| 139 | extern const char rslib_bc[]; |
| 140 | extern unsigned rslib_bc_size; |
| 141 | |
| 142 | static bool PreloadLibraries(bool NoStdLib, |
| 143 | const std::vector<std::string> &AdditionalLibs, |
| 144 | std::list<MemoryBuffer *> LibBitcode) { |
| 145 | MemoryBuffer *MB; |
| 146 | |
| 147 | LibBitcode.clear(); |
| 148 | |
| 149 | if (!NoStdLib) { |
| 150 | // rslib.bc |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame^] | 151 | MB = MemoryBuffer::getMemBuffer(llvm::StringRef(rslib_bc, rslib_bc_size), |
Zonr Chang | d124ee6 | 2010-10-22 18:26:25 +0800 | [diff] [blame] | 152 | "rslib.bc"); |
Zonr Chang | a02010c | 2010-10-11 20:54:28 +0800 | [diff] [blame] | 153 | if (MB == NULL) { |
| 154 | errs() << "Failed to load (in-memory) `rslib.bc'!\n"; |
| 155 | return false; |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 156 | } |
Zonr Chang | a02010c | 2010-10-11 20:54:28 +0800 | [diff] [blame] | 157 | |
| 158 | LibBitcode.push_back(MB); |
| 159 | } |
| 160 | |
| 161 | // Load additional libraries |
| 162 | for (std::vector<std::string>::const_iterator |
| 163 | I = AdditionalLibs.begin(), E = AdditionalLibs.end(); |
| 164 | I != E; |
| 165 | I++) { |
| 166 | MB = LoadFileIntoMemory(*I); |
| 167 | if (MB == NULL) |
| 168 | return false; |
| 169 | LibBitcode.push_back(MB); |
| 170 | } |
| 171 | |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | static void UnloadLibraries(std::list<MemoryBuffer *>& LibBitcode) { |
| 176 | for (std::list<MemoryBuffer *>::iterator |
| 177 | I = LibBitcode.begin(), E = LibBitcode.end(); |
| 178 | I != E; |
| 179 | I++) |
| 180 | delete *I; |
| 181 | LibBitcode.clear(); |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | Module *PerformLinking(const std::string &InputFile, |
| 186 | const std::list<MemoryBuffer *> &LibBitcode, |
| 187 | LLVMContext &Context) { |
| 188 | std::string Err; |
| 189 | std::auto_ptr<Module> Composite(LoadBitcodeFile(InputFile, Context)); |
| 190 | |
| 191 | if (Composite.get() == NULL) |
| 192 | return NULL; |
| 193 | |
| 194 | for (std::list<MemoryBuffer *>::const_iterator I = LibBitcode.begin(), |
| 195 | E = LibBitcode.end(); |
| 196 | I != E; |
| 197 | I++) { |
| 198 | Module *Lib = ParseBitcodeFromMemoryBuffer(*I, Context); |
| 199 | if (Lib == NULL) |
| 200 | return NULL; |
| 201 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame^] | 202 | if (llvm::Linker::LinkModules(Composite.get(), Lib, &Err)) { |
Zonr Chang | a02010c | 2010-10-11 20:54:28 +0800 | [diff] [blame] | 203 | errs() << "Failed to link `" << InputFile << "' with library bitcode `" |
| 204 | << (*I)->getBufferIdentifier() << "' (" << Err << ")\n"; |
| 205 | return NULL; |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
Zonr Chang | a02010c | 2010-10-11 20:54:28 +0800 | [diff] [blame] | 209 | return Composite.release(); |
| 210 | } |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 211 | |
Zonr Chang | a02010c | 2010-10-11 20:54:28 +0800 | [diff] [blame] | 212 | bool OptimizeModule(Module *M) { |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame^] | 213 | llvm::PassManager Passes; |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 214 | |
Zonr Chang | a02010c | 2010-10-11 20:54:28 +0800 | [diff] [blame] | 215 | const std::string &ModuleDataLayout = M->getDataLayout(); |
Zonr Chang | 7f2f385 | 2010-10-07 19:12:23 +0800 | [diff] [blame] | 216 | if (!ModuleDataLayout.empty()) |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame^] | 217 | if (llvm::TargetData *TD = new llvm::TargetData(ModuleDataLayout)) |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 218 | Passes.add(TD); |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 219 | |
Zonr Chang | 7f2f385 | 2010-10-07 19:12:23 +0800 | [diff] [blame] | 220 | // Some symbols must not be internalized |
| 221 | std::vector<const char *> ExportList; |
| 222 | ExportList.push_back("init"); |
| 223 | ExportList.push_back("root"); |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 224 | |
Zonr Chang | a02010c | 2010-10-11 20:54:28 +0800 | [diff] [blame] | 225 | if (!GetExportSymbols(M, ExportList)) { |
| 226 | return false; |
| 227 | } |
Shih-wei Liao | 9c9bbc8 | 2010-08-18 03:08:44 -0700 | [diff] [blame] | 228 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame^] | 229 | Passes.add(llvm::createInternalizePass(ExportList)); |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 230 | |
Zonr Chang | a02010c | 2010-10-11 20:54:28 +0800 | [diff] [blame] | 231 | // TODO(zonr): Do we need to run all LTO passes? |
Zonr Chang | 7f2f385 | 2010-10-07 19:12:23 +0800 | [diff] [blame] | 232 | createStandardLTOPasses(&Passes, |
| 233 | /* Internalize = */false, |
| 234 | /* RunInliner = */true, |
| 235 | /* VerifyEach = */false); |
Zonr Chang | a02010c | 2010-10-11 20:54:28 +0800 | [diff] [blame] | 236 | Passes.run(*M); |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 237 | |
Zonr Chang | a02010c | 2010-10-11 20:54:28 +0800 | [diff] [blame] | 238 | return true; |
| 239 | } |
Zonr Chang | 7f2f385 | 2010-10-07 19:12:23 +0800 | [diff] [blame] | 240 | |
Zonr Chang | a02010c | 2010-10-11 20:54:28 +0800 | [diff] [blame] | 241 | int main(int argc, char **argv) { |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame^] | 242 | llvm::llvm_shutdown_obj X; // Call llvm_shutdown() on exit. |
Zonr Chang | a02010c | 2010-10-11 20:54:28 +0800 | [diff] [blame] | 243 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame^] | 244 | llvm::cl::ParseCommandLineOptions(argc, argv, "llvm-rs-link\n"); |
Zonr Chang | a02010c | 2010-10-11 20:54:28 +0800 | [diff] [blame] | 245 | |
| 246 | std::list<MemoryBuffer *> LibBitcode; |
| 247 | |
| 248 | if (!PreloadLibraries(NoStdLib, AdditionalLibs, LibBitcode)) |
| 249 | return 1; |
| 250 | |
| 251 | // No libraries specified to be linked |
| 252 | if (LibBitcode.size() == 0) |
| 253 | return 0; |
| 254 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame^] | 255 | LLVMContext &Context = llvm::getGlobalContext(); |
Zonr Chang | a02010c | 2010-10-11 20:54:28 +0800 | [diff] [blame] | 256 | bool HasError = true; |
| 257 | std::string Err; |
| 258 | |
| 259 | for (unsigned i = 0, e = InputFilenames.size(); i != e; i++) { |
| 260 | std::auto_ptr<Module> Linked( |
| 261 | PerformLinking(InputFilenames[i], LibBitcode, Context)); |
| 262 | |
| 263 | // Failed to link with InputFilenames[i] with LibBitcode |
| 264 | if (Linked.get() == NULL) |
| 265 | break; |
| 266 | |
| 267 | // Verify linked module |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame^] | 268 | if (verifyModule(*Linked, llvm::ReturnStatusAction, &Err)) { |
Zonr Chang | a02010c | 2010-10-11 20:54:28 +0800 | [diff] [blame] | 269 | errs() << InputFilenames[i] << " linked, but does not verify as " |
| 270 | "correct! (" << Err << ")\n"; |
| 271 | break; |
| 272 | } |
| 273 | |
| 274 | if (!OptimizeModule(Linked.get())) |
| 275 | break; |
| 276 | |
| 277 | // Write out the module |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame^] | 278 | llvm::tool_output_file Out(InputFilenames[i].c_str(), Err, |
| 279 | llvm::raw_fd_ostream::F_Binary); |
Zonr Chang | a02010c | 2010-10-11 20:54:28 +0800 | [diff] [blame] | 280 | |
| 281 | if (!Err.empty()) { |
| 282 | errs() << InputFilenames[i] << " linked, but failed to write out! " |
| 283 | "(" << Err << ")\n"; |
| 284 | break; |
| 285 | } |
| 286 | |
| 287 | WriteBitcodeToFile(Linked.get(), Out); |
| 288 | |
| 289 | Out.keep(); |
| 290 | Linked.reset(); |
| 291 | |
| 292 | if (i == (InputFilenames.size() - 1)) |
| 293 | // This is the last file and no error occured. |
| 294 | HasError = false; |
| 295 | } |
| 296 | |
| 297 | UnloadLibraries(LibBitcode); |
| 298 | |
| 299 | return HasError; |
Shih-wei Liao | 835a7b7 | 2010-08-06 02:29:11 -0700 | [diff] [blame] | 300 | } |