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