1 /* 2 * Copyright 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/Renderscript/RSCompiler.h" 18 19 #include <llvm/IR/Module.h> 20 #include <llvm/PassManager.h> 21 #include <llvm/Transforms/IPO.h> 22 23 #include "bcc/Renderscript/RSExecutable.h" 24 #include "bcc/Renderscript/RSInfo.h" 25 #include "bcc/Renderscript/RSScript.h" 26 #include "bcc/Renderscript/RSTransforms.h" 27 #include "bcc/Source.h" 28 #include "bcc/Support/Log.h" 29 30 using namespace bcc; 31 32 bool RSCompiler::addInternalizeSymbolsPass(Script &pScript, llvm::PassManager &pPM) { 33 // Add a pass to internalize the symbols that don't need to have global 34 // visibility. 35 RSScript &script = static_cast<RSScript &>(pScript); 36 const RSInfo *info = script.getInfo(); 37 38 // The vector contains the symbols that should not be internalized. 39 std::vector<const char *> export_symbols; 40 41 // Special RS functions should always be global symbols. 42 const char **special_functions = RSExecutable::SpecialFunctionNames; 43 while (*special_functions != NULL) { 44 export_symbols.push_back(*special_functions); 45 special_functions++; 46 } 47 48 // Visibility of symbols appeared in rs_export_var and rs_export_func should 49 // also be preserved. 50 const RSInfo::ExportVarNameListTy &export_vars = info->getExportVarNames(); 51 const RSInfo::ExportFuncNameListTy &export_funcs = info->getExportFuncNames(); 52 53 for (RSInfo::ExportVarNameListTy::const_iterator 54 export_var_iter = export_vars.begin(), 55 export_var_end = export_vars.end(); 56 export_var_iter != export_var_end; export_var_iter++) { 57 export_symbols.push_back(*export_var_iter); 58 } 59 60 for (RSInfo::ExportFuncNameListTy::const_iterator 61 export_func_iter = export_funcs.begin(), 62 export_func_end = export_funcs.end(); 63 export_func_iter != export_func_end; export_func_iter++) { 64 export_symbols.push_back(*export_func_iter); 65 } 66 67 // Expanded foreach functions should not be internalized, too. 68 const RSInfo::ExportForeachFuncListTy &export_foreach_func = 69 info->getExportForeachFuncs(); 70 std::vector<std::string> expanded_foreach_funcs; 71 for (RSInfo::ExportForeachFuncListTy::const_iterator 72 foreach_func_iter = export_foreach_func.begin(), 73 foreach_func_end = export_foreach_func.end(); 74 foreach_func_iter != foreach_func_end; foreach_func_iter++) { 75 std::string name(foreach_func_iter->first); 76 expanded_foreach_funcs.push_back(name.append(".expand")); 77 } 78 79 // Need to wait until ForEachExpandList is fully populated to fill in 80 // exported symbols. 81 for (size_t i = 0; i < expanded_foreach_funcs.size(); i++) { 82 export_symbols.push_back(expanded_foreach_funcs[i].c_str()); 83 } 84 85 pPM.add(llvm::createInternalizePass(export_symbols)); 86 87 return true; 88 } 89 90 bool RSCompiler::addExpandForEachPass(Script &pScript, llvm::PassManager &pPM) { 91 // Script passed to RSCompiler must be a RSScript. 92 RSScript &script = static_cast<RSScript &>(pScript); 93 const RSInfo *info = script.getInfo(); 94 llvm::Module &module = script.getSource().getModule(); 95 96 if (info == NULL) { 97 ALOGE("Missing RSInfo in RSScript to run the pass for foreach expansion on " 98 "%s!", module.getModuleIdentifier().c_str()); 99 return false; 100 } 101 102 // Expand ForEach on CPU path to reduce launch overhead. 103 bool pEnableStepOpt = true; 104 pPM.add(createRSForEachExpandPass(info->getExportForeachFuncs(), 105 pEnableStepOpt)); 106 if (script.getEmbedInfo()) 107 pPM.add(createRSEmbedInfoPass(info)); 108 109 return true; 110 } 111 112 bool RSCompiler::beforeAddLTOPasses(Script &pScript, llvm::PassManager &pPM) { 113 if (!addExpandForEachPass(pScript, pPM)) 114 return false; 115 116 if (!addInternalizeSymbolsPass(pScript, pPM)) 117 return false; 118 119 return true; 120 } 121