blob: 706d97463ea6c6dfbe99e0db8b4fd47fb5e6ae47 [file] [log] [blame]
// Copyright 2021 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/lang/core/builtin_value.h"
#include "src/tint/lang/core/type/texture_dimension.h"
#include "src/tint/lang/wgsl/ast/disable_validation_attribute.h"
#include "src/tint/lang/wgsl/ast/transform/add_block_attribute.h"
#include "src/tint/lang/wgsl/resolver/resolver.h"
#include "src/tint/lang/wgsl/resolver/resolver_helper_test.h"
#include "src/tint/utils/text/string_stream.h"
#include "gmock/gmock.h"
namespace tint::resolver {
using namespace tint::core::fluent_types; // NOLINT
using namespace tint::core::number_suffixes; // NOLINT
// Helpers and typedefs
template <typename T>
using DataType = builder::DataType<T>;
template <typename T, int ID = 0>
using alias = builder::alias<T, ID>;
template <typename T>
using alias1 = builder::alias1<T>;
template <typename T>
using alias2 = builder::alias2<T>;
template <typename T>
using alias3 = builder::alias3<T>;
namespace AttributeTests {
namespace {
enum class AttributeKind {
kAlign,
kBinding,
kBuiltin,
kDiagnostic,
kGroup,
kId,
kIndex,
kInterpolate,
kInvariant,
kLocation,
kMustUse,
kOffset,
kSize,
kStage,
kStride,
kWorkgroup,
kBindingAndGroup,
};
static bool IsBindingAttribute(AttributeKind kind) {
switch (kind) {
case AttributeKind::kBinding:
case AttributeKind::kGroup:
case AttributeKind::kBindingAndGroup:
return true;
default:
return false;
}
}
struct TestParams {
AttributeKind kind;
bool should_pass;
};
struct TestWithParams : ResolverTestWithParam<TestParams> {
void EnableExtensionIfNecessary(AttributeKind attributeKind) {
if (attributeKind == AttributeKind::kIndex) {
Enable(wgsl::Extension::kChromiumInternalDualSourceBlending);
}
}
};
static Vector<const ast::Attribute*, 2> createAttributes(const Source& source,
ProgramBuilder& builder,
AttributeKind kind) {
switch (kind) {
case AttributeKind::kAlign:
return {builder.MemberAlign(source, 4_i)};
case AttributeKind::kBinding:
return {builder.Binding(source, 1_a)};
case AttributeKind::kBuiltin:
return {builder.Builtin(source, core::BuiltinValue::kPosition)};
case AttributeKind::kDiagnostic:
return {builder.DiagnosticAttribute(source, wgsl::DiagnosticSeverity::kInfo, "chromium",
"unreachable_code")};
case AttributeKind::kGroup:
return {builder.Group(source, 1_a)};
case AttributeKind::kId:
return {builder.Id(source, 0_a)};
case AttributeKind::kIndex:
return {builder.Index(source, 0_a)};
case AttributeKind::kInterpolate:
return {builder.Interpolate(source, core::InterpolationType::kLinear,
core::InterpolationSampling::kCenter)};
case AttributeKind::kInvariant:
return {builder.Invariant(source)};
case AttributeKind::kLocation:
return {builder.Location(source, 1_a)};
case AttributeKind::kOffset:
return {builder.MemberOffset(source, 4_a)};
case AttributeKind::kMustUse:
return {builder.MustUse(source)};
case AttributeKind::kSize:
return {builder.MemberSize(source, 16_a)};
case AttributeKind::kStage:
return {builder.Stage(source, ast::PipelineStage::kCompute)};
case AttributeKind::kStride:
return {builder.create<ast::StrideAttribute>(source, 4u)};
case AttributeKind::kWorkgroup:
return {builder.create<ast::WorkgroupAttribute>(source, builder.Expr(1_i))};
case AttributeKind::kBindingAndGroup:
return {builder.Binding(source, 1_a), builder.Group(source, 1_a)};
}
return {};
}
static std::string name(AttributeKind kind) {
switch (kind) {
case AttributeKind::kAlign:
return "@align";
case AttributeKind::kBinding:
return "@binding";
case AttributeKind::kBuiltin:
return "@builtin";
case AttributeKind::kDiagnostic:
return "@diagnostic";
case AttributeKind::kGroup:
return "@group";
case AttributeKind::kId:
return "@id";
case AttributeKind::kIndex:
return "@index";
case AttributeKind::kInterpolate:
return "@interpolate";
case AttributeKind::kInvariant:
return "@invariant";
case AttributeKind::kLocation:
return "@location";
case AttributeKind::kOffset:
return "@offset";
case AttributeKind::kMustUse:
return "@must_use";
case AttributeKind::kSize:
return "@size";
case AttributeKind::kStage:
return "@stage";
case AttributeKind::kStride:
return "@stride";
case AttributeKind::kWorkgroup:
return "@workgroup_size";
case AttributeKind::kBindingAndGroup:
return "@binding";
}
return "<unknown>";
}
namespace FunctionInputAndOutputTests {
using FunctionParameterAttributeTest = TestWithParams;
TEST_P(FunctionParameterAttributeTest, IsValid) {
auto& params = GetParam();
EnableExtensionIfNecessary(params.kind);
Func("main",
Vector{
Param("a", ty.vec4<f32>(), createAttributes({}, *this, params.kind)),
},
ty.void_(), tint::Empty);
if (params.should_pass) {
EXPECT_TRUE(r()->Resolve()) << r()->error();
} else if (params.kind == AttributeKind::kLocation || params.kind == AttributeKind::kBuiltin ||
params.kind == AttributeKind::kInvariant ||
params.kind == AttributeKind::kInterpolate) {
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "error: " + name(params.kind) +
" is not valid for non-entry point function parameters");
} else {
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"error: " + name(params.kind) + " is not valid for function parameters");
}
}
INSTANTIATE_TEST_SUITE_P(ResolverAttributeValidationTest,
FunctionParameterAttributeTest,
testing::Values(TestParams{AttributeKind::kAlign, false},
TestParams{AttributeKind::kBinding, false},
TestParams{AttributeKind::kBuiltin, false},
TestParams{AttributeKind::kDiagnostic, false},
TestParams{AttributeKind::kGroup, false},
TestParams{AttributeKind::kId, false},
TestParams{AttributeKind::kIndex, false},
TestParams{AttributeKind::kInterpolate, false},
TestParams{AttributeKind::kInvariant, false},
TestParams{AttributeKind::kLocation, false},
TestParams{AttributeKind::kMustUse, false},
TestParams{AttributeKind::kOffset, false},
TestParams{AttributeKind::kSize, false},
TestParams{AttributeKind::kStage, false},
TestParams{AttributeKind::kStride, false},
TestParams{AttributeKind::kWorkgroup, false},
TestParams{AttributeKind::kBindingAndGroup, false}));
using FunctionReturnTypeAttributeTest = TestWithParams;
TEST_P(FunctionReturnTypeAttributeTest, IsValid) {
auto& params = GetParam();
EnableExtensionIfNecessary(params.kind);
Func("main", tint::Empty, ty.f32(),
Vector{
Return(1_f),
},
tint::Empty, createAttributes({}, *this, params.kind));
if (params.should_pass) {
EXPECT_TRUE(r()->Resolve()) << r()->error();
} else {
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "error: " + name(params.kind) +
" is not valid for non-entry point function "
"return types");
}
}
INSTANTIATE_TEST_SUITE_P(ResolverAttributeValidationTest,
FunctionReturnTypeAttributeTest,
testing::Values(TestParams{AttributeKind::kAlign, false},
TestParams{AttributeKind::kBinding, false},
TestParams{AttributeKind::kBuiltin, false},
TestParams{AttributeKind::kDiagnostic, false},
TestParams{AttributeKind::kGroup, false},
TestParams{AttributeKind::kId, false},
TestParams{AttributeKind::kIndex, false},
TestParams{AttributeKind::kInterpolate, false},
TestParams{AttributeKind::kInvariant, false},
TestParams{AttributeKind::kLocation, false},
TestParams{AttributeKind::kMustUse, false},
TestParams{AttributeKind::kOffset, false},
TestParams{AttributeKind::kSize, false},
TestParams{AttributeKind::kStage, false},
TestParams{AttributeKind::kStride, false},
TestParams{AttributeKind::kWorkgroup, false},
TestParams{AttributeKind::kBindingAndGroup, false}));
} // namespace FunctionInputAndOutputTests
namespace EntryPointInputAndOutputTests {
using ComputeShaderParameterAttributeTest = TestWithParams;
TEST_P(ComputeShaderParameterAttributeTest, IsValid) {
auto& params = GetParam();
EnableExtensionIfNecessary(params.kind);
Func("main",
Vector{
Param("a", ty.vec4<f32>(), createAttributes(Source{{12, 34}}, *this, params.kind)),
},
ty.void_(), tint::Empty,
Vector{
Stage(ast::PipelineStage::kCompute),
WorkgroupSize(1_i),
});
if (params.should_pass) {
EXPECT_TRUE(r()->Resolve()) << r()->error();
} else {
EXPECT_FALSE(r()->Resolve());
if (params.kind == AttributeKind::kBuiltin) {
EXPECT_EQ(
r()->error(),
R"(12:34 error: @builtin(position) cannot be used in input of compute pipeline stage)");
} else if (params.kind == AttributeKind::kInterpolate ||
params.kind == AttributeKind::kLocation ||
params.kind == AttributeKind::kInvariant) {
EXPECT_EQ(r()->error(), "12:34 error: " + name(params.kind) +
" is not valid for compute shader inputs");
} else {
EXPECT_EQ(r()->error(), "12:34 error: " + name(params.kind) +
" is not valid for function parameters");
}
}
}
INSTANTIATE_TEST_SUITE_P(ResolverAttributeValidationTest,
ComputeShaderParameterAttributeTest,
testing::Values(TestParams{AttributeKind::kAlign, false},
TestParams{AttributeKind::kBinding, false},
TestParams{AttributeKind::kBuiltin, false},
TestParams{AttributeKind::kDiagnostic, false},
TestParams{AttributeKind::kGroup, false},
TestParams{AttributeKind::kId, false},
TestParams{AttributeKind::kIndex, false},
TestParams{AttributeKind::kInterpolate, false},
TestParams{AttributeKind::kInvariant, false},
TestParams{AttributeKind::kLocation, false},
TestParams{AttributeKind::kMustUse, false},
TestParams{AttributeKind::kOffset, false},
TestParams{AttributeKind::kSize, false},
TestParams{AttributeKind::kStage, false},
TestParams{AttributeKind::kStride, false},
TestParams{AttributeKind::kWorkgroup, false},
TestParams{AttributeKind::kBindingAndGroup, false}));
using FragmentShaderParameterAttributeTest = TestWithParams;
TEST_P(FragmentShaderParameterAttributeTest, IsValid) {
auto& params = GetParam();
EnableExtensionIfNecessary(params.kind);
auto attrs = createAttributes(Source{{12, 34}}, *this, params.kind);
if (params.kind != AttributeKind::kBuiltin && params.kind != AttributeKind::kLocation) {
attrs.Push(Builtin(Source{{34, 56}}, core::BuiltinValue::kPosition));
}
auto* p = Param("a", ty.vec4<f32>(), attrs);
Func("frag_main", Vector{p}, ty.void_(), tint::Empty,
Vector{
Stage(ast::PipelineStage::kFragment),
});
if (params.should_pass) {
EXPECT_TRUE(r()->Resolve()) << r()->error();
} else {
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: " + name(params.kind) + " is not valid for function parameters");
}
}
INSTANTIATE_TEST_SUITE_P(ResolverAttributeValidationTest,
FragmentShaderParameterAttributeTest,
testing::Values(TestParams{AttributeKind::kAlign, false},
TestParams{AttributeKind::kBinding, false},
TestParams{AttributeKind::kBuiltin, true},
TestParams{AttributeKind::kDiagnostic, false},
TestParams{AttributeKind::kGroup, false},
TestParams{AttributeKind::kId, false},
TestParams{AttributeKind::kIndex, false},
// kInterpolate tested separately (requires @location)
TestParams{AttributeKind::kInvariant, true},
TestParams{AttributeKind::kLocation, true},
TestParams{AttributeKind::kMustUse, false},
TestParams{AttributeKind::kOffset, false},
TestParams{AttributeKind::kSize, false},
TestParams{AttributeKind::kStage, false},
TestParams{AttributeKind::kStride, false},
TestParams{AttributeKind::kWorkgroup, false},
TestParams{AttributeKind::kBindingAndGroup, false}));
using VertexShaderParameterAttributeTest = TestWithParams;
TEST_P(VertexShaderParameterAttributeTest, IsValid) {
auto& params = GetParam();
EnableExtensionIfNecessary(params.kind);
auto attrs = createAttributes(Source{{12, 34}}, *this, params.kind);
if (params.kind != AttributeKind::kLocation) {
attrs.Push(Location(Source{{34, 56}}, 2_a));
}
auto* p = Param("a", ty.vec4<f32>(), attrs);
Func("vertex_main", Vector{p}, ty.vec4<f32>(),
Vector{
Return(Call<vec4<f32>>()),
},
Vector{
Stage(ast::PipelineStage::kVertex),
},
Vector{
Builtin(core::BuiltinValue::kPosition),
});
if (params.should_pass) {
EXPECT_TRUE(r()->Resolve()) << r()->error();
} else {
EXPECT_FALSE(r()->Resolve());
if (params.kind == AttributeKind::kBuiltin) {
EXPECT_EQ(
r()->error(),
R"(12:34 error: @builtin(position) cannot be used in input of vertex pipeline stage)");
} else if (params.kind == AttributeKind::kInvariant) {
EXPECT_EQ(r()->error(),
"12:34 error: invariant attribute must only be applied to a "
"position builtin");
} else {
EXPECT_EQ(r()->error(), "12:34 error: " + name(params.kind) +
" is not valid for function parameters");
}
}
}
INSTANTIATE_TEST_SUITE_P(ResolverAttributeValidationTest,
VertexShaderParameterAttributeTest,
testing::Values(TestParams{AttributeKind::kAlign, false},
TestParams{AttributeKind::kBinding, false},
TestParams{AttributeKind::kBuiltin, false},
TestParams{AttributeKind::kDiagnostic, false},
TestParams{AttributeKind::kGroup, false},
TestParams{AttributeKind::kId, false},
TestParams{AttributeKind::kIndex, false},
TestParams{AttributeKind::kInterpolate, true},
TestParams{AttributeKind::kInvariant, false},
TestParams{AttributeKind::kLocation, true},
TestParams{AttributeKind::kMustUse, false},
TestParams{AttributeKind::kOffset, false},
TestParams{AttributeKind::kSize, false},
TestParams{AttributeKind::kStage, false},
TestParams{AttributeKind::kStride, false},
TestParams{AttributeKind::kWorkgroup, false},
TestParams{AttributeKind::kBindingAndGroup, false}));
using ComputeShaderReturnTypeAttributeTest = TestWithParams;
TEST_P(ComputeShaderReturnTypeAttributeTest, IsValid) {
auto& params = GetParam();
EnableExtensionIfNecessary(params.kind);
Func("main", tint::Empty, ty.vec4<f32>(),
Vector{
Return(Call<vec4<f32>>(1_f)),
},
Vector{
Stage(ast::PipelineStage::kCompute),
WorkgroupSize(1_i),
},
createAttributes(Source{{12, 34}}, *this, params.kind));
if (params.should_pass) {
EXPECT_TRUE(r()->Resolve()) << r()->error();
} else {
EXPECT_FALSE(r()->Resolve());
if (params.kind == AttributeKind::kBuiltin) {
EXPECT_EQ(
r()->error(),
R"(12:34 error: @builtin(position) cannot be used in output of compute pipeline stage)");
} else if (params.kind == AttributeKind::kInterpolate ||
params.kind == AttributeKind::kLocation ||
params.kind == AttributeKind::kInvariant ||
params.kind == AttributeKind::kIndex) {
EXPECT_EQ(r()->error(), "12:34 error: " + name(params.kind) +
" is not valid for compute shader output");
} else {
EXPECT_EQ(r()->error(), "12:34 error: " + name(params.kind) +
" is not valid for entry point return "
"types");
}
}
}
INSTANTIATE_TEST_SUITE_P(ResolverAttributeValidationTest,
ComputeShaderReturnTypeAttributeTest,
testing::Values(TestParams{AttributeKind::kAlign, false},
TestParams{AttributeKind::kBinding, false},
TestParams{AttributeKind::kBuiltin, false},
TestParams{AttributeKind::kDiagnostic, false},
TestParams{AttributeKind::kGroup, false},
TestParams{AttributeKind::kId, false},
TestParams{AttributeKind::kIndex, false},
TestParams{AttributeKind::kInterpolate, false},
TestParams{AttributeKind::kInvariant, false},
TestParams{AttributeKind::kLocation, false},
TestParams{AttributeKind::kMustUse, false},
TestParams{AttributeKind::kOffset, false},
TestParams{AttributeKind::kSize, false},
TestParams{AttributeKind::kStage, false},
TestParams{AttributeKind::kStride, false},
TestParams{AttributeKind::kWorkgroup, false},
TestParams{AttributeKind::kBindingAndGroup, false}));
using FragmentShaderReturnTypeAttributeTest = TestWithParams;
TEST_P(FragmentShaderReturnTypeAttributeTest, IsValid) {
auto& params = GetParam();
EnableExtensionIfNecessary(params.kind);
auto attrs = createAttributes(Source{{12, 34}}, *this, params.kind);
attrs.Push(Location(Source{{34, 56}}, 2_a));
Func("frag_main", tint::Empty, ty.vec4<f32>(), Vector{Return(Call<vec4<f32>>())},
Vector{
Stage(ast::PipelineStage::kFragment),
},
attrs);
if (params.should_pass) {
EXPECT_TRUE(r()->Resolve()) << r()->error();
} else {
EXPECT_FALSE(r()->Resolve());
if (params.kind == AttributeKind::kBuiltin) {
EXPECT_EQ(
r()->error(),
R"(12:34 error: @builtin(position) cannot be used in output of fragment pipeline stage)");
} else if (params.kind == AttributeKind::kInvariant) {
EXPECT_EQ(
r()->error(),
R"(12:34 error: invariant attribute must only be applied to a position builtin)");
} else if (params.kind == AttributeKind::kLocation) {
EXPECT_EQ(r()->error(),
R"(34:56 error: duplicate location attribute
12:34 note: first attribute declared here)");
} else {
EXPECT_EQ(r()->error(), "12:34 error: " + name(params.kind) +
" is not valid for entry point return types");
}
}
}
INSTANTIATE_TEST_SUITE_P(ResolverAttributeValidationTest,
FragmentShaderReturnTypeAttributeTest,
testing::Values(TestParams{AttributeKind::kAlign, false},
TestParams{AttributeKind::kBinding, false},
TestParams{AttributeKind::kBuiltin, false},
TestParams{AttributeKind::kDiagnostic, false},
TestParams{AttributeKind::kGroup, false},
TestParams{AttributeKind::kId, false},
TestParams{AttributeKind::kIndex, true},
TestParams{AttributeKind::kInterpolate, true},
TestParams{AttributeKind::kInvariant, false},
TestParams{AttributeKind::kLocation, false},
TestParams{AttributeKind::kMustUse, false},
TestParams{AttributeKind::kOffset, false},
TestParams{AttributeKind::kSize, false},
TestParams{AttributeKind::kStage, false},
TestParams{AttributeKind::kStride, false},
TestParams{AttributeKind::kWorkgroup, false},
TestParams{AttributeKind::kBindingAndGroup, false}));
using VertexShaderReturnTypeAttributeTest = TestWithParams;
TEST_P(VertexShaderReturnTypeAttributeTest, IsValid) {
auto& params = GetParam();
EnableExtensionIfNecessary(params.kind);
auto attrs = createAttributes(Source{{12, 34}}, *this, params.kind);
// a vertex shader must include the 'position' builtin in its return type
if (params.kind != AttributeKind::kBuiltin) {
attrs.Push(Builtin(Source{{34, 56}}, core::BuiltinValue::kPosition));
}
Func("vertex_main", tint::Empty, ty.vec4<f32>(),
Vector{
Return(Call<vec4<f32>>()),
},
Vector{
Stage(ast::PipelineStage::kVertex),
},
attrs);
if (params.should_pass) {
EXPECT_TRUE(r()->Resolve()) << r()->error();
} else {
EXPECT_FALSE(r()->Resolve());
if (params.kind == AttributeKind::kLocation) {
EXPECT_EQ(r()->error(),
R"(34:56 error: multiple entry point IO attributes
12:34 note: previously consumed @location)");
} else if (params.kind == AttributeKind::kIndex) {
EXPECT_EQ(r()->error(), R"(12:34 error: @index is not valid for vertex shader output)");
} else {
EXPECT_EQ(r()->error(), "12:34 error: " + name(params.kind) +
" is not valid for entry point return types");
}
}
}
INSTANTIATE_TEST_SUITE_P(ResolverAttributeValidationTest,
VertexShaderReturnTypeAttributeTest,
testing::Values(TestParams{AttributeKind::kAlign, false},
TestParams{AttributeKind::kBinding, false},
TestParams{AttributeKind::kBuiltin, true},
TestParams{AttributeKind::kDiagnostic, false},
TestParams{AttributeKind::kGroup, false},
TestParams{AttributeKind::kId, false},
TestParams{AttributeKind::kIndex, false},
// kInterpolate tested separately (requires @location)
TestParams{AttributeKind::kInvariant, true},
TestParams{AttributeKind::kLocation, false},
TestParams{AttributeKind::kMustUse, false},
TestParams{AttributeKind::kOffset, false},
TestParams{AttributeKind::kSize, false},
TestParams{AttributeKind::kStage, false},
TestParams{AttributeKind::kStride, false},
TestParams{AttributeKind::kWorkgroup, false},
TestParams{AttributeKind::kBindingAndGroup, false}));
using EntryPointParameterAttributeTest = TestWithParams;
TEST_F(EntryPointParameterAttributeTest, DuplicateAttribute) {
Func("main", tint::Empty, ty.f32(),
Vector{
Return(1_f),
},
Vector{
Stage(ast::PipelineStage::kFragment),
},
Vector{
Location(Source{{12, 34}}, 2_a),
Location(Source{{56, 78}}, 3_a),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(56:78 error: duplicate location attribute
12:34 note: first attribute declared here)");
}
TEST_F(EntryPointParameterAttributeTest, DuplicateInternalAttribute) {
auto* s = Param("s", ty.sampler(core::type::SamplerKind::kSampler),
Vector{
Binding(0_a),
Group(0_a),
Disable(ast::DisabledValidation::kBindingPointCollision),
Disable(ast::DisabledValidation::kEntryPointParameter),
});
Func("f", Vector{s}, ty.void_(), tint::Empty,
Vector{
Stage(ast::PipelineStage::kFragment),
});
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
using EntryPointReturnTypeAttributeTest = ResolverTest;
TEST_F(EntryPointReturnTypeAttributeTest, DuplicateAttribute) {
Func("main", tint::Empty, ty.f32(),
Vector{
Return(1_f),
},
Vector{
Stage(ast::PipelineStage::kFragment),
},
Vector{
Location(Source{{12, 34}}, 2_a),
Location(Source{{56, 78}}, 3_a),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(56:78 error: duplicate location attribute
12:34 note: first attribute declared here)");
}
TEST_F(EntryPointReturnTypeAttributeTest, DuplicateInternalAttribute) {
Func("f", tint::Empty, ty.i32(), Vector{Return(1_i)},
Vector{
Stage(ast::PipelineStage::kFragment),
},
Vector{
Disable(ast::DisabledValidation::kBindingPointCollision),
Disable(ast::DisabledValidation::kEntryPointParameter),
});
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
} // namespace EntryPointInputAndOutputTests
namespace StructAndStructMemberTests {
using StructAttributeTest = TestWithParams;
using SpirvBlockAttribute = ast::transform::AddBlockAttribute::BlockAttribute;
TEST_P(StructAttributeTest, IsValid) {
auto& params = GetParam();
EnableExtensionIfNecessary(params.kind);
Structure("mystruct", Vector{Member("a", ty.f32())},
createAttributes(Source{{12, 34}}, *this, params.kind));
if (params.should_pass) {
EXPECT_TRUE(r()->Resolve()) << r()->error();
} else {
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: " + name(params.kind) + " is not valid for struct declarations");
}
}
INSTANTIATE_TEST_SUITE_P(ResolverAttributeValidationTest,
StructAttributeTest,
testing::Values(TestParams{AttributeKind::kAlign, false},
TestParams{AttributeKind::kBinding, false},
TestParams{AttributeKind::kBuiltin, false},
TestParams{AttributeKind::kDiagnostic, false},
TestParams{AttributeKind::kGroup, false},
TestParams{AttributeKind::kId, false},
TestParams{AttributeKind::kIndex, false},
TestParams{AttributeKind::kInterpolate, false},
TestParams{AttributeKind::kInvariant, false},
TestParams{AttributeKind::kLocation, false},
TestParams{AttributeKind::kMustUse, false},
TestParams{AttributeKind::kOffset, false},
TestParams{AttributeKind::kSize, false},
TestParams{AttributeKind::kStage, false},
TestParams{AttributeKind::kStride, false},
TestParams{AttributeKind::kWorkgroup, false},
TestParams{AttributeKind::kBindingAndGroup, false}));
using StructMemberAttributeTest = TestWithParams;
TEST_P(StructMemberAttributeTest, IsValid) {
auto& params = GetParam();
EnableExtensionIfNecessary(params.kind);
Vector<const ast::StructMember*, 1> members;
if (params.kind == AttributeKind::kBuiltin) {
members.Push(
Member("a", ty.vec4<f32>(), createAttributes(Source{{12, 34}}, *this, params.kind)));
} else {
members.Push(Member("a", ty.f32(), createAttributes(Source{{12, 34}}, *this, params.kind)));
}
Structure("mystruct", members);
if (params.should_pass) {
EXPECT_TRUE(r()->Resolve()) << r()->error();
} else {
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: " + name(params.kind) + " is not valid for struct members");
}
}
INSTANTIATE_TEST_SUITE_P(ResolverAttributeValidationTest,
StructMemberAttributeTest,
testing::Values(TestParams{AttributeKind::kAlign, true},
TestParams{AttributeKind::kBinding, false},
TestParams{AttributeKind::kBuiltin, true},
TestParams{AttributeKind::kDiagnostic, false},
TestParams{AttributeKind::kGroup, false},
TestParams{AttributeKind::kId, false},
// kIndex tested separately (requires @location)
// kInterpolate tested separately (requires @location)
// kInvariant tested separately (requires position builtin)
TestParams{AttributeKind::kLocation, true},
TestParams{AttributeKind::kMustUse, false},
TestParams{AttributeKind::kOffset, true},
TestParams{AttributeKind::kSize, true},
TestParams{AttributeKind::kStage, false},
TestParams{AttributeKind::kStride, false},
TestParams{AttributeKind::kWorkgroup, false},
TestParams{AttributeKind::kBindingAndGroup, false}));
TEST_F(StructMemberAttributeTest, DuplicateAttribute) {
Structure("mystruct", Vector{
Member("a", ty.i32(),
Vector{
MemberAlign(Source{{12, 34}}, 4_i),
MemberAlign(Source{{56, 78}}, 8_i),
}),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(56:78 error: duplicate align attribute
12:34 note: first attribute declared here)");
}
TEST_F(StructMemberAttributeTest, InvariantAttributeWithPosition) {
Structure("mystruct", Vector{
Member("a", ty.vec4<f32>(),
Vector{
Invariant(),
Builtin(core::BuiltinValue::kPosition),
}),
});
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(StructMemberAttributeTest, InvariantAttributeWithoutPosition) {
Structure("mystruct", Vector{
Member("a", ty.vec4<f32>(),
Vector{
Invariant(Source{{12, 34}}),
}),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: invariant attribute must only be applied to a "
"position builtin");
}
TEST_F(StructMemberAttributeTest, Align_Attribute_Const) {
GlobalConst("val", ty.i32(), Expr(1_i));
Structure("mystruct", Vector{Member("a", ty.f32(), Vector{MemberAlign("val")})});
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(StructMemberAttributeTest, Align_Attribute_ConstNegative) {
GlobalConst("val", ty.i32(), Expr(-2_i));
Structure("mystruct",
Vector{Member("a", ty.f32(), Vector{MemberAlign(Source{{12, 34}}, "val")})});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(12:34 error: @align value must be a positive, power-of-two integer)");
}
TEST_F(StructMemberAttributeTest, Align_Attribute_ConstPowerOfTwo) {
GlobalConst("val", ty.i32(), Expr(3_i));
Structure("mystruct",
Vector{Member("a", ty.f32(), Vector{MemberAlign(Source{{12, 34}}, "val")})});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(12:34 error: @align value must be a positive, power-of-two integer)");
}
TEST_F(StructMemberAttributeTest, Align_Attribute_ConstF32) {
GlobalConst("val", ty.f32(), Expr(1.23_f));
Structure("mystruct",
Vector{Member("a", ty.f32(), Vector{MemberAlign(Source{{12, 34}}, "val")})});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(12:34 error: @align must be an i32 or u32 value)");
}
TEST_F(StructMemberAttributeTest, Align_Attribute_ConstU32) {
GlobalConst("val", ty.u32(), Expr(2_u));
Structure("mystruct",
Vector{Member("a", ty.f32(), Vector{MemberAlign(Source{{12, 34}}, "val")})});
EXPECT_TRUE(r()->Resolve());
}
TEST_F(StructMemberAttributeTest, Align_Attribute_ConstAInt) {
GlobalConst("val", Expr(2_a));
Structure("mystruct",
Vector{Member("a", ty.f32(), Vector{MemberAlign(Source{{12, 34}}, "val")})});
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(StructMemberAttributeTest, Align_Attribute_ConstAFloat) {
GlobalConst("val", Expr(2.0_a));
Structure("mystruct",
Vector{Member("a", ty.f32(), Vector{MemberAlign(Source{{12, 34}}, "val")})});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(12:34 error: @align must be an i32 or u32 value)");
}
TEST_F(StructMemberAttributeTest, Align_Attribute_Var) {
GlobalVar(Source{{1, 2}}, "val", ty.f32(), core::AddressSpace::kPrivate,
core::Access::kUndefined, Expr(1.23_f));
Structure(Source{{6, 4}}, "mystruct",
Vector{Member(Source{{12, 5}}, "a", ty.f32(),
Vector{MemberAlign(Expr(Source{{12, 35}}, "val"))})});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(12:35 error: var 'val' cannot be referenced at module-scope
1:2 note: var 'val' declared here)");
}
TEST_F(StructMemberAttributeTest, Align_Attribute_Override) {
Override("val", ty.f32(), Expr(1.23_f));
Structure("mystruct",
Vector{Member("a", ty.f32(), Vector{MemberAlign(Expr(Source{{12, 34}}, "val"))})});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(
r()->error(),
R"(12:34 error: @align requires a const-expression, but expression is an override-expression)");
}
TEST_F(StructMemberAttributeTest, Size_Attribute_Const) {
GlobalConst("val", ty.i32(), Expr(4_i));
Structure("mystruct", Vector{Member("a", ty.f32(), Vector{MemberSize("val")})});
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(StructMemberAttributeTest, Size_Attribute_ConstNegative) {
GlobalConst("val", ty.i32(), Expr(-2_i));
Structure("mystruct",
Vector{Member("a", ty.f32(), Vector{MemberSize(Source{{12, 34}}, "val")})});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(12:34 error: @size must be a positive integer)");
}
TEST_F(StructMemberAttributeTest, Size_Attribute_ConstF32) {
GlobalConst("val", ty.f32(), Expr(1.23_f));
Structure("mystruct",
Vector{Member("a", ty.f32(), Vector{MemberSize(Source{{12, 34}}, "val")})});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(12:34 error: @size must be an i32 or u32 value)");
}
TEST_F(StructMemberAttributeTest, Size_Attribute_ConstU32) {
GlobalConst("val", ty.u32(), Expr(4_u));
Structure("mystruct",
Vector{Member("a", ty.f32(), Vector{MemberSize(Source{{12, 34}}, "val")})});
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(StructMemberAttributeTest, Size_Attribute_ConstAInt) {
GlobalConst("val", Expr(4_a));
Structure("mystruct",
Vector{Member("a", ty.f32(), Vector{MemberSize(Source{{12, 34}}, "val")})});
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(StructMemberAttributeTest, Size_Attribute_ConstAFloat) {
GlobalConst("val", Expr(2.0_a));
Structure("mystruct",
Vector{Member("a", ty.f32(), Vector{MemberSize(Source{{12, 34}}, "val")})});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(12:34 error: @size must be an i32 or u32 value)");
}
TEST_F(StructMemberAttributeTest, Size_Attribute_Var) {
GlobalVar(Source{{1, 2}}, "val", ty.f32(), core::AddressSpace::kPrivate,
core::Access::kUndefined, Expr(1.23_f));
Structure(Source{{6, 4}}, "mystruct",
Vector{Member(Source{{12, 5}}, "a", ty.f32(),
Vector{MemberSize(Expr(Source{{12, 35}}, "val"))})});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(12:35 error: var 'val' cannot be referenced at module-scope
1:2 note: var 'val' declared here)");
}
TEST_F(StructMemberAttributeTest, Size_Attribute_Override) {
Override("val", ty.f32(), Expr(1.23_f));
Structure("mystruct",
Vector{
Member("a", ty.f32(), Vector{MemberSize(Expr(Source{{12, 34}}, "val"))}),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(
r()->error(),
R"(12:34 error: @size requires a const-expression, but expression is an override-expression)");
}
TEST_F(StructMemberAttributeTest, Size_On_RuntimeSizedArray) {
Structure("mystruct",
Vector{
Member("a", ty.array<i32>(), Vector{MemberSize(Source{{12, 34}}, 8_a)}),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(
r()->error(),
R"(12:34 error: @size can only be applied to members where the member's type size can be fully determined at shader creation time)");
}
} // namespace StructAndStructMemberTests
using ArrayAttributeTest = TestWithParams;
TEST_P(ArrayAttributeTest, IsValid) {
auto& params = GetParam();
EnableExtensionIfNecessary(params.kind);
auto arr = ty.array(ty.f32(), createAttributes(Source{{12, 34}}, *this, params.kind));
Structure("mystruct", Vector{
Member("a", arr),
});
if (params.should_pass) {
EXPECT_TRUE(r()->Resolve()) << r()->error();
} else {
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: " + name(params.kind) + " is not valid for array types");
}
}
INSTANTIATE_TEST_SUITE_P(ResolverAttributeValidationTest,
ArrayAttributeTest,
testing::Values(TestParams{AttributeKind::kAlign, false},
TestParams{AttributeKind::kBinding, false},
TestParams{AttributeKind::kBuiltin, false},
TestParams{AttributeKind::kDiagnostic, false},
TestParams{AttributeKind::kGroup, false},
TestParams{AttributeKind::kId, false},
TestParams{AttributeKind::kIndex, false},
TestParams{AttributeKind::kInterpolate, false},
TestParams{AttributeKind::kInvariant, false},
TestParams{AttributeKind::kLocation, false},
TestParams{AttributeKind::kMustUse, false},
TestParams{AttributeKind::kOffset, false},
TestParams{AttributeKind::kSize, false},
TestParams{AttributeKind::kStage, false},
TestParams{AttributeKind::kStride, true},
TestParams{AttributeKind::kWorkgroup, false},
TestParams{AttributeKind::kBindingAndGroup, false}));
using VariableAttributeTest = TestWithParams;
TEST_P(VariableAttributeTest, IsValid) {
auto& params = GetParam();
EnableExtensionIfNecessary(params.kind);
auto attrs = createAttributes(Source{{12, 34}}, *this, params.kind);
if (IsBindingAttribute(params.kind)) {
GlobalVar("a", ty.sampler(core::type::SamplerKind::kSampler), attrs);
} else {
GlobalVar("a", ty.f32(), core::AddressSpace::kPrivate, attrs);
}
if (params.should_pass) {
EXPECT_TRUE(r()->Resolve()) << r()->error();
} else {
EXPECT_FALSE(r()->Resolve());
if (!IsBindingAttribute(params.kind)) {
EXPECT_EQ(r()->error(),
"12:34 error: " + name(params.kind) + " is not valid for module-scope 'var'");
}
}
}
INSTANTIATE_TEST_SUITE_P(ResolverAttributeValidationTest,
VariableAttributeTest,
testing::Values(TestParams{AttributeKind::kAlign, false},
TestParams{AttributeKind::kBinding, false},
TestParams{AttributeKind::kBuiltin, false},
TestParams{AttributeKind::kDiagnostic, false},
TestParams{AttributeKind::kGroup, false},
TestParams{AttributeKind::kId, false},
TestParams{AttributeKind::kIndex, false},
TestParams{AttributeKind::kInterpolate, false},
TestParams{AttributeKind::kInvariant, false},
TestParams{AttributeKind::kLocation, false},
TestParams{AttributeKind::kMustUse, false},
TestParams{AttributeKind::kOffset, false},
TestParams{AttributeKind::kSize, false},
TestParams{AttributeKind::kStage, false},
TestParams{AttributeKind::kStride, false},
TestParams{AttributeKind::kWorkgroup, false},
TestParams{AttributeKind::kBindingAndGroup, true}));
TEST_F(VariableAttributeTest, DuplicateAttribute) {
GlobalVar("a", ty.sampler(core::type::SamplerKind::kSampler), Binding(Source{{12, 34}}, 2_a),
Group(2_a), Binding(Source{{56, 78}}, 3_a));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(56:78 error: duplicate binding attribute
12:34 note: first attribute declared here)");
}
TEST_F(VariableAttributeTest, LocalVar) {
auto* v = Var("a", ty.f32(), Vector{Binding(Source{{12, 34}}, 2_a)});
WrapInFunction(v);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:34 error: @binding is not valid for function-scope 'var'");
}
TEST_F(VariableAttributeTest, LocalLet) {
auto* v = Let("a", Vector{Binding(Source{{12, 34}}, 2_a)}, Expr(1_a));
WrapInFunction(v);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:34 error: @binding is not valid for 'let' declaration");
}
using ConstantAttributeTest = TestWithParams;
TEST_P(ConstantAttributeTest, IsValid) {
auto& params = GetParam();
EnableExtensionIfNecessary(params.kind);
GlobalConst("a", ty.f32(), Expr(1.23_f),
createAttributes(Source{{12, 34}}, *this, params.kind));
if (params.should_pass) {
EXPECT_TRUE(r()->Resolve()) << r()->error();
} else {
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: " + name(params.kind) + " is not valid for 'const' declaration");
}
}
INSTANTIATE_TEST_SUITE_P(ResolverAttributeValidationTest,
ConstantAttributeTest,
testing::Values(TestParams{AttributeKind::kAlign, false},
TestParams{AttributeKind::kBinding, false},
TestParams{AttributeKind::kBuiltin, false},
TestParams{AttributeKind::kDiagnostic, false},
TestParams{AttributeKind::kGroup, false},
TestParams{AttributeKind::kId, false},
TestParams{AttributeKind::kIndex, false},
TestParams{AttributeKind::kInterpolate, false},
TestParams{AttributeKind::kInvariant, false},
TestParams{AttributeKind::kLocation, false},
TestParams{AttributeKind::kMustUse, false},
TestParams{AttributeKind::kOffset, false},
TestParams{AttributeKind::kSize, false},
TestParams{AttributeKind::kStage, false},
TestParams{AttributeKind::kStride, false},
TestParams{AttributeKind::kWorkgroup, false},
TestParams{AttributeKind::kBindingAndGroup, false}));
TEST_F(ConstantAttributeTest, InvalidAttribute) {
GlobalConst("a", ty.f32(), Expr(1.23_f),
Vector{
Id(Source{{12, 34}}, 0_a),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:34 error: @id is not valid for 'const' declaration");
}
using OverrideAttributeTest = TestWithParams;
TEST_P(OverrideAttributeTest, IsValid) {
auto& params = GetParam();
EnableExtensionIfNecessary(params.kind);
Override("a", ty.f32(), Expr(1.23_f), createAttributes(Source{{12, 34}}, *this, params.kind));
if (params.should_pass) {
EXPECT_TRUE(r()->Resolve()) << r()->error();
} else {
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: " + name(params.kind) + " is not valid for 'override' declaration");
}
}
INSTANTIATE_TEST_SUITE_P(ResolverAttributeValidationTest,
OverrideAttributeTest,
testing::Values(TestParams{AttributeKind::kAlign, false},
TestParams{AttributeKind::kBinding, false},
TestParams{AttributeKind::kBuiltin, false},
TestParams{AttributeKind::kDiagnostic, false},
TestParams{AttributeKind::kGroup, false},
TestParams{AttributeKind::kIndex, false},
TestParams{AttributeKind::kId, true},
TestParams{AttributeKind::kInterpolate, false},
TestParams{AttributeKind::kInvariant, false},
TestParams{AttributeKind::kLocation, false},
TestParams{AttributeKind::kMustUse, false},
TestParams{AttributeKind::kOffset, false},
TestParams{AttributeKind::kSize, false},
TestParams{AttributeKind::kStage, false},
TestParams{AttributeKind::kStride, false},
TestParams{AttributeKind::kWorkgroup, false},
TestParams{AttributeKind::kBindingAndGroup, false}));
TEST_F(OverrideAttributeTest, DuplicateAttribute) {
Override("a", ty.f32(), Expr(1.23_f),
Vector{
Id(Source{{12, 34}}, 0_a),
Id(Source{{56, 78}}, 1_a),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(56:78 error: duplicate id attribute
12:34 note: first attribute declared here)");
}
using SwitchStatementAttributeTest = TestWithParams;
TEST_P(SwitchStatementAttributeTest, IsValid) {
auto& params = GetParam();
EnableExtensionIfNecessary(params.kind);
WrapInFunction(Switch(Expr(0_a), Vector{DefaultCase()},
createAttributes(Source{{12, 34}}, *this, params.kind)));
if (params.should_pass) {
EXPECT_TRUE(r()->Resolve()) << r()->error();
} else {
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: " + name(params.kind) + " is not valid for switch statements");
}
}
INSTANTIATE_TEST_SUITE_P(ResolverAttributeValidationTest,
SwitchStatementAttributeTest,
testing::Values(TestParams{AttributeKind::kAlign, false},
TestParams{AttributeKind::kBinding, false},
TestParams{AttributeKind::kBuiltin, false},
TestParams{AttributeKind::kDiagnostic, true},
TestParams{AttributeKind::kGroup, false},
TestParams{AttributeKind::kId, false},
TestParams{AttributeKind::kIndex, false},
TestParams{AttributeKind::kInterpolate, false},
TestParams{AttributeKind::kInvariant, false},
TestParams{AttributeKind::kLocation, false},
TestParams{AttributeKind::kMustUse, false},
TestParams{AttributeKind::kOffset, false},
TestParams{AttributeKind::kSize, false},
TestParams{AttributeKind::kStage, false},
TestParams{AttributeKind::kStride, false},
TestParams{AttributeKind::kWorkgroup, false},
TestParams{AttributeKind::kBindingAndGroup, false}));
using SwitchBodyAttributeTest = TestWithParams;
TEST_P(SwitchBodyAttributeTest, IsValid) {
auto& params = GetParam();
EnableExtensionIfNecessary(params.kind);
WrapInFunction(Switch(Expr(0_a), Vector{DefaultCase()}, tint::Empty,
createAttributes(Source{{12, 34}}, *this, params.kind)));
if (params.should_pass) {
EXPECT_TRUE(r()->Resolve()) << r()->error();
} else {
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: " + name(params.kind) + " is not valid for switch body");
}
}
INSTANTIATE_TEST_SUITE_P(ResolverAttributeValidationTest,
SwitchBodyAttributeTest,
testing::Values(TestParams{AttributeKind::kAlign, false},
TestParams{AttributeKind::kBinding, false},
TestParams{AttributeKind::kBuiltin, false},
TestParams{AttributeKind::kDiagnostic, true},
TestParams{AttributeKind::kGroup, false},
TestParams{AttributeKind::kId, false},
TestParams{AttributeKind::kIndex, false},
TestParams{AttributeKind::kInterpolate, false},
TestParams{AttributeKind::kInvariant, false},
TestParams{AttributeKind::kLocation, false},
TestParams{AttributeKind::kMustUse, false},
TestParams{AttributeKind::kOffset, false},
TestParams{AttributeKind::kSize, false},
TestParams{AttributeKind::kStage, false},
TestParams{AttributeKind::kStride, false},
TestParams{AttributeKind::kWorkgroup, false},
TestParams{AttributeKind::kBindingAndGroup, false}));
using IfStatementAttributeTest = TestWithParams;
TEST_P(IfStatementAttributeTest, IsValid) {
auto& params = GetParam();
EnableExtensionIfNecessary(params.kind);
WrapInFunction(If(Expr(true), Block(), ElseStmt(),
createAttributes(Source{{12, 34}}, *this, params.kind)));
if (params.should_pass) {
EXPECT_TRUE(r()->Resolve()) << r()->error();
} else {
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: " + name(params.kind) + " is not valid for if statements");
}
}
INSTANTIATE_TEST_SUITE_P(ResolverAttributeValidationTest,
IfStatementAttributeTest,
testing::Values(TestParams{AttributeKind::kAlign, false},
TestParams{AttributeKind::kBinding, false},
TestParams{AttributeKind::kBuiltin, false},
TestParams{AttributeKind::kDiagnostic, true},
TestParams{AttributeKind::kGroup, false},
TestParams{AttributeKind::kId, false},
TestParams{AttributeKind::kIndex, false},
TestParams{AttributeKind::kInterpolate, false},
TestParams{AttributeKind::kInvariant, false},
TestParams{AttributeKind::kLocation, false},
TestParams{AttributeKind::kMustUse, false},
TestParams{AttributeKind::kOffset, false},
TestParams{AttributeKind::kSize, false},
TestParams{AttributeKind::kStage, false},
TestParams{AttributeKind::kStride, false},
TestParams{AttributeKind::kWorkgroup, false},
TestParams{AttributeKind::kBindingAndGroup, false}));
using ForStatementAttributeTest = TestWithParams;
TEST_P(ForStatementAttributeTest, IsValid) {
auto& params = GetParam();
EnableExtensionIfNecessary(params.kind);
WrapInFunction(For(nullptr, Expr(false), nullptr, Block(),
createAttributes(Source{{12, 34}}, *this, params.kind)));
if (params.should_pass) {
EXPECT_TRUE(r()->Resolve()) << r()->error();
} else {
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: " + name(params.kind) + " is not valid for for statements");
}
}
INSTANTIATE_TEST_SUITE_P(ResolverAttributeValidationTest,
ForStatementAttributeTest,
testing::Values(TestParams{AttributeKind::kAlign, false},
TestParams{AttributeKind::kBinding, false},
TestParams{AttributeKind::kBuiltin, false},
TestParams{AttributeKind::kDiagnostic, true},
TestParams{AttributeKind::kGroup, false},
TestParams{AttributeKind::kIndex, false},
TestParams{AttributeKind::kId, false},
TestParams{AttributeKind::kInterpolate, false},
TestParams{AttributeKind::kInvariant, false},
TestParams{AttributeKind::kLocation, false},
TestParams{AttributeKind::kMustUse, false},
TestParams{AttributeKind::kOffset, false},
TestParams{AttributeKind::kSize, false},
TestParams{AttributeKind::kStage, false},
TestParams{AttributeKind::kStride, false},
TestParams{AttributeKind::kWorkgroup, false},
TestParams{AttributeKind::kBindingAndGroup, false}));
using LoopStatementAttributeTest = TestWithParams;
TEST_P(LoopStatementAttributeTest, IsValid) {
auto& params = GetParam();
EnableExtensionIfNecessary(params.kind);
WrapInFunction(
Loop(Block(Return()), Block(), createAttributes(Source{{12, 34}}, *this, params.kind)));
if (params.should_pass) {
EXPECT_TRUE(r()->Resolve()) << r()->error();
} else {
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: " + name(params.kind) + " is not valid for loop statements");
}
}
INSTANTIATE_TEST_SUITE_P(ResolverAttributeValidationTest,
LoopStatementAttributeTest,
testing::Values(TestParams{AttributeKind::kAlign, false},
TestParams{AttributeKind::kBinding, false},
TestParams{AttributeKind::kBuiltin, false},
TestParams{AttributeKind::kDiagnostic, true},
TestParams{AttributeKind::kGroup, false},
TestParams{AttributeKind::kIndex, false},
TestParams{AttributeKind::kId, false},
TestParams{AttributeKind::kInterpolate, false},
TestParams{AttributeKind::kInvariant, false},
TestParams{AttributeKind::kLocation, false},
TestParams{AttributeKind::kMustUse, false},
TestParams{AttributeKind::kOffset, false},
TestParams{AttributeKind::kSize, false},
TestParams{AttributeKind::kStage, false},
TestParams{AttributeKind::kStride, false},
TestParams{AttributeKind::kWorkgroup, false},
TestParams{AttributeKind::kBindingAndGroup, false}));
using WhileStatementAttributeTest = TestWithParams;
TEST_P(WhileStatementAttributeTest, IsValid) {
auto& params = GetParam();
EnableExtensionIfNecessary(params.kind);
WrapInFunction(
While(Expr(false), Block(), createAttributes(Source{{12, 34}}, *this, params.kind)));
if (params.should_pass) {
EXPECT_TRUE(r()->Resolve()) << r()->error();
} else {
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: " + name(params.kind) + " is not valid for while statements");
}
}
INSTANTIATE_TEST_SUITE_P(ResolverAttributeValidationTest,
WhileStatementAttributeTest,
testing::Values(TestParams{AttributeKind::kAlign, false},
TestParams{AttributeKind::kBinding, false},
TestParams{AttributeKind::kBuiltin, false},
TestParams{AttributeKind::kDiagnostic, true},
TestParams{AttributeKind::kGroup, false},
TestParams{AttributeKind::kIndex, false},
TestParams{AttributeKind::kId, false},
TestParams{AttributeKind::kInterpolate, false},
TestParams{AttributeKind::kInvariant, false},
TestParams{AttributeKind::kLocation, false},
TestParams{AttributeKind::kMustUse, false},
TestParams{AttributeKind::kOffset, false},
TestParams{AttributeKind::kSize, false},
TestParams{AttributeKind::kStage, false},
TestParams{AttributeKind::kStride, false},
TestParams{AttributeKind::kWorkgroup, false},
TestParams{AttributeKind::kBindingAndGroup, false}));
namespace BlockStatementTests {
class BlockStatementTest : public TestWithParams {
protected:
void Check() {
if (GetParam().should_pass) {
EXPECT_TRUE(r()->Resolve()) << r()->error();
} else {
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"error: " + name(GetParam().kind) + " is not valid for block statements");
}
}
public:
BlockStatementTest() { EnableExtensionIfNecessary(GetParam().kind); }
};
TEST_P(BlockStatementTest, CompoundStatement) {
Func("foo", tint::Empty, ty.void_(),
Vector{
Block(Vector{Return()}, createAttributes({}, *this, GetParam().kind)),
});
Check();
}
TEST_P(BlockStatementTest, FunctionBody) {
Func("foo", tint::Empty, ty.void_(),
Block(Vector{Return()}, createAttributes({}, *this, GetParam().kind)));
Check();
}
TEST_P(BlockStatementTest, IfStatementBody) {
Func("foo", tint::Empty, ty.void_(),
Vector{
If(Expr(true), Block(Vector{Return()}, createAttributes({}, *this, GetParam().kind))),
});
Check();
}
TEST_P(BlockStatementTest, ElseStatementBody) {
Func("foo", tint::Empty, ty.void_(),
Vector{
If(Expr(true), Block(Vector{Return()}),
Else(Block(Vector{Return()}, createAttributes({}, *this, GetParam().kind)))),
});
Check();
}
TEST_P(BlockStatementTest, ForStatementBody) {
Func("foo", tint::Empty, ty.void_(),
Vector{
For(nullptr, Expr(true), nullptr,
Block(Vector{Break()}, createAttributes({}, *this, GetParam().kind))),
});
Check();
}
TEST_P(BlockStatementTest, LoopStatementBody) {
Func("foo", tint::Empty, ty.void_(),
Vector{
Loop(Block(Vector{Break()}, createAttributes({}, *this, GetParam().kind))),
});
Check();
}
TEST_P(BlockStatementTest, WhileStatementBody) {
Func(
"foo", tint::Empty, ty.void_(),
Vector{
While(Expr(true), Block(Vector{Break()}, createAttributes({}, *this, GetParam().kind))),
});
Check();
}
TEST_P(BlockStatementTest, CaseStatementBody) {
Func("foo", tint::Empty, ty.void_(),
Vector{
Switch(1_a,
Case(CaseSelector(1_a),
Block(Vector{Break()}, createAttributes({}, *this, GetParam().kind))),
DefaultCase(Block({}))),
});
Check();
}
TEST_P(BlockStatementTest, DefaultStatementBody) {
Func("foo", tint::Empty, ty.void_(),
Vector{
Switch(
1_a, Case(CaseSelector(1_a), Block()),
DefaultCase(Block(Vector{Break()}, createAttributes({}, *this, GetParam().kind)))),
});
Check();
}
INSTANTIATE_TEST_SUITE_P(ResolverAttributeValidationTest,
BlockStatementTest,
testing::Values(TestParams{AttributeKind::kAlign, false},
TestParams{AttributeKind::kBinding, false},
TestParams{AttributeKind::kBuiltin, false},
TestParams{AttributeKind::kDiagnostic, true},
TestParams{AttributeKind::kGroup, false},
TestParams{AttributeKind::kId, false},
TestParams{AttributeKind::kIndex, false},
TestParams{AttributeKind::kInterpolate, false},
TestParams{AttributeKind::kInvariant, false},
TestParams{AttributeKind::kLocation, false},
TestParams{AttributeKind::kMustUse, false},
TestParams{AttributeKind::kOffset, false},
TestParams{AttributeKind::kSize, false},
TestParams{AttributeKind::kStage, false},
TestParams{AttributeKind::kStride, false},
TestParams{AttributeKind::kWorkgroup, false},
TestParams{AttributeKind::kBindingAndGroup, false}));
} // namespace BlockStatementTests
} // namespace
} // namespace AttributeTests
namespace ArrayStrideTests {
namespace {
struct Params {
builder::ast_type_func_ptr create_el_type;
uint32_t stride;
bool should_pass;
};
template <typename T>
constexpr Params ParamsFor(uint32_t stride, bool should_pass) {
return Params{DataType<T>::AST, stride, should_pass};
}
struct TestWithParams : ResolverTestWithParam<Params> {};
using ArrayStrideTest = TestWithParams;
TEST_P(ArrayStrideTest, All) {
auto& params = GetParam();
ast::Type el_ty = params.create_el_type(*this);
StringStream ss;
ss << "el_ty: " << FriendlyName(el_ty) << ", stride: " << params.stride
<< ", should_pass: " << params.should_pass;
SCOPED_TRACE(ss.str());
auto arr = ty.array(el_ty, 4_u,
Vector{
create<ast::StrideAttribute>(Source{{12, 34}}, params.stride),
});
GlobalVar("myarray", arr, core::AddressSpace::kPrivate);
if (params.should_pass) {
EXPECT_TRUE(r()->Resolve()) << r()->error();
} else {
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: arrays decorated with the stride attribute must "
"have a stride that is at least the size of the element type, "
"and be a multiple of the element type's alignment value");
}
}
struct SizeAndAlignment {
uint32_t size;
uint32_t align;
};
constexpr SizeAndAlignment default_u32 = {4, 4};
constexpr SizeAndAlignment default_i32 = {4, 4};
constexpr SizeAndAlignment default_f32 = {4, 4};
constexpr SizeAndAlignment default_vec2 = {8, 8};
constexpr SizeAndAlignment default_vec3 = {12, 16};
constexpr SizeAndAlignment default_vec4 = {16, 16};
constexpr SizeAndAlignment default_mat2x2 = {16, 8};
constexpr SizeAndAlignment default_mat3x3 = {48, 16};
constexpr SizeAndAlignment default_mat4x4 = {64, 16};
INSTANTIATE_TEST_SUITE_P(ResolverAttributeValidationTest,
ArrayStrideTest,
testing::Values(
// Succeed because stride >= element size (while being multiple of
// element alignment)
ParamsFor<u32>(default_u32.size, true),
ParamsFor<i32>(default_i32.size, true),
ParamsFor<f32>(default_f32.size, true),
ParamsFor<vec2<f32>>(default_vec2.size, true),
// vec3's default size is not a multiple of its alignment
// ParamsFor<vec3<f32>, default_vec3.size, true},
ParamsFor<vec4<f32>>(default_vec4.size, true),
ParamsFor<mat2x2<f32>>(default_mat2x2.size, true),
ParamsFor<mat3x3<f32>>(default_mat3x3.size, true),
ParamsFor<mat4x4<f32>>(default_mat4x4.size, true),
// Fail because stride is < element size
ParamsFor<u32>(default_u32.size - 1, false),
ParamsFor<i32>(default_i32.size - 1, false),
ParamsFor<f32>(default_f32.size - 1, false),
ParamsFor<vec2<f32>>(default_vec2.size - 1, false),
ParamsFor<vec3<f32>>(default_vec3.size - 1, false),
ParamsFor<vec4<f32>>(default_vec4.size - 1, false),
ParamsFor<mat2x2<f32>>(default_mat2x2.size - 1, false),
ParamsFor<mat3x3<f32>>(default_mat3x3.size - 1, false),
ParamsFor<mat4x4<f32>>(default_mat4x4.size - 1, false),
// Succeed because stride equals multiple of element alignment
ParamsFor<u32>(default_u32.align * 7, true),
ParamsFor<i32>(default_i32.align * 7, true),
ParamsFor<f32>(default_f32.align * 7, true),
ParamsFor<vec2<f32>>(default_vec2.align * 7, true),
ParamsFor<vec3<f32>>(default_vec3.align * 7, true),
ParamsFor<vec4<f32>>(default_vec4.align * 7, true),
ParamsFor<mat2x2<f32>>(default_mat2x2.align * 7, true),
ParamsFor<mat3x3<f32>>(default_mat3x3.align * 7, true),
ParamsFor<mat4x4<f32>>(default_mat4x4.align * 7, true),
// Fail because stride is not multiple of element alignment
ParamsFor<u32>((default_u32.align - 1) * 7, false),
ParamsFor<i32>((default_i32.align - 1) * 7, false),
ParamsFor<f32>((default_f32.align - 1) * 7, false),
ParamsFor<vec2<f32>>((default_vec2.align - 1) * 7, false),
ParamsFor<vec3<f32>>((default_vec3.align - 1) * 7, false),
ParamsFor<vec4<f32>>((default_vec4.align - 1) * 7, false),
ParamsFor<mat2x2<f32>>((default_mat2x2.align - 1) * 7, false),
ParamsFor<mat3x3<f32>>((default_mat3x3.align - 1) * 7, false),
ParamsFor<mat4x4<f32>>((default_mat4x4.align - 1) * 7, false)));
TEST_F(ArrayStrideTest, DuplicateAttribute) {
auto arr = ty.array(Source{{12, 34}}, ty.i32(), 4_u,
Vector{
create<ast::StrideAttribute>(Source{{12, 34}}, 4u),
create<ast::StrideAttribute>(Source{{56, 78}}, 4u),
});
GlobalVar("myarray", arr, core::AddressSpace::kPrivate);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(56:78 error: duplicate stride attribute
12:34 note: first attribute declared here)");
}
} // namespace
} // namespace ArrayStrideTests
namespace ResourceTests {
namespace {
using ResourceAttributeTest = ResolverTest;
TEST_F(ResourceAttributeTest, UniformBufferMissingBinding) {
auto* s = Structure("S", Vector{
Member("x", ty.i32()),
});
GlobalVar(Source{{12, 34}}, "G", ty.Of(s), core::AddressSpace::kUniform);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(12:34 error: resource variables require @group and @binding attributes)");
}
TEST_F(ResourceAttributeTest, StorageBufferMissingBinding) {
auto* s = Structure("S", Vector{
Member("x", ty.i32()),
});
GlobalVar(Source{{12, 34}}, "G", ty.Of(s), core::AddressSpace::kStorage, core::Access::kRead);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(12:34 error: resource variables require @group and @binding attributes)");
}
TEST_F(ResourceAttributeTest, TextureMissingBinding) {
GlobalVar(Source{{12, 34}}, "G", ty.depth_texture(core::type::TextureDimension::k2d));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(12:34 error: resource variables require @group and @binding attributes)");
}
TEST_F(ResourceAttributeTest, SamplerMissingBinding) {
GlobalVar(Source{{12, 34}}, "G", ty.sampler(core::type::SamplerKind::kSampler));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(12:34 error: resource variables require @group and @binding attributes)");
}
TEST_F(ResourceAttributeTest, BindingPairMissingBinding) {
GlobalVar(Source{{12, 34}}, "G", ty.sampler(core::type::SamplerKind::kSampler), Group(1_a));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(12:34 error: resource variables require @group and @binding attributes)");
}
TEST_F(ResourceAttributeTest, BindingPairMissingGroup) {
GlobalVar(Source{{12, 34}}, "G", ty.sampler(core::type::SamplerKind::kSampler), Binding(1_a));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(12:34 error: resource variables require @group and @binding attributes)");
}
TEST_F(ResourceAttributeTest, BindingPointUsedTwiceByEntryPoint) {
GlobalVar(Source{{12, 34}}, "A",
ty.sampled_texture(core::type::TextureDimension::k2d, ty.f32()), Binding(1_a),
Group(2_a));
GlobalVar(Source{{56, 78}}, "B",
ty.sampled_texture(core::type::TextureDimension::k2d, ty.f32()), Binding(1_a),
Group(2_a));
Func("F", tint::Empty, ty.void_(),
Vector{
Decl(Var("a", ty.vec4<f32>(),
Call("textureLoad", "A", Call<vec2<i32>>(1_i, 2_i), 0_i))),
Decl(Var("b", ty.vec4<f32>(),
Call("textureLoad", "B", Call<vec2<i32>>(1_i, 2_i), 0_i))),
},
Vector{
Stage(ast::PipelineStage::kFragment),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(
r()->error(),
R"(56:78 error: entry point 'F' references multiple variables that use the same resource binding @group(2), @binding(1)
12:34 note: first resource binding usage declared here)");
}
TEST_F(ResourceAttributeTest, BindingPointUsedTwiceByDifferentEntryPoints) {
GlobalVar(Source{{12, 34}}, "A",
ty.sampled_texture(core::type::TextureDimension::k2d, ty.f32()), Binding(1_a),
Group(2_a));
GlobalVar(Source{{56, 78}}, "B",
ty.sampled_texture(core::type::TextureDimension::k2d, ty.f32()), Binding(1_a),
Group(2_a));
Func("F_A", tint::Empty, ty.void_(),
Vector{
Decl(Var("a", ty.vec4<f32>(),
Call("textureLoad", "A", Call<vec2<i32>>(1_i, 2_i), 0_i))),
},
Vector{
Stage(ast::PipelineStage::kFragment),
});
Func("F_B", tint::Empty, ty.void_(),
Vector{
Decl(Var("b", ty.vec4<f32>(),
Call("textureLoad", "B", Call<vec2<i32>>(1_i, 2_i), 0_i))),
},
Vector{
Stage(ast::PipelineStage::kFragment),
});
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(ResourceAttributeTest, BindingPointOnNonResource) {
GlobalVar(Source{{12, 34}}, "G", ty.f32(), core::AddressSpace::kPrivate, Binding(1_a),
Group(2_a));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(12:34 error: non-resource variables must not have @group or @binding attributes)");
}
} // namespace
} // namespace ResourceTests
namespace InvariantAttributeTests {
namespace {
using InvariantAttributeTests = ResolverTest;
TEST_F(InvariantAttributeTests, InvariantWithPosition) {
auto* param = Param("p", ty.vec4<f32>(),
Vector{
Invariant(Source{{12, 34}}),
Builtin(Source{{56, 78}}, core::BuiltinValue::kPosition),
});
Func("main", Vector{param}, ty.vec4<f32>(),
Vector{
Return(Call<vec4<f32>>()),
},
Vector{
Stage(ast::PipelineStage::kFragment),
},
Vector{
Location(0_a),
});
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(InvariantAttributeTests, InvariantWithoutPosition) {
auto* param = Param("p", ty.vec4<f32>(),
Vector{
Invariant(Source{{12, 34}}),
Location(0_a),
});
Func("main", Vector{param}, ty.vec4<f32>(),
Vector{
Return(Call<vec4<f32>>()),
},
Vector{
Stage(ast::PipelineStage::kFragment),
},
Vector{
Location(0_a),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: invariant attribute must only be applied to a "
"position builtin");
}
} // namespace
} // namespace InvariantAttributeTests
namespace MustUseAttributeTests {
namespace {
using MustUseAttributeTests = ResolverTest;
TEST_F(MustUseAttributeTests, MustUse) {
Func("main", tint::Empty, ty.vec4<f32>(),
Vector{
Return(Call<vec4<f32>>()),
},
Vector{
MustUse(Source{{12, 34}}),
});
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
} // namespace
} // namespace MustUseAttributeTests
namespace WorkgroupAttributeTests {
namespace {
using WorkgroupAttribute = ResolverTest;
TEST_F(WorkgroupAttribute, ComputeShaderPass) {
Func("main", tint::Empty, ty.void_(), tint::Empty,
Vector{
Stage(ast::PipelineStage::kCompute),
create<ast::WorkgroupAttribute>(Source{{12, 34}}, Expr(1_i)),
});
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(WorkgroupAttribute, Missing) {
Func(Source{{12, 34}}, "main", tint::Empty, ty.void_(), tint::Empty,
Vector{
Stage(ast::PipelineStage::kCompute),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: a compute shader must include 'workgroup_size' in its "
"attributes");
}
TEST_F(WorkgroupAttribute, NotAnEntryPoint) {
Func("main", tint::Empty, ty.void_(), tint::Empty,
Vector{
create<ast::WorkgroupAttribute>(Source{{12, 34}}, Expr(1_i)),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:34 error: @workgroup_size is only valid for compute stages");
}
TEST_F(WorkgroupAttribute, NotAComputeShader) {
Func("main", tint::Empty, ty.void_(), tint::Empty,
Vector{
Stage(ast::PipelineStage::kFragment),
create<ast::WorkgroupAttribute>(Source{{12, 34}}, Expr(1_i)),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:34 error: @workgroup_size is only valid for compute stages");
}
TEST_F(WorkgroupAttribute, DuplicateAttribute) {
Func(Source{{12, 34}}, "main", tint::Empty, ty.void_(), tint::Empty,
Vector{
Stage(ast::PipelineStage::kCompute),
WorkgroupSize(Source{{12, 34}}, 1_i, nullptr, nullptr),
WorkgroupSize(Source{{56, 78}}, 2_i, nullptr, nullptr),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(56:78 error: duplicate workgroup_size attribute
12:34 note: first attribute declared here)");
}
} // namespace
} // namespace WorkgroupAttributeTests
namespace InterpolateTests {
namespace {
using InterpolateTest = ResolverTest;
struct Params {
core::InterpolationType type;
core::InterpolationSampling sampling;
bool should_pass;
};
struct TestWithParams : ResolverTestWithParam<Params> {};
using InterpolateParameterTest = TestWithParams;
TEST_P(InterpolateParameterTest, All) {
auto& params = GetParam();
Func("main",
Vector{
Param("a", ty.f32(),
Vector{
Location(0_a),
Interpolate(Source{{12, 34}}, params.type, params.sampling),
}),
},
ty.void_(), tint::Empty,
Vector{
Stage(ast::PipelineStage::kFragment),
});
if (params.should_pass) {
EXPECT_TRUE(r()->Resolve()) << r()->error();
} else {
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: flat interpolation attribute must not have a "
"sampling parameter");
}
}
TEST_P(InterpolateParameterTest, IntegerScalar) {
auto& params = GetParam();
Func("main",
Vector{
Param("a", ty.i32(),
Vector{
Location(0_a),
Interpolate(Source{{12, 34}}, params.type, params.sampling),
}),
},
ty.void_(), tint::Empty,
Vector{
Stage(ast::PipelineStage::kFragment),
});
if (params.type != core::InterpolationType::kFlat) {
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: interpolation type must be 'flat' for integral "
"user-defined IO types");
} else if (params.should_pass) {
EXPECT_TRUE(r()->Resolve()) << r()->error();
} else {
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: flat interpolation attribute must not have a "
"sampling parameter");
}
}
TEST_P(InterpolateParameterTest, IntegerVector) {
auto& params = GetParam();
Func("main",
Vector{
Param("a", ty.vec4<u32>(),
Vector{
Location(0_a),
Interpolate(Source{{12, 34}}, params.type, params.sampling),
}),
},
ty.void_(), tint::Empty,
Vector{
Stage(ast::PipelineStage::kFragment),
});
if (params.type != core::InterpolationType::kFlat) {
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: interpolation type must be 'flat' for integral "
"user-defined IO types");
} else if (params.should_pass) {
EXPECT_TRUE(r()->Resolve()) << r()->error();
} else {
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: flat interpolation attribute must not have a "
"sampling parameter");
}
}
INSTANTIATE_TEST_SUITE_P(
ResolverAttributeValidationTest,
InterpolateParameterTest,
testing::Values(
Params{core::InterpolationType::kPerspective, core::InterpolationSampling::kUndefined,
true},
Params{core::InterpolationType::kPerspective, core::InterpolationSampling::kCenter, true},
Params{core::InterpolationType::kPerspective, core::InterpolationSampling::kCentroid, true},
Params{core::InterpolationType::kPerspective, core::InterpolationSampling::kSample, true},
Params{core::InterpolationType::kLinear, core::InterpolationSampling::kUndefined, true},
Params{core::InterpolationType::kLinear, core::InterpolationSampling::kCenter, true},
Params{core::InterpolationType::kLinear, core::InterpolationSampling::kCentroid, true},
Params{core::InterpolationType::kLinear, core::InterpolationSampling::kSample, true},
// flat interpolation must not have a sampling type
Params{core::InterpolationType::kFlat, core::InterpolationSampling::kUndefined, true},
Params{core::InterpolationType::kFlat, core::InterpolationSampling::kCenter, false},
Params{core::InterpolationType::kFlat, core::InterpolationSampling::kCentroid, false},
Params{core::InterpolationType::kFlat, core::InterpolationSampling::kSample, false}));
TEST_F(InterpolateTest, FragmentInput_Integer_MissingFlatInterpolation) {
Func("main", Vector{Param(Source{{12, 34}}, "a", ty.i32(), Vector{Location(0_a)})}, ty.void_(),
tint::Empty,
Vector{
Stage(ast::PipelineStage::kFragment),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(
r()->error(),
R"(12:34 error: integral user-defined fragment inputs must have a flat interpolation attribute)");
}
TEST_F(InterpolateTest, VertexOutput_Integer_MissingFlatInterpolation) {
auto* s = Structure(
"S", Vector{
Member("pos", ty.vec4<f32>(), Vector{Builtin(core::BuiltinValue::kPosition)}),
Member(Source{{12, 34}}, "u", ty.u32(), Vector{Location(0_a)}),
});
Func("main", tint::Empty, ty.Of(s),
Vector{
Return(Call(ty.Of(s))),
},
Vector{
Stage(ast::PipelineStage::kVertex),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(
r()->error(),
R"(12:34 error: integral user-defined vertex outputs must have a flat interpolation attribute
note: while analyzing entry point 'main')");
}
TEST_F(InterpolateTest, MissingLocationAttribute_Parameter) {
Func("main",
Vector{
Param("a", ty.vec4<f32>(),
Vector{
Builtin(core::BuiltinValue::kPosition),
Interpolate(Source{{12, 34}}, core::InterpolationType::kFlat),
}),
},
ty.void_(), tint::Empty,
Vector{
Stage(ast::PipelineStage::kFragment),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(12:34 error: interpolate attribute must only be used with @location)");
}
TEST_F(InterpolateTest, MissingLocationAttribute_ReturnType) {
Func("main", tint::Empty, ty.vec4<f32>(),
Vector{
Return(Call<vec4<f32>>()),
},
Vector{
Stage(ast::PipelineStage::kVertex),
},
Vector{
Builtin(core::BuiltinValue::kPosition),
Interpolate(Source{{12, 34}}, core::InterpolationType::kFlat),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(12:34 error: interpolate attribute must only be used with @location)");
}
TEST_F(InterpolateTest, MissingLocationAttribute_Struct) {
Structure("S",
Vector{
Member("a", ty.f32(),
Vector{Interpolate(Source{{12, 34}}, core::InterpolationType::kFlat)}),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(12:34 error: interpolate attribute must only be used with @location)");
}
using GroupAndBindingTest = ResolverTest;
TEST_F(GroupAndBindingTest, Const_I32) {
GlobalConst("b", Expr(4_i));
GlobalConst("g", Expr(2_i));
GlobalVar("val", ty.sampled_texture(core::type::TextureDimension::k2d, ty.f32()), Binding("b"),
Group("g"));
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(GroupAndBindingTest, Const_U32) {
GlobalConst("b", Expr(4_u));
GlobalConst("g", Expr(2_u));
GlobalVar("val", ty.sampled_texture(core::type::TextureDimension::k2d, ty.f32()), Binding("b"),
Group("g"));
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(GroupAndBindingTest, Const_AInt) {
GlobalConst("b", Expr(4_a));
GlobalConst("g", Expr(2_a));
GlobalVar("val", ty.sampled_texture(core::type::TextureDimension::k2d, ty.f32()), Binding("b"),
Group("g"));
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(GroupAndBindingTest, Binding_NonConstant) {
GlobalVar("val", ty.sampled_texture(core::type::TextureDimension::k2d, ty.f32()),
Binding(Call<u32>(Call(Source{{12, 34}}, "dpdx", 1_a))), Group(1_i));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(
r()->error(),
R"(12:34 error: @binding requires a const-expression, but expression is a runtime-expression)");
}
TEST_F(GroupAndBindingTest, Binding_Negative) {
GlobalVar("val", ty.sampled_texture(core::type::TextureDimension::k2d, ty.f32()),
Binding(Source{{12, 34}}, -2_i), Group(1_i));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(12:34 error: @binding value must be non-negative)");
}
TEST_F(GroupAndBindingTest, Binding_F32) {
GlobalVar("val", ty.sampled_texture(core::type::TextureDimension::k2d, ty.f32()),
Binding(Source{{12, 34}}, 2.0_f), Group(1_u));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(12:34 error: @binding must be an i32 or u32 value)");
}
TEST_F(GroupAndBindingTest, Binding_AFloat) {
GlobalVar("val", ty.sampled_texture(core::type::TextureDimension::k2d, ty.f32()),
Binding(Source{{12, 34}}, 2.0_a), Group(1_u));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(12:34 error: @binding must be an i32 or u32 value)");
}
TEST_F(GroupAndBindingTest, Group_NonConstant) {
GlobalVar("val", ty.sampled_texture(core::type::TextureDimension::k2d, ty.f32()), Binding(2_u),
Group(Call<u32>(Call(Source{{12, 34}}, "dpdx", 1_a))));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(
r()->error(),
R"(12:34 error: @group requires a const-expression, but expression is a runtime-expression)");
}
TEST_F(GroupAndBindingTest, Group_Negative) {
GlobalVar("val", ty.sampled_texture(core::type::TextureDimension::k2d, ty.f32()), Binding(2_u),
Group(Source{{12, 34}}, -1_i));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(12:34 error: @group value must be non-negative)");
}
TEST_F(GroupAndBindingTest, Group_F32) {
GlobalVar("val", ty.sampled_texture(core::type::TextureDimension::k2d, ty.f32()), Binding(2_u),
Group(Source{{12, 34}}, 1.0_f));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(12:34 error: @group must be an i32 or u32 value)");
}
TEST_F(GroupAndBindingTest, Group_AFloat) {
GlobalVar("val", ty.sampled_texture(core::type::TextureDimension::k2d, ty.f32()), Binding(2_u),
Group(Source{{12, 34}}, 1.0_a));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(12:34 error: @group must be an i32 or u32 value)");
}
using IdTest = ResolverTest;
TEST_F(IdTest, Const_I32) {
Override("val", ty.f32(), Vector{Id(1_i)});
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(IdTest, Const_U32) {
Override("val", ty.f32(), Vector{Id(1_u)});
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(IdTest, Const_AInt) {
Override("val", ty.f32(), Vector{Id(1_a)});
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(IdTest, NonConstant) {
Override("val", ty.f32(), Vector{Id(Call<u32>(Call(Source{{12, 34}}, "dpdx", 1_a)))});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(
r()->error(),
R"(12:34 error: @id requires a const-expression, but expression is a runtime-expression)");
}
TEST_F(IdTest, Negative) {
Override("val", ty.f32(), Vector{Id(Source{{12, 34}}, -1_i)});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(12:34 error: @id value must be non-negative)");
}
TEST_F(IdTest, F32) {
Override("val", ty.f32(), Vector{Id(Source{{12, 34}}, 1_f)});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(12:34 error: @id must be an i32 or u32 value)");
}
TEST_F(IdTest, AFloat) {
Override("val", ty.f32(), Vector{Id(Source{{12, 34}}, 1.0_a)});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(12:34 error: @id must be an i32 or u32 value)");
}
enum class LocationAttributeType {
kEntryPointParameter,
kEntryPointReturnType,
kStructureMember,
};
struct LocationTest : ResolverTestWithParam<LocationAttributeType> {
void Build(const ast::Expression* location_value) {
switch (GetParam()) {
case LocationAttributeType::kEntryPointParameter:
Func("main",
Vector{Param(Source{{12, 34}}, "a", ty.i32(),
Vector{
Location(Source{{12, 34}}, location_value),
Flat(),
})},
ty.void_(), tint::Empty,
Vector{
Stage(ast::PipelineStage::kFragment),
});
return;
case LocationAttributeType::kEntryPointReturnType:
Func("main", tint::Empty, ty.f32(),
Vector{
Return(1_a),
},
Vector{
Stage(ast::PipelineStage::kFragment),
},
Vector{
Location(Source{{12, 34}}, location_value),
});
return;
case LocationAttributeType::kStructureMember:
Structure("S", Vector{
Member("m", ty.f32(),
Vector{
Location(Source{{12, 34}}, location_value),
}),
});
return;
}
}
};
TEST_P(LocationTest, Const_I32) {
Build(Expr(0_i));
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
TEST_P(LocationTest, Const_U32) {
Build(Expr(0_u));
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
TEST_P(LocationTest, Const_AInt) {
Build(Expr(0_a));
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
TEST_P(LocationTest, NonConstant) {
Build(Call<u32>(Call(Source{{12, 34}}, "dpdx", 1_a)));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(
r()->error(),
R"(12:34 error: @location value requires a const-expression, but expression is a runtime-expression)");
}
TEST_P(LocationTest, Negative) {
Build(Expr(-1_a));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(12:34 error: @location value must be non-negative)");
}
TEST_P(LocationTest, F32) {
Build(Expr(1_f));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(12:34 error: @location must be an i32 or u32 value)");
}
TEST_P(LocationTest, AFloat) {
Build(Expr(1.0_a));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(12:34 error: @location must be an i32 or u32 value)");
}
INSTANTIATE_TEST_SUITE_P(LocationTest,
LocationTest,
testing::Values(LocationAttributeType::kEntryPointParameter,
LocationAttributeType::kEntryPointReturnType,
LocationAttributeType::kStructureMember));
} // namespace
} // namespace InterpolateTests
namespace MustUseTests {
namespace {
using MustUseAttributeTest = ResolverTest;
TEST_F(MustUseAttributeTest, UsedOnFnWithNoReturnValue) {
Func("fn_must_use", tint::Empty, ty.void_(), tint::Empty, Vector{MustUse(Source{{12, 34}})});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(12:34 error: @must_use can only be applied to functions that return a value)");
}
} // namespace
} // namespace MustUseTests
namespace InternalAttributeDeps {
namespace {
class TestAttribute : public Castable<TestAttribute, ast::InternalAttribute> {
public:
TestAttribute(GenerationID pid, ast::NodeID nid, const ast::IdentifierExpression* dep)
: Base(pid, nid, Vector{dep}) {}
std::string InternalName() const override { return "test_attribute"; }
const Node* Clone(ast::CloneContext&) const override { return nullptr; }
};
using InternalAttributeDepsTest = ResolverTest;
TEST_F(InternalAttributeDepsTest, Dependency) {
auto* ident = Expr("v");
auto* attr = ASTNodes().Create<TestAttribute>(ID(), AllocateNodeID(), ident);
auto* f = Func("f", tint::Empty, ty.void_(), tint::Empty, Vector{attr});
auto* v = GlobalVar("v", ty.i32(), core::AddressSpace::kPrivate);
EXPECT_TRUE(r()->Resolve()) << r()->error();
auto* user = As<sem::VariableUser>(Sem().Get(ident));
ASSERT_NE(user, nullptr);
auto* var = Sem().Get(v);
EXPECT_EQ(user->Variable(), var);
auto* fn = Sem().Get(f);
EXPECT_THAT(fn->DirectlyReferencedGlobals(), testing::ElementsAre(var));
EXPECT_THAT(fn->TransitivelyReferencedGlobals(), testing::ElementsAre(var));
}
} // namespace
} // namespace InternalAttributeDeps
} // namespace tint::resolver
TINT_INSTANTIATE_TYPEINFO(tint::resolver::InternalAttributeDeps::TestAttribute);