Move array to type/

This CL moves array to the type/ folder. Namespaces are updated as
needed. A FriendlyName method was added to ArrayCount so the sem::
ArrayCount entries do not need to be referenced inside type/.

Bug: tint:1718
Change-Id: I16a8f32b3fab1131b284a6981a5c386081138b08
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/113427
Commit-Queue: Dan Sinclair <dsinclair@chromium.org>
Reviewed-by: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
dan sinclair
2022-12-08 22:21:24 +00:00
committed by Dan Sinclair
parent f53b2b5b2e
commit 946858ad56
55 changed files with 260 additions and 226 deletions

View File

@@ -141,14 +141,14 @@ struct ArrayLengthFromUniform::State {
// array_stride
const ast::Expression* total_size = total_storage_buffer_size;
auto* storage_buffer_type = storage_buffer_sem->Type()->UnwrapRef();
const sem::Array* array_type = nullptr;
const type::Array* array_type = nullptr;
if (auto* str = storage_buffer_type->As<sem::Struct>()) {
// The variable is a struct, so subtract the byte offset of the array
// member.
auto* array_member_sem = str->Members().Back();
array_type = array_member_sem->Type()->As<sem::Array>();
array_type = array_member_sem->Type()->As<type::Array>();
total_size = b.Sub(total_storage_buffer_size, u32(array_member_sem->Offset()));
} else if (auto* arr = storage_buffer_type->As<sem::Array>()) {
} else if (auto* arr = storage_buffer_type->As<type::Array>()) {
array_type = arr;
} else {
TINT_ICE(Transform, b.Diagnostics())

View File

@@ -202,16 +202,16 @@ Transform::ApplyResult CalculateArrayLength::Apply(const Program* src,
const ast::Expression* total_size =
b.Expr(buffer_size_result->variable);
const sem::Array* array_type = Switch(
const type::Array* array_type = Switch(
storage_buffer_type->StoreType(),
[&](const sem::Struct* str) {
// The variable is a struct, so subtract the byte offset of
// the array member.
auto* array_member_sem = str->Members().Back();
total_size = b.Sub(total_size, u32(array_member_sem->Offset()));
return array_member_sem->Type()->As<sem::Array>();
return array_member_sem->Type()->As<type::Array>();
},
[&](const sem::Array* arr) { return arr; });
[&](const type::Array* arr) { return arr; });
if (!array_type) {
TINT_ICE(Transform, b.Diagnostics())

View File

@@ -26,12 +26,12 @@
#include "src/tint/ast/type_name.h"
#include "src/tint/ast/unary_op.h"
#include "src/tint/program_builder.h"
#include "src/tint/sem/array.h"
#include "src/tint/sem/call.h"
#include "src/tint/sem/member_accessor_expression.h"
#include "src/tint/sem/statement.h"
#include "src/tint/sem/struct.h"
#include "src/tint/sem/variable.h"
#include "src/tint/type/array.h"
#include "src/tint/type/atomic.h"
#include "src/tint/type/reference.h"
#include "src/tint/utils/block_allocator.h"
@@ -490,7 +490,7 @@ struct DecomposeMemoryAccess::State {
},
utils::Empty);
b.AST().AddFunction(func);
} else if (auto* arr_ty = el_ty->As<sem::Array>()) {
} else if (auto* arr_ty = el_ty->As<type::Array>()) {
// fn load_func(buffer : buf_ty, offset : u32) -> array<T, N> {
// var arr : array<T, N>;
// for (var i = 0u; i < array_count; i = i + 1) {
@@ -592,7 +592,7 @@ struct DecomposeMemoryAccess::State {
} else {
auto body = Switch<utils::Vector<const ast::Statement*, 8>>(
el_ty, //
[&](const sem::Array* arr_ty) {
[&](const type::Array* arr_ty) {
// fn store_func(buffer : buf_ty, offset : u32, value : el_ty) {
// var array = value; // No dynamic indexing on constant arrays
// for (var i = 0u; i < array_count; i = i + 1) {
@@ -928,7 +928,7 @@ Transform::ApplyResult DecomposeMemoryAccess::Apply(const Program* src,
if (auto* accessor = node->As<ast::IndexAccessorExpression>()) {
if (auto access = state.TakeAccess(accessor->object)) {
// X[Y]
if (auto* arr = access.type->As<sem::Array>()) {
if (auto* arr = access.type->As<type::Array>()) {
auto* offset = state.Mul(arr->Stride(), accessor->index);
state.AddAccess(accessor, {
access.var,

View File

@@ -32,7 +32,7 @@ TINT_INSTANTIATE_TYPEINFO(tint::transform::DecomposeStridedArray);
namespace tint::transform {
namespace {
using DecomposedArrays = std::unordered_map<const sem::Array*, Symbol>;
using DecomposedArrays = std::unordered_map<const type::Array*, Symbol>;
bool ShouldRun(const Program* program) {
for (auto* node : program->ASTNodes().Objects()) {
@@ -66,7 +66,7 @@ Transform::ApplyResult DecomposeStridedArray::Apply(const Program* src,
// Maps an array type in the source program to the name of the struct wrapper
// type in the target program.
std::unordered_map<const sem::Array*, Symbol> decomposed;
std::unordered_map<const type::Array*, Symbol> decomposed;
// Find and replace all arrays with a @stride attribute with a array that has
// the @stride removed. If the source array stride does not match the natural
@@ -105,7 +105,7 @@ Transform::ApplyResult DecomposeStridedArray::Apply(const Program* src,
// Example: `arr[i]` -> `arr[i].el`
ctx.ReplaceAll([&](const ast::IndexAccessorExpression* idx) -> const ast::Expression* {
if (auto* ty = src->TypeOf(idx->object)) {
if (auto* arr = ty->UnwrapRef()->As<sem::Array>()) {
if (auto* arr = ty->UnwrapRef()->As<type::Array>()) {
if (!arr->IsStrideImplicit()) {
auto* expr = ctx.CloneWithoutTransform(idx);
return b.MemberAccessor(expr, kMemberName);
@@ -127,7 +127,7 @@ Transform::ApplyResult DecomposeStridedArray::Apply(const Program* src,
if (!expr->args.IsEmpty()) {
if (auto* call = sem.Get(expr)->UnwrapMaterialize()->As<sem::Call>()) {
if (auto* ctor = call->Target()->As<sem::TypeInitializer>()) {
if (auto* arr = ctor->ReturnType()->As<sem::Array>()) {
if (auto* arr = ctor->ReturnType()->As<type::Array>()) {
// Begin by cloning the array initializer type or name
// If this is an unaliased array, this may add a new entry to
// decomposed.

View File

@@ -157,7 +157,7 @@ struct LocalizeStructArrayAssignment::State {
// Indexing a member access expr?
if (auto* ma = ia->object->As<ast::MemberAccessorExpression>()) {
// That accesses an array?
if (src->TypeOf(ma)->UnwrapRef()->Is<sem::Array>()) {
if (src->TypeOf(ma)->UnwrapRef()->Is<type::Array>()) {
result = true;
return ast::TraverseAction::Stop;
}

View File

@@ -52,7 +52,7 @@ bool ContainsMatrix(const type::Type* type) {
type = type->UnwrapRef();
if (type->Is<type::Matrix>()) {
return true;
} else if (auto* ary = type->As<sem::Array>()) {
} else if (auto* ary = type->As<type::Array>()) {
return ContainsMatrix(ary->ElemType());
} else if (auto* str = type->As<sem::Struct>()) {
for (auto* member : str->Members()) {
@@ -95,7 +95,7 @@ struct ModuleScopeVarToEntryPointParam::State {
auto* ast_str = str->Declaration();
ctx.dst->AST().AddTypeDecl(ctx.Clone(ast_str));
ctx.Remove(ctx.src->AST().GlobalDeclarations(), ast_str);
} else if (auto* arr = ty->As<sem::Array>()) {
} else if (auto* arr = ty->As<type::Array>()) {
CloneStructTypes(arr->ElemType());
}
}
@@ -146,7 +146,7 @@ struct ModuleScopeVarToEntryPointParam::State {
attributes.Push(ctx.dst->Disable(ast::DisabledValidation::kIgnoreAddressSpace));
auto* param_type = store_type();
if (auto* arr = ty->As<sem::Array>();
if (auto* arr = ty->As<type::Array>();
arr && arr->Count()->Is<type::RuntimeArrayCount>()) {
// Wrap runtime-sized arrays in structures, so that we can declare pointers to
// them. Ideally we'd just emit the array itself as a pointer, but this is not

View File

@@ -83,7 +83,7 @@ Transform::ApplyResult PadStructs::Apply(const Program* src, const DataMap&, Dat
if (ty->Is<sem::Struct>() && str->UsedAs(ast::AddressSpace::kUniform)) {
// std140 structs should be padded out to 16 bytes.
size = utils::RoundUp(16u, size);
} else if (auto* array_ty = ty->As<sem::Array>()) {
} else if (auto* array_ty = ty->As<type::Array>()) {
if (array_ty->Count()->Is<type::RuntimeArrayCount>()) {
has_runtime_sized_array = true;
}

View File

@@ -132,7 +132,7 @@ struct PreservePadding::State {
return Switch(
ty, //
[&](const sem::Array* arr) {
[&](const type::Array* arr) {
// Call a helper function that uses a loop to assigns each element separately.
return call_helper([&]() {
utils::Vector<const ast::Statement*, 8> body;
@@ -171,7 +171,7 @@ struct PreservePadding::State {
bool HasPadding(const type::Type* ty) {
return Switch(
ty, //
[&](const sem::Array* arr) {
[&](const type::Array* arr) {
auto* elem_ty = arr->ElemType();
if (elem_ty->Size() % elem_ty->Align() > 0) {
return true;

View File

@@ -64,7 +64,7 @@ Transform::ApplyResult PromoteInitializersToLet::Apply(const Program* src,
}
auto* src_ty = expr->Type();
if (!src_ty->IsAnyOf<sem::Array, sem::Struct>()) {
if (!src_ty->IsAnyOf<type::Array, sem::Struct>()) {
// We only care about array and struct initializers
return true;
}

View File

@@ -104,7 +104,7 @@ struct Robustness::State {
return b.Call("min", idx(), u32(mat->columns() - 1u));
},
[&](const sem::Array* arr) -> const ast::Expression* {
[&](const type::Array* arr) -> const ast::Expression* {
const ast::Expression* max = nullptr;
if (arr->Count()->Is<type::RuntimeArrayCount>()) {
// Size is unknown until runtime.
@@ -122,7 +122,7 @@ struct Robustness::State {
// Note: Don't be tempted to use the array override variable as an expression
// here, the name might be shadowed!
b.Diagnostics().add_error(diag::System::Transform,
sem::Array::kErrExpectedConstantCount);
type::Array::kErrExpectedConstantCount);
return nullptr;
}

View File

@@ -70,7 +70,7 @@ Transform::ApplyResult SingleEntryPoint::Apply(const Program* src,
decl, //
[&](const ast::TypeDecl* ty) {
// Strip aliases that reference unused override declarations.
if (auto* arr = sem.Get(ty)->As<sem::Array>()) {
if (auto* arr = sem.Get(ty)->As<type::Array>()) {
auto* refs = sem.TransitivelyReferencedOverrides(arr);
if (refs) {
for (auto* o : *refs) {

View File

@@ -198,7 +198,7 @@ struct SpirvAtomic::State {
[&](const type::I32*) { return b.ty.atomic(CreateASTTypeFor(ctx, ty)); },
[&](const type::U32*) { return b.ty.atomic(CreateASTTypeFor(ctx, ty)); },
[&](const sem::Struct* str) { return b.ty.type_name(Fork(str->Declaration()).name); },
[&](const sem::Array* arr) -> const ast::Type* {
[&](const type::Array* arr) -> const ast::Type* {
if (arr->Count()->Is<type::RuntimeArrayCount>()) {
return b.ty.array(AtomicTypeFor(arr->ElemType()));
}

View File

@@ -129,7 +129,7 @@ struct Std140::State {
bool ShouldRun() const {
// Returns true if the type needs to be forked for std140 usage.
auto needs_fork = [&](const type::Type* ty) {
while (auto* arr = ty->As<sem::Array>()) {
while (auto* arr = ty->As<type::Array>()) {
ty = arr->ElemType();
}
if (auto* mat = ty->As<type::Matrix>()) {
@@ -426,7 +426,7 @@ struct Std140::State {
}
return nullptr;
},
[&](const sem::Array* arr) -> const ast::Type* {
[&](const type::Array* arr) -> const ast::Type* {
if (auto* std140 = Std140Type(arr->ElemType())) {
utils::Vector<const ast::Attribute*, 1> attrs;
if (!arr->IsStrideImplicit()) {
@@ -631,7 +631,7 @@ struct Std140::State {
return Switch(
ty, //
[&](const sem::Struct* str) { return sym.NameFor(str->Name()); },
[&](const sem::Array* arr) {
[&](const type::Array* arr) {
auto count = arr->ConstantCount();
if (!count) {
// Non-constant counts should not be possible:
@@ -730,7 +730,7 @@ struct Std140::State {
<< "failed to find std140 matrix info for: " << src->FriendlyName(ty);
}
}, //
[&](const sem::Array* arr) {
[&](const type::Array* arr) {
// Converting an array. Create a function var for the converted array, and
// loop over the input elements, converting each and assigning the result to
// the local array.
@@ -1081,7 +1081,7 @@ struct Std140::State {
auto name = "p" + std::to_string(dyn_idx->slot);
return Switch(
ty, //
[&](const sem::Array* arr) -> ExprTypeName {
[&](const type::Array* arr) -> ExprTypeName {
auto* idx = dynamic_index(dyn_idx->slot);
auto* expr = b.IndexAccessor(lhs, idx);
return {expr, arr->ElemType(), name};
@@ -1134,7 +1134,7 @@ struct Std140::State {
ty = member->Type();
return {expr, ty, member_name};
}, //
[&](const sem::Array* arr) -> ExprTypeName {
[&](const type::Array* arr) -> ExprTypeName {
auto* expr = b.IndexAccessor(lhs, idx);
return {expr, arr->ElemType(), std::to_string(idx)};
}, //

View File

@@ -100,7 +100,7 @@ const ast::Type* Transform::CreateASTTypeFor(CloneContext& ctx, const type::Type
auto* el = CreateASTTypeFor(ctx, v->type());
return ctx.dst->create<ast::Vector>(el, v->Width());
}
if (auto* a = ty->As<sem::Array>()) {
if (auto* a = ty->As<type::Array>()) {
auto* el = CreateASTTypeFor(ctx, a->ElemType());
utils::Vector<const ast::Attribute*, 1> attrs;
if (!a->IsStrideImplicit()) {
@@ -133,7 +133,7 @@ const ast::Type* Transform::CreateASTTypeFor(CloneContext& ctx, const type::Type
if (auto count = a->ConstantCount()) {
return ctx.dst->ty.array(el, u32(count.value()), std::move(attrs));
}
TINT_ICE(Transform, ctx.dst->Diagnostics()) << sem::Array::kErrExpectedConstantCount;
TINT_ICE(Transform, ctx.dst->Diagnostics()) << type::Array::kErrExpectedConstantCount;
return ctx.dst->ty.array(el, u32(1), std::move(attrs));
}
if (auto* s = ty->As<sem::Struct>()) {

View File

@@ -69,8 +69,8 @@ TEST_F(CreateASTTypeForTest, Vector) {
TEST_F(CreateASTTypeForTest, ArrayImplicitStride) {
auto* arr = create([](ProgramBuilder& b) {
return b.create<sem::Array>(b.create<type::F32>(), b.create<type::ConstantArrayCount>(2u),
4u, 4u, 32u, 32u);
return b.create<type::Array>(b.create<type::F32>(), b.create<type::ConstantArrayCount>(2u),
4u, 4u, 32u, 32u);
});
ASSERT_TRUE(arr->Is<ast::Array>());
ASSERT_TRUE(arr->As<ast::Array>()->type->Is<ast::F32>());
@@ -83,8 +83,8 @@ TEST_F(CreateASTTypeForTest, ArrayImplicitStride) {
TEST_F(CreateASTTypeForTest, ArrayNonImplicitStride) {
auto* arr = create([](ProgramBuilder& b) {
return b.create<sem::Array>(b.create<type::F32>(), b.create<type::ConstantArrayCount>(2u),
4u, 4u, 64u, 32u);
return b.create<type::Array>(b.create<type::F32>(), b.create<type::ConstantArrayCount>(2u),
4u, 4u, 64u, 32u);
});
ASSERT_TRUE(arr->Is<ast::Array>());
ASSERT_TRUE(arr->As<ast::Array>()->type->Is<ast::F32>());

View File

@@ -47,7 +47,7 @@ Transform::ApplyResult VarForDynamicIndex::Apply(const Program* src,
}
auto* indexed = sem.Get(object_expr);
if (!indexed->Type()->IsAnyOf<sem::Array, type::Matrix>()) {
if (!indexed->Type()->IsAnyOf<type::Array, type::Matrix>()) {
// We only care about array and matrices.
return true;
}

View File

@@ -333,7 +333,7 @@ struct ZeroInitWorkgroupMemory::State {
return true;
}
if (auto* arr = ty->As<sem::Array>()) {
if (auto* arr = ty->As<type::Array>()) {
auto get_el = [&](uint32_t num_values) {
// num_values is the number of values to zero for the element type.
// The number of iterations required to zero the array and its elements is:
@@ -343,7 +343,7 @@ struct ZeroInitWorkgroupMemory::State {
auto count = arr->ConstantCount();
if (!count) {
ctx.dst->Diagnostics().add_error(diag::System::Transform,
sem::Array::kErrExpectedConstantCount);
type::Array::kErrExpectedConstantCount);
return Expression{}; // error
}
auto modulo = num_values * count.value();
@@ -449,7 +449,7 @@ struct ZeroInitWorkgroupMemory::State {
}
}
}
if (ty->Is<sem::Array>()) {
if (ty->Is<type::Array>()) {
return false;
}
// True for all other storable types