writer/hlsl: Zero initialize with (T) 0

For structures and arrays.
This behaves identically to the per-element zero-initialization, but can be significantly less verbose.

Change-Id: I380ef86f16c2b3f37a9de2820e707f368955b761
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/56764
Auto-Submit: Ben Clayton <bclayton@google.com>
Reviewed-by: David Neto <dneto@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
Ben Clayton
2021-07-02 19:27:42 +00:00
parent efc46c1873
commit 2bb45389b7
54 changed files with 132 additions and 158 deletions

View File

@@ -2508,38 +2508,13 @@ bool GeneratorImpl::EmitZeroValue(std::ostream& out, const sem::Type* type) {
return false;
}
}
} else if (auto* str = type->As<sem::Struct>()) {
auto it = structure_builders_.find(str);
if (it != structure_builders_.end()) {
out << it->second << "(";
} else {
out << "{";
} else if (type->IsAnyOf<sem::Struct, sem::Array>()) {
out << "(";
if (!EmitType(out, type, ast::StorageClass::kNone, ast::Access::kUndefined,
"")) {
return false;
}
bool first = true;
for (auto* member : str->Members()) {
if (!first) {
out << ", ";
}
first = false;
if (!EmitZeroValue(out, member->Type())) {
return false;
}
}
out << (it != structure_builders_.end() ? ")" : "}");
} else if (auto* arr = type->As<sem::Array>()) {
out << "{";
auto* elem = arr->ElemType();
for (size_t i = 0; i < arr->Count(); i++) {
if (i > 0) {
out << ", ";
}
if (!EmitZeroValue(out, elem)) {
return false;
}
}
out << "}";
out << ")0";
} else {
diagnostics_.add_error(
diag::System::Writer,

View File

@@ -233,7 +233,7 @@ TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Struct_Empty) {
GeneratorImpl& gen = SanitizeAndBuild();
ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_THAT(gen.result(), HasSubstr("{0, 0.0f, int3(0, 0, 0)}"));
EXPECT_THAT(gen.result(), HasSubstr("(S)0"));
}
} // namespace

View File

@@ -784,7 +784,7 @@ struct tint_array_wrapper {
};
tint_array_wrapper my_func() {
const tint_array_wrapper tint_symbol = {{0.0f, 0.0f, 0.0f, 0.0f, 0.0f}};
const tint_array_wrapper tint_symbol = {(float[5])0};
return tint_symbol;
}
)"));

View File

@@ -136,7 +136,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor, EmitExpression_MemberAccessor) {
float mem;
};
static Data str = {0.0f};
static Data str = (Data)0;
[numthreads(1, 1, 1)]
void test_function() {

View File

@@ -61,8 +61,7 @@ TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement_Array) {
gen.increment_indent();
ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_THAT(gen.result(),
HasSubstr(" float a[5] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f};\n"));
EXPECT_THAT(gen.result(), HasSubstr(" float a[5] = (float[5])0;\n"));
}
TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement_Private) {

View File

@@ -81,7 +81,7 @@ class TextGenerator {
~LineWriter();
/// @returns the ostringstream
operator std::ostream &() { return os; }
operator std::ostream&() { return os; }
/// @param rhs the value to write to the line
/// @returns the ostream so calls can be chained