blob: 759844f1fc25e91ef99ba0629f4e236d516b3cb6 [file] [log] [blame]
Logan1f028c02010-11-27 01:02:48 +08001/*
Stephen Hinesdb169182012-01-05 18:46:36 -08002 * Copyright 2010-2012, The Android Open Source Project
Logan1f028c02010-11-27 01:02:48 +08003 *
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
Zonr Changc72c4dd2012-04-12 15:38:53 +080017#include "bcc/Compiler.h"
Logan1f028c02010-11-27 01:02:48 +080018
Zonr Changade92772012-04-13 15:58:24 +080019#include <llvm/Analysis/Passes.h>
Stephen Hines1bd9f622015-03-18 14:53:10 -070020#include <llvm/Analysis/TargetTransformInfo.h>
Zonr Changade92772012-04-13 15:58:24 +080021#include <llvm/CodeGen/RegAllocRegistry.h>
Stephen Hines1bd9f622015-03-18 14:53:10 -070022#include <llvm/IR/LegacyPassManager.h>
Stephen Hinesb730e232013-01-09 15:31:36 -080023#include <llvm/IR/Module.h>
Zonr Changade92772012-04-13 15:58:24 +080024#include <llvm/Support/TargetRegistry.h>
25#include <llvm/Support/raw_ostream.h>
Stephen Hinesb730e232013-01-09 15:31:36 -080026#include <llvm/IR/DataLayout.h>
Stephen Hines57936132014-11-25 17:54:59 -080027#include <llvm/Target/TargetSubtargetInfo.h>
Zonr Changade92772012-04-13 15:58:24 +080028#include <llvm/Target/TargetMachine.h>
29#include <llvm/Transforms/IPO.h>
Tobias Grosser2f6103b2013-07-01 14:01:06 -070030#include <llvm/Transforms/IPO/PassManagerBuilder.h>
Zonr Changade92772012-04-13 15:58:24 +080031#include <llvm/Transforms/Scalar.h>
Tim Murray50f5eb42014-12-09 17:36:24 -080032#include <llvm/Transforms/Vectorize.h>
Logan35849002011-01-15 07:30:43 +080033
Chris Wailesb4447cd2014-08-19 16:22:20 -070034#include "bcc/Assert.h"
Pirama Arumuga Nainar9e0f8f02016-04-12 14:04:50 -070035#include "bcc/Config/Config.h"
Chris Wailesb4447cd2014-08-19 16:22:20 -070036#include "bcc/Renderscript/RSScript.h"
37#include "bcc/Renderscript/RSTransforms.h"
David Gross57fd9f82016-04-08 12:35:41 -070038#include "bcc/Renderscript/RSUtils.h"
Zonr Changc72c4dd2012-04-12 15:38:53 +080039#include "bcc/Script.h"
40#include "bcc/Source.h"
41#include "bcc/Support/CompilerConfig.h"
Zonr Changef73a242012-04-12 16:44:01 +080042#include "bcc/Support/Log.h"
Zonr Changc72c4dd2012-04-12 15:38:53 +080043#include "bcc/Support/OutputFile.h"
Chris Wailesb4447cd2014-08-19 16:22:20 -070044#include "bcinfo/MetadataExtractor.h"
Stephen Hinesfb81ec12015-05-18 20:04:23 -070045#include "rsDefines.h"
Loganeb3d12b2010-12-16 06:20:18 +080046
Stephen Hines10ee6af2014-09-09 17:28:23 -070047#include <string>
48
Zonr Changade92772012-04-13 15:58:24 +080049using namespace bcc;
Logan Chienda5e0c32011-06-13 03:47:21 +080050
Zonr Changade92772012-04-13 15:58:24 +080051const char *Compiler::GetErrorString(enum ErrorCode pErrCode) {
Tobias Grosser5b7f52a2013-07-23 14:57:00 -070052 switch (pErrCode) {
53 case kSuccess:
54 return "Successfully compiled.";
55 case kInvalidConfigNoTarget:
Chris Wailes900c6c12014-08-13 15:40:00 -070056 return "Invalid compiler config supplied (getTarget() returns nullptr.) "
Tobias Grosser5b7f52a2013-07-23 14:57:00 -070057 "(missing call to CompilerConfig::initialize()?)";
58 case kErrCreateTargetMachine:
59 return "Failed to create llvm::TargetMachine.";
60 case kErrSwitchTargetMachine:
61 return "Failed to switch llvm::TargetMachine.";
62 case kErrNoTargetMachine:
63 return "Failed to compile the script since there's no available "
64 "TargetMachine. (missing call to Compiler::config()?)";
Tobias Grosser5b7f52a2013-07-23 14:57:00 -070065 case kErrMaterialization:
66 return "Failed to materialize the module.";
67 case kErrInvalidOutputFileState:
68 return "Supplied output file was invalid (in the error state.)";
69 case kErrPrepareOutput:
70 return "Failed to prepare file for output.";
71 case kPrepareCodeGenPass:
72 return "Failed to construct pass list for code-generation.";
Chris Wailesb4447cd2014-08-19 16:22:20 -070073 case kErrCustomPasses:
74 return "Error occurred while adding custom passes.";
Tobias Grosser5b7f52a2013-07-23 14:57:00 -070075 case kErrInvalidSource:
76 return "Error loading input bitcode";
Pirama Arumuga Nainar1e0557a2014-12-02 15:02:18 -080077 case kIllegalGlobalFunction:
78 return "Use of undefined external function";
Pirama Arumuga Nainar9e0f8f02016-04-12 14:04:50 -070079 case kErrInvalidTargetMachine:
80 return "Invalid/unexpected llvm::TargetMachine.";
Logan1f028c02010-11-27 01:02:48 +080081 }
Zonr Changfef9a1b2012-04-13 15:58:24 +080082
Tobias Grosser5b7f52a2013-07-23 14:57:00 -070083 // This assert should never be reached as the compiler verifies that the
84 // above switch coveres all enum values.
David Grossc2ca7422015-05-29 14:54:33 -070085 bccAssert(false && "Unknown error code encountered");
Tobias Grosser5b7f52a2013-07-23 14:57:00 -070086 return "";
Stephen Hines4a68b1c2012-05-03 12:28:14 -070087}
88
Zonr Changade92772012-04-13 15:58:24 +080089//===----------------------------------------------------------------------===//
90// Instance Methods
91//===----------------------------------------------------------------------===//
Chris Wailesb4447cd2014-08-19 16:22:20 -070092Compiler::Compiler() : mTarget(nullptr), mEnableOpt(true) {
Zonr Changfef9a1b2012-04-13 15:58:24 +080093 return;
Logan1f028c02010-11-27 01:02:48 +080094}
95
Chris Wailes900c6c12014-08-13 15:40:00 -070096Compiler::Compiler(const CompilerConfig &pConfig) : mTarget(nullptr),
Chris Wailesb4447cd2014-08-19 16:22:20 -070097 mEnableOpt(true) {
Zonr Changade92772012-04-13 15:58:24 +080098 const std::string &triple = pConfig.getTriple();
99
100 enum ErrorCode err = config(pConfig);
101 if (err != kSuccess) {
102 ALOGE("%s (%s, features: %s)", GetErrorString(err),
103 triple.c_str(), pConfig.getFeatureString().c_str());
104 return;
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700105 }
Zonr Changade92772012-04-13 15:58:24 +0800106
107 return;
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700108}
109
Zonr Changade92772012-04-13 15:58:24 +0800110enum Compiler::ErrorCode Compiler::config(const CompilerConfig &pConfig) {
Chris Wailes900c6c12014-08-13 15:40:00 -0700111 if (pConfig.getTarget() == nullptr) {
Zonr Changade92772012-04-13 15:58:24 +0800112 return kInvalidConfigNoTarget;
Daniel Malea094881f2011-12-14 17:39:16 -0500113 }
114
Zonr Changade92772012-04-13 15:58:24 +0800115 llvm::TargetMachine *new_target =
116 (pConfig.getTarget())->createTargetMachine(pConfig.getTriple(),
117 pConfig.getCPU(),
118 pConfig.getFeatureString(),
119 pConfig.getTargetOptions(),
120 pConfig.getRelocationModel(),
121 pConfig.getCodeModel(),
122 pConfig.getOptimizationLevel());
Daniel Malea094881f2011-12-14 17:39:16 -0500123
Chris Wailes900c6c12014-08-13 15:40:00 -0700124 if (new_target == nullptr) {
125 return ((mTarget != nullptr) ? kErrSwitchTargetMachine :
Chris Wailesb4447cd2014-08-19 16:22:20 -0700126 kErrCreateTargetMachine);
Zonr Changade92772012-04-13 15:58:24 +0800127 }
128
129 // Replace the old TargetMachine.
130 delete mTarget;
131 mTarget = new_target;
132
133 // Adjust register allocation policy according to the optimization level.
Daniel Malea094881f2011-12-14 17:39:16 -0500134 // createFastRegisterAllocator: fast but bad quality
135 // createLinearScanRegisterAllocator: not so fast but good quality
Zonr Changade92772012-04-13 15:58:24 +0800136 if ((pConfig.getOptimizationLevel() == llvm::CodeGenOpt::None)) {
137 llvm::RegisterRegAlloc::setDefault(llvm::createFastRegisterAllocator);
138 } else {
139 llvm::RegisterRegAlloc::setDefault(llvm::createGreedyRegisterAllocator);
Logan1f028c02010-11-27 01:02:48 +0800140 }
141
Zonr Changade92772012-04-13 15:58:24 +0800142 return kSuccess;
Logan Chienda5e0c32011-06-13 03:47:21 +0800143}
144
Zonr Changade92772012-04-13 15:58:24 +0800145Compiler::~Compiler() {
146 delete mTarget;
147}
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700148
Pirama Arumuga Nainar1e0557a2014-12-02 15:02:18 -0800149
David Gross5aefc982015-08-04 10:41:33 -0700150// This function has complete responsibility for creating and executing the
151// exact list of compiler passes.
Chris Wailesb4447cd2014-08-19 16:22:20 -0700152enum Compiler::ErrorCode Compiler::runPasses(Script &pScript,
Pirama Arumuga Nainar98137cc2015-05-06 11:18:56 -0700153 llvm::raw_pwrite_stream &pResult) {
Zonr Changade92772012-04-13 15:58:24 +0800154 // Pass manager for link-time optimization
Dean De Leo1e321862015-11-25 12:35:24 +0000155 llvm::legacy::PassManager transformPasses;
Chris Wailesb4447cd2014-08-19 16:22:20 -0700156
157 // Empty MCContext.
158 llvm::MCContext *mc_context = nullptr;
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700159
Dean De Leo1e321862015-11-25 12:35:24 +0000160 transformPasses.add(
161 createTargetTransformInfoWrapperPass(mTarget->getTargetIRAnalysis()));
Tim Murraybb73b742014-11-04 11:20:10 -0800162
David Gross5aefc982015-08-04 10:41:33 -0700163 // Add some initial custom passes.
Dean De Leo1e321862015-11-25 12:35:24 +0000164 addInvokeHelperPass(transformPasses);
165 addExpandKernelPass(transformPasses);
Dean De Leo09c7a412015-11-25 12:45:45 +0000166 addDebugInfoPass(pScript, transformPasses);
Dean De Leo1e321862015-11-25 12:35:24 +0000167 addInvariantPass(transformPasses);
Dean De Leo7a9a9672015-11-25 12:51:54 +0000168 if (mTarget->getOptLevel() != llvm::CodeGenOpt::None) {
169 if (!addInternalizeSymbolsPass(pScript, transformPasses))
170 return kErrCustomPasses;
171 }
Dean De Leo1e321862015-11-25 12:35:24 +0000172 addGlobalInfoPass(pScript, transformPasses);
Daniel Malea094881f2011-12-14 17:39:16 -0500173
Zonr Changade92772012-04-13 15:58:24 +0800174 if (mTarget->getOptLevel() == llvm::CodeGenOpt::None) {
Dean De Leo1e321862015-11-25 12:35:24 +0000175 transformPasses.add(llvm::createGlobalOptimizerPass());
176 transformPasses.add(llvm::createConstantMergePass());
Chris Wailesb4447cd2014-08-19 16:22:20 -0700177
Zonr Changade92772012-04-13 15:58:24 +0800178 } else {
Tobias Grosser2f6103b2013-07-01 14:01:06 -0700179 // FIXME: Figure out which passes should be executed.
180 llvm::PassManagerBuilder Builder;
Stephen Hines57936132014-11-25 17:54:59 -0800181 Builder.Inliner = llvm::createFunctionInliningPass();
Dean De Leo1e321862015-11-25 12:35:24 +0000182 Builder.populateLTOPassManager(transformPasses);
Tim Murray50f5eb42014-12-09 17:36:24 -0800183
Tim Murray7f59b5f2015-02-12 14:38:05 -0800184 /* FIXME: Reenable autovectorization after rebase.
185 bug 19324423
Tim Murray50f5eb42014-12-09 17:36:24 -0800186 // Add vectorization passes after LTO passes are in
187 // additional flag: -unroll-runtime
Dean De Leo1e321862015-11-25 12:35:24 +0000188 transformPasses.add(llvm::createLoopUnrollPass(-1, 16, 0, 1));
Tim Murray50f5eb42014-12-09 17:36:24 -0800189 // Need to pass appropriate flags here: -scalarize-load-store
Dean De Leo1e321862015-11-25 12:35:24 +0000190 transformPasses.add(llvm::createScalarizerPass());
191 transformPasses.add(llvm::createCFGSimplificationPass());
192 transformPasses.add(llvm::createScopedNoAliasAAPass());
193 transformPasses.add(llvm::createScalarEvolutionAliasAnalysisPass());
Tim Murray50f5eb42014-12-09 17:36:24 -0800194 // additional flags: -slp-vectorize-hor -slp-vectorize-hor-store (unnecessary?)
Dean De Leo1e321862015-11-25 12:35:24 +0000195 transformPasses.add(llvm::createSLPVectorizerPass());
196 transformPasses.add(llvm::createDeadCodeEliminationPass());
197 transformPasses.add(llvm::createInstructionCombiningPass());
Tim Murray7f59b5f2015-02-12 14:38:05 -0800198 */
Zonr Changade92772012-04-13 15:58:24 +0800199 }
200
Pirama Arumuga Nainar8c24f8d2015-03-17 13:11:25 -0700201 // These passes have to come after LTO, since we don't want to examine
202 // functions that are never actually called.
David Gross5aefc982015-08-04 10:41:33 -0700203 if (llvm::Triple(getTargetMachine().getTargetTriple()).getArch() == llvm::Triple::x86_64)
Dean De Leo1e321862015-11-25 12:35:24 +0000204 transformPasses.add(createRSX86_64CallConvPass()); // Add pass to correct calling convention for X86-64.
205 transformPasses.add(createRSIsThreadablePass()); // Add pass to mark script as threadable.
Pirama Arumuga Nainar9fe081b2015-01-27 14:09:19 -0800206
207 // RSEmbedInfoPass needs to come after we have scanned for non-threadable
208 // functions.
209 // Script passed to RSCompiler must be a RSScript.
210 RSScript &script = static_cast<RSScript &>(pScript);
211 if (script.getEmbedInfo())
Dean De Leo1e321862015-11-25 12:35:24 +0000212 transformPasses.add(createRSEmbedInfoPass());
213
214 // Execute the passes.
215 transformPasses.run(pScript.getSource().getModule());
216
217 // Run backend separately to avoid interference between debug metadata
218 // generation and backend initialization.
219 llvm::legacy::PassManager codeGenPasses;
Stephen Hines5db508c2015-01-06 01:42:56 -0800220
Zonr Changade92772012-04-13 15:58:24 +0800221 // Add passes to the pass manager to emit machine code through MC layer.
Dean De Leo1e321862015-11-25 12:35:24 +0000222 if (mTarget->addPassesToEmitMC(codeGenPasses, mc_context, pResult,
Zonr Changade92772012-04-13 15:58:24 +0800223 /* DisableVerify */false)) {
224 return kPrepareCodeGenPass;
225 }
226
Chris Wailesb4447cd2014-08-19 16:22:20 -0700227 // Execute the passes.
Dean De Leo1e321862015-11-25 12:35:24 +0000228 codeGenPasses.run(pScript.getSource().getModule());
Zonr Changade92772012-04-13 15:58:24 +0800229
230 return kSuccess;
Logan Chienda5e0c32011-06-13 03:47:21 +0800231}
Logan Chienda5e0c32011-06-13 03:47:21 +0800232
Zonr Changade92772012-04-13 15:58:24 +0800233enum Compiler::ErrorCode Compiler::compile(Script &pScript,
Pirama Arumuga Nainar98137cc2015-05-06 11:18:56 -0700234 llvm::raw_pwrite_stream &pResult,
Tobias Grosser27fb7ed2013-06-21 18:34:56 -0700235 llvm::raw_ostream *IRStream) {
Zonr Changade92772012-04-13 15:58:24 +0800236 llvm::Module &module = pScript.getSource().getModule();
237 enum ErrorCode err;
Logan Chienda5e0c32011-06-13 03:47:21 +0800238
Chris Wailes900c6c12014-08-13 15:40:00 -0700239 if (mTarget == nullptr) {
Zonr Changade92772012-04-13 15:58:24 +0800240 return kErrNoTargetMachine;
241 }
242
Stephen Hines10ee6af2014-09-09 17:28:23 -0700243 const std::string &triple = module.getTargetTriple();
Pirama Arumuga Nainar8e908932016-03-06 23:05:45 -0800244 const llvm::DataLayout dl = getTargetMachine().createDataLayout();
245 unsigned int pointerSize = dl.getPointerSizeInBits();
Stephen Hines10ee6af2014-09-09 17:28:23 -0700246 if (triple == "armv7-none-linux-gnueabi") {
247 if (pointerSize != 32) {
248 return kErrInvalidSource;
249 }
250 } else if (triple == "aarch64-none-linux-gnueabi") {
251 if (pointerSize != 64) {
252 return kErrInvalidSource;
253 }
254 } else {
255 return kErrInvalidSource;
256 }
257
Pirama Arumuga Nainar9e0f8f02016-04-12 14:04:50 -0700258 if (getTargetMachine().getTargetTriple().getArch() == llvm::Triple::x86) {
259 // Detect and fail if TargetMachine datalayout is different than what we
260 // expect. This is to detect changes in default target layout for x86 and
261 // update X86_CUSTOM_DL_STRING in include/bcc/Config/Config.h appropriately.
262 if (dl.getStringRepresentation().compare(X86_DEFAULT_DL_STRING) != 0) {
263 return kErrInvalidTargetMachine;
264 }
265 }
266
David Grosscf8b2d02015-05-19 11:55:29 -0700267 // Sanitize module's target information.
Pirama Arumuga Nainar8e908932016-03-06 23:05:45 -0800268 module.setTargetTriple(getTargetMachine().getTargetTriple().str());
269 module.setDataLayout(getTargetMachine().createDataLayout());
David Grosscf8b2d02015-05-19 11:55:29 -0700270
Zonr Changade92772012-04-13 15:58:24 +0800271 // Materialize the bitcode module.
Chris Wailes900c6c12014-08-13 15:40:00 -0700272 if (module.getMaterializer() != nullptr) {
Zonr Changade92772012-04-13 15:58:24 +0800273 // A module with non-null materializer means that it is a lazy-load module.
Pirama Arumuga Nainar8e908932016-03-06 23:05:45 -0800274 // Materialize it now. This function returns false when the materialization
275 // is successful.
276 std::error_code ec = module.materializeAll();
Tim Murrayc2074ca2014-04-08 15:39:08 -0700277 if (ec) {
Zonr Changade92772012-04-13 15:58:24 +0800278 ALOGE("Failed to materialize the module `%s'! (%s)",
Tim Murrayc2074ca2014-04-08 15:39:08 -0700279 module.getModuleIdentifier().c_str(), ec.message().c_str());
Zonr Changade92772012-04-13 15:58:24 +0800280 return kErrMaterialization;
281 }
282 }
283
Chris Wailesb4447cd2014-08-19 16:22:20 -0700284 if ((err = runPasses(pScript, pResult)) != kSuccess) {
Zonr Changade92772012-04-13 15:58:24 +0800285 return err;
286 }
287
Chris Wailesb4447cd2014-08-19 16:22:20 -0700288 if (IRStream) {
Tobias Grosser27fb7ed2013-06-21 18:34:56 -0700289 *IRStream << module;
Zonr Changade92772012-04-13 15:58:24 +0800290 }
291
292 return kSuccess;
Logan1f028c02010-11-27 01:02:48 +0800293}
294
Zonr Changade92772012-04-13 15:58:24 +0800295enum Compiler::ErrorCode Compiler::compile(Script &pScript,
Tobias Grosser27fb7ed2013-06-21 18:34:56 -0700296 OutputFile &pResult,
297 llvm::raw_ostream *IRStream) {
Zonr Changade92772012-04-13 15:58:24 +0800298 // Check the state of the specified output file.
299 if (pResult.hasError()) {
300 return kErrInvalidOutputFileState;
301 }
Shih-wei Liao90cd3d12011-06-20 15:43:34 -0700302
Zonr Changade92772012-04-13 15:58:24 +0800303 // Open the output file decorated in llvm::raw_ostream.
Pirama Arumuga Nainar98137cc2015-05-06 11:18:56 -0700304 llvm::raw_pwrite_stream *out = pResult.dup();
Chris Wailes900c6c12014-08-13 15:40:00 -0700305 if (out == nullptr) {
Zonr Changade92772012-04-13 15:58:24 +0800306 return kErrPrepareOutput;
307 }
308
309 // Delegate the request.
Tobias Grosser27fb7ed2013-06-21 18:34:56 -0700310 enum Compiler::ErrorCode err = compile(pScript, *out, IRStream);
Zonr Changade92772012-04-13 15:58:24 +0800311
312 // Close the output before return.
313 delete out;
314
315 return err;
316}
Chris Wailesb4447cd2014-08-19 16:22:20 -0700317
Stephen Hines1bd9f622015-03-18 14:53:10 -0700318bool Compiler::addInternalizeSymbolsPass(Script &pScript, llvm::legacy::PassManager &pPM) {
Chris Wailesb4447cd2014-08-19 16:22:20 -0700319 // Add a pass to internalize the symbols that don't need to have global
320 // visibility.
321 RSScript &script = static_cast<RSScript &>(pScript);
322 llvm::Module &module = script.getSource().getModule();
323 bcinfo::MetadataExtractor me(&module);
324 if (!me.extract()) {
325 bccAssert(false && "Could not extract metadata for module!");
326 return false;
327 }
328
329 // The vector contains the symbols that should not be internalized.
330 std::vector<const char *> export_symbols;
331
Stephen Hines107f50d2015-01-19 21:02:13 -0800332 const char *sf[] = {
Stephen Hinesfb81ec12015-05-18 20:04:23 -0700333 kRoot, // Graphics drawing function or compute kernel.
334 kInit, // Initialization routine called implicitly on startup.
335 kRsDtor, // Static global destructor for a script instance.
336 kRsInfo, // Variable containing string of RS metadata info.
337 kRsGlobalEntries, // Optional number of global variables.
338 kRsGlobalNames, // Optional global variable name info.
339 kRsGlobalAddresses, // Optional global variable address info.
340 kRsGlobalSizes, // Optional global variable size info.
341 kRsGlobalProperties, // Optional global variable properties.
342 nullptr // Must be nullptr-terminated.
Stephen Hines107f50d2015-01-19 21:02:13 -0800343 };
344 const char **special_functions = sf;
Chris Wailesb4447cd2014-08-19 16:22:20 -0700345 // Special RS functions should always be global symbols.
Chris Wailesb4447cd2014-08-19 16:22:20 -0700346 while (*special_functions != nullptr) {
347 export_symbols.push_back(*special_functions);
348 special_functions++;
349 }
350
351 // Visibility of symbols appeared in rs_export_var and rs_export_func should
352 // also be preserved.
353 size_t exportVarCount = me.getExportVarCount();
354 size_t exportFuncCount = me.getExportFuncCount();
355 size_t exportForEachCount = me.getExportForEachSignatureCount();
Matt Wala4e7a5062015-07-30 16:27:51 -0700356 size_t exportReduceCount = me.getExportReduceCount();
Chris Wailesb4447cd2014-08-19 16:22:20 -0700357 const char **exportVarNameList = me.getExportVarNameList();
358 const char **exportFuncNameList = me.getExportFuncNameList();
359 const char **exportForEachNameList = me.getExportForEachNameList();
David Grossa48ea362016-06-02 14:46:55 -0700360 const bcinfo::MetadataExtractor::Reduce *exportReduceList = me.getExportReduceList();
Chris Wailesb4447cd2014-08-19 16:22:20 -0700361 size_t i;
362
363 for (i = 0; i < exportVarCount; ++i) {
364 export_symbols.push_back(exportVarNameList[i]);
365 }
366
367 for (i = 0; i < exportFuncCount; ++i) {
368 export_symbols.push_back(exportFuncNameList[i]);
369 }
370
David Grossa48ea362016-06-02 14:46:55 -0700371 // Expanded foreach functions should not be internalized; nor should
372 // general reduction initializer, combiner, and outconverter
373 // functions. keep_funcs keeps the names of these functions around
374 // until createInternalizePass() is finished making its own copy of
375 // the visible symbols.
David Grossc545d6f2016-02-08 13:49:02 -0800376 std::vector<std::string> keep_funcs;
David Grossa48ea362016-06-02 14:46:55 -0700377 keep_funcs.reserve(exportForEachCount + exportReduceCount*4);
Matt Wala4e7a5062015-07-30 16:27:51 -0700378
Chris Wailesb4447cd2014-08-19 16:22:20 -0700379 for (i = 0; i < exportForEachCount; ++i) {
David Grossc545d6f2016-02-08 13:49:02 -0800380 keep_funcs.push_back(std::string(exportForEachNameList[i]) + ".expand");
Matt Wala4e7a5062015-07-30 16:27:51 -0700381 }
David Grossc545d6f2016-02-08 13:49:02 -0800382 auto keepFuncsPushBackIfPresent = [&keep_funcs](const char *Name) {
383 if (Name) keep_funcs.push_back(Name);
384 };
David Grossa48ea362016-06-02 14:46:55 -0700385 for (i = 0; i < exportReduceCount; ++i) {
386 keep_funcs.push_back(std::string(exportReduceList[i].mAccumulatorName) + ".expand");
387 keepFuncsPushBackIfPresent(exportReduceList[i].mInitializerName);
388 if (exportReduceList[i].mCombinerName != nullptr) {
389 keep_funcs.push_back(exportReduceList[i].mCombinerName);
David Gross57fd9f82016-04-08 12:35:41 -0700390 } else {
David Grossa48ea362016-06-02 14:46:55 -0700391 keep_funcs.push_back(nameReduceCombinerFromAccumulator(exportReduceList[i].mAccumulatorName));
David Gross57fd9f82016-04-08 12:35:41 -0700392 }
David Grossa48ea362016-06-02 14:46:55 -0700393 keepFuncsPushBackIfPresent(exportReduceList[i].mOutConverterName);
David Gross79e1a052016-01-11 14:42:51 -0800394 }
Chris Wailesb4447cd2014-08-19 16:22:20 -0700395
David Grossc545d6f2016-02-08 13:49:02 -0800396 for (auto &symbol_name : keep_funcs) {
Matt Wala4e7a5062015-07-30 16:27:51 -0700397 export_symbols.push_back(symbol_name.c_str());
Chris Wailesb4447cd2014-08-19 16:22:20 -0700398 }
399
Pirama Arumuga Nainar10f2a8f2016-02-03 15:51:51 -0800400 // http://b/26165616 - WAR for this bug defines the __truncxfhf2 function in
401 // frameworks/rs/driver/runtime. Don't internalize this function for x86, so
402 // that a script can find and link against it.
403 llvm::Triple triple(getTargetMachine().getTargetTriple());
404 if (triple.getArch() == llvm::Triple::x86) {
405 export_symbols.push_back("__truncxfhf2");
406 }
407
Chris Wailesb4447cd2014-08-19 16:22:20 -0700408 pPM.add(llvm::createInternalizePass(export_symbols));
409
410 return true;
411}
412
David Gross5aefc982015-08-04 10:41:33 -0700413void Compiler::addInvokeHelperPass(llvm::legacy::PassManager &pPM) {
Tim Murrayb7bce742014-11-03 16:17:30 -0800414 llvm::Triple arch(getTargetMachine().getTargetTriple());
415 if (arch.isArch64Bit()) {
416 pPM.add(createRSInvokeHelperPass());
417 }
Tim Murrayb7bce742014-11-03 16:17:30 -0800418}
419
Dean De Leo09c7a412015-11-25 12:45:45 +0000420void Compiler::addDebugInfoPass(Script &pScript, llvm::legacy::PassManager &pPM) {
421 if (pScript.getSource().getDebugInfoEnabled())
422 pPM.add(createRSAddDebugInfoPass());
423}
424
Matt Wala4e7a5062015-07-30 16:27:51 -0700425void Compiler::addExpandKernelPass(llvm::legacy::PassManager &pPM) {
426 // Expand ForEach and reduce on CPU path to reduce launch overhead.
Chris Wailesb4447cd2014-08-19 16:22:20 -0700427 bool pEnableStepOpt = true;
Matt Wala4e7a5062015-07-30 16:27:51 -0700428 pPM.add(createRSKernelExpandPass(pEnableStepOpt));
Chris Wailesb4447cd2014-08-19 16:22:20 -0700429}
430
David Gross5aefc982015-08-04 10:41:33 -0700431void Compiler::addGlobalInfoPass(Script &pScript, llvm::legacy::PassManager &pPM) {
Stephen Hines750ee652015-04-16 16:24:18 -0700432 // Add additional information about RS global variables inside the Module.
433 RSScript &script = static_cast<RSScript &>(pScript);
434 if (script.getEmbedGlobalInfo()) {
435 pPM.add(createRSGlobalInfoPass(script.getEmbedGlobalInfoSkipConstant()));
436 }
Stephen Hines750ee652015-04-16 16:24:18 -0700437}
438
David Gross5aefc982015-08-04 10:41:33 -0700439void Compiler::addInvariantPass(llvm::legacy::PassManager &pPM) {
David Gross1d93a192015-03-25 14:59:27 -0700440 // Mark Loads from RsExpandKernelDriverInfo as "load.invariant".
441 // Should run after ExpandForEach and before inlining.
442 pPM.add(createRSInvariantPass());
Pirama Arumuga Nainar8c24f8d2015-03-17 13:11:25 -0700443}
Pirama Arumuga Nainarebff2ea2015-05-21 15:45:05 -0700444
445enum Compiler::ErrorCode Compiler::screenGlobalFunctions(Script &pScript) {
446 llvm::Module &module = pScript.getSource().getModule();
447
448 // Materialize the bitcode module in case this is a lazy-load module. Do not
449 // clear the materializer by calling materializeAllPermanently since the
450 // runtime library has not been merged into the module yet.
451 if (module.getMaterializer() != nullptr) {
452 std::error_code ec = module.materializeAll();
453 if (ec) {
454 ALOGE("Failed to materialize module `%s' when screening globals! (%s)",
455 module.getModuleIdentifier().c_str(), ec.message().c_str());
456 return kErrMaterialization;
457 }
458 }
459
460 // Add pass to check for illegal function calls.
461 llvm::legacy::PassManager pPM;
462 pPM.add(createRSScreenFunctionsPass());
463 pPM.run(module);
464
465 return kSuccess;
466
467}
Pirama Arumuga Nainar9e0f8f02016-04-12 14:04:50 -0700468
469void Compiler::translateGEPs(Script &pScript) {
470 llvm::legacy::PassManager pPM;
471 pPM.add(createRSX86TranslateGEPPass());
472
473 // Materialization done in screenGlobalFunctions above.
474 pPM.run(pScript.getSource().getModule());
475}