dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 1 | // Copyright 2022 The Tint Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #ifndef SRC_TINT_CONSTANT_COMPOSITE_H_ |
| 16 | #define SRC_TINT_CONSTANT_COMPOSITE_H_ |
| 17 | |
dan sinclair | b53b8cf | 2022-12-15 16:25:31 +0000 | [diff] [blame] | 18 | #include "src/tint/constant/value.h" |
dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 19 | #include "src/tint/number.h" |
| 20 | #include "src/tint/type/type.h" |
dan sinclair | 12fa303 | 2023-04-19 23:52:33 +0000 | [diff] [blame] | 21 | #include "src/tint/utils/castable.h" |
dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 22 | #include "src/tint/utils/hash.h" |
| 23 | #include "src/tint/utils/vector.h" |
| 24 | |
dan sinclair | 8626c9e | 2022-12-14 19:22:19 +0000 | [diff] [blame] | 25 | namespace tint::constant { |
dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 26 | |
dan sinclair | b53b8cf | 2022-12-15 16:25:31 +0000 | [diff] [blame] | 27 | /// Composite holds a number of mixed child values. |
Ben Clayton | 574b4b1 | 2023-03-09 23:22:27 +0000 | [diff] [blame] | 28 | /// Composite may be of a vector, matrix, array or structure type. |
dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 29 | /// If each element is the same type and value, then a Splat would be a more efficient constant |
dan sinclair | b53b8cf | 2022-12-15 16:25:31 +0000 | [diff] [blame] | 30 | /// implementation. Use CreateComposite() to create the appropriate type. |
dan sinclair | 12fa303 | 2023-04-19 23:52:33 +0000 | [diff] [blame] | 31 | class Composite : public utils::Castable<Composite, Value> { |
dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 32 | public: |
| 33 | /// Constructor |
| 34 | /// @param t the compsite type |
| 35 | /// @param els the composite elements |
| 36 | /// @param all_0 true if all elements are 0 |
| 37 | /// @param any_0 true if any element is 0 |
Ben Clayton | 574b4b1 | 2023-03-09 23:22:27 +0000 | [diff] [blame] | 38 | Composite(const type::Type* t, utils::VectorRef<const Value*> els, bool all_0, bool any_0); |
dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 39 | ~Composite() override; |
| 40 | |
Ben Clayton | 574b4b1 | 2023-03-09 23:22:27 +0000 | [diff] [blame] | 41 | /// @copydoc Value::Type() |
dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 42 | const type::Type* Type() const override { return type; } |
| 43 | |
Ben Clayton | 574b4b1 | 2023-03-09 23:22:27 +0000 | [diff] [blame] | 44 | /// @copydoc Value::Index() |
| 45 | const Value* Index(size_t i) const override { |
dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 46 | return i < elements.Length() ? elements[i] : nullptr; |
| 47 | } |
| 48 | |
Ben Clayton | 574b4b1 | 2023-03-09 23:22:27 +0000 | [diff] [blame] | 49 | /// @copydoc Value::NumElements() |
| 50 | size_t NumElements() const override { return elements.Length(); } |
| 51 | |
| 52 | /// @copydoc Value::AllZero() |
dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 53 | bool AllZero() const override { return all_zero; } |
Ben Clayton | 574b4b1 | 2023-03-09 23:22:27 +0000 | [diff] [blame] | 54 | |
| 55 | /// @copydoc Value::AnyZero() |
dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 56 | bool AnyZero() const override { return any_zero; } |
Ben Clayton | 574b4b1 | 2023-03-09 23:22:27 +0000 | [diff] [blame] | 57 | |
Ben Clayton | 574b4b1 | 2023-03-09 23:22:27 +0000 | [diff] [blame] | 58 | /// @copydoc Value::Hash() |
dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 59 | size_t Hash() const override { return hash; } |
| 60 | |
dan sinclair | 529c3fd | 2023-01-06 02:57:36 +0000 | [diff] [blame] | 61 | /// Clones the constant into the provided context |
| 62 | /// @param ctx the clone context |
| 63 | /// @returns the cloned node |
| 64 | Composite* Clone(CloneContext& ctx) const override; |
| 65 | |
dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 66 | /// The composite type |
| 67 | type::Type const* const type; |
| 68 | /// The composite elements |
Ben Clayton | 574b4b1 | 2023-03-09 23:22:27 +0000 | [diff] [blame] | 69 | const utils::Vector<const Value*, 4> elements; |
dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 70 | /// True if all elements are zero |
| 71 | const bool all_zero; |
| 72 | /// True if any element is zero |
| 73 | const bool any_zero; |
| 74 | /// The hash of the composite |
| 75 | const size_t hash; |
| 76 | |
dan sinclair | b53b8cf | 2022-12-15 16:25:31 +0000 | [diff] [blame] | 77 | protected: |
Ben Clayton | 574b4b1 | 2023-03-09 23:22:27 +0000 | [diff] [blame] | 78 | /// @copydoc Value::InternalValue() |
dan sinclair | b53b8cf | 2022-12-15 16:25:31 +0000 | [diff] [blame] | 79 | std::variant<std::monostate, AInt, AFloat> InternalValue() const override { return {}; } |
| 80 | |
dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 81 | private: |
| 82 | size_t CalcHash() { |
| 83 | auto h = utils::Hash(type, all_zero, any_zero); |
| 84 | for (auto* el : elements) { |
| 85 | h = utils::HashCombine(h, el->Hash()); |
| 86 | } |
| 87 | return h; |
| 88 | } |
| 89 | }; |
| 90 | |
dan sinclair | 8626c9e | 2022-12-14 19:22:19 +0000 | [diff] [blame] | 91 | } // namespace tint::constant |
dan sinclair | 0890380 | 2022-12-14 18:13:37 +0000 | [diff] [blame] | 92 | |
| 93 | #endif // SRC_TINT_CONSTANT_COMPOSITE_H_ |