Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 1 | /* |
Stephen Hines | db16918 | 2012-01-05 18:46:36 -0800 | [diff] [blame] | 2 | * Copyright 2010-2012, The Android Open Source Project |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 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 | |
Zonr Chang | c72c4dd | 2012-04-12 15:38:53 +0800 | [diff] [blame] | 17 | #include "bcc/Compiler.h" |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 18 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 19 | #include <llvm/Analysis/Passes.h> |
Stephen Hines | 1bd9f62 | 2015-03-18 14:53:10 -0700 | [diff] [blame] | 20 | #include <llvm/Analysis/TargetTransformInfo.h> |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 21 | #include <llvm/CodeGen/RegAllocRegistry.h> |
Stephen Hines | 1bd9f62 | 2015-03-18 14:53:10 -0700 | [diff] [blame] | 22 | #include <llvm/IR/LegacyPassManager.h> |
Stephen Hines | b730e23 | 2013-01-09 15:31:36 -0800 | [diff] [blame] | 23 | #include <llvm/IR/Module.h> |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 24 | #include <llvm/Support/TargetRegistry.h> |
| 25 | #include <llvm/Support/raw_ostream.h> |
Stephen Hines | b730e23 | 2013-01-09 15:31:36 -0800 | [diff] [blame] | 26 | #include <llvm/IR/DataLayout.h> |
Stephen Hines | 5793613 | 2014-11-25 17:54:59 -0800 | [diff] [blame] | 27 | #include <llvm/Target/TargetSubtargetInfo.h> |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 28 | #include <llvm/Target/TargetMachine.h> |
| 29 | #include <llvm/Transforms/IPO.h> |
Tobias Grosser | 2f6103b | 2013-07-01 14:01:06 -0700 | [diff] [blame] | 30 | #include <llvm/Transforms/IPO/PassManagerBuilder.h> |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 31 | #include <llvm/Transforms/Scalar.h> |
Tim Murray | 50f5eb4 | 2014-12-09 17:36:24 -0800 | [diff] [blame] | 32 | #include <llvm/Transforms/Vectorize.h> |
Logan | 3584900 | 2011-01-15 07:30:43 +0800 | [diff] [blame] | 33 | |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 34 | #include "bcc/Assert.h" |
Pirama Arumuga Nainar | 9e0f8f0 | 2016-04-12 14:04:50 -0700 | [diff] [blame] | 35 | #include "bcc/Config/Config.h" |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 36 | #include "bcc/Renderscript/RSScript.h" |
| 37 | #include "bcc/Renderscript/RSTransforms.h" |
David Gross | 57fd9f8 | 2016-04-08 12:35:41 -0700 | [diff] [blame] | 38 | #include "bcc/Renderscript/RSUtils.h" |
Zonr Chang | c72c4dd | 2012-04-12 15:38:53 +0800 | [diff] [blame] | 39 | #include "bcc/Script.h" |
| 40 | #include "bcc/Source.h" |
| 41 | #include "bcc/Support/CompilerConfig.h" |
Zonr Chang | ef73a24 | 2012-04-12 16:44:01 +0800 | [diff] [blame] | 42 | #include "bcc/Support/Log.h" |
Zonr Chang | c72c4dd | 2012-04-12 15:38:53 +0800 | [diff] [blame] | 43 | #include "bcc/Support/OutputFile.h" |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 44 | #include "bcinfo/MetadataExtractor.h" |
Stephen Hines | fb81ec1 | 2015-05-18 20:04:23 -0700 | [diff] [blame] | 45 | #include "rsDefines.h" |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 46 | |
Stephen Hines | 10ee6af | 2014-09-09 17:28:23 -0700 | [diff] [blame] | 47 | #include <string> |
| 48 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 49 | using namespace bcc; |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 50 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 51 | const char *Compiler::GetErrorString(enum ErrorCode pErrCode) { |
Tobias Grosser | 5b7f52a | 2013-07-23 14:57:00 -0700 | [diff] [blame] | 52 | switch (pErrCode) { |
| 53 | case kSuccess: |
| 54 | return "Successfully compiled."; |
| 55 | case kInvalidConfigNoTarget: |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 56 | return "Invalid compiler config supplied (getTarget() returns nullptr.) " |
Tobias Grosser | 5b7f52a | 2013-07-23 14:57:00 -0700 | [diff] [blame] | 57 | "(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 Grosser | 5b7f52a | 2013-07-23 14:57:00 -0700 | [diff] [blame] | 65 | 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 Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 73 | case kErrCustomPasses: |
| 74 | return "Error occurred while adding custom passes."; |
Tobias Grosser | 5b7f52a | 2013-07-23 14:57:00 -0700 | [diff] [blame] | 75 | case kErrInvalidSource: |
| 76 | return "Error loading input bitcode"; |
Pirama Arumuga Nainar | 1e0557a | 2014-12-02 15:02:18 -0800 | [diff] [blame] | 77 | case kIllegalGlobalFunction: |
| 78 | return "Use of undefined external function"; |
Pirama Arumuga Nainar | 9e0f8f0 | 2016-04-12 14:04:50 -0700 | [diff] [blame] | 79 | case kErrInvalidTargetMachine: |
| 80 | return "Invalid/unexpected llvm::TargetMachine."; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 81 | } |
Zonr Chang | fef9a1b | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 82 | |
Tobias Grosser | 5b7f52a | 2013-07-23 14:57:00 -0700 | [diff] [blame] | 83 | // This assert should never be reached as the compiler verifies that the |
| 84 | // above switch coveres all enum values. |
David Gross | c2ca742 | 2015-05-29 14:54:33 -0700 | [diff] [blame] | 85 | bccAssert(false && "Unknown error code encountered"); |
Tobias Grosser | 5b7f52a | 2013-07-23 14:57:00 -0700 | [diff] [blame] | 86 | return ""; |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 89 | //===----------------------------------------------------------------------===// |
| 90 | // Instance Methods |
| 91 | //===----------------------------------------------------------------------===// |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 92 | Compiler::Compiler() : mTarget(nullptr), mEnableOpt(true) { |
Zonr Chang | fef9a1b | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 93 | return; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 94 | } |
| 95 | |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 96 | Compiler::Compiler(const CompilerConfig &pConfig) : mTarget(nullptr), |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 97 | mEnableOpt(true) { |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 98 | 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 Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 105 | } |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 106 | |
| 107 | return; |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 110 | enum Compiler::ErrorCode Compiler::config(const CompilerConfig &pConfig) { |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 111 | if (pConfig.getTarget() == nullptr) { |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 112 | return kInvalidConfigNoTarget; |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 113 | } |
| 114 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 115 | 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 Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 123 | |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 124 | if (new_target == nullptr) { |
| 125 | return ((mTarget != nullptr) ? kErrSwitchTargetMachine : |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 126 | kErrCreateTargetMachine); |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 127 | } |
| 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 Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 134 | // createFastRegisterAllocator: fast but bad quality |
| 135 | // createLinearScanRegisterAllocator: not so fast but good quality |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 136 | if ((pConfig.getOptimizationLevel() == llvm::CodeGenOpt::None)) { |
| 137 | llvm::RegisterRegAlloc::setDefault(llvm::createFastRegisterAllocator); |
| 138 | } else { |
| 139 | llvm::RegisterRegAlloc::setDefault(llvm::createGreedyRegisterAllocator); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 140 | } |
| 141 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 142 | return kSuccess; |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 143 | } |
| 144 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 145 | Compiler::~Compiler() { |
| 146 | delete mTarget; |
| 147 | } |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 148 | |
Pirama Arumuga Nainar | 1e0557a | 2014-12-02 15:02:18 -0800 | [diff] [blame] | 149 | |
David Gross | 5aefc98 | 2015-08-04 10:41:33 -0700 | [diff] [blame] | 150 | // This function has complete responsibility for creating and executing the |
| 151 | // exact list of compiler passes. |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 152 | enum Compiler::ErrorCode Compiler::runPasses(Script &pScript, |
Pirama Arumuga Nainar | 98137cc | 2015-05-06 11:18:56 -0700 | [diff] [blame] | 153 | llvm::raw_pwrite_stream &pResult) { |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 154 | // Pass manager for link-time optimization |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 155 | llvm::legacy::PassManager transformPasses; |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 156 | |
| 157 | // Empty MCContext. |
| 158 | llvm::MCContext *mc_context = nullptr; |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 159 | |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 160 | transformPasses.add( |
| 161 | createTargetTransformInfoWrapperPass(mTarget->getTargetIRAnalysis())); |
Tim Murray | bb73b74 | 2014-11-04 11:20:10 -0800 | [diff] [blame] | 162 | |
David Gross | 5aefc98 | 2015-08-04 10:41:33 -0700 | [diff] [blame] | 163 | // Add some initial custom passes. |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 164 | addInvokeHelperPass(transformPasses); |
| 165 | addExpandKernelPass(transformPasses); |
Dean De Leo | 09c7a41 | 2015-11-25 12:45:45 +0000 | [diff] [blame] | 166 | addDebugInfoPass(pScript, transformPasses); |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 167 | addInvariantPass(transformPasses); |
Dean De Leo | 7a9a967 | 2015-11-25 12:51:54 +0000 | [diff] [blame] | 168 | if (mTarget->getOptLevel() != llvm::CodeGenOpt::None) { |
| 169 | if (!addInternalizeSymbolsPass(pScript, transformPasses)) |
| 170 | return kErrCustomPasses; |
| 171 | } |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 172 | addGlobalInfoPass(pScript, transformPasses); |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 173 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 174 | if (mTarget->getOptLevel() == llvm::CodeGenOpt::None) { |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 175 | transformPasses.add(llvm::createGlobalOptimizerPass()); |
| 176 | transformPasses.add(llvm::createConstantMergePass()); |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 177 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 178 | } else { |
Tobias Grosser | 2f6103b | 2013-07-01 14:01:06 -0700 | [diff] [blame] | 179 | // FIXME: Figure out which passes should be executed. |
| 180 | llvm::PassManagerBuilder Builder; |
Stephen Hines | 5793613 | 2014-11-25 17:54:59 -0800 | [diff] [blame] | 181 | Builder.Inliner = llvm::createFunctionInliningPass(); |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 182 | Builder.populateLTOPassManager(transformPasses); |
Tim Murray | 50f5eb4 | 2014-12-09 17:36:24 -0800 | [diff] [blame] | 183 | |
Tim Murray | 7f59b5f | 2015-02-12 14:38:05 -0800 | [diff] [blame] | 184 | /* FIXME: Reenable autovectorization after rebase. |
| 185 | bug 19324423 |
Tim Murray | 50f5eb4 | 2014-12-09 17:36:24 -0800 | [diff] [blame] | 186 | // Add vectorization passes after LTO passes are in |
| 187 | // additional flag: -unroll-runtime |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 188 | transformPasses.add(llvm::createLoopUnrollPass(-1, 16, 0, 1)); |
Tim Murray | 50f5eb4 | 2014-12-09 17:36:24 -0800 | [diff] [blame] | 189 | // Need to pass appropriate flags here: -scalarize-load-store |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 190 | transformPasses.add(llvm::createScalarizerPass()); |
| 191 | transformPasses.add(llvm::createCFGSimplificationPass()); |
| 192 | transformPasses.add(llvm::createScopedNoAliasAAPass()); |
| 193 | transformPasses.add(llvm::createScalarEvolutionAliasAnalysisPass()); |
Tim Murray | 50f5eb4 | 2014-12-09 17:36:24 -0800 | [diff] [blame] | 194 | // additional flags: -slp-vectorize-hor -slp-vectorize-hor-store (unnecessary?) |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 195 | transformPasses.add(llvm::createSLPVectorizerPass()); |
| 196 | transformPasses.add(llvm::createDeadCodeEliminationPass()); |
| 197 | transformPasses.add(llvm::createInstructionCombiningPass()); |
Tim Murray | 7f59b5f | 2015-02-12 14:38:05 -0800 | [diff] [blame] | 198 | */ |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 199 | } |
| 200 | |
Pirama Arumuga Nainar | 8c24f8d | 2015-03-17 13:11:25 -0700 | [diff] [blame] | 201 | // These passes have to come after LTO, since we don't want to examine |
| 202 | // functions that are never actually called. |
David Gross | 5aefc98 | 2015-08-04 10:41:33 -0700 | [diff] [blame] | 203 | if (llvm::Triple(getTargetMachine().getTargetTriple()).getArch() == llvm::Triple::x86_64) |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 204 | 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 Nainar | 9fe081b | 2015-01-27 14:09:19 -0800 | [diff] [blame] | 206 | |
| 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 Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 212 | 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 Hines | 5db508c | 2015-01-06 01:42:56 -0800 | [diff] [blame] | 220 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 221 | // Add passes to the pass manager to emit machine code through MC layer. |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 222 | if (mTarget->addPassesToEmitMC(codeGenPasses, mc_context, pResult, |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 223 | /* DisableVerify */false)) { |
| 224 | return kPrepareCodeGenPass; |
| 225 | } |
| 226 | |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 227 | // Execute the passes. |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 228 | codeGenPasses.run(pScript.getSource().getModule()); |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 229 | |
| 230 | return kSuccess; |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 231 | } |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 232 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 233 | enum Compiler::ErrorCode Compiler::compile(Script &pScript, |
Pirama Arumuga Nainar | 98137cc | 2015-05-06 11:18:56 -0700 | [diff] [blame] | 234 | llvm::raw_pwrite_stream &pResult, |
Tobias Grosser | 27fb7ed | 2013-06-21 18:34:56 -0700 | [diff] [blame] | 235 | llvm::raw_ostream *IRStream) { |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 236 | llvm::Module &module = pScript.getSource().getModule(); |
| 237 | enum ErrorCode err; |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 238 | |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 239 | if (mTarget == nullptr) { |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 240 | return kErrNoTargetMachine; |
| 241 | } |
| 242 | |
Stephen Hines | 10ee6af | 2014-09-09 17:28:23 -0700 | [diff] [blame] | 243 | const std::string &triple = module.getTargetTriple(); |
Pirama Arumuga Nainar | 8e90893 | 2016-03-06 23:05:45 -0800 | [diff] [blame] | 244 | const llvm::DataLayout dl = getTargetMachine().createDataLayout(); |
| 245 | unsigned int pointerSize = dl.getPointerSizeInBits(); |
Stephen Hines | 10ee6af | 2014-09-09 17:28:23 -0700 | [diff] [blame] | 246 | 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 Nainar | 9e0f8f0 | 2016-04-12 14:04:50 -0700 | [diff] [blame] | 258 | 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 Gross | cf8b2d0 | 2015-05-19 11:55:29 -0700 | [diff] [blame] | 267 | // Sanitize module's target information. |
Pirama Arumuga Nainar | 8e90893 | 2016-03-06 23:05:45 -0800 | [diff] [blame] | 268 | module.setTargetTriple(getTargetMachine().getTargetTriple().str()); |
| 269 | module.setDataLayout(getTargetMachine().createDataLayout()); |
David Gross | cf8b2d0 | 2015-05-19 11:55:29 -0700 | [diff] [blame] | 270 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 271 | // Materialize the bitcode module. |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 272 | if (module.getMaterializer() != nullptr) { |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 273 | // A module with non-null materializer means that it is a lazy-load module. |
Pirama Arumuga Nainar | 8e90893 | 2016-03-06 23:05:45 -0800 | [diff] [blame] | 274 | // Materialize it now. This function returns false when the materialization |
| 275 | // is successful. |
| 276 | std::error_code ec = module.materializeAll(); |
Tim Murray | c2074ca | 2014-04-08 15:39:08 -0700 | [diff] [blame] | 277 | if (ec) { |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 278 | ALOGE("Failed to materialize the module `%s'! (%s)", |
Tim Murray | c2074ca | 2014-04-08 15:39:08 -0700 | [diff] [blame] | 279 | module.getModuleIdentifier().c_str(), ec.message().c_str()); |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 280 | return kErrMaterialization; |
| 281 | } |
| 282 | } |
| 283 | |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 284 | if ((err = runPasses(pScript, pResult)) != kSuccess) { |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 285 | return err; |
| 286 | } |
| 287 | |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 288 | if (IRStream) { |
Tobias Grosser | 27fb7ed | 2013-06-21 18:34:56 -0700 | [diff] [blame] | 289 | *IRStream << module; |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | return kSuccess; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 293 | } |
| 294 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 295 | enum Compiler::ErrorCode Compiler::compile(Script &pScript, |
Tobias Grosser | 27fb7ed | 2013-06-21 18:34:56 -0700 | [diff] [blame] | 296 | OutputFile &pResult, |
| 297 | llvm::raw_ostream *IRStream) { |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 298 | // Check the state of the specified output file. |
| 299 | if (pResult.hasError()) { |
| 300 | return kErrInvalidOutputFileState; |
| 301 | } |
Shih-wei Liao | 90cd3d1 | 2011-06-20 15:43:34 -0700 | [diff] [blame] | 302 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 303 | // Open the output file decorated in llvm::raw_ostream. |
Pirama Arumuga Nainar | 98137cc | 2015-05-06 11:18:56 -0700 | [diff] [blame] | 304 | llvm::raw_pwrite_stream *out = pResult.dup(); |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 305 | if (out == nullptr) { |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 306 | return kErrPrepareOutput; |
| 307 | } |
| 308 | |
| 309 | // Delegate the request. |
Tobias Grosser | 27fb7ed | 2013-06-21 18:34:56 -0700 | [diff] [blame] | 310 | enum Compiler::ErrorCode err = compile(pScript, *out, IRStream); |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 311 | |
| 312 | // Close the output before return. |
| 313 | delete out; |
| 314 | |
| 315 | return err; |
| 316 | } |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 317 | |
Stephen Hines | 1bd9f62 | 2015-03-18 14:53:10 -0700 | [diff] [blame] | 318 | bool Compiler::addInternalizeSymbolsPass(Script &pScript, llvm::legacy::PassManager &pPM) { |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 319 | // 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 Hines | 107f50d | 2015-01-19 21:02:13 -0800 | [diff] [blame] | 332 | const char *sf[] = { |
Stephen Hines | fb81ec1 | 2015-05-18 20:04:23 -0700 | [diff] [blame] | 333 | 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 Hines | 107f50d | 2015-01-19 21:02:13 -0800 | [diff] [blame] | 343 | }; |
| 344 | const char **special_functions = sf; |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 345 | // Special RS functions should always be global symbols. |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 346 | 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 Wala | 4e7a506 | 2015-07-30 16:27:51 -0700 | [diff] [blame] | 356 | size_t exportReduceCount = me.getExportReduceCount(); |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 357 | const char **exportVarNameList = me.getExportVarNameList(); |
| 358 | const char **exportFuncNameList = me.getExportFuncNameList(); |
| 359 | const char **exportForEachNameList = me.getExportForEachNameList(); |
David Gross | a48ea36 | 2016-06-02 14:46:55 -0700 | [diff] [blame] | 360 | const bcinfo::MetadataExtractor::Reduce *exportReduceList = me.getExportReduceList(); |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 361 | 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 Gross | a48ea36 | 2016-06-02 14:46:55 -0700 | [diff] [blame] | 371 | // 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 Gross | c545d6f | 2016-02-08 13:49:02 -0800 | [diff] [blame] | 376 | std::vector<std::string> keep_funcs; |
David Gross | a48ea36 | 2016-06-02 14:46:55 -0700 | [diff] [blame] | 377 | keep_funcs.reserve(exportForEachCount + exportReduceCount*4); |
Matt Wala | 4e7a506 | 2015-07-30 16:27:51 -0700 | [diff] [blame] | 378 | |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 379 | for (i = 0; i < exportForEachCount; ++i) { |
David Gross | c545d6f | 2016-02-08 13:49:02 -0800 | [diff] [blame] | 380 | keep_funcs.push_back(std::string(exportForEachNameList[i]) + ".expand"); |
Matt Wala | 4e7a506 | 2015-07-30 16:27:51 -0700 | [diff] [blame] | 381 | } |
David Gross | c545d6f | 2016-02-08 13:49:02 -0800 | [diff] [blame] | 382 | auto keepFuncsPushBackIfPresent = [&keep_funcs](const char *Name) { |
| 383 | if (Name) keep_funcs.push_back(Name); |
| 384 | }; |
David Gross | a48ea36 | 2016-06-02 14:46:55 -0700 | [diff] [blame] | 385 | 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 Gross | 57fd9f8 | 2016-04-08 12:35:41 -0700 | [diff] [blame] | 390 | } else { |
David Gross | a48ea36 | 2016-06-02 14:46:55 -0700 | [diff] [blame] | 391 | keep_funcs.push_back(nameReduceCombinerFromAccumulator(exportReduceList[i].mAccumulatorName)); |
David Gross | 57fd9f8 | 2016-04-08 12:35:41 -0700 | [diff] [blame] | 392 | } |
David Gross | a48ea36 | 2016-06-02 14:46:55 -0700 | [diff] [blame] | 393 | keepFuncsPushBackIfPresent(exportReduceList[i].mOutConverterName); |
David Gross | 79e1a05 | 2016-01-11 14:42:51 -0800 | [diff] [blame] | 394 | } |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 395 | |
David Gross | c545d6f | 2016-02-08 13:49:02 -0800 | [diff] [blame] | 396 | for (auto &symbol_name : keep_funcs) { |
Matt Wala | 4e7a506 | 2015-07-30 16:27:51 -0700 | [diff] [blame] | 397 | export_symbols.push_back(symbol_name.c_str()); |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Pirama Arumuga Nainar | 10f2a8f | 2016-02-03 15:51:51 -0800 | [diff] [blame] | 400 | // 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 Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 408 | pPM.add(llvm::createInternalizePass(export_symbols)); |
| 409 | |
| 410 | return true; |
| 411 | } |
| 412 | |
David Gross | 5aefc98 | 2015-08-04 10:41:33 -0700 | [diff] [blame] | 413 | void Compiler::addInvokeHelperPass(llvm::legacy::PassManager &pPM) { |
Tim Murray | b7bce74 | 2014-11-03 16:17:30 -0800 | [diff] [blame] | 414 | llvm::Triple arch(getTargetMachine().getTargetTriple()); |
| 415 | if (arch.isArch64Bit()) { |
| 416 | pPM.add(createRSInvokeHelperPass()); |
| 417 | } |
Tim Murray | b7bce74 | 2014-11-03 16:17:30 -0800 | [diff] [blame] | 418 | } |
| 419 | |
Dean De Leo | 09c7a41 | 2015-11-25 12:45:45 +0000 | [diff] [blame] | 420 | void Compiler::addDebugInfoPass(Script &pScript, llvm::legacy::PassManager &pPM) { |
| 421 | if (pScript.getSource().getDebugInfoEnabled()) |
| 422 | pPM.add(createRSAddDebugInfoPass()); |
| 423 | } |
| 424 | |
Matt Wala | 4e7a506 | 2015-07-30 16:27:51 -0700 | [diff] [blame] | 425 | void Compiler::addExpandKernelPass(llvm::legacy::PassManager &pPM) { |
| 426 | // Expand ForEach and reduce on CPU path to reduce launch overhead. |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 427 | bool pEnableStepOpt = true; |
Matt Wala | 4e7a506 | 2015-07-30 16:27:51 -0700 | [diff] [blame] | 428 | pPM.add(createRSKernelExpandPass(pEnableStepOpt)); |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 429 | } |
| 430 | |
David Gross | 5aefc98 | 2015-08-04 10:41:33 -0700 | [diff] [blame] | 431 | void Compiler::addGlobalInfoPass(Script &pScript, llvm::legacy::PassManager &pPM) { |
Stephen Hines | 750ee65 | 2015-04-16 16:24:18 -0700 | [diff] [blame] | 432 | // 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 Hines | 750ee65 | 2015-04-16 16:24:18 -0700 | [diff] [blame] | 437 | } |
| 438 | |
David Gross | 5aefc98 | 2015-08-04 10:41:33 -0700 | [diff] [blame] | 439 | void Compiler::addInvariantPass(llvm::legacy::PassManager &pPM) { |
David Gross | 1d93a19 | 2015-03-25 14:59:27 -0700 | [diff] [blame] | 440 | // Mark Loads from RsExpandKernelDriverInfo as "load.invariant". |
| 441 | // Should run after ExpandForEach and before inlining. |
| 442 | pPM.add(createRSInvariantPass()); |
Pirama Arumuga Nainar | 8c24f8d | 2015-03-17 13:11:25 -0700 | [diff] [blame] | 443 | } |
Pirama Arumuga Nainar | ebff2ea | 2015-05-21 15:45:05 -0700 | [diff] [blame] | 444 | |
| 445 | enum 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 Nainar | 9e0f8f0 | 2016-04-12 14:04:50 -0700 | [diff] [blame] | 468 | |
| 469 | void 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 | } |