Compiler.cpp revision 1e0557ae75ae780352845bd2ba0f4babdc5ae4e6
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
32#include "bcc/Assert.h"
33#include "bcc/Renderscript/RSExecutable.h"
34#include "bcc/Renderscript/RSScript.h"
35#include "bcc/Renderscript/RSTransforms.h"
36#include "bcc/Script.h"
37#include "bcc/Source.h"
38#include "bcc/Support/CompilerConfig.h"
39#include "bcc/Support/Log.h"
40#include "bcc/Support/OutputFile.h"
41#include "bcinfo/MetadataExtractor.h"
42
43#include <string>
44
45using namespace bcc;
46
47const char *Compiler::GetErrorString(enum ErrorCode pErrCode) {
48  switch (pErrCode) {
49  case kSuccess:
50    return "Successfully compiled.";
51  case kInvalidConfigNoTarget:
52    return "Invalid compiler config supplied (getTarget() returns nullptr.) "
53           "(missing call to CompilerConfig::initialize()?)";
54  case kErrCreateTargetMachine:
55    return "Failed to create llvm::TargetMachine.";
56  case kErrSwitchTargetMachine:
57    return  "Failed to switch llvm::TargetMachine.";
58  case kErrNoTargetMachine:
59    return "Failed to compile the script since there's no available "
60           "TargetMachine. (missing call to Compiler::config()?)";
61  case kErrDataLayoutNoMemory:
62    return "Out of memory when create DataLayout during compilation.";
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  assert(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
145enum Compiler::ErrorCode Compiler::screenGlobals(Script &pScript) {
146  // Separate pass manager for screening globals
147  llvm::PassManager pm;
148
149  ScreenFunctionStatus status;
150  status.failed = false;
151  pm.add(createRSScreenFunctionsPass(&status));
152
153  pm.run(pScript.getSource().getModule());
154  if (status.failed) {
155    return kIllegalGlobalFunction;
156  }
157  return kSuccess;
158}
159
160enum Compiler::ErrorCode Compiler::runPasses(Script &pScript,
161                                             llvm::raw_ostream &pResult) {
162  // Pass manager for link-time optimization
163  llvm::PassManager passes;
164
165  // Empty MCContext.
166  llvm::MCContext *mc_context = nullptr;
167
168  mTarget->addAnalysisPasses(passes);
169
170  // Prepare DataLayout target data from Module
171  llvm::DataLayoutPass *data_layout_pass =
172    new (std::nothrow) llvm::DataLayoutPass();
173
174  if (data_layout_pass == nullptr) {
175    return kErrDataLayoutNoMemory;
176  }
177
178  // Add DataLayout to the pass manager.
179  passes.add(data_layout_pass);
180
181  // Add our custom passes.
182  if (!addCustomPasses(pScript, passes)) {
183    return kErrCustomPasses;
184  }
185
186  if (mTarget->getOptLevel() == llvm::CodeGenOpt::None) {
187    passes.add(llvm::createGlobalOptimizerPass());
188    passes.add(llvm::createConstantMergePass());
189
190  } else {
191    // FIXME: Figure out which passes should be executed.
192    llvm::PassManagerBuilder Builder;
193    Builder.Inliner = llvm::createFunctionInliningPass();
194    Builder.populateLTOPassManager(passes, mTarget);
195  }
196
197  // Add passes to the pass manager to emit machine code through MC layer.
198  if (mTarget->addPassesToEmitMC(passes, mc_context, pResult,
199                                 /* DisableVerify */false)) {
200    return kPrepareCodeGenPass;
201  }
202
203  // Execute the passes.
204  passes.run(pScript.getSource().getModule());
205
206  return kSuccess;
207}
208
209enum Compiler::ErrorCode Compiler::compile(Script &pScript,
210                                           llvm::raw_ostream &pResult,
211                                           llvm::raw_ostream *IRStream) {
212  llvm::Module &module = pScript.getSource().getModule();
213  enum ErrorCode err;
214
215  if (mTarget == nullptr) {
216    return kErrNoTargetMachine;
217  }
218
219  const std::string &triple = module.getTargetTriple();
220  const llvm::DataLayout *dl = getTargetMachine().getSubtargetImpl()->getDataLayout();
221  unsigned int pointerSize = dl->getPointerSizeInBits();
222  if (triple == "armv7-none-linux-gnueabi") {
223    if (pointerSize != 32) {
224      return kErrInvalidSource;
225    }
226  } else if (triple == "aarch64-none-linux-gnueabi") {
227    if (pointerSize != 64) {
228      return kErrInvalidSource;
229    }
230  } else {
231    return kErrInvalidSource;
232  }
233
234  // Materialize the bitcode module.
235  if (module.getMaterializer() != nullptr) {
236    // A module with non-null materializer means that it is a lazy-load module.
237    // Materialize it now via invoking MaterializeAllPermanently(). This
238    // function returns false when the materialization is successful.
239    std::error_code ec = module.materializeAllPermanently();
240    if (ec) {
241      ALOGE("Failed to materialize the module `%s'! (%s)",
242            module.getModuleIdentifier().c_str(), ec.message().c_str());
243      return kErrMaterialization;
244    }
245  }
246
247  if ((err = screenGlobals(pScript)) != kSuccess) {
248    return err;
249  }
250
251  if ((err = runPasses(pScript, pResult)) != kSuccess) {
252    return err;
253  }
254
255  if (IRStream) {
256    *IRStream << module;
257  }
258
259  return kSuccess;
260}
261
262enum Compiler::ErrorCode Compiler::compile(Script &pScript,
263                                           OutputFile &pResult,
264                                           llvm::raw_ostream *IRStream) {
265  // Check the state of the specified output file.
266  if (pResult.hasError()) {
267    return kErrInvalidOutputFileState;
268  }
269
270  // Open the output file decorated in llvm::raw_ostream.
271  llvm::raw_ostream *out = pResult.dup();
272  if (out == nullptr) {
273    return kErrPrepareOutput;
274  }
275
276  // Delegate the request.
277  enum Compiler::ErrorCode err = compile(pScript, *out, IRStream);
278
279  // Close the output before return.
280  delete out;
281
282  return err;
283}
284
285bool Compiler::addInternalizeSymbolsPass(Script &pScript, llvm::PassManager &pPM) {
286  // Add a pass to internalize the symbols that don't need to have global
287  // visibility.
288  RSScript &script = static_cast<RSScript &>(pScript);
289  llvm::Module &module = script.getSource().getModule();
290  bcinfo::MetadataExtractor me(&module);
291  if (!me.extract()) {
292    bccAssert(false && "Could not extract metadata for module!");
293    return false;
294  }
295
296  // The vector contains the symbols that should not be internalized.
297  std::vector<const char *> export_symbols;
298
299  // Special RS functions should always be global symbols.
300  const char **special_functions = RSExecutable::SpecialFunctionNames;
301  while (*special_functions != nullptr) {
302    export_symbols.push_back(*special_functions);
303    special_functions++;
304  }
305
306  // Visibility of symbols appeared in rs_export_var and rs_export_func should
307  // also be preserved.
308  size_t exportVarCount = me.getExportVarCount();
309  size_t exportFuncCount = me.getExportFuncCount();
310  size_t exportForEachCount = me.getExportForEachSignatureCount();
311  const char **exportVarNameList = me.getExportVarNameList();
312  const char **exportFuncNameList = me.getExportFuncNameList();
313  const char **exportForEachNameList = me.getExportForEachNameList();
314  size_t i;
315
316  for (i = 0; i < exportVarCount; ++i) {
317    export_symbols.push_back(exportVarNameList[i]);
318  }
319
320  for (i = 0; i < exportFuncCount; ++i) {
321    export_symbols.push_back(exportFuncNameList[i]);
322  }
323
324  // Expanded foreach functions should not be internalized, too.
325  // expanded_foreach_funcs keeps the .expand version of the kernel names
326  // around until createInternalizePass() is finished making its own
327  // copy of the visible symbols.
328  std::vector<std::string> expanded_foreach_funcs;
329  for (i = 0; i < exportForEachCount; ++i) {
330    expanded_foreach_funcs.push_back(
331        std::string(exportForEachNameList[i]) + ".expand");
332  }
333
334  for (i = 0; i < exportForEachCount; i++) {
335      export_symbols.push_back(expanded_foreach_funcs[i].c_str());
336  }
337
338  pPM.add(llvm::createInternalizePass(export_symbols));
339
340  return true;
341}
342
343bool Compiler::addInvokeHelperPass(llvm::PassManager &pPM) {
344  llvm::Triple arch(getTargetMachine().getTargetTriple());
345  if (arch.isArch64Bit()) {
346    pPM.add(createRSInvokeHelperPass());
347  }
348  return true;
349}
350
351bool Compiler::addExpandForEachPass(Script &pScript, llvm::PassManager &pPM) {
352  // Script passed to RSCompiler must be a RSScript.
353  RSScript &script = static_cast<RSScript &>(pScript);
354
355  // Expand ForEach on CPU path to reduce launch overhead.
356  bool pEnableStepOpt = true;
357  pPM.add(createRSForEachExpandPass(pEnableStepOpt));
358  if (script.getEmbedInfo())
359    pPM.add(createRSEmbedInfoPass());
360
361  return true;
362}
363
364bool Compiler::addCustomPasses(Script &pScript, llvm::PassManager &pPM) {
365  if (!addInvokeHelperPass(pPM))
366    return false;
367
368  if (!addExpandForEachPass(pScript, pPM))
369    return false;
370
371  if (!addInternalizeSymbolsPass(pScript, pPM))
372    return false;
373
374  return true;
375}
376