immediate_int_test.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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(spv::Op::OpTypeMatrix, {1, 2, 3})));
  53. EXPECT_THAT(CompiledInstructions("!0x00040018 !1 %b %123"),
  54. Eq(MakeInstruction(spv::Op::OpTypeMatrix, {1, 1, 2})));
  55. EXPECT_THAT(CompiledInstructions("%a = OpTypeMatrix !2 %123"),
  56. Eq(MakeInstruction(spv::Op::OpTypeMatrix, {1, 2, 2})));
  57. EXPECT_THAT(CompiledInstructions("%a = OpTypeMatrix %b !123"),
  58. Eq(MakeInstruction(spv::Op::OpTypeMatrix, {1, 2, 123})));
  59. EXPECT_THAT(CompiledInstructions("!0x00040018 %a !2 %123"),
  60. Eq(MakeInstruction(spv::Op::OpTypeMatrix, {1, 2, 2})));
  61. EXPECT_THAT(CompiledInstructions("!0x00040018 !1 %b !123"),
  62. Eq(MakeInstruction(spv::Op::OpTypeMatrix, {1, 1, 123})));
  63. EXPECT_THAT(CompiledInstructions("!0x00040018 !1 !2 !123"),
  64. Eq(MakeInstruction(spv::Op::OpTypeMatrix, {1, 2, 123})));
  65. }
  66. TEST_F(ImmediateIntTest, AnyWordAfterEqualsAndOpCode) {
  67. EXPECT_THAT(CompiledInstructions("%a = OpArrayLength !2 %c 123"),
  68. Eq(MakeInstruction(spv::Op::OpArrayLength, {2, 1, 2, 123})));
  69. EXPECT_THAT(CompiledInstructions("%a = OpArrayLength %b !3 123"),
  70. Eq(MakeInstruction(spv::Op::OpArrayLength, {1, 2, 3, 123})));
  71. EXPECT_THAT(CompiledInstructions("%a = OpArrayLength %b %c !123"),
  72. Eq(MakeInstruction(spv::Op::OpArrayLength, {1, 2, 3, 123})));
  73. EXPECT_THAT(CompiledInstructions("%a = OpArrayLength %b !3 !123"),
  74. Eq(MakeInstruction(spv::Op::OpArrayLength, {1, 2, 3, 123})));
  75. EXPECT_THAT(CompiledInstructions("%a = OpArrayLength !2 !3 123"),
  76. Eq(MakeInstruction(spv::Op::OpArrayLength, {2, 1, 3, 123})));
  77. EXPECT_THAT(CompiledInstructions("%a = OpArrayLength !2 !3 !123"),
  78. Eq(MakeInstruction(spv::Op::OpArrayLength, {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(spv::Op::OpTypeInt, {1, 64, 0}),
  99. MakeInstruction(spv::Op::OpConstant, {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. // Insert OpNop to avoid reading the immediate value as the extra FP encoding
  124. // operand to OpTypeFloat.
  125. TEST_F(ImmediateIntTest, FloatFollowingImmediate) {
  126. EXPECT_EQ(CompiledInstructions(
  127. "%1 = OpTypeFloat 32\nOpNop %2 = OpConstant %1 0.123"),
  128. CompiledInstructions(
  129. "%1 = OpTypeFloat 32\nOpNop !0x0004002b %1 !2 0.123"));
  130. EXPECT_EQ(CompiledInstructions(
  131. "%1 = OpTypeFloat 32\nOpNop %2 = OpConstant %1 -0.5"),
  132. CompiledInstructions(
  133. "%1 = OpTypeFloat 32\nOpNop !0x0004002b %1 !2 -0.5"));
  134. EXPECT_EQ(CompiledInstructions(
  135. "%1 = OpTypeFloat 32\nOpNop %2 = OpConstant %1 0.123"),
  136. CompiledInstructions(
  137. "%1 = OpTypeFloat 32\nOpNop !0x0004002b %1 %2 0.123"));
  138. EXPECT_EQ(CompiledInstructions(
  139. "%1 = OpTypeFloat 32\nOpNop %2 = OpConstant %1 -0.5"),
  140. CompiledInstructions(
  141. "%1 = OpTypeFloat 32\nOpNop !0x0004002b %1 %2 -0.5"));
  142. EXPECT_EQ(Concatenate({
  143. MakeInstruction(spv::Op::OpTypeInt, {1, 64, 0}),
  144. MakeInstruction(spv::Op::OpConstant, {1, 2, 0xb, 0xa}),
  145. MakeInstruction(spv::Op::OpSwitch,
  146. {2, 1234, BitwiseCast<uint32_t>(2.5f), 3}),
  147. }),
  148. CompiledInstructions("%i64 = OpTypeInt 64 0\n"
  149. "%big = OpConstant %i64 0xa0000000b\n"
  150. "OpSwitch %big !1234 2.5 %target\n"));
  151. }
  152. // Literal strings after !<integer> are handled correctly.
  153. TEST_F(ImmediateIntTest, StringFollowingImmediate) {
  154. // Try a variety of strings, including empty and single-character.
  155. for (std::string name : {"", "s", "longish", "really looooooooooooooooong"}) {
  156. const SpirvVector original =
  157. CompiledInstructions("OpMemberName %10 4 \"" + name + "\"");
  158. EXPECT_EQ(original,
  159. CompiledInstructions("OpMemberName %10 !4 \"" + name + "\""))
  160. << name;
  161. EXPECT_EQ(original,
  162. CompiledInstructions("OpMemberName !1 !4 \"" + name + "\""))
  163. << name;
  164. const uint16_t wordCount = static_cast<uint16_t>(4 + name.size() / 4);
  165. const uint32_t firstWord = spvOpcodeMake(wordCount, spv::Op::OpMemberName);
  166. EXPECT_EQ(original, CompiledInstructions("!" + std::to_string(firstWord) +
  167. " %10 !4 \"" + name + "\""))
  168. << name;
  169. }
  170. }
  171. // IDs after !<integer> are handled correctly.
  172. TEST_F(ImmediateIntTest, IdFollowingImmediate) {
  173. EXPECT_EQ(CompileSuccessfully("%123 = OpDecorationGroup"),
  174. CompileSuccessfully("!0x00020049 %123"));
  175. EXPECT_EQ(CompileSuccessfully("%group = OpDecorationGroup"),
  176. CompileSuccessfully("!0x00020049 %group"));
  177. }
  178. // !<integer> after !<integer> is handled correctly.
  179. TEST_F(ImmediateIntTest, ImmediateFollowingImmediate) {
  180. const SpirvVector original = CompiledInstructions("%a = OpTypeMatrix %b 7");
  181. EXPECT_EQ(original, CompiledInstructions("%a = OpTypeMatrix !2 !7"));
  182. EXPECT_EQ(original, CompiledInstructions("!0x00040018 %a !2 !7"));
  183. }
  184. TEST_F(ImmediateIntTest, InvalidStatement) {
  185. EXPECT_THAT(Subvector(CompileSuccessfully("!4 !3 !2 !1"), kFirstInstruction),
  186. ElementsAre(4, 3, 2, 1));
  187. }
  188. TEST_F(ImmediateIntTest, InvalidStatementBetweenValidOnes) {
  189. EXPECT_THAT(Subvector(CompileSuccessfully(
  190. "%10 = OpTypeInt 32 0 !5 !6 !7 OpEmitVertex"),
  191. kFirstInstruction),
  192. ElementsAre(spvOpcodeMake(4, spv::Op::OpTypeInt), 1, 32, 0, 5, 6,
  193. 7, spvOpcodeMake(1, spv::Op::OpEmitVertex)));
  194. }
  195. TEST_F(ImmediateIntTest, NextOpcodeRecognized) {
  196. const SpirvVector original = CompileSuccessfully(R"(
  197. %1 = OpLoad %10 %2 Volatile
  198. %4 = OpCompositeInsert %11 %1 %3 0 1 2
  199. )");
  200. const SpirvVector alternate = CompileSuccessfully(R"(
  201. %1 = OpLoad %10 %2 !1
  202. %4 = OpCompositeInsert %11 %1 %3 0 1 2
  203. )");
  204. EXPECT_EQ(original, alternate);
  205. }
  206. TEST_F(ImmediateIntTest, WrongLengthButNextOpcodeStillRecognized) {
  207. const SpirvVector original = CompileSuccessfully(R"(
  208. %1 = OpLoad %10 %2 Volatile
  209. OpCopyMemorySized %3 %4 %1
  210. )");
  211. const SpirvVector alternate = CompileSuccessfully(R"(
  212. !0x0002003D %10 %1 %2 !1
  213. OpCopyMemorySized %3 %4 %1
  214. )");
  215. EXPECT_EQ(0x0002003Du, alternate[kFirstInstruction]);
  216. EXPECT_EQ(Subvector(original, kFirstInstruction + 1),
  217. Subvector(alternate, kFirstInstruction + 1));
  218. }
  219. // Like NextOpcodeRecognized, but next statement is in assignment form.
  220. TEST_F(ImmediateIntTest, NextAssignmentRecognized) {
  221. const SpirvVector original = CompileSuccessfully(R"(
  222. %1 = OpLoad %10 %2 None
  223. %4 = OpFunctionCall %10 %3 %123
  224. )");
  225. const SpirvVector alternate = CompileSuccessfully(R"(
  226. %1 = OpLoad %10 %2 !0
  227. %4 = OpFunctionCall %10 %3 %123
  228. )");
  229. EXPECT_EQ(original, alternate);
  230. }
  231. // Two instructions in a row each have !<integer> opcode.
  232. TEST_F(ImmediateIntTest, ConsecutiveImmediateOpcodes) {
  233. const SpirvVector original = CompileSuccessfully(R"(
  234. %1 = OpConstantSampler %10 Clamp 78 Linear
  235. %4 = OpFRem %11 %3 %2
  236. %5 = OpIsValidEvent %12 %2
  237. )");
  238. const SpirvVector alternate = CompileSuccessfully(R"(
  239. !0x0006002D %10 %1 !2 78 !1
  240. !0x0005008C %11 %4 %3 %2
  241. %5 = OpIsValidEvent %12 %2
  242. )");
  243. EXPECT_EQ(original, alternate);
  244. }
  245. // !<integer> followed by, eg, an enum or '=' or a random bareword.
  246. TEST_F(ImmediateIntTest, ForbiddenOperands) {
  247. EXPECT_THAT(CompileFailure("OpMemoryModel !0 OpenCL"), HasSubstr("OpenCL"));
  248. EXPECT_THAT(CompileFailure("!1 %0 = !2"), HasSubstr("="));
  249. EXPECT_THAT(CompileFailure("OpMemoryModel !0 random_bareword"),
  250. HasSubstr("random_bareword"));
  251. // Immediate integers longer than one 32-bit word.
  252. EXPECT_THAT(CompileFailure("!5000000000"), HasSubstr("5000000000"));
  253. EXPECT_THAT(CompileFailure("!999999999999999999"),
  254. HasSubstr("999999999999999999"));
  255. EXPECT_THAT(CompileFailure("!0x00020049 !5000000000"),
  256. HasSubstr("5000000000"));
  257. // Negative numbers.
  258. EXPECT_THAT(CompileFailure("!0x00020049 !-123"), HasSubstr("-123"));
  259. }
  260. TEST_F(ImmediateIntTest, NotInteger) {
  261. EXPECT_THAT(CompileFailure("!abc"), StrEq("Invalid immediate integer: !abc"));
  262. EXPECT_THAT(CompileFailure("!12.3"),
  263. StrEq("Invalid immediate integer: !12.3"));
  264. EXPECT_THAT(CompileFailure("!12K"), StrEq("Invalid immediate integer: !12K"));
  265. }
  266. } // namespace
  267. } // namespace utils
  268. } // namespace spvtools