Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2002-2015 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // CallDAG.h: Implements a call graph DAG of functions to be re-used accross |
| 8 | // analyses, allows to efficiently traverse the functions in topological |
| 9 | // order. |
| 10 | |
| 11 | #include "compiler/translator/CallDAG.h" |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame^] | 12 | #include "compiler/translator/Diagnostics.h" |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 13 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 14 | namespace sh |
| 15 | { |
| 16 | |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 17 | // The CallDAGCreator does all the processing required to create the CallDAG |
| 18 | // structure so that the latter contains only the necessary variables. |
| 19 | class CallDAG::CallDAGCreator : public TIntermTraverser |
| 20 | { |
| 21 | public: |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame^] | 22 | CallDAGCreator(TDiagnostics *diagnostics) |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 23 | : TIntermTraverser(true, false, true), |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame^] | 24 | mDiagnostics(diagnostics), |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 25 | mCurrentFunction(nullptr), |
Corentin Wallez | bc99bb6 | 2015-05-14 17:42:20 -0400 | [diff] [blame] | 26 | mCurrentIndex(0) |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 27 | { |
| 28 | } |
| 29 | |
| 30 | InitResult assignIndices() |
| 31 | { |
| 32 | int skipped = 0; |
| 33 | for (auto &it : mFunctions) |
| 34 | { |
| 35 | // Skip unimplemented functions |
| 36 | if (it.second.node) |
| 37 | { |
| 38 | InitResult result = assignIndicesInternal(&it.second); |
| 39 | if (result != INITDAG_SUCCESS) |
| 40 | { |
| 41 | return result; |
| 42 | } |
| 43 | } |
| 44 | else |
| 45 | { |
| 46 | skipped++; |
| 47 | } |
| 48 | } |
Corentin Wallez | a59fcdf | 2016-09-14 10:52:14 -0400 | [diff] [blame] | 49 | |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 50 | ASSERT(mFunctions.size() == mCurrentIndex + skipped); |
| 51 | return INITDAG_SUCCESS; |
| 52 | } |
| 53 | |
| 54 | void fillDataStructures(std::vector<Record> *records, std::map<int, int> *idToIndex) |
| 55 | { |
| 56 | ASSERT(records->empty()); |
| 57 | ASSERT(idToIndex->empty()); |
| 58 | |
| 59 | records->resize(mCurrentIndex); |
| 60 | |
| 61 | for (auto &it : mFunctions) |
| 62 | { |
| 63 | CreatorFunctionData &data = it.second; |
| 64 | // Skip unimplemented functions |
| 65 | if (!data.node) |
| 66 | { |
| 67 | continue; |
| 68 | } |
| 69 | ASSERT(data.index < records->size()); |
| 70 | Record &record = (*records)[data.index]; |
| 71 | |
| 72 | record.name = data.name.data(); |
| 73 | record.node = data.node; |
| 74 | |
| 75 | record.callees.reserve(data.callees.size()); |
| 76 | for (auto &callee : data.callees) |
| 77 | { |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 78 | record.callees.push_back(static_cast<int>(callee->index)); |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Olli Etuaho | bd67455 | 2016-10-06 13:28:42 +0100 | [diff] [blame] | 81 | (*idToIndex)[data.node->getFunctionSymbolInfo()->getId()] = |
| 82 | static_cast<int>(data.index); |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | private: |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 87 | struct CreatorFunctionData |
| 88 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 89 | CreatorFunctionData() : node(nullptr), index(0), indexAssigned(false), visiting(false) {} |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 90 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 91 | std::set<CreatorFunctionData *> callees; |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 92 | TIntermFunctionDefinition *node; |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 93 | TString name; |
| 94 | size_t index; |
| 95 | bool indexAssigned; |
| 96 | bool visiting; |
| 97 | }; |
| 98 | |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 99 | bool visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node) override |
| 100 | { |
| 101 | // Create the record if need be and remember the node. |
| 102 | if (visit == PreVisit) |
| 103 | { |
| 104 | auto it = mFunctions.find(node->getFunctionSymbolInfo()->getName()); |
| 105 | |
| 106 | if (it == mFunctions.end()) |
| 107 | { |
| 108 | mCurrentFunction = &mFunctions[node->getFunctionSymbolInfo()->getName()]; |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | mCurrentFunction = &it->second; |
| 113 | } |
| 114 | |
| 115 | mCurrentFunction->node = node; |
| 116 | mCurrentFunction->name = node->getFunctionSymbolInfo()->getName(); |
| 117 | } |
| 118 | else if (visit == PostVisit) |
| 119 | { |
| 120 | mCurrentFunction = nullptr; |
| 121 | } |
| 122 | return true; |
| 123 | } |
| 124 | |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 125 | // Aggregates the AST node for each function as well as the name of the functions called by it |
| 126 | bool visitAggregate(Visit visit, TIntermAggregate *node) override |
| 127 | { |
| 128 | switch (node->getOp()) |
| 129 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 130 | case EOpPrototype: |
| 131 | if (visit == PreVisit) |
| 132 | { |
| 133 | // Function declaration, create an empty record. |
| 134 | auto &record = mFunctions[node->getFunctionSymbolInfo()->getName()]; |
| 135 | record.name = node->getFunctionSymbolInfo()->getName(); |
| 136 | } |
| 137 | break; |
| 138 | case EOpFunctionCall: |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 139 | { |
| 140 | // Function call, add the callees |
| 141 | if (visit == PreVisit) |
| 142 | { |
| 143 | // Do not handle calls to builtin functions |
| 144 | if (node->isUserDefined()) |
| 145 | { |
Olli Etuaho | bd67455 | 2016-10-06 13:28:42 +0100 | [diff] [blame] | 146 | auto it = mFunctions.find(node->getFunctionSymbolInfo()->getName()); |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 147 | ASSERT(it != mFunctions.end()); |
| 148 | |
| 149 | // We might be in a top-level function call to set a global variable |
| 150 | if (mCurrentFunction) |
| 151 | { |
| 152 | mCurrentFunction->callees.insert(&it->second); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | break; |
| 157 | } |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 158 | default: |
| 159 | break; |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 160 | } |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | // Recursively assigns indices to a sub DAG |
Corentin Wallez | a59fcdf | 2016-09-14 10:52:14 -0400 | [diff] [blame] | 165 | InitResult assignIndicesInternal(CreatorFunctionData *root) |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 166 | { |
Corentin Wallez | a59fcdf | 2016-09-14 10:52:14 -0400 | [diff] [blame] | 167 | // Iterative implementation of the index assignment algorithm. A recursive version |
| 168 | // would be prettier but since the CallDAG creation runs before the limiting of the |
| 169 | // call depth, we might get stack overflows (computation of the call depth uses the |
| 170 | // CallDAG). |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 171 | |
Corentin Wallez | a59fcdf | 2016-09-14 10:52:14 -0400 | [diff] [blame] | 172 | ASSERT(root); |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 173 | |
Corentin Wallez | a59fcdf | 2016-09-14 10:52:14 -0400 | [diff] [blame] | 174 | if (root->indexAssigned) |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 175 | { |
| 176 | return INITDAG_SUCCESS; |
| 177 | } |
| 178 | |
Corentin Wallez | a59fcdf | 2016-09-14 10:52:14 -0400 | [diff] [blame] | 179 | // If we didn't have to detect recursion, functionsToProcess could be a simple queue |
| 180 | // in which we add the function being processed's callees. However in order to detect |
| 181 | // recursion we need to know which functions we are currently visiting. For that reason |
| 182 | // functionsToProcess will look like a concatenation of segments of the form |
| 183 | // [F visiting = true, subset of F callees with visiting = false] and the following |
| 184 | // segment (if any) will be start with a callee of F. |
| 185 | // This way we can remember when we started visiting a function, to put visiting back |
| 186 | // to false. |
| 187 | TVector<CreatorFunctionData *> functionsToProcess; |
| 188 | functionsToProcess.push_back(root); |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 189 | |
Corentin Wallez | a59fcdf | 2016-09-14 10:52:14 -0400 | [diff] [blame] | 190 | InitResult result = INITDAG_SUCCESS; |
| 191 | |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame^] | 192 | std::stringstream errorStream; |
| 193 | |
Corentin Wallez | a59fcdf | 2016-09-14 10:52:14 -0400 | [diff] [blame] | 194 | while (!functionsToProcess.empty()) |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 195 | { |
Corentin Wallez | a59fcdf | 2016-09-14 10:52:14 -0400 | [diff] [blame] | 196 | CreatorFunctionData *function = functionsToProcess.back(); |
| 197 | |
| 198 | if (function->visiting) |
| 199 | { |
| 200 | function->visiting = false; |
| 201 | function->index = mCurrentIndex++; |
| 202 | function->indexAssigned = true; |
| 203 | |
| 204 | functionsToProcess.pop_back(); |
| 205 | continue; |
| 206 | } |
| 207 | |
| 208 | if (!function->node) |
| 209 | { |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame^] | 210 | errorStream << "Undefined function '" << function->name |
| 211 | << ")' used in the following call chain:"; |
Corentin Wallez | a59fcdf | 2016-09-14 10:52:14 -0400 | [diff] [blame] | 212 | result = INITDAG_UNDEFINED; |
| 213 | break; |
| 214 | } |
| 215 | |
| 216 | if (function->indexAssigned) |
| 217 | { |
| 218 | functionsToProcess.pop_back(); |
| 219 | continue; |
| 220 | } |
| 221 | |
| 222 | function->visiting = true; |
| 223 | |
| 224 | for (auto callee : function->callees) |
| 225 | { |
| 226 | functionsToProcess.push_back(callee); |
| 227 | |
| 228 | // Check if the callee is already being visited after pushing it so that it appears |
| 229 | // in the chain printed in the info log. |
| 230 | if (callee->visiting) |
| 231 | { |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame^] | 232 | errorStream << "Recursive function call in the following call chain:"; |
Corentin Wallez | a59fcdf | 2016-09-14 10:52:14 -0400 | [diff] [blame] | 233 | result = INITDAG_RECURSION; |
| 234 | break; |
| 235 | } |
| 236 | } |
| 237 | |
Corentin Wallez | 91dec84 | 2015-12-08 15:18:08 -0500 | [diff] [blame] | 238 | if (result != INITDAG_SUCCESS) |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 239 | { |
Corentin Wallez | a59fcdf | 2016-09-14 10:52:14 -0400 | [diff] [blame] | 240 | break; |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
Corentin Wallez | a59fcdf | 2016-09-14 10:52:14 -0400 | [diff] [blame] | 244 | // The call chain is made of the function we were visiting when the error was detected. |
| 245 | if (result != INITDAG_SUCCESS) |
| 246 | { |
| 247 | bool first = true; |
| 248 | for (auto function : functionsToProcess) |
| 249 | { |
| 250 | if (function->visiting) |
| 251 | { |
| 252 | if (!first) |
| 253 | { |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame^] | 254 | errorStream << " -> "; |
Corentin Wallez | a59fcdf | 2016-09-14 10:52:14 -0400 | [diff] [blame] | 255 | } |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame^] | 256 | errorStream << function->name << ")"; |
Corentin Wallez | a59fcdf | 2016-09-14 10:52:14 -0400 | [diff] [blame] | 257 | first = false; |
| 258 | } |
| 259 | } |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame^] | 260 | if (mDiagnostics) |
| 261 | { |
| 262 | std::string errorStr = errorStream.str(); |
| 263 | mDiagnostics->globalError(errorStr.c_str()); |
| 264 | } |
Corentin Wallez | a59fcdf | 2016-09-14 10:52:14 -0400 | [diff] [blame] | 265 | } |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 266 | |
Corentin Wallez | a59fcdf | 2016-09-14 10:52:14 -0400 | [diff] [blame] | 267 | return result; |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 268 | } |
| 269 | |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame^] | 270 | TDiagnostics *mDiagnostics; |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 271 | |
| 272 | std::map<TString, CreatorFunctionData> mFunctions; |
| 273 | CreatorFunctionData *mCurrentFunction; |
| 274 | size_t mCurrentIndex; |
| 275 | }; |
| 276 | |
| 277 | // CallDAG |
| 278 | |
| 279 | CallDAG::CallDAG() |
| 280 | { |
| 281 | } |
| 282 | |
| 283 | CallDAG::~CallDAG() |
| 284 | { |
| 285 | } |
| 286 | |
| 287 | const size_t CallDAG::InvalidIndex = std::numeric_limits<size_t>::max(); |
| 288 | |
Olli Etuaho | bd67455 | 2016-10-06 13:28:42 +0100 | [diff] [blame] | 289 | size_t CallDAG::findIndex(const TFunctionSymbolInfo *functionInfo) const |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 290 | { |
Olli Etuaho | bd67455 | 2016-10-06 13:28:42 +0100 | [diff] [blame] | 291 | auto it = mFunctionIdToIndex.find(functionInfo->getId()); |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 292 | |
| 293 | if (it == mFunctionIdToIndex.end()) |
| 294 | { |
| 295 | return InvalidIndex; |
| 296 | } |
| 297 | else |
| 298 | { |
| 299 | return it->second; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | const CallDAG::Record &CallDAG::getRecordFromIndex(size_t index) const |
| 304 | { |
| 305 | ASSERT(index != InvalidIndex && index < mRecords.size()); |
| 306 | return mRecords[index]; |
| 307 | } |
| 308 | |
| 309 | const CallDAG::Record &CallDAG::getRecord(const TIntermAggregate *function) const |
| 310 | { |
Olli Etuaho | bd67455 | 2016-10-06 13:28:42 +0100 | [diff] [blame] | 311 | size_t index = findIndex(function->getFunctionSymbolInfo()); |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 312 | ASSERT(index != InvalidIndex && index < mRecords.size()); |
| 313 | return mRecords[index]; |
| 314 | } |
| 315 | |
| 316 | size_t CallDAG::size() const |
| 317 | { |
| 318 | return mRecords.size(); |
| 319 | } |
| 320 | |
| 321 | void CallDAG::clear() |
| 322 | { |
| 323 | mRecords.clear(); |
| 324 | mFunctionIdToIndex.clear(); |
| 325 | } |
| 326 | |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame^] | 327 | CallDAG::InitResult CallDAG::init(TIntermNode *root, TDiagnostics *diagnostics) |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 328 | { |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame^] | 329 | CallDAGCreator creator(diagnostics); |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 330 | |
| 331 | // Creates the mapping of functions to callees |
| 332 | root->traverse(&creator); |
| 333 | |
| 334 | // Does the topological sort and detects recursions |
| 335 | InitResult result = creator.assignIndices(); |
| 336 | if (result != INITDAG_SUCCESS) |
| 337 | { |
| 338 | return result; |
| 339 | } |
| 340 | |
| 341 | creator.fillDataStructures(&mRecords, &mFunctionIdToIndex); |
| 342 | return INITDAG_SUCCESS; |
| 343 | } |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 344 | |
| 345 | } // namespace sh |