David Neto | 85e13bc | 2023-02-03 22:00:07 +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 "gmock/gmock.h" |
| 16 | #include "src/tint/reader/spirv/parser_impl_test_helper.h" |
| 17 | #include "src/tint/reader/spirv/spirv_tools_helpers_test.h" |
| 18 | |
| 19 | namespace tint::reader::spirv { |
| 20 | namespace { |
| 21 | |
| 22 | using ::testing::Eq; |
| 23 | using ::testing::HasSubstr; |
| 24 | |
| 25 | std::string Preamble() { |
| 26 | return R"( |
| 27 | OpCapability Shader |
| 28 | OpCapability Sampled1D |
| 29 | OpCapability Image1D |
| 30 | OpCapability StorageImageExtendedFormats |
| 31 | OpCapability ImageQuery |
| 32 | OpMemoryModel Logical Simple |
| 33 | )"; |
| 34 | } |
| 35 | |
| 36 | std::string FragMain() { |
| 37 | return R"( |
| 38 | OpEntryPoint Fragment %main "main" ; assume no IO |
| 39 | OpExecutionMode %main OriginUpperLeft |
| 40 | )"; |
| 41 | } |
| 42 | |
| 43 | std::string MainBody() { |
| 44 | return R"( |
| 45 | %main = OpFunction %void None %voidfn |
| 46 | %main_entry = OpLabel |
| 47 | OpReturn |
| 48 | OpFunctionEnd |
| 49 | )"; |
| 50 | } |
| 51 | |
| 52 | std::string CommonTypes() { |
| 53 | return R"( |
| 54 | %void = OpTypeVoid |
| 55 | %voidfn = OpTypeFunction %void |
| 56 | |
| 57 | %bool = OpTypeBool |
| 58 | %float = OpTypeFloat 32 |
| 59 | %uint = OpTypeInt 32 0 |
| 60 | %int = OpTypeInt 32 1 |
| 61 | |
| 62 | %v2int = OpTypeVector %int 2 |
| 63 | %v3int = OpTypeVector %int 3 |
| 64 | %v4int = OpTypeVector %int 4 |
| 65 | %v2uint = OpTypeVector %uint 2 |
| 66 | %v3uint = OpTypeVector %uint 3 |
| 67 | %v4uint = OpTypeVector %uint 4 |
| 68 | %v2float = OpTypeVector %float 2 |
| 69 | %v3float = OpTypeVector %float 3 |
| 70 | %v4float = OpTypeVector %float 4 |
| 71 | |
| 72 | %true = OpConstantTrue %bool |
| 73 | %false = OpConstantFalse %bool |
| 74 | |
| 75 | %int_1 = OpConstant %int 1 |
| 76 | %int_m5 = OpConstant %int -5 |
| 77 | %int_min = OpConstant %int 0x80000000 |
| 78 | %int_max = OpConstant %int 0x7fffffff |
| 79 | %uint_0 = OpConstant %uint 0 |
| 80 | %uint_max = OpConstant %uint 0xffffffff |
| 81 | |
| 82 | %float_minus_5 = OpConstant %float -5 |
| 83 | %float_half = OpConstant %float 0.5 |
| 84 | %float_ten = OpConstant %float 10 |
| 85 | )"; |
| 86 | } |
| 87 | |
| 88 | struct ConstantCase { |
| 89 | std::string spirv_type; |
| 90 | std::string spirv_value; |
| 91 | std::string wgsl_value; |
| 92 | }; |
| 93 | inline std::ostream& operator<<(std::ostream& out, const ConstantCase& c) { |
| 94 | out << "ConstantCase(" << c.spirv_type << ", " << c.spirv_value << ", " << c.wgsl_value << ")"; |
| 95 | return out; |
| 96 | } |
| 97 | |
| 98 | using SpvParserConstantTest = SpvParserTestBase<::testing::TestWithParam<ConstantCase>>; |
| 99 | |
| 100 | TEST_P(SpvParserConstantTest, ReturnValue) { |
| 101 | const auto spirv_type = GetParam().spirv_type; |
| 102 | const auto spirv_value = GetParam().spirv_value; |
| 103 | const auto wgsl_value = GetParam().wgsl_value; |
| 104 | const auto assembly = Preamble() + FragMain() + CommonTypes() + R"( |
| 105 | %fty = OpTypeFunction )" + |
| 106 | spirv_type + R"( |
| 107 | |
| 108 | %200 = OpFunction )" + |
| 109 | spirv_type + R"( None %fty |
| 110 | %fentry = OpLabel |
| 111 | OpReturnValue )" + spirv_value + |
| 112 | R"( |
| 113 | OpFunctionEnd |
| 114 | )" + MainBody(); |
| 115 | auto p = parser(test::Assemble(assembly)); |
| 116 | ASSERT_TRUE(p->Parse()); |
| 117 | auto fe = p->function_emitter(200); |
| 118 | EXPECT_TRUE(fe.EmitBody()) << p->error(); |
| 119 | EXPECT_TRUE(p->error().empty()); |
| 120 | const auto got = test::ToString(p->program(), fe.ast_body()); |
| 121 | const auto expect = std::string("return ") + wgsl_value + std::string(";\n"); |
| 122 | |
| 123 | EXPECT_EQ(got, expect); |
| 124 | } |
| 125 | |
| 126 | INSTANTIATE_TEST_SUITE_P(Scalars, |
| 127 | SpvParserConstantTest, |
| 128 | ::testing::ValuesIn(std::vector<ConstantCase>{ |
| 129 | {"%bool", "%true", "true"}, |
| 130 | {"%bool", "%false", "false"}, |
| 131 | {"%int", "%int_1", "1i"}, |
| 132 | {"%int", "%int_m5", "-5i"}, |
| 133 | {"%int", "%int_min", "i32(-2147483648)"}, |
| 134 | {"%int", "%int_max", "2147483647i"}, |
| 135 | {"%uint", "%uint_0", "0u"}, |
| 136 | {"%uint", "%uint_max", "4294967295u"}, |
| 137 | {"%float", "%float_minus_5", "-5.0f"}, |
| 138 | {"%float", "%float_half", "0.5f"}, |
| 139 | {"%float", "%float_ten", "10.0f"}})); |
| 140 | |
| 141 | } // namespace |
| 142 | } // namespace tint::reader::spirv |