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