blob: f1c15cc41d95810fb8311774300da25d1c9b3da4 [file] [log] [blame]
Shih-wei Liaod577d112012-04-25 04:06:29 -07001/*
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 Hinesc3437f02014-01-30 17:57:21 -080020#include <dlfcn.h>
Shih-wei Liaod577d112012-04-25 04:06:29 -070021#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 Murrayc46a3f52015-01-08 14:52:15 -080028#include <llvm/Support/ManagedStatic.h>
Stephen Hines8be8dba2013-06-18 09:53:43 -070029#include <llvm/Support/MemoryBuffer.h>
Pirama Arumuga Nainar1e0557a2014-12-02 15:02:18 -080030#include <llvm/Support/Path.h>
Stephen Hinesc3437f02014-01-30 17:57:21 -080031#include <llvm/Support/PluginLoader.h>
Shih-wei Liaod577d112012-04-25 04:06:29 -070032#include <llvm/Support/raw_ostream.h>
Shih-wei Liaod577d112012-04-25 04:06:29 -070033
34#include <bcc/BCCContext.h>
35#include <bcc/Compiler.h>
Shih-wei Liaod577d112012-04-25 04:06:29 -070036#include <bcc/Config/Config.h>
Shih-wei Liao09ca9542013-01-25 17:00:08 -080037#include <bcc/ExecutionEngine/CompilerRTSymbolResolver.h>
Shih-wei Liaod577d112012-04-25 04:06:29 -070038#include <bcc/ExecutionEngine/ObjectLoader.h>
39#include <bcc/ExecutionEngine/SymbolResolverProxy.h>
40#include <bcc/ExecutionEngine/SymbolResolvers.h>
Stephen Hines8be8dba2013-06-18 09:53:43 -070041#include <bcc/Renderscript/RSCompilerDriver.h>
Shih-wei Liaod577d112012-04-25 04:06:29 -070042#include <bcc/Script.h>
43#include <bcc/Source.h>
44#include <bcc/Support/CompilerConfig.h>
45#include <bcc/Support/Initialization.h>
46#include <bcc/Support/InputFile.h>
47#include <bcc/Support/OutputFile.h>
Shih-wei Liaod577d112012-04-25 04:06:29 -070048
49using namespace bcc;
50
Stephen Hinesc3437f02014-01-30 17:57:21 -080051#define STR2(a) #a
52#define STR(a) STR2(a)
53
Shih-wei Liaod577d112012-04-25 04:06:29 -070054//===----------------------------------------------------------------------===//
55// General Options
56//===----------------------------------------------------------------------===//
57namespace {
58
Stephen Hines8be8dba2013-06-18 09:53:43 -070059llvm::cl::opt<std::string>
60OptInputFilename(llvm::cl::Positional, llvm::cl::ValueRequired,
61 llvm::cl::desc("<input bitcode file>"));
Shih-wei Liaod577d112012-04-25 04:06:29 -070062
63llvm::cl::opt<std::string>
64OptOutputFilename("o", llvm::cl::desc("Specify the output filename"),
Stephen Hines8be8dba2013-06-18 09:53:43 -070065 llvm::cl::value_desc("filename"),
66 llvm::cl::init("bcc_output"));
67
68llvm::cl::opt<std::string>
69OptBCLibFilename("bclib", llvm::cl::desc("Specify the bclib filename"),
70 llvm::cl::value_desc("bclib"));
71
72llvm::cl::opt<std::string>
73OptOutputPath("output_path", llvm::cl::desc("Specify the output path"),
74 llvm::cl::value_desc("output path"),
75 llvm::cl::init("."));
Shih-wei Liaod577d112012-04-25 04:06:29 -070076
Tobias Grosser7b980e12013-06-20 10:12:13 -070077llvm::cl::opt<bool>
78OptEmitLLVM("emit-llvm",
79 llvm::cl::desc("Emit an LLVM-IR version of the generated program"));
80
Shih-wei Liaod577d112012-04-25 04:06:29 -070081llvm::cl::opt<std::string>
82OptTargetTriple("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
88llvm::cl::alias OptTargetTripleC("C", llvm::cl::NotHidden,
89 llvm::cl::desc("Alias for -mtriple"),
90 llvm::cl::aliasopt(OptTargetTriple));
Shih-wei Liaod577d112012-04-25 04:06:29 -070091
Stephen Hinesc3437f02014-01-30 17:57:21 -080092llvm::cl::opt<bool>
93OptRSDebugContext("rs-debug-ctx",
94 llvm::cl::desc("Enable build to work with a RenderScript debug context"));
95
Shih-wei Liaod577d112012-04-25 04:06:29 -070096//===----------------------------------------------------------------------===//
97// Compiler Options
98//===----------------------------------------------------------------------===//
Pirama Arumuga Nainar1e0557a2014-12-02 15:02:18 -080099llvm::cl::opt<bool>
100OptPIC("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.
106llvm::cl::opt<bool>
107OptEmbedRSInfo("embedRSInfo",
108 llvm::cl::desc("Embed RS Info into the object file instead of generating"
109 " a separate .o.info file"));
Shih-wei Liaod577d112012-04-25 04:06:29 -0700110
Stephen Hines7e9c1852013-06-21 19:25:34 -0700111// RenderScript uses -O3 by default
Shih-wei Liaod577d112012-04-25 04:06:29 -0700112llvm::cl::opt<char>
113OptOptLevel("O", llvm::cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
Stephen Hines7e9c1852013-06-21 19:25:34 -0700114 "(default: -O3)"),
115 llvm::cl::Prefix, llvm::cl::ZeroOrMore, llvm::cl::init('3'));
Shih-wei Liaod577d112012-04-25 04:06:29 -0700116
Shih-wei Liaod577d112012-04-25 04:06:29 -0700117// Override "bcc -version" since the LLVM version information is not correct on
118// Android build.
119void BCCVersionPrinter() {
120 llvm::raw_ostream &os = llvm::outs();
121 os << "libbcc (The Android Open Source Project, http://www.android.com/):\n"
Jean-Luc Brouilletc5e607a2014-06-18 18:14:02 -0700122 << " Default target: " << DEFAULT_TARGET_TRIPLE_STRING << "\n\n"
123 << "LLVM (http://llvm.org/):\n"
Shih-wei Liaod577d112012-04-25 04:06:29 -0700124 << " Version: " << PACKAGE_VERSION << "\n";
125 return;
126}
127
128} // end anonymous namespace
129
130static inline
Stephen Hines7e9c1852013-06-21 19:25:34 -0700131bool ConfigCompiler(RSCompilerDriver &pRSCD) {
Chris Wailesb4447cd2014-08-19 16:22:20 -0700132 Compiler *RSC = pRSCD.getCompiler();
Chris Wailes900c6c12014-08-13 15:40:00 -0700133 CompilerConfig *config = nullptr;
Shih-wei Liaod577d112012-04-25 04:06:29 -0700134
Shih-wei Liaod577d112012-04-25 04:06:29 -0700135 config = new (std::nothrow) CompilerConfig(OptTargetTriple);
Chris Wailes900c6c12014-08-13 15:40:00 -0700136 if (config == nullptr) {
Shih-wei Liaod577d112012-04-25 04:06:29 -0700137 llvm::errs() << "Out of memory when create the compiler configuration!\n";
138 return false;
139 }
140
Jian Li602ce032014-09-03 18:54:26 +0800141 // llvm3.5 has removed the auto-detect feature for x86 subtarget,
142 // so set features explicitly in bcc.
143 if ((config->getTriple().find("i686") != std::string::npos) ||
144 (config->getTriple().find("x86_64") != std::string::npos)) {
145 std::vector<std::string> fv;
146
147#if defined(__SSE3__)
148 fv.push_back("+sse3");
149#endif
150#if defined(__SSSE3__)
151 fv.push_back("+ssse3");
152#endif
153#if defined(__SSE4_1__)
154 fv.push_back("+sse4.1");
155#endif
156#if defined(__SSE4_2__)
157 fv.push_back("+sse4.2");
158#endif
159
160 if (fv.size()) {
161 config->setFeatureString(fv);
162 }
163 }
164
Pirama Arumuga Nainar1e0557a2014-12-02 15:02:18 -0800165 if (OptPIC) {
166 config->setRelocationModel(llvm::Reloc::PIC_);
167
168 // For x86_64, CodeModel needs to be small if PIC_ reloc is used.
169 // Otherwise, we end up with TEXTRELs in the shared library.
170 if (config->getTriple().find("x86_64") != std::string::npos) {
171 config->setCodeModel(llvm::CodeModel::Small);
172 }
173 }
Shih-wei Liaod577d112012-04-25 04:06:29 -0700174 switch (OptOptLevel) {
175 case '0': config->setOptimizationLevel(llvm::CodeGenOpt::None); break;
176 case '1': config->setOptimizationLevel(llvm::CodeGenOpt::Less); break;
Stephen Hines7e9c1852013-06-21 19:25:34 -0700177 case '2': config->setOptimizationLevel(llvm::CodeGenOpt::Default); break;
178 case '3':
Shih-wei Liaod577d112012-04-25 04:06:29 -0700179 default: {
Stephen Hines7e9c1852013-06-21 19:25:34 -0700180 config->setOptimizationLevel(llvm::CodeGenOpt::Aggressive);
Shih-wei Liaod577d112012-04-25 04:06:29 -0700181 break;
182 }
183 }
184
Stephen Hines7e9c1852013-06-21 19:25:34 -0700185 pRSCD.setConfig(config);
186 Compiler::ErrorCode result = RSC->config(*config);
Shih-wei Liaod577d112012-04-25 04:06:29 -0700187
Stephen Hinesc3437f02014-01-30 17:57:21 -0800188 if (OptRSDebugContext) {
189 pRSCD.setDebugContext(true);
190 }
191
Shih-wei Liaod577d112012-04-25 04:06:29 -0700192 if (result != Compiler::kSuccess) {
193 llvm::errs() << "Failed to configure the compiler! (detail: "
194 << Compiler::GetErrorString(result) << ")\n";
195 return false;
196 }
197
198 return true;
199}
200
Shih-wei Liaod577d112012-04-25 04:06:29 -0700201int main(int argc, char **argv) {
Tim Murrayc46a3f52015-01-08 14:52:15 -0800202
203 llvm::llvm_shutdown_obj Y;
204 init::Initialize();
Shih-wei Liaod577d112012-04-25 04:06:29 -0700205 llvm::cl::SetVersionPrinter(BCCVersionPrinter);
206 llvm::cl::ParseCommandLineOptions(argc, argv);
Jean-Luc Brouilletf2ac3172014-06-25 18:21:36 -0700207 std::string commandLine = bcc::getCommandLine(argc, argv);
Shih-wei Liaod577d112012-04-25 04:06:29 -0700208
209 BCCContext context;
Stephen Hines8be8dba2013-06-18 09:53:43 -0700210 RSCompilerDriver RSCD;
Shih-wei Liaod577d112012-04-25 04:06:29 -0700211
Jean-Luc Brouilletc5e607a2014-06-18 18:14:02 -0700212 if (OptBCLibFilename.empty()) {
213 ALOGE("Failed to compile bit code, -bclib was not specified");
214 return EXIT_FAILURE;
215 }
216
Stephen Hinesd0993af2014-07-15 16:49:25 -0700217 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> mb_or_error =
218 llvm::MemoryBuffer::getFile(OptInputFilename.c_str());
219 if (mb_or_error.getError()) {
Stephen Hines8be8dba2013-06-18 09:53:43 -0700220 ALOGE("Failed to load bitcode from path %s! (%s)",
Stephen Hinesd0993af2014-07-15 16:49:25 -0700221 OptInputFilename.c_str(), mb_or_error.getError().message().c_str());
Shih-wei Liaod577d112012-04-25 04:06:29 -0700222 return EXIT_FAILURE;
223 }
Stephen Hinesd0993af2014-07-15 16:49:25 -0700224 std::unique_ptr<llvm::MemoryBuffer> input_data = std::move(mb_or_error.get());
Shih-wei Liaod577d112012-04-25 04:06:29 -0700225
Stephen Hines1ae3fd62014-05-15 12:16:26 -0700226 const char *bitcode = input_data->getBufferStart();
227 size_t bitcodeSize = input_data->getBufferSize();
Shih-wei Liaod577d112012-04-25 04:06:29 -0700228
Stephen Hines7e9c1852013-06-21 19:25:34 -0700229 if (!ConfigCompiler(RSCD)) {
230 ALOGE("Failed to configure compiler");
231 return EXIT_FAILURE;
232 }
Stephen Hinesc3437f02014-01-30 17:57:21 -0800233
234 // Attempt to dynamically initialize the compiler driver if such a function
235 // is present. It is only present if passed via "-load libFOO.so".
236 RSCompilerDriverInit_t rscdi = (RSCompilerDriverInit_t)
237 dlsym(RTLD_DEFAULT, STR(RS_COMPILER_DRIVER_INIT_FN));
Chris Wailes900c6c12014-08-13 15:40:00 -0700238 if (rscdi != nullptr) {
Stephen Hinesc3437f02014-01-30 17:57:21 -0800239 rscdi(&RSCD);
240 }
241
Pirama Arumuga Nainar1e0557a2014-12-02 15:02:18 -0800242 if (!OptEmbedRSInfo) {
243 bool built = RSCD.build(context, OptOutputPath.c_str(), OptOutputFilename.c_str(), bitcode,
244 bitcodeSize, commandLine.c_str(), OptBCLibFilename.c_str(), nullptr,
245 OptEmitLLVM);
Stephen Hines8be8dba2013-06-18 09:53:43 -0700246
Pirama Arumuga Nainar1e0557a2014-12-02 15:02:18 -0800247 if (!built) {
248 return EXIT_FAILURE;
249 }
250 }
251 else {
252 // embedRSInfo is set. Use buildForCompatLib to embed RS symbol information
253 // into the .rs.info symbol.
254 Source *source = Source::CreateFromBuffer(context, OptInputFilename.c_str(),
255 bitcode, bitcodeSize);
256 RSScript *s = new (std::nothrow) RSScript(*source);
257 if (s == nullptr) {
258 llvm::errs() << "Out of memory when creating script for file `"
259 << OptInputFilename << "'!\n";
260 delete source;
261 return EXIT_FAILURE;
262 }
263
264 llvm::SmallString<80> output(OptOutputPath);
265 llvm::sys::path::append(output, "/", OptOutputFilename);
266 llvm::sys::path::replace_extension(output, ".o");
267
268 if (!RSCD.buildForCompatLib(*s, output.c_str(), OptBCLibFilename.c_str())) {
269 fprintf(stderr, "Failed to compile script!");
270 return EXIT_FAILURE;
271 }
Shih-wei Liaod577d112012-04-25 04:06:29 -0700272 }
273
Shih-wei Liaod577d112012-04-25 04:06:29 -0700274 return EXIT_SUCCESS;
275}