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 | |
Jean-Luc Brouillet | a2dd52f | 2017-02-16 20:57:26 -0800 | [diff] [blame] | 17 | #include "Assert.h" |
| 18 | #include "Log.h" |
Jean-Luc Brouillet | a2dd52f | 2017-02-16 20:57:26 -0800 | [diff] [blame] | 19 | #include "RSTransforms.h" |
| 20 | #include "RSUtils.h" |
| 21 | #include "rsDefines.h" |
| 22 | |
Zonr Chang | c72c4dd | 2012-04-12 15:38:53 +0800 | [diff] [blame] | 23 | #include "bcc/Compiler.h" |
Jean-Luc Brouillet | a2dd52f | 2017-02-16 20:57:26 -0800 | [diff] [blame] | 24 | #include "bcc/CompilerConfig.h" |
| 25 | #include "bcc/Config.h" |
Jean-Luc Brouillet | a2dd52f | 2017-02-16 20:57:26 -0800 | [diff] [blame] | 26 | #include "bcc/Script.h" |
| 27 | #include "bcc/Source.h" |
| 28 | #include "bcinfo/MetadataExtractor.h" |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 29 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 30 | #include <llvm/Analysis/Passes.h> |
Stephen Hines | 1bd9f62 | 2015-03-18 14:53:10 -0700 | [diff] [blame] | 31 | #include <llvm/Analysis/TargetTransformInfo.h> |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 32 | #include <llvm/CodeGen/RegAllocRegistry.h> |
Stephen Hines | 1bd9f62 | 2015-03-18 14:53:10 -0700 | [diff] [blame] | 33 | #include <llvm/IR/LegacyPassManager.h> |
Stephen Hines | b730e23 | 2013-01-09 15:31:36 -0800 | [diff] [blame] | 34 | #include <llvm/IR/Module.h> |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 35 | #include <llvm/Support/TargetRegistry.h> |
| 36 | #include <llvm/Support/raw_ostream.h> |
Stephen Hines | b730e23 | 2013-01-09 15:31:36 -0800 | [diff] [blame] | 37 | #include <llvm/IR/DataLayout.h> |
Stephen Hines | 5793613 | 2014-11-25 17:54:59 -0800 | [diff] [blame] | 38 | #include <llvm/Target/TargetSubtargetInfo.h> |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 39 | #include <llvm/Target/TargetMachine.h> |
| 40 | #include <llvm/Transforms/IPO.h> |
Tobias Grosser | 2f6103b | 2013-07-01 14:01:06 -0700 | [diff] [blame] | 41 | #include <llvm/Transforms/IPO/PassManagerBuilder.h> |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 42 | #include <llvm/Transforms/Scalar.h> |
Tim Murray | 50f5eb4 | 2014-12-09 17:36:24 -0800 | [diff] [blame] | 43 | #include <llvm/Transforms/Vectorize.h> |
Logan | 3584900 | 2011-01-15 07:30:43 +0800 | [diff] [blame] | 44 | |
Stephen Hines | 10ee6af | 2014-09-09 17:28:23 -0700 | [diff] [blame] | 45 | #include <string> |
Pirama Arumuga Nainar | f5b49a0 | 2016-09-15 23:04:25 -0700 | [diff] [blame] | 46 | #include <set> |
Stephen Hines | 10ee6af | 2014-09-09 17:28:23 -0700 | [diff] [blame] | 47 | |
David Gross | 97e5099 | 2017-03-29 20:52:30 +0000 | [diff] [blame] | 48 | namespace { |
| 49 | |
| 50 | // Name of metadata node where list of exported types resides |
| 51 | // (should be synced with slang_rs_metadata.h) |
| 52 | static const llvm::StringRef ExportedTypeMetadataName = "#rs_export_type"; |
| 53 | |
| 54 | // Every exported struct type must have the same layout according to |
| 55 | // the Module's DataLayout that it does according to the |
| 56 | // TargetMachine's DataLayout -- that is, the front end (represented |
| 57 | // by Module) and back end (represented by TargetMachine) must agree. |
| 58 | bool validateLayoutOfExportedTypes(const llvm::Module &module, |
| 59 | const llvm::DataLayout &moduleDataLayout, |
| 60 | const llvm::DataLayout &targetDataLayout) { |
| 61 | if (moduleDataLayout == targetDataLayout) |
| 62 | return true; |
| 63 | |
| 64 | const llvm::NamedMDNode *const exportedTypesMD = |
| 65 | module.getNamedMetadata(ExportedTypeMetadataName); |
| 66 | if (!exportedTypesMD) |
| 67 | return true; |
| 68 | |
| 69 | bool allOk = true; |
| 70 | for (const llvm::MDNode *const exportedTypeMD : exportedTypesMD->operands()) { |
| 71 | bccAssert(exportedTypeMD->getNumOperands() == 1); |
| 72 | |
| 73 | // The name of the type in LLVM is the name of the type in the |
| 74 | // metadata with "struct." prepended. |
| 75 | std::string exportedTypeName = |
| 76 | "struct." + |
| 77 | llvm::cast<llvm::MDString>(exportedTypeMD->getOperand(0))->getString().str(); |
| 78 | |
| 79 | llvm::StructType *const exportedType = module.getTypeByName(exportedTypeName); |
| 80 | |
| 81 | if (!exportedType) { |
| 82 | // presumably this means the type got optimized away |
| 83 | continue; |
| 84 | } |
| 85 | |
| 86 | const llvm::StructLayout *const moduleStructLayout = moduleDataLayout.getStructLayout(exportedType); |
| 87 | const llvm::StructLayout *const targetStructLayout = targetDataLayout.getStructLayout(exportedType); |
| 88 | |
| 89 | if (moduleStructLayout->getSizeInBits() != targetStructLayout->getSizeInBits()) { |
| 90 | ALOGE("%s: getSizeInBits() does not match (%u, %u)", exportedTypeName.c_str(), |
| 91 | unsigned(moduleStructLayout->getSizeInBits()), unsigned(targetStructLayout->getSizeInBits())); |
| 92 | allOk = false; |
| 93 | } |
| 94 | |
| 95 | // We deliberately do not check alignment of the struct as a whole -- the explicit padding |
| 96 | // from slang doesn't force the alignment. |
| 97 | |
| 98 | for (unsigned elementCount = exportedType->getNumElements(), elementIdx = 0; |
| 99 | elementIdx < elementCount; ++elementIdx) { |
| 100 | if (moduleStructLayout->getElementOffsetInBits(elementIdx) != |
| 101 | targetStructLayout->getElementOffsetInBits(elementIdx)) { |
| 102 | ALOGE("%s: getElementOffsetInBits(%u) does not match (%u, %u)", |
| 103 | exportedTypeName.c_str(), elementIdx, |
| 104 | unsigned(moduleStructLayout->getElementOffsetInBits(elementIdx)), |
| 105 | unsigned(targetStructLayout->getElementOffsetInBits(elementIdx))); |
| 106 | allOk = false; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return allOk; |
| 112 | } |
| 113 | |
| 114 | } // end unnamed namespace |
| 115 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 116 | using namespace bcc; |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 117 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 118 | const char *Compiler::GetErrorString(enum ErrorCode pErrCode) { |
Tobias Grosser | 5b7f52a | 2013-07-23 14:57:00 -0700 | [diff] [blame] | 119 | switch (pErrCode) { |
| 120 | case kSuccess: |
| 121 | return "Successfully compiled."; |
| 122 | case kInvalidConfigNoTarget: |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 123 | return "Invalid compiler config supplied (getTarget() returns nullptr.) " |
Tobias Grosser | 5b7f52a | 2013-07-23 14:57:00 -0700 | [diff] [blame] | 124 | "(missing call to CompilerConfig::initialize()?)"; |
| 125 | case kErrCreateTargetMachine: |
| 126 | return "Failed to create llvm::TargetMachine."; |
| 127 | case kErrSwitchTargetMachine: |
| 128 | return "Failed to switch llvm::TargetMachine."; |
| 129 | case kErrNoTargetMachine: |
| 130 | return "Failed to compile the script since there's no available " |
| 131 | "TargetMachine. (missing call to Compiler::config()?)"; |
Tobias Grosser | 5b7f52a | 2013-07-23 14:57:00 -0700 | [diff] [blame] | 132 | case kErrMaterialization: |
| 133 | return "Failed to materialize the module."; |
| 134 | case kErrInvalidOutputFileState: |
| 135 | return "Supplied output file was invalid (in the error state.)"; |
| 136 | case kErrPrepareOutput: |
| 137 | return "Failed to prepare file for output."; |
| 138 | case kPrepareCodeGenPass: |
| 139 | return "Failed to construct pass list for code-generation."; |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 140 | case kErrCustomPasses: |
| 141 | return "Error occurred while adding custom passes."; |
Tobias Grosser | 5b7f52a | 2013-07-23 14:57:00 -0700 | [diff] [blame] | 142 | case kErrInvalidSource: |
| 143 | return "Error loading input bitcode"; |
Pirama Arumuga Nainar | 1e0557a | 2014-12-02 15:02:18 -0800 | [diff] [blame] | 144 | case kIllegalGlobalFunction: |
| 145 | return "Use of undefined external function"; |
Pirama Arumuga Nainar | 9e0f8f0 | 2016-04-12 14:04:50 -0700 | [diff] [blame] | 146 | case kErrInvalidTargetMachine: |
| 147 | return "Invalid/unexpected llvm::TargetMachine."; |
David Gross | 97e5099 | 2017-03-29 20:52:30 +0000 | [diff] [blame] | 148 | case kErrInvalidLayout: |
| 149 | return "Invalid layout (RenderScript ABI and native ABI are incompatible)"; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 150 | } |
Zonr Chang | fef9a1b | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 151 | |
Tobias Grosser | 5b7f52a | 2013-07-23 14:57:00 -0700 | [diff] [blame] | 152 | // This assert should never be reached as the compiler verifies that the |
| 153 | // above switch coveres all enum values. |
David Gross | c2ca742 | 2015-05-29 14:54:33 -0700 | [diff] [blame] | 154 | bccAssert(false && "Unknown error code encountered"); |
Tobias Grosser | 5b7f52a | 2013-07-23 14:57:00 -0700 | [diff] [blame] | 155 | return ""; |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 158 | //===----------------------------------------------------------------------===// |
| 159 | // Instance Methods |
| 160 | //===----------------------------------------------------------------------===// |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 161 | Compiler::Compiler() : mTarget(nullptr), mEnableOpt(true) { |
Zonr Chang | fef9a1b | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 162 | return; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 163 | } |
| 164 | |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 165 | Compiler::Compiler(const CompilerConfig &pConfig) : mTarget(nullptr), |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 166 | mEnableOpt(true) { |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 167 | const std::string &triple = pConfig.getTriple(); |
| 168 | |
| 169 | enum ErrorCode err = config(pConfig); |
| 170 | if (err != kSuccess) { |
| 171 | ALOGE("%s (%s, features: %s)", GetErrorString(err), |
| 172 | triple.c_str(), pConfig.getFeatureString().c_str()); |
| 173 | return; |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 174 | } |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 175 | |
| 176 | return; |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 179 | enum Compiler::ErrorCode Compiler::config(const CompilerConfig &pConfig) { |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 180 | if (pConfig.getTarget() == nullptr) { |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 181 | return kInvalidConfigNoTarget; |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 182 | } |
| 183 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 184 | llvm::TargetMachine *new_target = |
| 185 | (pConfig.getTarget())->createTargetMachine(pConfig.getTriple(), |
| 186 | pConfig.getCPU(), |
| 187 | pConfig.getFeatureString(), |
| 188 | pConfig.getTargetOptions(), |
| 189 | pConfig.getRelocationModel(), |
| 190 | pConfig.getCodeModel(), |
| 191 | pConfig.getOptimizationLevel()); |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 192 | |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 193 | if (new_target == nullptr) { |
| 194 | return ((mTarget != nullptr) ? kErrSwitchTargetMachine : |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 195 | kErrCreateTargetMachine); |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | // Replace the old TargetMachine. |
| 199 | delete mTarget; |
| 200 | mTarget = new_target; |
| 201 | |
| 202 | // Adjust register allocation policy according to the optimization level. |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 203 | // createFastRegisterAllocator: fast but bad quality |
| 204 | // createLinearScanRegisterAllocator: not so fast but good quality |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 205 | if ((pConfig.getOptimizationLevel() == llvm::CodeGenOpt::None)) { |
| 206 | llvm::RegisterRegAlloc::setDefault(llvm::createFastRegisterAllocator); |
| 207 | } else { |
| 208 | llvm::RegisterRegAlloc::setDefault(llvm::createGreedyRegisterAllocator); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 209 | } |
| 210 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 211 | return kSuccess; |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 212 | } |
| 213 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 214 | Compiler::~Compiler() { |
| 215 | delete mTarget; |
| 216 | } |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 217 | |
Pirama Arumuga Nainar | 1e0557a | 2014-12-02 15:02:18 -0800 | [diff] [blame] | 218 | |
David Gross | 5aefc98 | 2015-08-04 10:41:33 -0700 | [diff] [blame] | 219 | // This function has complete responsibility for creating and executing the |
| 220 | // exact list of compiler passes. |
Jean-Luc Brouillet | 0a2acce | 2017-02-17 13:29:47 -0800 | [diff] [blame] | 221 | enum Compiler::ErrorCode Compiler::runPasses(Script &script, |
Pirama Arumuga Nainar | 98137cc | 2015-05-06 11:18:56 -0700 | [diff] [blame] | 222 | llvm::raw_pwrite_stream &pResult) { |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 223 | // Pass manager for link-time optimization |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 224 | llvm::legacy::PassManager transformPasses; |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 225 | |
| 226 | // Empty MCContext. |
| 227 | llvm::MCContext *mc_context = nullptr; |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 228 | |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 229 | transformPasses.add( |
| 230 | createTargetTransformInfoWrapperPass(mTarget->getTargetIRAnalysis())); |
Tim Murray | bb73b74 | 2014-11-04 11:20:10 -0800 | [diff] [blame] | 231 | |
David Gross | 5aefc98 | 2015-08-04 10:41:33 -0700 | [diff] [blame] | 232 | // Add some initial custom passes. |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 233 | addInvokeHelperPass(transformPasses); |
| 234 | addExpandKernelPass(transformPasses); |
Jean-Luc Brouillet | 0a2acce | 2017-02-17 13:29:47 -0800 | [diff] [blame] | 235 | addDebugInfoPass(script, transformPasses); |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 236 | addInvariantPass(transformPasses); |
Dean De Leo | 7a9a967 | 2015-11-25 12:51:54 +0000 | [diff] [blame] | 237 | if (mTarget->getOptLevel() != llvm::CodeGenOpt::None) { |
Jean-Luc Brouillet | 0a2acce | 2017-02-17 13:29:47 -0800 | [diff] [blame] | 238 | if (!addInternalizeSymbolsPass(script, transformPasses)) |
Dean De Leo | 7a9a967 | 2015-11-25 12:51:54 +0000 | [diff] [blame] | 239 | return kErrCustomPasses; |
| 240 | } |
Jean-Luc Brouillet | 0a2acce | 2017-02-17 13:29:47 -0800 | [diff] [blame] | 241 | addGlobalInfoPass(script, transformPasses); |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 242 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 243 | if (mTarget->getOptLevel() == llvm::CodeGenOpt::None) { |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 244 | transformPasses.add(llvm::createGlobalOptimizerPass()); |
| 245 | transformPasses.add(llvm::createConstantMergePass()); |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 246 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 247 | } else { |
Tobias Grosser | 2f6103b | 2013-07-01 14:01:06 -0700 | [diff] [blame] | 248 | // FIXME: Figure out which passes should be executed. |
| 249 | llvm::PassManagerBuilder Builder; |
Stephen Hines | 5793613 | 2014-11-25 17:54:59 -0800 | [diff] [blame] | 250 | Builder.Inliner = llvm::createFunctionInliningPass(); |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 251 | Builder.populateLTOPassManager(transformPasses); |
Tim Murray | 50f5eb4 | 2014-12-09 17:36:24 -0800 | [diff] [blame] | 252 | |
Tim Murray | 7f59b5f | 2015-02-12 14:38:05 -0800 | [diff] [blame] | 253 | /* FIXME: Reenable autovectorization after rebase. |
| 254 | bug 19324423 |
Tim Murray | 50f5eb4 | 2014-12-09 17:36:24 -0800 | [diff] [blame] | 255 | // Add vectorization passes after LTO passes are in |
| 256 | // additional flag: -unroll-runtime |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 257 | transformPasses.add(llvm::createLoopUnrollPass(-1, 16, 0, 1)); |
Tim Murray | 50f5eb4 | 2014-12-09 17:36:24 -0800 | [diff] [blame] | 258 | // Need to pass appropriate flags here: -scalarize-load-store |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 259 | transformPasses.add(llvm::createScalarizerPass()); |
| 260 | transformPasses.add(llvm::createCFGSimplificationPass()); |
| 261 | transformPasses.add(llvm::createScopedNoAliasAAPass()); |
| 262 | transformPasses.add(llvm::createScalarEvolutionAliasAnalysisPass()); |
Tim Murray | 50f5eb4 | 2014-12-09 17:36:24 -0800 | [diff] [blame] | 263 | // additional flags: -slp-vectorize-hor -slp-vectorize-hor-store (unnecessary?) |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 264 | transformPasses.add(llvm::createSLPVectorizerPass()); |
| 265 | transformPasses.add(llvm::createDeadCodeEliminationPass()); |
| 266 | transformPasses.add(llvm::createInstructionCombiningPass()); |
Tim Murray | 7f59b5f | 2015-02-12 14:38:05 -0800 | [diff] [blame] | 267 | */ |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 268 | } |
| 269 | |
Pirama Arumuga Nainar | 8c24f8d | 2015-03-17 13:11:25 -0700 | [diff] [blame] | 270 | // These passes have to come after LTO, since we don't want to examine |
| 271 | // functions that are never actually called. |
Lazar Trsic | 67c67ce | 2017-05-16 14:42:37 +0200 | [diff] [blame] | 272 | if (llvm::Triple(getTargetMachine().getTargetTriple()).getArch() == llvm::Triple::x86_64 || |
| 273 | llvm::Triple(getTargetMachine().getTargetTriple()).getArch() == llvm::Triple::mips64el) |
| 274 | transformPasses.add(createRSX86_64CallConvPass()); // Add pass to correct calling convention for X86-64 and mips64. |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 275 | transformPasses.add(createRSIsThreadablePass()); // Add pass to mark script as threadable. |
Pirama Arumuga Nainar | 9fe081b | 2015-01-27 14:09:19 -0800 | [diff] [blame] | 276 | |
| 277 | // RSEmbedInfoPass needs to come after we have scanned for non-threadable |
| 278 | // functions. |
Pirama Arumuga Nainar | 9fe081b | 2015-01-27 14:09:19 -0800 | [diff] [blame] | 279 | if (script.getEmbedInfo()) |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 280 | transformPasses.add(createRSEmbedInfoPass()); |
| 281 | |
| 282 | // Execute the passes. |
Jean-Luc Brouillet | 0a2acce | 2017-02-17 13:29:47 -0800 | [diff] [blame] | 283 | transformPasses.run(script.getSource().getModule()); |
Dean De Leo | 1e32186 | 2015-11-25 12:35:24 +0000 | [diff] [blame] | 284 | |
| 285 | // Run backend separately to avoid interference between debug metadata |
| 286 | // generation and backend initialization. |
| 287 | llvm::legacy::PassManager codeGenPasses; |
Stephen Hines | 5db508c | 2015-01-06 01:42:56 -0800 | [diff] [blame] | 288 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 289 | // 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] | 290 | if (mTarget->addPassesToEmitMC(codeGenPasses, mc_context, pResult, |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 291 | /* DisableVerify */false)) { |
| 292 | return kPrepareCodeGenPass; |
| 293 | } |
| 294 | |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 295 | // Execute the passes. |
Jean-Luc Brouillet | 0a2acce | 2017-02-17 13:29:47 -0800 | [diff] [blame] | 296 | codeGenPasses.run(script.getSource().getModule()); |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 297 | |
| 298 | return kSuccess; |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 299 | } |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 300 | |
Jean-Luc Brouillet | 0a2acce | 2017-02-17 13:29:47 -0800 | [diff] [blame] | 301 | enum Compiler::ErrorCode Compiler::compile(Script &script, |
Pirama Arumuga Nainar | 98137cc | 2015-05-06 11:18:56 -0700 | [diff] [blame] | 302 | llvm::raw_pwrite_stream &pResult, |
Tobias Grosser | 27fb7ed | 2013-06-21 18:34:56 -0700 | [diff] [blame] | 303 | llvm::raw_ostream *IRStream) { |
Jean-Luc Brouillet | 0a2acce | 2017-02-17 13:29:47 -0800 | [diff] [blame] | 304 | llvm::Module &module = script.getSource().getModule(); |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 305 | enum ErrorCode err; |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 306 | |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 307 | if (mTarget == nullptr) { |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 308 | return kErrNoTargetMachine; |
| 309 | } |
| 310 | |
Stephen Hines | 10ee6af | 2014-09-09 17:28:23 -0700 | [diff] [blame] | 311 | const std::string &triple = module.getTargetTriple(); |
Pirama Arumuga Nainar | 8e90893 | 2016-03-06 23:05:45 -0800 | [diff] [blame] | 312 | const llvm::DataLayout dl = getTargetMachine().createDataLayout(); |
| 313 | unsigned int pointerSize = dl.getPointerSizeInBits(); |
Stephen Hines | 10ee6af | 2014-09-09 17:28:23 -0700 | [diff] [blame] | 314 | if (triple == "armv7-none-linux-gnueabi") { |
| 315 | if (pointerSize != 32) { |
| 316 | return kErrInvalidSource; |
| 317 | } |
| 318 | } else if (triple == "aarch64-none-linux-gnueabi") { |
| 319 | if (pointerSize != 64) { |
| 320 | return kErrInvalidSource; |
| 321 | } |
| 322 | } else { |
| 323 | return kErrInvalidSource; |
| 324 | } |
| 325 | |
David Gross | 97e5099 | 2017-03-29 20:52:30 +0000 | [diff] [blame] | 326 | if (script.isStructExplicitlyPaddedBySlang()) { |
| 327 | if (!validateLayoutOfExportedTypes(module, module.getDataLayout(), dl)) |
| 328 | return kErrInvalidLayout; |
| 329 | } else { |
| 330 | if (getTargetMachine().getTargetTriple().getArch() == llvm::Triple::x86) { |
| 331 | // Detect and fail if TargetMachine datalayout is different than what we |
| 332 | // expect. This is to detect changes in default target layout for x86 and |
| 333 | // update X86_CUSTOM_DL_STRING in include/bcc/Config/Config.h appropriately. |
| 334 | if (dl.getStringRepresentation().compare(X86_DEFAULT_DL_STRING) != 0) { |
| 335 | return kErrInvalidTargetMachine; |
| 336 | } |
Pirama Arumuga Nainar | 9e0f8f0 | 2016-04-12 14:04:50 -0700 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
David Gross | cf8b2d0 | 2015-05-19 11:55:29 -0700 | [diff] [blame] | 340 | // Sanitize module's target information. |
Pirama Arumuga Nainar | 8e90893 | 2016-03-06 23:05:45 -0800 | [diff] [blame] | 341 | module.setTargetTriple(getTargetMachine().getTargetTriple().str()); |
| 342 | module.setDataLayout(getTargetMachine().createDataLayout()); |
David Gross | cf8b2d0 | 2015-05-19 11:55:29 -0700 | [diff] [blame] | 343 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 344 | // Materialize the bitcode module. |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 345 | if (module.getMaterializer() != nullptr) { |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 346 | // 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] | 347 | // Materialize it now. This function returns false when the materialization |
| 348 | // is successful. |
| 349 | std::error_code ec = module.materializeAll(); |
Tim Murray | c2074ca | 2014-04-08 15:39:08 -0700 | [diff] [blame] | 350 | if (ec) { |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 351 | ALOGE("Failed to materialize the module `%s'! (%s)", |
Tim Murray | c2074ca | 2014-04-08 15:39:08 -0700 | [diff] [blame] | 352 | module.getModuleIdentifier().c_str(), ec.message().c_str()); |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 353 | return kErrMaterialization; |
| 354 | } |
| 355 | } |
| 356 | |
Jean-Luc Brouillet | 0a2acce | 2017-02-17 13:29:47 -0800 | [diff] [blame] | 357 | if ((err = runPasses(script, pResult)) != kSuccess) { |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 358 | return err; |
| 359 | } |
| 360 | |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 361 | if (IRStream) { |
Tobias Grosser | 27fb7ed | 2013-06-21 18:34:56 -0700 | [diff] [blame] | 362 | *IRStream << module; |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | return kSuccess; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 366 | } |
| 367 | |
Jean-Luc Brouillet | 0a2acce | 2017-02-17 13:29:47 -0800 | [diff] [blame] | 368 | bool Compiler::addInternalizeSymbolsPass(Script &script, llvm::legacy::PassManager &pPM) { |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 369 | // Add a pass to internalize the symbols that don't need to have global |
| 370 | // visibility. |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 371 | llvm::Module &module = script.getSource().getModule(); |
| 372 | bcinfo::MetadataExtractor me(&module); |
| 373 | if (!me.extract()) { |
| 374 | bccAssert(false && "Could not extract metadata for module!"); |
| 375 | return false; |
| 376 | } |
| 377 | |
Pirama Arumuga Nainar | f5b49a0 | 2016-09-15 23:04:25 -0700 | [diff] [blame] | 378 | // Set of symbols that should not be internalized. |
| 379 | std::set<std::string> export_symbols; |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 380 | |
Stephen Hines | 107f50d | 2015-01-19 21:02:13 -0800 | [diff] [blame] | 381 | const char *sf[] = { |
Stephen Hines | fb81ec1 | 2015-05-18 20:04:23 -0700 | [diff] [blame] | 382 | kRoot, // Graphics drawing function or compute kernel. |
| 383 | kInit, // Initialization routine called implicitly on startup. |
| 384 | kRsDtor, // Static global destructor for a script instance. |
| 385 | kRsInfo, // Variable containing string of RS metadata info. |
| 386 | kRsGlobalEntries, // Optional number of global variables. |
| 387 | kRsGlobalNames, // Optional global variable name info. |
| 388 | kRsGlobalAddresses, // Optional global variable address info. |
| 389 | kRsGlobalSizes, // Optional global variable size info. |
| 390 | kRsGlobalProperties, // Optional global variable properties. |
| 391 | nullptr // Must be nullptr-terminated. |
Stephen Hines | 107f50d | 2015-01-19 21:02:13 -0800 | [diff] [blame] | 392 | }; |
| 393 | const char **special_functions = sf; |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 394 | // Special RS functions should always be global symbols. |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 395 | while (*special_functions != nullptr) { |
Pirama Arumuga Nainar | f5b49a0 | 2016-09-15 23:04:25 -0700 | [diff] [blame] | 396 | export_symbols.insert(*special_functions); |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 397 | special_functions++; |
| 398 | } |
| 399 | |
| 400 | // Visibility of symbols appeared in rs_export_var and rs_export_func should |
| 401 | // also be preserved. |
| 402 | size_t exportVarCount = me.getExportVarCount(); |
| 403 | size_t exportFuncCount = me.getExportFuncCount(); |
| 404 | size_t exportForEachCount = me.getExportForEachSignatureCount(); |
Matt Wala | 4e7a506 | 2015-07-30 16:27:51 -0700 | [diff] [blame] | 405 | size_t exportReduceCount = me.getExportReduceCount(); |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 406 | const char **exportVarNameList = me.getExportVarNameList(); |
| 407 | const char **exportFuncNameList = me.getExportFuncNameList(); |
| 408 | const char **exportForEachNameList = me.getExportForEachNameList(); |
David Gross | a48ea36 | 2016-06-02 14:46:55 -0700 | [diff] [blame] | 409 | const bcinfo::MetadataExtractor::Reduce *exportReduceList = me.getExportReduceList(); |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 410 | size_t i; |
| 411 | |
| 412 | for (i = 0; i < exportVarCount; ++i) { |
Pirama Arumuga Nainar | f5b49a0 | 2016-09-15 23:04:25 -0700 | [diff] [blame] | 413 | export_symbols.insert(exportVarNameList[i]); |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | for (i = 0; i < exportFuncCount; ++i) { |
Pirama Arumuga Nainar | f5b49a0 | 2016-09-15 23:04:25 -0700 | [diff] [blame] | 417 | export_symbols.insert(exportFuncNameList[i]); |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 418 | } |
| 419 | |
David Gross | a48ea36 | 2016-06-02 14:46:55 -0700 | [diff] [blame] | 420 | // Expanded foreach functions should not be internalized; nor should |
| 421 | // general reduction initializer, combiner, and outconverter |
| 422 | // functions. keep_funcs keeps the names of these functions around |
| 423 | // until createInternalizePass() is finished making its own copy of |
| 424 | // the visible symbols. |
David Gross | c545d6f | 2016-02-08 13:49:02 -0800 | [diff] [blame] | 425 | std::vector<std::string> keep_funcs; |
David Gross | a48ea36 | 2016-06-02 14:46:55 -0700 | [diff] [blame] | 426 | keep_funcs.reserve(exportForEachCount + exportReduceCount*4); |
Matt Wala | 4e7a506 | 2015-07-30 16:27:51 -0700 | [diff] [blame] | 427 | |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 428 | for (i = 0; i < exportForEachCount; ++i) { |
David Gross | c545d6f | 2016-02-08 13:49:02 -0800 | [diff] [blame] | 429 | keep_funcs.push_back(std::string(exportForEachNameList[i]) + ".expand"); |
Matt Wala | 4e7a506 | 2015-07-30 16:27:51 -0700 | [diff] [blame] | 430 | } |
David Gross | c545d6f | 2016-02-08 13:49:02 -0800 | [diff] [blame] | 431 | auto keepFuncsPushBackIfPresent = [&keep_funcs](const char *Name) { |
| 432 | if (Name) keep_funcs.push_back(Name); |
| 433 | }; |
David Gross | a48ea36 | 2016-06-02 14:46:55 -0700 | [diff] [blame] | 434 | for (i = 0; i < exportReduceCount; ++i) { |
| 435 | keep_funcs.push_back(std::string(exportReduceList[i].mAccumulatorName) + ".expand"); |
| 436 | keepFuncsPushBackIfPresent(exportReduceList[i].mInitializerName); |
| 437 | if (exportReduceList[i].mCombinerName != nullptr) { |
| 438 | keep_funcs.push_back(exportReduceList[i].mCombinerName); |
David Gross | 57fd9f8 | 2016-04-08 12:35:41 -0700 | [diff] [blame] | 439 | } else { |
David Gross | a48ea36 | 2016-06-02 14:46:55 -0700 | [diff] [blame] | 440 | keep_funcs.push_back(nameReduceCombinerFromAccumulator(exportReduceList[i].mAccumulatorName)); |
David Gross | 57fd9f8 | 2016-04-08 12:35:41 -0700 | [diff] [blame] | 441 | } |
David Gross | a48ea36 | 2016-06-02 14:46:55 -0700 | [diff] [blame] | 442 | keepFuncsPushBackIfPresent(exportReduceList[i].mOutConverterName); |
David Gross | 79e1a05 | 2016-01-11 14:42:51 -0800 | [diff] [blame] | 443 | } |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 444 | |
David Gross | c545d6f | 2016-02-08 13:49:02 -0800 | [diff] [blame] | 445 | for (auto &symbol_name : keep_funcs) { |
Pirama Arumuga Nainar | f5b49a0 | 2016-09-15 23:04:25 -0700 | [diff] [blame] | 446 | export_symbols.insert(symbol_name); |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Pirama Arumuga Nainar | f5b49a0 | 2016-09-15 23:04:25 -0700 | [diff] [blame] | 449 | auto IsExportedSymbol = [=](const llvm::GlobalValue &GV) { |
| 450 | return export_symbols.count(GV.getName()) > 0; |
| 451 | }; |
| 452 | |
| 453 | pPM.add(llvm::createInternalizePass(IsExportedSymbol)); |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 454 | |
| 455 | return true; |
| 456 | } |
| 457 | |
David Gross | 5aefc98 | 2015-08-04 10:41:33 -0700 | [diff] [blame] | 458 | void Compiler::addInvokeHelperPass(llvm::legacy::PassManager &pPM) { |
Tim Murray | b7bce74 | 2014-11-03 16:17:30 -0800 | [diff] [blame] | 459 | llvm::Triple arch(getTargetMachine().getTargetTriple()); |
| 460 | if (arch.isArch64Bit()) { |
| 461 | pPM.add(createRSInvokeHelperPass()); |
| 462 | } |
Tim Murray | b7bce74 | 2014-11-03 16:17:30 -0800 | [diff] [blame] | 463 | } |
| 464 | |
Jean-Luc Brouillet | 0a2acce | 2017-02-17 13:29:47 -0800 | [diff] [blame] | 465 | void Compiler::addDebugInfoPass(Script &script, llvm::legacy::PassManager &pPM) { |
| 466 | if (script.getSource().getDebugInfoEnabled()) |
Dean De Leo | 09c7a41 | 2015-11-25 12:45:45 +0000 | [diff] [blame] | 467 | pPM.add(createRSAddDebugInfoPass()); |
| 468 | } |
| 469 | |
Matt Wala | 4e7a506 | 2015-07-30 16:27:51 -0700 | [diff] [blame] | 470 | void Compiler::addExpandKernelPass(llvm::legacy::PassManager &pPM) { |
| 471 | // Expand ForEach and reduce on CPU path to reduce launch overhead. |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 472 | bool pEnableStepOpt = true; |
Matt Wala | 4e7a506 | 2015-07-30 16:27:51 -0700 | [diff] [blame] | 473 | pPM.add(createRSKernelExpandPass(pEnableStepOpt)); |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Jean-Luc Brouillet | 0a2acce | 2017-02-17 13:29:47 -0800 | [diff] [blame] | 476 | void Compiler::addGlobalInfoPass(Script &script, llvm::legacy::PassManager &pPM) { |
Stephen Hines | 750ee65 | 2015-04-16 16:24:18 -0700 | [diff] [blame] | 477 | // Add additional information about RS global variables inside the Module. |
Stephen Hines | 750ee65 | 2015-04-16 16:24:18 -0700 | [diff] [blame] | 478 | if (script.getEmbedGlobalInfo()) { |
| 479 | pPM.add(createRSGlobalInfoPass(script.getEmbedGlobalInfoSkipConstant())); |
| 480 | } |
Stephen Hines | 750ee65 | 2015-04-16 16:24:18 -0700 | [diff] [blame] | 481 | } |
| 482 | |
David Gross | 5aefc98 | 2015-08-04 10:41:33 -0700 | [diff] [blame] | 483 | void Compiler::addInvariantPass(llvm::legacy::PassManager &pPM) { |
David Gross | 1d93a19 | 2015-03-25 14:59:27 -0700 | [diff] [blame] | 484 | // Mark Loads from RsExpandKernelDriverInfo as "load.invariant". |
| 485 | // Should run after ExpandForEach and before inlining. |
| 486 | pPM.add(createRSInvariantPass()); |
Pirama Arumuga Nainar | 8c24f8d | 2015-03-17 13:11:25 -0700 | [diff] [blame] | 487 | } |
Pirama Arumuga Nainar | ebff2ea | 2015-05-21 15:45:05 -0700 | [diff] [blame] | 488 | |
Jean-Luc Brouillet | 0a2acce | 2017-02-17 13:29:47 -0800 | [diff] [blame] | 489 | enum Compiler::ErrorCode Compiler::screenGlobalFunctions(Script &script) { |
| 490 | llvm::Module &module = script.getSource().getModule(); |
Pirama Arumuga Nainar | ebff2ea | 2015-05-21 15:45:05 -0700 | [diff] [blame] | 491 | |
| 492 | // Materialize the bitcode module in case this is a lazy-load module. Do not |
| 493 | // clear the materializer by calling materializeAllPermanently since the |
| 494 | // runtime library has not been merged into the module yet. |
| 495 | if (module.getMaterializer() != nullptr) { |
| 496 | std::error_code ec = module.materializeAll(); |
| 497 | if (ec) { |
| 498 | ALOGE("Failed to materialize module `%s' when screening globals! (%s)", |
| 499 | module.getModuleIdentifier().c_str(), ec.message().c_str()); |
| 500 | return kErrMaterialization; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | // Add pass to check for illegal function calls. |
| 505 | llvm::legacy::PassManager pPM; |
| 506 | pPM.add(createRSScreenFunctionsPass()); |
| 507 | pPM.run(module); |
| 508 | |
| 509 | return kSuccess; |
| 510 | |
| 511 | } |
Pirama Arumuga Nainar | 9e0f8f0 | 2016-04-12 14:04:50 -0700 | [diff] [blame] | 512 | |
Jean-Luc Brouillet | 0a2acce | 2017-02-17 13:29:47 -0800 | [diff] [blame] | 513 | void Compiler::translateGEPs(Script &script) { |
Pirama Arumuga Nainar | 9e0f8f0 | 2016-04-12 14:04:50 -0700 | [diff] [blame] | 514 | llvm::legacy::PassManager pPM; |
| 515 | pPM.add(createRSX86TranslateGEPPass()); |
| 516 | |
| 517 | // Materialization done in screenGlobalFunctions above. |
Jean-Luc Brouillet | 0a2acce | 2017-02-17 13:29:47 -0800 | [diff] [blame] | 518 | pPM.run(script.getSource().getModule()); |
Pirama Arumuga Nainar | 9e0f8f0 | 2016-04-12 14:04:50 -0700 | [diff] [blame] | 519 | } |