|
| 1 | +#ifndef LIB_UTILS_ARITHMETICDAG_H_ |
| 2 | +#define LIB_UTILS_ARITHMETICDAG_H_ |
| 3 | + |
| 4 | +#include <cassert> |
| 5 | +#include <cstddef> |
| 6 | +#include <memory> |
| 7 | +#include <unordered_map> |
| 8 | +#include <utility> |
| 9 | +#include <variant> |
| 10 | + |
| 11 | +namespace mlir { |
| 12 | +namespace heir { |
| 13 | + |
| 14 | +// This file contains a generic DAG structure that can be used for representing |
| 15 | +// arithmetic DAGs with leaf nodes of various types. |
| 16 | +template <typename T> |
| 17 | +struct ArithmeticDagNode; |
| 18 | + |
| 19 | +// A leaf node for the DAG |
| 20 | +template <typename T> |
| 21 | +struct LeafNode { |
| 22 | + T value; |
| 23 | +}; |
| 24 | + |
| 25 | +struct ConstantNode { |
| 26 | + double value; |
| 27 | +}; |
| 28 | + |
| 29 | +template <typename T> |
| 30 | +struct AddNode { |
| 31 | + std::shared_ptr<ArithmeticDagNode<T>> left; |
| 32 | + std::shared_ptr<ArithmeticDagNode<T>> right; |
| 33 | +}; |
| 34 | + |
| 35 | +template <typename T> |
| 36 | +struct MultiplyNode { |
| 37 | + std::shared_ptr<ArithmeticDagNode<T>> left; |
| 38 | + std::shared_ptr<ArithmeticDagNode<T>> right; |
| 39 | +}; |
| 40 | + |
| 41 | +template <typename T> |
| 42 | +struct PowerNode { |
| 43 | + std::shared_ptr<ArithmeticDagNode<T>> base; |
| 44 | + size_t exponent; |
| 45 | +}; |
| 46 | + |
| 47 | +template <typename T> |
| 48 | +struct ArithmeticDagNode { |
| 49 | + public: |
| 50 | + std::variant<ConstantNode, LeafNode<T>, AddNode<T>, MultiplyNode<T>, |
| 51 | + PowerNode<T>> |
| 52 | + node_variant; |
| 53 | + |
| 54 | + explicit ArithmeticDagNode(const T& value) |
| 55 | + : node_variant(LeafNode<T>{value}) {} |
| 56 | + explicit ArithmeticDagNode(T&& value) |
| 57 | + : node_variant(LeafNode<T>{std::move(value)}) {} |
| 58 | + |
| 59 | + private: |
| 60 | + ArithmeticDagNode() = default; |
| 61 | + |
| 62 | + public: |
| 63 | + // Static factory methods |
| 64 | + static std::shared_ptr<ArithmeticDagNode<T>> leaf(const T& value) { |
| 65 | + // This factory method differs from the others because T may not have a |
| 66 | + // default constructor to use with emplace. In that case, we need to rely |
| 67 | + // on the move or copy constructors, which corresponds to the two |
| 68 | + // ArithmeticDagNode constructors above. |
| 69 | + return std::shared_ptr<ArithmeticDagNode<T>>( |
| 70 | + new ArithmeticDagNode<T>(value)); |
| 71 | + } |
| 72 | + |
| 73 | + static std::shared_ptr<ArithmeticDagNode<T>> constant(double constant) { |
| 74 | + auto node = |
| 75 | + std::shared_ptr<ArithmeticDagNode<T>>(new ArithmeticDagNode<T>()); |
| 76 | + // Note, to satisfy variant we need to use aggregate initialization inside |
| 77 | + // emplace |
| 78 | + node->node_variant.template emplace<ConstantNode>(ConstantNode{constant}); |
| 79 | + return node; |
| 80 | + } |
| 81 | + |
| 82 | + static std::shared_ptr<ArithmeticDagNode<T>> add( |
| 83 | + std::shared_ptr<ArithmeticDagNode<T>> lhs, |
| 84 | + std::shared_ptr<ArithmeticDagNode<T>> rhs) { |
| 85 | + assert(lhs && rhs && "invalid add"); |
| 86 | + auto node = |
| 87 | + std::shared_ptr<ArithmeticDagNode<T>>(new ArithmeticDagNode<T>()); |
| 88 | + node->node_variant.template emplace<AddNode<T>>( |
| 89 | + AddNode<T>{std::move(lhs), std::move(rhs)}); |
| 90 | + return node; |
| 91 | + } |
| 92 | + |
| 93 | + static std::shared_ptr<ArithmeticDagNode<T>> mul( |
| 94 | + std::shared_ptr<ArithmeticDagNode<T>> lhs, |
| 95 | + std::shared_ptr<ArithmeticDagNode<T>> rhs) { |
| 96 | + assert(lhs && rhs && "invalid mul"); |
| 97 | + auto node = |
| 98 | + std::shared_ptr<ArithmeticDagNode<T>>(new ArithmeticDagNode<T>()); |
| 99 | + node->node_variant.template emplace<MultiplyNode<T>>( |
| 100 | + MultiplyNode<T>{std::move(lhs), std::move(rhs)}); |
| 101 | + return node; |
| 102 | + } |
| 103 | + |
| 104 | + static std::shared_ptr<ArithmeticDagNode<T>> power( |
| 105 | + std::shared_ptr<ArithmeticDagNode<T>> base, size_t exponent) { |
| 106 | + assert(base && "invalid base for power"); |
| 107 | + auto node = |
| 108 | + std::shared_ptr<ArithmeticDagNode<T>>(new ArithmeticDagNode<T>()); |
| 109 | + node->node_variant.template emplace<PowerNode<T>>( |
| 110 | + PowerNode<T>{std::move(base), exponent}); |
| 111 | + return node; |
| 112 | + } |
| 113 | + |
| 114 | + ArithmeticDagNode(const ArithmeticDagNode&) = default; |
| 115 | + ArithmeticDagNode& operator=(const ArithmeticDagNode&) = default; |
| 116 | + ArithmeticDagNode(ArithmeticDagNode&&) noexcept = default; |
| 117 | + ArithmeticDagNode& operator=(ArithmeticDagNode&&) noexcept = default; |
| 118 | + |
| 119 | + // Visitor pattern |
| 120 | + template <typename VisitorFunc> |
| 121 | + decltype(auto) visit(VisitorFunc&& visitor) { |
| 122 | + return std::visit(std::forward<VisitorFunc>(visitor), node_variant); |
| 123 | + } |
| 124 | + |
| 125 | + template <typename VisitorFunc> |
| 126 | + decltype(auto) visit(VisitorFunc&& visitor) const { |
| 127 | + return std::visit(std::forward<VisitorFunc>(visitor), node_variant); |
| 128 | + } |
| 129 | +}; |
| 130 | + |
| 131 | +/// A base class for visitors that caches intermediate results. |
| 132 | +/// |
| 133 | +/// Template parameters: |
| 134 | +/// T: The type of the leaf nodes. |
| 135 | +/// ResultType: The type of the result of the visit. |
| 136 | +template <typename T, typename ResultType> |
| 137 | +class CachingVisitor { |
| 138 | + public: |
| 139 | + virtual ~CachingVisitor() = default; |
| 140 | + |
| 141 | + /// The main entry point that contains the caching logic. |
| 142 | + ResultType process(const std::shared_ptr<ArithmeticDagNode<T>>& node) { |
| 143 | + assert(node != nullptr && "invalid null node!"); |
| 144 | + |
| 145 | + const auto* node_ptr = node.get(); |
| 146 | + if (auto it = cache.find(node_ptr); it != cache.end()) { |
| 147 | + return it->second; |
| 148 | + } |
| 149 | + |
| 150 | + ResultType result = std::visit(*this, node->node_variant); |
| 151 | + cache[node_ptr] = result; |
| 152 | + return result; |
| 153 | + } |
| 154 | + |
| 155 | + // --- Virtual Visit Methods --- |
| 156 | + // Derived classes must override these for the node types they support. |
| 157 | + |
| 158 | + virtual ResultType operator()(const ConstantNode& node) { |
| 159 | + assert(false && "Visit logic for ConstantNode is not implemented."); |
| 160 | + } |
| 161 | + |
| 162 | + virtual ResultType operator()(const LeafNode<T>& node) { |
| 163 | + assert(false && "Visit logic for LeafNode is not implemented."); |
| 164 | + } |
| 165 | + |
| 166 | + virtual ResultType operator()(const AddNode<T>& node) { |
| 167 | + assert(false && "Visit logic for AddNode is not implemented."); |
| 168 | + } |
| 169 | + |
| 170 | + virtual ResultType operator()(const MultiplyNode<T>& node) { |
| 171 | + assert(false && "Visit logic for MultiplyNode is not implemented."); |
| 172 | + } |
| 173 | + |
| 174 | + virtual ResultType operator()(const PowerNode<T>& node) { |
| 175 | + assert(false && "Visit logic for PowerNode is not implemented."); |
| 176 | + } |
| 177 | + |
| 178 | + private: |
| 179 | + std::unordered_map<const ArithmeticDagNode<T>*, ResultType> cache; |
| 180 | +}; |
| 181 | + |
| 182 | +} // namespace heir |
| 183 | +} // namespace mlir |
| 184 | + |
| 185 | +#endif // LIB_UTILS_ARITHMETICDAG_H_ |
0 commit comments