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