Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012, 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 | |
| 17 | #include <string> |
| 18 | #include <vector> |
| 19 | |
Stephen Hines | c3437f0 | 2014-01-30 17:57:21 -0800 | [diff] [blame] | 20 | #include <dlfcn.h> |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 21 | #include <stdlib.h> |
| 22 | |
| 23 | #include <llvm/ADT/STLExtras.h> |
| 24 | #include <llvm/ADT/SmallString.h> |
| 25 | #include <llvm/Config/config.h> |
| 26 | #include <llvm/Support/CommandLine.h> |
| 27 | #include <llvm/Support/FileSystem.h> |
Tim Murray | c46a3f5 | 2015-01-08 14:52:15 -0800 | [diff] [blame] | 28 | #include <llvm/Support/ManagedStatic.h> |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 29 | #include <llvm/Support/MemoryBuffer.h> |
Pirama Arumuga Nainar | 1e0557a | 2014-12-02 15:02:18 -0800 | [diff] [blame] | 30 | #include <llvm/Support/Path.h> |
Stephen Hines | c3437f0 | 2014-01-30 17:57:21 -0800 | [diff] [blame] | 31 | #include <llvm/Support/PluginLoader.h> |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 32 | #include <llvm/Support/raw_ostream.h> |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 33 | |
| 34 | #include <bcc/BCCContext.h> |
| 35 | #include <bcc/Compiler.h> |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 36 | #include <bcc/Config/Config.h> |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 37 | #include <bcc/Renderscript/RSCompilerDriver.h> |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 38 | #include <bcc/Script.h> |
| 39 | #include <bcc/Source.h> |
| 40 | #include <bcc/Support/CompilerConfig.h> |
| 41 | #include <bcc/Support/Initialization.h> |
| 42 | #include <bcc/Support/InputFile.h> |
| 43 | #include <bcc/Support/OutputFile.h> |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 44 | |
| 45 | using namespace bcc; |
| 46 | |
Stephen Hines | c3437f0 | 2014-01-30 17:57:21 -0800 | [diff] [blame] | 47 | #define STR2(a) #a |
| 48 | #define STR(a) STR2(a) |
| 49 | |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 50 | //===----------------------------------------------------------------------===// |
| 51 | // General Options |
| 52 | //===----------------------------------------------------------------------===// |
| 53 | namespace { |
| 54 | |
Yang Ni | a4ded13 | 2014-11-17 17:44:08 -0800 | [diff] [blame^] | 55 | llvm::cl::list<std::string> |
| 56 | OptInputFilenames(llvm::cl::Positional, llvm::cl::OneOrMore, |
| 57 | llvm::cl::desc("<input bitcode files>")); |
| 58 | |
| 59 | llvm::cl::list<int> |
| 60 | OptKernelSlots("k", llvm::cl::ZeroOrMore, |
| 61 | llvm::cl::desc("kernel function slot numbers")); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 62 | |
| 63 | llvm::cl::opt<std::string> |
| 64 | OptOutputFilename("o", llvm::cl::desc("Specify the output filename"), |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 65 | llvm::cl::value_desc("filename"), |
| 66 | llvm::cl::init("bcc_output")); |
| 67 | |
| 68 | llvm::cl::opt<std::string> |
| 69 | OptBCLibFilename("bclib", llvm::cl::desc("Specify the bclib filename"), |
| 70 | llvm::cl::value_desc("bclib")); |
| 71 | |
| 72 | llvm::cl::opt<std::string> |
| 73 | OptOutputPath("output_path", llvm::cl::desc("Specify the output path"), |
| 74 | llvm::cl::value_desc("output path"), |
| 75 | llvm::cl::init(".")); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 76 | |
Tobias Grosser | 7b980e1 | 2013-06-20 10:12:13 -0700 | [diff] [blame] | 77 | llvm::cl::opt<bool> |
| 78 | OptEmitLLVM("emit-llvm", |
| 79 | llvm::cl::desc("Emit an LLVM-IR version of the generated program")); |
| 80 | |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 81 | llvm::cl::opt<std::string> |
| 82 | OptTargetTriple("mtriple", |
| 83 | llvm::cl::desc("Specify the target triple (default: " |
| 84 | DEFAULT_TARGET_TRIPLE_STRING ")"), |
| 85 | llvm::cl::init(DEFAULT_TARGET_TRIPLE_STRING), |
| 86 | llvm::cl::value_desc("triple")); |
| 87 | |
| 88 | llvm::cl::alias OptTargetTripleC("C", llvm::cl::NotHidden, |
| 89 | llvm::cl::desc("Alias for -mtriple"), |
| 90 | llvm::cl::aliasopt(OptTargetTriple)); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 91 | |
Stephen Hines | c3437f0 | 2014-01-30 17:57:21 -0800 | [diff] [blame] | 92 | llvm::cl::opt<bool> |
| 93 | OptRSDebugContext("rs-debug-ctx", |
| 94 | llvm::cl::desc("Enable build to work with a RenderScript debug context")); |
| 95 | |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 96 | //===----------------------------------------------------------------------===// |
| 97 | // Compiler Options |
| 98 | //===----------------------------------------------------------------------===// |
Pirama Arumuga Nainar | 1e0557a | 2014-12-02 15:02:18 -0800 | [diff] [blame] | 99 | llvm::cl::opt<bool> |
| 100 | OptPIC("fPIC", llvm::cl::desc("Generate fully relocatable, position independent" |
| 101 | " code")); |
| 102 | |
| 103 | // If set, use buildForCompatLib to embed RS symbol information into the object |
| 104 | // file. The information is stored in the .rs.info variable. This option is |
| 105 | // to be used in tandem with -fPIC. |
| 106 | llvm::cl::opt<bool> |
| 107 | OptEmbedRSInfo("embedRSInfo", |
| 108 | llvm::cl::desc("Embed RS Info into the object file instead of generating" |
| 109 | " a separate .o.info file")); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 110 | |
Stephen Hines | 7e9c185 | 2013-06-21 19:25:34 -0700 | [diff] [blame] | 111 | // RenderScript uses -O3 by default |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 112 | llvm::cl::opt<char> |
| 113 | OptOptLevel("O", llvm::cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] " |
Stephen Hines | 7e9c185 | 2013-06-21 19:25:34 -0700 | [diff] [blame] | 114 | "(default: -O3)"), |
| 115 | llvm::cl::Prefix, llvm::cl::ZeroOrMore, llvm::cl::init('3')); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 116 | |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 117 | // Override "bcc -version" since the LLVM version information is not correct on |
| 118 | // Android build. |
| 119 | void BCCVersionPrinter() { |
| 120 | llvm::raw_ostream &os = llvm::outs(); |
| 121 | os << "libbcc (The Android Open Source Project, http://www.android.com/):\n" |
Jean-Luc Brouillet | c5e607a | 2014-06-18 18:14:02 -0700 | [diff] [blame] | 122 | << " Default target: " << DEFAULT_TARGET_TRIPLE_STRING << "\n\n" |
| 123 | << "LLVM (http://llvm.org/):\n" |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 124 | << " Version: " << PACKAGE_VERSION << "\n"; |
| 125 | return; |
| 126 | } |
| 127 | |
Yang Ni | a4ded13 | 2014-11-17 17:44:08 -0800 | [diff] [blame^] | 128 | bool fuseKernels(BCCContext& Context, RSCompilerDriver& RSCD) { |
| 129 | if (OptInputFilenames.size() != OptKernelSlots.size()) { |
| 130 | llvm::errs() << "Mismatching number of input files and kernel slots.\n"; |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | std::vector<const bcc::Source*> sources; |
| 135 | std::vector<int> slots; |
| 136 | |
| 137 | for (unsigned i = 0; i < OptInputFilenames.size(); ++i) { |
| 138 | const bcc::Source* source = |
| 139 | bcc::Source::CreateFromFile(Context, OptInputFilenames[i]); |
| 140 | if (!source) { |
| 141 | llvm::errs() << "Error loading file '" << OptInputFilenames[i]<< "'\n"; |
| 142 | return false; |
| 143 | } |
| 144 | int slot = OptKernelSlots[i]; |
| 145 | |
| 146 | sources.push_back(source); |
| 147 | slots.push_back(slot); |
| 148 | } |
| 149 | |
| 150 | std::string outputFilepath(OptOutputPath); |
| 151 | outputFilepath.append("/"); |
| 152 | outputFilepath.append(OptOutputFilename); |
| 153 | |
| 154 | bool success = RSCD.buildScriptGroup( |
| 155 | Context, outputFilepath.c_str(), OptBCLibFilename.c_str(), sources, |
| 156 | slots, true); |
| 157 | |
| 158 | return success; |
| 159 | } |
| 160 | |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 161 | } // end anonymous namespace |
| 162 | |
| 163 | static inline |
Stephen Hines | 7e9c185 | 2013-06-21 19:25:34 -0700 | [diff] [blame] | 164 | bool ConfigCompiler(RSCompilerDriver &pRSCD) { |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 165 | Compiler *RSC = pRSCD.getCompiler(); |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 166 | CompilerConfig *config = nullptr; |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 167 | |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 168 | config = new (std::nothrow) CompilerConfig(OptTargetTriple); |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 169 | if (config == nullptr) { |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 170 | llvm::errs() << "Out of memory when create the compiler configuration!\n"; |
| 171 | return false; |
| 172 | } |
| 173 | |
Jian Li | 602ce03 | 2014-09-03 18:54:26 +0800 | [diff] [blame] | 174 | // llvm3.5 has removed the auto-detect feature for x86 subtarget, |
| 175 | // so set features explicitly in bcc. |
| 176 | if ((config->getTriple().find("i686") != std::string::npos) || |
| 177 | (config->getTriple().find("x86_64") != std::string::npos)) { |
| 178 | std::vector<std::string> fv; |
| 179 | |
| 180 | #if defined(__SSE3__) |
| 181 | fv.push_back("+sse3"); |
| 182 | #endif |
| 183 | #if defined(__SSSE3__) |
| 184 | fv.push_back("+ssse3"); |
| 185 | #endif |
| 186 | #if defined(__SSE4_1__) |
| 187 | fv.push_back("+sse4.1"); |
| 188 | #endif |
| 189 | #if defined(__SSE4_2__) |
| 190 | fv.push_back("+sse4.2"); |
| 191 | #endif |
| 192 | |
| 193 | if (fv.size()) { |
| 194 | config->setFeatureString(fv); |
| 195 | } |
| 196 | } |
| 197 | |
Pirama Arumuga Nainar | 1e0557a | 2014-12-02 15:02:18 -0800 | [diff] [blame] | 198 | if (OptPIC) { |
| 199 | config->setRelocationModel(llvm::Reloc::PIC_); |
| 200 | |
| 201 | // For x86_64, CodeModel needs to be small if PIC_ reloc is used. |
| 202 | // Otherwise, we end up with TEXTRELs in the shared library. |
| 203 | if (config->getTriple().find("x86_64") != std::string::npos) { |
| 204 | config->setCodeModel(llvm::CodeModel::Small); |
| 205 | } |
| 206 | } |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 207 | switch (OptOptLevel) { |
| 208 | case '0': config->setOptimizationLevel(llvm::CodeGenOpt::None); break; |
| 209 | case '1': config->setOptimizationLevel(llvm::CodeGenOpt::Less); break; |
Stephen Hines | 7e9c185 | 2013-06-21 19:25:34 -0700 | [diff] [blame] | 210 | case '2': config->setOptimizationLevel(llvm::CodeGenOpt::Default); break; |
| 211 | case '3': |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 212 | default: { |
Stephen Hines | 7e9c185 | 2013-06-21 19:25:34 -0700 | [diff] [blame] | 213 | config->setOptimizationLevel(llvm::CodeGenOpt::Aggressive); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 214 | break; |
| 215 | } |
| 216 | } |
| 217 | |
Stephen Hines | 7e9c185 | 2013-06-21 19:25:34 -0700 | [diff] [blame] | 218 | pRSCD.setConfig(config); |
| 219 | Compiler::ErrorCode result = RSC->config(*config); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 220 | |
Stephen Hines | c3437f0 | 2014-01-30 17:57:21 -0800 | [diff] [blame] | 221 | if (OptRSDebugContext) { |
| 222 | pRSCD.setDebugContext(true); |
| 223 | } |
| 224 | |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 225 | if (result != Compiler::kSuccess) { |
| 226 | llvm::errs() << "Failed to configure the compiler! (detail: " |
| 227 | << Compiler::GetErrorString(result) << ")\n"; |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | return true; |
| 232 | } |
| 233 | |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 234 | int main(int argc, char **argv) { |
Tim Murray | c46a3f5 | 2015-01-08 14:52:15 -0800 | [diff] [blame] | 235 | |
| 236 | llvm::llvm_shutdown_obj Y; |
| 237 | init::Initialize(); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 238 | llvm::cl::SetVersionPrinter(BCCVersionPrinter); |
| 239 | llvm::cl::ParseCommandLineOptions(argc, argv); |
Jean-Luc Brouillet | f2ac317 | 2014-06-25 18:21:36 -0700 | [diff] [blame] | 240 | std::string commandLine = bcc::getCommandLine(argc, argv); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 241 | |
| 242 | BCCContext context; |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 243 | RSCompilerDriver RSCD; |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 244 | |
Jean-Luc Brouillet | c5e607a | 2014-06-18 18:14:02 -0700 | [diff] [blame] | 245 | if (OptBCLibFilename.empty()) { |
Yang Ni | a4ded13 | 2014-11-17 17:44:08 -0800 | [diff] [blame^] | 246 | ALOGE("Failed to compile bitcode, -bclib was not specified"); |
Jean-Luc Brouillet | c5e607a | 2014-06-18 18:14:02 -0700 | [diff] [blame] | 247 | return EXIT_FAILURE; |
| 248 | } |
| 249 | |
Stephen Hines | 7e9c185 | 2013-06-21 19:25:34 -0700 | [diff] [blame] | 250 | if (!ConfigCompiler(RSCD)) { |
| 251 | ALOGE("Failed to configure compiler"); |
| 252 | return EXIT_FAILURE; |
| 253 | } |
Stephen Hines | c3437f0 | 2014-01-30 17:57:21 -0800 | [diff] [blame] | 254 | |
| 255 | // Attempt to dynamically initialize the compiler driver if such a function |
| 256 | // is present. It is only present if passed via "-load libFOO.so". |
| 257 | RSCompilerDriverInit_t rscdi = (RSCompilerDriverInit_t) |
| 258 | dlsym(RTLD_DEFAULT, STR(RS_COMPILER_DRIVER_INIT_FN)); |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 259 | if (rscdi != nullptr) { |
Stephen Hines | c3437f0 | 2014-01-30 17:57:21 -0800 | [diff] [blame] | 260 | rscdi(&RSCD); |
| 261 | } |
| 262 | |
Yang Ni | a4ded13 | 2014-11-17 17:44:08 -0800 | [diff] [blame^] | 263 | if (OptInputFilenames.size() > 1) { |
| 264 | bool success = fuseKernels(context, RSCD); |
| 265 | if (!success) { |
| 266 | return EXIT_FAILURE; |
| 267 | } |
| 268 | return EXIT_SUCCESS; |
| 269 | } |
| 270 | |
| 271 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> mb_or_error = |
| 272 | llvm::MemoryBuffer::getFile(OptInputFilenames[0].c_str()); |
| 273 | if (mb_or_error.getError()) { |
| 274 | ALOGE("Failed to load bitcode from path %s! (%s)", |
| 275 | OptInputFilenames[0].c_str(), mb_or_error.getError().message().c_str()); |
| 276 | return EXIT_FAILURE; |
| 277 | } |
| 278 | std::unique_ptr<llvm::MemoryBuffer> input_data = std::move(mb_or_error.get()); |
| 279 | |
| 280 | const char *bitcode = input_data->getBufferStart(); |
| 281 | size_t bitcodeSize = input_data->getBufferSize(); |
| 282 | |
Pirama Arumuga Nainar | 1e0557a | 2014-12-02 15:02:18 -0800 | [diff] [blame] | 283 | if (!OptEmbedRSInfo) { |
| 284 | bool built = RSCD.build(context, OptOutputPath.c_str(), OptOutputFilename.c_str(), bitcode, |
| 285 | bitcodeSize, commandLine.c_str(), OptBCLibFilename.c_str(), nullptr, |
| 286 | OptEmitLLVM); |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 287 | |
Pirama Arumuga Nainar | 1e0557a | 2014-12-02 15:02:18 -0800 | [diff] [blame] | 288 | if (!built) { |
| 289 | return EXIT_FAILURE; |
| 290 | } |
| 291 | } |
| 292 | else { |
| 293 | // embedRSInfo is set. Use buildForCompatLib to embed RS symbol information |
| 294 | // into the .rs.info symbol. |
Yang Ni | a4ded13 | 2014-11-17 17:44:08 -0800 | [diff] [blame^] | 295 | Source *source = Source::CreateFromBuffer(context, OptInputFilenames[0].c_str(), |
Pirama Arumuga Nainar | 1e0557a | 2014-12-02 15:02:18 -0800 | [diff] [blame] | 296 | bitcode, bitcodeSize); |
| 297 | RSScript *s = new (std::nothrow) RSScript(*source); |
| 298 | if (s == nullptr) { |
| 299 | llvm::errs() << "Out of memory when creating script for file `" |
Yang Ni | a4ded13 | 2014-11-17 17:44:08 -0800 | [diff] [blame^] | 300 | << OptInputFilenames[0] << "'!\n"; |
Pirama Arumuga Nainar | 1e0557a | 2014-12-02 15:02:18 -0800 | [diff] [blame] | 301 | delete source; |
| 302 | return EXIT_FAILURE; |
| 303 | } |
| 304 | |
| 305 | llvm::SmallString<80> output(OptOutputPath); |
| 306 | llvm::sys::path::append(output, "/", OptOutputFilename); |
| 307 | llvm::sys::path::replace_extension(output, ".o"); |
| 308 | |
| 309 | if (!RSCD.buildForCompatLib(*s, output.c_str(), OptBCLibFilename.c_str())) { |
| 310 | fprintf(stderr, "Failed to compile script!"); |
| 311 | return EXIT_FAILURE; |
| 312 | } |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 315 | return EXIT_SUCCESS; |
| 316 | } |