1 //===- ReductionNode.h - Reduction Node Implementation ----------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the reduction nodes which are used to track of the metadata 10 // for a specific generated variant within a reduction pass and are the building 11 // blocks of the reduction tree structure. A reduction tree is used to keep 12 // track of the different generated variants throughout a reduction pass in the 13 // MLIR Reduce tool. 14 // 15 //===----------------------------------------------------------------------===// 16 17 #ifndef MLIR_REDUCER_REDUCTIONNODE_H 18 #define MLIR_REDUCER_REDUCTIONNODE_H 19 20 #include <vector> 21 22 #include "mlir/Reducer/Tester.h" 23 #include "llvm/Support/ToolOutputFile.h" 24 25 namespace mlir { 26 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. 31 class ReductionNode { 32 public: 33 ReductionNode(ModuleOp module, ReductionNode *parent); 34 35 ReductionNode(ModuleOp module, ReductionNode *parent, 36 std::vector<bool> transformSpace); 37 38 /// Calculates and initializes the size and interesting values of the node. 39 void measureAndTest(const Tester &test); 40 41 /// Returns the module. getModule()42 ModuleOp getModule() const { return module; } 43 44 /// Returns true if the size and interestingness have been calculated. 45 bool isEvaluated() const; 46 47 /// Returns the size in bytes of the module. 48 int getSize() const; 49 50 /// 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; 55 56 /// Returns the number of child variants. 57 int variantsSize() const; 58 59 /// Returns true if the vector containing the child variants is empty. 60 bool variantsEmpty() const; 61 62 /// Sort the child variants and remove the uninteresting ones. 63 void organizeVariants(const Tester &test); 64 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(); 70 71 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; 92 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; 96 }; 97 98 } // end namespace mlir 99 100 #endif 101