Zonr Chang | ade9277 | 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 | |
Stephen Hines | e198abe | 2012-07-27 18:05:41 -0700 | [diff] [blame] | 17 | #include "bcc/Renderscript/RSCompiler.h" |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 18 | |
Stephen Hines | b730e23 | 2013-01-09 15:31:36 -0800 | [diff] [blame] | 19 | #include <llvm/IR/Module.h> |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 20 | #include <llvm/PassManager.h> |
| 21 | #include <llvm/Transforms/IPO.h> |
| 22 | |
Stephen Hines | 25eb586 | 2014-05-08 18:25:50 -0700 | [diff] [blame] | 23 | #include "bcc/Assert.h" |
Stephen Hines | e198abe | 2012-07-27 18:05:41 -0700 | [diff] [blame] | 24 | #include "bcc/Renderscript/RSExecutable.h" |
Stephen Hines | e198abe | 2012-07-27 18:05:41 -0700 | [diff] [blame] | 25 | #include "bcc/Renderscript/RSScript.h" |
| 26 | #include "bcc/Renderscript/RSTransforms.h" |
Zonr Chang | c72c4dd | 2012-04-12 15:38:53 +0800 | [diff] [blame] | 27 | #include "bcc/Source.h" |
Zonr Chang | ef73a24 | 2012-04-12 16:44:01 +0800 | [diff] [blame] | 28 | #include "bcc/Support/Log.h" |
Stephen Hines | 25eb586 | 2014-05-08 18:25:50 -0700 | [diff] [blame] | 29 | #include "bcinfo/MetadataExtractor.h" |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 30 | |
| 31 | using namespace bcc; |
| 32 | |
Tobias Grosser | 6a5fa14 | 2013-07-23 15:19:26 -0700 | [diff] [blame] | 33 | bool RSCompiler::addInternalizeSymbolsPass(Script &pScript, llvm::PassManager &pPM) { |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 34 | // Add a pass to internalize the symbols that don't need to have global |
| 35 | // visibility. |
| 36 | RSScript &script = static_cast<RSScript &>(pScript); |
Stephen Hines | 25eb586 | 2014-05-08 18:25:50 -0700 | [diff] [blame] | 37 | llvm::Module &module = script.getSource().getModule(); |
| 38 | bcinfo::MetadataExtractor me(&module); |
| 39 | if (!me.extract()) { |
| 40 | bccAssert(false && "Could not extract metadata for module!"); |
| 41 | return false; |
| 42 | } |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 43 | |
| 44 | // The vector contains the symbols that should not be internalized. |
| 45 | std::vector<const char *> export_symbols; |
| 46 | |
| 47 | // Special RS functions should always be global symbols. |
| 48 | const char **special_functions = RSExecutable::SpecialFunctionNames; |
| 49 | while (*special_functions != NULL) { |
| 50 | export_symbols.push_back(*special_functions); |
| 51 | special_functions++; |
| 52 | } |
| 53 | |
| 54 | // Visibility of symbols appeared in rs_export_var and rs_export_func should |
| 55 | // also be preserved. |
Stephen Hines | 25eb586 | 2014-05-08 18:25:50 -0700 | [diff] [blame] | 56 | size_t exportVarCount = me.getExportVarCount(); |
| 57 | size_t exportFuncCount = me.getExportFuncCount(); |
| 58 | size_t exportForEachCount = me.getExportForEachSignatureCount(); |
| 59 | const char **exportVarNameList = me.getExportVarNameList(); |
| 60 | const char **exportFuncNameList = me.getExportFuncNameList(); |
| 61 | const char **exportForEachNameList = me.getExportForEachNameList(); |
| 62 | size_t i; |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 63 | |
Stephen Hines | 25eb586 | 2014-05-08 18:25:50 -0700 | [diff] [blame] | 64 | for (i = 0; i < exportVarCount; ++i) { |
| 65 | export_symbols.push_back(exportVarNameList[i]); |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 66 | } |
| 67 | |
Stephen Hines | 25eb586 | 2014-05-08 18:25:50 -0700 | [diff] [blame] | 68 | for (i = 0; i < exportFuncCount; ++i) { |
| 69 | export_symbols.push_back(exportFuncNameList[i]); |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | // Expanded foreach functions should not be internalized, too. |
Stephen Hines | 25eb586 | 2014-05-08 18:25:50 -0700 | [diff] [blame] | 73 | // expanded_foreach_funcs keeps the .expand version of the kernel names |
| 74 | // around until createInternalizePass() is finished making its own |
| 75 | // copy of the visible symbols. |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 76 | std::vector<std::string> expanded_foreach_funcs; |
Stephen Hines | 25eb586 | 2014-05-08 18:25:50 -0700 | [diff] [blame] | 77 | for (i = 0; i < exportForEachCount; ++i) { |
| 78 | expanded_foreach_funcs.push_back( |
| 79 | std::string(exportForEachNameList[i]) + ".expand"); |
Stephen Hines | c104ec9 | 2012-04-25 19:11:33 -0700 | [diff] [blame] | 80 | export_symbols.push_back(expanded_foreach_funcs[i].c_str()); |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 81 | } |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 82 | pPM.add(llvm::createInternalizePass(export_symbols)); |
| 83 | |
| 84 | return true; |
| 85 | } |
| 86 | |
Tobias Grosser | 6a5fa14 | 2013-07-23 15:19:26 -0700 | [diff] [blame] | 87 | bool RSCompiler::addExpandForEachPass(Script &pScript, llvm::PassManager &pPM) { |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 88 | // Script passed to RSCompiler must be a RSScript. |
| 89 | RSScript &script = static_cast<RSScript &>(pScript); |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 90 | |
| 91 | // Expand ForEach on CPU path to reduce launch overhead. |
Tobias Grosser | 6a5fa14 | 2013-07-23 15:19:26 -0700 | [diff] [blame] | 92 | bool pEnableStepOpt = true; |
Stephen Hines | 25eb586 | 2014-05-08 18:25:50 -0700 | [diff] [blame] | 93 | pPM.add(createRSForEachExpandPass(pEnableStepOpt)); |
Tobias Grosser | 6a5fa14 | 2013-07-23 15:19:26 -0700 | [diff] [blame] | 94 | if (script.getEmbedInfo()) |
Stephen Hines | 1253c19 | 2014-05-08 18:18:01 -0700 | [diff] [blame] | 95 | pPM.add(createRSEmbedInfoPass()); |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 96 | |
Tobias Grosser | 6a5fa14 | 2013-07-23 15:19:26 -0700 | [diff] [blame] | 97 | return true; |
| 98 | } |
| 99 | |
| 100 | bool RSCompiler::beforeAddLTOPasses(Script &pScript, llvm::PassManager &pPM) { |
| 101 | if (!addExpandForEachPass(pScript, pPM)) |
| 102 | return false; |
| 103 | |
| 104 | if (!addInternalizeSymbolsPass(pScript, pPM)) |
| 105 | return false; |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 106 | |
| 107 | return true; |
| 108 | } |