dan sinclair | 99ad0e8 | 2020-07-25 14:39:23 +0000 | [diff] [blame] | 1 | // Copyright 2020 The Tint Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "gtest/gtest.h" |
| 16 | #include "src/ast/call_expression.h" |
| 17 | #include "src/ast/identifier_expression.h" |
| 18 | #include "src/ast/module.h" |
| 19 | #include "src/ast/type/f32_type.h" |
| 20 | #include "src/ast/type/vector_type.h" |
dan sinclair | 99ad0e8 | 2020-07-25 14:39:23 +0000 | [diff] [blame] | 21 | #include "src/type_determiner.h" |
| 22 | #include "src/writer/msl/generator_impl.h" |
dan sinclair | 196e097 | 2020-11-13 18:13:24 +0000 | [diff] [blame] | 23 | #include "src/writer/msl/test_helper.h" |
dan sinclair | 99ad0e8 | 2020-07-25 14:39:23 +0000 | [diff] [blame] | 24 | |
| 25 | namespace tint { |
| 26 | namespace writer { |
| 27 | namespace msl { |
| 28 | namespace { |
| 29 | |
dan sinclair | 196e097 | 2020-11-13 18:13:24 +0000 | [diff] [blame] | 30 | using MslGeneratorImplTest = TestHelper; |
dan sinclair | 99ad0e8 | 2020-07-25 14:39:23 +0000 | [diff] [blame] | 31 | |
| 32 | struct IntrinsicData { |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 33 | ast::Intrinsic intrinsic; |
dan sinclair | 99ad0e8 | 2020-07-25 14:39:23 +0000 | [diff] [blame] | 34 | const char* msl_name; |
| 35 | }; |
| 36 | inline std::ostream& operator<<(std::ostream& out, IntrinsicData data) { |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 37 | out << data.msl_name; |
dan sinclair | 99ad0e8 | 2020-07-25 14:39:23 +0000 | [diff] [blame] | 38 | return out; |
| 39 | } |
dan sinclair | 196e097 | 2020-11-13 18:13:24 +0000 | [diff] [blame] | 40 | using MslIntrinsicTest = TestParamHelper<IntrinsicData>; |
dan sinclair | 99ad0e8 | 2020-07-25 14:39:23 +0000 | [diff] [blame] | 41 | TEST_P(MslIntrinsicTest, Emit) { |
| 42 | auto param = GetParam(); |
dan sinclair | 196e097 | 2020-11-13 18:13:24 +0000 | [diff] [blame] | 43 | EXPECT_EQ(gen.generate_intrinsic_name(param.intrinsic), param.msl_name); |
dan sinclair | 99ad0e8 | 2020-07-25 14:39:23 +0000 | [diff] [blame] | 44 | } |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 45 | INSTANTIATE_TEST_SUITE_P( |
| 46 | MslGeneratorImplTest, |
| 47 | MslIntrinsicTest, |
| 48 | testing::Values(IntrinsicData{ast::Intrinsic::kAny, "any"}, |
| 49 | IntrinsicData{ast::Intrinsic::kAll, "all"}, |
dan sinclair | 94b7c66 | 2020-10-05 19:46:16 +0000 | [diff] [blame] | 50 | IntrinsicData{ast::Intrinsic::kCountOneBits, "popcount"}, |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 51 | IntrinsicData{ast::Intrinsic::kDot, "dot"}, |
| 52 | IntrinsicData{ast::Intrinsic::kDpdx, "dfdx"}, |
| 53 | IntrinsicData{ast::Intrinsic::kDpdxCoarse, "dfdx"}, |
| 54 | IntrinsicData{ast::Intrinsic::kDpdxFine, "dfdx"}, |
| 55 | IntrinsicData{ast::Intrinsic::kDpdy, "dfdy"}, |
| 56 | IntrinsicData{ast::Intrinsic::kDpdyCoarse, "dfdy"}, |
| 57 | IntrinsicData{ast::Intrinsic::kDpdyFine, "dfdy"}, |
| 58 | IntrinsicData{ast::Intrinsic::kFwidth, "fwidth"}, |
| 59 | IntrinsicData{ast::Intrinsic::kFwidthCoarse, "fwidth"}, |
| 60 | IntrinsicData{ast::Intrinsic::kFwidthFine, "fwidth"}, |
| 61 | IntrinsicData{ast::Intrinsic::kIsFinite, "isfinite"}, |
| 62 | IntrinsicData{ast::Intrinsic::kIsInf, "isinf"}, |
| 63 | IntrinsicData{ast::Intrinsic::kIsNan, "isnan"}, |
| 64 | IntrinsicData{ast::Intrinsic::kIsNormal, "isnormal"}, |
dan sinclair | 94b7c66 | 2020-10-05 19:46:16 +0000 | [diff] [blame] | 65 | IntrinsicData{ast::Intrinsic::kReverseBits, "reverse_bits"}, |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 66 | IntrinsicData{ast::Intrinsic::kSelect, "select"})); |
dan sinclair | 99ad0e8 | 2020-07-25 14:39:23 +0000 | [diff] [blame] | 67 | |
| 68 | TEST_F(MslGeneratorImplTest, DISABLED_Intrinsic_OuterProduct) { |
dan sinclair | b583993 | 2020-12-16 21:38:40 +0000 | [diff] [blame] | 69 | auto* a = Var("a", ast::StorageClass::kNone, ty.vec2<f32>()); |
| 70 | auto* b = Var("b", ast::StorageClass::kNone, ty.vec3<f32>()); |
dan sinclair | 99ad0e8 | 2020-07-25 14:39:23 +0000 | [diff] [blame] | 71 | |
dan sinclair | f74b90b | 2021-01-11 16:24:32 +0000 | [diff] [blame^] | 72 | auto* call = Call("outerProduct", "a", "b"); |
Ben Clayton | b053acf | 2020-11-16 16:31:07 +0000 | [diff] [blame] | 73 | td.RegisterVariableForTesting(a); |
| 74 | td.RegisterVariableForTesting(b); |
dan sinclair | 99ad0e8 | 2020-07-25 14:39:23 +0000 | [diff] [blame] | 75 | |
dan sinclair | 5e5e36e | 2020-12-16 14:41:00 +0000 | [diff] [blame] | 76 | mod->AddGlobalVariable(a); |
| 77 | mod->AddGlobalVariable(b); |
dan sinclair | 99ad0e8 | 2020-07-25 14:39:23 +0000 | [diff] [blame] | 78 | |
| 79 | ASSERT_TRUE(td.Determine()) << td.error(); |
dan sinclair | b583993 | 2020-12-16 21:38:40 +0000 | [diff] [blame] | 80 | ASSERT_TRUE(td.DetermineResultType(call)) << td.error(); |
dan sinclair | 99ad0e8 | 2020-07-25 14:39:23 +0000 | [diff] [blame] | 81 | |
dan sinclair | 196e097 | 2020-11-13 18:13:24 +0000 | [diff] [blame] | 82 | gen.increment_indent(); |
dan sinclair | b583993 | 2020-12-16 21:38:40 +0000 | [diff] [blame] | 83 | ASSERT_TRUE(gen.EmitExpression(call)) << gen.error(); |
dan sinclair | f74b90b | 2021-01-11 16:24:32 +0000 | [diff] [blame^] | 84 | EXPECT_EQ( |
| 85 | gen.result(), |
| 86 | " float3x2(test_a * test_b[0], test_a * test_b[1], test_a * test_b[2])"); |
dan sinclair | 99ad0e8 | 2020-07-25 14:39:23 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | TEST_F(MslGeneratorImplTest, Intrinsic_Bad_Name) { |
dan sinclair | 196e097 | 2020-11-13 18:13:24 +0000 | [diff] [blame] | 90 | EXPECT_EQ(gen.generate_intrinsic_name(ast::Intrinsic::kNone), ""); |
dan sinclair | 99ad0e8 | 2020-07-25 14:39:23 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | TEST_F(MslGeneratorImplTest, Intrinsic_Call) { |
dan sinclair | b583993 | 2020-12-16 21:38:40 +0000 | [diff] [blame] | 94 | auto* call = Call("dot", "param1", "param2"); |
dan sinclair | ff267ca | 2020-10-14 18:26:31 +0000 | [diff] [blame] | 95 | |
dan sinclair | b583993 | 2020-12-16 21:38:40 +0000 | [diff] [blame] | 96 | auto* v1 = Var("param1", ast::StorageClass::kFunction, ty.vec2<f32>()); |
| 97 | auto* v2 = Var("param2", ast::StorageClass::kFunction, ty.vec2<f32>()); |
dan sinclair | 99ad0e8 | 2020-07-25 14:39:23 +0000 | [diff] [blame] | 98 | |
dan sinclair | b583993 | 2020-12-16 21:38:40 +0000 | [diff] [blame] | 99 | td.RegisterVariableForTesting(v1); |
| 100 | td.RegisterVariableForTesting(v2); |
dan sinclair | 99ad0e8 | 2020-07-25 14:39:23 +0000 | [diff] [blame] | 101 | |
dan sinclair | b583993 | 2020-12-16 21:38:40 +0000 | [diff] [blame] | 102 | ASSERT_TRUE(td.DetermineResultType(call)) << td.error(); |
dan sinclair | b4fee2f | 2020-09-22 19:42:13 +0000 | [diff] [blame] | 103 | |
dan sinclair | 196e097 | 2020-11-13 18:13:24 +0000 | [diff] [blame] | 104 | gen.increment_indent(); |
dan sinclair | b583993 | 2020-12-16 21:38:40 +0000 | [diff] [blame] | 105 | ASSERT_TRUE(gen.EmitExpression(call)) << gen.error(); |
dan sinclair | f74b90b | 2021-01-11 16:24:32 +0000 | [diff] [blame^] | 106 | EXPECT_EQ(gen.result(), " dot(test_param1, test_param2)"); |
dan sinclair | 99ad0e8 | 2020-07-25 14:39:23 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | } // namespace |
| 110 | } // namespace msl |
| 111 | } // namespace writer |
| 112 | } // namespace tint |