|
| 1 | +// Copyright 2024 The Dawn & Tint Authors |
| 2 | +// |
| 3 | +// Redistribution and use in source and binary forms, with or without |
| 4 | +// modification, are permitted provided that the following conditions are met: |
| 5 | +// |
| 6 | +// 1. Redistributions of source code must retain the above copyright notice, this |
| 7 | +// list of conditions and the following disclaimer. |
| 8 | +// |
| 9 | +// 2. Redistributions in binary form must reproduce the above copyright notice, |
| 10 | +// this list of conditions and the following disclaimer in the documentation |
| 11 | +// and/or other materials provided with the distribution. |
| 12 | +// |
| 13 | +// 3. Neither the name of the copyright holder nor the names of its |
| 14 | +// contributors may be used to endorse or promote products derived from |
| 15 | +// this software without specific prior written permission. |
| 16 | +// |
| 17 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 | +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 21 | +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 23 | +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 24 | +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 25 | +// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | + |
| 28 | +#ifndef SRC_TINT_LANG_CORE_IR_REFERENCED_FUNCTIONS_H_ |
| 29 | +#define SRC_TINT_LANG_CORE_IR_REFERENCED_FUNCTIONS_H_ |
| 30 | + |
| 31 | +#include "src/tint/lang/core/ir/control_instruction.h" |
| 32 | +#include "src/tint/lang/core/ir/module.h" |
| 33 | +#include "src/tint/lang/core/ir/return.h" |
| 34 | +#include "src/tint/lang/core/ir/user_call.h" |
| 35 | +#include "src/tint/utils/containers/hashmap.h" |
| 36 | +#include "src/tint/utils/rtti/switch.h" |
| 37 | + |
| 38 | +/// Utility that helps guarantee makes sure the same const-ness is applied to both type |
| 39 | +template <class Src, class Dst> |
| 40 | +using TranscribeConst = std::conditional_t<std::is_const<Src>{}, std::add_const_t<Dst>, Dst>; |
| 41 | + |
| 42 | +namespace tint::core::ir { |
| 43 | + |
| 44 | +/// ReferencedFunctions is a helper to determine the set of functions that are transitively |
| 45 | +/// referenced by functions in a module. References are determined lazily and cached for future |
| 46 | +/// requests. |
| 47 | +/// |
| 48 | +/// Note: |
| 49 | +/// The template param M is used to ensure that inputs and outputs of this class have the same |
| 50 | +/// const-ness. If 'Module' is supplied then the internal operations and output will not be |
| 51 | +/// const, which is needed for transforms. Whereas if the param is 'const Module' the internals |
| 52 | +/// and outputs will be const, which is needed for the IR validator. |
| 53 | +/// Note: |
| 54 | +/// Changes to the module can invalidate the cached data. This is intended to be created by |
| 55 | +/// operations that need this information, and discarded when they complete. Tracking this |
| 56 | +/// information inside the IR module would add overhead any time an instruction is added or |
| 57 | +/// removed from the module. Since only a few operations need this information, it is expected |
| 58 | +/// to be more efficient to generate it on demand. |
| 59 | +template <typename M> |
| 60 | +class ReferencedFunctions { |
| 61 | + // Replace this with concepts when C++20 is available. |
| 62 | + static_assert(std::is_same<std::remove_cv_t<M>, Module>()); |
| 63 | + |
| 64 | + public: |
| 65 | + /// Short form aliases for types that have the same constant-ness as M. |
| 66 | + using BlockT = TranscribeConst<M, Block>; |
| 67 | + using FunctionT = TranscribeConst<M, Function>; |
| 68 | + |
| 69 | + /// A set of a functions referenced by a function. |
| 70 | + using FunctionSet = Hashset<FunctionT*, 16>; |
| 71 | + |
| 72 | + /// Constructor. |
| 73 | + /// @param ir the module |
| 74 | + explicit ReferencedFunctions(M& ir) { |
| 75 | + // Loop over functions, recording the blocks that they are called from. |
| 76 | + for (auto func : ir.functions) { |
| 77 | + if (!func) { |
| 78 | + continue; |
| 79 | + } |
| 80 | + func->ForEachUseUnsorted([&](const Usage& use) { |
| 81 | + if (auto* call = use.instruction->As<UserCall>()) { |
| 82 | + block_to_direct_calls_.GetOrAddZero(call->Block()).Add(func); |
| 83 | + } else { |
| 84 | + TINT_ASSERT(use.instruction->Is<Return>()); |
| 85 | + } |
| 86 | + }); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /// Get the set of transitively referenced functions for a function. |
| 91 | + /// @param func the function |
| 92 | + /// @returns the set of transitively reference functions |
| 93 | + FunctionSet& TransitiveReferences(FunctionT* func) { |
| 94 | + return transitive_references_.GetOrAdd(func, [&] { |
| 95 | + FunctionSet functions; |
| 96 | + if (!func) { |
| 97 | + return functions; |
| 98 | + } |
| 99 | + |
| 100 | + // Walk blocks in the function to find function calls. |
| 101 | + Vector<BlockT*, 64> block_queue{func->Block()}; |
| 102 | + while (!block_queue.IsEmpty()) { |
| 103 | + auto* next = block_queue.Pop(); |
| 104 | + if (!next) { |
| 105 | + continue; |
| 106 | + } |
| 107 | + |
| 108 | + // Add directly referenced functions. |
| 109 | + if (auto itr = block_to_direct_calls_.Get(next)) { |
| 110 | + for (auto& callee : *itr) { |
| 111 | + if (functions.Add(callee)) { |
| 112 | + // Add functions transitively referenced by the callee. |
| 113 | + const auto& callee_functions = TransitiveReferences(callee); |
| 114 | + for (auto& transitive_func : callee_functions) { |
| 115 | + functions.Add(transitive_func); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // Loop over instructions in the block to find nested blocks. |
| 122 | + for (auto* inst : *next) { |
| 123 | + if (auto* ctrl = inst->template As<ControlInstruction>()) { |
| 124 | + // Add nested blocks to the queue. |
| 125 | + ctrl->ForeachBlock([&](BlockT* blk) { block_queue.Push(blk); }); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return functions; |
| 131 | + }); |
| 132 | + } |
| 133 | + |
| 134 | + private: |
| 135 | + /// A map from blocks to their directly called functions. |
| 136 | + Hashmap<BlockT*, FunctionSet, 64> block_to_direct_calls_{}; |
| 137 | + |
| 138 | + /// A map from functions to their transitively referenced functions. |
| 139 | + Hashmap<FunctionT*, FunctionSet, 8> transitive_references_; |
| 140 | +}; |
| 141 | + |
| 142 | +} // namespace tint::core::ir |
| 143 | + |
| 144 | +#endif // SRC_TINT_LANG_CORE_IR_REFERENCED_FUNCTIONS_H_ |
0 commit comments