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" |
Zonr Chang | c72c4dd | 2012-04-12 15:38:53 +0800 | [diff] [blame] | 38 | #include "bcc/Script.h" |
| 39 | #include "bcc/Source.h" |
| 40 | #include "bcc/Support/CompilerConfig.h" |
Zonr Chang | ef73a24 | 2012-04-12 16:44:01 +0800 | [diff] [blame] | 41 | #include "bcc/Support/Log.h" |
Zonr Chang | c72c4dd | 2012-04-12 15:38:53 +0800 | [diff] [blame] | 42 | #include "bcc/Support/OutputFile.h" |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 43 | #include "bcinfo/MetadataExtractor.h" |
Stephen Hines | fb81ec1 | 2015-05-18 20:04:23 -0700 | [diff] [blame] | 44 | #include "rsDefines.h" |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 45 | |
Stephen Hines | 10ee6af | 2014-09-09 17:28:23 -0700 | [diff] [blame] | 46 | #include <string> |
| 47 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 48 | using namespace bcc; |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 49 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 50 | const char *Compiler::GetErrorString(enum ErrorCode pErrCode) { |
Tobias Grosser | 5b7f52a | 2013-07-23 14:57:00 -0700 | [diff] [blame] | 51 | switch (pErrCode) { |
| 52 | case kSuccess: |
| 53 | return "Successfully compiled."; |
| 54 | case kInvalidConfigNoTarget: |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 55 | return "Invalid compiler config supplied (getTarget() returns nullptr.) " |
Tobias Grosser | 5b7f52a | 2013-07-23 14:57:00 -0700 | [diff] [blame] | 56 | "(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 Grosser | 5b7f52a | 2013-07-23 14:57:00 -0700 | [diff] [blame] | 64 | 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 Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 72 | case kErrCustomPasses: |
| 73 | return "Error occurred while adding custom passes."; |
Tobias Grosser | 5b7f52a | 2013-07-23 14:57:00 -0700 | [diff] [blame] | 74 | case kErrInvalidSource: |
| 75 | return "Error loading input bitcode"; |
Pirama Arumuga Nainar | 1e0557a | 2014-12-02 15:02:18 -0800 | [diff] [blame] | 76 | case kIllegalGlobalFunction: |
| 77 | return "Use of undefined external function"; |
Pirama Arumuga Nainar | 9e0f8f0 | 2016-04-12 14:04:50 -0700 | [diff] [blame^] | 78 | case kErrInvalidTargetMachine: |
| 79 | return "Invalid/unexpected llvm::TargetMachine."; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 80 | } |
Zonr Chang | fef9a1b | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 81 | |
Tobias Grosser | 5b7f52a | 2013-07-23 14:57:00 -0700 | [diff] [blame] | 82 | // This assert should never be reached as the compiler verifies that the |
| 83 | // above switch coveres all enum values. |
David Gross | c2ca742 | 2015-05-29 14:54:33 -0700 | [diff] [blame] | 84 | bccAssert(false && "Unknown error code encountered"); |
Tobias Grosser | 5b7f52a | 2013-07-23 14:57:00 -0700 | [diff] [blame] | 85 | return ""; |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 88 | //===----------------------------------------------------------------------===// |
| 89 | // Instance Methods |
| 90 | //===----------------------------------------------------------------------===// |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 91 | Compiler::Compiler() : mTarget(nullptr), mEnableOpt(true) { |
Zonr Chang | fef9a1b | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 92 | return; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 93 | } |
| 94 | |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 95 | Compiler::Compiler(const CompilerConfig &pConfig) : mTarget(nullptr), |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 96 | mEnableOpt(true) { |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 97 | 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 Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 104 | } |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 105 | |
| 106 | return; |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 109 | enum Compiler::ErrorCode Compiler::config(const CompilerConfig &pConfig) { |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 110 | if (pConfig.getTarget() == nullptr) { |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 111 | return kInvalidConfigNoTarget; |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 112 | } |
| 113 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 114 | 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 Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 122 | |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 123 | if (new_target == nullptr) { |
| 124 | return ((mTarget != nullptr) ? kErrSwitchTargetMachine : |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 125 | kErrCreateTargetMachine); |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 126 | } |
| 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 Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 133 | // createFastRegisterAllocator: fast but bad quality |
| 134 | // createLinearScanRegisterAllocator: not so fast but good quality |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 135 | if ((pConfig.getOptimizationLevel() == llvm::CodeGenOpt::None)) { |
| 136 | llvm::RegisterRegAlloc::setDefault(llvm::createFastRegisterAllocator); |
| 137 | } else { |
| 138 | llvm::RegisterRegAlloc::setDefault(llvm::createGreedyRegisterAllocator); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 139 | } |
| 140 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 141 | return kSuccess; |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 142 | } |
| 143 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 144 | Compiler::~Compiler() { |
| 145 | delete mTarget; |
| 146 | } |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 147 | |
Pirama Arumuga Nainar | 1e0557a | 2014-12-02 15:02:18 -0800 | [diff] [blame] | 148 | |
David Gross | 5aefc98 | 2015-08-04 10:41:33 -0700 | [diff] [blame] | 149 | // This function has complete responsibility for creating and executing the |
| 150 | // exact list of compiler passes. |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 151 | enum Compiler::ErrorCode Compiler::runPasses(Script &pScript, |
Pirama Arumuga Nainar | 98137cc | 2015-05-06 11:18:56 -0700 | [diff] [blame] | 152 | llvm::raw_pwrite_stream &pResult) { |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 153 | // Pass manager for link-time optimization |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 154 | llvm::legacy::PassManager transformPasses; |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 155 | |
| 156 | // Empty MCContext. |
| 157 | llvm::MCContext *mc_context = nullptr; |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 158 | |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 159 | transformPasses.add( |
| 160 | createTargetTransformInfoWrapperPass(mTarget->getTargetIRAnalysis())); |
Tim Murray | bb73b74 | 2014-11-04 11:20:10 -0800 | [diff] [blame] | 161 | |
David Gross | 5aefc98 | 2015-08-04 10:41:33 -0700 | [diff] [blame] | 162 | // Add some initial custom passes. |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 163 | addInvokeHelperPass(transformPasses); |
| 164 | addExpandKernelPass(transformPasses); |
Dean De Leo | 09c7a41 | 2015-11-25 12:45:45 +0000 | [diff] [blame] | 165 | addDebugInfoPass(pScript, transformPasses); |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 166 | addInvariantPass(transformPasses); |
Dean De Leo | 7a9a967 | 2015-11-25 12:51:54 +0000 | [diff] [blame] | 167 | if (mTarget->getOptLevel() != llvm::CodeGenOpt::None) { |
| 168 | if (!addInternalizeSymbolsPass(pScript, transformPasses)) |
| 169 | return kErrCustomPasses; |
| 170 | } |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 171 | addGlobalInfoPass(pScript, transformPasses); |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 172 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 173 | if (mTarget->getOptLevel() == llvm::CodeGenOpt::None) { |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 174 | transformPasses.add(llvm::createGlobalOptimizerPass()); |
| 175 | transformPasses.add(llvm::createConstantMergePass()); |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 176 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 177 | } else { |
Tobias Grosser | 2f6103b | 2013-07-01 14:01:06 -0700 | [diff] [blame] | 178 | // FIXME: Figure out which passes should be executed. |
| 179 | llvm::PassManagerBuilder Builder; |
Stephen Hines | 5793613 | 2014-11-25 17:54:59 -0800 | [diff] [blame] | 180 | Builder.Inliner = llvm::createFunctionInliningPass(); |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 181 | Builder.populateLTOPassManager(transformPasses); |
Tim Murray | 50f5eb4 | 2014-12-09 17:36:24 -0800 | [diff] [blame] | 182 | |
Tim Murray | 7f59b5f | 2015-02-12 14:38:05 -0800 | [diff] [blame] | 183 | /* FIXME: Reenable autovectorization after rebase. |
| 184 | bug 19324423 |
Tim Murray | 50f5eb4 | 2014-12-09 17:36:24 -0800 | [diff] [blame] | 185 | // Add vectorization passes after LTO passes are in |
| 186 | // additional flag: -unroll-runtime |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 187 | transformPasses.add(llvm::createLoopUnrollPass(-1, 16, 0, 1)); |
Tim Murray | 50f5eb4 | 2014-12-09 17:36:24 -0800 | [diff] [blame] | 188 | // Need to pass appropriate flags here: -scalarize-load-store |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 189 | transformPasses.add(llvm::createScalarizerPass()); |
| 190 | transformPasses.add(llvm::createCFGSimplificationPass()); |
| 191 | transformPasses.add(llvm::createScopedNoAliasAAPass()); |
| 192 | transformPasses.add(llvm::createScalarEvolutionAliasAnalysisPass()); |
Tim Murray | 50f5eb4 | 2014-12-09 17:36:24 -0800 | [diff] [blame] | 193 | // additional flags: -slp-vectorize-hor -slp-vectorize-hor-store (unnecessary?) |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 194 | transformPasses.add(llvm::createSLPVectorizerPass()); |
| 195 | transformPasses.add(llvm::createDeadCodeEliminationPass()); |
| 196 | transformPasses.add(llvm::createInstructionCombiningPass()); |
Tim Murray | 7f59b5f | 2015-02-12 14:38:05 -0800 | [diff] [blame] | 197 | */ |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 198 | } |
| 199 | |
Pirama Arumuga Nainar | 8c24f8d | 2015-03-17 13:11:25 -0700 | [diff] [blame] | 200 | // These passes have to come after LTO, since we don't want to examine |
| 201 | // functions that are never actually called. |
David Gross | 5aefc98 | 2015-08-04 10:41:33 -0700 | [diff] [blame] | 202 | if (llvm::Triple(getTargetMachine().getTargetTriple()).getArch() == llvm::Triple::x86_64) |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 203 | 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 Nainar | 9fe081b | 2015-01-27 14:09:19 -0800 | [diff] [blame] | 205 | |
| 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 Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 211 | 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 Hines | 5db508c | 2015-01-06 01:42:56 -0800 | [diff] [blame] | 219 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 220 | // 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] | 221 | if (mTarget->addPassesToEmitMC(codeGenPasses, mc_context, pResult, |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 222 | /* DisableVerify */false)) { |
| 223 | return kPrepareCodeGenPass; |
| 224 | } |
| 225 | |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 226 | // Execute the passes. |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 227 | codeGenPasses.run(pScript.getSource().getModule()); |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 228 | |
| 229 | return kSuccess; |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 230 | } |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 231 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 232 | enum Compiler::ErrorCode Compiler::compile(Script &pScript, |
Pirama Arumuga Nainar | 98137cc | 2015-05-06 11:18:56 -0700 | [diff] [blame] | 233 | llvm::raw_pwrite_stream &pResult, |
Tobias Grosser | 27fb7ed | 2013-06-21 18:34:56 -0700 | [diff] [blame] | 234 | llvm::raw_ostream *IRStream) { |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 235 | llvm::Module &module = pScript.getSource().getModule(); |
| 236 | enum ErrorCode err; |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 237 | |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 238 | if (mTarget == nullptr) { |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 239 | return kErrNoTargetMachine; |
| 240 | } |
| 241 | |
Stephen Hines | 10ee6af | 2014-09-09 17:28:23 -0700 | [diff] [blame] | 242 | const std::string &triple = module.getTargetTriple(); |
Pirama Arumuga Nainar | 8e90893 | 2016-03-06 23:05:45 -0800 | [diff] [blame] | 243 | const llvm::DataLayout dl = getTargetMachine().createDataLayout(); |
| 244 | unsigned int pointerSize = dl.getPointerSizeInBits(); |
Stephen Hines | 10ee6af | 2014-09-09 17:28:23 -0700 | [diff] [blame] | 245 | 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 Nainar | 9e0f8f0 | 2016-04-12 14:04:50 -0700 | [diff] [blame^] | 257 | 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 Gross | cf8b2d0 | 2015-05-19 11:55:29 -0700 | [diff] [blame] | 266 | // Sanitize module's target information. |
Pirama Arumuga Nainar | 8e90893 | 2016-03-06 23:05:45 -0800 | [diff] [blame] | 267 | module.setTargetTriple(getTargetMachine().getTargetTriple().str()); |
| 268 | module.setDataLayout(getTargetMachine().createDataLayout()); |
David Gross | cf8b2d0 | 2015-05-19 11:55:29 -0700 | [diff] [blame] | 269 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 270 | // Materialize the bitcode module. |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 271 | if (module.getMaterializer() != nullptr) { |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 272 | // 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] | 273 | // Materialize it now. This function returns false when the materialization |
| 274 | // is successful. |
| 275 | std::error_code ec = module.materializeAll(); |
Tim Murray | c2074ca | 2014-04-08 15:39:08 -0700 | [diff] [blame] | 276 | if (ec) { |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 277 | ALOGE("Failed to materialize the module `%s'! (%s)", |
Tim Murray | c2074ca | 2014-04-08 15:39:08 -0700 | [diff] [blame] | 278 | module.getModuleIdentifier().c_str(), ec.message().c_str()); |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 279 | return kErrMaterialization; |
| 280 | } |
| 281 | } |
| 282 | |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 283 | if ((err = runPasses(pScript, pResult)) != kSuccess) { |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 284 | return err; |
| 285 | } |
| 286 | |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 287 | if (IRStream) { |
Tobias Grosser | 27fb7ed | 2013-06-21 18:34:56 -0700 | [diff] [blame] | 288 | *IRStream << module; |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | return kSuccess; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 292 | } |
| 293 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 294 | enum Compiler::ErrorCode Compiler::compile(Script &pScript, |
Tobias Grosser | 27fb7ed | 2013-06-21 18:34:56 -0700 | [diff] [blame] | 295 | OutputFile &pResult, |
| 296 | llvm::raw_ostream *IRStream) { |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 297 | // Check the state of the specified output file. |
| 298 | if (pResult.hasError()) { |
| 299 | return kErrInvalidOutputFileState; |
| 300 | } |
Shih-wei Liao | 90cd3d1 | 2011-06-20 15:43:34 -0700 | [diff] [blame] | 301 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 302 | // Open the output file decorated in llvm::raw_ostream. |
Pirama Arumuga Nainar | 98137cc | 2015-05-06 11:18:56 -0700 | [diff] [blame] | 303 | llvm::raw_pwrite_stream *out = pResult.dup(); |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 304 | if (out == nullptr) { |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 305 | return kErrPrepareOutput; |
| 306 | } |
| 307 | |
| 308 | // Delegate the request. |
Tobias Grosser | 27fb7ed | 2013-06-21 18:34:56 -0700 | [diff] [blame] | 309 | enum Compiler::ErrorCode err = compile(pScript, *out, IRStream); |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 310 | |
| 311 | // Close the output before return. |
| 312 | delete out; |
| 313 | |
| 314 | return err; |
| 315 | } |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 316 | |
Stephen Hines | 1bd9f62 | 2015-03-18 14:53:10 -0700 | [diff] [blame] | 317 | bool Compiler::addInternalizeSymbolsPass(Script &pScript, llvm::legacy::PassManager &pPM) { |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 318 | // 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 Hines | 107f50d | 2015-01-19 21:02:13 -0800 | [diff] [blame] | 331 | const char *sf[] = { |
Stephen Hines | fb81ec1 | 2015-05-18 20:04:23 -0700 | [diff] [blame] | 332 | 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 Hines | 107f50d | 2015-01-19 21:02:13 -0800 | [diff] [blame] | 342 | }; |
| 343 | const char **special_functions = sf; |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 344 | // Special RS functions should always be global symbols. |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 345 | 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 Wala | 4e7a506 | 2015-07-30 16:27:51 -0700 | [diff] [blame] | 355 | size_t exportReduceCount = me.getExportReduceCount(); |
David Gross | 79e1a05 | 2016-01-11 14:42:51 -0800 | [diff] [blame] | 356 | size_t exportReduceNewCount = me.getExportReduceNewCount(); |
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(); |
Matt Wala | 4e7a506 | 2015-07-30 16:27:51 -0700 | [diff] [blame] | 360 | const char **exportReduceNameList = me.getExportReduceNameList(); |
David Gross | 79e1a05 | 2016-01-11 14:42:51 -0800 | [diff] [blame] | 361 | const bcinfo::MetadataExtractor::ReduceNew *exportReduceNewList = me.getExportReduceNewList(); |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 362 | 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 Gross | c545d6f | 2016-02-08 13:49:02 -0800 | [diff] [blame] | 372 | // Expanded foreach and reduce functions should not be internalized; |
David Gross | 8ca1357 | 2016-03-21 14:32:16 -0700 | [diff] [blame] | 373 | // 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 Gross | c545d6f | 2016-02-08 13:49:02 -0800 | [diff] [blame] | 377 | std::vector<std::string> keep_funcs; |
David Gross | 8ca1357 | 2016-03-21 14:32:16 -0700 | [diff] [blame] | 378 | keep_funcs.reserve(exportForEachCount + exportReduceCount + exportReduceNewCount*4); |
Matt Wala | 4e7a506 | 2015-07-30 16:27:51 -0700 | [diff] [blame] | 379 | |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 380 | for (i = 0; i < exportForEachCount; ++i) { |
David Gross | c545d6f | 2016-02-08 13:49:02 -0800 | [diff] [blame] | 381 | keep_funcs.push_back(std::string(exportForEachNameList[i]) + ".expand"); |
Matt Wala | 4e7a506 | 2015-07-30 16:27:51 -0700 | [diff] [blame] | 382 | } |
| 383 | for (i = 0; i < exportReduceCount; ++i) { |
David Gross | c545d6f | 2016-02-08 13:49:02 -0800 | [diff] [blame] | 384 | keep_funcs.push_back(std::string(exportReduceNameList[i]) + ".expand"); |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 385 | } |
David Gross | c545d6f | 2016-02-08 13:49:02 -0800 | [diff] [blame] | 386 | auto keepFuncsPushBackIfPresent = [&keep_funcs](const char *Name) { |
| 387 | if (Name) keep_funcs.push_back(Name); |
| 388 | }; |
David Gross | 79e1a05 | 2016-01-11 14:42:51 -0800 | [diff] [blame] | 389 | for (i = 0; i < exportReduceNewCount; ++i) { |
David Gross | c545d6f | 2016-02-08 13:49:02 -0800 | [diff] [blame] | 390 | keep_funcs.push_back(std::string(exportReduceNewList[i].mAccumulatorName) + ".expand"); |
David Gross | c545d6f | 2016-02-08 13:49:02 -0800 | [diff] [blame] | 391 | keepFuncsPushBackIfPresent(exportReduceNewList[i].mInitializerName); |
David Gross | 8ca1357 | 2016-03-21 14:32:16 -0700 | [diff] [blame] | 392 | keepFuncsPushBackIfPresent(exportReduceNewList[i].mCombinerName); |
David Gross | c545d6f | 2016-02-08 13:49:02 -0800 | [diff] [blame] | 393 | keepFuncsPushBackIfPresent(exportReduceNewList[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 | } |