tint: Implement f16 keyword in Tint frontend

This patch:
1. Add the `f16` WGSL extension.
2. Add `f16` as keyword, and remove it from reserved word list.
3. Add ast::f16 and sem::f16, and implement validation that using `f16`
   type must be with `f16` extension enabled.
4. Add `Number<NumberKindF16>` for f16 literal and constant, and add
   `ast::FloatLiteralExpression::Suffix::kH`.
5. Add placeholder in all writer which report error when try to emit f16
   type.

Bugs: tint:1473, tint:1502
Change-Id: Ifb363beeb2699ed7cac57e07227d1b2cfa8050b4
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/89922
Reviewed-by: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Zhaoming Jiang <zhaoming.jiang@intel.com>
This commit is contained in:
Zhaoming Jiang
2022-05-13 12:01:11 +00:00
committed by Dawn LUCI CQ
parent e221ddd30e
commit 62bfd318ae
34 changed files with 502 additions and 11 deletions

View File

@@ -35,6 +35,8 @@ class Constant {
tint::u32 u32;
/// The scalar value as a f32
tint::f32 f32;
/// The scalar value as a f16, internally stored as float
tint::f16 f16;
/// The scalar value as a bool
bool bool_;
@@ -50,6 +52,10 @@ class Constant {
/// @param v the value of the Scalar
Scalar(tint::f32 v) : f32(v) {} // NOLINT
/// Constructs the scalar with the f16 value `v`
/// @param v the value of the Scalar
Scalar(tint::f16 v) : f16({v}) {} // NOLINT
/// Constructs the scalar with the bool value `v`
/// @param v the value of the Scalar
Scalar(bool v) : bool_(v) {} // NOLINT
@@ -106,6 +112,7 @@ class Constant {
ElementType(), //
[&](const I32*) { return func(elems_[index].i32); },
[&](const U32*) { return func(elems_[index].u32); },
[&](const F16*) { return func(elems_[index].f16); },
[&](const F32*) { return func(elems_[index].f32); },
[&](const Bool*) { return func(elems_[index].bool_); },
[&](Default) {

55
src/tint/sem/f16.cc Normal file
View File

@@ -0,0 +1,55 @@
// 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/sem/f16.h"
#include "src/tint/program_builder.h"
TINT_INSTANTIATE_TYPEINFO(tint::sem::F16);
namespace tint {
namespace sem {
F16::F16() = default;
F16::F16(F16&&) = default;
F16::~F16() = default;
size_t F16::Hash() const {
return static_cast<size_t>(TypeInfo::Of<F16>().full_hashcode);
}
bool F16::Equals(const Type& other) const {
return other.Is<F16>();
}
std::string F16::FriendlyName(const SymbolTable&) const {
return "f16";
}
bool F16::IsConstructible() const {
return true;
}
uint32_t F16::Size() const {
return 2;
}
uint32_t F16::Align() const {
return 2;
}
} // namespace sem
} // namespace tint

58
src/tint/sem/f16.h Normal file
View File

@@ -0,0 +1,58 @@
// 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_SEM_F16_H_
#define SRC_TINT_SEM_F16_H_
#include <string>
#include "src/tint/sem/type.h"
namespace tint::sem {
/// A float 16 type
class F16 : public Castable<F16, Type> {
public:
/// Constructor
F16();
/// Move constructor
F16(F16&&);
~F16() override;
/// @returns a hash of the type.
size_t Hash() const override;
/// @param other the other type to compare against
/// @returns true if the this type is equal to the given type
bool Equals(const Type& other) const override;
/// @param symbols the program's symbol table
/// @returns the name for this type that closely resembles how it would be
/// declared in WGSL.
std::string FriendlyName(const SymbolTable& symbols) const override;
/// @returns true if constructible as per
/// https://gpuweb.github.io/gpuweb/wgsl/#constructible-types
bool IsConstructible() const override;
/// @returns the size in bytes of the type.
uint32_t Size() const override;
/// @returns the alignment in bytes of the type.
uint32_t Align() const override;
};
} // namespace tint::sem
#endif // SRC_TINT_SEM_F16_H_

48
src/tint/sem/f16_test.cc Normal file
View File

@@ -0,0 +1,48 @@
// 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/sem/test_helper.h"
#include "src/tint/sem/texture.h"
namespace tint::sem {
namespace {
using F16Test = TestHelper;
TEST_F(F16Test, Creation) {
auto* a = create<F16>();
auto* b = create<F16>();
EXPECT_EQ(a, b);
}
TEST_F(F16Test, Hash) {
auto* a = create<F16>();
auto* b = create<F16>();
EXPECT_EQ(a->Hash(), b->Hash());
}
TEST_F(F16Test, Equals) {
auto* a = create<F16>();
auto* b = create<F16>();
EXPECT_TRUE(a->Equals(*b));
EXPECT_FALSE(a->Equals(Void{}));
}
TEST_F(F16Test, FriendlyName) {
F16 f;
EXPECT_EQ(f.FriendlyName(Symbols()), "f16");
}
} // namespace
} // namespace tint::sem

View File

@@ -15,6 +15,7 @@
#include "src/tint/sem/type.h"
#include "src/tint/sem/bool.h"
#include "src/tint/sem/f16.h"
#include "src/tint/sem/f32.h"
#include "src/tint/sem/i32.h"
#include "src/tint/sem/matrix.h"
@@ -64,15 +65,15 @@ bool Type::IsConstructible() const {
}
bool Type::is_scalar() const {
return IsAnyOf<F32, U32, I32, Bool>();
return IsAnyOf<F16, F32, U32, I32, Bool>();
}
bool Type::is_numeric_scalar() const {
return IsAnyOf<F32, U32, I32>();
return IsAnyOf<F16, F32, U32, I32>();
}
bool Type::is_float_scalar() const {
return Is<F32>();
return IsAnyOf<F16, F32>();
}
bool Type::is_float_matrix() const {