Compiler.cpp revision c2ca742d7d0197c52e49467862844463fb42280f
1/*
2 * Copyright 2010-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 "bcc/Compiler.h"
18
19#include <llvm/Analysis/Passes.h>
20#include <llvm/Analysis/TargetTransformInfo.h>
21#include <llvm/CodeGen/RegAllocRegistry.h>
22#include <llvm/IR/LegacyPassManager.h>
23#include <llvm/IR/Module.h>
24#include <llvm/Support/TargetRegistry.h>
25#include <llvm/Support/raw_ostream.h>
26#include <llvm/IR/DataLayout.h>
27#include <llvm/Target/TargetSubtargetInfo.h>
28#include <llvm/Target/TargetMachine.h>
29#include <llvm/Transforms/IPO.h>
30#include <llvm/Transforms/IPO/PassManagerBuilder.h>
31#include <llvm/Transforms/Scalar.h>
32#include <llvm/Transforms/Vectorize.h>
33
34#include "bcc/Assert.h"
35#include "bcc/Renderscript/RSScript.h"
36#include "bcc/Renderscript/RSTransforms.h"
37#include "bcc/Script.h"
38#include "bcc/Source.h"
39#include "bcc/Support/CompilerConfig.h"
40#include "bcc/Support/Log.h"
41#include "bcc/Support/OutputFile.h"
42#include "bcinfo/MetadataExtractor.h"
43#include "rsDefines.h"
44
45#include <string>
46
47using namespace bcc;
48
49const char *Compiler::GetErrorString(enum ErrorCode pErrCode) {
50  switch (pErrCode) {
51  case kSuccess:
52    return "Successfully compiled.";
53  case kInvalidConfigNoTarget:
54    return "Invalid compiler config supplied (getTarget() returns nullptr.) "
55           "(missing call to CompilerConfig::initialize()?)";
56  case kErrCreateTargetMachine:
57    return "Failed to create llvm::TargetMachine.";
58  case kErrSwitchTargetMachine:
59    return  "Failed to switch llvm::TargetMachine.";
60  case kErrNoTargetMachine:
61    return "Failed to compile the script since there's no available "
62           "TargetMachine. (missing call to Compiler::config()?)";
63  case kErrMaterialization:
64    return "Failed to materialize the module.";
65  case kErrInvalidOutputFileState:
66    return "Supplied output file was invalid (in the error state.)";
67  case kErrPrepareOutput:
68    return "Failed to prepare file for output.";
69  case kPrepareCodeGenPass:
70    return "Failed to construct pass list for code-generation.";
71  case kErrCustomPasses:
72    return "Error occurred while adding custom passes.";
73  case kErrInvalidSource:
74    return "Error loading input bitcode";
75  case kIllegalGlobalFunction:
76    return "Use of undefined external function";
77  }
78
79  // This assert should never be reached as the compiler verifies that the
80  // above switch coveres all enum values.
81  bccAssert(false && "Unknown error code encountered");
82  return  "";
83}
84
85//===----------------------------------------------------------------------===//
86// Instance Methods
87//===----------------------------------------------------------------------===//
88Compiler::Compiler() : mTarget(nullptr), mEnableOpt(true) {
89  return;
90}
91
92Compiler::Compiler(const CompilerConfig &pConfig) : mTarget(nullptr),
93                                                    mEnableOpt(true) {
94  const std::string &triple = pConfig.getTriple();
95
96  enum ErrorCode err = config(pConfig);
97  if (err != kSuccess) {
98    ALOGE("%s (%s, features: %s)", GetErrorString(err),
99          triple.c_str(), pConfig.getFeatureString().c_str());
100    return;
101  }
102
103  return;
104}
105
106enum Compiler::ErrorCode Compiler::config(const CompilerConfig &pConfig) {
107  if (pConfig.getTarget() == nullptr) {
108    return kInvalidConfigNoTarget;
109  }
110
111  llvm::TargetMachine *new_target =
112      (pConfig.getTarget())->createTargetMachine(pConfig.getTriple(),
113                                                 pConfig.getCPU(),
114                                                 pConfig.getFeatureString(),
115                                                 pConfig.getTargetOptions(),
116                                                 pConfig.getRelocationModel(),
117                                                 pConfig.getCodeModel(),
118                                                 pConfig.getOptimizationLevel());
119
120  if (new_target == nullptr) {
121    return ((mTarget != nullptr) ? kErrSwitchTargetMachine :
122                                   kErrCreateTargetMachine);
123  }
124
125  // Replace the old TargetMachine.
126  delete mTarget;
127  mTarget = new_target;
128
129  // Adjust register allocation policy according to the optimization level.
130  //  createFastRegisterAllocator: fast but bad quality
131  //  createLinearScanRegisterAllocator: not so fast but good quality
132  if ((pConfig.getOptimizationLevel() == llvm::CodeGenOpt::None)) {
133    llvm::RegisterRegAlloc::setDefault(llvm::createFastRegisterAllocator);
134  } else {
135    llvm::RegisterRegAlloc::setDefault(llvm::createGreedyRegisterAllocator);
136  }
137
138  return kSuccess;
139}
140
141Compiler::~Compiler() {
142  delete mTarget;
143}
144
145
146enum Compiler::ErrorCode Compiler::runPasses(Script &pScript,
147                                             llvm::raw_pwrite_stream &pResult) {
148  // Pass manager for link-time optimization
149  llvm::legacy::PassManager passes;
150
151  // Empty MCContext.
152  llvm::MCContext *mc_context = nullptr;
153
154  passes.add(createTargetTransformInfoWrapperPass(mTarget->getTargetIRAnalysis()));
155
156  // Add our custom passes.
157  if (!addCustomPasses(pScript, passes)) {
158    return kErrCustomPasses;
159  }
160
161  if (mTarget->getOptLevel() == llvm::CodeGenOpt::None) {
162    passes.add(llvm::createGlobalOptimizerPass());
163    passes.add(llvm::createConstantMergePass());
164
165  } else {
166    // FIXME: Figure out which passes should be executed.
167    llvm::PassManagerBuilder Builder;
168    Builder.Inliner = llvm::createFunctionInliningPass();
169    Builder.populateLTOPassManager(passes);
170
171    /* FIXME: Reenable autovectorization after rebase.
172       bug 19324423
173    // Add vectorization passes after LTO passes are in
174    // additional flag: -unroll-runtime
175    passes.add(llvm::createLoopUnrollPass(-1, 16, 0, 1));
176    // Need to pass appropriate flags here: -scalarize-load-store
177    passes.add(llvm::createScalarizerPass());
178    passes.add(llvm::createCFGSimplificationPass());
179    passes.add(llvm::createScopedNoAliasAAPass());
180    passes.add(llvm::createScalarEvolutionAliasAnalysisPass());
181    // additional flags: -slp-vectorize-hor -slp-vectorize-hor-store (unnecessary?)
182    passes.add(llvm::createSLPVectorizerPass());
183    passes.add(llvm::createDeadCodeEliminationPass());
184    passes.add(llvm::createInstructionCombiningPass());
185    */
186  }
187
188  // These passes have to come after LTO, since we don't want to examine
189  // functions that are never actually called.
190  if (!addPostLTOCustomPasses(passes)) {
191    return kErrCustomPasses;
192  }
193
194  // RSEmbedInfoPass needs to come after we have scanned for non-threadable
195  // functions.
196  // Script passed to RSCompiler must be a RSScript.
197  RSScript &script = static_cast<RSScript &>(pScript);
198  if (script.getEmbedInfo())
199    passes.add(createRSEmbedInfoPass());
200
201  // Add passes to the pass manager to emit machine code through MC layer.
202  if (mTarget->addPassesToEmitMC(passes, mc_context, pResult,
203                                 /* DisableVerify */false)) {
204    return kPrepareCodeGenPass;
205  }
206
207  // Execute the passes.
208  passes.run(pScript.getSource().getModule());
209
210  return kSuccess;
211}
212
213enum Compiler::ErrorCode Compiler::compile(Script &pScript,
214                                           llvm::raw_pwrite_stream &pResult,
215                                           llvm::raw_ostream *IRStream) {
216  llvm::Module &module = pScript.getSource().getModule();
217  enum ErrorCode err;
218
219  if (mTarget == nullptr) {
220    return kErrNoTargetMachine;
221  }
222
223  const std::string &triple = module.getTargetTriple();
224  const llvm::DataLayout *dl = getTargetMachine().getDataLayout();
225  unsigned int pointerSize = dl->getPointerSizeInBits();
226  if (triple == "armv7-none-linux-gnueabi") {
227    if (pointerSize != 32) {
228      return kErrInvalidSource;
229    }
230  } else if (triple == "aarch64-none-linux-gnueabi") {
231    if (pointerSize != 64) {
232      return kErrInvalidSource;
233    }
234  } else {
235    return kErrInvalidSource;
236  }
237
238  // Sanitize module's target information.
239  module.setTargetTriple(getTargetMachine().getTargetTriple());
240  module.setDataLayout(*getTargetMachine().getDataLayout());
241
242  // Materialize the bitcode module.
243  if (module.getMaterializer() != nullptr) {
244    // A module with non-null materializer means that it is a lazy-load module.
245    // Materialize it now via invoking MaterializeAllPermanently(). This
246    // function returns false when the materialization is successful.
247    std::error_code ec = module.materializeAllPermanently();
248    if (ec) {
249      ALOGE("Failed to materialize the module `%s'! (%s)",
250            module.getModuleIdentifier().c_str(), ec.message().c_str());
251      return kErrMaterialization;
252    }
253  }
254
255  if ((err = runPasses(pScript, pResult)) != kSuccess) {
256    return err;
257  }
258
259  if (IRStream) {
260    *IRStream << module;
261  }
262
263  return kSuccess;
264}
265
266enum Compiler::ErrorCode Compiler::compile(Script &pScript,
267                                           OutputFile &pResult,
268                                           llvm::raw_ostream *IRStream) {
269  // Check the state of the specified output file.
270  if (pResult.hasError()) {
271    return kErrInvalidOutputFileState;
272  }
273
274  // Open the output file decorated in llvm::raw_ostream.
275  llvm::raw_pwrite_stream *out = pResult.dup();
276  if (out == nullptr) {
277    return kErrPrepareOutput;
278  }
279
280  // Delegate the request.
281  enum Compiler::ErrorCode err = compile(pScript, *out, IRStream);
282
283  // Close the output before return.
284  delete out;
285
286  return err;
287}
288
289bool Compiler::addInternalizeSymbolsPass(Script &pScript, llvm::legacy::PassManager &pPM) {
290  // Add a pass to internalize the symbols that don't need to have global
291  // visibility.
292  RSScript &script = static_cast<RSScript &>(pScript);
293  llvm::Module &module = script.getSource().getModule();
294  bcinfo::MetadataExtractor me(&module);
295  if (!me.extract()) {
296    bccAssert(false && "Could not extract metadata for module!");
297    return false;
298  }
299
300  // The vector contains the symbols that should not be internalized.
301  std::vector<const char *> export_symbols;
302
303  const char *sf[] = {
304    kRoot,               // Graphics drawing function or compute kernel.
305    kInit,               // Initialization routine called implicitly on startup.
306    kRsDtor,             // Static global destructor for a script instance.
307    kRsInfo,             // Variable containing string of RS metadata info.
308    kRsGlobalEntries,    // Optional number of global variables.
309    kRsGlobalNames,      // Optional global variable name info.
310    kRsGlobalAddresses,  // Optional global variable address info.
311    kRsGlobalSizes,      // Optional global variable size info.
312    kRsGlobalProperties, // Optional global variable properties.
313    nullptr              // Must be nullptr-terminated.
314  };
315  const char **special_functions = sf;
316  // Special RS functions should always be global symbols.
317  while (*special_functions != nullptr) {
318    export_symbols.push_back(*special_functions);
319    special_functions++;
320  }
321
322  // Visibility of symbols appeared in rs_export_var and rs_export_func should
323  // also be preserved.
324  size_t exportVarCount = me.getExportVarCount();
325  size_t exportFuncCount = me.getExportFuncCount();
326  size_t exportForEachCount = me.getExportForEachSignatureCount();
327  const char **exportVarNameList = me.getExportVarNameList();
328  const char **exportFuncNameList = me.getExportFuncNameList();
329  const char **exportForEachNameList = me.getExportForEachNameList();
330  size_t i;
331
332  for (i = 0; i < exportVarCount; ++i) {
333    export_symbols.push_back(exportVarNameList[i]);
334  }
335
336  for (i = 0; i < exportFuncCount; ++i) {
337    export_symbols.push_back(exportFuncNameList[i]);
338  }
339
340  // Expanded foreach functions should not be internalized, too.
341  // expanded_foreach_funcs keeps the .expand version of the kernel names
342  // around until createInternalizePass() is finished making its own
343  // copy of the visible symbols.
344  std::vector<std::string> expanded_foreach_funcs;
345  for (i = 0; i < exportForEachCount; ++i) {
346    expanded_foreach_funcs.push_back(
347        std::string(exportForEachNameList[i]) + ".expand");
348  }
349
350  for (i = 0; i < exportForEachCount; i++) {
351      export_symbols.push_back(expanded_foreach_funcs[i].c_str());
352  }
353
354  pPM.add(llvm::createInternalizePass(export_symbols));
355
356  return true;
357}
358
359bool Compiler::addInvokeHelperPass(llvm::legacy::PassManager &pPM) {
360  llvm::Triple arch(getTargetMachine().getTargetTriple());
361  if (arch.isArch64Bit()) {
362    pPM.add(createRSInvokeHelperPass());
363  }
364  return true;
365}
366
367bool Compiler::addExpandForEachPass(Script &pScript, llvm::legacy::PassManager &pPM) {
368  // Expand ForEach on CPU path to reduce launch overhead.
369  bool pEnableStepOpt = true;
370  pPM.add(createRSForEachExpandPass(pEnableStepOpt));
371
372  return true;
373}
374
375bool Compiler::addGlobalInfoPass(Script &pScript, llvm::legacy::PassManager &pPM) {
376  // Add additional information about RS global variables inside the Module.
377  RSScript &script = static_cast<RSScript &>(pScript);
378  if (script.getEmbedGlobalInfo()) {
379    pPM.add(createRSGlobalInfoPass(script.getEmbedGlobalInfoSkipConstant()));
380  }
381
382  return true;
383}
384
385bool Compiler::addInvariantPass(llvm::legacy::PassManager &pPM) {
386  // Mark Loads from RsExpandKernelDriverInfo as "load.invariant".
387  // Should run after ExpandForEach and before inlining.
388  pPM.add(createRSInvariantPass());
389
390  return true;
391}
392
393bool Compiler::addCustomPasses(Script &pScript, llvm::legacy::PassManager &pPM) {
394  if (!addInvokeHelperPass(pPM))
395    return false;
396
397  if (!addExpandForEachPass(pScript, pPM))
398    return false;
399
400  if (!addInvariantPass(pPM))
401    return false;
402
403  if (!addInternalizeSymbolsPass(pScript, pPM))
404    return false;
405
406  if (!addGlobalInfoPass(pScript, pPM))
407    return false;
408
409  return true;
410}
411
412bool Compiler::addPostLTOCustomPasses(llvm::legacy::PassManager &pPM) {
413  // Add pass to correct calling convention for X86-64.
414  llvm::Triple arch(getTargetMachine().getTargetTriple());
415  if (arch.getArch() == llvm::Triple::x86_64)
416    pPM.add(createRSX86_64CallConvPass());
417
418  // Add pass to check for illegal function calls.
419  pPM.add(createRSScreenFunctionsPass());
420
421  // Add pass to mark script as threadable.
422  pPM.add(createRSIsThreadablePass());
423
424  return true;
425}
426