alan-baker | a71f193 | 2019-04-11 11:04:34 -0400 | [diff] [blame] | 1 | // Copyright 2019 The Clspv Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include <set> |
| 16 | |
| 17 | #include "llvm/ADT/DenseMap.h" |
| 18 | #include "llvm/ADT/SmallVector.h" |
| 19 | #include "llvm/IR/Instructions.h" |
| 20 | |
| 21 | #include "CallGraphOrderedFunctions.h" |
| 22 | |
| 23 | using namespace llvm; |
| 24 | |
| 25 | namespace clspv { |
| 26 | |
| 27 | UniqueVector<Function *> CallGraphOrderedFunctions(Module &M) { |
| 28 | // Use a topological sort. |
| 29 | |
| 30 | // Make an ordered list of all functions having bodies, with kernel entry |
| 31 | // points listed first. |
| 32 | UniqueVector<Function *> functions; |
| 33 | SmallVector<Function *, 10> entry_points; |
| 34 | for (Function &F : M) { |
| 35 | if (F.isDeclaration()) { |
| 36 | continue; |
| 37 | } |
| 38 | if (F.getCallingConv() == CallingConv::SPIR_KERNEL) { |
| 39 | functions.insert(&F); |
| 40 | entry_points.push_back(&F); |
| 41 | } |
| 42 | } |
| 43 | // Add the remaining functions. |
| 44 | for (Function &F : M) { |
| 45 | if (F.isDeclaration()) { |
| 46 | continue; |
| 47 | } |
| 48 | if (F.getCallingConv() != CallingConv::SPIR_KERNEL) { |
| 49 | functions.insert(&F); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // This will be a complete set of reveresed edges, i.e. with all pairs |
| 54 | // of (callee, caller). |
| 55 | using Edge = std::pair<unsigned, unsigned>; |
| 56 | auto make_edge = [&functions](Function *callee, Function *caller) { |
| 57 | return std::pair<unsigned, unsigned>{functions.idFor(callee), |
| 58 | functions.idFor(caller)}; |
| 59 | }; |
| 60 | std::set<Edge> reverse_edges; |
| 61 | // Map each function to the functions it calls, and populate |reverse_edges|. |
| 62 | std::map<Function *, SmallVector<Function *, 3>> calls_functions; |
| 63 | for (Function *callee : functions) { |
| 64 | for (auto &use : callee->uses()) { |
| 65 | if (auto *call = dyn_cast<CallInst>(use.getUser())) { |
| 66 | Function *caller = call->getParent()->getParent(); |
| 67 | calls_functions[caller].push_back(callee); |
| 68 | reverse_edges.insert(make_edge(callee, caller)); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | // Sort the callees in module-order. This helps us produce a deterministic |
| 73 | // result. |
| 74 | for (auto &pair : calls_functions) { |
| 75 | auto &callees = pair.second; |
| 76 | std::sort(callees.begin(), callees.end(), |
| 77 | [&functions](Function *lhs, Function *rhs) { |
| 78 | return functions.idFor(lhs) < functions.idFor(rhs); |
| 79 | }); |
| 80 | } |
| 81 | |
| 82 | // Use Kahn's algorithm for topoological sort. |
| 83 | UniqueVector<Function *> result; |
| 84 | SmallVector<Function *, 10> work_list(entry_points.begin(), |
| 85 | entry_points.end()); |
| 86 | while (!work_list.empty()) { |
| 87 | Function *caller = work_list.back(); |
| 88 | work_list.pop_back(); |
| 89 | result.insert(caller); |
| 90 | auto &callees = calls_functions[caller]; |
| 91 | for (auto *callee : callees) { |
| 92 | reverse_edges.erase(make_edge(callee, caller)); |
| 93 | auto lower_bound = reverse_edges.lower_bound(make_edge(callee, nullptr)); |
| 94 | if (lower_bound == reverse_edges.end() || |
| 95 | lower_bound->first != functions.idFor(callee)) { |
| 96 | // Callee has no other unvisited callers. |
| 97 | work_list.push_back(callee); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | // If reverse_edges is not empty then there was a cycle. But we don't care |
| 102 | // about that erroneous case. |
| 103 | |
| 104 | return result; |
| 105 | } |
| 106 | |
| 107 | } // namespace clspv |