blob: 7b291f078379fe4b0bce11bef49a0c9a88deadd3 [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"
Zonr Changc72c4dd2012-04-12 15:38:53 +080038#include "bcc/Script.h"
39#include "bcc/Source.h"
40#include "bcc/Support/CompilerConfig.h"
Zonr Changef73a242012-04-12 16:44:01 +080041#include "bcc/Support/Log.h"
Zonr Changc72c4dd2012-04-12 15:38:53 +080042#include "bcc/Support/OutputFile.h"
Chris Wailesb4447cd2014-08-19 16:22:20 -070043#include "bcinfo/MetadataExtractor.h"
Stephen Hinesfb81ec12015-05-18 20:04:23 -070044#include "rsDefines.h"
Loganeb3d12b2010-12-16 06:20:18 +080045
Stephen Hines10ee6af2014-09-09 17:28:23 -070046#include <string>
47
Zonr Changade92772012-04-13 15:58:24 +080048using namespace bcc;
Logan Chienda5e0c32011-06-13 03:47:21 +080049
Zonr Changade92772012-04-13 15:58:24 +080050const char *Compiler::GetErrorString(enum ErrorCode pErrCode) {
Tobias Grosser5b7f52a2013-07-23 14:57:00 -070051 switch (pErrCode) {
52 case kSuccess:
53 return "Successfully compiled.";
54 case kInvalidConfigNoTarget:
Chris Wailes900c6c12014-08-13 15:40:00 -070055 return "Invalid compiler config supplied (getTarget() returns nullptr.) "
Tobias Grosser5b7f52a2013-07-23 14:57:00 -070056 "(missing call to CompilerConfig::initialize()?)";
57 case kErrCreateTargetMachine:
58 return "Failed to create llvm::TargetMachine.";
59 case kErrSwitchTargetMachine:
60 return "Failed to switch llvm::TargetMachine.";
61 case kErrNoTargetMachine:
62 return "Failed to compile the script since there's no available "
63 "TargetMachine. (missing call to Compiler::config()?)";
Tobias Grosser5b7f52a2013-07-23 14:57:00 -070064 case kErrMaterialization:
65 return "Failed to materialize the module.";
66 case kErrInvalidOutputFileState:
67 return "Supplied output file was invalid (in the error state.)";
68 case kErrPrepareOutput:
69 return "Failed to prepare file for output.";
70 case kPrepareCodeGenPass:
71 return "Failed to construct pass list for code-generation.";
Chris Wailesb4447cd2014-08-19 16:22:20 -070072 case kErrCustomPasses:
73 return "Error occurred while adding custom passes.";
Tobias Grosser5b7f52a2013-07-23 14:57:00 -070074 case kErrInvalidSource:
75 return "Error loading input bitcode";
Pirama Arumuga Nainar1e0557a2014-12-02 15:02:18 -080076 case kIllegalGlobalFunction:
77 return "Use of undefined external function";
Pirama Arumuga Nainar9e0f8f02016-04-12 14:04:50 -070078 case kErrInvalidTargetMachine:
79 return "Invalid/unexpected llvm::TargetMachine.";
Logan1f028c02010-11-27 01:02:48 +080080 }
Zonr Changfef9a1b2012-04-13 15:58:24 +080081
Tobias Grosser5b7f52a2013-07-23 14:57:00 -070082 // This assert should never be reached as the compiler verifies that the
83 // above switch coveres all enum values.
David Grossc2ca7422015-05-29 14:54:33 -070084 bccAssert(false && "Unknown error code encountered");
Tobias Grosser5b7f52a2013-07-23 14:57:00 -070085 return "";
Stephen Hines4a68b1c2012-05-03 12:28:14 -070086}
87
Zonr Changade92772012-04-13 15:58:24 +080088//===----------------------------------------------------------------------===//
89// Instance Methods
90//===----------------------------------------------------------------------===//
Chris Wailesb4447cd2014-08-19 16:22:20 -070091Compiler::Compiler() : mTarget(nullptr), mEnableOpt(true) {
Zonr Changfef9a1b2012-04-13 15:58:24 +080092 return;
Logan1f028c02010-11-27 01:02:48 +080093}
94
Chris Wailes900c6c12014-08-13 15:40:00 -070095Compiler::Compiler(const CompilerConfig &pConfig) : mTarget(nullptr),
Chris Wailesb4447cd2014-08-19 16:22:20 -070096 mEnableOpt(true) {
Zonr Changade92772012-04-13 15:58:24 +080097 const std::string &triple = pConfig.getTriple();
98
99 enum ErrorCode err = config(pConfig);
100 if (err != kSuccess) {
101 ALOGE("%s (%s, features: %s)", GetErrorString(err),
102 triple.c_str(), pConfig.getFeatureString().c_str());
103 return;
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700104 }
Zonr Changade92772012-04-13 15:58:24 +0800105
106 return;
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700107}
108
Zonr Changade92772012-04-13 15:58:24 +0800109enum Compiler::ErrorCode Compiler::config(const CompilerConfig &pConfig) {
Chris Wailes900c6c12014-08-13 15:40:00 -0700110 if (pConfig.getTarget() == nullptr) {
Zonr Changade92772012-04-13 15:58:24 +0800111 return kInvalidConfigNoTarget;
Daniel Malea094881f2011-12-14 17:39:16 -0500112 }
113
Zonr Changade92772012-04-13 15:58:24 +0800114 llvm::TargetMachine *new_target =
115 (pConfig.getTarget())->createTargetMachine(pConfig.getTriple(),
116 pConfig.getCPU(),
117 pConfig.getFeatureString(),
118 pConfig.getTargetOptions(),
119 pConfig.getRelocationModel(),
120 pConfig.getCodeModel(),
121 pConfig.getOptimizationLevel());
Daniel Malea094881f2011-12-14 17:39:16 -0500122
Chris Wailes900c6c12014-08-13 15:40:00 -0700123 if (new_target == nullptr) {
124 return ((mTarget != nullptr) ? kErrSwitchTargetMachine :
Chris Wailesb4447cd2014-08-19 16:22:20 -0700125 kErrCreateTargetMachine);
Zonr Changade92772012-04-13 15:58:24 +0800126 }
127
128 // Replace the old TargetMachine.
129 delete mTarget;
130 mTarget = new_target;
131
132 // Adjust register allocation policy according to the optimization level.
Daniel Malea094881f2011-12-14 17:39:16 -0500133 // createFastRegisterAllocator: fast but bad quality
134 // createLinearScanRegisterAllocator: not so fast but good quality
Zonr Changade92772012-04-13 15:58:24 +0800135 if ((pConfig.getOptimizationLevel() == llvm::CodeGenOpt::None)) {
136 llvm::RegisterRegAlloc::setDefault(llvm::createFastRegisterAllocator);
137 } else {
138 llvm::RegisterRegAlloc::setDefault(llvm::createGreedyRegisterAllocator);
Logan1f028c02010-11-27 01:02:48 +0800139 }
140
Zonr Changade92772012-04-13 15:58:24 +0800141 return kSuccess;
Logan Chienda5e0c32011-06-13 03:47:21 +0800142}
143
Zonr Changade92772012-04-13 15:58:24 +0800144Compiler::~Compiler() {
145 delete mTarget;
146}
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700147
Pirama Arumuga Nainar1e0557a2014-12-02 15:02:18 -0800148
David Gross5aefc982015-08-04 10:41:33 -0700149// This function has complete responsibility for creating and executing the
150// exact list of compiler passes.
Chris Wailesb4447cd2014-08-19 16:22:20 -0700151enum Compiler::ErrorCode Compiler::runPasses(Script &pScript,
Pirama Arumuga Nainar98137cc2015-05-06 11:18:56 -0700152 llvm::raw_pwrite_stream &pResult) {
Zonr Changade92772012-04-13 15:58:24 +0800153 // Pass manager for link-time optimization
Dean De Leo1e321862015-11-25 12:35:24 +0000154 llvm::legacy::PassManager transformPasses;
Chris Wailesb4447cd2014-08-19 16:22:20 -0700155
156 // Empty MCContext.
157 llvm::MCContext *mc_context = nullptr;
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700158
Dean De Leo1e321862015-11-25 12:35:24 +0000159 transformPasses.add(
160 createTargetTransformInfoWrapperPass(mTarget->getTargetIRAnalysis()));
Tim Murraybb73b742014-11-04 11:20:10 -0800161
David Gross5aefc982015-08-04 10:41:33 -0700162 // Add some initial custom passes.
Dean De Leo1e321862015-11-25 12:35:24 +0000163 addInvokeHelperPass(transformPasses);
164 addExpandKernelPass(transformPasses);
Dean De Leo09c7a412015-11-25 12:45:45 +0000165 addDebugInfoPass(pScript, transformPasses);
Dean De Leo1e321862015-11-25 12:35:24 +0000166 addInvariantPass(transformPasses);
Dean De Leo7a9a9672015-11-25 12:51:54 +0000167 if (mTarget->getOptLevel() != llvm::CodeGenOpt::None) {
168 if (!addInternalizeSymbolsPass(pScript, transformPasses))
169 return kErrCustomPasses;
170 }
Dean De Leo1e321862015-11-25 12:35:24 +0000171 addGlobalInfoPass(pScript, transformPasses);
Daniel Malea094881f2011-12-14 17:39:16 -0500172
Zonr Changade92772012-04-13 15:58:24 +0800173 if (mTarget->getOptLevel() == llvm::CodeGenOpt::None) {
Dean De Leo1e321862015-11-25 12:35:24 +0000174 transformPasses.add(llvm::createGlobalOptimizerPass());
175 transformPasses.add(llvm::createConstantMergePass());
Chris Wailesb4447cd2014-08-19 16:22:20 -0700176
Zonr Changade92772012-04-13 15:58:24 +0800177 } else {
Tobias Grosser2f6103b2013-07-01 14:01:06 -0700178 // FIXME: Figure out which passes should be executed.
179 llvm::PassManagerBuilder Builder;
Stephen Hines57936132014-11-25 17:54:59 -0800180 Builder.Inliner = llvm::createFunctionInliningPass();
Dean De Leo1e321862015-11-25 12:35:24 +0000181 Builder.populateLTOPassManager(transformPasses);
Tim Murray50f5eb42014-12-09 17:36:24 -0800182
Tim Murray7f59b5f2015-02-12 14:38:05 -0800183 /* FIXME: Reenable autovectorization after rebase.
184 bug 19324423
Tim Murray50f5eb42014-12-09 17:36:24 -0800185 // Add vectorization passes after LTO passes are in
186 // additional flag: -unroll-runtime
Dean De Leo1e321862015-11-25 12:35:24 +0000187 transformPasses.add(llvm::createLoopUnrollPass(-1, 16, 0, 1));
Tim Murray50f5eb42014-12-09 17:36:24 -0800188 // Need to pass appropriate flags here: -scalarize-load-store
Dean De Leo1e321862015-11-25 12:35:24 +0000189 transformPasses.add(llvm::createScalarizerPass());
190 transformPasses.add(llvm::createCFGSimplificationPass());
191 transformPasses.add(llvm::createScopedNoAliasAAPass());
192 transformPasses.add(llvm::createScalarEvolutionAliasAnalysisPass());
Tim Murray50f5eb42014-12-09 17:36:24 -0800193 // additional flags: -slp-vectorize-hor -slp-vectorize-hor-store (unnecessary?)
Dean De Leo1e321862015-11-25 12:35:24 +0000194 transformPasses.add(llvm::createSLPVectorizerPass());
195 transformPasses.add(llvm::createDeadCodeEliminationPass());
196 transformPasses.add(llvm::createInstructionCombiningPass());
Tim Murray7f59b5f2015-02-12 14:38:05 -0800197 */
Zonr Changade92772012-04-13 15:58:24 +0800198 }
199
Pirama Arumuga Nainar8c24f8d2015-03-17 13:11:25 -0700200 // These passes have to come after LTO, since we don't want to examine
201 // functions that are never actually called.
David Gross5aefc982015-08-04 10:41:33 -0700202 if (llvm::Triple(getTargetMachine().getTargetTriple()).getArch() == llvm::Triple::x86_64)
Dean De Leo1e321862015-11-25 12:35:24 +0000203 transformPasses.add(createRSX86_64CallConvPass()); // Add pass to correct calling convention for X86-64.
204 transformPasses.add(createRSIsThreadablePass()); // Add pass to mark script as threadable.
Pirama Arumuga Nainar9fe081b2015-01-27 14:09:19 -0800205
206 // RSEmbedInfoPass needs to come after we have scanned for non-threadable
207 // functions.
208 // Script passed to RSCompiler must be a RSScript.
209 RSScript &script = static_cast<RSScript &>(pScript);
210 if (script.getEmbedInfo())
Dean De Leo1e321862015-11-25 12:35:24 +0000211 transformPasses.add(createRSEmbedInfoPass());
212
213 // Execute the passes.
214 transformPasses.run(pScript.getSource().getModule());
215
216 // Run backend separately to avoid interference between debug metadata
217 // generation and backend initialization.
218 llvm::legacy::PassManager codeGenPasses;
Stephen Hines5db508c2015-01-06 01:42:56 -0800219
Zonr Changade92772012-04-13 15:58:24 +0800220 // Add passes to the pass manager to emit machine code through MC layer.
Dean De Leo1e321862015-11-25 12:35:24 +0000221 if (mTarget->addPassesToEmitMC(codeGenPasses, mc_context, pResult,
Zonr Changade92772012-04-13 15:58:24 +0800222 /* DisableVerify */false)) {
223 return kPrepareCodeGenPass;
224 }
225
Chris Wailesb4447cd2014-08-19 16:22:20 -0700226 // Execute the passes.
Dean De Leo1e321862015-11-25 12:35:24 +0000227 codeGenPasses.run(pScript.getSource().getModule());
Zonr Changade92772012-04-13 15:58:24 +0800228
229 return kSuccess;
Logan Chienda5e0c32011-06-13 03:47:21 +0800230}
Logan Chienda5e0c32011-06-13 03:47:21 +0800231
Zonr Changade92772012-04-13 15:58:24 +0800232enum Compiler::ErrorCode Compiler::compile(Script &pScript,
Pirama Arumuga Nainar98137cc2015-05-06 11:18:56 -0700233 llvm::raw_pwrite_stream &pResult,
Tobias Grosser27fb7ed2013-06-21 18:34:56 -0700234 llvm::raw_ostream *IRStream) {
Zonr Changade92772012-04-13 15:58:24 +0800235 llvm::Module &module = pScript.getSource().getModule();
236 enum ErrorCode err;
Logan Chienda5e0c32011-06-13 03:47:21 +0800237
Chris Wailes900c6c12014-08-13 15:40:00 -0700238 if (mTarget == nullptr) {
Zonr Changade92772012-04-13 15:58:24 +0800239 return kErrNoTargetMachine;
240 }
241
Stephen Hines10ee6af2014-09-09 17:28:23 -0700242 const std::string &triple = module.getTargetTriple();
Pirama Arumuga Nainar8e908932016-03-06 23:05:45 -0800243 const llvm::DataLayout dl = getTargetMachine().createDataLayout();
244 unsigned int pointerSize = dl.getPointerSizeInBits();
Stephen Hines10ee6af2014-09-09 17:28:23 -0700245 if (triple == "armv7-none-linux-gnueabi") {
246 if (pointerSize != 32) {
247 return kErrInvalidSource;
248 }
249 } else if (triple == "aarch64-none-linux-gnueabi") {
250 if (pointerSize != 64) {
251 return kErrInvalidSource;
252 }
253 } else {
254 return kErrInvalidSource;
255 }
256
Pirama Arumuga Nainar9e0f8f02016-04-12 14:04:50 -0700257 if (getTargetMachine().getTargetTriple().getArch() == llvm::Triple::x86) {
258 // Detect and fail if TargetMachine datalayout is different than what we
259 // expect. This is to detect changes in default target layout for x86 and
260 // update X86_CUSTOM_DL_STRING in include/bcc/Config/Config.h appropriately.
261 if (dl.getStringRepresentation().compare(X86_DEFAULT_DL_STRING) != 0) {
262 return kErrInvalidTargetMachine;
263 }
264 }
265
David Grosscf8b2d02015-05-19 11:55:29 -0700266 // Sanitize module's target information.
Pirama Arumuga Nainar8e908932016-03-06 23:05:45 -0800267 module.setTargetTriple(getTargetMachine().getTargetTriple().str());
268 module.setDataLayout(getTargetMachine().createDataLayout());
David Grosscf8b2d02015-05-19 11:55:29 -0700269
Zonr Changade92772012-04-13 15:58:24 +0800270 // Materialize the bitcode module.
Chris Wailes900c6c12014-08-13 15:40:00 -0700271 if (module.getMaterializer() != nullptr) {
Zonr Changade92772012-04-13 15:58:24 +0800272 // A module with non-null materializer means that it is a lazy-load module.
Pirama Arumuga Nainar8e908932016-03-06 23:05:45 -0800273 // Materialize it now. This function returns false when the materialization
274 // is successful.
275 std::error_code ec = module.materializeAll();
Tim Murrayc2074ca2014-04-08 15:39:08 -0700276 if (ec) {
Zonr Changade92772012-04-13 15:58:24 +0800277 ALOGE("Failed to materialize the module `%s'! (%s)",
Tim Murrayc2074ca2014-04-08 15:39:08 -0700278 module.getModuleIdentifier().c_str(), ec.message().c_str());
Zonr Changade92772012-04-13 15:58:24 +0800279 return kErrMaterialization;
280 }
281 }
282
Chris Wailesb4447cd2014-08-19 16:22:20 -0700283 if ((err = runPasses(pScript, pResult)) != kSuccess) {
Zonr Changade92772012-04-13 15:58:24 +0800284 return err;
285 }
286
Chris Wailesb4447cd2014-08-19 16:22:20 -0700287 if (IRStream) {
Tobias Grosser27fb7ed2013-06-21 18:34:56 -0700288 *IRStream << module;
Zonr Changade92772012-04-13 15:58:24 +0800289 }
290
291 return kSuccess;
Logan1f028c02010-11-27 01:02:48 +0800292}
293
Zonr Changade92772012-04-13 15:58:24 +0800294enum Compiler::ErrorCode Compiler::compile(Script &pScript,
Tobias Grosser27fb7ed2013-06-21 18:34:56 -0700295 OutputFile &pResult,
296 llvm::raw_ostream *IRStream) {
Zonr Changade92772012-04-13 15:58:24 +0800297 // Check the state of the specified output file.
298 if (pResult.hasError()) {
299 return kErrInvalidOutputFileState;
300 }
Shih-wei Liao90cd3d12011-06-20 15:43:34 -0700301
Zonr Changade92772012-04-13 15:58:24 +0800302 // Open the output file decorated in llvm::raw_ostream.
Pirama Arumuga Nainar98137cc2015-05-06 11:18:56 -0700303 llvm::raw_pwrite_stream *out = pResult.dup();
Chris Wailes900c6c12014-08-13 15:40:00 -0700304 if (out == nullptr) {
Zonr Changade92772012-04-13 15:58:24 +0800305 return kErrPrepareOutput;
306 }
307
308 // Delegate the request.
Tobias Grosser27fb7ed2013-06-21 18:34:56 -0700309 enum Compiler::ErrorCode err = compile(pScript, *out, IRStream);
Zonr Changade92772012-04-13 15:58:24 +0800310
311 // Close the output before return.
312 delete out;
313
314 return err;
315}
Chris Wailesb4447cd2014-08-19 16:22:20 -0700316
Stephen Hines1bd9f622015-03-18 14:53:10 -0700317bool Compiler::addInternalizeSymbolsPass(Script &pScript, llvm::legacy::PassManager &pPM) {
Chris Wailesb4447cd2014-08-19 16:22:20 -0700318 // Add a pass to internalize the symbols that don't need to have global
319 // visibility.
320 RSScript &script = static_cast<RSScript &>(pScript);
321 llvm::Module &module = script.getSource().getModule();
322 bcinfo::MetadataExtractor me(&module);
323 if (!me.extract()) {
324 bccAssert(false && "Could not extract metadata for module!");
325 return false;
326 }
327
328 // The vector contains the symbols that should not be internalized.
329 std::vector<const char *> export_symbols;
330
Stephen Hines107f50d2015-01-19 21:02:13 -0800331 const char *sf[] = {
Stephen Hinesfb81ec12015-05-18 20:04:23 -0700332 kRoot, // Graphics drawing function or compute kernel.
333 kInit, // Initialization routine called implicitly on startup.
334 kRsDtor, // Static global destructor for a script instance.
335 kRsInfo, // Variable containing string of RS metadata info.
336 kRsGlobalEntries, // Optional number of global variables.
337 kRsGlobalNames, // Optional global variable name info.
338 kRsGlobalAddresses, // Optional global variable address info.
339 kRsGlobalSizes, // Optional global variable size info.
340 kRsGlobalProperties, // Optional global variable properties.
341 nullptr // Must be nullptr-terminated.
Stephen Hines107f50d2015-01-19 21:02:13 -0800342 };
343 const char **special_functions = sf;
Chris Wailesb4447cd2014-08-19 16:22:20 -0700344 // Special RS functions should always be global symbols.
Chris Wailesb4447cd2014-08-19 16:22:20 -0700345 while (*special_functions != nullptr) {
346 export_symbols.push_back(*special_functions);
347 special_functions++;
348 }
349
350 // Visibility of symbols appeared in rs_export_var and rs_export_func should
351 // also be preserved.
352 size_t exportVarCount = me.getExportVarCount();
353 size_t exportFuncCount = me.getExportFuncCount();
354 size_t exportForEachCount = me.getExportForEachSignatureCount();
Matt Wala4e7a5062015-07-30 16:27:51 -0700355 size_t exportReduceCount = me.getExportReduceCount();
David Gross79e1a052016-01-11 14:42:51 -0800356 size_t exportReduceNewCount = me.getExportReduceNewCount();
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();
Matt Wala4e7a5062015-07-30 16:27:51 -0700360 const char **exportReduceNameList = me.getExportReduceNameList();
David Gross79e1a052016-01-11 14:42:51 -0800361 const bcinfo::MetadataExtractor::ReduceNew *exportReduceNewList = me.getExportReduceNewList();
Chris Wailesb4447cd2014-08-19 16:22:20 -0700362 size_t i;
363
364 for (i = 0; i < exportVarCount; ++i) {
365 export_symbols.push_back(exportVarNameList[i]);
366 }
367
368 for (i = 0; i < exportFuncCount; ++i) {
369 export_symbols.push_back(exportFuncNameList[i]);
370 }
371
David Grossc545d6f2016-02-08 13:49:02 -0800372 // Expanded foreach and reduce functions should not be internalized;
David Gross8ca13572016-03-21 14:32:16 -0700373 // nor should general reduction initializer, combiner, and
374 // outconverter functions. keep_funcs keeps the names of these
375 // functions around until createInternalizePass() is finished making
376 // its own copy of the visible symbols.
David Grossc545d6f2016-02-08 13:49:02 -0800377 std::vector<std::string> keep_funcs;
David Gross8ca13572016-03-21 14:32:16 -0700378 keep_funcs.reserve(exportForEachCount + exportReduceCount + exportReduceNewCount*4);
Matt Wala4e7a5062015-07-30 16:27:51 -0700379
Chris Wailesb4447cd2014-08-19 16:22:20 -0700380 for (i = 0; i < exportForEachCount; ++i) {
David Grossc545d6f2016-02-08 13:49:02 -0800381 keep_funcs.push_back(std::string(exportForEachNameList[i]) + ".expand");
Matt Wala4e7a5062015-07-30 16:27:51 -0700382 }
383 for (i = 0; i < exportReduceCount; ++i) {
David Grossc545d6f2016-02-08 13:49:02 -0800384 keep_funcs.push_back(std::string(exportReduceNameList[i]) + ".expand");
Chris Wailesb4447cd2014-08-19 16:22:20 -0700385 }
David Grossc545d6f2016-02-08 13:49:02 -0800386 auto keepFuncsPushBackIfPresent = [&keep_funcs](const char *Name) {
387 if (Name) keep_funcs.push_back(Name);
388 };
David Gross79e1a052016-01-11 14:42:51 -0800389 for (i = 0; i < exportReduceNewCount; ++i) {
David Grossc545d6f2016-02-08 13:49:02 -0800390 keep_funcs.push_back(std::string(exportReduceNewList[i].mAccumulatorName) + ".expand");
David Grossc545d6f2016-02-08 13:49:02 -0800391 keepFuncsPushBackIfPresent(exportReduceNewList[i].mInitializerName);
David Gross8ca13572016-03-21 14:32:16 -0700392 keepFuncsPushBackIfPresent(exportReduceNewList[i].mCombinerName);
David Grossc545d6f2016-02-08 13:49:02 -0800393 keepFuncsPushBackIfPresent(exportReduceNewList[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}