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