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 | |
Tim Murray | c2074ca | 2014-04-08 15:39:08 -0700 | [diff] [blame] | 23 | #include <llvm/ADT/OwningPtr.h> |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 24 | #include <llvm/ADT/STLExtras.h> |
| 25 | #include <llvm/ADT/SmallString.h> |
| 26 | #include <llvm/Config/config.h> |
| 27 | #include <llvm/Support/CommandLine.h> |
| 28 | #include <llvm/Support/FileSystem.h> |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 29 | #include <llvm/Support/MemoryBuffer.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> |
| 32 | #include <llvm/Support/system_error.h> |
| 33 | |
| 34 | #include <bcc/BCCContext.h> |
| 35 | #include <bcc/Compiler.h> |
| 36 | #include <bcc/Config/BuildInfo.h> |
| 37 | #include <bcc/Config/Config.h> |
Shih-wei Liao | 09ca954 | 2013-01-25 17:00:08 -0800 | [diff] [blame] | 38 | #include <bcc/ExecutionEngine/CompilerRTSymbolResolver.h> |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 39 | #include <bcc/ExecutionEngine/ObjectLoader.h> |
| 40 | #include <bcc/ExecutionEngine/SymbolResolverProxy.h> |
| 41 | #include <bcc/ExecutionEngine/SymbolResolvers.h> |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 42 | #include <bcc/Renderscript/RSCompilerDriver.h> |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 43 | #include <bcc/Script.h> |
| 44 | #include <bcc/Source.h> |
| 45 | #include <bcc/Support/CompilerConfig.h> |
| 46 | #include <bcc/Support/Initialization.h> |
| 47 | #include <bcc/Support/InputFile.h> |
| 48 | #include <bcc/Support/OutputFile.h> |
| 49 | #include <bcc/Support/TargetCompilerConfigs.h> |
| 50 | |
| 51 | using namespace bcc; |
| 52 | |
Stephen Hines | c3437f0 | 2014-01-30 17:57:21 -0800 | [diff] [blame] | 53 | #define STR2(a) #a |
| 54 | #define STR(a) STR2(a) |
| 55 | |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 56 | //===----------------------------------------------------------------------===// |
| 57 | // General Options |
| 58 | //===----------------------------------------------------------------------===// |
| 59 | namespace { |
| 60 | |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 61 | llvm::cl::opt<std::string> |
| 62 | OptInputFilename(llvm::cl::Positional, llvm::cl::ValueRequired, |
| 63 | llvm::cl::desc("<input bitcode file>")); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 64 | |
| 65 | llvm::cl::opt<std::string> |
| 66 | OptOutputFilename("o", llvm::cl::desc("Specify the output filename"), |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 67 | llvm::cl::value_desc("filename"), |
| 68 | llvm::cl::init("bcc_output")); |
| 69 | |
| 70 | llvm::cl::opt<std::string> |
| 71 | OptBCLibFilename("bclib", llvm::cl::desc("Specify the bclib filename"), |
| 72 | llvm::cl::value_desc("bclib")); |
| 73 | |
| 74 | llvm::cl::opt<std::string> |
| 75 | OptOutputPath("output_path", llvm::cl::desc("Specify the output path"), |
| 76 | llvm::cl::value_desc("output path"), |
| 77 | llvm::cl::init(".")); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 78 | |
Tobias Grosser | 7b980e1 | 2013-06-20 10:12:13 -0700 | [diff] [blame] | 79 | llvm::cl::opt<bool> |
| 80 | OptEmitLLVM("emit-llvm", |
| 81 | llvm::cl::desc("Emit an LLVM-IR version of the generated program")); |
| 82 | |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 83 | llvm::cl::opt<std::string> |
| 84 | OptTargetTriple("mtriple", |
| 85 | llvm::cl::desc("Specify the target triple (default: " |
| 86 | DEFAULT_TARGET_TRIPLE_STRING ")"), |
| 87 | llvm::cl::init(DEFAULT_TARGET_TRIPLE_STRING), |
| 88 | llvm::cl::value_desc("triple")); |
| 89 | |
| 90 | llvm::cl::alias OptTargetTripleC("C", llvm::cl::NotHidden, |
| 91 | llvm::cl::desc("Alias for -mtriple"), |
| 92 | llvm::cl::aliasopt(OptTargetTriple)); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 93 | |
Stephen Hines | c3437f0 | 2014-01-30 17:57:21 -0800 | [diff] [blame] | 94 | llvm::cl::opt<bool> |
| 95 | OptRSDebugContext("rs-debug-ctx", |
| 96 | llvm::cl::desc("Enable build to work with a RenderScript debug context")); |
| 97 | |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 98 | //===----------------------------------------------------------------------===// |
| 99 | // Compiler Options |
| 100 | //===----------------------------------------------------------------------===// |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 101 | |
Stephen Hines | 7e9c185 | 2013-06-21 19:25:34 -0700 | [diff] [blame] | 102 | // RenderScript uses -O3 by default |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 103 | llvm::cl::opt<char> |
| 104 | OptOptLevel("O", llvm::cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] " |
Stephen Hines | 7e9c185 | 2013-06-21 19:25:34 -0700 | [diff] [blame] | 105 | "(default: -O3)"), |
| 106 | llvm::cl::Prefix, llvm::cl::ZeroOrMore, llvm::cl::init('3')); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 107 | |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 108 | // Override "bcc -version" since the LLVM version information is not correct on |
| 109 | // Android build. |
| 110 | void BCCVersionPrinter() { |
| 111 | llvm::raw_ostream &os = llvm::outs(); |
| 112 | os << "libbcc (The Android Open Source Project, http://www.android.com/):\n" |
| 113 | << " Build time: " << BuildInfo::GetBuildTime() << "\n" |
| 114 | << " Build revision: " << BuildInfo::GetBuildRev() << "\n" |
| 115 | << " Build source blob: " << BuildInfo::GetBuildSourceBlob() << "\n" |
| 116 | << " Default target: " << DEFAULT_TARGET_TRIPLE_STRING << "\n"; |
| 117 | |
| 118 | os << "\n"; |
| 119 | |
| 120 | os << "LLVM (http://llvm.org/):\n" |
| 121 | << " Version: " << PACKAGE_VERSION << "\n"; |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | } // end anonymous namespace |
| 126 | |
| 127 | static inline |
Stephen Hines | 7e9c185 | 2013-06-21 19:25:34 -0700 | [diff] [blame] | 128 | bool ConfigCompiler(RSCompilerDriver &pRSCD) { |
| 129 | RSCompiler *RSC = pRSCD.getCompiler(); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 130 | CompilerConfig *config = NULL; |
| 131 | |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 132 | config = new (std::nothrow) CompilerConfig(OptTargetTriple); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 133 | if (config == NULL) { |
| 134 | llvm::errs() << "Out of memory when create the compiler configuration!\n"; |
| 135 | return false; |
| 136 | } |
| 137 | |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 138 | switch (OptOptLevel) { |
| 139 | case '0': config->setOptimizationLevel(llvm::CodeGenOpt::None); break; |
| 140 | case '1': config->setOptimizationLevel(llvm::CodeGenOpt::Less); break; |
Stephen Hines | 7e9c185 | 2013-06-21 19:25:34 -0700 | [diff] [blame] | 141 | case '2': config->setOptimizationLevel(llvm::CodeGenOpt::Default); break; |
| 142 | case '3': |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 143 | default: { |
Stephen Hines | 7e9c185 | 2013-06-21 19:25:34 -0700 | [diff] [blame] | 144 | config->setOptimizationLevel(llvm::CodeGenOpt::Aggressive); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 145 | break; |
| 146 | } |
| 147 | } |
| 148 | |
Stephen Hines | 7e9c185 | 2013-06-21 19:25:34 -0700 | [diff] [blame] | 149 | pRSCD.setConfig(config); |
| 150 | Compiler::ErrorCode result = RSC->config(*config); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 151 | |
Stephen Hines | c3437f0 | 2014-01-30 17:57:21 -0800 | [diff] [blame] | 152 | if (OptRSDebugContext) { |
| 153 | pRSCD.setDebugContext(true); |
| 154 | } |
| 155 | |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 156 | if (result != Compiler::kSuccess) { |
| 157 | llvm::errs() << "Failed to configure the compiler! (detail: " |
| 158 | << Compiler::GetErrorString(result) << ")\n"; |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | return true; |
| 163 | } |
| 164 | |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 165 | int main(int argc, char **argv) { |
| 166 | llvm::cl::SetVersionPrinter(BCCVersionPrinter); |
| 167 | llvm::cl::ParseCommandLineOptions(argc, argv); |
| 168 | init::Initialize(); |
| 169 | |
| 170 | BCCContext context; |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 171 | RSCompilerDriver RSCD; |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 172 | |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 173 | llvm::OwningPtr<llvm::MemoryBuffer> input_data; |
| 174 | |
| 175 | llvm::error_code ec = |
| 176 | llvm::MemoryBuffer::getFile(OptInputFilename.c_str(), input_data); |
| 177 | if (ec != llvm::error_code::success()) { |
| 178 | ALOGE("Failed to load bitcode from path %s! (%s)", |
| 179 | OptInputFilename.c_str(), ec.message().c_str()); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 180 | return EXIT_FAILURE; |
| 181 | } |
| 182 | |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 183 | llvm::MemoryBuffer *input_memory = input_data.take(); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 184 | |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 185 | const char *bitcode = input_memory->getBufferStart(); |
| 186 | size_t bitcodeSize = input_memory->getBufferSize(); |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 187 | |
Stephen Hines | 7e9c185 | 2013-06-21 19:25:34 -0700 | [diff] [blame] | 188 | if (!ConfigCompiler(RSCD)) { |
| 189 | ALOGE("Failed to configure compiler"); |
| 190 | return EXIT_FAILURE; |
| 191 | } |
Stephen Hines | c3437f0 | 2014-01-30 17:57:21 -0800 | [diff] [blame] | 192 | |
| 193 | // Attempt to dynamically initialize the compiler driver if such a function |
| 194 | // is present. It is only present if passed via "-load libFOO.so". |
| 195 | RSCompilerDriverInit_t rscdi = (RSCompilerDriverInit_t) |
| 196 | dlsym(RTLD_DEFAULT, STR(RS_COMPILER_DRIVER_INIT_FN)); |
| 197 | if (rscdi != NULL) { |
| 198 | rscdi(&RSCD); |
| 199 | } |
| 200 | |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 201 | bool built = RSCD.build(context, OptOutputPath.c_str(), |
| 202 | OptOutputFilename.c_str(), bitcode, bitcodeSize, |
Tobias Grosser | 7b980e1 | 2013-06-20 10:12:13 -0700 | [diff] [blame] | 203 | OptBCLibFilename.c_str(), NULL, OptEmitLLVM); |
Stephen Hines | 8be8dba | 2013-06-18 09:53:43 -0700 | [diff] [blame] | 204 | |
| 205 | if (!built) { |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 206 | return EXIT_FAILURE; |
| 207 | } |
| 208 | |
Shih-wei Liao | d577d11 | 2012-04-25 04:06:29 -0700 | [diff] [blame] | 209 | return EXIT_SUCCESS; |
| 210 | } |