tint: Add a negation operator to Number

Use this for expressing negative i32s.
Replacing `i32(-123)` with `-123_i` is more readable as the former looks
like it should be generating a WGSL cast, which it does not.

Bug: tint:1504
Change-Id: Iead3885b903e1f707b8a7e6b9090d65930df118e
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/89401
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2022-05-10 16:02:06 +00:00 committed by Dawn LUCI CQ
parent 17fa42d527
commit 636e3d0111
11 changed files with 26 additions and 22 deletions

View File

@ -991,7 +991,7 @@ TEST_F(InspectorGetConstantIDsTest, U32) {
TEST_F(InspectorGetConstantIDsTest, I32) {
AddOverridableConstantWithID("foo", 1, ty.i32(), nullptr);
AddOverridableConstantWithID("bar", 20, ty.i32(), Expr(i32(-42)));
AddOverridableConstantWithID("bar", 20, ty.i32(), Expr(-42_i));
AddOverridableConstantWithID("baz", 300, ty.i32(), Expr(42_i));
Inspector& inspector = Build();

View File

@ -40,6 +40,10 @@ struct Number {
/// @returns the value as T
operator T() const { return value; }
/// Negation operator
/// @returns the negative value of the number
Number operator-() const { return Number(-value); }
/// Assignment operator
/// @param v the new value
/// @returns this Number so calls can be chained

View File

@ -947,7 +947,7 @@ TEST_P(ResolverBuiltinTest_SingleParam_FloatOrInt, Float_Vector) {
TEST_P(ResolverBuiltinTest_SingleParam_FloatOrInt, Sint_Scalar) {
auto param = GetParam();
auto* call = Call(param.name, i32(-1));
auto* call = Call(param.name, -1_i);
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();

View File

@ -232,9 +232,9 @@ TEST_F(ResolverControlBlockValidationTest, SwitchConditionTypeMustMatchSelectorT
// }
auto* var = Var("a", ty.u32(), Expr(2_u));
auto* block = Block(Decl(var), //
Switch("a", //
Case(Source{{12, 34}}, {Expr(i32(-1))}), //
auto* block = Block(Decl(var), //
Switch("a", //
Case(Source{{12, 34}}, {Expr(-1_i)}), //
DefaultCase()));
WrapInFunction(block);
@ -281,12 +281,12 @@ TEST_F(ResolverControlBlockValidationTest, NonUniqueCaseSelectorValueSint_Fail)
auto* block = Block(Decl(var), //
Switch("a", //
Case(Expr(Source{{12, 34}}, i32(-10))),
Case(Expr(Source{{12, 34}}, -10_i)),
Case({
Expr(0_i),
Expr(1_i),
Expr(2_i),
Expr(Source{{56, 78}}, i32(-10)),
Expr(Source{{56, 78}}, -10_i),
}),
DefaultCase()));
WrapInFunction(block);

View File

@ -552,7 +552,7 @@ TEST_F(ResolverFunctionValidationTest, WorkgroupSize_Literal_Negative) {
// fn main() {}
Func("main", {}, ty.void_(), {},
{Stage(ast::PipelineStage::kCompute), WorkgroupSize(Expr(Source{{12, 34}}, i32(-2)))});
{Stage(ast::PipelineStage::kCompute), WorkgroupSize(Expr(Source{{12, 34}}, -2_i))});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:34 error: workgroup_size argument must be at least 1");
@ -587,7 +587,7 @@ TEST_F(ResolverFunctionValidationTest, WorkgroupSize_Const_Negative) {
// let x = -2i;
// @stage(compute) @workgroup_size(x)
// fn main() {}
GlobalConst("x", ty.i32(), Expr(i32(-2)));
GlobalConst("x", ty.i32(), Expr(-2_i));
Func("main", {}, ty.void_(), {},
{Stage(ast::PipelineStage::kCompute), WorkgroupSize(Expr(Source{{12, 34}}, "x"))});

View File

@ -232,7 +232,7 @@ TEST_F(ResolverTypeValidationTest, ArraySize_SignedLiteral_Zero) {
TEST_F(ResolverTypeValidationTest, ArraySize_SignedLiteral_Negative) {
// var<private> a : array<f32, -10i>;
Global("a", ty.array(ty.f32(), Expr(Source{{12, 34}}, i32(-10))), ast::StorageClass::kPrivate);
Global("a", ty.array(ty.f32(), Expr(Source{{12, 34}}, -10_i)), ast::StorageClass::kPrivate);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:34 error: array size must be at least 1");
}
@ -258,7 +258,7 @@ TEST_F(ResolverTypeValidationTest, ArraySize_SignedConstant_Zero) {
TEST_F(ResolverTypeValidationTest, ArraySize_SignedConstant_Negative) {
// let size = -10i;
// var<private> a : array<f32, size>;
GlobalConst("size", nullptr, Expr(i32(-10)));
GlobalConst("size", nullptr, Expr(-10_i));
Global("a", ty.array(ty.f32(), Expr(Source{{12, 34}}, "size")), ast::StorageClass::kPrivate);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:34 error: array size must be at least 1");

View File

@ -34,7 +34,7 @@ TEST_F(GlslGeneratorImplTest_Constructor, EmitConstructor_Bool) {
}
TEST_F(GlslGeneratorImplTest_Constructor, EmitConstructor_Int) {
WrapInFunction(Expr(i32(-12345)));
WrapInFunction(Expr(-12345_i));
GeneratorImpl& gen = Build();
@ -80,7 +80,7 @@ TEST_F(GlslGeneratorImplTest_Constructor, EmitConstructor_Type_Bool) {
}
TEST_F(GlslGeneratorImplTest_Constructor, EmitConstructor_Type_Int) {
WrapInFunction(Construct<i32>(i32(-12345)));
WrapInFunction(Construct<i32>(-12345_i));
GeneratorImpl& gen = Build();

View File

@ -34,7 +34,7 @@ TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Bool) {
}
TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Int) {
WrapInFunction(Expr(i32(-12345)));
WrapInFunction(Expr(-12345_i));
GeneratorImpl& gen = Build();
@ -80,7 +80,7 @@ TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Bool) {
}
TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Int) {
WrapInFunction(Construct<i32>(i32(-12345)));
WrapInFunction(Construct<i32>(-12345_i));
GeneratorImpl& gen = Build();

View File

@ -34,7 +34,7 @@ TEST_F(MslGeneratorImplTest, EmitConstructor_Bool) {
}
TEST_F(MslGeneratorImplTest, EmitConstructor_Int) {
WrapInFunction(Expr(i32(-12345)));
WrapInFunction(Expr(-12345_i));
GeneratorImpl& gen = Build();
@ -80,7 +80,7 @@ TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Bool) {
}
TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Int) {
WrapInFunction(Construct<i32>(i32(-12345)));
WrapInFunction(Construct<i32>(-12345_i));
GeneratorImpl& gen = Build();

View File

@ -72,7 +72,7 @@ TEST_F(BuilderTest, Literal_Bool_Dedup) {
}
TEST_F(BuilderTest, Literal_I32) {
auto* i = Expr(i32(-23));
auto* i = Expr(-23_i);
WrapInFunction(i);
spirv::Builder& b = Build();
@ -86,8 +86,8 @@ TEST_F(BuilderTest, Literal_I32) {
}
TEST_F(BuilderTest, Literal_I32_Dedup) {
auto* i1 = Expr(i32(-23));
auto* i2 = Expr(i32(-23));
auto* i1 = Expr(-23_i);
auto* i2 = Expr(-23_i);
WrapInFunction(i1, i2);
spirv::Builder& b = Build();

View File

@ -34,7 +34,7 @@ TEST_F(WgslGeneratorImplTest, EmitConstructor_Bool) {
}
TEST_F(WgslGeneratorImplTest, EmitConstructor_Int) {
WrapInFunction(Expr(i32(-12345)));
WrapInFunction(Expr(-12345_i));
GeneratorImpl& gen = Build();
@ -80,7 +80,7 @@ TEST_F(WgslGeneratorImplTest, EmitConstructor_Type_Bool) {
}
TEST_F(WgslGeneratorImplTest, EmitConstructor_Type_Int) {
WrapInFunction(Construct<i32>(i32(-12345)));
WrapInFunction(Construct<i32>(-12345_i));
GeneratorImpl& gen = Build();