blob: d87b11c810b19d5cd7e9bc209b6479e2cb9655e2 [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>
Stephen Hines8be8dba2013-06-18 09:53:43 -070037#include <bcc/Renderscript/RSCompilerDriver.h>
Shih-wei Liaod577d112012-04-25 04:06:29 -070038#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 Liaod577d112012-04-25 04:06:29 -070044
45using namespace bcc;
46
Stephen Hinesc3437f02014-01-30 17:57:21 -080047#define STR2(a) #a
48#define STR(a) STR2(a)
49
Shih-wei Liaod577d112012-04-25 04:06:29 -070050//===----------------------------------------------------------------------===//
51// General Options
52//===----------------------------------------------------------------------===//
53namespace {
54
Stephen Hines8be8dba2013-06-18 09:53:43 -070055llvm::cl::opt<std::string>
56OptInputFilename(llvm::cl::Positional, llvm::cl::ValueRequired,
57 llvm::cl::desc("<input bitcode file>"));
Shih-wei Liaod577d112012-04-25 04:06:29 -070058
59llvm::cl::opt<std::string>
60OptOutputFilename("o", llvm::cl::desc("Specify the output filename"),
Stephen Hines8be8dba2013-06-18 09:53:43 -070061 llvm::cl::value_desc("filename"),
62 llvm::cl::init("bcc_output"));
63
64llvm::cl::opt<std::string>
65OptBCLibFilename("bclib", llvm::cl::desc("Specify the bclib filename"),
66 llvm::cl::value_desc("bclib"));
67
68llvm::cl::opt<std::string>
69OptOutputPath("output_path", llvm::cl::desc("Specify the output path"),
70 llvm::cl::value_desc("output path"),
71 llvm::cl::init("."));
Shih-wei Liaod577d112012-04-25 04:06:29 -070072
Tobias Grosser7b980e12013-06-20 10:12:13 -070073llvm::cl::opt<bool>
74OptEmitLLVM("emit-llvm",
75 llvm::cl::desc("Emit an LLVM-IR version of the generated program"));
76
Shih-wei Liaod577d112012-04-25 04:06:29 -070077llvm::cl::opt<std::string>
78OptTargetTriple("mtriple",
79 llvm::cl::desc("Specify the target triple (default: "
80 DEFAULT_TARGET_TRIPLE_STRING ")"),
81 llvm::cl::init(DEFAULT_TARGET_TRIPLE_STRING),
82 llvm::cl::value_desc("triple"));
83
84llvm::cl::alias OptTargetTripleC("C", llvm::cl::NotHidden,
85 llvm::cl::desc("Alias for -mtriple"),
86 llvm::cl::aliasopt(OptTargetTriple));
Shih-wei Liaod577d112012-04-25 04:06:29 -070087
Stephen Hinesc3437f02014-01-30 17:57:21 -080088llvm::cl::opt<bool>
89OptRSDebugContext("rs-debug-ctx",
90 llvm::cl::desc("Enable build to work with a RenderScript debug context"));
91
Shih-wei Liaod577d112012-04-25 04:06:29 -070092//===----------------------------------------------------------------------===//
93// Compiler Options
94//===----------------------------------------------------------------------===//
Pirama Arumuga Nainar1e0557a2014-12-02 15:02:18 -080095llvm::cl::opt<bool>
96OptPIC("fPIC", llvm::cl::desc("Generate fully relocatable, position independent"
97 " code"));
98
99// If set, use buildForCompatLib to embed RS symbol information into the object
100// file. The information is stored in the .rs.info variable. This option is
101// to be used in tandem with -fPIC.
102llvm::cl::opt<bool>
103OptEmbedRSInfo("embedRSInfo",
104 llvm::cl::desc("Embed RS Info into the object file instead of generating"
105 " a separate .o.info file"));
Shih-wei Liaod577d112012-04-25 04:06:29 -0700106
Stephen Hines7e9c1852013-06-21 19:25:34 -0700107// RenderScript uses -O3 by default
Shih-wei Liaod577d112012-04-25 04:06:29 -0700108llvm::cl::opt<char>
109OptOptLevel("O", llvm::cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
Stephen Hines7e9c1852013-06-21 19:25:34 -0700110 "(default: -O3)"),
111 llvm::cl::Prefix, llvm::cl::ZeroOrMore, llvm::cl::init('3'));
Shih-wei Liaod577d112012-04-25 04:06:29 -0700112
Shih-wei Liaod577d112012-04-25 04:06:29 -0700113// Override "bcc -version" since the LLVM version information is not correct on
114// Android build.
115void BCCVersionPrinter() {
116 llvm::raw_ostream &os = llvm::outs();
117 os << "libbcc (The Android Open Source Project, http://www.android.com/):\n"
Jean-Luc Brouilletc5e607a2014-06-18 18:14:02 -0700118 << " Default target: " << DEFAULT_TARGET_TRIPLE_STRING << "\n\n"
119 << "LLVM (http://llvm.org/):\n"
Shih-wei Liaod577d112012-04-25 04:06:29 -0700120 << " Version: " << PACKAGE_VERSION << "\n";
121 return;
122}
123
124} // end anonymous namespace
125
126static inline
Stephen Hines7e9c1852013-06-21 19:25:34 -0700127bool ConfigCompiler(RSCompilerDriver &pRSCD) {
Chris Wailesb4447cd2014-08-19 16:22:20 -0700128 Compiler *RSC = pRSCD.getCompiler();
Chris Wailes900c6c12014-08-13 15:40:00 -0700129 CompilerConfig *config = nullptr;
Shih-wei Liaod577d112012-04-25 04:06:29 -0700130
Shih-wei Liaod577d112012-04-25 04:06:29 -0700131 config = new (std::nothrow) CompilerConfig(OptTargetTriple);
Chris Wailes900c6c12014-08-13 15:40:00 -0700132 if (config == nullptr) {
Shih-wei Liaod577d112012-04-25 04:06:29 -0700133 llvm::errs() << "Out of memory when create the compiler configuration!\n";
134 return false;
135 }
136
Jian Li602ce032014-09-03 18:54:26 +0800137 // llvm3.5 has removed the auto-detect feature for x86 subtarget,
138 // so set features explicitly in bcc.
139 if ((config->getTriple().find("i686") != std::string::npos) ||
140 (config->getTriple().find("x86_64") != std::string::npos)) {
141 std::vector<std::string> fv;
142
143#if defined(__SSE3__)
144 fv.push_back("+sse3");
145#endif
146#if defined(__SSSE3__)
147 fv.push_back("+ssse3");
148#endif
149#if defined(__SSE4_1__)
150 fv.push_back("+sse4.1");
151#endif
152#if defined(__SSE4_2__)
153 fv.push_back("+sse4.2");
154#endif
155
156 if (fv.size()) {
157 config->setFeatureString(fv);
158 }
159 }
160
Pirama Arumuga Nainar1e0557a2014-12-02 15:02:18 -0800161 if (OptPIC) {
162 config->setRelocationModel(llvm::Reloc::PIC_);
163
164 // For x86_64, CodeModel needs to be small if PIC_ reloc is used.
165 // Otherwise, we end up with TEXTRELs in the shared library.
166 if (config->getTriple().find("x86_64") != std::string::npos) {
167 config->setCodeModel(llvm::CodeModel::Small);
168 }
169 }
Shih-wei Liaod577d112012-04-25 04:06:29 -0700170 switch (OptOptLevel) {
171 case '0': config->setOptimizationLevel(llvm::CodeGenOpt::None); break;
172 case '1': config->setOptimizationLevel(llvm::CodeGenOpt::Less); break;
Stephen Hines7e9c1852013-06-21 19:25:34 -0700173 case '2': config->setOptimizationLevel(llvm::CodeGenOpt::Default); break;
174 case '3':
Shih-wei Liaod577d112012-04-25 04:06:29 -0700175 default: {
Stephen Hines7e9c1852013-06-21 19:25:34 -0700176 config->setOptimizationLevel(llvm::CodeGenOpt::Aggressive);
Shih-wei Liaod577d112012-04-25 04:06:29 -0700177 break;
178 }
179 }
180
Stephen Hines7e9c1852013-06-21 19:25:34 -0700181 pRSCD.setConfig(config);
182 Compiler::ErrorCode result = RSC->config(*config);
Shih-wei Liaod577d112012-04-25 04:06:29 -0700183
Stephen Hinesc3437f02014-01-30 17:57:21 -0800184 if (OptRSDebugContext) {
185 pRSCD.setDebugContext(true);
186 }
187
Shih-wei Liaod577d112012-04-25 04:06:29 -0700188 if (result != Compiler::kSuccess) {
189 llvm::errs() << "Failed to configure the compiler! (detail: "
190 << Compiler::GetErrorString(result) << ")\n";
191 return false;
192 }
193
194 return true;
195}
196
Shih-wei Liaod577d112012-04-25 04:06:29 -0700197int main(int argc, char **argv) {
Tim Murrayc46a3f52015-01-08 14:52:15 -0800198
199 llvm::llvm_shutdown_obj Y;
200 init::Initialize();
Shih-wei Liaod577d112012-04-25 04:06:29 -0700201 llvm::cl::SetVersionPrinter(BCCVersionPrinter);
202 llvm::cl::ParseCommandLineOptions(argc, argv);
Jean-Luc Brouilletf2ac3172014-06-25 18:21:36 -0700203 std::string commandLine = bcc::getCommandLine(argc, argv);
Shih-wei Liaod577d112012-04-25 04:06:29 -0700204
205 BCCContext context;
Stephen Hines8be8dba2013-06-18 09:53:43 -0700206 RSCompilerDriver RSCD;
Shih-wei Liaod577d112012-04-25 04:06:29 -0700207
Jean-Luc Brouilletc5e607a2014-06-18 18:14:02 -0700208 if (OptBCLibFilename.empty()) {
209 ALOGE("Failed to compile bit code, -bclib was not specified");
210 return EXIT_FAILURE;
211 }
212
Stephen Hinesd0993af2014-07-15 16:49:25 -0700213 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> mb_or_error =
214 llvm::MemoryBuffer::getFile(OptInputFilename.c_str());
215 if (mb_or_error.getError()) {
Stephen Hines8be8dba2013-06-18 09:53:43 -0700216 ALOGE("Failed to load bitcode from path %s! (%s)",
Stephen Hinesd0993af2014-07-15 16:49:25 -0700217 OptInputFilename.c_str(), mb_or_error.getError().message().c_str());
Shih-wei Liaod577d112012-04-25 04:06:29 -0700218 return EXIT_FAILURE;
219 }
Stephen Hinesd0993af2014-07-15 16:49:25 -0700220 std::unique_ptr<llvm::MemoryBuffer> input_data = std::move(mb_or_error.get());
Shih-wei Liaod577d112012-04-25 04:06:29 -0700221
Stephen Hines1ae3fd62014-05-15 12:16:26 -0700222 const char *bitcode = input_data->getBufferStart();
223 size_t bitcodeSize = input_data->getBufferSize();
Shih-wei Liaod577d112012-04-25 04:06:29 -0700224
Stephen Hines7e9c1852013-06-21 19:25:34 -0700225 if (!ConfigCompiler(RSCD)) {
226 ALOGE("Failed to configure compiler");
227 return EXIT_FAILURE;
228 }
Stephen Hinesc3437f02014-01-30 17:57:21 -0800229
230 // Attempt to dynamically initialize the compiler driver if such a function
231 // is present. It is only present if passed via "-load libFOO.so".
232 RSCompilerDriverInit_t rscdi = (RSCompilerDriverInit_t)
233 dlsym(RTLD_DEFAULT, STR(RS_COMPILER_DRIVER_INIT_FN));
Chris Wailes900c6c12014-08-13 15:40:00 -0700234 if (rscdi != nullptr) {
Stephen Hinesc3437f02014-01-30 17:57:21 -0800235 rscdi(&RSCD);
236 }
237
Pirama Arumuga Nainar1e0557a2014-12-02 15:02:18 -0800238 if (!OptEmbedRSInfo) {
239 bool built = RSCD.build(context, OptOutputPath.c_str(), OptOutputFilename.c_str(), bitcode,
240 bitcodeSize, commandLine.c_str(), OptBCLibFilename.c_str(), nullptr,
241 OptEmitLLVM);
Stephen Hines8be8dba2013-06-18 09:53:43 -0700242
Pirama Arumuga Nainar1e0557a2014-12-02 15:02:18 -0800243 if (!built) {
244 return EXIT_FAILURE;
245 }
246 }
247 else {
248 // embedRSInfo is set. Use buildForCompatLib to embed RS symbol information
249 // into the .rs.info symbol.
250 Source *source = Source::CreateFromBuffer(context, OptInputFilename.c_str(),
251 bitcode, bitcodeSize);
252 RSScript *s = new (std::nothrow) RSScript(*source);
253 if (s == nullptr) {
254 llvm::errs() << "Out of memory when creating script for file `"
255 << OptInputFilename << "'!\n";
256 delete source;
257 return EXIT_FAILURE;
258 }
259
260 llvm::SmallString<80> output(OptOutputPath);
261 llvm::sys::path::append(output, "/", OptOutputFilename);
262 llvm::sys::path::replace_extension(output, ".o");
263
264 if (!RSCD.buildForCompatLib(*s, output.c_str(), OptBCLibFilename.c_str())) {
265 fprintf(stderr, "Failed to compile script!");
266 return EXIT_FAILURE;
267 }
Shih-wei Liaod577d112012-04-25 04:06:29 -0700268 }
269
Shih-wei Liaod577d112012-04-25 04:06:29 -0700270 return EXIT_SUCCESS;
271}