mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-18 01:15:39 +00:00
tint: add missing F16 conversion expression support
This CL add missing type conversions for f16, especially for SPIRV backend which require special handling. A transform, VectorizeMatrixConversions, are also added for SPIRV to replace a matrix conversion to a matrix construction with converted column vectors. Unittests for the transform and SPIRV writer, and end-to-end tests for all conversion rules are added. Bug: tint:1473, tint:1502, chromium:1356215 Change-Id: Iaff125e5dd295d35c4ab74757eb56b642802a51a Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/100483 Reviewed-by: David Neto <dneto@google.com> Commit-Queue: Zhaoming Jiang <zhaoming.jiang@intel.com> Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
committed by
Dawn LUCI CQ
parent
0df4e4aea3
commit
426b47e481
@@ -547,6 +547,8 @@ libtint_source_set("libtint_core_all_src") {
|
||||
"transform/utils/hoist_to_decl_before.h",
|
||||
"transform/var_for_dynamic_index.cc",
|
||||
"transform/var_for_dynamic_index.h",
|
||||
"transform/vectorize_matrix_conversions.cc",
|
||||
"transform/vectorize_matrix_conversions.h",
|
||||
"transform/vectorize_scalar_matrix_constructors.cc",
|
||||
"transform/vectorize_scalar_matrix_constructors.h",
|
||||
"transform/vertex_pulling.cc",
|
||||
@@ -1219,6 +1221,7 @@ if (tint_build_unittests) {
|
||||
"transform/utils/get_insertion_point_test.cc",
|
||||
"transform/utils/hoist_to_decl_before_test.cc",
|
||||
"transform/var_for_dynamic_index_test.cc",
|
||||
"transform/vectorize_matrix_conversions_test.cc",
|
||||
"transform/vectorize_scalar_matrix_constructors_test.cc",
|
||||
"transform/vertex_pulling_test.cc",
|
||||
"transform/while_to_loop_test.cc",
|
||||
|
||||
@@ -459,6 +459,8 @@ set(TINT_LIB_SRCS
|
||||
transform/utils/hoist_to_decl_before.h
|
||||
transform/var_for_dynamic_index.cc
|
||||
transform/var_for_dynamic_index.h
|
||||
transform/vectorize_matrix_conversions.cc
|
||||
transform/vectorize_matrix_conversions.h
|
||||
transform/vectorize_scalar_matrix_constructors.cc
|
||||
transform/vectorize_scalar_matrix_constructors.h
|
||||
transform/vertex_pulling.cc
|
||||
@@ -1131,6 +1133,7 @@ if(TINT_BUILD_TESTS)
|
||||
transform/unshadow_test.cc
|
||||
transform/unwind_discard_functions_test.cc
|
||||
transform/var_for_dynamic_index_test.cc
|
||||
transform/vectorize_matrix_conversions_test.cc
|
||||
transform/vectorize_scalar_matrix_constructors_test.cc
|
||||
transform/vertex_pulling_test.cc
|
||||
transform/while_to_loop_test.cc
|
||||
|
||||
136
src/tint/transform/vectorize_matrix_conversions.cc
Normal file
136
src/tint/transform/vectorize_matrix_conversions.cc
Normal file
@@ -0,0 +1,136 @@
|
||||
// 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/transform/vectorize_matrix_conversions.h"
|
||||
|
||||
#include <tuple>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
|
||||
#include "src/tint/program_builder.h"
|
||||
#include "src/tint/sem/abstract_numeric.h"
|
||||
#include "src/tint/sem/call.h"
|
||||
#include "src/tint/sem/expression.h"
|
||||
#include "src/tint/sem/type_conversion.h"
|
||||
#include "src/tint/utils/hash.h"
|
||||
#include "src/tint/utils/map.h"
|
||||
|
||||
TINT_INSTANTIATE_TYPEINFO(tint::transform::VectorizeMatrixConversions);
|
||||
|
||||
namespace tint::transform {
|
||||
|
||||
VectorizeMatrixConversions::VectorizeMatrixConversions() = default;
|
||||
|
||||
VectorizeMatrixConversions::~VectorizeMatrixConversions() = default;
|
||||
|
||||
bool VectorizeMatrixConversions::ShouldRun(const Program* program, const DataMap&) const {
|
||||
for (auto* node : program->ASTNodes().Objects()) {
|
||||
if (auto* sem = program->Sem().Get<sem::Expression>(node)) {
|
||||
if (auto* call = sem->UnwrapMaterialize()->As<sem::Call>()) {
|
||||
if (call->Target()->Is<sem::TypeConversion>() && call->Type()->Is<sem::Matrix>()) {
|
||||
auto& args = call->Arguments();
|
||||
if (args.Length() == 1 && args[0]->Type()->UnwrapRef()->is_float_matrix()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void VectorizeMatrixConversions::Run(CloneContext& ctx, const DataMap&, DataMap&) const {
|
||||
using HelperFunctionKey =
|
||||
utils::UnorderedKeyWrapper<std::tuple<const sem::Matrix*, const sem::Matrix*>>;
|
||||
|
||||
std::unordered_map<HelperFunctionKey, Symbol> matrix_convs;
|
||||
|
||||
ctx.ReplaceAll([&](const ast::CallExpression* expr) -> const ast::CallExpression* {
|
||||
auto* call = ctx.src->Sem().Get(expr)->UnwrapMaterialize()->As<sem::Call>();
|
||||
auto* ty_conv = call->Target()->As<sem::TypeConversion>();
|
||||
if (!ty_conv) {
|
||||
return nullptr;
|
||||
}
|
||||
auto* dst_type = call->Type()->As<sem::Matrix>();
|
||||
if (!dst_type) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto& args = call->Arguments();
|
||||
if (args.Length() != 1) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto& src = args[0];
|
||||
|
||||
auto* src_type = args[0]->Type()->UnwrapRef()->As<sem::Matrix>();
|
||||
if (!src_type) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// The source and destination type of a matrix conversion must have a same shape.
|
||||
if (!(src_type->rows() == dst_type->rows() && src_type->columns() == dst_type->columns())) {
|
||||
TINT_ICE(Transform, ctx.dst->Diagnostics())
|
||||
<< "source and destination matrix has different shape in matrix conversion";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto build_vectorized_conversion_expression = [&](auto&& src_expression_builder) {
|
||||
utils::Vector<const ast::Expression*, 4> columns;
|
||||
for (uint32_t c = 0; c < dst_type->columns(); c++) {
|
||||
auto* src_matrix_expr = src_expression_builder();
|
||||
auto* src_column_expr =
|
||||
ctx.dst->IndexAccessor(src_matrix_expr, ctx.dst->Expr(tint::AInt(c)));
|
||||
columns.Push(ctx.dst->Construct(CreateASTTypeFor(ctx, dst_type->ColumnType()),
|
||||
src_column_expr));
|
||||
}
|
||||
return ctx.dst->Construct(CreateASTTypeFor(ctx, dst_type), columns);
|
||||
};
|
||||
|
||||
// Replace the matrix conversion to column vector conversions and a matrix construction.
|
||||
if (!src->HasSideEffects()) {
|
||||
// Simply use the argument's declaration if it has no side effects.
|
||||
return build_vectorized_conversion_expression([&]() { //
|
||||
return ctx.Clone(src->Declaration());
|
||||
});
|
||||
} else {
|
||||
// If has side effects, use a helper function.
|
||||
auto fn =
|
||||
utils::GetOrCreate(matrix_convs, HelperFunctionKey{{src_type, dst_type}}, [&] {
|
||||
auto name =
|
||||
ctx.dst->Symbols().New("convert_mat" + std::to_string(src_type->columns()) +
|
||||
"x" + std::to_string(src_type->rows()) + "_" +
|
||||
ctx.dst->FriendlyName(src_type->type()) + "_" +
|
||||
ctx.dst->FriendlyName(dst_type->type()));
|
||||
ctx.dst->Func(
|
||||
name,
|
||||
utils::Vector{
|
||||
ctx.dst->Param("value", CreateASTTypeFor(ctx, src_type)),
|
||||
},
|
||||
CreateASTTypeFor(ctx, dst_type),
|
||||
utils::Vector{
|
||||
ctx.dst->Return(build_vectorized_conversion_expression([&]() { //
|
||||
return ctx.dst->Expr("value");
|
||||
})),
|
||||
});
|
||||
return name;
|
||||
});
|
||||
return ctx.dst->Call(fn, ctx.Clone(args[0]->Declaration()));
|
||||
}
|
||||
});
|
||||
|
||||
ctx.Clone();
|
||||
}
|
||||
|
||||
} // namespace tint::transform
|
||||
48
src/tint/transform/vectorize_matrix_conversions.h
Normal file
48
src/tint/transform/vectorize_matrix_conversions.h
Normal 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.
|
||||
|
||||
#ifndef SRC_TINT_TRANSFORM_VECTORIZE_MATRIX_CONVERSIONS_H_
|
||||
#define SRC_TINT_TRANSFORM_VECTORIZE_MATRIX_CONVERSIONS_H_
|
||||
|
||||
#include "src/tint/transform/transform.h"
|
||||
|
||||
namespace tint::transform {
|
||||
|
||||
/// A transform that converts matrix conversions (between f32 and f16 matrices) to the vector form.
|
||||
class VectorizeMatrixConversions final : public Castable<VectorizeMatrixConversions, Transform> {
|
||||
public:
|
||||
/// Constructor
|
||||
VectorizeMatrixConversions();
|
||||
|
||||
/// Destructor
|
||||
~VectorizeMatrixConversions() override;
|
||||
|
||||
/// @param program the program to inspect
|
||||
/// @param data optional extra transform-specific input data
|
||||
/// @returns true if this transform should be run for the given program
|
||||
bool ShouldRun(const Program* program, const DataMap& data = {}) const override;
|
||||
|
||||
protected:
|
||||
/// Runs the transform using the CloneContext built for transforming a
|
||||
/// program. Run() is responsible for calling Clone() on the CloneContext.
|
||||
/// @param ctx the CloneContext primed with the input program and
|
||||
/// ProgramBuilder
|
||||
/// @param inputs optional extra transform-specific input data
|
||||
/// @param outputs optional extra transform-specific output data
|
||||
void Run(CloneContext& ctx, const DataMap& inputs, DataMap& outputs) const override;
|
||||
};
|
||||
|
||||
} // namespace tint::transform
|
||||
|
||||
#endif // SRC_TINT_TRANSFORM_VECTORIZE_MATRIX_CONVERSIONS_H_
|
||||
411
src/tint/transform/vectorize_matrix_conversions_test.cc
Normal file
411
src/tint/transform/vectorize_matrix_conversions_test.cc
Normal file
@@ -0,0 +1,411 @@
|
||||
// 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/transform/vectorize_matrix_conversions.h"
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "src/tint/transform/test_helper.h"
|
||||
#include "src/tint/utils/string.h"
|
||||
|
||||
namespace tint::transform {
|
||||
namespace {
|
||||
|
||||
using VectorizeMatrixConversionsTest = TransformTestWithParam<std::pair<uint32_t, uint32_t>>;
|
||||
|
||||
TEST_F(VectorizeMatrixConversionsTest, ShouldRunEmptyModule) {
|
||||
auto* src = R"()";
|
||||
|
||||
EXPECT_FALSE(ShouldRun<VectorizeMatrixConversions>(src));
|
||||
}
|
||||
|
||||
// Test that VectorizeMatrixConversions transforms the matRxC<f32> to matRxC<f16> conversion as
|
||||
// expected.
|
||||
//
|
||||
// Example input:
|
||||
//
|
||||
// enable f16;
|
||||
//
|
||||
// @fragment
|
||||
// fn main() {
|
||||
// let m = mat3x2<f32>(vec2<f32>(0.0, 1.0), vec2<f32>(2.0, 3.0), vec2<f32>(4.0, 5.0));
|
||||
// let n : mat3x2<f16> = mat3x2<f16>(m);
|
||||
// }
|
||||
//
|
||||
// Example output:
|
||||
//
|
||||
// enable f16;
|
||||
//
|
||||
// @fragment
|
||||
// fn main() {
|
||||
// let m = mat3x2<f32>(vec2<f32>(0.0, 1.0), vec2<f32>(2.0, 3.0), vec2<f32>(4.0, 5.0));
|
||||
// let n : mat3x2<f16> = mat3x2<f16>(vec2<f16>(m[0]), vec2<f16>(m[1]), vec2<f16>(m[2]));
|
||||
// }
|
||||
TEST_P(VectorizeMatrixConversionsTest, Conversion_F32ToF16) {
|
||||
uint32_t cols = GetParam().first;
|
||||
uint32_t rows = GetParam().second;
|
||||
std::string src_mat_type = "mat" + std::to_string(cols) + "x" + std::to_string(rows) + "<f32>";
|
||||
std::string src_vec_type = "vec" + std::to_string(rows) + "<f32>";
|
||||
std::string dst_mat_type = "mat" + std::to_string(cols) + "x" + std::to_string(rows) + "<f16>";
|
||||
std::string dst_vec_type = "vec" + std::to_string(rows) + "<f16>";
|
||||
std::string vector_values;
|
||||
for (uint32_t c = 0; c < cols; c++) {
|
||||
if (c > 0) {
|
||||
vector_values += ", ";
|
||||
}
|
||||
vector_values += src_vec_type + "(";
|
||||
for (uint32_t r = 0; r < rows; r++) {
|
||||
if (r > 0) {
|
||||
vector_values += ", ";
|
||||
}
|
||||
auto value = std::to_string(c * rows + r) + ".0";
|
||||
vector_values += value;
|
||||
}
|
||||
vector_values += ")";
|
||||
}
|
||||
|
||||
std::string vectorized_args = "";
|
||||
for (uint32_t c = 0; c < cols; c++) {
|
||||
if (c > 0) {
|
||||
vectorized_args += ", ";
|
||||
}
|
||||
vectorized_args += dst_vec_type + "(m[" + std::to_string(c) + "])";
|
||||
}
|
||||
|
||||
std::string tmpl = R"(
|
||||
enable f16;
|
||||
|
||||
@fragment
|
||||
fn main() {
|
||||
let m = ${src_mat_type}(${values});
|
||||
let n : ${dst_mat_type} = ${dst_mat_type}(${args});
|
||||
}
|
||||
)";
|
||||
tmpl = utils::ReplaceAll(tmpl, "${src_mat_type}", src_mat_type);
|
||||
tmpl = utils::ReplaceAll(tmpl, "${dst_mat_type}", dst_mat_type);
|
||||
tmpl = utils::ReplaceAll(tmpl, "${values}", vector_values);
|
||||
auto src = utils::ReplaceAll(tmpl, "${args}", "m");
|
||||
auto expect = utils::ReplaceAll(tmpl, "${args}", vectorized_args);
|
||||
|
||||
EXPECT_TRUE(ShouldRun<VectorizeMatrixConversions>(src));
|
||||
|
||||
auto got = Run<VectorizeMatrixConversions>(src);
|
||||
|
||||
EXPECT_EQ(expect, str(got));
|
||||
}
|
||||
|
||||
// Test that VectorizeMatrixConversions transforms the matRxC<f32> to matRxC<f16> conversion as
|
||||
// expected.
|
||||
//
|
||||
// Example input:
|
||||
//
|
||||
// enable f16;
|
||||
//
|
||||
// @fragment
|
||||
// fn main() {
|
||||
// let m = mat3x2<f16>(vec2<f16>(0.0, 1.0), vec2<f16>(2.0, 3.0), vec2<f16>(4.0, 5.0));
|
||||
// let n : mat3x2<f32> = mat3x2<f32>(m);
|
||||
// }
|
||||
//
|
||||
// Example output:
|
||||
//
|
||||
// enable f16;
|
||||
//
|
||||
// @fragment
|
||||
// fn main() {
|
||||
// let m = mat3x2<f16>(vec2<f16>(0.0, 1.0), vec2<f16>(2.0, 3.0), vec2<f16>(4.0, 5.0));
|
||||
// let n : mat3x2<f32> = mat3x2<f32>(vec2<f32>(m[0]), vec2<f32>(m[1]), vec2<f32>(m[2]));
|
||||
// }
|
||||
TEST_P(VectorizeMatrixConversionsTest, Conversion_F16ToF32) {
|
||||
uint32_t cols = GetParam().first;
|
||||
uint32_t rows = GetParam().second;
|
||||
std::string src_mat_type = "mat" + std::to_string(cols) + "x" + std::to_string(rows) + "<f16>";
|
||||
std::string src_vec_type = "vec" + std::to_string(rows) + "<f16>";
|
||||
std::string dst_mat_type = "mat" + std::to_string(cols) + "x" + std::to_string(rows) + "<f32>";
|
||||
std::string dst_vec_type = "vec" + std::to_string(rows) + "<f32>";
|
||||
std::string vector_values;
|
||||
for (uint32_t c = 0; c < cols; c++) {
|
||||
if (c > 0) {
|
||||
vector_values += ", ";
|
||||
}
|
||||
vector_values += src_vec_type + "(";
|
||||
for (uint32_t r = 0; r < rows; r++) {
|
||||
if (r > 0) {
|
||||
vector_values += ", ";
|
||||
}
|
||||
auto value = std::to_string(c * rows + r) + ".0";
|
||||
vector_values += value;
|
||||
}
|
||||
vector_values += ")";
|
||||
}
|
||||
|
||||
std::string vectorized_args = "";
|
||||
for (uint32_t c = 0; c < cols; c++) {
|
||||
if (c > 0) {
|
||||
vectorized_args += ", ";
|
||||
}
|
||||
vectorized_args += dst_vec_type + "(m[" + std::to_string(c) + "])";
|
||||
}
|
||||
|
||||
std::string tmpl = R"(
|
||||
enable f16;
|
||||
|
||||
@fragment
|
||||
fn main() {
|
||||
let m = ${src_mat_type}(${values});
|
||||
let n : ${dst_mat_type} = ${dst_mat_type}(${args});
|
||||
}
|
||||
)";
|
||||
tmpl = utils::ReplaceAll(tmpl, "${src_mat_type}", src_mat_type);
|
||||
tmpl = utils::ReplaceAll(tmpl, "${dst_mat_type}", dst_mat_type);
|
||||
tmpl = utils::ReplaceAll(tmpl, "${values}", vector_values);
|
||||
auto src = utils::ReplaceAll(tmpl, "${args}", "m");
|
||||
auto expect = utils::ReplaceAll(tmpl, "${args}", vectorized_args);
|
||||
|
||||
EXPECT_TRUE(ShouldRun<VectorizeMatrixConversions>(src));
|
||||
|
||||
auto got = Run<VectorizeMatrixConversions>(src);
|
||||
|
||||
EXPECT_EQ(expect, str(got));
|
||||
}
|
||||
|
||||
// Test that VectorizeMatrixConversions transform generates help functions for conversions of which
|
||||
// input expression has side effect.
|
||||
//
|
||||
// Example input:
|
||||
//
|
||||
// enable f16;
|
||||
//
|
||||
// var<private> i : i32 = 0;
|
||||
//
|
||||
// fn mat_f32() -> mat2x2<f32> {
|
||||
// i = (i + 1);
|
||||
// return mat2x2<f32>(vec2<f32>(f32(i), f32(i)), vec2<f32>(f32(i), f32(i)));
|
||||
// }
|
||||
//
|
||||
// fn mat_f16() -> mat2x2<f16> {
|
||||
// i = (i + 1);
|
||||
// return mat2x2<f16>(vec2<f16>(f16(i), f16(i)), vec2<f16>(f16(i), f16(i)));
|
||||
// }
|
||||
//
|
||||
// @fragment
|
||||
// fn main() {
|
||||
// let m32 : mat2x2<f32> = mat2x2<f32>(mat_f16());
|
||||
// let m16 : mat2x2<f16> = mat2x2<f16>(mat_f32());
|
||||
// }
|
||||
//
|
||||
// Example output:
|
||||
//
|
||||
// enable f16;
|
||||
//
|
||||
// var<private> i : i32 = 0;
|
||||
//
|
||||
// fn mat_f32() -> mat2x2<f32> {
|
||||
// i = (i + 1);
|
||||
// return mat2x2<f32>(vec2<f32>(f32(i), f32(i)), vec2<f32>(f32(i), f32(i)));
|
||||
// }
|
||||
//
|
||||
// fn mat_f16() -> mat2x2<f16> {
|
||||
// i = (i + 1);
|
||||
// return mat2x2<f16>(vec2<f16>(f16(i), f16(i)), vec2<f16>(f16(i), f16(i)));
|
||||
// }
|
||||
//
|
||||
// fn convert_mat2x2_f16_f32(value : mat2x2<f16>) -> mat2x2<f32> {
|
||||
// return mat2x2<f32>(vec2<f32>(value[0]), vec2<f32>(value[1]));
|
||||
// }
|
||||
//
|
||||
// fn convert_mat2x2_f32_f16(value : mat2x2<f32>) -> mat2x2<f16> {
|
||||
// return mat2x2<f16>(vec2<f16>(value[0]), vec2<f16>(value[1]));
|
||||
// }
|
||||
//
|
||||
// @fragment
|
||||
// fn main() {
|
||||
// let m32 : mat2x2<f32> = convert_mat2x2_f16_f32(mat_f16());
|
||||
// let m16 : mat2x2<f16> = convert_mat2x2_f32_f16(mat_f32());
|
||||
// }
|
||||
TEST_P(VectorizeMatrixConversionsTest, Conversion_WithSideEffect) {
|
||||
uint32_t cols = GetParam().first;
|
||||
uint32_t rows = GetParam().second;
|
||||
std::string mat_shape = "mat" + std::to_string(cols) + "x" + std::to_string(rows);
|
||||
std::string f32_mat_type = mat_shape + "<f32>";
|
||||
std::string f32_vec_type = "vec" + std::to_string(rows) + "<f32>";
|
||||
std::string f16_mat_type = mat_shape + "<f16>";
|
||||
std::string f16_vec_type = "vec" + std::to_string(rows) + "<f16>";
|
||||
std::string f32_vector_values;
|
||||
std::string f16_vector_values;
|
||||
for (uint32_t c = 0; c < cols; c++) {
|
||||
if (c > 0) {
|
||||
f32_vector_values += ", ";
|
||||
f16_vector_values += ", ";
|
||||
}
|
||||
f32_vector_values += f32_vec_type + "(";
|
||||
f16_vector_values += f16_vec_type + "(";
|
||||
for (uint32_t r = 0; r < rows; r++) {
|
||||
if (r > 0) {
|
||||
f32_vector_values += ", ";
|
||||
f16_vector_values += ", ";
|
||||
}
|
||||
f32_vector_values += "f32(i)";
|
||||
f16_vector_values += "f16(i)";
|
||||
}
|
||||
f32_vector_values += ")";
|
||||
f16_vector_values += ")";
|
||||
}
|
||||
|
||||
std::string f32_vectorized_args = "";
|
||||
std::string f16_vectorized_args = "";
|
||||
for (uint32_t c = 0; c < cols; c++) {
|
||||
if (c > 0) {
|
||||
f32_vectorized_args += ", ";
|
||||
f16_vectorized_args += ", ";
|
||||
}
|
||||
f32_vectorized_args += f32_vec_type + "(value[" + std::to_string(c) + "])";
|
||||
f16_vectorized_args += f16_vec_type + "(value[" + std::to_string(c) + "])";
|
||||
}
|
||||
|
||||
std::string tmpl = R"(
|
||||
enable f16;
|
||||
|
||||
var<private> i : i32 = 0;
|
||||
|
||||
fn mat_f32() -> ${f32_mat_type} {
|
||||
i = (i + 1);
|
||||
return ${f32_mat_type}(${f32_values});
|
||||
}
|
||||
|
||||
fn mat_f16() -> ${f16_mat_type} {
|
||||
i = (i + 1);
|
||||
return ${f16_mat_type}(${f16_values});
|
||||
}
|
||||
${helper_function}
|
||||
@fragment
|
||||
fn main() {
|
||||
let m32 : ${f32_mat_type} = ${f32_matrix_conversion};
|
||||
let m16 : ${f16_mat_type} = ${f16_matrix_conversion};
|
||||
}
|
||||
)";
|
||||
tmpl = utils::ReplaceAll(tmpl, "${f32_values}", f32_vector_values);
|
||||
tmpl = utils::ReplaceAll(tmpl, "${f16_values}", f16_vector_values);
|
||||
auto src = utils::ReplaceAll(tmpl, "${f32_matrix_conversion}", "${f32_mat_type}(mat_f16())");
|
||||
src = utils::ReplaceAll(src, "${f16_matrix_conversion}", "${f16_mat_type}(mat_f32())");
|
||||
src = utils::ReplaceAll(src, "${helper_function}", "");
|
||||
src = utils::ReplaceAll(src, "${f32_mat_type}", f32_mat_type);
|
||||
src = utils::ReplaceAll(src, "${f16_mat_type}", f16_mat_type);
|
||||
|
||||
auto helper_function = std::string(R"(
|
||||
fn convert_${mat_shape}_f16_f32(value : ${f16_mat_type}) -> ${f32_mat_type} {
|
||||
return ${f32_mat_type}(${f32_vectorized_args});
|
||||
}
|
||||
|
||||
fn convert_${mat_shape}_f32_f16(value : ${f32_mat_type}) -> ${f16_mat_type} {
|
||||
return ${f16_mat_type}(${f16_vectorized_args});
|
||||
}
|
||||
)");
|
||||
auto expect = utils::ReplaceAll(tmpl, "${helper_function}", helper_function);
|
||||
expect = utils::ReplaceAll(expect, "${f32_mat_type}", f32_mat_type);
|
||||
expect = utils::ReplaceAll(expect, "${f16_mat_type}", f16_mat_type);
|
||||
expect = utils::ReplaceAll(expect, "${f32_matrix_conversion}",
|
||||
"convert_${mat_shape}_f16_f32(mat_f16())");
|
||||
expect = utils::ReplaceAll(expect, "${f16_matrix_conversion}",
|
||||
"convert_${mat_shape}_f32_f16(mat_f32())");
|
||||
expect = utils::ReplaceAll(expect, "${mat_shape}", mat_shape);
|
||||
expect = utils::ReplaceAll(expect, "${f32_vectorized_args}", f32_vectorized_args);
|
||||
expect = utils::ReplaceAll(expect, "${f16_vectorized_args}", f16_vectorized_args);
|
||||
|
||||
EXPECT_TRUE(ShouldRun<VectorizeMatrixConversions>(src));
|
||||
|
||||
auto got = Run<VectorizeMatrixConversions>(src);
|
||||
|
||||
EXPECT_EQ(expect, str(got));
|
||||
}
|
||||
|
||||
// Test that VectorizeMatrixConversions transform will not run for matrix constructor.
|
||||
TEST_P(VectorizeMatrixConversionsTest, NonConversion_ConstructorFromVectors) {
|
||||
uint32_t cols = GetParam().first;
|
||||
uint32_t rows = GetParam().second;
|
||||
std::string mat_type = "mat" + std::to_string(cols) + "x" + std::to_string(rows) + "<f32>";
|
||||
std::string vec_type = "vec" + std::to_string(rows) + "<f32>";
|
||||
std::string columns;
|
||||
for (uint32_t c = 0; c < cols; c++) {
|
||||
if (c > 0) {
|
||||
columns += ", ";
|
||||
}
|
||||
columns += vec_type + "()";
|
||||
}
|
||||
|
||||
std::string tmpl = R"(
|
||||
@fragment
|
||||
fn main() {
|
||||
let m = ${matrix}(${columns});
|
||||
}
|
||||
)";
|
||||
tmpl = utils::ReplaceAll(tmpl, "${matrix}", mat_type);
|
||||
auto src = utils::ReplaceAll(tmpl, "${columns}", columns);
|
||||
auto expect = src;
|
||||
|
||||
EXPECT_FALSE(ShouldRun<VectorizeMatrixConversions>(src));
|
||||
|
||||
auto got = Run<VectorizeMatrixConversions>(src);
|
||||
|
||||
EXPECT_EQ(expect, str(got));
|
||||
}
|
||||
|
||||
// Test that VectorizeMatrixConversions transform will not run for identity matrix constructor,
|
||||
// which also take a single matrix as input.
|
||||
TEST_P(VectorizeMatrixConversionsTest, NonConversion_IdentityConstructor) {
|
||||
uint32_t cols = GetParam().first;
|
||||
uint32_t rows = GetParam().second;
|
||||
std::string mat_type = "mat" + std::to_string(cols) + "x" + std::to_string(rows) + "<f32>";
|
||||
std::string vec_type = "vec" + std::to_string(rows) + "<f32>";
|
||||
std::string columns;
|
||||
for (uint32_t c = 0; c < cols; c++) {
|
||||
if (c > 0) {
|
||||
columns += ", ";
|
||||
}
|
||||
columns += vec_type + "()";
|
||||
}
|
||||
|
||||
std::string tmpl = R"(
|
||||
@fragment
|
||||
fn main() {
|
||||
let m = ${matrix}(${columns});
|
||||
let n : ${matrix} = ${matrix}(m);
|
||||
}
|
||||
)";
|
||||
tmpl = utils::ReplaceAll(tmpl, "${matrix}", mat_type);
|
||||
auto src = utils::ReplaceAll(tmpl, "${columns}", columns);
|
||||
auto expect = src;
|
||||
|
||||
EXPECT_FALSE(ShouldRun<VectorizeMatrixConversions>(src));
|
||||
|
||||
auto got = Run<VectorizeMatrixConversions>(src);
|
||||
|
||||
EXPECT_EQ(expect, str(got));
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(VectorizeMatrixConversionsTest,
|
||||
VectorizeMatrixConversionsTest,
|
||||
testing::Values(std::make_pair(2, 2),
|
||||
std::make_pair(2, 3),
|
||||
std::make_pair(2, 4),
|
||||
std::make_pair(3, 2),
|
||||
std::make_pair(3, 3),
|
||||
std::make_pair(3, 4),
|
||||
std::make_pair(4, 2),
|
||||
std::make_pair(4, 3),
|
||||
std::make_pair(4, 4)));
|
||||
|
||||
} // namespace
|
||||
} // namespace tint::transform
|
||||
@@ -1390,8 +1390,7 @@ uint32_t Builder::GenerateTypeConstructorOrConversion(const sem::Call* call,
|
||||
auto* value_type = args[0]->Type()->UnwrapRef();
|
||||
if (auto* val_mat = value_type->As<sem::Matrix>()) {
|
||||
// Generate passthrough for matrices of the same type
|
||||
can_cast_or_copy =
|
||||
(res_mat->columns() == val_mat->columns()) && (res_mat->rows() == val_mat->rows());
|
||||
can_cast_or_copy = res_mat == val_mat;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1578,13 +1577,19 @@ uint32_t Builder::GenerateCastOrCopyOrPassthrough(const sem::Type* to_type,
|
||||
} else if ((from_type->is_float_scalar() && to_type->Is<sem::U32>()) ||
|
||||
(from_type->is_float_vector() && to_type->is_unsigned_integer_vector())) {
|
||||
op = spv::Op::OpConvertFToU;
|
||||
} else if ((from_type->Is<sem::Bool>() && to_type->Is<sem::Bool>()) ||
|
||||
(from_type->Is<sem::U32>() && to_type->Is<sem::U32>()) ||
|
||||
(from_type->Is<sem::I32>() && to_type->Is<sem::I32>()) ||
|
||||
(from_type->Is<sem::F32>() && to_type->Is<sem::F32>()) ||
|
||||
(from_type->Is<sem::F16>() && to_type->Is<sem::F16>()) ||
|
||||
(from_type->Is<sem::Vector>() && (from_type == to_type))) {
|
||||
} else if (from_type
|
||||
->IsAnyOf<sem::Bool, sem::F32, sem::I32, sem::U32, sem::F16, sem::Vector>() &&
|
||||
from_type == to_type) {
|
||||
// Identity constructor for scalar and vector types
|
||||
return val_id;
|
||||
} else if ((from_type->is_float_scalar() && to_type->is_float_scalar()) ||
|
||||
(from_type->is_float_vector() && to_type->is_float_vector() &&
|
||||
from_type->As<sem::Vector>()->Width() == to_type->As<sem::Vector>()->Width())) {
|
||||
// Convert between f32 and f16 types.
|
||||
// OpFConvert requires the scalar component types to be different, and the case of from_type
|
||||
// and to_type being the same floating point scalar or vector type, i.e. identity
|
||||
// constructor, is already handled in the previous else-if clause.
|
||||
op = spv::Op::OpFConvert;
|
||||
} else if ((from_type->Is<sem::I32>() && to_type->Is<sem::U32>()) ||
|
||||
(from_type->Is<sem::U32>() && to_type->Is<sem::I32>()) ||
|
||||
(from_type->is_signed_integer_vector() && to_type->is_unsigned_integer_vector()) ||
|
||||
@@ -1644,8 +1649,18 @@ uint32_t Builder::GenerateCastOrCopyOrPassthrough(const sem::Type* to_type,
|
||||
}
|
||||
|
||||
return result_id;
|
||||
} else if (from_type->Is<sem::Matrix>()) {
|
||||
return val_id;
|
||||
} else if (from_type->Is<sem::Matrix>() && to_type->Is<sem::Matrix>()) {
|
||||
// SPIRV does not support matrix conversion, the only valid case is matrix identity
|
||||
// constructor. Matrix conversion between f32 and f16 should be transformed into vector
|
||||
// conversions for each column vectors by VectorizeMatrixConversions.
|
||||
auto* from_mat = from_type->As<sem::Matrix>();
|
||||
auto* to_mat = to_type->As<sem::Matrix>();
|
||||
if (from_mat == to_mat) {
|
||||
return val_id;
|
||||
}
|
||||
TINT_ICE(Writer, builder_.Diagnostics())
|
||||
<< "matrix conversion is not supported and should have been handled by "
|
||||
"VectorizeMatrixConversions";
|
||||
} else {
|
||||
TINT_ICE(Writer, builder_.Diagnostics()) << "Invalid from_type";
|
||||
}
|
||||
|
||||
@@ -3927,30 +3927,6 @@ TEST_F(SpvBuilderConstructorTest, Type_Convert_U32_To_I32) {
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(SpvBuilderConstructorTest, Type_Convert_I32_To_U32) {
|
||||
auto* var = Decl(Var("x", ty.i32(), Expr(2_i)));
|
||||
auto* cast = Construct<u32>("x");
|
||||
WrapInFunction(var, cast);
|
||||
|
||||
spirv::Builder& b = Build();
|
||||
|
||||
b.push_function(Function{});
|
||||
EXPECT_TRUE(b.GenerateStatement(var)) << b.error();
|
||||
EXPECT_EQ(b.GenerateExpression(cast), 6u) << b.error();
|
||||
|
||||
EXPECT_EQ(DumpInstructions(b.types()), R"(%1 = OpTypeInt 32 1
|
||||
%2 = OpConstant %1 2
|
||||
%4 = OpTypePointer Function %1
|
||||
%5 = OpConstantNull %1
|
||||
%7 = OpTypeInt 32 0
|
||||
)");
|
||||
EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
|
||||
R"(OpStore %3 %2
|
||||
%8 = OpLoad %1 %3
|
||||
%6 = OpBitcast %7 %8
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(SpvBuilderConstructorTest, Type_Convert_F32_To_I32) {
|
||||
auto* var = Decl(Var("x", ty.f32(), Expr(2.4_f)));
|
||||
auto* cast = Construct<i32>("x");
|
||||
@@ -4001,6 +3977,30 @@ TEST_F(SpvBuilderConstructorTest, Type_Convert_F16_To_I32) {
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(SpvBuilderConstructorTest, Type_Convert_I32_To_U32) {
|
||||
auto* var = Decl(Var("x", ty.i32(), Expr(2_i)));
|
||||
auto* cast = Construct<u32>("x");
|
||||
WrapInFunction(var, cast);
|
||||
|
||||
spirv::Builder& b = Build();
|
||||
|
||||
b.push_function(Function{});
|
||||
EXPECT_TRUE(b.GenerateStatement(var)) << b.error();
|
||||
EXPECT_EQ(b.GenerateExpression(cast), 6u) << b.error();
|
||||
|
||||
EXPECT_EQ(DumpInstructions(b.types()), R"(%1 = OpTypeInt 32 1
|
||||
%2 = OpConstant %1 2
|
||||
%4 = OpTypePointer Function %1
|
||||
%5 = OpConstantNull %1
|
||||
%7 = OpTypeInt 32 0
|
||||
)");
|
||||
EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
|
||||
R"(OpStore %3 %2
|
||||
%8 = OpLoad %1 %3
|
||||
%6 = OpBitcast %7 %8
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(SpvBuilderConstructorTest, Type_Convert_F32_To_U32) {
|
||||
auto* var = Decl(Var("x", ty.f32(), Expr(2.4_f)));
|
||||
auto* cast = Construct<u32>("x");
|
||||
@@ -4075,6 +4075,56 @@ TEST_F(SpvBuilderConstructorTest, Type_Convert_I32_To_F32) {
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(SpvBuilderConstructorTest, Type_Convert_U32_To_F32) {
|
||||
auto* var = Decl(Var("x", ty.u32(), Expr(2_u)));
|
||||
auto* cast = Construct<f32>("x");
|
||||
WrapInFunction(var, cast);
|
||||
|
||||
spirv::Builder& b = Build();
|
||||
|
||||
b.push_function(Function{});
|
||||
EXPECT_TRUE(b.GenerateStatement(var)) << b.error();
|
||||
EXPECT_EQ(b.GenerateExpression(cast), 6u) << b.error();
|
||||
|
||||
EXPECT_EQ(DumpInstructions(b.types()), R"(%1 = OpTypeInt 32 0
|
||||
%2 = OpConstant %1 2
|
||||
%4 = OpTypePointer Function %1
|
||||
%5 = OpConstantNull %1
|
||||
%7 = OpTypeFloat 32
|
||||
)");
|
||||
EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
|
||||
R"(OpStore %3 %2
|
||||
%8 = OpLoad %1 %3
|
||||
%6 = OpConvertUToF %7 %8
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(SpvBuilderConstructorTest, Type_Convert_F16_To_F32) {
|
||||
Enable(ast::Extension::kF16);
|
||||
|
||||
auto* var = Decl(Var("x", ty.f16(), Expr(2_h)));
|
||||
auto* cast = Construct<f32>("x");
|
||||
WrapInFunction(var, cast);
|
||||
|
||||
spirv::Builder& b = Build();
|
||||
|
||||
b.push_function(Function{});
|
||||
EXPECT_TRUE(b.GenerateStatement(var)) << b.error();
|
||||
EXPECT_EQ(b.GenerateExpression(cast), 6u) << b.error();
|
||||
|
||||
EXPECT_EQ(DumpInstructions(b.types()), R"(%1 = OpTypeFloat 16
|
||||
%2 = OpConstant %1 0x1p+1
|
||||
%4 = OpTypePointer Function %1
|
||||
%5 = OpConstantNull %1
|
||||
%7 = OpTypeFloat 32
|
||||
)");
|
||||
EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
|
||||
R"(OpStore %3 %2
|
||||
%8 = OpLoad %1 %3
|
||||
%6 = OpFConvert %7 %8
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(SpvBuilderConstructorTest, Type_Convert_I32_To_F16) {
|
||||
Enable(ast::Extension::kF16);
|
||||
|
||||
@@ -4101,30 +4151,6 @@ TEST_F(SpvBuilderConstructorTest, Type_Convert_I32_To_F16) {
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(SpvBuilderConstructorTest, Type_Convert_U32_To_F32) {
|
||||
auto* var = Decl(Var("x", ty.u32(), Expr(2_u)));
|
||||
auto* cast = Construct<f32>("x");
|
||||
WrapInFunction(var, cast);
|
||||
|
||||
spirv::Builder& b = Build();
|
||||
|
||||
b.push_function(Function{});
|
||||
EXPECT_TRUE(b.GenerateStatement(var)) << b.error();
|
||||
EXPECT_EQ(b.GenerateExpression(cast), 6u) << b.error();
|
||||
|
||||
EXPECT_EQ(DumpInstructions(b.types()), R"(%1 = OpTypeInt 32 0
|
||||
%2 = OpConstant %1 2
|
||||
%4 = OpTypePointer Function %1
|
||||
%5 = OpConstantNull %1
|
||||
%7 = OpTypeFloat 32
|
||||
)");
|
||||
EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
|
||||
R"(OpStore %3 %2
|
||||
%8 = OpLoad %1 %3
|
||||
%6 = OpConvertUToF %7 %8
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(SpvBuilderConstructorTest, Type_Convert_U32_To_F16) {
|
||||
Enable(ast::Extension::kF16);
|
||||
|
||||
@@ -4151,6 +4177,32 @@ TEST_F(SpvBuilderConstructorTest, Type_Convert_U32_To_F16) {
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(SpvBuilderConstructorTest, Type_Convert_F32_To_F16) {
|
||||
Enable(ast::Extension::kF16);
|
||||
|
||||
auto* var = Decl(Var("x", ty.f32(), Expr(2_f)));
|
||||
auto* cast = Construct<f16>("x");
|
||||
WrapInFunction(var, cast);
|
||||
|
||||
spirv::Builder& b = Build();
|
||||
|
||||
b.push_function(Function{});
|
||||
EXPECT_TRUE(b.GenerateStatement(var)) << b.error();
|
||||
EXPECT_EQ(b.GenerateExpression(cast), 6u) << b.error();
|
||||
|
||||
EXPECT_EQ(DumpInstructions(b.types()), R"(%1 = OpTypeFloat 32
|
||||
%2 = OpConstant %1 2
|
||||
%4 = OpTypePointer Function %1
|
||||
%5 = OpConstantNull %1
|
||||
%7 = OpTypeFloat 16
|
||||
)");
|
||||
EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
|
||||
R"(OpStore %3 %2
|
||||
%8 = OpLoad %1 %3
|
||||
%6 = OpFConvert %7 %8
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(SpvBuilderConstructorTest, Type_Convert_Vectors_U32_to_I32) {
|
||||
auto* var = GlobalVar("i", ty.vec3<u32>(), ast::StorageClass::kPrivate);
|
||||
|
||||
@@ -4337,6 +4389,60 @@ TEST_F(SpvBuilderConstructorTest, Type_Convert_Vectors_I32_to_F32) {
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(SpvBuilderConstructorTest, Type_Convert_Vectors_U32_to_F32) {
|
||||
auto* var = GlobalVar("i", ty.vec3<u32>(), ast::StorageClass::kPrivate);
|
||||
|
||||
auto* cast = vec3<f32>("i");
|
||||
WrapInFunction(cast);
|
||||
|
||||
spirv::Builder& b = Build();
|
||||
|
||||
b.push_function(Function{});
|
||||
ASSERT_TRUE(b.GenerateGlobalVariable(var)) << b.error();
|
||||
EXPECT_EQ(b.GenerateExpression(cast), 6u) << b.error();
|
||||
|
||||
EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeInt 32 0
|
||||
%3 = OpTypeVector %4 3
|
||||
%2 = OpTypePointer Private %3
|
||||
%5 = OpConstantNull %3
|
||||
%1 = OpVariable %2 Private %5
|
||||
%8 = OpTypeFloat 32
|
||||
%7 = OpTypeVector %8 3
|
||||
)");
|
||||
EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
|
||||
R"(%9 = OpLoad %3 %1
|
||||
%6 = OpConvertUToF %7 %9
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(SpvBuilderConstructorTest, Type_Convert_Vectors_F16_to_F32) {
|
||||
Enable(ast::Extension::kF16);
|
||||
|
||||
auto* var = GlobalVar("i", ty.vec3<f16>(), ast::StorageClass::kPrivate);
|
||||
|
||||
auto* cast = vec3<f32>("i");
|
||||
WrapInFunction(cast);
|
||||
|
||||
spirv::Builder& b = Build();
|
||||
|
||||
b.push_function(Function{});
|
||||
ASSERT_TRUE(b.GenerateGlobalVariable(var)) << b.error();
|
||||
EXPECT_EQ(b.GenerateExpression(cast), 6u) << b.error();
|
||||
|
||||
EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeFloat 16
|
||||
%3 = OpTypeVector %4 3
|
||||
%2 = OpTypePointer Private %3
|
||||
%5 = OpConstantNull %3
|
||||
%1 = OpVariable %2 Private %5
|
||||
%8 = OpTypeFloat 32
|
||||
%7 = OpTypeVector %8 3
|
||||
)");
|
||||
EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
|
||||
R"(%9 = OpLoad %3 %1
|
||||
%6 = OpFConvert %7 %9
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(SpvBuilderConstructorTest, Type_Convert_Vectors_I32_to_F16) {
|
||||
Enable(ast::Extension::kF16);
|
||||
|
||||
@@ -4365,32 +4471,6 @@ TEST_F(SpvBuilderConstructorTest, Type_Convert_Vectors_I32_to_F16) {
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(SpvBuilderConstructorTest, Type_Convert_Vectors_U32_to_F32) {
|
||||
auto* var = GlobalVar("i", ty.vec3<u32>(), ast::StorageClass::kPrivate);
|
||||
|
||||
auto* cast = vec3<f32>("i");
|
||||
WrapInFunction(cast);
|
||||
|
||||
spirv::Builder& b = Build();
|
||||
|
||||
b.push_function(Function{});
|
||||
ASSERT_TRUE(b.GenerateGlobalVariable(var)) << b.error();
|
||||
EXPECT_EQ(b.GenerateExpression(cast), 6u) << b.error();
|
||||
|
||||
EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeInt 32 0
|
||||
%3 = OpTypeVector %4 3
|
||||
%2 = OpTypePointer Private %3
|
||||
%5 = OpConstantNull %3
|
||||
%1 = OpVariable %2 Private %5
|
||||
%8 = OpTypeFloat 32
|
||||
%7 = OpTypeVector %8 3
|
||||
)");
|
||||
EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
|
||||
R"(%9 = OpLoad %3 %1
|
||||
%6 = OpConvertUToF %7 %9
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(SpvBuilderConstructorTest, Type_Convert_Vectors_U32_to_F16) {
|
||||
Enable(ast::Extension::kF16);
|
||||
|
||||
@@ -4419,6 +4499,34 @@ TEST_F(SpvBuilderConstructorTest, Type_Convert_Vectors_U32_to_F16) {
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(SpvBuilderConstructorTest, Type_Convert_Vectors_F32_to_F16) {
|
||||
Enable(ast::Extension::kF16);
|
||||
|
||||
auto* var = GlobalVar("i", ty.vec3<f32>(), ast::StorageClass::kPrivate);
|
||||
|
||||
auto* cast = vec3<f16>("i");
|
||||
WrapInFunction(cast);
|
||||
|
||||
spirv::Builder& b = Build();
|
||||
|
||||
b.push_function(Function{});
|
||||
ASSERT_TRUE(b.GenerateGlobalVariable(var)) << b.error();
|
||||
EXPECT_EQ(b.GenerateExpression(cast), 6u) << b.error();
|
||||
|
||||
EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeFloat 32
|
||||
%3 = OpTypeVector %4 3
|
||||
%2 = OpTypePointer Private %3
|
||||
%5 = OpConstantNull %3
|
||||
%1 = OpVariable %2 Private %5
|
||||
%8 = OpTypeFloat 16
|
||||
%7 = OpTypeVector %8 3
|
||||
)");
|
||||
EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
|
||||
R"(%9 = OpLoad %3 %1
|
||||
%6 = OpFConvert %7 %9
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(SpvBuilderConstructorTest, IsConstructorConst_GlobalVectorWithAllConstConstructors) {
|
||||
// vec3<f32>(1.0, 2.0, 3.0) -> true
|
||||
auto* t = vec3<f32>(1_f, 2_f, 3_f);
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
#include "src/tint/transform/unshadow.h"
|
||||
#include "src/tint/transform/unwind_discard_functions.h"
|
||||
#include "src/tint/transform/var_for_dynamic_index.h"
|
||||
#include "src/tint/transform/vectorize_matrix_conversions.h"
|
||||
#include "src/tint/transform/vectorize_scalar_matrix_constructors.h"
|
||||
#include "src/tint/transform/while_to_loop.h"
|
||||
#include "src/tint/transform/zero_init_workgroup_memory.h"
|
||||
@@ -78,6 +79,7 @@ SanitizedResult Sanitize(const Program* in, const Options& options) {
|
||||
manager.Add<transform::SimplifyPointers>(); // Required for arrayLength()
|
||||
manager.Add<transform::RemovePhonies>();
|
||||
manager.Add<transform::VectorizeScalarMatrixConstructors>();
|
||||
manager.Add<transform::VectorizeMatrixConversions>();
|
||||
manager.Add<transform::ForLoopToLoop>(); // Must come after
|
||||
manager.Add<transform::WhileToLoop>(); // ZeroInitWorkgroupMemory
|
||||
manager.Add<transform::CanonicalizeEntryPointIO>();
|
||||
|
||||
Reference in New Issue
Block a user