Have DeepestElementOf always return a type.

Currently deepest element only returns the type if it was a scalar
or the element of a vector, array or matrix. Otherwise it would
return `nullptr`. There are cases where we want to get the deepest
type which is a struct or some other type.

This Cl updates ElementOf to return the type instead of nullptr for
types which previously returned nullptr.

Change-Id: I7963d4ce55d2e2b1a537a7533fa332813eed035c
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/103900
Reviewed-by: Ben Clayton <bclayton@google.com>
Commit-Queue: Dan Sinclair <dsinclair@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
dan sinclair 2022-09-27 19:20:06 +00:00 committed by Dawn LUCI CQ
parent b634dca6d6
commit 764a2abc90
3 changed files with 13 additions and 15 deletions

View File

@ -254,9 +254,9 @@ const Type* Type::ElementOf(const Type* ty, uint32_t* count /* = nullptr */) {
}, },
[&](Default) { [&](Default) {
if (count) { if (count) {
*count = 0; *count = 1;
} }
return nullptr; return ty;
}); });
} }

View File

@ -140,21 +140,19 @@ class Type : public Castable<Type, Node> {
/// @param count if not null, then this is assigned the number of child elements in the type. /// @param count if not null, then this is assigned the number of child elements in the type.
/// For example, the count of an `array<vec3<f32>, 5>` type would be 5. /// For example, the count of an `array<vec3<f32>, 5>` type would be 5.
/// @returns /// @returns
/// * `ty` if `ty` is an abstract or scalar
/// * the element type if `ty` is a vector or array /// * the element type if `ty` is a vector or array
/// * the column type if `ty` is a matrix /// * the column type if `ty` is a matrix
/// * `nullptr` if `ty` is none of the above /// * `ty` if `ty` is none of the above
static const Type* ElementOf(const Type* ty, uint32_t* count = nullptr); static const Type* ElementOf(const Type* ty, uint32_t* count = nullptr);
/// @param ty the type to obtain the deepest element type from /// @param ty the type to obtain the deepest element type from
/// @param count if not null, then this is assigned the full number of most deeply nested /// @param count if not null, then this is assigned the full number of most deeply nested
/// elements in the type. For example, the count of an `array<vec3<f32>, 5>` type would be 15. /// elements in the type. For example, the count of an `array<vec3<f32>, 5>` type would be 15.
/// @returns /// @returns
/// * `ty` if `ty` is an abstract or scalar
/// * the element type if `ty` is a vector /// * the element type if `ty` is a vector
/// * the matrix element type if `ty` is a matrix /// * the matrix element type if `ty` is a matrix
/// * the deepest element type if `ty` is an array /// * the deepest element type if `ty` is an array
/// * `nullptr` if `ty` is none of the above /// * `ty` if `ty` is none of the above
static const Type* DeepestElementOf(const Type* ty, uint32_t* count = nullptr); static const Type* DeepestElementOf(const Type* ty, uint32_t* count = nullptr);
/// @param types the list of types /// @param types the list of types

View File

@ -192,7 +192,7 @@ TEST_F(TypeTest, ElementOf) {
EXPECT_TYPE(Type::ElementOf(mat2x4_f32), vec4_f32); EXPECT_TYPE(Type::ElementOf(mat2x4_f32), vec4_f32);
EXPECT_TYPE(Type::ElementOf(mat4x2_f32), vec2_f32); EXPECT_TYPE(Type::ElementOf(mat4x2_f32), vec2_f32);
EXPECT_TYPE(Type::ElementOf(mat4x3_f16), vec3_f16); EXPECT_TYPE(Type::ElementOf(mat4x3_f16), vec3_f16);
EXPECT_TYPE(Type::ElementOf(str), nullptr); EXPECT_TYPE(Type::ElementOf(str), str);
EXPECT_TYPE(Type::ElementOf(arr_i32), i32); EXPECT_TYPE(Type::ElementOf(arr_i32), i32);
EXPECT_TYPE(Type::ElementOf(arr_vec3_i32), vec3_i32); EXPECT_TYPE(Type::ElementOf(arr_vec3_i32), vec3_i32);
EXPECT_TYPE(Type::ElementOf(arr_mat4x3_f16), mat4x3_f16); EXPECT_TYPE(Type::ElementOf(arr_mat4x3_f16), mat4x3_f16);
@ -237,8 +237,8 @@ TEST_F(TypeTest, ElementOf) {
EXPECT_TYPE(Type::ElementOf(mat4x3_f16, &count), vec3_f16); EXPECT_TYPE(Type::ElementOf(mat4x3_f16, &count), vec3_f16);
EXPECT_EQ(count, 4u); EXPECT_EQ(count, 4u);
count = 42; count = 42;
EXPECT_TYPE(Type::ElementOf(str, &count), nullptr); EXPECT_TYPE(Type::ElementOf(str, &count), str);
EXPECT_EQ(count, 0u); EXPECT_EQ(count, 1u);
count = 42; count = 42;
EXPECT_TYPE(Type::ElementOf(arr_i32, &count), i32); EXPECT_TYPE(Type::ElementOf(arr_i32, &count), i32);
EXPECT_EQ(count, 5u); EXPECT_EQ(count, 5u);
@ -270,12 +270,12 @@ TEST_F(TypeTest, DeepestElementOf) {
EXPECT_TYPE(Type::DeepestElementOf(mat2x4_f32), f32); EXPECT_TYPE(Type::DeepestElementOf(mat2x4_f32), f32);
EXPECT_TYPE(Type::DeepestElementOf(mat4x2_f32), f32); EXPECT_TYPE(Type::DeepestElementOf(mat4x2_f32), f32);
EXPECT_TYPE(Type::DeepestElementOf(mat4x3_f16), f16); EXPECT_TYPE(Type::DeepestElementOf(mat4x3_f16), f16);
EXPECT_TYPE(Type::DeepestElementOf(str), nullptr); EXPECT_TYPE(Type::DeepestElementOf(str), str);
EXPECT_TYPE(Type::DeepestElementOf(arr_i32), i32); EXPECT_TYPE(Type::DeepestElementOf(arr_i32), i32);
EXPECT_TYPE(Type::DeepestElementOf(arr_vec3_i32), i32); EXPECT_TYPE(Type::DeepestElementOf(arr_vec3_i32), i32);
EXPECT_TYPE(Type::DeepestElementOf(arr_mat4x3_f16), f16); EXPECT_TYPE(Type::DeepestElementOf(arr_mat4x3_f16), f16);
EXPECT_TYPE(Type::DeepestElementOf(arr_mat4x3_af), af); EXPECT_TYPE(Type::DeepestElementOf(arr_mat4x3_af), af);
EXPECT_TYPE(Type::DeepestElementOf(arr_str), nullptr); EXPECT_TYPE(Type::DeepestElementOf(arr_str), str);
// With count // With count
uint32_t count = 42; uint32_t count = 42;
@ -315,8 +315,8 @@ TEST_F(TypeTest, DeepestElementOf) {
EXPECT_TYPE(Type::DeepestElementOf(mat4x3_f16, &count), f16); EXPECT_TYPE(Type::DeepestElementOf(mat4x3_f16, &count), f16);
EXPECT_EQ(count, 12u); EXPECT_EQ(count, 12u);
count = 42; count = 42;
EXPECT_TYPE(Type::DeepestElementOf(str, &count), nullptr); EXPECT_TYPE(Type::DeepestElementOf(str, &count), str);
EXPECT_EQ(count, 0u); EXPECT_EQ(count, 1u);
count = 42; count = 42;
EXPECT_TYPE(Type::DeepestElementOf(arr_i32, &count), i32); EXPECT_TYPE(Type::DeepestElementOf(arr_i32, &count), i32);
EXPECT_EQ(count, 5u); EXPECT_EQ(count, 5u);
@ -330,8 +330,8 @@ TEST_F(TypeTest, DeepestElementOf) {
EXPECT_TYPE(Type::DeepestElementOf(arr_mat4x3_af, &count), af); EXPECT_TYPE(Type::DeepestElementOf(arr_mat4x3_af, &count), af);
EXPECT_EQ(count, 60u); EXPECT_EQ(count, 60u);
count = 42; count = 42;
EXPECT_TYPE(Type::DeepestElementOf(arr_str, &count), nullptr); EXPECT_TYPE(Type::DeepestElementOf(arr_str, &count), str);
EXPECT_EQ(count, 0u); EXPECT_EQ(count, 5u);
} }
TEST_F(TypeTest, Common2) { TEST_F(TypeTest, Common2) {