val_literals_test.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright (c) 2017 Google Inc.
  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. // Validation tests for illegal literals
  15. #include <string>
  16. #include <utility>
  17. #include "gmock/gmock.h"
  18. #include "test/val/val_fixtures.h"
  19. namespace spvtools {
  20. namespace val {
  21. namespace {
  22. using ::testing::HasSubstr;
  23. using ValidateLiterals = spvtest::ValidateBase<std::string>;
  24. using ValidateLiteralsShader = spvtest::ValidateBase<std::string>;
  25. using ValidateLiteralsKernel = spvtest::ValidateBase<std::string>;
  26. std::string GenerateShaderCode() {
  27. std::string str = R"(
  28. OpCapability Shader
  29. OpCapability Linkage
  30. OpCapability Int16
  31. OpCapability Int64
  32. OpCapability Float16
  33. OpCapability Float64
  34. OpMemoryModel Logical GLSL450
  35. %int16 = OpTypeInt 16 1
  36. %uint16 = OpTypeInt 16 0
  37. %int32 = OpTypeInt 32 1
  38. %uint32 = OpTypeInt 32 0
  39. %int64 = OpTypeInt 64 1
  40. %uint64 = OpTypeInt 64 0
  41. %half = OpTypeFloat 16
  42. %float = OpTypeFloat 32
  43. %double = OpTypeFloat 64
  44. %10 = OpTypeVoid
  45. )";
  46. return str;
  47. }
  48. std::string GenerateKernelCode() {
  49. std::string str = R"(
  50. OpCapability Kernel
  51. OpCapability Addresses
  52. OpCapability Linkage
  53. OpCapability Int8
  54. OpMemoryModel Physical64 OpenCL
  55. %uint8 = OpTypeInt 8 0
  56. )";
  57. return str;
  58. }
  59. TEST_F(ValidateLiterals, LiteralsShaderGood) {
  60. std::string str = GenerateShaderCode() + R"(
  61. %11 = OpConstant %int16 !0x00007FFF
  62. %12 = OpConstant %int16 !0xFFFF8000
  63. %13 = OpConstant %int16 !0xFFFFABCD
  64. %14 = OpConstant %uint16 !0x0000ABCD
  65. %15 = OpConstant %int16 -32768
  66. %16 = OpConstant %uint16 65535
  67. %17 = OpConstant %int32 -2147483648
  68. %18 = OpConstant %uint32 4294967295
  69. %19 = OpConstant %int64 -9223372036854775808
  70. %20 = OpConstant %uint64 18446744073709551615
  71. %21 = OpConstant %half !0x0000FFFF
  72. %22 = OpConstant %float !0xFFFFFFFF
  73. %23 = OpConstant %double !0xFFFFFFFF !0xFFFFFFFF
  74. )";
  75. CompileSuccessfully(str);
  76. ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
  77. }
  78. TEST_F(ValidateLiterals, InvalidInt) {
  79. std::string str = GenerateShaderCode() + R"(
  80. %11 = OpTypeInt 32 90
  81. )";
  82. CompileSuccessfully(str);
  83. EXPECT_EQ(SPV_ERROR_INVALID_VALUE, ValidateInstructions());
  84. EXPECT_THAT(getDiagnosticString(),
  85. HasSubstr("OpTypeInt has invalid signedness:"));
  86. }
  87. TEST_P(ValidateLiteralsShader, LiteralsShaderBad) {
  88. std::string str = GenerateShaderCode() + GetParam();
  89. std::string inst_id = "11";
  90. CompileSuccessfully(str);
  91. EXPECT_EQ(SPV_ERROR_INVALID_VALUE, ValidateInstructions());
  92. EXPECT_THAT(
  93. getDiagnosticString(),
  94. HasSubstr("The high-order bits of a literal number in instruction <id> " +
  95. inst_id +
  96. " must be 0 for a floating-point type, "
  97. "or 0 for an integer type with Signedness of 0, "
  98. "or sign extended when Signedness is 1"));
  99. }
  100. INSTANTIATE_TEST_SUITE_P(
  101. LiteralsShaderCases, ValidateLiteralsShader,
  102. ::testing::Values("%11 = OpConstant %int16 !0xFFFF0000", // Sign bit is 0
  103. "%11 = OpConstant %int16 !0x00008000", // Sign bit is 1
  104. "%11 = OpConstant %int16 !0xABCD8000", // Sign bit is 1
  105. "%11 = OpConstant %int16 !0xABCD0000",
  106. "%11 = OpConstant %uint16 !0xABCD0000",
  107. "%11 = OpConstant %half !0xABCD0000",
  108. "%11 = OpConstant %half !0x00010000"));
  109. TEST_F(ValidateLiterals, LiteralsKernelGood) {
  110. std::string str = GenerateKernelCode() + R"(
  111. %4 = OpConstant %uint8 !0x000000AB
  112. %6 = OpConstant %uint8 255
  113. )";
  114. CompileSuccessfully(str);
  115. ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
  116. }
  117. TEST_P(ValidateLiteralsKernel, LiteralsKernelBad) {
  118. std::string str = GenerateKernelCode() + GetParam();
  119. std::string inst_id = "2";
  120. CompileSuccessfully(str);
  121. EXPECT_EQ(SPV_ERROR_INVALID_VALUE, ValidateInstructions());
  122. EXPECT_THAT(
  123. getDiagnosticString(),
  124. HasSubstr("The high-order bits of a literal number in instruction <id> " +
  125. inst_id +
  126. " must be 0 for a floating-point type, "
  127. "or 0 for an integer type with Signedness of 0, "
  128. "or sign extended when Signedness is 1"));
  129. }
  130. INSTANTIATE_TEST_SUITE_P(
  131. LiteralsKernelCases, ValidateLiteralsKernel,
  132. ::testing::Values("%2 = OpConstant %uint8 !0xABCDEF00",
  133. "%2 = OpConstant %uint8 !0xABCDEFFF"));
  134. } // namespace
  135. } // namespace val
  136. } // namespace spvtools