blob: 59473722960bebe916c5ee47bf04c42895b32a4a [file] [log] [blame]
Zonr Changade92772012-04-13 15:58:24 +08001/*
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 Hinese198abe2012-07-27 18:05:41 -070017#include "bcc/Renderscript/RSCompiler.h"
Zonr Changade92772012-04-13 15:58:24 +080018
Stephen Hinesb730e232013-01-09 15:31:36 -080019#include <llvm/IR/Module.h>
Zonr Changade92772012-04-13 15:58:24 +080020#include <llvm/PassManager.h>
21#include <llvm/Transforms/IPO.h>
22
Stephen Hines25eb5862014-05-08 18:25:50 -070023#include "bcc/Assert.h"
Stephen Hinese198abe2012-07-27 18:05:41 -070024#include "bcc/Renderscript/RSExecutable.h"
Stephen Hinese198abe2012-07-27 18:05:41 -070025#include "bcc/Renderscript/RSScript.h"
26#include "bcc/Renderscript/RSTransforms.h"
Zonr Changc72c4dd2012-04-12 15:38:53 +080027#include "bcc/Source.h"
Zonr Changef73a242012-04-12 16:44:01 +080028#include "bcc/Support/Log.h"
Stephen Hines25eb5862014-05-08 18:25:50 -070029#include "bcinfo/MetadataExtractor.h"
Zonr Changade92772012-04-13 15:58:24 +080030
31using namespace bcc;
32
Tobias Grosser6a5fa142013-07-23 15:19:26 -070033bool RSCompiler::addInternalizeSymbolsPass(Script &pScript, llvm::PassManager &pPM) {
Zonr Changade92772012-04-13 15:58:24 +080034 // Add a pass to internalize the symbols that don't need to have global
35 // visibility.
36 RSScript &script = static_cast<RSScript &>(pScript);
Stephen Hines25eb5862014-05-08 18:25:50 -070037 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 Changade92772012-04-13 15:58:24 +080043
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 Hines25eb5862014-05-08 18:25:50 -070056 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 Changade92772012-04-13 15:58:24 +080063
Stephen Hines25eb5862014-05-08 18:25:50 -070064 for (i = 0; i < exportVarCount; ++i) {
65 export_symbols.push_back(exportVarNameList[i]);
Zonr Changade92772012-04-13 15:58:24 +080066 }
67
Stephen Hines25eb5862014-05-08 18:25:50 -070068 for (i = 0; i < exportFuncCount; ++i) {
69 export_symbols.push_back(exportFuncNameList[i]);
Zonr Changade92772012-04-13 15:58:24 +080070 }
71
72 // Expanded foreach functions should not be internalized, too.
Stephen Hines25eb5862014-05-08 18:25:50 -070073 // 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 Changade92772012-04-13 15:58:24 +080076 std::vector<std::string> expanded_foreach_funcs;
Stephen Hines25eb5862014-05-08 18:25:50 -070077 for (i = 0; i < exportForEachCount; ++i) {
78 expanded_foreach_funcs.push_back(
79 std::string(exportForEachNameList[i]) + ".expand");
Stephen Hinesc104ec92012-04-25 19:11:33 -070080 export_symbols.push_back(expanded_foreach_funcs[i].c_str());
Zonr Changade92772012-04-13 15:58:24 +080081 }
Zonr Changade92772012-04-13 15:58:24 +080082 pPM.add(llvm::createInternalizePass(export_symbols));
83
84 return true;
85}
86
Tobias Grosser6a5fa142013-07-23 15:19:26 -070087bool RSCompiler::addExpandForEachPass(Script &pScript, llvm::PassManager &pPM) {
Zonr Changade92772012-04-13 15:58:24 +080088 // Script passed to RSCompiler must be a RSScript.
89 RSScript &script = static_cast<RSScript &>(pScript);
Zonr Changade92772012-04-13 15:58:24 +080090
91 // Expand ForEach on CPU path to reduce launch overhead.
Tobias Grosser6a5fa142013-07-23 15:19:26 -070092 bool pEnableStepOpt = true;
Stephen Hines25eb5862014-05-08 18:25:50 -070093 pPM.add(createRSForEachExpandPass(pEnableStepOpt));
Tobias Grosser6a5fa142013-07-23 15:19:26 -070094 if (script.getEmbedInfo())
Stephen Hines1253c192014-05-08 18:18:01 -070095 pPM.add(createRSEmbedInfoPass());
Zonr Changade92772012-04-13 15:58:24 +080096
Tobias Grosser6a5fa142013-07-23 15:19:26 -070097 return true;
98}
99
100bool 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 Changade92772012-04-13 15:58:24 +0800106
107 return true;
108}