blob: 759fdab0ac5212ff51498403cde1b9b86bfaae34 [file] [log] [blame]
// Copyright 2023 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/ir/transform/handle_matrix_arithmetic.h"
#include <utility>
#include "src/tint/ir/transform/test_helper.h"
#include "src/tint/type/matrix.h"
namespace tint::ir::transform {
namespace {
using namespace tint::builtin::fluent_types; // NOLINT
using namespace tint::number_suffixes; // NOLINT
using IR_HandleMatrixArithmeticTest = TransformTest;
TEST_F(IR_HandleMatrixArithmeticTest, Add_Mat2x3f) {
auto* arg1 = b.FunctionParam("arg1", ty.mat2x3<f32>());
auto* arg2 = b.FunctionParam("arg2", ty.mat2x3<f32>());
auto* func = b.Function("foo", ty.mat2x3<f32>());
func->SetParams({arg1, arg2});
b.With(func->Block(), [&] {
auto* result = b.Add(ty.mat2x3<f32>(), arg1, arg2);
b.Return(func, result);
});
auto* src = R"(
%foo = func(%arg1:mat2x3<f32>, %arg2:mat2x3<f32>):mat2x3<f32> -> %b1 {
%b1 = block {
%4:mat2x3<f32> = add %arg1, %arg2
ret %4
}
}
)";
EXPECT_EQ(src, str());
auto* expect = R"(
%foo = func(%arg1:mat2x3<f32>, %arg2:mat2x3<f32>):mat2x3<f32> -> %b1 {
%b1 = block {
%4:vec3<f32> = access %arg1, 0u
%5:vec3<f32> = access %arg2, 0u
%6:vec3<f32> = add %4, %5
%7:vec3<f32> = access %arg1, 1u
%8:vec3<f32> = access %arg2, 1u
%9:vec3<f32> = add %7, %8
%10:mat2x3<f32> = construct %6, %9
ret %10
}
}
)";
Run<HandleMatrixArithmetic>();
EXPECT_EQ(expect, str());
}
TEST_F(IR_HandleMatrixArithmeticTest, Add_Mat4x2h) {
auto* arg1 = b.FunctionParam("arg1", ty.mat4x2<f16>());
auto* arg2 = b.FunctionParam("arg2", ty.mat4x2<f16>());
auto* func = b.Function("foo", ty.mat4x2<f16>());
func->SetParams({arg1, arg2});
b.With(func->Block(), [&] {
auto* result = b.Add(ty.mat4x2<f16>(), arg1, arg2);
b.Return(func, result);
});
auto* src = R"(
%foo = func(%arg1:mat4x2<f16>, %arg2:mat4x2<f16>):mat4x2<f16> -> %b1 {
%b1 = block {
%4:mat4x2<f16> = add %arg1, %arg2
ret %4
}
}
)";
EXPECT_EQ(src, str());
auto* expect = R"(
%foo = func(%arg1:mat4x2<f16>, %arg2:mat4x2<f16>):mat4x2<f16> -> %b1 {
%b1 = block {
%4:vec2<f16> = access %arg1, 0u
%5:vec2<f16> = access %arg2, 0u
%6:vec2<f16> = add %4, %5
%7:vec2<f16> = access %arg1, 1u
%8:vec2<f16> = access %arg2, 1u
%9:vec2<f16> = add %7, %8
%10:vec2<f16> = access %arg1, 2u
%11:vec2<f16> = access %arg2, 2u
%12:vec2<f16> = add %10, %11
%13:vec2<f16> = access %arg1, 3u
%14:vec2<f16> = access %arg2, 3u
%15:vec2<f16> = add %13, %14
%16:mat4x2<f16> = construct %6, %9, %12, %15
ret %16
}
}
)";
Run<HandleMatrixArithmetic>();
EXPECT_EQ(expect, str());
}
TEST_F(IR_HandleMatrixArithmeticTest, Subtract_Mat3x2f) {
auto* arg1 = b.FunctionParam("arg1", ty.mat3x2<f32>());
auto* arg2 = b.FunctionParam("arg2", ty.mat3x2<f32>());
auto* func = b.Function("foo", ty.mat3x2<f32>());
func->SetParams({arg1, arg2});
b.With(func->Block(), [&] {
auto* result = b.Subtract(ty.mat3x2<f32>(), arg1, arg2);
b.Return(func, result);
});
auto* src = R"(
%foo = func(%arg1:mat3x2<f32>, %arg2:mat3x2<f32>):mat3x2<f32> -> %b1 {
%b1 = block {
%4:mat3x2<f32> = sub %arg1, %arg2
ret %4
}
}
)";
EXPECT_EQ(src, str());
auto* expect = R"(
%foo = func(%arg1:mat3x2<f32>, %arg2:mat3x2<f32>):mat3x2<f32> -> %b1 {
%b1 = block {
%4:vec2<f32> = access %arg1, 0u
%5:vec2<f32> = access %arg2, 0u
%6:vec2<f32> = sub %4, %5
%7:vec2<f32> = access %arg1, 1u
%8:vec2<f32> = access %arg2, 1u
%9:vec2<f32> = sub %7, %8
%10:vec2<f32> = access %arg1, 2u
%11:vec2<f32> = access %arg2, 2u
%12:vec2<f32> = sub %10, %11
%13:mat3x2<f32> = construct %6, %9, %12
ret %13
}
}
)";
Run<HandleMatrixArithmetic>();
EXPECT_EQ(expect, str());
}
TEST_F(IR_HandleMatrixArithmeticTest, Subtract_Mat2x4h) {
auto* arg1 = b.FunctionParam("arg1", ty.mat2x4<f16>());
auto* arg2 = b.FunctionParam("arg2", ty.mat2x4<f16>());
auto* func = b.Function("foo", ty.mat2x4<f16>());
func->SetParams({arg1, arg2});
b.With(func->Block(), [&] {
auto* result = b.Subtract(ty.mat2x4<f16>(), arg1, arg2);
b.Return(func, result);
});
auto* src = R"(
%foo = func(%arg1:mat2x4<f16>, %arg2:mat2x4<f16>):mat2x4<f16> -> %b1 {
%b1 = block {
%4:mat2x4<f16> = sub %arg1, %arg2
ret %4
}
}
)";
EXPECT_EQ(src, str());
auto* expect = R"(
%foo = func(%arg1:mat2x4<f16>, %arg2:mat2x4<f16>):mat2x4<f16> -> %b1 {
%b1 = block {
%4:vec4<f16> = access %arg1, 0u
%5:vec4<f16> = access %arg2, 0u
%6:vec4<f16> = sub %4, %5
%7:vec4<f16> = access %arg1, 1u
%8:vec4<f16> = access %arg2, 1u
%9:vec4<f16> = sub %7, %8
%10:mat2x4<f16> = construct %6, %9
ret %10
}
}
)";
Run<HandleMatrixArithmetic>();
EXPECT_EQ(expect, str());
}
TEST_F(IR_HandleMatrixArithmeticTest, Mul_Mat2x3f_Scalar) {
auto* arg1 = b.FunctionParam("arg1", ty.mat2x3<f32>());
auto* arg2 = b.FunctionParam("arg2", ty.f32());
auto* func = b.Function("foo", ty.mat2x3<f32>());
func->SetParams({arg1, arg2});
b.With(func->Block(), [&] {
auto* result = b.Multiply(ty.mat2x3<f32>(), arg1, arg2);
b.Return(func, result);
});
auto* src = R"(
%foo = func(%arg1:mat2x3<f32>, %arg2:f32):mat2x3<f32> -> %b1 {
%b1 = block {
%4:mat2x3<f32> = mul %arg1, %arg2
ret %4
}
}
)";
EXPECT_EQ(src, str());
auto* expect = R"(
%foo = func(%arg1:mat2x3<f32>, %arg2:f32):mat2x3<f32> -> %b1 {
%b1 = block {
%4:mat2x3<f32> = spirv.matrix_times_scalar %arg1, %arg2
ret %4
}
}
)";
Run<HandleMatrixArithmetic>();
EXPECT_EQ(expect, str());
}
TEST_F(IR_HandleMatrixArithmeticTest, Mul_Mat3x4f_Vector) {
auto* arg1 = b.FunctionParam("arg1", ty.mat3x4<f32>());
auto* arg2 = b.FunctionParam("arg2", ty.vec3<f32>());
auto* func = b.Function("foo", ty.vec4<f32>());
func->SetParams({arg1, arg2});
b.With(func->Block(), [&] {
auto* result = b.Multiply(ty.vec4<f32>(), arg1, arg2);
b.Return(func, result);
});
auto* src = R"(
%foo = func(%arg1:mat3x4<f32>, %arg2:vec3<f32>):vec4<f32> -> %b1 {
%b1 = block {
%4:vec4<f32> = mul %arg1, %arg2
ret %4
}
}
)";
EXPECT_EQ(src, str());
auto* expect = R"(
%foo = func(%arg1:mat3x4<f32>, %arg2:vec3<f32>):vec4<f32> -> %b1 {
%b1 = block {
%4:vec4<f32> = spirv.matrix_times_vector %arg1, %arg2
ret %4
}
}
)";
Run<HandleMatrixArithmetic>();
EXPECT_EQ(expect, str());
}
TEST_F(IR_HandleMatrixArithmeticTest, Mul_Mat4x2f_Mat2x4) {
auto* arg1 = b.FunctionParam("arg1", ty.mat4x2<f32>());
auto* arg2 = b.FunctionParam("arg2", ty.mat2x4<f32>());
auto* func = b.Function("foo", ty.mat2x2<f32>());
func->SetParams({arg1, arg2});
b.With(func->Block(), [&] {
auto* result = b.Multiply(ty.mat2x2<f32>(), arg1, arg2);
b.Return(func, result);
});
auto* src = R"(
%foo = func(%arg1:mat4x2<f32>, %arg2:mat2x4<f32>):mat2x2<f32> -> %b1 {
%b1 = block {
%4:mat2x2<f32> = mul %arg1, %arg2
ret %4
}
}
)";
EXPECT_EQ(src, str());
auto* expect = R"(
%foo = func(%arg1:mat4x2<f32>, %arg2:mat2x4<f32>):mat2x2<f32> -> %b1 {
%b1 = block {
%4:mat2x2<f32> = spirv.matrix_times_matrix %arg1, %arg2
ret %4
}
}
)";
Run<HandleMatrixArithmetic>();
EXPECT_EQ(expect, str());
}
TEST_F(IR_HandleMatrixArithmeticTest, Mul_Scalar_Mat3x2h) {
auto* arg1 = b.FunctionParam("arg1", ty.f16());
auto* arg2 = b.FunctionParam("arg2", ty.mat3x2<f16>());
auto* func = b.Function("foo", ty.mat3x2<f16>());
func->SetParams({arg1, arg2});
b.With(func->Block(), [&] {
auto* result = b.Multiply(ty.mat3x2<f16>(), arg1, arg2);
b.Return(func, result);
});
auto* src = R"(
%foo = func(%arg1:f16, %arg2:mat3x2<f16>):mat3x2<f16> -> %b1 {
%b1 = block {
%4:mat3x2<f16> = mul %arg1, %arg2
ret %4
}
}
)";
EXPECT_EQ(src, str());
auto* expect = R"(
%foo = func(%arg1:f16, %arg2:mat3x2<f16>):mat3x2<f16> -> %b1 {
%b1 = block {
%4:mat3x2<f16> = spirv.matrix_times_scalar %arg2, %arg1
ret %4
}
}
)";
Run<HandleMatrixArithmetic>();
EXPECT_EQ(expect, str());
}
TEST_F(IR_HandleMatrixArithmeticTest, Mul_Vector_Mat3x4f) {
auto* arg1 = b.FunctionParam("arg1", ty.vec3<f16>());
auto* arg2 = b.FunctionParam("arg2", ty.mat4x3<f16>());
auto* func = b.Function("foo", ty.vec4<f16>());
func->SetParams({arg1, arg2});
b.With(func->Block(), [&] {
auto* result = b.Multiply(ty.vec4<f16>(), arg1, arg2);
b.Return(func, result);
});
auto* src = R"(
%foo = func(%arg1:vec3<f16>, %arg2:mat4x3<f16>):vec4<f16> -> %b1 {
%b1 = block {
%4:vec4<f16> = mul %arg1, %arg2
ret %4
}
}
)";
EXPECT_EQ(src, str());
auto* expect = R"(
%foo = func(%arg1:vec3<f16>, %arg2:mat4x3<f16>):vec4<f16> -> %b1 {
%b1 = block {
%4:vec4<f16> = spirv.vector_times_matrix %arg1, %arg2
ret %4
}
}
)";
Run<HandleMatrixArithmetic>();
EXPECT_EQ(expect, str());
}
TEST_F(IR_HandleMatrixArithmeticTest, Mul_Mat3x3f_Mat3x3) {
auto* arg1 = b.FunctionParam("arg1", ty.mat3x3<f16>());
auto* arg2 = b.FunctionParam("arg2", ty.mat3x3<f16>());
auto* func = b.Function("foo", ty.mat3x3<f16>());
func->SetParams({arg1, arg2});
b.With(func->Block(), [&] {
auto* result = b.Multiply(ty.mat3x3<f16>(), arg1, arg2);
b.Return(func, result);
});
auto* src = R"(
%foo = func(%arg1:mat3x3<f16>, %arg2:mat3x3<f16>):mat3x3<f16> -> %b1 {
%b1 = block {
%4:mat3x3<f16> = mul %arg1, %arg2
ret %4
}
}
)";
EXPECT_EQ(src, str());
auto* expect = R"(
%foo = func(%arg1:mat3x3<f16>, %arg2:mat3x3<f16>):mat3x3<f16> -> %b1 {
%b1 = block {
%4:mat3x3<f16> = spirv.matrix_times_matrix %arg1, %arg2
ret %4
}
}
)";
Run<HandleMatrixArithmetic>();
EXPECT_EQ(expect, str());
}
} // namespace
} // namespace tint::ir::transform