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