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 // Assembler tests for instructions in the "Device-Side Enqueue Instructions"
16 // section of the SPIR-V spec.
17
18 #include <string>
19 #include <vector>
20
21 #include "gmock/gmock.h"
22 #include "test/test_fixture.h"
23 #include "test/unit_spirv.h"
24
25 namespace spvtools {
26 namespace {
27
28 using spvtest::MakeInstruction;
29 using ::testing::Eq;
30
31 // Test OpEnqueueKernel
32
33 struct KernelEnqueueCase {
34 std::string local_size_source;
35 std::vector<uint32_t> local_size_operands;
36 };
37
38 using OpEnqueueKernelGood =
39 spvtest::TextToBinaryTestBase<::testing::TestWithParam<KernelEnqueueCase>>;
40
TEST_P(OpEnqueueKernelGood,Sample)41 TEST_P(OpEnqueueKernelGood, Sample) {
42 const std::string input =
43 "%result = OpEnqueueKernel %type %queue %flags %NDRange %num_events"
44 " %wait_events %ret_event %invoke %param %param_size %param_align " +
45 GetParam().local_size_source;
46 EXPECT_THAT(CompiledInstructions(input),
47 Eq(MakeInstruction(SpvOpEnqueueKernel,
48 {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
49 GetParam().local_size_operands)));
50 }
51
52 INSTANTIATE_TEST_CASE_P(
53 TextToBinaryTest, OpEnqueueKernelGood,
54 ::testing::ValuesIn(std::vector<KernelEnqueueCase>{
55 // Provide IDs for pointer-to-local arguments for the
56 // invoked function.
57 // Test up to 10 such arguments.
58 // I (dneto) can't find a limit on the number of kernel
59 // arguments in OpenCL C 2.0 Rev 29, e.g. in section 6.9
60 // Restrictions.
61 {"", {}},
62 {"%l0", {13}},
63 {"%l0 %l1", {13, 14}},
64 {"%l0 %l1 %l2", {13, 14, 15}},
65 {"%l0 %l1 %l2 %l3", {13, 14, 15, 16}},
66 {"%l0 %l1 %l2 %l3 %l4", {13, 14, 15, 16, 17}},
67 {"%l0 %l1 %l2 %l3 %l4 %l5", {13, 14, 15, 16, 17, 18}},
68 {"%l0 %l1 %l2 %l3 %l4 %l5 %l6", {13, 14, 15, 16, 17, 18, 19}},
69 {"%l0 %l1 %l2 %l3 %l4 %l5 %l6 %l7", {13, 14, 15, 16, 17, 18, 19, 20}},
70 {"%l0 %l1 %l2 %l3 %l4 %l5 %l6 %l7 %l8",
71 {13, 14, 15, 16, 17, 18, 19, 20, 21}},
72 {"%l0 %l1 %l2 %l3 %l4 %l5 %l6 %l7 %l8 %l9",
73 {13, 14, 15, 16, 17, 18, 19, 20, 21, 22}},
74 }), );
75
76 // Test some bad parses of OpEnqueueKernel. For other cases, we're relying
77 // on the uniformity of the parsing algorithm. The following two tests, ensure
78 // that every required ID operand is specified, and is actually an ID operand.
79 using OpKernelEnqueueBad = spvtest::TextToBinaryTest;
80
TEST_F(OpKernelEnqueueBad,MissingLastOperand)81 TEST_F(OpKernelEnqueueBad, MissingLastOperand) {
82 EXPECT_THAT(
83 CompileFailure(
84 "%result = OpEnqueueKernel %type %queue %flags %NDRange %num_events"
85 " %wait_events %ret_event %invoke %param %param_size"),
86 Eq("Expected operand, found end of stream."));
87 }
88
TEST_F(OpKernelEnqueueBad,InvalidLastOperand)89 TEST_F(OpKernelEnqueueBad, InvalidLastOperand) {
90 EXPECT_THAT(
91 CompileFailure(
92 "%result = OpEnqueueKernel %type %queue %flags %NDRange %num_events"
93 " %wait_events %ret_event %invoke %param %param_size 42"),
94 Eq("Expected id to start with %."));
95 }
96
97 // TODO(dneto): OpEnqueueMarker
98 // TODO(dneto): OpGetKernelNDRangeSubGroupCount
99 // TODO(dneto): OpGetKernelNDRangeMaxSubGroupSize
100 // TODO(dneto): OpGetKernelWorkGroupSize
101 // TODO(dneto): OpGetKernelPreferredWorkGroupSizeMultiple
102 // TODO(dneto): OpRetainEvent
103 // TODO(dneto): OpReleaseEvent
104 // TODO(dneto): OpCreateUserEvent
105 // TODO(dneto): OpSetUserEventStatus
106 // TODO(dneto): OpCaptureEventProfilingInfo
107 // TODO(dneto): OpGetDefaultQueue
108 // TODO(dneto): OpBuildNDRange
109 // TODO(dneto): OpBuildNDRange
110
111 } // namespace
112 } // namespace spvtools
113