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