call_graph.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright (c) 2020 Google LLC
  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. #ifndef SOURCE_FUZZ_CALL_GRAPH_H_
  15. #define SOURCE_FUZZ_CALL_GRAPH_H_
  16. #include <map>
  17. #include <set>
  18. #include "source/opt/ir_context.h"
  19. namespace spvtools {
  20. namespace fuzz {
  21. // Represents the acyclic call graph of a SPIR-V module.
  22. // The module is assumed to be recursion-free, so there are no cycles in the
  23. // graph. This class is immutable, so it will need to be recomputed if the
  24. // module changes.
  25. class CallGraph {
  26. public:
  27. // Creates a call graph corresponding to the given SPIR-V module.
  28. explicit CallGraph(opt::IRContext* context);
  29. // Returns a mapping from each function to its number of distinct callers.
  30. const std::map<uint32_t, uint32_t>& GetFunctionInDegree() const {
  31. return function_in_degree_;
  32. }
  33. // Returns the ids of the functions that |function_id| directly invokes.
  34. const std::set<uint32_t>& GetDirectCallees(uint32_t function_id) const {
  35. return call_graph_edges_.at(function_id);
  36. }
  37. // Returns the ids of the functions that |function_id| directly or indirectly
  38. // invokes.
  39. std::set<uint32_t> GetIndirectCallees(uint32_t function_id) const;
  40. // Returns the ids of all the functions in the graph in a topological order,
  41. // in relation to the function calls, which are assumed to be recursion-free.
  42. const std::vector<uint32_t>& GetFunctionsInTopologicalOrder() const {
  43. return functions_in_topological_order_;
  44. }
  45. // Returns the maximum loop nesting depth from which |function_id| can be
  46. // called. This is computed inter-procedurally (i.e. if main calls A from
  47. // depth 2 and A calls B from depth 1, the result will be 3 for A).
  48. // This is a static analysis, so it's not necessarily true that the depth
  49. // returned can actually be reached at runtime.
  50. uint32_t GetMaxCallNestingDepth(uint32_t function_id) const {
  51. return function_max_loop_nesting_depth_.at(function_id);
  52. }
  53. private:
  54. // Computes |call_graph_edges_| and |function_in_degree_|. For each pair (A,
  55. // B) of functions such that there is at least a function call from A to B,
  56. // adds, to |call_to_max_depth|, a mapping from (A, B) to the maximum loop
  57. // nesting depth (within A) of any such function call.
  58. void BuildGraphAndGetDepthOfFunctionCalls(
  59. opt::IRContext* context,
  60. std::map<std::pair<uint32_t, uint32_t>, uint32_t>* call_to_max_depth);
  61. // Computes a topological order of the functions in the graph, writing the
  62. // result to |functions_in_topological_order_|. Assumes that the function
  63. // calls are recursion-free and that |function_in_degree_| has been computed.
  64. void ComputeTopologicalOrderOfFunctions();
  65. // Computes |function_max_loop_nesting_depth_| so that each function is mapped
  66. // to the maximum loop nesting depth from which it can be called, as described
  67. // by the comment to GetMaxCallNestingDepth. Assumes that |call_graph_edges_|
  68. // and |functions_in_topological_order_| have been computed, and that
  69. // |call_to_max_depth| contains a mapping for each edge in the graph.
  70. void ComputeInterproceduralFunctionCallDepths(
  71. const std::map<std::pair<uint32_t, uint32_t>, uint32_t>&
  72. call_to_max_depth);
  73. // Pushes the direct callees of |function_id| on to |queue|.
  74. void PushDirectCallees(uint32_t function_id,
  75. std::queue<uint32_t>* queue) const;
  76. // Maps each function id to the ids of its immediate callees.
  77. std::map<uint32_t, std::set<uint32_t>> call_graph_edges_;
  78. // For each function id, stores the number of distinct functions that call
  79. // the function.
  80. std::map<uint32_t, uint32_t> function_in_degree_;
  81. // Stores the ids of the functions in a topological order,
  82. // in relation to the function calls, which are assumed to be recursion-free.
  83. std::vector<uint32_t> functions_in_topological_order_;
  84. // For each function id, stores the maximum loop nesting depth that the
  85. // function can be called from.
  86. std::map<uint32_t, uint32_t> function_max_loop_nesting_depth_;
  87. };
  88. } // namespace fuzz
  89. } // namespace spvtools
  90. #endif // SOURCE_FUZZ_CALL_GRAPH_H_