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 | fe48632 | 2017-03-21 09:30:54 +0000 | [diff] [blame^] | 12 | |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 13 | #include "compiler/translator/Diagnostics.h" |
Olli Etuaho | fe48632 | 2017-03-21 09:30:54 +0000 | [diff] [blame^] | 14 | #include "compiler/translator/SymbolTable.h" |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 15 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 16 | namespace sh |
| 17 | { |
| 18 | |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 19 | // The CallDAGCreator does all the processing required to create the CallDAG |
| 20 | // structure so that the latter contains only the necessary variables. |
| 21 | class CallDAG::CallDAGCreator : public TIntermTraverser |
| 22 | { |
| 23 | public: |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 24 | CallDAGCreator(TDiagnostics *diagnostics) |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 25 | : TIntermTraverser(true, false, true), |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 26 | mDiagnostics(diagnostics), |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 27 | mCurrentFunction(nullptr), |
Corentin Wallez | bc99bb6 | 2015-05-14 17:42:20 -0400 | [diff] [blame] | 28 | mCurrentIndex(0) |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 29 | { |
| 30 | } |
| 31 | |
| 32 | InitResult assignIndices() |
| 33 | { |
| 34 | int skipped = 0; |
| 35 | for (auto &it : mFunctions) |
| 36 | { |
| 37 | // Skip unimplemented functions |
| 38 | if (it.second.node) |
| 39 | { |
| 40 | InitResult result = assignIndicesInternal(&it.second); |
| 41 | if (result != INITDAG_SUCCESS) |
| 42 | { |
| 43 | return result; |
| 44 | } |
| 45 | } |
| 46 | else |
| 47 | { |
| 48 | skipped++; |
| 49 | } |
| 50 | } |
Corentin Wallez | a59fcdf | 2016-09-14 10:52:14 -0400 | [diff] [blame] | 51 | |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 52 | ASSERT(mFunctions.size() == mCurrentIndex + skipped); |
| 53 | return INITDAG_SUCCESS; |
| 54 | } |
| 55 | |
| 56 | void fillDataStructures(std::vector<Record> *records, std::map<int, int> *idToIndex) |
| 57 | { |
| 58 | ASSERT(records->empty()); |
| 59 | ASSERT(idToIndex->empty()); |
| 60 | |
| 61 | records->resize(mCurrentIndex); |
| 62 | |
| 63 | for (auto &it : mFunctions) |
| 64 | { |
| 65 | CreatorFunctionData &data = it.second; |
| 66 | // Skip unimplemented functions |
| 67 | if (!data.node) |
| 68 | { |
| 69 | continue; |
| 70 | } |
| 71 | ASSERT(data.index < records->size()); |
| 72 | Record &record = (*records)[data.index]; |
| 73 | |
| 74 | record.name = data.name.data(); |
| 75 | record.node = data.node; |
| 76 | |
| 77 | record.callees.reserve(data.callees.size()); |
| 78 | for (auto &callee : data.callees) |
| 79 | { |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 80 | record.callees.push_back(static_cast<int>(callee->index)); |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Olli Etuaho | fe48632 | 2017-03-21 09:30:54 +0000 | [diff] [blame^] | 83 | (*idToIndex)[data.node->getFunctionSymbolInfo()->getId().get()] = |
Olli Etuaho | bd67455 | 2016-10-06 13:28:42 +0100 | [diff] [blame] | 84 | static_cast<int>(data.index); |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | private: |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 89 | struct CreatorFunctionData |
| 90 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 91 | CreatorFunctionData() : node(nullptr), index(0), indexAssigned(false), visiting(false) {} |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 92 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 93 | std::set<CreatorFunctionData *> callees; |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 94 | TIntermFunctionDefinition *node; |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 95 | TString name; |
| 96 | size_t index; |
| 97 | bool indexAssigned; |
| 98 | bool visiting; |
| 99 | }; |
| 100 | |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 101 | bool visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node) override |
| 102 | { |
| 103 | // Create the record if need be and remember the node. |
| 104 | if (visit == PreVisit) |
| 105 | { |
Olli Etuaho | fe48632 | 2017-03-21 09:30:54 +0000 | [diff] [blame^] | 106 | auto it = mFunctions.find(node->getFunctionSymbolInfo()->getId().get()); |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 107 | |
| 108 | if (it == mFunctions.end()) |
| 109 | { |
Olli Etuaho | fe48632 | 2017-03-21 09:30:54 +0000 | [diff] [blame^] | 110 | mCurrentFunction = &mFunctions[node->getFunctionSymbolInfo()->getId().get()]; |
| 111 | mCurrentFunction->name = node->getFunctionSymbolInfo()->getName(); |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 112 | } |
| 113 | else |
| 114 | { |
| 115 | mCurrentFunction = &it->second; |
Olli Etuaho | fe48632 | 2017-03-21 09:30:54 +0000 | [diff] [blame^] | 116 | ASSERT(mCurrentFunction->name == node->getFunctionSymbolInfo()->getName()); |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | mCurrentFunction->node = node; |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 120 | } |
| 121 | else if (visit == PostVisit) |
| 122 | { |
| 123 | mCurrentFunction = nullptr; |
| 124 | } |
| 125 | return true; |
| 126 | } |
| 127 | |
Olli Etuaho | 16c745a | 2017-01-16 17:02:27 +0000 | [diff] [blame] | 128 | bool visitFunctionPrototype(Visit visit, TIntermFunctionPrototype *node) override |
| 129 | { |
| 130 | ASSERT(visit == PreVisit); |
Olli Etuaho | fe48632 | 2017-03-21 09:30:54 +0000 | [diff] [blame^] | 131 | if (mCurrentFunction != nullptr) |
| 132 | { |
| 133 | return false; |
| 134 | } |
| 135 | |
Olli Etuaho | 16c745a | 2017-01-16 17:02:27 +0000 | [diff] [blame] | 136 | // Function declaration, create an empty record. |
Olli Etuaho | fe48632 | 2017-03-21 09:30:54 +0000 | [diff] [blame^] | 137 | auto &record = mFunctions[node->getFunctionSymbolInfo()->getId().get()]; |
Olli Etuaho | 16c745a | 2017-01-16 17:02:27 +0000 | [diff] [blame] | 138 | record.name = node->getFunctionSymbolInfo()->getName(); |
| 139 | |
| 140 | // No need to traverse the parameters. |
| 141 | return false; |
| 142 | } |
| 143 | |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 144 | // Aggregates the AST node for each function as well as the name of the functions called by it |
| 145 | bool visitAggregate(Visit visit, TIntermAggregate *node) override |
| 146 | { |
Olli Etuaho | 1ecd14b | 2017-01-26 13:54:15 -0800 | [diff] [blame] | 147 | if (visit == PreVisit && node->getOp() == EOpCallFunctionInAST) |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 148 | { |
Olli Etuaho | 1ecd14b | 2017-01-26 13:54:15 -0800 | [diff] [blame] | 149 | // Function call, add the callees |
Olli Etuaho | fe48632 | 2017-03-21 09:30:54 +0000 | [diff] [blame^] | 150 | auto it = mFunctions.find(node->getFunctionSymbolInfo()->getId().get()); |
Olli Etuaho | 1ecd14b | 2017-01-26 13:54:15 -0800 | [diff] [blame] | 151 | ASSERT(it != mFunctions.end()); |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 152 | |
Olli Etuaho | fe48632 | 2017-03-21 09:30:54 +0000 | [diff] [blame^] | 153 | // We might be traversing the initializer of a global variable. Even though function |
| 154 | // calls in global scope are forbidden by the parser, some subsequent AST |
| 155 | // transformations can add them to emulate particular features. |
Olli Etuaho | 1ecd14b | 2017-01-26 13:54:15 -0800 | [diff] [blame] | 156 | if (mCurrentFunction) |
| 157 | { |
| 158 | mCurrentFunction->callees.insert(&it->second); |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 159 | } |
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 | |
Olli Etuaho | fe48632 | 2017-03-21 09:30:54 +0000 | [diff] [blame^] | 272 | std::map<int, CreatorFunctionData> mFunctions; |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 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 | fe48632 | 2017-03-21 09:30:54 +0000 | [diff] [blame^] | 291 | auto it = mFunctionIdToIndex.find(functionInfo->getId().get()); |
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 |