| 85 | // BufferValue(%tuple_constant, {1, 1}) // Holds value "43" |
| 86 | |
| 87 | class BufferValue { |
| 88 | public: |
| 89 | TF_LIB_GTL_DEFINE_INT_TYPE(Color, int64); |
| 90 | |
| 91 | // Id is a unique identifier for the BufferValue to facilitate efficient |
| 92 | // collections of BufferValues with stable iteration order. |
| 93 | using Id = int64; |
| 94 | |
| 95 | // Functions which return the size and alignment of a logical buffer in bytes. |
| 96 | using SizeFunction = std::function<int64(const BufferValue&)>; |
| 97 | using AlignmentFunction = std::function<int64(BufferValue::Color)>; |
| 98 | |
| 99 | virtual ~BufferValue(); |
| 100 | |
| 101 | Id id() const { return id_; } |
| 102 | |
| 103 | // Return the instruction that defines the buffer. |
| 104 | virtual HloInstruction* instruction() const = 0; |
| 105 | |
| 106 | // Return the index within the output of the instruction where the buffer is |
| 107 | // defined. Index used defined as in ShapeUtil::GetSubshape() |
| 108 | virtual const ShapeIndex& index() const = 0; |
| 109 | |
| 110 | // Return the color of the BufferValue. Differently colored buffers can not be |
| 111 | // parts of the same allocation. |
| 112 | Color color() const { |
| 113 | CHECK_NE(color_, kInvalidColor) |
| 114 | << "Should not query the color of a buffer that was never colored"; |
| 115 | return color_; |
| 116 | } |
| 117 | |
| 118 | void set_color(Color color) { |
| 119 | CHECK_NE(color, kInvalidColor) |
| 120 | << "Should not set the color of a buffer to the invalid color"; |
| 121 | color_ = color; |
| 122 | } |
| 123 | |
| 124 | bool has_color() const { return color_ != kInvalidColor; } |
| 125 | |
| 126 | // Return the shape of the buffer. This reference points into the shape field |
| 127 | // of the instruction defining the buffer. Therefore, the returned shape will |
| 128 | // contain the layout of instruction, if any. |
| 129 | virtual const Shape& shape() const = 0; |
| 130 | |
| 131 | // Returns true if this buffer is the top-level output buffer of the defining |
| 132 | // HLO instruction. This is equivalent to index == {}. |
| 133 | bool IsTopLevel() const { return index().empty(); } |
| 134 | |
| 135 | // Whether this buffer contains a tuple. |
| 136 | bool IsTuple() const { return is_tuple_; } |
| 137 | |
| 138 | // Whether this buffer contains an array. |
| 139 | bool IsArray() const { return is_array_; } |
| 140 | |
| 141 | // operator< is required for std::set. |
| 142 | bool operator<(const BufferValue& other) const { return id_ < other.id_; } |
| 143 | |
| 144 | bool operator==(const BufferValue& other) const { return id_ == other.id_; } |