Zonr Chang | fef9a1b | 2012-04-13 15:58:24 +0800 | [diff] [blame^] | 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 "RSCompiler.h" |
| 18 | |
| 19 | #include <llvm/Module.h> |
| 20 | #include <llvm/PassManager.h> |
| 21 | #include <llvm/Transforms/IPO.h> |
| 22 | |
| 23 | #include "DebugHelper.h" |
| 24 | #include "RSExecutable.h" |
| 25 | #include "RSInfo.h" |
| 26 | #include "RSScript.h" |
| 27 | #include "RSTransforms.h" |
| 28 | #include "Source.h" |
| 29 | |
| 30 | using namespace bcc; |
| 31 | |
| 32 | bool RSCompiler::beforeAddLTOPasses(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 | export_symbols.push_back(expanded_foreach_funcs.back().c_str()); |
| 78 | } |
| 79 | |
| 80 | pPM.add(llvm::createInternalizePass(export_symbols)); |
| 81 | |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | bool RSCompiler::beforeExecuteLTOPasses(Script &pScript, |
| 86 | llvm::PassManager &pPM) { |
| 87 | // Execute a pass to expand foreach-able functions |
| 88 | llvm::PassManager rs_passes; |
| 89 | |
| 90 | // Script passed to RSCompiler must be a RSScript. |
| 91 | RSScript &script = static_cast<RSScript &>(pScript); |
| 92 | const RSInfo *info = script.getInfo(); |
| 93 | llvm::Module &module = script.getSource().getModule(); |
| 94 | |
| 95 | if (info == NULL) { |
| 96 | ALOGE("Missing RSInfo in RSScript to run the pass for foreach expansion on " |
| 97 | "%s!", module.getModuleIdentifier().c_str()); |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | // Expand ForEach on CPU path to reduce launch overhead. |
| 102 | rs_passes.add(createRSForEachExpandPass(info->getExportForeachFuncs())); |
| 103 | |
| 104 | // Execute the pass. |
| 105 | rs_passes.run(module); |
| 106 | |
| 107 | return true; |
| 108 | } |