immediate_int_test.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // Copyright (c) 2015-2016 The Khronos Group 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. #include <cassert>
  15. #include <string>
  16. #include <vector>
  17. #include "gmock/gmock.h"
  18. #include "source/util/bitutils.h"
  19. #include "test/test_fixture.h"
  20. namespace spvtools {
  21. namespace utils {
  22. namespace {
  23. using spvtest::Concatenate;
  24. using spvtest::MakeInstruction;
  25. using spvtest::ScopedContext;
  26. using spvtest::TextToBinaryTest;
  27. using ::testing::ElementsAre;
  28. using ::testing::Eq;
  29. using ::testing::HasSubstr;
  30. using ::testing::StrEq;
  31. TEST_F(TextToBinaryTest, ImmediateIntOpCode) {
  32. SetText("!0x00FF00FF");
  33. ASSERT_EQ(SPV_SUCCESS, spvTextToBinary(ScopedContext().context, text.str,
  34. text.length, &binary, &diagnostic));
  35. EXPECT_EQ(0x00FF00FFu, binary->code[5]);
  36. if (diagnostic) {
  37. spvDiagnosticPrint(diagnostic);
  38. }
  39. }
  40. TEST_F(TextToBinaryTest, ImmediateIntOperand) {
  41. SetText("OpCapability !0x00FF00FF");
  42. EXPECT_EQ(SPV_SUCCESS, spvTextToBinary(ScopedContext().context, text.str,
  43. text.length, &binary, &diagnostic));
  44. EXPECT_EQ(0x00FF00FFu, binary->code[6]);
  45. if (diagnostic) {
  46. spvDiagnosticPrint(diagnostic);
  47. }
  48. }
  49. using ImmediateIntTest = TextToBinaryTest;
  50. TEST_F(ImmediateIntTest, AnyWordInSimpleStatement) {
  51. EXPECT_THAT(CompiledInstructions("!0x00040018 %a %b %123"),
  52. Eq(MakeInstruction(SpvOpTypeMatrix, {1, 2, 3})));
  53. EXPECT_THAT(CompiledInstructions("!0x00040018 !1 %b %123"),
  54. Eq(MakeInstruction(SpvOpTypeMatrix, {1, 1, 2})));
  55. EXPECT_THAT(CompiledInstructions("%a = OpTypeMatrix !2 %123"),
  56. Eq(MakeInstruction(SpvOpTypeMatrix, {1, 2, 2})));
  57. EXPECT_THAT(CompiledInstructions("%a = OpTypeMatrix %b !123"),
  58. Eq(MakeInstruction(SpvOpTypeMatrix, {1, 2, 123})));
  59. EXPECT_THAT(CompiledInstructions("!0x00040018 %a !2 %123"),
  60. Eq(MakeInstruction(SpvOpTypeMatrix, {1, 2, 2})));
  61. EXPECT_THAT(CompiledInstructions("!0x00040018 !1 %b !123"),
  62. Eq(MakeInstruction(SpvOpTypeMatrix, {1, 1, 123})));
  63. EXPECT_THAT(CompiledInstructions("!0x00040018 !1 !2 !123"),
  64. Eq(MakeInstruction(SpvOpTypeMatrix, {1, 2, 123})));
  65. }
  66. TEST_F(ImmediateIntTest, AnyWordAfterEqualsAndOpCode) {
  67. EXPECT_THAT(CompiledInstructions("%a = OpArrayLength !2 %c 123"),
  68. Eq(MakeInstruction(SpvOpArrayLength, {2, 1, 2, 123})));
  69. EXPECT_THAT(CompiledInstructions("%a = OpArrayLength %b !3 123"),
  70. Eq(MakeInstruction(SpvOpArrayLength, {1, 2, 3, 123})));
  71. EXPECT_THAT(CompiledInstructions("%a = OpArrayLength %b %c !123"),
  72. Eq(MakeInstruction(SpvOpArrayLength, {1, 2, 3, 123})));
  73. EXPECT_THAT(CompiledInstructions("%a = OpArrayLength %b !3 !123"),
  74. Eq(MakeInstruction(SpvOpArrayLength, {1, 2, 3, 123})));
  75. EXPECT_THAT(CompiledInstructions("%a = OpArrayLength !2 !3 123"),
  76. Eq(MakeInstruction(SpvOpArrayLength, {2, 1, 3, 123})));
  77. EXPECT_THAT(CompiledInstructions("%a = OpArrayLength !2 !3 !123"),
  78. Eq(MakeInstruction(SpvOpArrayLength, {2, 1, 3, 123})));
  79. }
  80. TEST_F(ImmediateIntTest, ResultIdInAssignment) {
  81. EXPECT_EQ("!2 not allowed before =.",
  82. CompileFailure("!2 = OpArrayLength %12 %1 123"));
  83. EXPECT_EQ("!2 not allowed before =.",
  84. CompileFailure("!2 = !0x00040044 %12 %1 123"));
  85. }
  86. TEST_F(ImmediateIntTest, OpCodeInAssignment) {
  87. EXPECT_EQ("Invalid Opcode prefix '!0x00040044'.",
  88. CompileFailure("%2 = !0x00040044 %12 %1 123"));
  89. }
  90. // Literal integers after !<integer> are handled correctly.
  91. TEST_F(ImmediateIntTest, IntegerFollowingImmediate) {
  92. const SpirvVector original = CompiledInstructions("%1 = OpTypeInt 8 1");
  93. EXPECT_EQ(original, CompiledInstructions("!0x00040015 1 8 1"));
  94. EXPECT_EQ(original, CompiledInstructions("!0x00040015 !1 8 1"));
  95. // With !<integer>, we can (and can only) accept 32-bit number literals,
  96. // even when we declare the return type is 64-bit.
  97. EXPECT_EQ(Concatenate({
  98. MakeInstruction(SpvOpTypeInt, {1, 64, 0}),
  99. MakeInstruction(SpvOpConstant, {1, 2, 4294967295}),
  100. }),
  101. CompiledInstructions("%i64 = OpTypeInt 64 0\n"
  102. "!0x0004002b %i64 !2 4294967295"));
  103. // 64-bit integer literal.
  104. EXPECT_EQ("Invalid word following !<integer>: 5000000000",
  105. CompileFailure("%2 = OpConstant !1 5000000000"));
  106. EXPECT_EQ("Invalid word following !<integer>: 5000000000",
  107. CompileFailure("%i64 = OpTypeInt 64 0\n"
  108. "!0x0005002b %i64 !2 5000000000"));
  109. // Negative integer.
  110. EXPECT_EQ(CompiledInstructions("%i64 = OpTypeInt 32 1\n"
  111. "%2 = OpConstant %i64 -123"),
  112. CompiledInstructions("%i64 = OpTypeInt 32 1\n"
  113. "!0x0004002b %i64 !2 -123"));
  114. // TODO(deki): uncomment assertions below and make them pass.
  115. // Hex value(s).
  116. // EXPECT_EQ(CompileSuccessfully("%1 = OpConstant %10 0x12345678"),
  117. // CompileSuccessfully("OpConstant %10 !1 0x12345678", kCAF));
  118. // EXPECT_EQ(
  119. // CompileSuccessfully("%1 = OpConstant %10 0x12345678 0x87654321"),
  120. // CompileSuccessfully("OpConstant %10 !1 0x12345678 0x87654321", kCAF));
  121. }
  122. // Literal floats after !<integer> are handled correctly.
  123. TEST_F(ImmediateIntTest, FloatFollowingImmediate) {
  124. EXPECT_EQ(
  125. CompiledInstructions("%1 = OpTypeFloat 32\n%2 = OpConstant %1 0.123"),
  126. CompiledInstructions("%1 = OpTypeFloat 32\n!0x0004002b %1 !2 0.123"));
  127. EXPECT_EQ(
  128. CompiledInstructions("%1 = OpTypeFloat 32\n%2 = OpConstant %1 -0.5"),
  129. CompiledInstructions("%1 = OpTypeFloat 32\n!0x0004002b %1 !2 -0.5"));
  130. EXPECT_EQ(
  131. CompiledInstructions("%1 = OpTypeFloat 32\n%2 = OpConstant %1 0.123"),
  132. CompiledInstructions("%1 = OpTypeFloat 32\n!0x0004002b %1 %2 0.123"));
  133. EXPECT_EQ(
  134. CompiledInstructions("%1 = OpTypeFloat 32\n%2 = OpConstant %1 -0.5"),
  135. CompiledInstructions("%1 = OpTypeFloat 32\n!0x0004002b %1 %2 -0.5"));
  136. EXPECT_EQ(Concatenate({
  137. MakeInstruction(SpvOpTypeInt, {1, 64, 0}),
  138. MakeInstruction(SpvOpConstant, {1, 2, 0xb, 0xa}),
  139. MakeInstruction(SpvOpSwitch,
  140. {2, 1234, BitwiseCast<uint32_t>(2.5f), 3}),
  141. }),
  142. CompiledInstructions("%i64 = OpTypeInt 64 0\n"
  143. "%big = OpConstant %i64 0xa0000000b\n"
  144. "OpSwitch %big !1234 2.5 %target\n"));
  145. }
  146. // Literal strings after !<integer> are handled correctly.
  147. TEST_F(ImmediateIntTest, StringFollowingImmediate) {
  148. // Try a variety of strings, including empty and single-character.
  149. for (std::string name : {"", "s", "longish", "really looooooooooooooooong"}) {
  150. const SpirvVector original =
  151. CompiledInstructions("OpMemberName %10 4 \"" + name + "\"");
  152. EXPECT_EQ(original,
  153. CompiledInstructions("OpMemberName %10 !4 \"" + name + "\""))
  154. << name;
  155. EXPECT_EQ(original,
  156. CompiledInstructions("OpMemberName !1 !4 \"" + name + "\""))
  157. << name;
  158. const uint16_t wordCount = static_cast<uint16_t>(4 + name.size() / 4);
  159. const uint32_t firstWord = spvOpcodeMake(wordCount, SpvOpMemberName);
  160. EXPECT_EQ(original, CompiledInstructions("!" + std::to_string(firstWord) +
  161. " %10 !4 \"" + name + "\""))
  162. << name;
  163. }
  164. }
  165. // IDs after !<integer> are handled correctly.
  166. TEST_F(ImmediateIntTest, IdFollowingImmediate) {
  167. EXPECT_EQ(CompileSuccessfully("%123 = OpDecorationGroup"),
  168. CompileSuccessfully("!0x00020049 %123"));
  169. EXPECT_EQ(CompileSuccessfully("%group = OpDecorationGroup"),
  170. CompileSuccessfully("!0x00020049 %group"));
  171. }
  172. // !<integer> after !<integer> is handled correctly.
  173. TEST_F(ImmediateIntTest, ImmediateFollowingImmediate) {
  174. const SpirvVector original = CompiledInstructions("%a = OpTypeMatrix %b 7");
  175. EXPECT_EQ(original, CompiledInstructions("%a = OpTypeMatrix !2 !7"));
  176. EXPECT_EQ(original, CompiledInstructions("!0x00040018 %a !2 !7"));
  177. }
  178. TEST_F(ImmediateIntTest, InvalidStatement) {
  179. EXPECT_THAT(Subvector(CompileSuccessfully("!4 !3 !2 !1"), kFirstInstruction),
  180. ElementsAre(4, 3, 2, 1));
  181. }
  182. TEST_F(ImmediateIntTest, InvalidStatementBetweenValidOnes) {
  183. EXPECT_THAT(Subvector(CompileSuccessfully(
  184. "%10 = OpTypeFloat 32 !5 !6 !7 OpEmitVertex"),
  185. kFirstInstruction),
  186. ElementsAre(spvOpcodeMake(3, SpvOpTypeFloat), 1, 32, 5, 6, 7,
  187. spvOpcodeMake(1, SpvOpEmitVertex)));
  188. }
  189. TEST_F(ImmediateIntTest, NextOpcodeRecognized) {
  190. const SpirvVector original = CompileSuccessfully(R"(
  191. %1 = OpLoad %10 %2 Volatile
  192. %4 = OpCompositeInsert %11 %1 %3 0 1 2
  193. )");
  194. const SpirvVector alternate = CompileSuccessfully(R"(
  195. %1 = OpLoad %10 %2 !1
  196. %4 = OpCompositeInsert %11 %1 %3 0 1 2
  197. )");
  198. EXPECT_EQ(original, alternate);
  199. }
  200. TEST_F(ImmediateIntTest, WrongLengthButNextOpcodeStillRecognized) {
  201. const SpirvVector original = CompileSuccessfully(R"(
  202. %1 = OpLoad %10 %2 Volatile
  203. OpCopyMemorySized %3 %4 %1
  204. )");
  205. const SpirvVector alternate = CompileSuccessfully(R"(
  206. !0x0002003D %10 %1 %2 !1
  207. OpCopyMemorySized %3 %4 %1
  208. )");
  209. EXPECT_EQ(0x0002003Du, alternate[kFirstInstruction]);
  210. EXPECT_EQ(Subvector(original, kFirstInstruction + 1),
  211. Subvector(alternate, kFirstInstruction + 1));
  212. }
  213. // Like NextOpcodeRecognized, but next statement is in assignment form.
  214. TEST_F(ImmediateIntTest, NextAssignmentRecognized) {
  215. const SpirvVector original = CompileSuccessfully(R"(
  216. %1 = OpLoad %10 %2 None
  217. %4 = OpFunctionCall %10 %3 %123
  218. )");
  219. const SpirvVector alternate = CompileSuccessfully(R"(
  220. %1 = OpLoad %10 %2 !0
  221. %4 = OpFunctionCall %10 %3 %123
  222. )");
  223. EXPECT_EQ(original, alternate);
  224. }
  225. // Two instructions in a row each have !<integer> opcode.
  226. TEST_F(ImmediateIntTest, ConsecutiveImmediateOpcodes) {
  227. const SpirvVector original = CompileSuccessfully(R"(
  228. %1 = OpConstantSampler %10 Clamp 78 Linear
  229. %4 = OpFRem %11 %3 %2
  230. %5 = OpIsValidEvent %12 %2
  231. )");
  232. const SpirvVector alternate = CompileSuccessfully(R"(
  233. !0x0006002D %10 %1 !2 78 !1
  234. !0x0005008C %11 %4 %3 %2
  235. %5 = OpIsValidEvent %12 %2
  236. )");
  237. EXPECT_EQ(original, alternate);
  238. }
  239. // !<integer> followed by, eg, an enum or '=' or a random bareword.
  240. TEST_F(ImmediateIntTest, ForbiddenOperands) {
  241. EXPECT_THAT(CompileFailure("OpMemoryModel !0 OpenCL"), HasSubstr("OpenCL"));
  242. EXPECT_THAT(CompileFailure("!1 %0 = !2"), HasSubstr("="));
  243. EXPECT_THAT(CompileFailure("OpMemoryModel !0 random_bareword"),
  244. HasSubstr("random_bareword"));
  245. // Immediate integers longer than one 32-bit word.
  246. EXPECT_THAT(CompileFailure("!5000000000"), HasSubstr("5000000000"));
  247. EXPECT_THAT(CompileFailure("!999999999999999999"),
  248. HasSubstr("999999999999999999"));
  249. EXPECT_THAT(CompileFailure("!0x00020049 !5000000000"),
  250. HasSubstr("5000000000"));
  251. // Negative numbers.
  252. EXPECT_THAT(CompileFailure("!0x00020049 !-123"), HasSubstr("-123"));
  253. }
  254. TEST_F(ImmediateIntTest, NotInteger) {
  255. EXPECT_THAT(CompileFailure("!abc"), StrEq("Invalid immediate integer: !abc"));
  256. EXPECT_THAT(CompileFailure("!12.3"),
  257. StrEq("Invalid immediate integer: !12.3"));
  258. EXPECT_THAT(CompileFailure("!12K"), StrEq("Invalid immediate integer: !12K"));
  259. }
  260. } // namespace
  261. } // namespace utils
  262. } // namespace spvtools