Import Tint changes from Dawn
Changes:
- 3740ac6092c82b2c385820fcafb385608f26d240 tint: implement const eval of binary divide by Antonio Maiorano <amaiorano@google.com>
- 679cf4f3517b4008ed12ba66bdfd5781a3c72b9c tint: HLSL and GLSL backends now emit 0 for inf and nan by Antonio Maiorano <amaiorano@google.com>
- 1741f4443ecb11fdd80a4be550f171f9885a52fa tint: add CheckedDiv for abstract numbers by Antonio Maiorano <amaiorano@google.com>
- 89dd52881c644466fc2351e694fd8abaa3acb1fd spirv-reader: Fix for SelectUsingReferenceVariable by Robert Stewart <admin@robertstewart.dev>
- 8478dfca563f8b0748422ecea4eafb91ac36a88c tint: improve error message for const eval operator overf... by Antonio Maiorano <amaiorano@google.com>
- 863d9edf59ee93e5ee925a675cbe01a800e36dc7 GLSL: Change Add[Spirv]BlockAttribute to support GLSL by Stephen White <senorblanco@chromium.org>
- 822de46c74d045fe761c73bebd723f75287984e4 spirv-reader: fix image write texel conversion for scalar... by David Neto <dneto@google.com>
- ece807852db8e1e785aa696b3812c7be22182bfd spirv-reader: Fix texel type confusion in loop by Robert Stewart <admin@robertstewart.dev>
- 8b30ca3efa5e35ed735c6cfa2b90ef5e0528c840 tint/transform: Fix FoldTrivialSingleUseLet for abstracts by Ben Clayton <bclayton@google.com>
- 188be384f779fcecad9c34825b2cd02bd295cb5b spirv-reader: UnwrapRef on image coords by Robert Stewart <admin@robertstewart.dev>
- 648bd7b4be1f2943125c370175b5c9b77cf038ec tint: Add TINT_REFLECT() & ForeachField() by Ben Clayton <bclayton@google.com>
GitOrigin-RevId: 3740ac6092c82b2c385820fcafb385608f26d240
Change-Id: I92c9a14e18222dfccefbe4a22842f948b991fc4b
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/101280
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
diff --git a/src/tint/BUILD.gn b/src/tint/BUILD.gn
index c1e84d1..ea32d78 100644
--- a/src/tint/BUILD.gn
+++ b/src/tint/BUILD.gn
@@ -392,6 +392,7 @@
"program_id.h",
"reader/reader.cc",
"reader/reader.h",
+ "reflection.h",
"resolver/const_eval.cc",
"resolver/const_eval.h",
"resolver/ctor_conv_intrinsic.cc",
@@ -469,10 +470,10 @@
"text/unicode.cc",
"text/unicode.h",
"traits.h",
+ "transform/add_block_attribute.cc",
+ "transform/add_block_attribute.h",
"transform/add_empty_entry_point.cc",
"transform/add_empty_entry_point.h",
- "transform/add_spirv_block_attribute.cc",
- "transform/add_spirv_block_attribute.h",
"transform/array_length_from_uniform.cc",
"transform/array_length_from_uniform.h",
"transform/binding_remapper.cc",
@@ -568,6 +569,7 @@
"utils/debugger.cc",
"utils/debugger.h",
"utils/enum_set.h",
+ "utils/foreach_macro.h",
"utils/hash.h",
"utils/hashmap.h",
"utils/hashset.h",
@@ -1184,8 +1186,8 @@
tint_unittests_source_set("tint_unittests_transform_src") {
sources = [
+ "transform/add_block_attribute_test.cc",
"transform/add_empty_entry_point_test.cc",
- "transform/add_spirv_block_attribute_test.cc",
"transform/array_length_from_uniform_test.cc",
"transform/binding_remapper_test.cc",
"transform/builtin_polyfill_test.cc",
@@ -1609,6 +1611,7 @@
"number_test.cc",
"program_builder_test.cc",
"program_test.cc",
+ "reflection_test.cc",
"scope_stack_test.cc",
"source_test.cc",
"symbol_table_test.cc",
diff --git a/src/tint/CMakeLists.txt b/src/tint/CMakeLists.txt
index e8f875b..08212d3 100644
--- a/src/tint/CMakeLists.txt
+++ b/src/tint/CMakeLists.txt
@@ -252,6 +252,7 @@
program_id.h
program.cc
program.h
+ reflection.h
reader/reader.cc
reader/reader.h
resolver/const_eval.cc
@@ -383,8 +384,8 @@
traits.h
transform/add_empty_entry_point.cc
transform/add_empty_entry_point.h
- transform/add_spirv_block_attribute.cc
- transform/add_spirv_block_attribute.h
+ transform/add_block_attribute.cc
+ transform/add_block_attribute.h
transform/array_length_from_uniform.cc
transform/array_length_from_uniform.h
transform/binding_remapper.cc
@@ -478,6 +479,7 @@
utils/concat.h
utils/crc32.h
utils/enum_set.h
+ utils/foreach_macro.h
utils/hash.h
utils/hashmap.h
utils/hashset.h
@@ -777,6 +779,7 @@
number_test.cc
program_builder_test.cc
program_test.cc
+ reflection_test.cc
resolver/array_accessor_test.cc
resolver/assignment_validation_test.cc
resolver/atomics_test.cc
@@ -1099,7 +1102,7 @@
if(${TINT_BUILD_WGSL_READER} AND ${TINT_BUILD_WGSL_WRITER})
list(APPEND TINT_TEST_SRCS
transform/add_empty_entry_point_test.cc
- transform/add_spirv_block_attribute_test.cc
+ transform/add_block_attribute_test.cc
transform/array_length_from_uniform_test.cc
transform/binding_remapper_test.cc
transform/builtin_polyfill_test.cc
diff --git a/src/tint/ast/struct_test.cc b/src/tint/ast/struct_test.cc
index 53ec9c8..29b9e9a 100644
--- a/src/tint/ast/struct_test.cc
+++ b/src/tint/ast/struct_test.cc
@@ -26,13 +26,13 @@
#include "src/tint/ast/texture.h"
#include "src/tint/ast/u32.h"
#include "src/tint/ast/vector.h"
-#include "src/tint/transform/add_spirv_block_attribute.h"
+#include "src/tint/transform/add_block_attribute.h"
namespace tint::ast {
namespace {
using AstStructTest = TestHelper;
-using SpirvBlockAttribute = transform::AddSpirvBlockAttribute::SpirvBlockAttribute;
+using BlockAttribute = transform::AddBlockAttribute::BlockAttribute;
TEST_F(AstStructTest, Creation) {
auto name = Sym("s");
@@ -51,12 +51,12 @@
auto* s = create<Struct>(name, utils::Vector{Member("a", ty.i32())},
utils::Vector{
- ASTNodes().Create<SpirvBlockAttribute>(ID(), AllocateNodeID()),
+ ASTNodes().Create<BlockAttribute>(ID(), AllocateNodeID()),
});
EXPECT_EQ(s->name, name);
EXPECT_EQ(s->members.Length(), 1u);
ASSERT_EQ(s->attributes.Length(), 1u);
- EXPECT_TRUE(s->attributes[0]->Is<SpirvBlockAttribute>());
+ EXPECT_TRUE(s->attributes[0]->Is<BlockAttribute>());
EXPECT_EQ(s->source.range.begin.line, 0u);
EXPECT_EQ(s->source.range.begin.column, 0u);
EXPECT_EQ(s->source.range.end.line, 0u);
@@ -65,14 +65,14 @@
TEST_F(AstStructTest, CreationWithSourceAndAttributes) {
auto name = Sym("s");
- auto* s = create<Struct>(
- Source{Source::Range{Source::Location{27, 4}, Source::Location{27, 8}}}, name,
- utils::Vector{Member("a", ty.i32())},
- utils::Vector{ASTNodes().Create<SpirvBlockAttribute>(ID(), AllocateNodeID())});
+ auto* s =
+ create<Struct>(Source{Source::Range{Source::Location{27, 4}, Source::Location{27, 8}}},
+ name, utils::Vector{Member("a", ty.i32())},
+ utils::Vector{ASTNodes().Create<BlockAttribute>(ID(), AllocateNodeID())});
EXPECT_EQ(s->name, name);
EXPECT_EQ(s->members.Length(), 1u);
ASSERT_EQ(s->attributes.Length(), 1u);
- EXPECT_TRUE(s->attributes[0]->Is<SpirvBlockAttribute>());
+ EXPECT_TRUE(s->attributes[0]->Is<BlockAttribute>());
EXPECT_EQ(s->source.range.begin.line, 27u);
EXPECT_EQ(s->source.range.begin.column, 4u);
EXPECT_EQ(s->source.range.end.line, 27u);
@@ -115,9 +115,9 @@
{
ProgramBuilder b1;
ProgramBuilder b2;
- b1.create<Struct>(b1.Sym("S"), utils::Vector{b1.Member("a", b1.ty.i32())},
- utils::Vector{b2.ASTNodes().Create<SpirvBlockAttribute>(
- b2.ID(), b2.AllocateNodeID())});
+ b1.create<Struct>(
+ b1.Sym("S"), utils::Vector{b1.Member("a", b1.ty.i32())},
+ utils::Vector{b2.ASTNodes().Create<BlockAttribute>(b2.ID(), b2.AllocateNodeID())});
},
"internal compiler error");
}
diff --git a/src/tint/fuzzers/data_builder.h b/src/tint/fuzzers/data_builder.h
index 92a565f..2aaecf7 100644
--- a/src/tint/fuzzers/data_builder.h
+++ b/src/tint/fuzzers/data_builder.h
@@ -17,6 +17,7 @@
#include <cassert>
#include <functional>
+#include <optional>
#include <string>
#include <unordered_map>
#include <vector>
@@ -128,11 +129,26 @@
/// @returns a variable of type T filled with pseudo-random data
static T impl(DataBuilder* b) {
T out{};
- b->build(&out, sizeof(T));
+ if constexpr (tint::HasReflection<T>) {
+ ForeachField(out, [&](auto& field) { b->build(field); });
+ } else if constexpr (std::is_pod_v<T>) {
+ b->build(&out, sizeof(T));
+ } else {
+ static_assert(sizeof(T) == 0, "cannot build type");
+ }
return out;
}
};
+ /// Specialization for bool
+ template <>
+ struct BuildImpl<bool> {
+ /// Generate a pseudo-random bool
+ /// @param b - data builder to use
+ /// @returns a boolean with even odds of being true or false
+ static bool impl(DataBuilder* b) { return b->generator_.GetBool(); }
+ };
+
/// Specialization for std::string
template <>
struct BuildImpl<std::string> {
@@ -150,74 +166,17 @@
}
};
- /// Specialization for bool
- template <>
- struct BuildImpl<bool> {
- /// Generate a pseudo-random bool
+ /// Specialization for std::optional
+ template <typename T>
+ struct BuildImpl<std::optional<T>> {
+ /// Generate a pseudo-random optional<T>
/// @param b - data builder to use
- /// @returns a boolean with even odds of being true or false
- static bool impl(DataBuilder* b) { return b->generator_.GetBool(); }
- };
-
- /// Specialization for writer::msl::Options
- template <>
- struct BuildImpl<writer::msl::Options> {
- /// Generate a pseudo-random writer::msl::Options struct
- /// @param b - data builder to use
- /// @returns writer::msl::Options filled with pseudo-random data
- static writer::msl::Options impl(DataBuilder* b) {
- writer::msl::Options out{};
- b->build(out.buffer_size_ubo_index);
- b->build(out.fixed_sample_mask);
- b->build(out.emit_vertex_point_size);
- b->build(out.disable_workgroup_init);
- b->build(out.generate_external_texture_bindings);
- b->build(out.array_length_from_uniform);
- return out;
- }
- };
-
- /// Specialization for writer::hlsl::Options
- template <>
- struct BuildImpl<writer::hlsl::Options> {
- /// Generate a pseudo-random writer::hlsl::Options struct
- /// @param b - data builder to use
- /// @returns writer::hlsl::Options filled with pseudo-random data
- static writer::hlsl::Options impl(DataBuilder* b) {
- writer::hlsl::Options out{};
- b->build(out.root_constant_binding_point);
- b->build(out.disable_workgroup_init);
- b->build(out.array_length_from_uniform);
- return out;
- }
- };
-
- /// Specialization for writer::spirv::Options
- template <>
- struct BuildImpl<writer::spirv::Options> {
- /// Generate a pseudo-random writer::spirv::Options struct
- /// @param b - data builder to use
- /// @returns writer::spirv::Options filled with pseudo-random data
- static writer::spirv::Options impl(DataBuilder* b) {
- writer::spirv::Options out{};
- b->build(out.emit_vertex_point_size);
- b->build(out.disable_workgroup_init);
- return out;
- }
- };
-
- /// Specialization for writer::ArrayLengthFromUniformOptions
- template <>
- struct BuildImpl<writer::ArrayLengthFromUniformOptions> {
- /// Generate a pseudo-random writer::ArrayLengthFromUniformOptions struct
- /// @param b - data builder to use
- /// @returns writer::ArrayLengthFromUniformOptions filled with pseudo-random
- /// data
- static writer::ArrayLengthFromUniformOptions impl(DataBuilder* b) {
- writer::ArrayLengthFromUniformOptions out{};
- b->build(out.ubo_binding);
- b->build(out.bindpoint_to_size_index);
- return out;
+ /// @returns a either a nullopt, or a randomly filled T
+ static std::optional<T> impl(DataBuilder* b) {
+ if (b->build<bool>()) {
+ return b->build<T>();
+ }
+ return std::nullopt;
}
};
diff --git a/src/tint/intrinsics.def b/src/tint/intrinsics.def
index a565160..8a80bf5 100644
--- a/src/tint/intrinsics.def
+++ b/src/tint/intrinsics.def
@@ -908,10 +908,10 @@
@const("MultiplyVecMat") op * <T: fa_f32_f16, C: num, R: num> (vec<R, T>, mat<C, R, T>) -> vec<C, T>
@const("MultiplyMatMat") op * <T: fa_f32_f16, K: num, C: num, R: num> (mat<K, R, T>, mat<C, K, T>) -> mat<C, R, T>
-op / <T: fiu32_f16>(T, T) -> T
-op / <T: fiu32_f16, N: num> (vec<N, T>, vec<N, T>) -> vec<N, T>
-op / <T: fiu32_f16, N: num> (vec<N, T>, T) -> vec<N, T>
-op / <T: fiu32_f16, N: num> (T, vec<N, T>) -> vec<N, T>
+@const op / <T: fia_fiu32_f16>(T, T) -> T
+@const op / <T: fia_fiu32_f16, N: num> (vec<N, T>, vec<N, T>) -> vec<N, T>
+@const op / <T: fia_fiu32_f16, N: num> (vec<N, T>, T) -> vec<N, T>
+@const op / <T: fia_fiu32_f16, N: num> (T, vec<N, T>) -> vec<N, T>
op % <T: fiu32_f16>(T, T) -> T
op % <T: fiu32_f16, N: num> (vec<N, T>, vec<N, T>) -> vec<N, T>
diff --git a/src/tint/number.h b/src/tint/number.h
index 032844c..5321158 100644
--- a/src/tint/number.h
+++ b/src/tint/number.h
@@ -64,19 +64,6 @@
namespace tint {
-/// Evaluates to true iff T is a floating-point type or is NumberKindF16.
-template <typename T>
-constexpr bool IsFloatingPoint =
- std::is_floating_point_v<T> || std::is_same_v<T, detail::NumberKindF16>;
-
-/// Evaluates to true iff T is an integer type.
-template <typename T>
-constexpr bool IsInteger = std::is_integral_v<T>;
-
-/// Evaluates to true iff T is an integer type, floating-point type or is NumberKindF16.
-template <typename T>
-constexpr bool IsNumeric = IsInteger<T> || IsFloatingPoint<T>;
-
/// Evaluates to true iff T is a Number
template <typename T>
constexpr bool IsNumber = detail::IsNumber<T>::value;
@@ -85,6 +72,27 @@
template <typename T>
using UnwrapNumber = typename detail::NumberUnwrapper<T>::type;
+/// Evaluates to true iff T or Number<T> is a floating-point type or is NumberKindF16.
+template <typename T, typename U = std::conditional_t<IsNumber<T>, UnwrapNumber<T>, T>>
+constexpr bool IsFloatingPoint =
+ std::is_floating_point_v<U> || std::is_same_v<T, detail::NumberKindF16>;
+
+/// Evaluates to true iff T or Number<T> is an integral type.
+template <typename T, typename U = std::conditional_t<IsNumber<T>, UnwrapNumber<T>, T>>
+constexpr bool IsIntegral = std::is_integral_v<U>;
+
+/// Evaluates to true iff T or Number<T> is a signed integer type.
+template <typename T, typename U = std::conditional_t<IsNumber<T>, UnwrapNumber<T>, T>>
+constexpr bool IsSignedIntegral = std::is_integral_v<U> && std::is_signed_v<U>;
+
+/// Evaluates to true iff T or Number<T> is an unsigned integer type.
+template <typename T, typename U = std::conditional_t<IsNumber<T>, UnwrapNumber<T>, T>>
+constexpr bool IsUnsignedIntegral = std::is_integral_v<U> && std::is_unsigned_v<U>;
+
+/// Evaluates to true iff T is an integer type, floating-point type or is NumberKindF16.
+template <typename T>
+constexpr bool IsNumeric = IsIntegral<T> || IsFloatingPoint<T>;
+
/// NumberBase is a CRTP base class for Number<T>
template <typename NumberT>
struct NumberBase {
@@ -479,6 +487,28 @@
return AFloat{result};
}
+/// @returns a / b, or an empty optional if the resulting value overflowed the AInt
+inline std::optional<AInt> CheckedDiv(AInt a, AInt b) {
+ if (b == 0) {
+ return {};
+ }
+
+ if (b == -1 && a == AInt::Lowest()) {
+ return {};
+ }
+
+ return AInt{a.value / b.value};
+}
+
+/// @returns a / b, or an empty optional if the resulting value overflowed the AFloat
+inline std::optional<AFloat> CheckedDiv(AFloat a, AFloat b) {
+ auto result = a.value / b.value;
+ if (!std::isfinite(result)) {
+ return {};
+ }
+ return AFloat{result};
+}
+
/// @returns a * b + c, or an empty optional if the value overflowed the AInt
inline std::optional<AInt> CheckedMadd(AInt a, AInt b, AInt c) {
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80635
diff --git a/src/tint/number_test.cc b/src/tint/number_test.cc
index 3182ad3..15d96e7 100644
--- a/src/tint/number_test.cc
+++ b/src/tint/number_test.cc
@@ -537,6 +537,60 @@
////////////////////////////////////////////////////////////////////////
}));
+using CheckedDivTest_AInt = testing::TestWithParam<BinaryCheckedCase_AInt>;
+TEST_P(CheckedDivTest_AInt, Test) {
+ auto expect = std::get<0>(GetParam());
+ auto a = std::get<1>(GetParam());
+ auto b = std::get<2>(GetParam());
+ EXPECT_EQ(CheckedDiv(a, b), expect) << std::hex << "0x" << a << " - 0x" << b;
+}
+INSTANTIATE_TEST_SUITE_P(
+ CheckedDivTest_AInt,
+ CheckedDivTest_AInt,
+ testing::ValuesIn(std::vector<BinaryCheckedCase_AInt>{
+ {AInt(0), AInt(0), AInt(1)},
+ {AInt(1), AInt(1), AInt(1)},
+ {AInt(1), AInt(1), AInt(1)},
+ {AInt(2), AInt(2), AInt(1)},
+ {AInt(2), AInt(4), AInt(2)},
+ {AInt::Highest(), AInt::Highest(), AInt(1)},
+ {AInt::Lowest(), AInt::Lowest(), AInt(1)},
+ {AInt(1), AInt::Highest(), AInt::Highest()},
+ {AInt(0), AInt(0), AInt::Highest()},
+ {AInt(0), AInt(0), AInt::Lowest()},
+ {OVERFLOW, AInt(123), AInt(0)},
+ {OVERFLOW, AInt(-123), AInt(0)},
+ ////////////////////////////////////////////////////////////////////////
+ }));
+
+using CheckedDivTest_AFloat = testing::TestWithParam<BinaryCheckedCase_AFloat>;
+TEST_P(CheckedDivTest_AFloat, Test) {
+ auto expect = std::get<0>(GetParam());
+ auto a = std::get<1>(GetParam());
+ auto b = std::get<2>(GetParam());
+ EXPECT_EQ(CheckedDiv(a, b), expect) << std::hex << "0x" << a << " - 0x" << b;
+}
+INSTANTIATE_TEST_SUITE_P(
+ CheckedDivTest_AFloat,
+ CheckedDivTest_AFloat,
+ testing::ValuesIn(std::vector<BinaryCheckedCase_AFloat>{
+ {AFloat(0), AFloat(0), AFloat(1)},
+ {AFloat(1), AFloat(1), AFloat(1)},
+ {AFloat(1), AFloat(1), AFloat(1)},
+ {AFloat(2), AFloat(2), AFloat(1)},
+ {AFloat(2), AFloat(4), AFloat(2)},
+ {AFloat::Highest(), AFloat::Highest(), AFloat(1)},
+ {AFloat::Lowest(), AFloat::Lowest(), AFloat(1)},
+ {AFloat(1), AFloat::Highest(), AFloat::Highest()},
+ {AFloat(0), AFloat(0), AFloat::Highest()},
+ {-AFloat(0), AFloat(0), AFloat::Lowest()},
+ {OVERFLOW, AFloat(123), AFloat(0)},
+ {OVERFLOW, AFloat(123), AFloat(-0)},
+ {OVERFLOW, AFloat(-123), AFloat(0)},
+ {OVERFLOW, AFloat(-123), AFloat(-0)},
+ ////////////////////////////////////////////////////////////////////////
+ }));
+
using TernaryCheckedCase = std::tuple<std::optional<AInt>, AInt, AInt, AInt>;
using CheckedMaddTest_AInt = testing::TestWithParam<TernaryCheckedCase>;
diff --git a/src/tint/reader/spirv/function.cc b/src/tint/reader/spirv/function.cc
index fcf149b..0acfa1d 100644
--- a/src/tint/reader/spirv/function.cc
+++ b/src/tint/reader/spirv/function.cc
@@ -5000,7 +5000,7 @@
// - true_value false_value, and result type to match.
// - you can't select over pointers or pointer vectors, unless you also have
// a VariablePointers* capability, which is not allowed in by WebGPU.
- auto* op_ty = true_value.type;
+ auto* op_ty = true_value.type->UnwrapRef();
if (op_ty->Is<Vector>() || op_ty->IsFloatScalar() || op_ty->IsIntegerScalar() ||
op_ty->Is<Bool>()) {
ExpressionList params;
@@ -5636,7 +5636,9 @@
const auto num_coords_required = num_axes + (is_arrayed ? 1 : 0) + (is_proj ? 1 : 0);
uint32_t num_coords_supplied = 0;
- auto* component_type = raw_coords.type;
+ // Get the component type. The raw_coords might have been hoisted into
+ // a 'var' declaration, so unwrap the referenece if needed.
+ auto* component_type = raw_coords.type->UnwrapRef();
if (component_type->IsFloatScalar() || component_type->IsIntegerScalar()) {
num_coords_supplied = 1;
} else if (auto* vec_type = As<Vector>(raw_coords.type)) {
@@ -5714,7 +5716,7 @@
TypedExpression texel,
const Texture* texture_type) {
auto* storage_texture_type = As<StorageTexture>(texture_type);
- auto* src_type = texel.type;
+ auto* src_type = texel.type->UnwrapRef();
if (!storage_texture_type) {
Fail() << "writing to other than storage texture: " << inst.PrettyPrint();
return nullptr;
@@ -5738,8 +5740,8 @@
// Component type must match floatness, or integral signedness.
if ((src_type->IsFloatScalarOrVector() != dest_type->IsFloatVector()) ||
- (src_type->IsUnsignedIntegerVector() != dest_type->IsUnsignedIntegerVector()) ||
- (src_type->IsSignedIntegerVector() != dest_type->IsSignedIntegerVector())) {
+ (src_type->IsUnsignedScalarOrVector() != dest_type->IsUnsignedIntegerVector()) ||
+ (src_type->IsSignedScalarOrVector() != dest_type->IsSignedIntegerVector())) {
Fail() << "invalid texel type for storage texture write: component must be "
"float, signed integer, or unsigned integer "
"to match the texture channel type: "
@@ -5762,14 +5764,14 @@
if (src_count < dest_count) {
// Expand the texel to a 4 element vector.
- auto* component_type = texel.type->IsScalar() ? texel.type : texel.type->As<Vector>()->type;
- texel.type = ty_.Vector(component_type, dest_count);
+ auto* component_type = src_type->IsScalar() ? src_type : src_type->As<Vector>()->type;
+ src_type = ty_.Vector(component_type, dest_count);
ExpressionList exprs;
exprs.Push(texel.expr);
for (auto i = src_count; i < dest_count; i++) {
exprs.Push(parser_impl_.MakeNullExpression(component_type).expr);
}
- texel.expr = builder_.Construct(Source{}, texel.type->Build(builder_), std::move(exprs));
+ texel.expr = builder_.Construct(Source{}, src_type->Build(builder_), std::move(exprs));
}
return texel.expr;
diff --git a/src/tint/reader/spirv/parser_impl_handle_test.cc b/src/tint/reader/spirv/parser_impl_handle_test.cc
index e1b4889..a165f7b 100644
--- a/src/tint/reader/spirv/parser_impl_handle_test.cc
+++ b/src/tint/reader/spirv/parser_impl_handle_test.cc
@@ -2407,10 +2407,10 @@
INSTANTIATE_TEST_SUITE_P(
// SPIR-V's texel parameter is a scalar or vector with at least as many
// components as there are channels in the underlying format, and the
- // componet type matches the sampled type (modulo signed/unsigned integer).
+ // component type matches the sampled type (modulo signed/unsigned integer).
// WGSL's texel parameter is a 4-element vector scalar or vector, with
// component type equal to the 32-bit form of the channel type.
- ImageWrite_ConvertTexelOperand_Arity,
+ ImageWrite_ConvertTexelOperand_Arity_Float,
SpvParserHandleTest_ImageAccessTest,
::testing::ValuesIn(std::vector<ImageAccessCase>{
// Source 1 component
@@ -2448,6 +2448,86 @@
"texture_storage_2d<rgba32float, write>;",
"textureStore(x_20, vi12, vf1234);"}}));
+INSTANTIATE_TEST_SUITE_P(
+ // As above, but unsigned integer.
+ ImageWrite_ConvertTexelOperand_Arity_Uint,
+ SpvParserHandleTest_ImageAccessTest,
+ ::testing::ValuesIn(std::vector<ImageAccessCase>{
+ // Source 1 component
+ {"%uint 2D 0 0 0 2 R32ui", "OpImageWrite %im %vi12 %u1",
+ R"(@group(2) @binding(1) var x_20 : texture_storage_2d<r32uint, write>;)",
+ "textureStore(x_20, vi12, vec4<u32>(u1, 0u, 0u, 0u));"},
+ // Source 2 component, dest 1 component
+ {"%uint 2D 0 0 0 2 R32ui", "OpImageWrite %im %vi12 %vu12",
+ R"(@group(2) @binding(1) var x_20 : texture_storage_2d<r32uint, write>;)",
+ "textureStore(x_20, vi12, vec4<u32>(vu12, 0u, 0u));"},
+ // Source 3 component, dest 1 component
+ {"%uint 2D 0 0 0 2 R32ui", "OpImageWrite %im %vi12 %vu123",
+ R"(@group(2) @binding(1) var x_20 : texture_storage_2d<r32uint, write>;)",
+ "textureStore(x_20, vi12, vec4<u32>(vu123, 0u));"},
+ // Source 4 component, dest 1 component
+ {"%uint 2D 0 0 0 2 R32ui", "OpImageWrite %im %vi12 %vu1234",
+ R"(@group(2) @binding(1) var x_20 : texture_storage_2d<r32uint, write>;)",
+ "textureStore(x_20, vi12, vu1234);"},
+ // Source 2 component, dest 2 component
+ {"%uint 2D 0 0 0 2 Rg32ui", "OpImageWrite %im %vi12 %vu12",
+ R"(@group(2) @binding(1) var x_20 : texture_storage_2d<rg32uint, write>;)",
+ "textureStore(x_20, vi12, vec4<u32>(vu12, 0u, 0u));"},
+ // Source 3 component, dest 2 component
+ {"%uint 2D 0 0 0 2 Rg32ui", "OpImageWrite %im %vi12 %vu123",
+ R"(@group(2) @binding(1) var x_20 : texture_storage_2d<rg32uint, write>;)",
+ "textureStore(x_20, vi12, vec4<u32>(vu123, 0u));"},
+ // Source 4 component, dest 2 component
+ {"%uint 2D 0 0 0 2 Rg32ui", "OpImageWrite %im %vi12 %vu1234",
+ R"(@group(2) @binding(1) var x_20 : texture_storage_2d<rg32uint, write>;)",
+ "textureStore(x_20, vi12, vu1234);"},
+ // WGSL does not support 3-component storage textures.
+ // Source 4 component, dest 4 component
+ {"%uint 2D 0 0 0 2 Rgba32ui", "OpImageWrite %im %vi12 %vu1234",
+ "@group(2) @binding(1) var x_20 : "
+ "texture_storage_2d<rgba32uint, write>;",
+ "textureStore(x_20, vi12, vu1234);"}}));
+
+INSTANTIATE_TEST_SUITE_P(
+ // As above, but signed integer.
+ ImageWrite_ConvertTexelOperand_Arity_Sint,
+ SpvParserHandleTest_ImageAccessTest,
+ ::testing::ValuesIn(std::vector<ImageAccessCase>{
+ // Source 1 component
+ {"%int 2D 0 0 0 2 R32i", "OpImageWrite %im %vi12 %i1",
+ R"(@group(2) @binding(1) var x_20 : texture_storage_2d<r32sint, write>;)",
+ "textureStore(x_20, vi12, vec4<i32>(i1, 0i, 0i, 0i));"},
+ // Source 2 component, dest 1 component
+ {"%int 2D 0 0 0 2 R32i", "OpImageWrite %im %vi12 %vi12",
+ R"(@group(2) @binding(1) var x_20 : texture_storage_2d<r32sint, write>;)",
+ "textureStore(x_20, vi12, vec4<i32>(vi12, 0i, 0i));"},
+ // Source 3 component, dest 1 component
+ {"%int 2D 0 0 0 2 R32i", "OpImageWrite %im %vi12 %vi123",
+ R"(@group(2) @binding(1) var x_20 : texture_storage_2d<r32sint, write>;)",
+ "textureStore(x_20, vi12, vec4<i32>(vi123, 0i));"},
+ // Source 4 component, dest 1 component
+ {"%int 2D 0 0 0 2 R32i", "OpImageWrite %im %vi12 %vi1234",
+ R"(@group(2) @binding(1) var x_20 : texture_storage_2d<r32sint, write>;)",
+ "textureStore(x_20, vi12, vi1234);"},
+ // Source 2 component, dest 2 component
+ {"%int 2D 0 0 0 2 Rg32i", "OpImageWrite %im %vi12 %vi12",
+ R"(@group(2) @binding(1) var x_20 : texture_storage_2d<rg32sint, write>;)",
+ "textureStore(x_20, vi12, vec4<i32>(vi12, 0i, 0i));"},
+ // Source 3 component, dest 2 component
+ {"%int 2D 0 0 0 2 Rg32i", "OpImageWrite %im %vi12 %vi123",
+ R"(@group(2) @binding(1) var x_20 : texture_storage_2d<rg32sint, write>;)",
+ "textureStore(x_20, vi12, vec4<i32>(vi123, 0i));"},
+ // Source 4 component, dest 2 component
+ {"%int 2D 0 0 0 2 Rg32i", "OpImageWrite %im %vi12 %vi1234",
+ R"(@group(2) @binding(1) var x_20 : texture_storage_2d<rg32sint, write>;)",
+ "textureStore(x_20, vi12, vi1234);"},
+ // WGSL does not support 3-component storage textures.
+ // Source 4 component, dest 4 component
+ {"%int 2D 0 0 0 2 Rgba32i", "OpImageWrite %im %vi12 %vi1234",
+ "@group(2) @binding(1) var x_20 : "
+ "texture_storage_2d<rgba32sint, write>;",
+ "textureStore(x_20, vi12, vi1234);"}}));
+
TEST_F(SpvParserHandleTest, ImageWrite_TooFewSrcTexelComponents_1_vs_4) {
const auto assembly = Preamble() + R"(
OpEntryPoint Fragment %main "main"
@@ -3392,9 +3472,10 @@
{"%float 1D 0 0 0 1 Unknown",
"%result = OpImageSampleImplicitLod "
// bad type for coordinate: not a number
- "%v4float %sampled_image %float_var",
+ // %10 is the sampler variable
+ "%v4float %sampled_image %10",
"bad or unsupported coordinate type for image access: %73 = "
- "OpImageSampleImplicitLod %42 %72 %1",
+ "OpImageSampleImplicitLod %42 %72 %10",
{}},
{"%float 2D 0 0 0 1 Unknown", // 2D
"%result = OpImageSampleImplicitLod "
@@ -3753,5 +3834,257 @@
ASSERT_EQ(expect, got);
}
+TEST_F(SpvParserHandleTest, ImageCoordinateCanBeHoistedConstant) {
+ // Demonstrates fix for crbug.com/tint/1646
+ // The problem is the coordinate for an image operation
+ // can be a combinatorial value that has been hoisted out
+ // to a 'var' declaration.
+ //
+ // In this test (and the original case form the bug), the
+ // definition for the value is in an outer construct, and
+ // the image operation using it is in a doubly nested
+ // construct.
+ //
+ // The coordinate handling has to unwrap the ref type it
+ // made for the 'var' declaration.
+ const auto assembly = Preamble() + R"(
+
+OpEntryPoint Fragment %100 "main"
+OpExecutionMode %100 OriginUpperLeft
+OpDecorate %10 DescriptorSet 0
+OpDecorate %10 Binding 0
+OpDecorate %20 DescriptorSet 2
+OpDecorate %20 Binding 1
+
+%void = OpTypeVoid
+%voidfn = OpTypeFunction %void
+
+%bool = OpTypeBool
+%true = OpConstantTrue %bool
+%float = OpTypeFloat 32
+
+%v4float = OpTypeVector %float 4
+
+%float_null = OpConstantNull %float
+
+%sampler = OpTypeSampler
+%ptr_sampler = OpTypePointer UniformConstant %sampler
+%im_ty = OpTypeImage %float 1D 0 0 0 1 Unknown
+%ptr_im_ty = OpTypePointer UniformConstant %im_ty
+%si_ty = OpTypeSampledImage %im_ty
+
+%10 = OpVariable %ptr_sampler UniformConstant
+%20 = OpVariable %ptr_im_ty UniformConstant
+
+%100 = OpFunction %void None %voidfn
+%entry = OpLabel
+%900 = OpCopyObject %float %float_null ; definition here
+OpSelectionMerge %99 None
+OpBranchConditional %true %40 %99
+
+ %40 = OpLabel
+ OpSelectionMerge %80 None
+ OpBranchConditional %true %50 %80
+
+ %50 = OpLabel
+ %sam = OpLoad %sampler %10
+ %im = OpLoad %im_ty %20
+ %sampled_image = OpSampledImage %si_ty %im %sam
+ %result = OpImageSampleImplicitLod %v4float %sampled_image %900 ; usage here
+ OpBranch %80
+
+ %80 = OpLabel
+ OpBranch %99
+
+%99 = OpLabel
+OpReturn
+OpFunctionEnd
+
+ )";
+ auto p = parser(test::Assemble(assembly));
+ EXPECT_TRUE(p->BuildAndParseInternalModule()) << assembly;
+ auto fe = p->function_emitter(100);
+ EXPECT_TRUE(fe.EmitBody()) << p->error();
+ EXPECT_TRUE(p->error().empty()) << p->error();
+ auto ast_body = fe.ast_body();
+ const auto got = test::ToString(p->program(), ast_body);
+ auto* expect = R"(var x_900 : f32;
+x_900 = 0.0f;
+if (true) {
+ if (true) {
+ let x_18 : vec4<f32> = textureSample(x_20, x_10, x_900);
+ }
+}
+return;
+)";
+ ASSERT_EQ(expect, got);
+}
+
+TEST_F(SpvParserHandleTest, TexelTypeWhenLoop) {
+ // Demonstrates fix for crbug.com/tint/1642
+ // The problem is the texel value for an image write
+ // can be given in 'var' declaration.
+ //
+ // The texel value handling has to unwrap the ref type first.
+ const auto assembly = Preamble() + R"(
+ OpCapability Shader
+ OpCapability StorageImageExtendedFormats
+ OpMemoryModel Logical GLSL450
+ OpEntryPoint GLCompute %100 "main"
+ OpExecutionMode %100 LocalSize 8 8 1
+ OpSource HLSL 600
+ OpName %type_2d_image "type.2d.image"
+ OpName %Output2Texture2D "Output2Texture2D"
+ OpName %100 "main"
+ OpDecorate %Output2Texture2D DescriptorSet 0
+ OpDecorate %Output2Texture2D Binding 0
+ %float = OpTypeFloat 32
+ %float_0 = OpConstant %float 0
+ %v2float = OpTypeVector %float 2
+ %7 = OpConstantComposite %v2float %float_0 %float_0
+ %int = OpTypeInt 32 1
+ %int_0 = OpConstant %int 0
+ %int_2 = OpConstant %int 2
+ %float_1 = OpConstant %float 1
+ %12 = OpConstantComposite %v2float %float_1 %float_1
+ %int_1 = OpConstant %int 1
+ %uint = OpTypeInt 32 0
+ %uint_1 = OpConstant %uint 1
+ %v2uint = OpTypeVector %uint 2
+ %17 = OpConstantComposite %v2uint %uint_1 %uint_1
+%type_2d_image = OpTypeImage %float 2D 2 0 0 2 Rg32f
+%_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image
+ %void = OpTypeVoid
+ %20 = OpTypeFunction %void
+ %bool = OpTypeBool
+%Output2Texture2D = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
+ %100 = OpFunction %void None %20
+ %22 = OpLabel
+ OpBranch %23
+ %23 = OpLabel
+ %24 = OpPhi %v2float %7 %22 %12 %25
+ %26 = OpPhi %int %int_0 %22 %27 %25
+ %28 = OpSLessThan %bool %26 %int_2
+ OpLoopMerge %29 %25 None
+ OpBranchConditional %28 %25 %29
+ %25 = OpLabel
+ %27 = OpIAdd %int %26 %int_1
+ OpBranch %23
+ %29 = OpLabel
+ %30 = OpLoad %type_2d_image %Output2Texture2D
+ OpImageWrite %30 %17 %24 None
+ OpReturn
+ OpFunctionEnd
+ )";
+ auto p = parser(test::Assemble(assembly));
+ EXPECT_TRUE(p->BuildAndParseInternalModule()) << assembly;
+ auto fe = p->function_emitter(100);
+ EXPECT_TRUE(fe.EmitBody()) << p->error();
+ EXPECT_TRUE(p->error().empty()) << p->error();
+ auto ast_body = fe.ast_body();
+ const auto got = test::ToString(p->program(), ast_body);
+ auto* expect = R"(var x_24 : vec2<f32>;
+var x_24_phi_1 : vec2<f32>;
+var x_26_phi_1 : i32;
+x_24_phi_1 = vec2<f32>(0.0f, 0.0f);
+x_26_phi_1 = 0i;
+loop {
+ var x_27 : i32;
+ x_24 = x_24_phi_1;
+ let x_26 : i32 = x_26_phi_1;
+ if ((x_26 < 2i)) {
+ } else {
+ break;
+ }
+
+ continuing {
+ x_27 = (x_26 + 1i);
+ x_24_phi_1 = vec2<f32>(1.0f, 1.0f);
+ x_26_phi_1 = x_27;
+ }
+}
+textureStore(Output2Texture2D, vec2<i32>(vec2<u32>(1u, 1u)), vec4<f32>(x_24, 0.0f, 0.0f));
+return;
+)";
+ ASSERT_EQ(expect, got);
+}
+
+TEST_F(SpvParserHandleTest, SimpleSelectCanSelectFromHoistedConstant) {
+ // Demonstrates fix for crbug.com/tint/1642
+ // The problem is an operand to a simple select can be a value
+ // that is hoisted into a 'var' declaration.
+ //
+ // The selection-generation logic has to UnwrapRef if needed.
+ const auto assembly = Preamble() + R"(
+ OpCapability Shader
+ OpMemoryModel Logical GLSL450
+ OpEntryPoint Vertex %100 "main" %gl_Position
+ OpSource HLSL 600
+ OpName %100 "main"
+ OpDecorate %gl_Position BuiltIn Position
+ %float = OpTypeFloat 32
+ %float_0 = OpConstant %float 0
+ %float_1 = OpConstant %float 1
+ %v4float = OpTypeVector %float 4
+%_ptr_Output_v4float = OpTypePointer Output %v4float
+ %void = OpTypeVoid
+ %9 = OpTypeFunction %void
+ %bool = OpTypeBool
+%gl_Position = OpVariable %_ptr_Output_v4float Output
+ %11 = OpUndef %float
+ %100 = OpFunction %void None %9
+ %12 = OpLabel
+ OpBranch %13
+ %13 = OpLabel
+ %14 = OpPhi %float %11 %12 %15 %16
+ %15 = OpPhi %float %float_0 %12 %17 %16
+ %18 = OpFOrdLessThan %bool %15 %float_1
+ OpLoopMerge %19 %16 None
+ OpBranchConditional %18 %16 %19
+ %16 = OpLabel
+ %17 = OpFAdd %float %15 %float_1
+ OpBranch %13
+ %19 = OpLabel
+ %20 = OpFOrdGreaterThan %bool %14 %float_1
+ %21 = OpSelect %float %20 %14 %float_0
+ %22 = OpCompositeConstruct %v4float %21 %21 %21 %21
+ OpStore %gl_Position %22
+ OpReturn
+ OpFunctionEnd
+ )";
+ auto p = parser(test::Assemble(assembly));
+ EXPECT_TRUE(p->BuildAndParseInternalModule()) << assembly;
+ auto fe = p->function_emitter(100);
+ EXPECT_TRUE(fe.EmitBody()) << p->error();
+ EXPECT_TRUE(p->error().empty()) << p->error();
+ auto ast_body = fe.ast_body();
+ const auto got = test::ToString(p->program(), ast_body);
+ auto* expect = R"(var x_14 : f32;
+var x_14_phi_1 : f32;
+var x_15_phi_1 : f32;
+x_14_phi_1 = 0.0f;
+x_15_phi_1 = 0.0f;
+loop {
+ var x_17 : f32;
+ x_14 = x_14_phi_1;
+ let x_15 : f32 = x_15_phi_1;
+ if ((x_15 < 1.0f)) {
+ } else {
+ break;
+ }
+
+ continuing {
+ x_17 = (x_15 + 1.0f);
+ x_14_phi_1 = x_15;
+ x_15_phi_1 = x_17;
+ }
+}
+let x_21 : f32 = select(0.0f, x_14, (x_14 > 1.0f));
+x_1 = vec4<f32>(x_21, x_21, x_21, x_21);
+return;
+)";
+ ASSERT_EQ(expect, got);
+}
+
} // namespace
} // namespace tint::reader::spirv
diff --git a/src/tint/reflection.h b/src/tint/reflection.h
new file mode 100644
index 0000000..0591838
--- /dev/null
+++ b/src/tint/reflection.h
@@ -0,0 +1,67 @@
+// Copyright 2022 The Tint Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef SRC_TINT_REFLECTION_H_
+#define SRC_TINT_REFLECTION_H_
+
+#include "src/tint/traits.h"
+#include "src/tint/utils/concat.h"
+#include "src/tint/utils/foreach_macro.h"
+
+namespace tint {
+
+namespace detail {
+
+/// Helper for detecting whether the type T contains a nested Reflection class.
+template <typename T, typename ENABLE = void>
+struct HasReflection : std::false_type {};
+
+/// Specialization for types that have a nested Reflection class.
+template <typename T>
+struct HasReflection<T, std::void_t<typename T::Reflection>> : std::true_type {};
+
+} // namespace detail
+
+/// Is true if the class T has reflected its fields with TINT_REFLECT()
+template <typename T>
+static constexpr bool HasReflection = detail::HasReflection<T>::value;
+
+/// Calls @p callback with each field of @p object
+/// @param object the object
+/// @param callback a function that is called for each field of @p object.
+/// @tparam CB a function with the signature `void(FIELD)`
+template <typename OBJECT, typename CB>
+void ForeachField(OBJECT&& object, CB&& callback) {
+ using T = std::decay_t<OBJECT>;
+ static_assert(HasReflection<T>, "object type requires a tint::Reflect<> specialization");
+ T::Reflection::Fields(object, callback);
+}
+
+/// Macro used by TINT_FOREACH() in TINT_REFLECT() to call the callback function with each field in
+/// the variadic.
+#define TINT_REFLECT_CALLBACK_FIELD(field) callback(object.field);
+
+// TINT_REFLECT(...) reflects each of the fields arguments so that the types can be used with
+// tint::ForeachField().
+#define TINT_REFLECT(...) \
+ struct Reflection { \
+ template <typename OBJECT, typename CB> \
+ static void Fields(OBJECT&& object, CB&& callback) { \
+ TINT_FOREACH(TINT_REFLECT_CALLBACK_FIELD, __VA_ARGS__) \
+ } \
+ }
+
+} // namespace tint
+
+#endif // SRC_TINT_REFLECTION_H_
diff --git a/src/tint/reflection_test.cc b/src/tint/reflection_test.cc
new file mode 100644
index 0000000..3c88b8d
--- /dev/null
+++ b/src/tint/reflection_test.cc
@@ -0,0 +1,91 @@
+// Copyright 2022 The Tint Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "src/tint/reflection.h"
+#include "gtest/gtest.h"
+
+namespace tint {
+namespace {
+
+struct S {
+ int i;
+ unsigned u;
+ bool b;
+ TINT_REFLECT(i, u, b);
+};
+
+static_assert(!HasReflection<int>);
+static_assert(HasReflection<S>);
+
+TEST(ReflectionTest, ForeachFieldConst) {
+ const S s{1, 2, true};
+ size_t field_idx = 0;
+ ForeachField(s, [&](auto& field) {
+ using T = std::decay_t<decltype(field)>;
+ switch (field_idx) {
+ case 0:
+ EXPECT_TRUE((std::is_same_v<T, int>));
+ EXPECT_EQ(field, static_cast<T>(1));
+ break;
+ case 1:
+ EXPECT_TRUE((std::is_same_v<T, unsigned>));
+ EXPECT_EQ(field, static_cast<T>(2));
+ break;
+ case 2:
+ EXPECT_TRUE((std::is_same_v<T, bool>));
+ EXPECT_EQ(field, static_cast<T>(true));
+ break;
+ default:
+ FAIL() << "unexpected field";
+ break;
+ }
+ field_idx++;
+ });
+}
+
+TEST(ReflectionTest, ForeachFieldNonConst) {
+ S s{1, 2, true};
+ size_t field_idx = 0;
+ ForeachField(s, [&](auto& field) {
+ using T = std::decay_t<decltype(field)>;
+ switch (field_idx) {
+ case 0:
+ EXPECT_TRUE((std::is_same_v<T, int>));
+ EXPECT_EQ(field, static_cast<T>(1));
+ field = static_cast<T>(10);
+ break;
+ case 1:
+ EXPECT_TRUE((std::is_same_v<T, unsigned>));
+ EXPECT_EQ(field, static_cast<T>(2));
+ field = static_cast<T>(20);
+ break;
+ case 2:
+ EXPECT_TRUE((std::is_same_v<T, bool>));
+ EXPECT_EQ(field, static_cast<T>(true));
+ field = static_cast<T>(false);
+ break;
+ default:
+ FAIL() << "unexpected field";
+ break;
+ }
+ field_idx++;
+ });
+
+ EXPECT_EQ(s.i, 10);
+ EXPECT_EQ(s.u, 20u);
+ EXPECT_EQ(s.b, false);
+}
+
+} // namespace
+} // namespace tint
diff --git a/src/tint/resolver/attribute_validation_test.cc b/src/tint/resolver/attribute_validation_test.cc
index 0180589..7f573a8 100644
--- a/src/tint/resolver/attribute_validation_test.cc
+++ b/src/tint/resolver/attribute_validation_test.cc
@@ -15,7 +15,7 @@
#include "src/tint/ast/disable_validation_attribute.h"
#include "src/tint/resolver/resolver.h"
#include "src/tint/resolver/resolver_test_helper.h"
-#include "src/tint/transform/add_spirv_block_attribute.h"
+#include "src/tint/transform/add_block_attribute.h"
#include "gmock/gmock.h"
@@ -557,7 +557,7 @@
namespace StructAndStructMemberTests {
using StructAttributeTest = TestWithParams;
-using SpirvBlockAttribute = transform::AddSpirvBlockAttribute::SpirvBlockAttribute;
+using SpirvBlockAttribute = transform::AddBlockAttribute::BlockAttribute;
TEST_P(StructAttributeTest, IsValid) {
auto& params = GetParam();
diff --git a/src/tint/resolver/const_eval.cc b/src/tint/resolver/const_eval.cc
index d363b67..0f4872b 100644
--- a/src/tint/resolver/const_eval.cc
+++ b/src/tint/resolver/const_eval.cc
@@ -140,6 +140,14 @@
return Number<N>(value) == Number<N>(0); // Considers sign bit
}
+template <typename NumberT>
+std::string OverflowErrorMessage(NumberT lhs, const char* op, NumberT rhs) {
+ std::stringstream ss;
+ ss << "'" << lhs.value << " " << op << " " << rhs.value << "' cannot be represented as '"
+ << FriendlyName<NumberT>() << "'";
+ return ss.str();
+}
+
/// Constant inherits from sem::Constant to add an private implementation method for conversion.
struct Constant : public sem::Constant {
/// Convert attempts to convert the constant value to the given type. On error, Convert()
@@ -515,28 +523,26 @@
template <typename NumberT>
utils::Result<NumberT> ConstEval::Add(NumberT a, NumberT b) {
- using T = UnwrapNumber<NumberT>;
- auto add_values = [](T lhs, T rhs) {
- if constexpr (std::is_integral_v<T> && std::is_signed_v<T>) {
- // Ensure no UB for signed overflow
- using UT = std::make_unsigned_t<T>;
- return static_cast<T>(static_cast<UT>(lhs) + static_cast<UT>(rhs));
- } else {
- return lhs + rhs;
- }
- };
NumberT result;
if constexpr (std::is_same_v<NumberT, AInt> || std::is_same_v<NumberT, AFloat>) {
// Check for over/underflow for abstract values
if (auto r = CheckedAdd(a, b)) {
result = r->value;
} else {
- AddError("'" + std::to_string(add_values(a.value, b.value)) +
- "' cannot be represented as '" + FriendlyName<NumberT>() + "'",
- *current_source);
+ AddError(OverflowErrorMessage(a, "+", b), *current_source);
return utils::Failure;
}
} else {
+ using T = UnwrapNumber<NumberT>;
+ auto add_values = [](T lhs, T rhs) {
+ if constexpr (std::is_integral_v<T> && std::is_signed_v<T>) {
+ // Ensure no UB for signed overflow
+ using UT = std::make_unsigned_t<T>;
+ return static_cast<T>(static_cast<UT>(lhs) + static_cast<UT>(rhs));
+ } else {
+ return lhs + rhs;
+ }
+ };
result = add_values(a.value, b.value);
}
return result;
@@ -545,27 +551,25 @@
template <typename NumberT>
utils::Result<NumberT> ConstEval::Mul(NumberT a, NumberT b) {
using T = UnwrapNumber<NumberT>;
- auto mul_values = [](T lhs, T rhs) { //
- if constexpr (std::is_integral_v<T> && std::is_signed_v<T>) {
- // For signed integrals, avoid C++ UB by multiplying as unsigned
- using UT = std::make_unsigned_t<T>;
- return static_cast<T>(static_cast<UT>(lhs) * static_cast<UT>(rhs));
- } else {
- return lhs * rhs;
- }
- };
NumberT result;
if constexpr (std::is_same_v<NumberT, AInt> || std::is_same_v<NumberT, AFloat>) {
// Check for over/underflow for abstract values
if (auto r = CheckedMul(a, b)) {
result = r->value;
} else {
- AddError("'" + std::to_string(mul_values(a.value, b.value)) +
- "' cannot be represented as '" + FriendlyName<NumberT>() + "'",
- *current_source);
+ AddError(OverflowErrorMessage(a, "*", b), *current_source);
return utils::Failure;
}
} else {
+ auto mul_values = [](T lhs, T rhs) {
+ if constexpr (std::is_integral_v<T> && std::is_signed_v<T>) {
+ // For signed integrals, avoid C++ UB by multiplying as unsigned
+ using UT = std::make_unsigned_t<T>;
+ return static_cast<T>(static_cast<UT>(lhs) * static_cast<UT>(rhs));
+ } else {
+ return lhs * rhs;
+ }
+ };
result = mul_values(a.value, b.value);
}
return result;
@@ -966,37 +970,32 @@
return r;
}
-ConstEval::ConstantResult ConstEval::OpMinus(const sem::Type* ty,
+ConstEval::ConstantResult ConstEval::OpMinus(const sem::Type*,
utils::VectorRef<const sem::Constant*> args,
const Source& source) {
auto transform = [&](const sem::Constant* c0, const sem::Constant* c1) {
auto create = [&](auto i, auto j) -> const Constant* {
using NumberT = decltype(i);
- using T = UnwrapNumber<NumberT>;
-
- auto subtract_values = [](T lhs, T rhs) {
- if constexpr (std::is_integral_v<T> && std::is_signed_v<T>) {
- // Ensure no UB for signed underflow
- using UT = std::make_unsigned_t<T>;
- return static_cast<T>(static_cast<UT>(lhs) - static_cast<UT>(rhs));
- } else {
- return lhs - rhs;
- }
- };
-
NumberT result;
if constexpr (std::is_same_v<NumberT, AInt> || std::is_same_v<NumberT, AFloat>) {
// Check for over/underflow for abstract values
if (auto r = CheckedSub(i, j)) {
result = r->value;
} else {
- AddError("'" + std::to_string(subtract_values(i.value, j.value)) +
- "' cannot be represented as '" +
- ty->FriendlyName(builder.Symbols()) + "'",
- source);
+ AddError(OverflowErrorMessage(i, "-", j), source);
return nullptr;
}
} else {
+ using T = UnwrapNumber<NumberT>;
+ auto subtract_values = [](T lhs, T rhs) {
+ if constexpr (std::is_integral_v<T> && std::is_signed_v<T>) {
+ // Ensure no UB for signed underflow
+ using UT = std::make_unsigned_t<T>;
+ return static_cast<T>(static_cast<UT>(lhs) - static_cast<UT>(rhs));
+ } else {
+ return lhs - rhs;
+ }
+ };
result = subtract_values(i.value, j.value);
}
return CreateElement(builder, c0->Type(), result);
@@ -1197,6 +1196,54 @@
return CreateComposite(builder, ty, result_mat);
}
+ConstEval::ConstantResult ConstEval::OpDivide(const sem::Type*,
+ utils::VectorRef<const sem::Constant*> args,
+ const Source& source) {
+ auto transform = [&](const sem::Constant* c0, const sem::Constant* c1) {
+ auto create = [&](auto i, auto j) -> const Constant* {
+ using NumberT = decltype(i);
+ NumberT result;
+ if constexpr (std::is_same_v<NumberT, AInt> || std::is_same_v<NumberT, AFloat>) {
+ // Check for over/underflow for abstract values
+ if (auto r = CheckedDiv(i, j)) {
+ result = r->value;
+ } else {
+ AddError(OverflowErrorMessage(i, "/", j), source);
+ return nullptr;
+ }
+ } else {
+ using T = UnwrapNumber<NumberT>;
+ auto divide_values = [](T lhs, T rhs) {
+ if constexpr (std::is_integral_v<T>) {
+ // For integers, lhs / 0 returns lhs
+ if (rhs == 0) {
+ return lhs;
+ }
+
+ if constexpr (std::is_signed_v<T>) {
+ // For signed integers, for lhs / -1, return lhs if lhs is the
+ // most negative value
+ if (rhs == -1 && lhs == std::numeric_limits<T>::min()) {
+ return lhs;
+ }
+ }
+ }
+ return lhs / rhs;
+ };
+ result = divide_values(i.value, j.value);
+ }
+ return CreateElement(builder, c0->Type(), result);
+ };
+ return Dispatch_fia_fiu32_f16(create, c0, c1);
+ };
+
+ auto r = TransformBinaryElements(builder, transform, args[0], args[1]);
+ if (builder.Diagnostics().contains_errors()) {
+ return utils::Failure;
+ }
+ return r;
+}
+
ConstEval::ConstantResult ConstEval::atan2(const sem::Type*,
utils::VectorRef<const sem::Constant*> args,
const Source&) {
diff --git a/src/tint/resolver/const_eval.h b/src/tint/resolver/const_eval.h
index 4007162..f84e28c 100644
--- a/src/tint/resolver/const_eval.h
+++ b/src/tint/resolver/const_eval.h
@@ -266,6 +266,15 @@
utils::VectorRef<const sem::Constant*> args,
const Source& source);
+ /// Divide operator '/'
+ /// @param ty the expression type
+ /// @param args the input arguments
+ /// @param source the source location of the conversion
+ /// @return the result value, or null if the value cannot be calculated
+ ConstantResult OpDivide(const sem::Type* ty,
+ utils::VectorRef<const sem::Constant*> args,
+ const Source& source);
+
////////////////////////////////////////////////////////////////////////////
// Builtins
////////////////////////////////////////////////////////////////////////////
diff --git a/src/tint/resolver/const_eval_test.cc b/src/tint/resolver/const_eval_test.cc
index a83cfa1..d21d60a 100644
--- a/src/tint/resolver/const_eval_test.cc
+++ b/src/tint/resolver/const_eval_test.cc
@@ -99,6 +99,20 @@
return std::move(v1);
}
+template <typename Vec, typename... Vecs>
+void ConcatInto(Vec& v1, Vecs&&... vs) {
+ auto total_size = v1.size() + (vs.size() + ...);
+ v1.reserve(total_size);
+ (std::move(vs.begin(), vs.end(), std::back_inserter(v1)), ...);
+}
+
+template <bool condition, typename Vec, typename... Vecs>
+void ConcatIntoIf([[maybe_unused]] Vec& v1, [[maybe_unused]] Vecs&&... vs) {
+ if constexpr (condition) {
+ ConcatInto(v1, std::forward<Vecs>(vs)...);
+ }
+}
+
using ResolverConstEvalTest = ResolverTest;
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -3071,7 +3085,7 @@
EXPECT_TYPE(value->Type(), sem->Type());
EXPECT_EQ(value->As<T>(), values.expect);
- if constexpr (IsInteger<UnwrapNumber<T>>) {
+ if constexpr (IsIntegral<UnwrapNumber<T>>) {
// Check that the constant's integer doesn't contain unexpected data in the MSBs
// that are outside of the bit-width of T.
EXPECT_EQ(value->As<AInt>(), AInt(values.expect));
@@ -3329,7 +3343,7 @@
ForEachElemPair(value, expected_value,
[&](const sem::Constant* a, const sem::Constant* b) {
EXPECT_EQ(a->As<T>(), b->As<T>());
- if constexpr (IsInteger<UnwrapNumber<T>>) {
+ if constexpr (IsIntegral<UnwrapNumber<T>>) {
// Check that the constant's integer doesn't contain unexpected
// data in the MSBs that are outside of the bit-width of T.
EXPECT_EQ(a->As<AInt>(), b->As<AInt>());
@@ -3351,7 +3365,7 @@
template <typename T>
std::vector<Case> OpAddIntCases() {
- static_assert(IsInteger<UnwrapNumber<T>>);
+ static_assert(IsIntegral<UnwrapNumber<T>>);
return {
C(T{0}, T{0}, T{0}),
C(T{1}, T{2}, T{3}),
@@ -3388,7 +3402,7 @@
template <typename T>
std::vector<Case> OpSubIntCases() {
- static_assert(IsInteger<UnwrapNumber<T>>);
+ static_assert(IsIntegral<UnwrapNumber<T>>);
return {
C(T{0}, T{0}, T{0}),
C(T{3}, T{2}, T{1}),
@@ -3510,12 +3524,69 @@
OpMulMatCases<f32>(),
OpMulMatCases<f16>()))));
+template <typename T>
+std::vector<Case> OpDivIntCases() {
+ std::vector<Case> r = {
+ C(Val(T{0}), Val(T{1}), Val(T{0})),
+ C(Val(T{1}), Val(T{1}), Val(T{1})),
+ C(Val(T{1}), Val(T{1}), Val(T{1})),
+ C(Val(T{2}), Val(T{1}), Val(T{2})),
+ C(Val(T{4}), Val(T{2}), Val(T{2})),
+ C(Val(T::Highest()), Val(T{1}), Val(T::Highest())),
+ C(Val(T::Lowest()), Val(T{1}), Val(T::Lowest())),
+ C(Val(T::Highest()), Val(T::Highest()), Val(T{1})),
+ C(Val(T{0}), Val(T::Highest()), Val(T{0})),
+ C(Val(T{0}), Val(T::Lowest()), Val(T{0})),
+ };
+ ConcatIntoIf<IsIntegral<T>>( //
+ r, std::vector<Case>{
+ // e1, when e2 is zero.
+ C(T{123}, T{0}, T{123}, true),
+ });
+ ConcatIntoIf<IsSignedIntegral<T>>( //
+ r, std::vector<Case>{
+ // e1, when e1 is the most negative value in T, and e2 is -1.
+ C(T::Smallest(), T{-1}, T::Smallest(), true),
+ });
+ return r;
+}
+
+template <typename T>
+std::vector<Case> OpDivFloatCases() {
+ return {
+ C(Val(T{0}), Val(T{1}), Val(T{0})),
+ C(Val(T{1}), Val(T{1}), Val(T{1})),
+ C(Val(T{1}), Val(T{1}), Val(T{1})),
+ C(Val(T{2}), Val(T{1}), Val(T{2})),
+ C(Val(T{4}), Val(T{2}), Val(T{2})),
+ C(Val(T::Highest()), Val(T{1}), Val(T::Highest())),
+ C(Val(T::Lowest()), Val(T{1}), Val(T::Lowest())),
+ C(Val(T::Highest()), Val(T::Highest()), Val(T{1})),
+ C(Val(T{0}), Val(T::Highest()), Val(T{0})),
+ C(Val(T{0}), Val(T::Lowest()), Val(-T{0})),
+ C(T{123}, T{0}, T::Inf(), true),
+ C(T{-123}, -T{0}, T::Inf(), true),
+ C(T{-123}, T{0}, -T::Inf(), true),
+ C(T{123}, -T{0}, -T::Inf(), true),
+ };
+}
+INSTANTIATE_TEST_SUITE_P(Div,
+ ResolverConstEvalBinaryOpTest,
+ testing::Combine( //
+ testing::Values(ast::BinaryOp::kDivide),
+ testing::ValuesIn(Concat( //
+ OpDivIntCases<AInt>(),
+ OpDivIntCases<i32>(),
+ OpDivIntCases<u32>(),
+ OpDivFloatCases<AFloat>(),
+ OpDivFloatCases<f32>(),
+ OpDivFloatCases<f16>()))));
+
// Tests for errors on overflow/underflow of binary operations with abstract numbers
struct OverflowCase {
ast::BinaryOp op;
Types lhs;
Types rhs;
- std::string overflowed_result;
};
static std::ostream& operator<<(std::ostream& o, const OverflowCase& c) {
@@ -3539,35 +3610,32 @@
},
c.lhs);
- EXPECT_THAT(r()->error(), HasSubstr("1:1 error: '" + c.overflowed_result +
- "' cannot be represented as '" + type_name + "'"));
+ EXPECT_THAT(r()->error(), HasSubstr("1:1 error: '"));
+ EXPECT_THAT(r()->error(), HasSubstr("' cannot be represented as '" + type_name + "'"));
}
INSTANTIATE_TEST_SUITE_P(
Test,
ResolverConstEvalBinaryOpTest_Overflow,
- testing::Values( //
- // scalar-scalar add
- OverflowCase{ast::BinaryOp::kAdd, Val(AInt::Highest()), Val(1_a), "-9223372036854775808"},
- OverflowCase{ast::BinaryOp::kAdd, Val(AInt::Lowest()), Val(-1_a), "9223372036854775807"},
- OverflowCase{ast::BinaryOp::kAdd, Val(AFloat::Highest()), Val(AFloat::Highest()), "inf"},
- OverflowCase{ast::BinaryOp::kAdd, Val(AFloat::Lowest()), Val(AFloat::Lowest()), "-inf"},
+ testing::Values(
+
+ // scalar-scalar add
+ OverflowCase{ast::BinaryOp::kAdd, Val(AInt::Highest()), Val(1_a)},
+ OverflowCase{ast::BinaryOp::kAdd, Val(AInt::Lowest()), Val(-1_a)},
+ OverflowCase{ast::BinaryOp::kAdd, Val(AFloat::Highest()), Val(AFloat::Highest())},
+ OverflowCase{ast::BinaryOp::kAdd, Val(AFloat::Lowest()), Val(AFloat::Lowest())},
// scalar-scalar subtract
- OverflowCase{ast::BinaryOp::kSubtract, Val(AInt::Lowest()), Val(1_a),
- "9223372036854775807"},
- OverflowCase{ast::BinaryOp::kSubtract, Val(AInt::Highest()), Val(-1_a),
- "-9223372036854775808"},
- OverflowCase{ast::BinaryOp::kSubtract, Val(AFloat::Highest()), Val(AFloat::Lowest()),
- "inf"},
- OverflowCase{ast::BinaryOp::kSubtract, Val(AFloat::Lowest()), Val(AFloat::Highest()),
- "-inf"},
+ OverflowCase{ast::BinaryOp::kSubtract, Val(AInt::Lowest()), Val(1_a)},
+ OverflowCase{ast::BinaryOp::kSubtract, Val(AInt::Highest()), Val(-1_a)},
+ OverflowCase{ast::BinaryOp::kSubtract, Val(AFloat::Highest()), Val(AFloat::Lowest())},
+ OverflowCase{ast::BinaryOp::kSubtract, Val(AFloat::Lowest()), Val(AFloat::Highest())},
// scalar-scalar multiply
- OverflowCase{ast::BinaryOp::kMultiply, Val(AInt::Highest()), Val(2_a), "-2"},
- OverflowCase{ast::BinaryOp::kMultiply, Val(AInt::Lowest()), Val(-2_a), "0"},
+ OverflowCase{ast::BinaryOp::kMultiply, Val(AInt::Highest()), Val(2_a)},
+ OverflowCase{ast::BinaryOp::kMultiply, Val(AInt::Lowest()), Val(-2_a)},
// scalar-vector multiply
- OverflowCase{ast::BinaryOp::kMultiply, Val(AInt::Highest()), Vec(2_a, 1_a), "-2"},
- OverflowCase{ast::BinaryOp::kMultiply, Val(AInt::Lowest()), Vec(-2_a, 1_a), "0"},
+ OverflowCase{ast::BinaryOp::kMultiply, Val(AInt::Highest()), Vec(2_a, 1_a)},
+ OverflowCase{ast::BinaryOp::kMultiply, Val(AInt::Lowest()), Vec(-2_a, 1_a)},
// vector-matrix multiply
@@ -3577,8 +3645,7 @@
OverflowCase{ast::BinaryOp::kMultiply, //
Vec(AFloat::Highest(), 1.0_a), //
Mat({2.0_a, 1.0_a}, //
- {1.0_a, 1.0_a}), //
- "inf"},
+ {1.0_a, 1.0_a})},
// Overflow from second multiplication of dot product of vector and matrix column 0
// i.e. (v[0] * m[0][0] + v[1] * m[0][1])
@@ -3586,8 +3653,7 @@
OverflowCase{ast::BinaryOp::kMultiply, //
Vec(1.0_a, AFloat::Highest()), //
Mat({1.0_a, 2.0_a}, //
- {1.0_a, 1.0_a}), //
- "inf"},
+ {1.0_a, 1.0_a})},
// Overflow from addition of dot product of vector and matrix column 0
// i.e. (v[0] * m[0][0] + v[1] * m[0][1])
@@ -3595,8 +3661,7 @@
OverflowCase{ast::BinaryOp::kMultiply, //
Vec(AFloat::Highest(), AFloat::Highest()), //
Mat({1.0_a, 1.0_a}, //
- {1.0_a, 1.0_a}), //
- "inf"},
+ {1.0_a, 1.0_a})},
// matrix-matrix multiply
@@ -3607,8 +3672,7 @@
Mat({AFloat::Highest(), 1.0_a}, //
{1.0_a, 1.0_a}), //
Mat({2.0_a, 1.0_a}, //
- {1.0_a, 1.0_a}), //
- "inf"},
+ {1.0_a, 1.0_a})},
// Overflow from second multiplication of dot product of lhs row 0 and rhs column 0
// i.e. m1[0][0] * m2[0][0] + m1[0][1] * m[1][0]
@@ -3617,8 +3681,7 @@
Mat({1.0_a, AFloat::Highest()}, //
{1.0_a, 1.0_a}), //
Mat({1.0_a, 1.0_a}, //
- {2.0_a, 1.0_a}), //
- "inf"},
+ {2.0_a, 1.0_a})},
// Overflow from addition of dot product of lhs row 0 and rhs column 0
// i.e. m1[0][0] * m2[0][0] + m1[0][1] * m[1][0]
@@ -3627,8 +3690,16 @@
Mat({AFloat::Highest(), 1.0_a}, //
{AFloat::Highest(), 1.0_a}), //
Mat({1.0_a, 1.0_a}, //
- {1.0_a, 1.0_a}), //
- "inf"}
+ {1.0_a, 1.0_a})},
+
+ // Divide by zero
+ OverflowCase{ast::BinaryOp::kDivide, Val(123_a), Val(0_a)},
+ OverflowCase{ast::BinaryOp::kDivide, Val(-123_a), Val(-0_a)},
+ OverflowCase{ast::BinaryOp::kDivide, Val(-123_a), Val(0_a)},
+ OverflowCase{ast::BinaryOp::kDivide, Val(123_a), Val(-0_a)},
+
+ // Most negative value divided by -1
+ OverflowCase{ast::BinaryOp::kDivide, Val(AInt::Lowest()), Val(-1_a)}
));
@@ -3636,26 +3707,29 @@
GlobalConst("c", Add(Source{{1, 1}}, Expr(AInt::Highest()), 1_a));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
- "1:1 error: '-9223372036854775808' cannot be represented as 'abstract-int'");
+ "1:1 error: '9223372036854775807 + 1' cannot be represented as 'abstract-int'");
}
TEST_F(ResolverConstEvalTest, BinaryAbstractAddUnderflow_AInt) {
GlobalConst("c", Add(Source{{1, 1}}, Expr(AInt::Lowest()), -1_a));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
- "1:1 error: '9223372036854775807' cannot be represented as 'abstract-int'");
+ "1:1 error: '-9223372036854775808 + -1' cannot be represented as 'abstract-int'");
}
TEST_F(ResolverConstEvalTest, BinaryAbstractAddOverflow_AFloat) {
GlobalConst("c", Add(Source{{1, 1}}, Expr(AFloat::Highest()), AFloat::Highest()));
EXPECT_FALSE(r()->Resolve());
- EXPECT_EQ(r()->error(), "1:1 error: 'inf' cannot be represented as 'abstract-float'");
+ EXPECT_EQ(r()->error(),
+ "1:1 error: '1.79769e+308 + 1.79769e+308' cannot be represented as 'abstract-float'");
}
TEST_F(ResolverConstEvalTest, BinaryAbstractAddUnderflow_AFloat) {
GlobalConst("c", Add(Source{{1, 1}}, Expr(AFloat::Lowest()), AFloat::Lowest()));
EXPECT_FALSE(r()->Resolve());
- EXPECT_EQ(r()->error(), "1:1 error: '-inf' cannot be represented as 'abstract-float'");
+ EXPECT_EQ(
+ r()->error(),
+ "1:1 error: '-1.79769e+308 + -1.79769e+308' cannot be represented as 'abstract-float'");
}
// Mixed AInt and AFloat args to test implicit conversion to AFloat
@@ -3756,7 +3830,7 @@
EXPECT_EQ(c.result_pos_or_neg ? Abs(actual) : actual, result);
}
- if constexpr (IsInteger<UnwrapNumber<T>>) {
+ if constexpr (IsIntegral<UnwrapNumber<T>>) {
// Check that the constant's integer doesn't contain unexpected data in the MSBs
// that are outside of the bit-width of T.
EXPECT_EQ(value->As<AInt>(), AInt(result));
diff --git a/src/tint/resolver/intrinsic_table.inl b/src/tint/resolver/intrinsic_table.inl
index e0a8ddc..675aa41 100644
--- a/src/tint/resolver/intrinsic_table.inl
+++ b/src/tint/resolver/intrinsic_table.inl
@@ -11276,48 +11276,48 @@
/* num parameters */ 2,
/* num template types */ 1,
/* num template numbers */ 0,
- /* template types */ &kTemplateTypes[15],
+ /* template types */ &kTemplateTypes[13],
/* template numbers */ &kTemplateNumbers[10],
/* parameters */ &kParameters[709],
/* return matcher indices */ &kMatcherIndices[1],
/* flags */ OverloadFlags(OverloadFlag::kIsOperator, OverloadFlag::kSupportsVertexPipeline, OverloadFlag::kSupportsFragmentPipeline, OverloadFlag::kSupportsComputePipeline),
- /* const eval */ nullptr,
+ /* const eval */ &ConstEval::OpDivide,
},
{
/* [260] */
/* num parameters */ 2,
/* num template types */ 1,
/* num template numbers */ 1,
- /* template types */ &kTemplateTypes[15],
+ /* template types */ &kTemplateTypes[13],
/* template numbers */ &kTemplateNumbers[6],
/* parameters */ &kParameters[707],
/* return matcher indices */ &kMatcherIndices[30],
/* flags */ OverloadFlags(OverloadFlag::kIsOperator, OverloadFlag::kSupportsVertexPipeline, OverloadFlag::kSupportsFragmentPipeline, OverloadFlag::kSupportsComputePipeline),
- /* const eval */ nullptr,
+ /* const eval */ &ConstEval::OpDivide,
},
{
/* [261] */
/* num parameters */ 2,
/* num template types */ 1,
/* num template numbers */ 1,
- /* template types */ &kTemplateTypes[15],
+ /* template types */ &kTemplateTypes[13],
/* template numbers */ &kTemplateNumbers[6],
/* parameters */ &kParameters[705],
/* return matcher indices */ &kMatcherIndices[30],
/* flags */ OverloadFlags(OverloadFlag::kIsOperator, OverloadFlag::kSupportsVertexPipeline, OverloadFlag::kSupportsFragmentPipeline, OverloadFlag::kSupportsComputePipeline),
- /* const eval */ nullptr,
+ /* const eval */ &ConstEval::OpDivide,
},
{
/* [262] */
/* num parameters */ 2,
/* num template types */ 1,
/* num template numbers */ 1,
- /* template types */ &kTemplateTypes[15],
+ /* template types */ &kTemplateTypes[13],
/* template numbers */ &kTemplateNumbers[6],
/* parameters */ &kParameters[781],
/* return matcher indices */ &kMatcherIndices[30],
/* flags */ OverloadFlags(OverloadFlag::kIsOperator, OverloadFlag::kSupportsVertexPipeline, OverloadFlag::kSupportsFragmentPipeline, OverloadFlag::kSupportsComputePipeline),
- /* const eval */ nullptr,
+ /* const eval */ &ConstEval::OpDivide,
},
{
/* [263] */
@@ -14644,10 +14644,10 @@
},
{
/* [3] */
- /* op /<T : fiu32_f16>(T, T) -> T */
- /* op /<T : fiu32_f16, N : num>(vec<N, T>, vec<N, T>) -> vec<N, T> */
- /* op /<T : fiu32_f16, N : num>(vec<N, T>, T) -> vec<N, T> */
- /* op /<T : fiu32_f16, N : num>(T, vec<N, T>) -> vec<N, T> */
+ /* op /<T : fia_fiu32_f16>(T, T) -> T */
+ /* op /<T : fia_fiu32_f16, N : num>(vec<N, T>, vec<N, T>) -> vec<N, T> */
+ /* op /<T : fia_fiu32_f16, N : num>(vec<N, T>, T) -> vec<N, T> */
+ /* op /<T : fia_fiu32_f16, N : num>(T, vec<N, T>) -> vec<N, T> */
/* num overloads */ 4,
/* overloads */ &kOverloads[259],
},
diff --git a/src/tint/sem/binding_point.h b/src/tint/sem/binding_point.h
index 993fb5e..b779b73 100644
--- a/src/tint/sem/binding_point.h
+++ b/src/tint/sem/binding_point.h
@@ -19,6 +19,7 @@
#include <functional>
+#include "src/tint/reflection.h"
#include "src/tint/utils/hash.h"
namespace tint::sem {
@@ -30,6 +31,9 @@
/// The `@binding` part of the binding point
uint32_t binding = 0;
+ /// Reflect the fields of this class so that it can be used by tint::ForeachField()
+ TINT_REFLECT(group, binding);
+
/// Equality operator
/// @param rhs the BindingPoint to compare against
/// @returns true if this BindingPoint is equal to `rhs`
diff --git a/src/tint/transform/add_spirv_block_attribute.cc b/src/tint/transform/add_block_attribute.cc
similarity index 67%
rename from src/tint/transform/add_spirv_block_attribute.cc
rename to src/tint/transform/add_block_attribute.cc
index 25abdce..619083d 100644
--- a/src/tint/transform/add_spirv_block_attribute.cc
+++ b/src/tint/transform/add_block_attribute.cc
@@ -12,8 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-#include "src/tint/transform/add_spirv_block_attribute.h"
+#include "src/tint/transform/add_block_attribute.h"
+#include <unordered_set>
#include <utility>
#include "src/tint/program_builder.h"
@@ -21,16 +22,29 @@
#include "src/tint/utils/hashmap.h"
#include "src/tint/utils/hashset.h"
-TINT_INSTANTIATE_TYPEINFO(tint::transform::AddSpirvBlockAttribute);
-TINT_INSTANTIATE_TYPEINFO(tint::transform::AddSpirvBlockAttribute::SpirvBlockAttribute);
+TINT_INSTANTIATE_TYPEINFO(tint::transform::AddBlockAttribute);
+TINT_INSTANTIATE_TYPEINFO(tint::transform::AddBlockAttribute::BlockAttribute);
namespace tint::transform {
-AddSpirvBlockAttribute::AddSpirvBlockAttribute() = default;
+namespace {
-AddSpirvBlockAttribute::~AddSpirvBlockAttribute() = default;
+bool IsUsedAsNonBuffer(const std::unordered_set<tint::ast::StorageClass>& uses) {
+ for (auto use : uses) {
+ if (!ast::IsHostShareable(use)) {
+ return true;
+ }
+ }
+ return false;
+}
-void AddSpirvBlockAttribute::Run(CloneContext& ctx, const DataMap&, DataMap&) const {
+} // namespace
+
+AddBlockAttribute::AddBlockAttribute() = default;
+
+AddBlockAttribute::~AddBlockAttribute() = default;
+
+void AddBlockAttribute::Run(CloneContext& ctx, const DataMap&, DataMap&) const {
auto& sem = ctx.src->Sem();
// Collect the set of structs that are nested in other types.
@@ -66,8 +80,10 @@
auto* ty = var->Type()->UnwrapRef();
auto* str = ty->As<sem::Struct>();
- bool needs_wrapping = !str || // Type is not a structure
- nested_structs.Contains(str); // Structure is nested by another type
+ bool needs_wrapping =
+ !str || // Type is not a structure
+ nested_structs.Contains(str) || // Structure is nested by another type
+ IsUsedAsNonBuffer(str->StorageClassUsage()); // Structure is used as a non-buffer usage
if (needs_wrapping) {
const char* kMemberName = "inner";
@@ -75,8 +91,8 @@
// This is a non-struct or a struct that is nested somewhere else, so we
// need to wrap it first.
auto* wrapper = wrapper_structs.GetOrCreate(ty, [&] {
- auto* block = ctx.dst->ASTNodes().Create<SpirvBlockAttribute>(
- ctx.dst->ID(), ctx.dst->AllocateNodeID());
+ auto* block = ctx.dst->ASTNodes().Create<BlockAttribute>(ctx.dst->ID(),
+ ctx.dst->AllocateNodeID());
auto wrapper_name = ctx.src->Symbols().NameFor(global->symbol) + "_block";
auto* ret = ctx.dst->create<ast::Struct>(
ctx.dst->Symbols().New(wrapper_name),
@@ -95,8 +111,8 @@
}
} else {
// Add a block attribute to this struct directly.
- auto* block = ctx.dst->ASTNodes().Create<SpirvBlockAttribute>(
- ctx.dst->ID(), ctx.dst->AllocateNodeID());
+ auto* block = ctx.dst->ASTNodes().Create<BlockAttribute>(ctx.dst->ID(),
+ ctx.dst->AllocateNodeID());
ctx.InsertFront(str->Declaration()->attributes, block);
}
}
@@ -104,16 +120,16 @@
ctx.Clone();
}
-AddSpirvBlockAttribute::SpirvBlockAttribute::SpirvBlockAttribute(ProgramID pid, ast::NodeID nid)
+AddBlockAttribute::BlockAttribute::BlockAttribute(ProgramID pid, ast::NodeID nid)
: Base(pid, nid) {}
-AddSpirvBlockAttribute::SpirvBlockAttribute::~SpirvBlockAttribute() = default;
-std::string AddSpirvBlockAttribute::SpirvBlockAttribute::InternalName() const {
- return "spirv_block";
+AddBlockAttribute::BlockAttribute::~BlockAttribute() = default;
+std::string AddBlockAttribute::BlockAttribute::InternalName() const {
+ return "block";
}
-const AddSpirvBlockAttribute::SpirvBlockAttribute*
-AddSpirvBlockAttribute::SpirvBlockAttribute::Clone(CloneContext* ctx) const {
- return ctx->dst->ASTNodes().Create<AddSpirvBlockAttribute::SpirvBlockAttribute>(
+const AddBlockAttribute::BlockAttribute* AddBlockAttribute::BlockAttribute::Clone(
+ CloneContext* ctx) const {
+ return ctx->dst->ASTNodes().Create<AddBlockAttribute::BlockAttribute>(
ctx->dst->ID(), ctx->dst->AllocateNodeID());
}
diff --git a/src/tint/transform/add_spirv_block_attribute.h b/src/tint/transform/add_block_attribute.h
similarity index 68%
rename from src/tint/transform/add_spirv_block_attribute.h
rename to src/tint/transform/add_block_attribute.h
index 51409c8..69dfab5 100644
--- a/src/tint/transform/add_spirv_block_attribute.h
+++ b/src/tint/transform/add_block_attribute.h
@@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-#ifndef SRC_TINT_TRANSFORM_ADD_SPIRV_BLOCK_ATTRIBUTE_H_
-#define SRC_TINT_TRANSFORM_ADD_SPIRV_BLOCK_ATTRIBUTE_H_
+#ifndef SRC_TINT_TRANSFORM_ADD_BLOCK_ATTRIBUTE_H_
+#define SRC_TINT_TRANSFORM_ADD_BLOCK_ATTRIBUTE_H_
#include <string>
@@ -22,23 +22,23 @@
namespace tint::transform {
-/// AddSpirvBlockAttribute is a transform that adds an
-/// `@internal(spirv_block)` attribute to any structure that is used as the
+/// AddBlockAttribute is a transform that adds an
+/// `@internal(block)` attribute to any structure that is used as the
/// store type of a buffer. If that structure is nested inside another structure
/// or an array, then it is wrapped inside another structure which gets the
-/// `@internal(spirv_block)` attribute instead.
-class AddSpirvBlockAttribute final : public Castable<AddSpirvBlockAttribute, Transform> {
+/// `@internal(block)` attribute instead.
+class AddBlockAttribute final : public Castable<AddBlockAttribute, Transform> {
public:
- /// SpirvBlockAttribute is an InternalAttribute that is used to decorate a
- // structure that needs a SPIR-V block attribute.
- class SpirvBlockAttribute final : public Castable<SpirvBlockAttribute, ast::InternalAttribute> {
+ /// BlockAttribute is an InternalAttribute that is used to decorate a
+ // structure that is used as a buffer in SPIR-V or GLSL.
+ class BlockAttribute final : public Castable<BlockAttribute, ast::InternalAttribute> {
public:
/// Constructor
/// @param program_id the identifier of the program that owns this node
/// @param nid the unique node identifier
- SpirvBlockAttribute(ProgramID program_id, ast::NodeID nid);
+ BlockAttribute(ProgramID program_id, ast::NodeID nid);
/// Destructor
- ~SpirvBlockAttribute() override;
+ ~BlockAttribute() override;
/// @return a short description of the internal attribute which will be
/// displayed as `@internal(<name>)`
@@ -47,14 +47,14 @@
/// Performs a deep clone of this object using the CloneContext `ctx`.
/// @param ctx the clone context
/// @return the newly cloned object
- const SpirvBlockAttribute* Clone(CloneContext* ctx) const override;
+ const BlockAttribute* Clone(CloneContext* ctx) const override;
};
/// Constructor
- AddSpirvBlockAttribute();
+ AddBlockAttribute();
/// Destructor
- ~AddSpirvBlockAttribute() override;
+ ~AddBlockAttribute() override;
protected:
/// Runs the transform using the CloneContext built for transforming a
@@ -68,4 +68,4 @@
} // namespace tint::transform
-#endif // SRC_TINT_TRANSFORM_ADD_SPIRV_BLOCK_ATTRIBUTE_H_
+#endif // SRC_TINT_TRANSFORM_ADD_BLOCK_ATTRIBUTE_H_
diff --git a/src/tint/transform/add_spirv_block_attribute_test.cc b/src/tint/transform/add_block_attribute_test.cc
similarity index 65%
rename from src/tint/transform/add_spirv_block_attribute_test.cc
rename to src/tint/transform/add_block_attribute_test.cc
index 62abae3..7bb5efc 100644
--- a/src/tint/transform/add_spirv_block_attribute_test.cc
+++ b/src/tint/transform/add_block_attribute_test.cc
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-#include "src/tint/transform/add_spirv_block_attribute.h"
+#include "src/tint/transform/add_block_attribute.h"
#include <memory>
#include <utility>
@@ -22,18 +22,18 @@
namespace tint::transform {
namespace {
-using AddSpirvBlockAttributeTest = TransformTest;
+using AddBlockAttributeTest = TransformTest;
-TEST_F(AddSpirvBlockAttributeTest, EmptyModule) {
+TEST_F(AddBlockAttributeTest, EmptyModule) {
auto* src = "";
auto* expect = "";
- auto got = Run<AddSpirvBlockAttribute>(src);
+ auto got = Run<AddBlockAttribute>(src);
EXPECT_EQ(expect, str(got));
}
-TEST_F(AddSpirvBlockAttributeTest, Noop_UsedForPrivateVar) {
+TEST_F(AddBlockAttributeTest, Noop_UsedForPrivateVar) {
auto* src = R"(
struct S {
f : f32,
@@ -48,12 +48,12 @@
)";
auto* expect = src;
- auto got = Run<AddSpirvBlockAttribute>(src);
+ auto got = Run<AddBlockAttribute>(src);
EXPECT_EQ(expect, str(got));
}
-TEST_F(AddSpirvBlockAttributeTest, Noop_UsedForShaderIO) {
+TEST_F(AddBlockAttributeTest, Noop_UsedForShaderIO) {
auto* src = R"(
struct S {
@location(0)
@@ -67,12 +67,12 @@
)";
auto* expect = src;
- auto got = Run<AddSpirvBlockAttribute>(src);
+ auto got = Run<AddBlockAttribute>(src);
EXPECT_EQ(expect, str(got));
}
-TEST_F(AddSpirvBlockAttributeTest, BasicScalar) {
+TEST_F(AddBlockAttributeTest, BasicScalar) {
auto* src = R"(
@group(0) @binding(0)
var<uniform> u : f32;
@@ -83,7 +83,7 @@
}
)";
auto* expect = R"(
-@internal(spirv_block)
+@internal(block)
struct u_block {
inner : f32,
}
@@ -96,12 +96,12 @@
}
)";
- auto got = Run<AddSpirvBlockAttribute>(src);
+ auto got = Run<AddBlockAttribute>(src);
EXPECT_EQ(expect, str(got));
}
-TEST_F(AddSpirvBlockAttributeTest, BasicArray) {
+TEST_F(AddBlockAttributeTest, BasicArray) {
auto* src = R"(
@group(0) @binding(0)
var<uniform> u : array<vec4<f32>, 4u>;
@@ -112,7 +112,7 @@
}
)";
auto* expect = R"(
-@internal(spirv_block)
+@internal(block)
struct u_block {
inner : array<vec4<f32>, 4u>,
}
@@ -125,12 +125,12 @@
}
)";
- auto got = Run<AddSpirvBlockAttribute>(src);
+ auto got = Run<AddBlockAttribute>(src);
EXPECT_EQ(expect, str(got));
}
-TEST_F(AddSpirvBlockAttributeTest, BasicArray_Alias) {
+TEST_F(AddBlockAttributeTest, BasicArray_Alias) {
auto* src = R"(
type Numbers = array<vec4<f32>, 4u>;
@@ -145,7 +145,7 @@
auto* expect = R"(
type Numbers = array<vec4<f32>, 4u>;
-@internal(spirv_block)
+@internal(block)
struct u_block {
inner : array<vec4<f32>, 4u>,
}
@@ -158,12 +158,12 @@
}
)";
- auto got = Run<AddSpirvBlockAttribute>(src);
+ auto got = Run<AddBlockAttribute>(src);
EXPECT_EQ(expect, str(got));
}
-TEST_F(AddSpirvBlockAttributeTest, BasicStruct_AccessRoot) {
+TEST_F(AddBlockAttributeTest, BasicStruct_AccessRoot) {
auto* src = R"(
struct S {
f : f32,
@@ -178,25 +178,29 @@
}
)";
auto* expect = R"(
-@internal(spirv_block)
struct S {
f : f32,
}
-@group(0) @binding(0) var<uniform> u : S;
+@internal(block)
+struct u_block {
+ inner : S,
+}
+
+@group(0) @binding(0) var<uniform> u : u_block;
@fragment
fn main() {
- let f = u;
+ let f = u.inner;
}
)";
- auto got = Run<AddSpirvBlockAttribute>(src);
+ auto got = Run<AddBlockAttribute>(src);
EXPECT_EQ(expect, str(got));
}
-TEST_F(AddSpirvBlockAttributeTest, BasicStruct_AccessField) {
+TEST_F(AddBlockAttributeTest, BasicStruct_AccessField) {
auto* src = R"(
struct S {
f : f32,
@@ -211,7 +215,7 @@
}
)";
auto* expect = R"(
-@internal(spirv_block)
+@internal(block)
struct S {
f : f32,
}
@@ -224,12 +228,12 @@
}
)";
- auto got = Run<AddSpirvBlockAttribute>(src);
+ auto got = Run<AddBlockAttribute>(src);
EXPECT_EQ(expect, str(got));
}
-TEST_F(AddSpirvBlockAttributeTest, BasicScalar_PushConstant) {
+TEST_F(AddBlockAttributeTest, BasicScalar_PushConstant) {
auto* src = R"(
enable chromium_experimental_push_constant;
var<push_constant> u : f32;
@@ -242,7 +246,7 @@
auto* expect = R"(
enable chromium_experimental_push_constant;
-@internal(spirv_block)
+@internal(block)
struct u_block {
inner : f32,
}
@@ -255,12 +259,12 @@
}
)";
- auto got = Run<AddSpirvBlockAttribute>(src);
+ auto got = Run<AddBlockAttribute>(src);
EXPECT_EQ(expect, str(got));
}
-TEST_F(AddSpirvBlockAttributeTest, BasicStruct_PushConstant) {
+TEST_F(AddBlockAttributeTest, BasicStruct_PushConstant) {
auto* src = R"(
enable chromium_experimental_push_constant;
struct S {
@@ -276,7 +280,7 @@
auto* expect = R"(
enable chromium_experimental_push_constant;
-@internal(spirv_block)
+@internal(block)
struct S {
f : f32,
}
@@ -289,12 +293,12 @@
}
)";
- auto got = Run<AddSpirvBlockAttribute>(src);
+ auto got = Run<AddBlockAttribute>(src);
EXPECT_EQ(expect, str(got));
}
-TEST_F(AddSpirvBlockAttributeTest, Nested_OuterBuffer_InnerNotBuffer) {
+TEST_F(AddBlockAttributeTest, Nested_OuterBuffer_InnerNotBuffer) {
auto* src = R"(
struct Inner {
f : f32,
@@ -317,7 +321,7 @@
f : f32,
}
-@internal(spirv_block)
+@internal(block)
struct Outer {
i : Inner,
}
@@ -330,12 +334,12 @@
}
)";
- auto got = Run<AddSpirvBlockAttribute>(src);
+ auto got = Run<AddBlockAttribute>(src);
EXPECT_EQ(expect, str(got));
}
-TEST_F(AddSpirvBlockAttributeTest, Nested_OuterBuffer_InnerBuffer) {
+TEST_F(AddBlockAttributeTest, Nested_OuterBuffer_InnerBuffer) {
auto* src = R"(
struct Inner {
f : f32,
@@ -362,14 +366,14 @@
f : f32,
}
-@internal(spirv_block)
+@internal(block)
struct Outer {
i : Inner,
}
@group(0) @binding(0) var<uniform> u0 : Outer;
-@internal(spirv_block)
+@internal(block)
struct u1_block {
inner : Inner,
}
@@ -383,12 +387,12 @@
}
)";
- auto got = Run<AddSpirvBlockAttribute>(src);
+ auto got = Run<AddBlockAttribute>(src);
EXPECT_EQ(expect, str(got));
}
-TEST_F(AddSpirvBlockAttributeTest, Nested_OuterNotBuffer_InnerBuffer) {
+TEST_F(AddBlockAttributeTest, Nested_OuterNotBuffer_InnerBuffer) {
auto* src = R"(
struct Inner {
f : f32,
@@ -420,7 +424,7 @@
var<private> p : Outer;
-@internal(spirv_block)
+@internal(block)
struct u_block {
inner : Inner,
}
@@ -434,12 +438,12 @@
}
)";
- auto got = Run<AddSpirvBlockAttribute>(src);
+ auto got = Run<AddBlockAttribute>(src);
EXPECT_EQ(expect, str(got));
}
-TEST_F(AddSpirvBlockAttributeTest, Nested_InnerUsedForMultipleBuffers) {
+TEST_F(AddBlockAttributeTest, Nested_InnerUsedForMultipleBuffers) {
auto* src = R"(
struct Inner {
f : f32,
@@ -470,14 +474,14 @@
f : f32,
}
-@internal(spirv_block)
+@internal(block)
struct S {
i : Inner,
}
@group(0) @binding(0) var<uniform> u0 : S;
-@internal(spirv_block)
+@internal(block)
struct u1_block {
inner : Inner,
}
@@ -494,12 +498,12 @@
}
)";
- auto got = Run<AddSpirvBlockAttribute>(src);
+ auto got = Run<AddBlockAttribute>(src);
EXPECT_EQ(expect, str(got));
}
-TEST_F(AddSpirvBlockAttributeTest, StructInArray) {
+TEST_F(AddBlockAttributeTest, StructInArray) {
auto* src = R"(
struct S {
f : f32,
@@ -519,7 +523,7 @@
f : f32,
}
-@internal(spirv_block)
+@internal(block)
struct u_block {
inner : S,
}
@@ -533,12 +537,12 @@
}
)";
- auto got = Run<AddSpirvBlockAttribute>(src);
+ auto got = Run<AddBlockAttribute>(src);
EXPECT_EQ(expect, str(got));
}
-TEST_F(AddSpirvBlockAttributeTest, StructInArray_MultipleBuffers) {
+TEST_F(AddBlockAttributeTest, StructInArray_MultipleBuffers) {
auto* src = R"(
struct S {
f : f32,
@@ -562,7 +566,7 @@
f : f32,
}
-@internal(spirv_block)
+@internal(block)
struct u0_block {
inner : S,
}
@@ -579,12 +583,12 @@
}
)";
- auto got = Run<AddSpirvBlockAttribute>(src);
+ auto got = Run<AddBlockAttribute>(src);
EXPECT_EQ(expect, str(got));
}
-TEST_F(AddSpirvBlockAttributeTest, Aliases_Nested_OuterBuffer_InnerBuffer) {
+TEST_F(AddBlockAttributeTest, Aliases_Nested_OuterBuffer_InnerBuffer) {
auto* src = R"(
struct Inner {
f : f32,
@@ -617,7 +621,7 @@
type MyInner = Inner;
-@internal(spirv_block)
+@internal(block)
struct Outer {
i : MyInner,
}
@@ -626,7 +630,7 @@
@group(0) @binding(0) var<uniform> u0 : MyOuter;
-@internal(spirv_block)
+@internal(block)
struct u1_block {
inner : Inner,
}
@@ -640,12 +644,12 @@
}
)";
- auto got = Run<AddSpirvBlockAttribute>(src);
+ auto got = Run<AddBlockAttribute>(src);
EXPECT_EQ(expect, str(got));
}
-TEST_F(AddSpirvBlockAttributeTest, Aliases_Nested_OuterBuffer_InnerBuffer_OutOfOrder) {
+TEST_F(AddBlockAttributeTest, Aliases_Nested_OuterBuffer_InnerBuffer_OutOfOrder) {
auto* src = R"(
@fragment
fn main() {
@@ -678,7 +682,7 @@
let f1 = u1.inner.f;
}
-@internal(spirv_block)
+@internal(block)
struct u1_block {
inner : Inner,
}
@@ -691,7 +695,7 @@
type MyOuter = Outer;
-@internal(spirv_block)
+@internal(block)
struct Outer {
i : MyInner,
}
@@ -701,7 +705,147 @@
}
)";
- auto got = Run<AddSpirvBlockAttribute>(src);
+ auto got = Run<AddBlockAttribute>(src);
+
+ EXPECT_EQ(expect, str(got));
+}
+
+TEST_F(AddBlockAttributeTest, UniformAndPrivateUsages) {
+ auto* src = R"(
+struct S {
+ f : f32,
+}
+
+@group(0) @binding(0) var<uniform> u : S;
+
+var<private> p : S;
+
+@fragment
+fn main() {
+ p = u;
+}
+)";
+ auto* expect = R"(
+struct S {
+ f : f32,
+}
+
+@internal(block)
+struct u_block {
+ inner : S,
+}
+
+@group(0) @binding(0) var<uniform> u : u_block;
+
+var<private> p : S;
+
+@fragment
+fn main() {
+ p = u.inner;
+}
+)";
+
+ auto got = Run<AddBlockAttribute>(src);
+
+ EXPECT_EQ(expect, str(got));
+}
+
+TEST_F(AddBlockAttributeTest, StorageAndPrivateUsages) {
+ auto* src = R"(
+struct S {
+ f : f32,
+}
+
+@group(0) @binding(0) var<storage, read_write> s : S;
+
+var<private> p : S;
+
+@fragment
+fn main() {
+ p = s;
+ p.f = 1234.0;
+ s = p;
+}
+)";
+ auto* expect = R"(
+struct S {
+ f : f32,
+}
+
+@internal(block)
+struct s_block {
+ inner : S,
+}
+
+@group(0) @binding(0) var<storage, read_write> s : s_block;
+
+var<private> p : S;
+
+@fragment
+fn main() {
+ p = s.inner;
+ p.f = 1234.0;
+ s.inner = p;
+}
+)";
+
+ auto got = Run<AddBlockAttribute>(src);
+
+ EXPECT_EQ(expect, str(got));
+}
+
+TEST_F(AddBlockAttributeTest, StorageAndUniformUsages) {
+ auto* src = R"(
+struct S {
+ f : f32,
+}
+
+@group(0) @binding(0) var<uniform> u : S;
+
+@group(0) @binding(1) var<storage, read_write> s : S;
+
+@fragment
+fn main() {
+ s = u;
+}
+)";
+ auto* expect = R"(
+@internal(block) @internal(block)
+struct S {
+ f : f32,
+}
+
+@group(0) @binding(0) var<uniform> u : S;
+
+@group(0) @binding(1) var<storage, read_write> s : S;
+
+@fragment
+fn main() {
+ s = u;
+}
+)";
+
+ auto got = Run<AddBlockAttribute>(src);
+
+ EXPECT_EQ(expect, str(got));
+}
+
+TEST_F(AddBlockAttributeTest, PrivateUsageOnly) {
+ auto* src = R"(
+struct S {
+ f : f32,
+}
+
+var<private> p : S;
+
+@fragment
+fn main() {
+ p.f = 4321.0f;
+}
+)";
+ auto* expect = src;
+
+ auto got = Run<AddBlockAttribute>(src);
EXPECT_EQ(expect, str(got));
}
diff --git a/src/tint/transform/fold_trivial_single_use_lets.cc b/src/tint/transform/fold_trivial_single_use_lets.cc
index 8a949ec..4d6a2a5 100644
--- a/src/tint/transform/fold_trivial_single_use_lets.cc
+++ b/src/tint/transform/fold_trivial_single_use_lets.cc
@@ -17,6 +17,7 @@
#include "src/tint/program_builder.h"
#include "src/tint/sem/block_statement.h"
#include "src/tint/sem/function.h"
+#include "src/tint/sem/materialize.h"
#include "src/tint/sem/statement.h"
#include "src/tint/sem/variable.h"
#include "src/tint/utils/scoped_assignment.h"
@@ -26,6 +27,8 @@
namespace tint::transform {
namespace {
+/// @returns a @p stmt cast to a ast::VariableDeclStatement iff the statement is a let declaration,
+/// with an initializer that's an identifier or literal expression.
const ast::VariableDeclStatement* AsTrivialLetDecl(const ast::Statement* stmt) {
auto* var_decl = stmt->As<ast::VariableDeclStatement>();
if (!var_decl) {
@@ -50,10 +53,12 @@
void FoldTrivialSingleUseLets::Run(CloneContext& ctx, const DataMap&, DataMap&) const {
for (auto* node : ctx.src->ASTNodes().Objects()) {
+ // For each statement in each block of the entire program...
if (auto* block = node->As<ast::BlockStatement>()) {
auto& stmts = block->statements;
for (size_t stmt_idx = 0; stmt_idx < stmts.Length(); stmt_idx++) {
auto* stmt = stmts[stmt_idx];
+ // Is the statement a trivial let declaration with a single user?
if (auto* let_decl = AsTrivialLetDecl(stmt)) {
auto* let = let_decl->variable;
auto* sem_let = ctx.src->Sem().Get(let);
@@ -65,11 +70,23 @@
auto* user = users[0];
auto* user_stmt = user->Stmt()->Declaration();
+ // Scan forward to find the statement of use. If there's a statement between the
+ // declaration and the use, then we cannot safely fold.
for (size_t i = stmt_idx; i < stmts.Length(); i++) {
if (user_stmt == stmts[i]) {
auto* user_expr = user->Declaration();
ctx.Remove(stmts, let_decl);
- ctx.Replace(user_expr, ctx.Clone(let->constructor));
+ auto* initializer = ctx.Clone(let->constructor);
+ if (auto* materialize =
+ sem_let->Constructor()->As<sem::Materialize>()) {
+ // The let initializer was an abstract numeric that was implicitly
+ // materialized to a concrete type. As we're inlining the
+ // initializer into the use, we need to make this cast explicit,
+ // otherwise we'll have a different type for the substitution.
+ auto* concrete_ty = CreateASTTypeFor(ctx, materialize->Type());
+ initializer = ctx.dst->Construct(concrete_ty, initializer);
+ }
+ ctx.Replace(user_expr, initializer);
}
if (!AsTrivialLetDecl(stmts[i])) {
// Stop if we hit a statement that isn't the single use of the
diff --git a/src/tint/transform/fold_trivial_single_use_lets_test.cc b/src/tint/transform/fold_trivial_single_use_lets_test.cc
index 00159e9..80e9841 100644
--- a/src/tint/transform/fold_trivial_single_use_lets_test.cc
+++ b/src/tint/transform/fold_trivial_single_use_lets_test.cc
@@ -30,7 +30,26 @@
EXPECT_EQ(expect, str(got));
}
-TEST_F(FoldTrivialSingleUseLetsTest, Single) {
+TEST_F(FoldTrivialSingleUseLetsTest, Single_Concrete) {
+ auto* src = R"(
+fn f() {
+ let x = 1i;
+ _ = x;
+}
+)";
+
+ auto* expect = R"(
+fn f() {
+ _ = 1i;
+}
+)";
+
+ auto got = Run<FoldTrivialSingleUseLets>(src);
+
+ EXPECT_EQ(expect, str(got));
+}
+
+TEST_F(FoldTrivialSingleUseLetsTest, Single_Abstract) {
auto* src = R"(
fn f() {
let x = 1;
@@ -40,7 +59,7 @@
auto* expect = R"(
fn f() {
- _ = 1;
+ _ = i32(1);
}
)";
@@ -49,7 +68,28 @@
EXPECT_EQ(expect, str(got));
}
-TEST_F(FoldTrivialSingleUseLetsTest, Multiple) {
+TEST_F(FoldTrivialSingleUseLetsTest, Multiple_Concrete) {
+ auto* src = R"(
+fn f() {
+ let x = 1u;
+ let y = 2u;
+ let z = 3u;
+ _ = x + y + z;
+}
+)";
+
+ auto* expect = R"(
+fn f() {
+ _ = ((1u + 2u) + 3u);
+}
+)";
+
+ auto got = Run<FoldTrivialSingleUseLets>(src);
+
+ EXPECT_EQ(expect, str(got));
+}
+
+TEST_F(FoldTrivialSingleUseLetsTest, Multiple_Abstract) {
auto* src = R"(
fn f() {
let x = 1;
@@ -61,7 +101,7 @@
auto* expect = R"(
fn f() {
- _ = ((1 + 2) + 3);
+ _ = ((i32(1) + i32(2)) + i32(3));
}
)";
@@ -70,7 +110,28 @@
EXPECT_EQ(expect, str(got));
}
-TEST_F(FoldTrivialSingleUseLetsTest, Chained) {
+TEST_F(FoldTrivialSingleUseLetsTest, Chained_Concrete) {
+ auto* src = R"(
+fn f() {
+ let x = 1i;
+ let y = x;
+ let z = y;
+ _ = z;
+}
+)";
+
+ auto* expect = R"(
+fn f() {
+ _ = 1i;
+}
+)";
+
+ auto got = Run<FoldTrivialSingleUseLets>(src);
+
+ EXPECT_EQ(expect, str(got));
+}
+
+TEST_F(FoldTrivialSingleUseLetsTest, Chained_Abstract) {
auto* src = R"(
fn f() {
let x = 1;
@@ -82,7 +143,7 @@
auto* expect = R"(
fn f() {
- _ = 1;
+ _ = i32(1);
}
)";
@@ -93,13 +154,13 @@
TEST_F(FoldTrivialSingleUseLetsTest, NoFold_NonTrivialLet) {
auto* src = R"(
-fn function_with_posssible_side_effect() -> i32 {
+fn function_with_possible_side_effect() -> i32 {
return 1;
}
fn f() {
let x = 1;
- let y = function_with_posssible_side_effect();
+ let y = function_with_possible_side_effect();
_ = (x + y);
}
)";
@@ -115,11 +176,11 @@
auto* src = R"(
fn f() {
let x = 1;
- let y = function_with_posssible_side_effect();
+ let y = function_with_possible_side_effect();
_ = (x + y);
}
-fn function_with_posssible_side_effect() -> i32 {
+fn function_with_possible_side_effect() -> i32 {
return 1;
}
)";
diff --git a/src/tint/transform/multiplanar_external_texture.h b/src/tint/transform/multiplanar_external_texture.h
index afd15a1..a10fed4 100644
--- a/src/tint/transform/multiplanar_external_texture.h
+++ b/src/tint/transform/multiplanar_external_texture.h
@@ -28,30 +28,33 @@
/// BindingPoint is an alias to sem::BindingPoint
using BindingPoint = sem::BindingPoint;
-/// This struct identifies the binding groups and locations for new bindings to
-/// use when transforming a texture_external instance.
-struct BindingPoints {
- /// The desired binding location of the texture_2d representing plane #1 when
- /// a texture_external binding is expanded.
- BindingPoint plane_1;
- /// The desired binding location of the ExternalTextureParams uniform when a
- /// texture_external binding is expanded.
- BindingPoint params;
-};
-
/// Within the MultiplanarExternalTexture transform, each instance of a
/// texture_external binding is unpacked into two texture_2d<f32> bindings
/// representing two possible planes of a texture and a uniform buffer binding
/// representing a struct of parameters. Calls to textureLoad or
/// textureSampleLevel that contain a texture_external parameter will be
/// transformed into a newly generated version of the function, which can
-/// perform the desired operation on a single RGBA plane or on seperate Y and UV
+/// perform the desired operation on a single RGBA plane or on separate Y and UV
/// planes, and do colorspace conversions including yuv->rgb conversion, gamma
/// decoding, gamut conversion, and gamma encoding steps. Specifically
// for BT.709 to SRGB conversion, it takes the fast path only doing the yuv->rgb
// step and skipping all other steps.
class MultiplanarExternalTexture final : public Castable<MultiplanarExternalTexture, Transform> {
public:
+ /// This struct identifies the binding groups and locations for new bindings to
+ /// use when transforming a texture_external instance.
+ struct BindingPoints {
+ /// The desired binding location of the texture_2d representing plane #1 when
+ /// a texture_external binding is expanded.
+ BindingPoint plane_1;
+ /// The desired binding location of the ExternalTextureParams uniform when a
+ /// texture_external binding is expanded.
+ BindingPoint params;
+
+ /// Reflect the fields of this class so that it can be used by tint::ForeachField()
+ TINT_REFLECT(plane_1, params);
+ };
+
/// BindingsMap is a map where the key is the binding location of a
/// texture_external and the value is a struct containing the desired
/// locations for new bindings expanded from the texture_external instance.
diff --git a/src/tint/transform/vertex_pulling.h b/src/tint/transform/vertex_pulling.h
index 92eb627..255a49a 100644
--- a/src/tint/transform/vertex_pulling.h
+++ b/src/tint/transform/vertex_pulling.h
@@ -20,6 +20,7 @@
#include <unordered_map>
#include <vector>
+#include "src/tint/reflection.h"
#include "src/tint/transform/transform.h"
namespace tint::transform {
@@ -72,6 +73,9 @@
uint32_t offset;
/// The shader location used for the attribute
uint32_t shader_location;
+
+ /// Reflect the fields of this class so that it can be used by tint::ForeachField()
+ TINT_REFLECT(format, offset, shader_location);
};
/// Describes a buffer containing multiple vertex attributes
@@ -102,6 +106,9 @@
VertexStepMode step_mode = VertexStepMode::kVertex;
/// The vertex attributes
std::vector<VertexAttributeDescriptor> attributes;
+
+ /// Reflect the fields of this class so that it can be used by tint::ForeachField()
+ TINT_REFLECT(array_stride, step_mode, attributes);
};
/// Describes vertex state, which consists of many buffers containing vertex
@@ -154,6 +161,9 @@
/// The "group" we will put all our vertex buffers into (as storage buffers)
/// Default to 4 as it is past the limits of user-accessible groups
uint32_t pulling_group = 4u;
+
+ /// Reflect the fields of this class so that it can be used by tint::ForeachField()
+ TINT_REFLECT(entry_point_name, vertex_state, pulling_group);
};
/// Constructor
diff --git a/src/tint/utils/foreach_macro.h b/src/tint/utils/foreach_macro.h
new file mode 100644
index 0000000..c76bfb3
--- /dev/null
+++ b/src/tint/utils/foreach_macro.h
@@ -0,0 +1,91 @@
+// Copyright 2022 The Tint Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef SRC_TINT_UTILS_FOREACH_MACRO_H_
+#define SRC_TINT_UTILS_FOREACH_MACRO_H_
+
+// Macro magic to perform macro variadic dispatch.
+// See:
+// https://renenyffenegger.ch/notes/development/languages/C-C-plus-plus/preprocessor/macros/__VA_ARGS__/count-arguments
+// Note, this doesn't attempt to use the ##__VA_ARGS__ trick to handle 0
+
+// Helper macro to force expanding __VA_ARGS__ to satisfy MSVC compiler.
+#define TINT_MSVC_EXPAND_BUG(X) X
+
+/// TINT_COUNT_ARGUMENTS_NTH_ARG is used by TINT_COUNT_ARGUMENTS to get the number of arguments in a
+/// variadic macro call.
+#define TINT_COUNT_ARGUMENTS_NTH_ARG(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, \
+ _15, _16, N, ...) \
+ N
+
+/// TINT_COUNT_ARGUMENTS evaluates to the number of arguments passed to the macro
+#define TINT_COUNT_ARGUMENTS(...) \
+ TINT_MSVC_EXPAND_BUG(TINT_COUNT_ARGUMENTS_NTH_ARG(__VA_ARGS__, 16, 15, 14, 13, 12, 11, 10, 9, \
+ 8, 7, 6, 5, 4, 3, 2, 1, 0))
+
+// Correctness checks.
+static_assert(1 == TINT_COUNT_ARGUMENTS(a), "TINT_COUNT_ARGUMENTS broken");
+static_assert(2 == TINT_COUNT_ARGUMENTS(a, b), "TINT_COUNT_ARGUMENTS broken");
+static_assert(3 == TINT_COUNT_ARGUMENTS(a, b, c), "TINT_COUNT_ARGUMENTS broken");
+
+/// TINT_FOREACH calls CB with each of the variadic arguments.
+#define TINT_FOREACH(CB, ...) \
+ TINT_MSVC_EXPAND_BUG( \
+ TINT_CONCAT(TINT_FOREACH_, TINT_COUNT_ARGUMENTS(__VA_ARGS__))(CB, __VA_ARGS__))
+
+#define TINT_FOREACH_1(CB, _1) CB(_1)
+#define TINT_FOREACH_2(CB, _1, _2) \
+ TINT_FOREACH_1(CB, _1) \
+ CB(_2)
+#define TINT_FOREACH_3(CB, _1, _2, _3) \
+ TINT_FOREACH_2(CB, _1, _2) \
+ CB(_3)
+#define TINT_FOREACH_4(CB, _1, _2, _3, _4) \
+ TINT_FOREACH_3(CB, _1, _2, _3) \
+ CB(_4)
+#define TINT_FOREACH_5(CB, _1, _2, _3, _4, _5) \
+ TINT_FOREACH_4(CB, _1, _2, _3, _4) \
+ CB(_5)
+#define TINT_FOREACH_6(CB, _1, _2, _3, _4, _5, _6) \
+ TINT_FOREACH_5(CB, _1, _2, _3, _4, _5) \
+ CB(_6)
+#define TINT_FOREACH_7(CB, _1, _2, _3, _4, _5, _6, _7) \
+ TINT_FOREACH_6(CB, _1, _2, _3, _4, _5, _6) \
+ CB(_7)
+#define TINT_FOREACH_8(CB, _1, _2, _3, _4, _5, _6, _7, _8) \
+ TINT_FOREACH_7(CB, _1, _2, _3, _4, _5, _6, _7) \
+ CB(_8)
+#define TINT_FOREACH_9(CB, _1, _2, _3, _4, _5, _6, _7, _8, _9) \
+ TINT_FOREACH_8(CB, _1, _2, _3, _4, _5, _6, _7, _8) \
+ CB(_9)
+#define TINT_FOREACH_10(CB, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10) \
+ TINT_FOREACH_9(CB, _1, _2, _3, _4, _5, _6, _7, _8, _9) \
+ CB(_10)
+#define TINT_FOREACH_11(CB, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11) \
+ TINT_FOREACH_10(CB, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10) \
+ CB(_11)
+#define TINT_FOREACH_12(CB, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12) \
+ TINT_FOREACH_11(CB, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11) \
+ CB(_12)
+#define TINT_FOREACH_13(CB, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13) \
+ TINT_FOREACH_11(CB, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12) \
+ CB(_13)
+#define TINT_FOREACH_14(CB, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14) \
+ TINT_FOREACH_11(CB, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13) \
+ CB(_14)
+#define TINT_FOREACH_15(CB, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15) \
+ TINT_FOREACH_11(CB, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14) \
+ CB(_15)
+
+#endif // SRC_TINT_UTILS_FOREACH_MACRO_H_
diff --git a/src/tint/writer/array_length_from_uniform_options.h b/src/tint/writer/array_length_from_uniform_options.h
index cfa2fbd..41d873e 100644
--- a/src/tint/writer/array_length_from_uniform_options.h
+++ b/src/tint/writer/array_length_from_uniform_options.h
@@ -43,8 +43,8 @@
/// uniform buffer where the length of the buffer is stored.
std::unordered_map<sem::BindingPoint, uint32_t> bindpoint_to_size_index;
- // NOTE: Update src/tint/fuzzers/data_builder.h when adding or changing any
- // struct members.
+ /// Reflect the fields of this class so that it can be used by tint::ForeachField()
+ TINT_REFLECT(ubo_binding, bindpoint_to_size_index);
};
} // namespace tint::writer
diff --git a/src/tint/writer/generate_external_texture_bindings.cc b/src/tint/writer/generate_external_texture_bindings.cc
index 16c33e3..6408e0e 100644
--- a/src/tint/writer/generate_external_texture_bindings.cc
+++ b/src/tint/writer/generate_external_texture_bindings.cc
@@ -50,7 +50,8 @@
for (auto bp : ext_tex_bps) {
uint32_t g = bp.group;
uint32_t& next_num = group_to_next_binding_number[g];
- auto new_bps = transform::BindingPoints{{g, next_num++}, {g, next_num++}};
+ auto new_bps =
+ transform::MultiplanarExternalTexture::BindingPoints{{g, next_num++}, {g, next_num++}};
new_bindings_map[bp] = new_bps;
}
return new_bindings_map;
diff --git a/src/tint/writer/glsl/generator_impl.cc b/src/tint/writer/glsl/generator_impl.cc
index e783b0b..8b2d898 100644
--- a/src/tint/writer/glsl/generator_impl.cc
+++ b/src/tint/writer/glsl/generator_impl.cc
@@ -46,8 +46,8 @@
#include "src/tint/sem/type_constructor.h"
#include "src/tint/sem/type_conversion.h"
#include "src/tint/sem/variable.h"
+#include "src/tint/transform/add_block_attribute.h"
#include "src/tint/transform/add_empty_entry_point.h"
-#include "src/tint/transform/add_spirv_block_attribute.h"
#include "src/tint/transform/binding_remapper.h"
#include "src/tint/transform/builtin_polyfill.h"
#include "src/tint/transform/canonicalize_entry_point_io.h"
@@ -150,25 +150,22 @@
}
void PrintF32(std::ostream& out, float value) {
- // Note: Currently inf and nan should not be constructable, but this is implemented for the day
- // we support them.
if (std::isinf(value)) {
- out << (value >= 0 ? "uintBitsToFloat(0x7f800000u)" : "uintBitsToFloat(0xff800000u)");
+ out << "0.0f " << (value >= 0 ? "/* inf */" : "/* -inf */");
} else if (std::isnan(value)) {
- out << "uintBitsToFloat(0x7fc00000u)";
+ out << "0.0f /* nan */";
} else {
out << FloatToString(value) << "f";
}
}
-bool PrintF16(std::ostream& out, float value) {
- // Note: Currently inf and nan should not be constructable, and there is no solid way to
- // generate constant/literal f16 Inf or NaN.
- if (std::isinf(value) || std::isnan(value)) {
- return false;
+void PrintF16(std::ostream& out, float value) {
+ if (std::isinf(value)) {
+ out << "0.0hf " << (value >= 0 ? "/* inf */" : "/* -inf */");
+ } else if (std::isnan(value)) {
+ out << "0.0hf /* nan */";
} else {
out << FloatToString(value) << "hf";
- return true;
}
}
@@ -244,7 +241,7 @@
manager.Add<transform::PromoteInitializersToLet>();
manager.Add<transform::AddEmptyEntryPoint>();
- manager.Add<transform::AddSpirvBlockAttribute>();
+ manager.Add<transform::AddBlockAttribute>();
data.Add<transform::CanonicalizeEntryPointIO::Config>(
transform::CanonicalizeEntryPointIO::ShaderStyle::kGlsl);
@@ -284,18 +281,15 @@
return false;
}
} else if (auto* str = decl->As<ast::Struct>()) {
- // Skip emission if the struct contains a runtime-sized array, since its
- // only use will be as the store-type of a buffer and we emit those
- // elsewhere.
- // TODO(crbug.com/tint/1339): We could also avoid emitting any other
- // struct that is only used as a buffer store type.
- const sem::Struct* sem_str = builder_.Sem().Get(str);
- const auto& members = sem_str->Members();
- TINT_ASSERT(Writer, members.size() > 0);
- auto* last_member = members[members.size() - 1];
- auto* arr = last_member->Type()->As<sem::Array>();
- if (!arr || !arr->IsRuntimeSized()) {
- if (!EmitStructType(current_buffer_, sem_str)) {
+ auto* sem = builder_.Sem().Get(str);
+ bool has_rt_arr = false;
+ if (auto* arr = sem->Members().back()->Type()->As<sem::Array>()) {
+ has_rt_arr = arr->IsRuntimeSized();
+ }
+ bool is_block =
+ ast::HasAttribute<transform::AddBlockAttribute::BlockAttribute>(str->attributes);
+ if (!has_rt_arr && !is_block) {
+ if (!EmitStructType(current_buffer_, sem)) {
return false;
}
}
@@ -1915,7 +1909,7 @@
if (version_.IsDesktop()) {
out << ", std140";
}
- out << ") uniform " << UniqueIdentifier(StructName(str)) << " {";
+ out << ") uniform " << UniqueIdentifier(StructName(str) + "_ubo") << " {";
}
EmitStructMembers(current_buffer_, str, /* emit_offsets */ true);
auto name = builder_.Symbols().NameFor(var->symbol);
@@ -1934,10 +1928,12 @@
}
auto bp = sem->As<sem::GlobalVariable>()->BindingPoint();
line() << "layout(binding = " << bp.binding << ", std430) buffer "
- << UniqueIdentifier(StructName(str)) << " {";
+ << UniqueIdentifier(StructName(str) + "_ssbo") << " {";
EmitStructMembers(current_buffer_, str, /* emit_offsets */ true);
auto name = builder_.Symbols().NameFor(var->symbol);
line() << "} " << name << ";";
+ line();
+
return true;
}
@@ -2186,7 +2182,10 @@
PrintF32(out, constant->As<float>());
return true;
},
- [&](const sem::F16*) { return PrintF16(out, constant->As<float>()); },
+ [&](const sem::F16*) {
+ PrintF16(out, constant->As<float>());
+ return true;
+ },
[&](const sem::I32*) {
out << constant->As<AInt>();
return true;
@@ -2286,9 +2285,10 @@
},
[&](const ast::FloatLiteralExpression* l) {
if (l->suffix == ast::FloatLiteralExpression::Suffix::kH) {
- return PrintF16(out, static_cast<float>(l->value));
+ PrintF16(out, static_cast<float>(l->value));
+ } else {
+ PrintF32(out, static_cast<float>(l->value));
}
- PrintF32(out, static_cast<float>(l->value));
return true;
},
[&](const ast::IntLiteralExpression* l) {
diff --git a/src/tint/writer/glsl/generator_impl_function_test.cc b/src/tint/writer/glsl/generator_impl_function_test.cc
index 83ba37b..c388005 100644
--- a/src/tint/writer/glsl/generator_impl_function_test.cc
+++ b/src/tint/writer/glsl/generator_impl_function_test.cc
@@ -384,7 +384,7 @@
vec4 coord;
};
-layout(binding = 0) uniform UBO_1 {
+layout(binding = 0) uniform UBO_ubo {
vec4 coord;
} ubo;
@@ -425,7 +425,7 @@
vec4 coord;
};
-layout(binding = 0) uniform Uniforms_1 {
+layout(binding = 0) uniform Uniforms_ubo {
vec4 coord;
} uniforms;
@@ -462,15 +462,11 @@
EXPECT_EQ(gen.result(), R"(#version 310 es
precision mediump float;
-struct Data {
- int a;
- float b;
-};
-
-layout(binding = 0, std430) buffer Data_1 {
+layout(binding = 0, std430) buffer Data_ssbo {
int a;
float b;
} coord;
+
void frag_main() {
float v = coord.b;
return;
@@ -510,15 +506,11 @@
R"(#version 310 es
precision mediump float;
-struct Data {
- int a;
- float b;
-};
-
-layout(binding = 0, std430) buffer Data_1 {
+layout(binding = 0, std430) buffer Data_ssbo {
int a;
float b;
} coord;
+
void frag_main() {
float v = coord.b;
return;
@@ -555,15 +547,11 @@
EXPECT_EQ(gen.result(), R"(#version 310 es
precision mediump float;
-struct Data {
- int a;
- float b;
-};
-
-layout(binding = 0, std430) buffer Data_1 {
+layout(binding = 0, std430) buffer Data_ssbo {
int a;
float b;
} coord;
+
void frag_main() {
coord.b = 2.0f;
return;
@@ -600,15 +588,11 @@
EXPECT_EQ(gen.result(), R"(#version 310 es
precision mediump float;
-struct Data {
- int a;
- float b;
-};
-
-layout(binding = 0, std430) buffer Data_1 {
+layout(binding = 0, std430) buffer Data_ssbo {
int a;
float b;
} coord;
+
void frag_main() {
coord.b = 2.0f;
return;
@@ -651,7 +635,7 @@
float x;
};
-layout(binding = 0) uniform S_1 {
+layout(binding = 0) uniform S_ubo {
float x;
} coord;
@@ -694,13 +678,10 @@
R"(#version 310 es
precision mediump float;
-struct S {
- float x;
-};
-
-layout(binding = 0, std430) buffer S_1 {
+layout(binding = 0, std430) buffer S_ssbo {
float x;
} coord;
+
float sub_func(float param) {
return coord.x;
}
@@ -930,13 +911,10 @@
ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#version 310 es
-struct Data {
- float d;
-};
-
-layout(binding = 0, std430) buffer Data_1 {
+layout(binding = 0, std430) buffer Data_ssbo {
float d;
} data;
+
void a() {
float v = data.d;
return;
diff --git a/src/tint/writer/glsl/generator_impl_member_accessor_test.cc b/src/tint/writer/glsl/generator_impl_member_accessor_test.cc
index 1602068..b44a11b 100644
--- a/src/tint/writer/glsl/generator_impl_member_accessor_test.cc
+++ b/src/tint/writer/glsl/generator_impl_member_accessor_test.cc
@@ -277,15 +277,11 @@
R"(#version 310 es
precision mediump float;
-struct Data {
- int a;
- mat2x3 b;
-};
-
-layout(binding = 0, std430) buffer Data_1 {
+layout(binding = 0, std430) buffer Data_ssbo {
int a;
mat2x3 b;
} data;
+
void tint_symbol() {
data.b = mat2x3(vec3(0.0f), vec3(0.0f));
}
@@ -322,15 +318,11 @@
R"(#version 310 es
precision mediump float;
-struct Data {
- float z;
- mat4x3 a;
-};
-
-layout(binding = 0, std430) buffer Data_1 {
+layout(binding = 0, std430) buffer Data_ssbo {
float z;
mat4x3 a;
} data;
+
void tint_symbol() {
float x = data.a[2][1];
}
@@ -367,15 +359,11 @@
R"(#version 310 es
precision mediump float;
-struct Data {
- float z;
- int a[5];
-};
-
-layout(binding = 0, std430) buffer Data_1 {
+layout(binding = 0, std430) buffer Data_ssbo {
float z;
int a[5];
} data;
+
void tint_symbol() {
int x = data.a[2];
}
@@ -415,15 +403,11 @@
R"(#version 310 es
precision mediump float;
-struct Data {
- float z;
- int a[5];
-};
-
-layout(binding = 0, std430) buffer Data_1 {
+layout(binding = 0, std430) buffer Data_ssbo {
float z;
int a[5];
} data;
+
void tint_symbol() {
int a = 2;
int b = 4;
@@ -462,15 +446,11 @@
R"(#version 310 es
precision mediump float;
-struct Data {
- float z;
- int a[5];
-};
-
-layout(binding = 0, std430) buffer Data_1 {
+layout(binding = 0, std430) buffer Data_ssbo {
float z;
int a[5];
} data;
+
void tint_symbol() {
data.a[2] = 2;
}
@@ -520,13 +500,10 @@
vec3 b;
};
-struct Data {
- Inner c[4];
-};
-
-layout(binding = 0, std430) buffer Data_1 {
+layout(binding = 0, std430) buffer Data_ssbo {
Inner c[4];
} data;
+
void tint_symbol() {
vec3 x = data.c[2].b;
}
@@ -578,13 +555,10 @@
vec3 b;
};
-struct Data {
- Inner c[4];
-};
-
-layout(binding = 0, std430) buffer Data_1 {
+layout(binding = 0, std430) buffer Data_ssbo {
Inner c[4];
} data;
+
void tint_symbol() {
vec2 x = data.c[2].b.xy;
}
@@ -637,13 +611,10 @@
vec3 b;
};
-struct Data {
- Inner c[4];
-};
-
-layout(binding = 0, std430) buffer Data_1 {
+layout(binding = 0, std430) buffer Data_ssbo {
Inner c[4];
} data;
+
void tint_symbol() {
float x = data.c[2].b.g;
}
@@ -695,13 +666,10 @@
vec3 b;
};
-struct Data {
- Inner c[4];
-};
-
-layout(binding = 0, std430) buffer Data_1 {
+layout(binding = 0, std430) buffer Data_ssbo {
Inner c[4];
} data;
+
void tint_symbol() {
float x = data.c[2].b[1];
}
@@ -752,13 +720,10 @@
vec3 b;
};
-struct Data {
- Inner c[4];
-};
-
-layout(binding = 0, std430) buffer Data_1 {
+layout(binding = 0, std430) buffer Data_ssbo {
Inner c[4];
} data;
+
void tint_symbol() {
data.c[2].b = vec3(1.0f, 2.0f, 3.0f);
}
@@ -810,13 +775,10 @@
vec3 b;
};
-struct Data {
- Inner c[4];
-};
-
-layout(binding = 0, std430) buffer Data_1 {
+layout(binding = 0, std430) buffer Data_ssbo {
Inner c[4];
} data;
+
void tint_symbol() {
data.c[2].b.y = 1.0f;
}
diff --git a/src/tint/writer/glsl/generator_impl_sanitizer_test.cc b/src/tint/writer/glsl/generator_impl_sanitizer_test.cc
index f1157f1..2d0d1ce 100644
--- a/src/tint/writer/glsl/generator_impl_sanitizer_test.cc
+++ b/src/tint/writer/glsl/generator_impl_sanitizer_test.cc
@@ -45,9 +45,10 @@
auto* expect = R"(#version 310 es
precision mediump float;
-layout(binding = 1, std430) buffer my_struct_1 {
+layout(binding = 1, std430) buffer my_struct_ssbo {
float a[];
} b;
+
void a_func() {
uint len = uint(b.a.length());
}
@@ -84,10 +85,11 @@
auto* expect = R"(#version 310 es
precision mediump float;
-layout(binding = 1, std430) buffer my_struct_1 {
+layout(binding = 1, std430) buffer my_struct_ssbo {
float z;
float a[];
} b;
+
void a_func() {
uint len = uint(b.a.length());
}
@@ -127,9 +129,10 @@
auto* expect = R"(#version 310 es
precision mediump float;
-layout(binding = 1, std430) buffer my_struct_1 {
+layout(binding = 1, std430) buffer my_struct_ssbo {
float a[];
} b;
+
void a_func() {
uint len = uint(b.a.length());
}
diff --git a/src/tint/writer/glsl/generator_impl_storage_buffer_test.cc b/src/tint/writer/glsl/generator_impl_storage_buffer_test.cc
index 8c60374..2c7bed1 100644
--- a/src/tint/writer/glsl/generator_impl_storage_buffer_test.cc
+++ b/src/tint/writer/glsl/generator_impl_storage_buffer_test.cc
@@ -57,11 +57,12 @@
float louie;
};
-layout(binding = 0, std430) buffer Nephews_1 {
+layout(binding = 0, std430) buffer Nephews_ssbo {
float huey;
float dewey;
float louie;
} nephews;
+
)");
}
@@ -79,11 +80,12 @@
float louie;
};
-layout(binding = 0, std430) buffer Nephews_1 {
+layout(binding = 0, std430) buffer Nephews_ssbo {
float huey;
layout(offset=256) float dewey;
layout(offset=512) float louie;
} nephews;
+
)");
}
diff --git a/src/tint/writer/glsl/generator_impl_uniform_buffer_test.cc b/src/tint/writer/glsl/generator_impl_uniform_buffer_test.cc
index 555e237..e3d2eee 100644
--- a/src/tint/writer/glsl/generator_impl_uniform_buffer_test.cc
+++ b/src/tint/writer/glsl/generator_impl_uniform_buffer_test.cc
@@ -37,7 +37,7 @@
float member;
};
-layout(binding = 0) uniform Simple_1 {
+layout(binding = 0) uniform Simple_ubo {
float member;
} simple;
@@ -57,7 +57,7 @@
float member;
};
-layout(binding = 0, std140) uniform Simple_1 {
+layout(binding = 0, std140) uniform Simple_ubo {
float member;
} simple;
diff --git a/src/tint/writer/hlsl/generator.h b/src/tint/writer/hlsl/generator.h
index 233416e..9974cde 100644
--- a/src/tint/writer/hlsl/generator.h
+++ b/src/tint/writer/hlsl/generator.h
@@ -23,6 +23,7 @@
#include <vector>
#include "src/tint/ast/pipeline_stage.h"
+#include "src/tint/reflection.h"
#include "src/tint/sem/binding_point.h"
#include "src/tint/writer/array_length_from_uniform_options.h"
#include "src/tint/writer/text.h"
@@ -56,8 +57,11 @@
/// from which to load buffer sizes.
ArrayLengthFromUniformOptions array_length_from_uniform = {};
- // NOTE: Update src/tint/fuzzers/data_builder.h when adding or changing any
- // struct members.
+ /// Reflect the fields of this class so that it can be used by tint::ForeachField()
+ TINT_REFLECT(root_constant_binding_point,
+ disable_workgroup_init,
+ generate_external_texture_bindings,
+ array_length_from_uniform);
};
/// The result produced when generating HLSL.
diff --git a/src/tint/writer/hlsl/generator_impl.cc b/src/tint/writer/hlsl/generator_impl.cc
index fca1515..3625db6 100644
--- a/src/tint/writer/hlsl/generator_impl.cc
+++ b/src/tint/writer/hlsl/generator_impl.cc
@@ -112,24 +112,22 @@
}
void PrintF32(std::ostream& out, float value) {
- // Note: Currently inf and nan should not be constructable, but this is implemented for the day
- // we support them.
if (std::isinf(value)) {
- out << (value >= 0 ? "asfloat(0x7f800000u)" : "asfloat(0xff800000u)");
+ out << "0.0f " << (value >= 0 ? "/* inf */" : "/* -inf */");
} else if (std::isnan(value)) {
- out << "asfloat(0x7fc00000u)";
+ out << "0.0f /* nan */";
} else {
out << FloatToString(value) << "f";
}
}
-bool PrintF16(std::ostream& out, float value) {
- // Note: Currently inf and nan should not be constructable, don't emit them.
- if (std::isinf(value) || std::isnan(value)) {
- return false;
+void PrintF16(std::ostream& out, float value) {
+ if (std::isinf(value)) {
+ out << "0.0h " << (value >= 0 ? "/* inf */" : "/* -inf */");
+ } else if (std::isnan(value)) {
+ out << "0.0h /* nan */";
} else {
out << FloatToString(value) << "h";
- return true;
}
}
@@ -3127,9 +3125,9 @@
[&](const sem::F16*) {
// emit a f16 scalar with explicit float16_t type declaration.
out << "float16_t(";
- bool valid = PrintF16(out, constant->As<float>());
+ PrintF16(out, constant->As<float>());
out << ")";
- return valid;
+ return true;
},
[&](const sem::I32*) {
out << constant->As<AInt>();
@@ -3254,9 +3252,8 @@
if (l->suffix == ast::FloatLiteralExpression::Suffix::kH) {
// Emit f16 literal with explicit float16_t type declaration.
out << "float16_t(";
- bool valid = PrintF16(out, static_cast<float>(l->value));
+ PrintF16(out, static_cast<float>(l->value));
out << ")";
- return valid;
}
PrintF32(out, static_cast<float>(l->value));
return true;
diff --git a/src/tint/writer/msl/generator.h b/src/tint/writer/msl/generator.h
index d4208cc..bdc2be5 100644
--- a/src/tint/writer/msl/generator.h
+++ b/src/tint/writer/msl/generator.h
@@ -21,6 +21,7 @@
#include <unordered_set>
#include <vector>
+#include "src/tint/reflection.h"
#include "src/tint/writer/array_length_from_uniform_options.h"
#include "src/tint/writer/text.h"
@@ -65,8 +66,13 @@
/// from which to load buffer sizes.
ArrayLengthFromUniformOptions array_length_from_uniform = {};
- // NOTE: Update src/tint/fuzzers/data_builder.h when adding or changing any
- // struct members.
+ /// Reflect the fields of this class so that it can be used by tint::ForeachField()
+ TINT_REFLECT(buffer_size_ubo_index,
+ fixed_sample_mask,
+ emit_vertex_point_size,
+ disable_workgroup_init,
+ generate_external_texture_bindings,
+ array_length_from_uniform);
};
/// The result produced when generating MSL.
diff --git a/src/tint/writer/spirv/builder.cc b/src/tint/writer/spirv/builder.cc
index c2056a4..04cbaac 100644
--- a/src/tint/writer/spirv/builder.cc
+++ b/src/tint/writer/spirv/builder.cc
@@ -43,7 +43,7 @@
#include "src/tint/sem/type_conversion.h"
#include "src/tint/sem/variable.h"
#include "src/tint/sem/vector.h"
-#include "src/tint/transform/add_spirv_block_attribute.h"
+#include "src/tint/transform/add_block_attribute.h"
#include "src/tint/utils/defer.h"
#include "src/tint/utils/map.h"
#include "src/tint/writer/append_vector.h"
@@ -4059,8 +4059,7 @@
ops.push_back(result);
auto* decl = struct_type->Declaration();
- if (decl && ast::HasAttribute<transform::AddSpirvBlockAttribute::SpirvBlockAttribute>(
- decl->attributes)) {
+ if (decl && ast::HasAttribute<transform::AddBlockAttribute::BlockAttribute>(decl->attributes)) {
push_annot(spv::Op::OpDecorate, {Operand(struct_id), U32Operand(SpvDecorationBlock)});
}
diff --git a/src/tint/writer/spirv/generator.h b/src/tint/writer/spirv/generator.h
index ce871ff..e406f6e 100644
--- a/src/tint/writer/spirv/generator.h
+++ b/src/tint/writer/spirv/generator.h
@@ -19,6 +19,7 @@
#include <string>
#include <vector>
+#include "src/tint/reflection.h"
#include "src/tint/writer/writer.h"
// Forward declarations
@@ -47,6 +48,12 @@
/// Set to `true` to initialize workgroup memory with OpConstantNull when
/// VK_KHR_zero_initialize_workgroup_memory is enabled.
bool use_zero_initialize_workgroup_memory_extension = false;
+
+ /// Reflect the fields of this class so that it can be used by tint::ForeachField()
+ TINT_REFLECT(emit_vertex_point_size,
+ disable_workgroup_init,
+ generate_external_texture_bindings,
+ use_zero_initialize_workgroup_memory_extension);
};
/// The result produced when generating SPIR-V.
diff --git a/src/tint/writer/spirv/generator_impl.cc b/src/tint/writer/spirv/generator_impl.cc
index d7a2b80..06f8613 100644
--- a/src/tint/writer/spirv/generator_impl.cc
+++ b/src/tint/writer/spirv/generator_impl.cc
@@ -17,8 +17,8 @@
#include <utility>
#include <vector>
+#include "src/tint/transform/add_block_attribute.h"
#include "src/tint/transform/add_empty_entry_point.h"
-#include "src/tint/transform/add_spirv_block_attribute.h"
#include "src/tint/transform/builtin_polyfill.h"
#include "src/tint/transform/canonicalize_entry_point_io.h"
#include "src/tint/transform/disable_uniformity_analysis.h"
@@ -86,7 +86,7 @@
manager.Add<transform::WhileToLoop>(); // ZeroInitWorkgroupMemory
manager.Add<transform::CanonicalizeEntryPointIO>();
manager.Add<transform::AddEmptyEntryPoint>();
- manager.Add<transform::AddSpirvBlockAttribute>();
+ manager.Add<transform::AddBlockAttribute>();
manager.Add<transform::VarForDynamicIndex>();
data.Add<transform::CanonicalizeEntryPointIO::Config>(