|
17 | 17 | #ifndef MLIR_REDUCER_REDUCTIONNODE_H
|
18 | 18 | #define MLIR_REDUCER_REDUCTIONNODE_H
|
19 | 19 |
|
| 20 | +#include <queue> |
20 | 21 | #include <vector>
|
21 | 22 |
|
22 | 23 | #include "mlir/Reducer/Tester.h"
|
| 24 | +#include "llvm/Support/Allocator.h" |
23 | 25 | #include "llvm/Support/ToolOutputFile.h"
|
24 | 26 |
|
25 | 27 | namespace mlir {
|
26 | 28 |
|
27 |
| -/// This class defines the ReductionNode which is used to wrap the module of |
28 |
| -/// a generated variant and keep track of the necessary metadata for the |
29 |
| -/// reduction pass. The nodes are linked together in a reduction tree structure |
30 |
| -/// which defines the relationship between all the different generated variants. |
| 29 | +/// Defines the traversal method options to be used in the reduction tree |
| 30 | +/// traversal. |
| 31 | +enum TraversalMode { SinglePath, Backtrack, MultiPath }; |
| 32 | + |
| 33 | +/// This class defines the ReductionNode which is used to generate variant and |
| 34 | +/// keep track of the necessary metadata for the reduction pass. The nodes are |
| 35 | +/// linked together in a reduction tree structure which defines the relationship |
| 36 | +/// between all the different generated variants. |
31 | 37 | class ReductionNode {
|
32 | 38 | public:
|
33 |
| - ReductionNode(ModuleOp module, ReductionNode *parent); |
34 |
| - |
35 |
| - ReductionNode(ModuleOp module, ReductionNode *parent, |
36 |
| - std::vector<bool> transformSpace); |
| 39 | + template <TraversalMode mode> |
| 40 | + class iterator; |
37 | 41 |
|
38 |
| - /// Calculates and initializes the size and interesting values of the node. |
39 |
| - void measureAndTest(const Tester &test); |
| 42 | + using Range = std::pair<int, int>; |
40 | 43 |
|
41 |
| - /// Returns the module. |
42 |
| - ModuleOp getModule() const { return module; } |
| 44 | + ReductionNode(ReductionNode *parent, std::vector<Range> range, |
| 45 | + llvm::SpecificBumpPtrAllocator<ReductionNode> &allocator); |
43 | 46 |
|
44 |
| - /// Returns true if the size and interestingness have been calculated. |
45 |
| - bool isEvaluated() const; |
| 47 | + ReductionNode *getParent() const; |
46 | 48 |
|
47 |
| - /// Returns the size in bytes of the module. |
48 |
| - int getSize() const; |
| 49 | + size_t getSize() const; |
49 | 50 |
|
50 | 51 | /// Returns true if the module exhibits the interesting behavior.
|
51 |
| - bool isInteresting() const; |
52 |
| - |
53 |
| - /// Returns the pointer to a child variant by index. |
54 |
| - ReductionNode *getVariant(unsigned long index) const; |
| 52 | + Tester::Interestingness isInteresting() const; |
55 | 53 |
|
56 |
| - /// Returns the number of child variants. |
57 |
| - int variantsSize() const; |
| 54 | + std::vector<Range> getRanges() const; |
58 | 55 |
|
59 |
| - /// Returns true if the vector containing the child variants is empty. |
60 |
| - bool variantsEmpty() const; |
| 56 | + std::vector<ReductionNode *> &getVariants(); |
61 | 57 |
|
62 |
| - /// Sort the child variants and remove the uninteresting ones. |
63 |
| - void organizeVariants(const Tester &test); |
| 58 | + /// Split the ranges and generate new variants. |
| 59 | + std::vector<ReductionNode *> generateNewVariants(); |
64 | 60 |
|
65 |
| - /// Returns the number of child variants. |
66 |
| - int transformSpaceSize(); |
67 |
| - |
68 |
| - /// Returns a vector indicating the transformed indices as true. |
69 |
| - const std::vector<bool> getTransformSpace(); |
| 61 | + /// Update the interestingness result from tester. |
| 62 | + void update(std::pair<Tester::Interestingness, size_t> result); |
70 | 63 |
|
71 | 64 | private:
|
72 |
| - /// Link a child variant node. |
73 |
| - void linkVariant(ReductionNode *newVariant); |
74 |
| - |
75 |
| - // This is the MLIR module of this variant. |
76 |
| - ModuleOp module; |
77 |
| - |
78 |
| - // This is true if the module has been evaluated and it exhibits the |
79 |
| - // interesting behavior. |
80 |
| - bool interesting; |
81 |
| - |
82 |
| - // This indicates the number of characters in the printed module if the module |
83 |
| - // has been evaluated. |
84 |
| - int size; |
85 |
| - |
86 |
| - // This indicates if the module has been evaluated (measured and tested). |
87 |
| - bool evaluated; |
88 |
| - |
89 |
| - // Indicates the indices in the node that have been transformed in previous |
90 |
| - // levels of the reduction tree. |
91 |
| - std::vector<bool> transformSpace; |
| 65 | + /// A custom BFS iterator. The difference between |
| 66 | + /// llvm/ADT/BreadthFirstIterator.h is the graph we're exploring is dynamic. |
| 67 | + /// We may explore more neighbors at certain node if we didn't find interested |
| 68 | + /// event. As a result, we defer pushing adjacent nodes until poping the last |
| 69 | + /// visited node. The graph exploration strategy will be put in |
| 70 | + /// getNeighbors(). |
| 71 | + /// |
| 72 | + /// Subclass BaseIterator and implement traversal strategy in getNeighbors(). |
| 73 | + template <typename T> |
| 74 | + class BaseIterator { |
| 75 | + public: |
| 76 | + BaseIterator(ReductionNode *node) { visitQueue.push(node); } |
| 77 | + BaseIterator(const BaseIterator &) = default; |
| 78 | + BaseIterator() = default; |
| 79 | + |
| 80 | + static BaseIterator end() { return BaseIterator(); } |
| 81 | + |
| 82 | + bool operator==(const BaseIterator &i) { |
| 83 | + return visitQueue == i.visitQueue; |
| 84 | + } |
| 85 | + bool operator!=(const BaseIterator &i) { return !(*this == i); } |
| 86 | + |
| 87 | + BaseIterator &operator++() { |
| 88 | + ReductionNode *top = visitQueue.front(); |
| 89 | + visitQueue.pop(); |
| 90 | + std::vector<ReductionNode *> neighbors = getNeighbors(top); |
| 91 | + for (ReductionNode *node : neighbors) |
| 92 | + visitQueue.push(node); |
| 93 | + return *this; |
| 94 | + } |
| 95 | + |
| 96 | + BaseIterator operator++(int) { |
| 97 | + BaseIterator tmp = *this; |
| 98 | + ++*this; |
| 99 | + return tmp; |
| 100 | + } |
| 101 | + |
| 102 | + ReductionNode &operator*() const { return *(visitQueue.front()); } |
| 103 | + ReductionNode *operator->() const { return visitQueue.front(); } |
| 104 | + |
| 105 | + protected: |
| 106 | + std::vector<ReductionNode *> getNeighbors(ReductionNode *node) { |
| 107 | + return static_cast<T *>(this)->getNeighbors(node); |
| 108 | + } |
| 109 | + |
| 110 | + private: |
| 111 | + std::queue<ReductionNode *> visitQueue; |
| 112 | + }; |
| 113 | + |
| 114 | + /// The size of module after applying the range constraints. |
| 115 | + size_t size; |
| 116 | + |
| 117 | + /// This is true if the module has been evaluated and it exhibits the |
| 118 | + /// interesting behavior. |
| 119 | + Tester::Interestingness interesting; |
| 120 | + |
| 121 | + ReductionNode *parent; |
| 122 | + |
| 123 | + /// We will only keep the operation with index falls into the ranges. |
| 124 | + /// For example, number each function in a certain module and then we will |
| 125 | + /// remove the functions with index outside the ranges and see if the |
| 126 | + /// resulting module is still interesting. |
| 127 | + std::vector<Range> ranges; |
| 128 | + |
| 129 | + /// This points to the child variants that were created using this node as a |
| 130 | + /// starting point. |
| 131 | + std::vector<ReductionNode *> variants; |
| 132 | + |
| 133 | + llvm::SpecificBumpPtrAllocator<ReductionNode> &allocator; |
| 134 | +}; |
92 | 135 |
|
93 |
| - // This points to the child variants that were created using this node as a |
94 |
| - // starting point. |
95 |
| - std::vector<std::unique_ptr<ReductionNode>> variants; |
| 136 | +// Specialized iterator for SinglePath traversal |
| 137 | +template <> |
| 138 | +class ReductionNode::iterator<SinglePath> |
| 139 | + : public BaseIterator<iterator<SinglePath>> { |
| 140 | + friend BaseIterator<iterator<SinglePath>>; |
| 141 | + using BaseIterator::BaseIterator; |
| 142 | + std::vector<ReductionNode *> getNeighbors(ReductionNode *node); |
96 | 143 | };
|
97 | 144 |
|
98 | 145 | } // end namespace mlir
|
|
0 commit comments