1fd4e5da5Sopenharmony_ci// Copyright (c) 2015-2016 The Khronos Group Inc. 2fd4e5da5Sopenharmony_ci// 3fd4e5da5Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License"); 4fd4e5da5Sopenharmony_ci// you may not use this file except in compliance with the License. 5fd4e5da5Sopenharmony_ci// You may obtain a copy of the License at 6fd4e5da5Sopenharmony_ci// 7fd4e5da5Sopenharmony_ci// http://www.apache.org/licenses/LICENSE-2.0 8fd4e5da5Sopenharmony_ci// 9fd4e5da5Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software 10fd4e5da5Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS, 11fd4e5da5Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fd4e5da5Sopenharmony_ci// See the License for the specific language governing permissions and 13fd4e5da5Sopenharmony_ci// limitations under the License. 14fd4e5da5Sopenharmony_ci 15fd4e5da5Sopenharmony_ci#include <algorithm> 16fd4e5da5Sopenharmony_ci#include <cstring> 17fd4e5da5Sopenharmony_ci#include <string> 18fd4e5da5Sopenharmony_ci#include <utility> 19fd4e5da5Sopenharmony_ci#include <vector> 20fd4e5da5Sopenharmony_ci 21fd4e5da5Sopenharmony_ci#include "gmock/gmock.h" 22fd4e5da5Sopenharmony_ci#include "source/spirv_constant.h" 23fd4e5da5Sopenharmony_ci#include "source/util/bitutils.h" 24fd4e5da5Sopenharmony_ci#include "source/util/hex_float.h" 25fd4e5da5Sopenharmony_ci#include "test/test_fixture.h" 26fd4e5da5Sopenharmony_ci#include "test/unit_spirv.h" 27fd4e5da5Sopenharmony_ci 28fd4e5da5Sopenharmony_cinamespace spvtools { 29fd4e5da5Sopenharmony_cinamespace { 30fd4e5da5Sopenharmony_ci 31fd4e5da5Sopenharmony_ciusing spvtest::AutoText; 32fd4e5da5Sopenharmony_ciusing spvtest::Concatenate; 33fd4e5da5Sopenharmony_ciusing spvtest::MakeInstruction; 34fd4e5da5Sopenharmony_ciusing spvtest::ScopedContext; 35fd4e5da5Sopenharmony_ciusing spvtest::TextToBinaryTest; 36fd4e5da5Sopenharmony_ciusing testing::Eq; 37fd4e5da5Sopenharmony_ciusing testing::IsNull; 38fd4e5da5Sopenharmony_ciusing testing::NotNull; 39fd4e5da5Sopenharmony_ci 40fd4e5da5Sopenharmony_ci// An mask parsing test case. 41fd4e5da5Sopenharmony_cistruct MaskCase { 42fd4e5da5Sopenharmony_ci spv_operand_type_t which_enum; 43fd4e5da5Sopenharmony_ci uint32_t expected_value; 44fd4e5da5Sopenharmony_ci const char* expression; 45fd4e5da5Sopenharmony_ci}; 46fd4e5da5Sopenharmony_ci 47fd4e5da5Sopenharmony_ciusing GoodMaskParseTest = ::testing::TestWithParam<MaskCase>; 48fd4e5da5Sopenharmony_ci 49fd4e5da5Sopenharmony_ciTEST_P(GoodMaskParseTest, GoodMaskExpressions) { 50fd4e5da5Sopenharmony_ci spv_context context = spvContextCreate(SPV_ENV_UNIVERSAL_1_0); 51fd4e5da5Sopenharmony_ci 52fd4e5da5Sopenharmony_ci uint32_t value; 53fd4e5da5Sopenharmony_ci EXPECT_EQ(SPV_SUCCESS, 54fd4e5da5Sopenharmony_ci AssemblyGrammar(context).parseMaskOperand( 55fd4e5da5Sopenharmony_ci GetParam().which_enum, GetParam().expression, &value)); 56fd4e5da5Sopenharmony_ci EXPECT_EQ(GetParam().expected_value, value); 57fd4e5da5Sopenharmony_ci 58fd4e5da5Sopenharmony_ci spvContextDestroy(context); 59fd4e5da5Sopenharmony_ci} 60fd4e5da5Sopenharmony_ci 61fd4e5da5Sopenharmony_ciINSTANTIATE_TEST_SUITE_P( 62fd4e5da5Sopenharmony_ci ParseMask, GoodMaskParseTest, 63fd4e5da5Sopenharmony_ci ::testing::ValuesIn(std::vector<MaskCase>{ 64fd4e5da5Sopenharmony_ci {SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, 0, "None"}, 65fd4e5da5Sopenharmony_ci {SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, 1, "NotNaN"}, 66fd4e5da5Sopenharmony_ci {SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, 2, "NotInf"}, 67fd4e5da5Sopenharmony_ci {SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, 3, "NotNaN|NotInf"}, 68fd4e5da5Sopenharmony_ci // Mask expressions are symmetric. 69fd4e5da5Sopenharmony_ci {SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, 3, "NotInf|NotNaN"}, 70fd4e5da5Sopenharmony_ci // Repeating a value has no effect. 71fd4e5da5Sopenharmony_ci {SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, 3, "NotInf|NotNaN|NotInf"}, 72fd4e5da5Sopenharmony_ci // Using 3 operands still works. 73fd4e5da5Sopenharmony_ci {SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, 0x13, "NotInf|NotNaN|Fast"}, 74fd4e5da5Sopenharmony_ci {SPV_OPERAND_TYPE_SELECTION_CONTROL, 0, "None"}, 75fd4e5da5Sopenharmony_ci {SPV_OPERAND_TYPE_SELECTION_CONTROL, 1, "Flatten"}, 76fd4e5da5Sopenharmony_ci {SPV_OPERAND_TYPE_SELECTION_CONTROL, 2, "DontFlatten"}, 77fd4e5da5Sopenharmony_ci // Weirdly, you can specify to flatten and don't flatten a selection. 78fd4e5da5Sopenharmony_ci {SPV_OPERAND_TYPE_SELECTION_CONTROL, 3, "Flatten|DontFlatten"}, 79fd4e5da5Sopenharmony_ci {SPV_OPERAND_TYPE_LOOP_CONTROL, 0, "None"}, 80fd4e5da5Sopenharmony_ci {SPV_OPERAND_TYPE_LOOP_CONTROL, 1, "Unroll"}, 81fd4e5da5Sopenharmony_ci {SPV_OPERAND_TYPE_LOOP_CONTROL, 2, "DontUnroll"}, 82fd4e5da5Sopenharmony_ci // Weirdly, you can specify to unroll and don't unroll a loop. 83fd4e5da5Sopenharmony_ci {SPV_OPERAND_TYPE_LOOP_CONTROL, 3, "Unroll|DontUnroll"}, 84fd4e5da5Sopenharmony_ci {SPV_OPERAND_TYPE_FUNCTION_CONTROL, 0, "None"}, 85fd4e5da5Sopenharmony_ci {SPV_OPERAND_TYPE_FUNCTION_CONTROL, 1, "Inline"}, 86fd4e5da5Sopenharmony_ci {SPV_OPERAND_TYPE_FUNCTION_CONTROL, 2, "DontInline"}, 87fd4e5da5Sopenharmony_ci {SPV_OPERAND_TYPE_FUNCTION_CONTROL, 4, "Pure"}, 88fd4e5da5Sopenharmony_ci {SPV_OPERAND_TYPE_FUNCTION_CONTROL, 8, "Const"}, 89fd4e5da5Sopenharmony_ci {SPV_OPERAND_TYPE_FUNCTION_CONTROL, 0xd, "Inline|Const|Pure"}, 90fd4e5da5Sopenharmony_ci })); 91fd4e5da5Sopenharmony_ci 92fd4e5da5Sopenharmony_ciusing BadFPFastMathMaskParseTest = ::testing::TestWithParam<const char*>; 93fd4e5da5Sopenharmony_ci 94fd4e5da5Sopenharmony_ciTEST_P(BadFPFastMathMaskParseTest, BadMaskExpressions) { 95fd4e5da5Sopenharmony_ci spv_context context = spvContextCreate(SPV_ENV_UNIVERSAL_1_0); 96fd4e5da5Sopenharmony_ci 97fd4e5da5Sopenharmony_ci uint32_t value; 98fd4e5da5Sopenharmony_ci EXPECT_NE(SPV_SUCCESS, 99fd4e5da5Sopenharmony_ci AssemblyGrammar(context).parseMaskOperand( 100fd4e5da5Sopenharmony_ci SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, GetParam(), &value)); 101fd4e5da5Sopenharmony_ci 102fd4e5da5Sopenharmony_ci spvContextDestroy(context); 103fd4e5da5Sopenharmony_ci} 104fd4e5da5Sopenharmony_ci 105fd4e5da5Sopenharmony_ciINSTANTIATE_TEST_SUITE_P(ParseMask, BadFPFastMathMaskParseTest, 106fd4e5da5Sopenharmony_ci ::testing::ValuesIn(std::vector<const char*>{ 107fd4e5da5Sopenharmony_ci nullptr, "", "NotValidEnum", "|", "NotInf|", 108fd4e5da5Sopenharmony_ci "|NotInf", "NotInf||NotNaN", 109fd4e5da5Sopenharmony_ci "Unroll" // A good word, but for the wrong enum 110fd4e5da5Sopenharmony_ci })); 111fd4e5da5Sopenharmony_ci 112fd4e5da5Sopenharmony_ciTEST_F(TextToBinaryTest, InvalidText) { 113fd4e5da5Sopenharmony_ci ASSERT_EQ(SPV_ERROR_INVALID_TEXT, 114fd4e5da5Sopenharmony_ci spvTextToBinary(ScopedContext().context, nullptr, 0, &binary, 115fd4e5da5Sopenharmony_ci &diagnostic)); 116fd4e5da5Sopenharmony_ci EXPECT_NE(nullptr, diagnostic); 117fd4e5da5Sopenharmony_ci EXPECT_THAT(diagnostic->error, Eq(std::string("Missing assembly text."))); 118fd4e5da5Sopenharmony_ci} 119fd4e5da5Sopenharmony_ci 120fd4e5da5Sopenharmony_ciTEST_F(TextToBinaryTest, InvalidPointer) { 121fd4e5da5Sopenharmony_ci SetText( 122fd4e5da5Sopenharmony_ci "OpEntryPoint Kernel 0 \"\"\nOpExecutionMode 0 LocalSizeHint 1 1 1\n"); 123fd4e5da5Sopenharmony_ci ASSERT_EQ(SPV_ERROR_INVALID_POINTER, 124fd4e5da5Sopenharmony_ci spvTextToBinary(ScopedContext().context, text.str, text.length, 125fd4e5da5Sopenharmony_ci nullptr, &diagnostic)); 126fd4e5da5Sopenharmony_ci} 127fd4e5da5Sopenharmony_ci 128fd4e5da5Sopenharmony_ciTEST_F(TextToBinaryTest, InvalidPrefix) { 129fd4e5da5Sopenharmony_ci EXPECT_EQ( 130fd4e5da5Sopenharmony_ci "Expected <opcode> or <result-id> at the beginning of an instruction, " 131fd4e5da5Sopenharmony_ci "found 'Invalid'.", 132fd4e5da5Sopenharmony_ci CompileFailure("Invalid")); 133fd4e5da5Sopenharmony_ci} 134fd4e5da5Sopenharmony_ci 135fd4e5da5Sopenharmony_ciTEST_F(TextToBinaryTest, EmptyAssemblyString) { 136fd4e5da5Sopenharmony_ci // An empty assembly module is valid! 137fd4e5da5Sopenharmony_ci // It should produce a valid module with zero instructions. 138fd4e5da5Sopenharmony_ci EXPECT_THAT(CompiledInstructions(""), Eq(std::vector<uint32_t>{})); 139fd4e5da5Sopenharmony_ci} 140fd4e5da5Sopenharmony_ci 141fd4e5da5Sopenharmony_ciTEST_F(TextToBinaryTest, StringSpace) { 142fd4e5da5Sopenharmony_ci const std::string code = ("OpSourceExtension \"string with spaces\"\n"); 143fd4e5da5Sopenharmony_ci EXPECT_EQ(code, EncodeAndDecodeSuccessfully(code)); 144fd4e5da5Sopenharmony_ci} 145fd4e5da5Sopenharmony_ci 146fd4e5da5Sopenharmony_ciTEST_F(TextToBinaryTest, UnknownBeginningOfInstruction) { 147fd4e5da5Sopenharmony_ci EXPECT_EQ( 148fd4e5da5Sopenharmony_ci "Expected <opcode> or <result-id> at the beginning of an instruction, " 149fd4e5da5Sopenharmony_ci "found 'Google'.", 150fd4e5da5Sopenharmony_ci CompileFailure( 151fd4e5da5Sopenharmony_ci "\nOpSource OpenCL_C 12\nOpMemoryModel Physical64 OpenCL\nGoogle\n")); 152fd4e5da5Sopenharmony_ci EXPECT_EQ(4u, diagnostic->position.line + 1); 153fd4e5da5Sopenharmony_ci EXPECT_EQ(1u, diagnostic->position.column + 1); 154fd4e5da5Sopenharmony_ci} 155fd4e5da5Sopenharmony_ci 156fd4e5da5Sopenharmony_ciTEST_F(TextToBinaryTest, NoEqualSign) { 157fd4e5da5Sopenharmony_ci EXPECT_EQ("Expected '=', found end of stream.", 158fd4e5da5Sopenharmony_ci CompileFailure("\nOpSource OpenCL_C 12\n" 159fd4e5da5Sopenharmony_ci "OpMemoryModel Physical64 OpenCL\n%2\n")); 160fd4e5da5Sopenharmony_ci EXPECT_EQ(5u, diagnostic->position.line + 1); 161fd4e5da5Sopenharmony_ci EXPECT_EQ(1u, diagnostic->position.column + 1); 162fd4e5da5Sopenharmony_ci} 163fd4e5da5Sopenharmony_ci 164fd4e5da5Sopenharmony_ciTEST_F(TextToBinaryTest, NoOpCode) { 165fd4e5da5Sopenharmony_ci EXPECT_EQ("Expected opcode, found end of stream.", 166fd4e5da5Sopenharmony_ci CompileFailure("\nOpSource OpenCL_C 12\n" 167fd4e5da5Sopenharmony_ci "OpMemoryModel Physical64 OpenCL\n%2 =\n")); 168fd4e5da5Sopenharmony_ci EXPECT_EQ(5u, diagnostic->position.line + 1); 169fd4e5da5Sopenharmony_ci EXPECT_EQ(1u, diagnostic->position.column + 1); 170fd4e5da5Sopenharmony_ci} 171fd4e5da5Sopenharmony_ci 172fd4e5da5Sopenharmony_ciTEST_F(TextToBinaryTest, WrongOpCode) { 173fd4e5da5Sopenharmony_ci EXPECT_EQ("Invalid Opcode prefix 'Wahahaha'.", 174fd4e5da5Sopenharmony_ci CompileFailure("\nOpSource OpenCL_C 12\n" 175fd4e5da5Sopenharmony_ci "OpMemoryModel Physical64 OpenCL\n%2 = Wahahaha\n")); 176fd4e5da5Sopenharmony_ci EXPECT_EQ(4u, diagnostic->position.line + 1); 177fd4e5da5Sopenharmony_ci EXPECT_EQ(6u, diagnostic->position.column + 1); 178fd4e5da5Sopenharmony_ci} 179fd4e5da5Sopenharmony_ci 180fd4e5da5Sopenharmony_ciTEST_F(TextToBinaryTest, CRLF) { 181fd4e5da5Sopenharmony_ci const std::string input = 182fd4e5da5Sopenharmony_ci "%i32 = OpTypeInt 32 1\r\n%c = OpConstant %i32 123\r\n"; 183fd4e5da5Sopenharmony_ci EXPECT_THAT( 184fd4e5da5Sopenharmony_ci CompiledInstructions(input), 185fd4e5da5Sopenharmony_ci Eq(Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 32, 1}), 186fd4e5da5Sopenharmony_ci MakeInstruction(spv::Op::OpConstant, {1, 2, 123})}))); 187fd4e5da5Sopenharmony_ci} 188fd4e5da5Sopenharmony_ci 189fd4e5da5Sopenharmony_ciusing TextToBinaryFloatValueTest = spvtest::TextToBinaryTestBase< 190fd4e5da5Sopenharmony_ci ::testing::TestWithParam<std::pair<std::string, uint32_t>>>; 191fd4e5da5Sopenharmony_ci 192fd4e5da5Sopenharmony_ciTEST_P(TextToBinaryFloatValueTest, Samples) { 193fd4e5da5Sopenharmony_ci const std::string input = 194fd4e5da5Sopenharmony_ci "%1 = OpTypeFloat 32\n%2 = OpConstant %1 " + GetParam().first; 195fd4e5da5Sopenharmony_ci EXPECT_THAT(CompiledInstructions(input), 196fd4e5da5Sopenharmony_ci Eq(Concatenate({MakeInstruction(spv::Op::OpTypeFloat, {1, 32}), 197fd4e5da5Sopenharmony_ci MakeInstruction(spv::Op::OpConstant, 198fd4e5da5Sopenharmony_ci {1, 2, GetParam().second})}))); 199fd4e5da5Sopenharmony_ci} 200fd4e5da5Sopenharmony_ci 201fd4e5da5Sopenharmony_ciINSTANTIATE_TEST_SUITE_P( 202fd4e5da5Sopenharmony_ci FloatValues, TextToBinaryFloatValueTest, 203fd4e5da5Sopenharmony_ci ::testing::ValuesIn(std::vector<std::pair<std::string, uint32_t>>{ 204fd4e5da5Sopenharmony_ci {"0.0", 0x00000000}, // +0 205fd4e5da5Sopenharmony_ci {"!0x00000001", 0x00000001}, // +denorm 206fd4e5da5Sopenharmony_ci {"!0x00800000", 0x00800000}, // +norm 207fd4e5da5Sopenharmony_ci {"1.5", 0x3fc00000}, 208fd4e5da5Sopenharmony_ci {"!0x7f800000", 0x7f800000}, // +inf 209fd4e5da5Sopenharmony_ci {"!0x7f800001", 0x7f800001}, // NaN 210fd4e5da5Sopenharmony_ci 211fd4e5da5Sopenharmony_ci {"-0.0", 0x80000000}, // -0 212fd4e5da5Sopenharmony_ci {"!0x80000001", 0x80000001}, // -denorm 213fd4e5da5Sopenharmony_ci {"!0x80800000", 0x80800000}, // -norm 214fd4e5da5Sopenharmony_ci {"-2.5", 0xc0200000}, 215fd4e5da5Sopenharmony_ci {"!0xff800000", 0xff800000}, // -inf 216fd4e5da5Sopenharmony_ci {"!0xff800001", 0xff800001}, // NaN 217fd4e5da5Sopenharmony_ci })); 218fd4e5da5Sopenharmony_ci 219fd4e5da5Sopenharmony_ciusing TextToBinaryHalfValueTest = spvtest::TextToBinaryTestBase< 220fd4e5da5Sopenharmony_ci ::testing::TestWithParam<std::pair<std::string, uint32_t>>>; 221fd4e5da5Sopenharmony_ci 222fd4e5da5Sopenharmony_ciTEST_P(TextToBinaryHalfValueTest, Samples) { 223fd4e5da5Sopenharmony_ci const std::string input = 224fd4e5da5Sopenharmony_ci "%1 = OpTypeFloat 16\n%2 = OpConstant %1 " + GetParam().first; 225fd4e5da5Sopenharmony_ci EXPECT_THAT(CompiledInstructions(input), 226fd4e5da5Sopenharmony_ci Eq(Concatenate({MakeInstruction(spv::Op::OpTypeFloat, {1, 16}), 227fd4e5da5Sopenharmony_ci MakeInstruction(spv::Op::OpConstant, 228fd4e5da5Sopenharmony_ci {1, 2, GetParam().second})}))); 229fd4e5da5Sopenharmony_ci} 230fd4e5da5Sopenharmony_ci 231fd4e5da5Sopenharmony_ciINSTANTIATE_TEST_SUITE_P( 232fd4e5da5Sopenharmony_ci HalfValues, TextToBinaryHalfValueTest, 233fd4e5da5Sopenharmony_ci ::testing::ValuesIn(std::vector<std::pair<std::string, uint32_t>>{ 234fd4e5da5Sopenharmony_ci {"0.0", 0x00000000}, 235fd4e5da5Sopenharmony_ci {"1.0", 0x00003c00}, 236fd4e5da5Sopenharmony_ci {"1.000844", 0x00003c00}, // Truncate to 1.0 237fd4e5da5Sopenharmony_ci {"1.000977", 0x00003c01}, // Don't have to truncate 238fd4e5da5Sopenharmony_ci {"1.001465", 0x00003c01}, // Truncate to 1.0000977 239fd4e5da5Sopenharmony_ci {"1.5", 0x00003e00}, 240fd4e5da5Sopenharmony_ci {"-1.0", 0x0000bc00}, 241fd4e5da5Sopenharmony_ci {"2.0", 0x00004000}, 242fd4e5da5Sopenharmony_ci {"-2.0", 0x0000c000}, 243fd4e5da5Sopenharmony_ci {"0x1p1", 0x00004000}, 244fd4e5da5Sopenharmony_ci {"-0x1p1", 0x0000c000}, 245fd4e5da5Sopenharmony_ci {"0x1.8p1", 0x00004200}, 246fd4e5da5Sopenharmony_ci {"0x1.8p4", 0x00004e00}, 247fd4e5da5Sopenharmony_ci {"0x1.801p4", 0x00004e00}, 248fd4e5da5Sopenharmony_ci {"0x1.804p4", 0x00004e01}, 249fd4e5da5Sopenharmony_ci })); 250fd4e5da5Sopenharmony_ci 251fd4e5da5Sopenharmony_ciTEST(CreateContext, UniversalEnvironment) { 252fd4e5da5Sopenharmony_ci auto c = spvContextCreate(SPV_ENV_UNIVERSAL_1_0); 253fd4e5da5Sopenharmony_ci EXPECT_THAT(c, NotNull()); 254fd4e5da5Sopenharmony_ci spvContextDestroy(c); 255fd4e5da5Sopenharmony_ci} 256fd4e5da5Sopenharmony_ci 257fd4e5da5Sopenharmony_ciTEST(CreateContext, VulkanEnvironment) { 258fd4e5da5Sopenharmony_ci auto c = spvContextCreate(SPV_ENV_VULKAN_1_0); 259fd4e5da5Sopenharmony_ci EXPECT_THAT(c, NotNull()); 260fd4e5da5Sopenharmony_ci spvContextDestroy(c); 261fd4e5da5Sopenharmony_ci} 262fd4e5da5Sopenharmony_ci 263fd4e5da5Sopenharmony_ci} // namespace 264fd4e5da5Sopenharmony_ci} // namespace spvtools 265