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 <iostream>
18 #include <list>
19 #include <map>
20 #include <sstream>
21 #include <string>
22 #include <vector>
23
24 #include <dlfcn.h>
25 #include <stdlib.h>
26
27 #include <log/log.h>
28
29 #include <llvm/ADT/STLExtras.h>
30 #include <llvm/ADT/SmallString.h>
31 #include <llvm/Config/config.h>
32 #include <llvm/Support/CommandLine.h>
33 #include <llvm/Support/FileSystem.h>
34 #include <llvm/Support/ManagedStatic.h>
35 #include <llvm/Support/MemoryBuffer.h>
36 #include <llvm/Support/Path.h>
37 #include <llvm/Support/PluginLoader.h>
38 #include <llvm/Support/raw_ostream.h>
39
40 #include <bcc/BCCContext.h>
41 #include <bcc/CompilerConfig.h>
42 #include <bcc/Config.h>
43 #include <bcc/Initialization.h>
44 #include <bcc/RSCompilerDriver.h>
45 #include <bcc/Source.h>
46
47 using namespace bcc;
48
49 #define STR2(a) #a
50 #define STR(a) STR2(a)
51
52 //===----------------------------------------------------------------------===//
53 // General Options
54 //===----------------------------------------------------------------------===//
55 namespace {
56
57 llvm::cl::list<std::string>
58 OptInputFilenames(llvm::cl::Positional, llvm::cl::OneOrMore,
59 llvm::cl::desc("<input bitcode files>"));
60
61 llvm::cl::list<std::string>
62 OptMergePlans("merge", llvm::cl::ZeroOrMore,
63 llvm::cl::desc("Lists of kernels to merge (as source-and-slot "
64 "pairs) and names for the final merged kernels"));
65
66 llvm::cl::list<std::string>
67 OptInvokes("invoke", llvm::cl::ZeroOrMore,
68 llvm::cl::desc("Invocable functions"));
69
70 llvm::cl::opt<std::string>
71 OptOutputFilename("o", llvm::cl::desc("Specify the output filename"),
72 llvm::cl::value_desc("filename"),
73 llvm::cl::init("bcc_output"));
74
75 llvm::cl::opt<std::string>
76 OptBCLibFilename("bclib", llvm::cl::desc("Specify the bclib filename"),
77 llvm::cl::value_desc("bclib"));
78
79 llvm::cl::opt<std::string>
80 OptBCLibRelaxedFilename("bclib_relaxed", llvm::cl::desc("Specify the bclib filename optimized for "
81 "relaxed precision floating point maths"),
82 llvm::cl::init(""),
83 llvm::cl::value_desc("bclib_relaxed"));
84
85 llvm::cl::opt<std::string>
86 OptOutputPath("output_path", llvm::cl::desc("Specify the output path"),
87 llvm::cl::value_desc("output path"),
88 llvm::cl::init("."));
89
90 llvm::cl::opt<bool>
91 OptEmitLLVM("emit-llvm",
92 llvm::cl::desc("Emit an LLVM-IR version of the generated program"));
93
94 llvm::cl::opt<std::string>
95 OptTargetTriple("mtriple",
96 llvm::cl::desc("Specify the target triple (default: "
97 DEFAULT_TARGET_TRIPLE_STRING ")"),
98 llvm::cl::init(DEFAULT_TARGET_TRIPLE_STRING),
99 llvm::cl::value_desc("triple"));
100
101 llvm::cl::alias OptTargetTripleC("C", llvm::cl::NotHidden,
102 llvm::cl::desc("Alias for -mtriple"),
103 llvm::cl::aliasopt(OptTargetTriple));
104
105 llvm::cl::opt<bool>
106 OptRSDebugContext("rs-debug-ctx",
107 llvm::cl::desc("Enable build to work with a RenderScript debug context"));
108
109 llvm::cl::opt<bool>
110 OptRSGlobalInfo("rs-global-info",
111 llvm::cl::desc("Embed information about global variables in the code"));
112
113 llvm::cl::opt<bool>
114 OptRSGlobalInfoSkipConstant("rs-global-info-skip-constant",
115 llvm::cl::desc("Skip embedding information about constant global "
116 "variables in the code"));
117
118 llvm::cl::opt<std::string>
119 OptChecksum("build-checksum",
120 llvm::cl::desc("Embed a checksum of this compiler invocation for"
121 " cache invalidation at a later time"),
122 llvm::cl::value_desc("checksum"));
123
124 //===----------------------------------------------------------------------===//
125 // Compiler Options
126 //===----------------------------------------------------------------------===//
127 llvm::cl::opt<bool>
128 OptPIC("fPIC", llvm::cl::desc("Generate fully relocatable, position independent"
129 " code"));
130
131 // If set, use buildForCompatLib to embed RS symbol information into the object
132 // file. The information is stored in the .rs.info variable. This option is
133 // to be used in tandem with -fPIC.
134 llvm::cl::opt<bool>
135 OptEmbedRSInfo("embedRSInfo",
136 llvm::cl::desc("Embed RS Info into the object file instead of generating"
137 " a separate .o.info file"));
138
139 // RenderScript uses -O3 by default
140 llvm::cl::opt<char>
141 OptOptLevel("O", llvm::cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
142 "(default: -O3)"),
143 llvm::cl::Prefix, llvm::cl::ZeroOrMore, llvm::cl::init('3'));
144
145 // Override "bcc -version" since the LLVM version information is not correct on
146 // Android build.
BCCVersionPrinter()147 void BCCVersionPrinter() {
148 llvm::raw_ostream &os = llvm::outs();
149 os << "libbcc (The Android Open Source Project, http://www.android.com/):\n"
150 << " Default target: " << DEFAULT_TARGET_TRIPLE_STRING << "\n\n"
151 << "LLVM (http://llvm.org/):\n"
152 << " Version: " << PACKAGE_VERSION << "\n";
153 return;
154 }
155
extractSourcesAndSlots(const llvm::cl::list<std::string> & optList,std::list<std::string> * batchNames,std::list<std::list<std::pair<int,int>>> * sourcesAndSlots)156 void extractSourcesAndSlots(const llvm::cl::list<std::string>& optList,
157 std::list<std::string>* batchNames,
158 std::list<std::list<std::pair<int, int>>>* sourcesAndSlots) {
159 for (unsigned i = 0; i < optList.size(); ++i) {
160 std::string plan = optList[i];
161 unsigned found = plan.find(':');
162
163 std::string name = plan.substr(0, found);
164 std::cerr << "new kernel name: " << name << std::endl;
165 batchNames->push_back(name);
166
167 std::istringstream iss(plan.substr(found + 1));
168 std::string s;
169 std::list<std::pair<int, int>> planList;
170 while (getline(iss, s, '.')) {
171 found = s.find(',');
172 std::string sourceStr = s.substr(0, found);
173 std::string slotStr = s.substr(found + 1);
174
175 std::cerr << "source " << sourceStr << ", slot " << slotStr << std::endl;
176
177 int source = std::stoi(sourceStr);
178 int slot = std::stoi(slotStr);
179 planList.push_back(std::make_pair(source, slot));
180 }
181
182 sourcesAndSlots->push_back(planList);
183 }
184 }
185
compileScriptGroup(BCCContext & Context,RSCompilerDriver & RSCD)186 bool compileScriptGroup(BCCContext& Context, RSCompilerDriver& RSCD) {
187 std::vector<bcc::Source*> sources;
188 for (unsigned i = 0; i < OptInputFilenames.size(); ++i) {
189 bcc::Source* source =
190 bcc::Source::CreateFromFile(Context, OptInputFilenames[i]);
191 if (!source) {
192 llvm::errs() << "Error loading file '" << OptInputFilenames[i]<< "'\n";
193 return false;
194 }
195 sources.push_back(source);
196 }
197
198 std::list<std::string> fusedKernelNames;
199 std::list<std::list<std::pair<int, int>>> sourcesAndSlots;
200 extractSourcesAndSlots(OptMergePlans, &fusedKernelNames, &sourcesAndSlots);
201
202 std::list<std::string> invokeBatchNames;
203 std::list<std::list<std::pair<int, int>>> invokeSourcesAndSlots;
204 extractSourcesAndSlots(OptInvokes, &invokeBatchNames, &invokeSourcesAndSlots);
205
206 std::string outputFilepath(OptOutputPath);
207 outputFilepath.append("/");
208 outputFilepath.append(OptOutputFilename);
209
210 bool success = RSCD.buildScriptGroup(
211 Context, outputFilepath.c_str(), OptBCLibFilename.c_str(),
212 OptBCLibRelaxedFilename.c_str(), OptEmitLLVM, OptChecksum.c_str(),
213 sources, sourcesAndSlots, fusedKernelNames,
214 invokeSourcesAndSlots, invokeBatchNames);
215
216 return success;
217 }
218
219 } // end anonymous namespace
220
221 static inline
ConfigCompiler(RSCompilerDriver & pRSCD)222 bool ConfigCompiler(RSCompilerDriver &pRSCD) {
223 Compiler *RSC = pRSCD.getCompiler();
224 CompilerConfig *config = nullptr;
225
226 config = new (std::nothrow) CompilerConfig(OptTargetTriple);
227 if (config == nullptr) {
228 llvm::errs() << "Out of memory when create the compiler configuration!\n";
229 return false;
230 }
231
232 if (OptPIC) {
233 config->setRelocationModel(llvm::Reloc::PIC_);
234
235 // For x86_64, CodeModel needs to be small if PIC_ reloc is used.
236 // Otherwise, we end up with TEXTRELs in the shared library.
237 if (config->getTriple().find("x86_64") != std::string::npos) {
238 config->setCodeModel(llvm::CodeModel::Small);
239 }
240 }
241 switch (OptOptLevel) {
242 case '0': config->setOptimizationLevel(llvm::CodeGenOpt::None); break;
243 case '1': config->setOptimizationLevel(llvm::CodeGenOpt::Less); break;
244 case '2': config->setOptimizationLevel(llvm::CodeGenOpt::Default); break;
245 case '3':
246 default: {
247 config->setOptimizationLevel(llvm::CodeGenOpt::Aggressive);
248 break;
249 }
250 }
251
252 pRSCD.setConfig(config);
253 Compiler::ErrorCode result = RSC->config(*config);
254
255 if (OptRSDebugContext) {
256 pRSCD.setDebugContext(true);
257 }
258
259 if (OptRSGlobalInfo) {
260 pRSCD.setEmbedGlobalInfo(true);
261 }
262
263 if (OptRSGlobalInfoSkipConstant) {
264 pRSCD.setEmbedGlobalInfoSkipConstant(true);
265 }
266
267 if (result != Compiler::kSuccess) {
268 llvm::errs() << "Failed to configure the compiler! (detail: "
269 << Compiler::GetErrorString(result) << ")\n";
270 return false;
271 }
272
273 return true;
274 }
275
main(int argc,char ** argv)276 int main(int argc, char **argv) {
277
278 llvm::llvm_shutdown_obj Y;
279 init::Initialize();
280 llvm::cl::SetVersionPrinter(BCCVersionPrinter);
281 llvm::cl::ParseCommandLineOptions(argc, argv);
282
283 BCCContext context;
284 RSCompilerDriver RSCD;
285
286 if (OptBCLibFilename.empty()) {
287 ALOGE("Failed to compile bitcode, -bclib was not specified");
288 return EXIT_FAILURE;
289 }
290
291 if (!ConfigCompiler(RSCD)) {
292 ALOGE("Failed to configure compiler");
293 return EXIT_FAILURE;
294 }
295
296 // Attempt to dynamically initialize the compiler driver if such a function
297 // is present. It is only present if passed via "-load libFOO.so".
298 RSCompilerDriverInit_t rscdi = (RSCompilerDriverInit_t)
299 dlsym(RTLD_DEFAULT, STR(RS_COMPILER_DRIVER_INIT_FN));
300 if (rscdi != nullptr) {
301 rscdi(&RSCD);
302 }
303
304 if (OptMergePlans.size() > 0) {
305 bool success = compileScriptGroup(context, RSCD);
306
307 if (!success) {
308 return EXIT_FAILURE;
309 }
310
311 return EXIT_SUCCESS;
312 }
313
314 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> mb_or_error =
315 llvm::MemoryBuffer::getFile(OptInputFilenames[0].c_str());
316 if (mb_or_error.getError()) {
317 ALOGE("Failed to load bitcode from path %s! (%s)",
318 OptInputFilenames[0].c_str(), mb_or_error.getError().message().c_str());
319 return EXIT_FAILURE;
320 }
321 std::unique_ptr<llvm::MemoryBuffer> input_data = std::move(mb_or_error.get());
322
323 const char *bitcode = input_data->getBufferStart();
324 size_t bitcodeSize = input_data->getBufferSize();
325
326 if (!OptEmbedRSInfo) {
327 bool built = RSCD.build(context, OptOutputPath.c_str(),
328 OptOutputFilename.c_str(),
329 bitcode, bitcodeSize,
330 OptChecksum.c_str(), OptBCLibFilename.c_str(),
331 nullptr, OptEmitLLVM);
332
333 if (!built) {
334 return EXIT_FAILURE;
335 }
336 } else {
337 // embedRSInfo is set. Use buildForCompatLib to embed RS symbol information
338 // into the .rs.info symbol.
339 Source *source = Source::CreateFromBuffer(context, OptInputFilenames[0].c_str(),
340 bitcode, bitcodeSize);
341
342 // If the bitcode fails verification in the bitcode loader, the returned Source is set to NULL.
343 if (!source) {
344 ALOGE("Failed to load source from file %s", OptInputFilenames[0].c_str());
345 return EXIT_FAILURE;
346 }
347
348 std::unique_ptr<Script> s(new (std::nothrow) Script(source));
349 if (s == nullptr) {
350 llvm::errs() << "Out of memory when creating script for file `"
351 << OptInputFilenames[0] << "'!\n";
352 delete source;
353 return EXIT_FAILURE;
354 }
355
356 s->setOptimizationLevel(RSCD.getConfig()->getOptimizationLevel());
357 llvm::SmallString<80> output(OptOutputPath);
358 llvm::sys::path::append(output, "/", OptOutputFilename);
359 llvm::sys::path::replace_extension(output, ".o");
360
361 if (!RSCD.buildForCompatLib(*s, output.c_str(), OptChecksum.c_str(),
362 OptBCLibFilename.c_str(), OptEmitLLVM)) {
363 fprintf(stderr, "Failed to compile script!");
364 return EXIT_FAILURE;
365 }
366 }
367
368 return EXIT_SUCCESS;
369 }
370