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