Debian Codegraph @

test/fuzz/transformation_add_no_contraction_decoration_test.cpp

raw
1 // Copyright (c) 2019 Google LLC
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 "source/fuzz/transformation_add_no_contraction_decoration.h"
16
17 #include "gtest/gtest.h"
18 #include "source/fuzz/fuzzer_util.h"
19 #include "test/fuzz/fuzz_test_util.h"
20
21 namespace spvtools {
22 namespace fuzz {
23 namespace {
24
25 TEST(TransformationAddNoContractionDecorationTest, BasicScenarios) {
26 // This is a simple transformation and this test handles the main cases.
27
28 std::string shader = R"(
29 OpCapability Shader
30 %1 = OpExtInstImport "GLSL.std.450"
31 OpMemoryModel Logical GLSL450
32 OpEntryPoint Fragment %4 "main"
33 OpExecutionMode %4 OriginUpperLeft
34 OpSource ESSL 310
35 OpName %4 "main"
36 OpName %8 "x"
37 OpName %10 "y"
38 OpName %14 "i"
39 %2 = OpTypeVoid
40 %3 = OpTypeFunction %2
41 %6 = OpTypeFloat 32
42 %7 = OpTypePointer Function %6
43 %9 = OpConstant %6 0
44 %11 = OpConstant %6 2
45 %12 = OpTypeInt 32 1
46 %13 = OpTypePointer Function %12
47 %15 = OpConstant %12 0
48 %22 = OpConstant %12 10
49 %23 = OpTypeBool
50 %31 = OpConstant %6 3.5999999
51 %38 = OpConstant %12 1
52 %4 = OpFunction %2 None %3
53 %5 = OpLabel
54 %8 = OpVariable %7 Function
55 %10 = OpVariable %7 Function
56 %14 = OpVariable %13 Function
57 OpStore %8 %9
58 OpStore %10 %11
59 OpStore %14 %15
60 OpBranch %16
61 %16 = OpLabel
62 OpLoopMerge %18 %19 None
63 OpBranch %20
64 %20 = OpLabel
65 %21 = OpLoad %12 %14
66 %24 = OpSLessThan %23 %21 %22
67 OpBranchConditional %24 %17 %18
68 %17 = OpLabel
69 %25 = OpLoad %6 %10
70 %26 = OpLoad %6 %10
71 %27 = OpFMul %6 %25 %26
72 %28 = OpLoad %6 %8
73 %29 = OpFAdd %6 %28 %27
74 OpStore %8 %29
75 %30 = OpLoad %6 %10
76 %32 = OpFDiv %6 %30 %31
77 OpStore %10 %32
78 %33 = OpLoad %12 %14
79 %34 = OpConvertSToF %6 %33
80 %35 = OpLoad %6 %8
81 %36 = OpFAdd %6 %35 %34
82 OpStore %8 %36
83 OpBranch %19
84 %19 = OpLabel
85 %37 = OpLoad %12 %14
86 %39 = OpIAdd %12 %37 %38
87 OpStore %14 %39
88 OpBranch %16
89 %18 = OpLabel
90 OpReturn
91 OpFunctionEnd
92 )";
93
94 const auto env = SPV_ENV_UNIVERSAL_1_4;
95 const auto consumer = nullptr;
96 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
97 spvtools::ValidatorOptions validator_options;
98 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
99 kConsoleMessageConsumer));
100 TransformationContext transformation_context(
101 MakeUnique<FactManager>(context.get()), validator_options);
102 // Invalid: 200 is not an id
103 ASSERT_FALSE(TransformationAddNoContractionDecoration(200).IsApplicable(
104 context.get(), transformation_context));
105 // Invalid: 17 is a block id
106 ASSERT_FALSE(TransformationAddNoContractionDecoration(17).IsApplicable(
107 context.get(), transformation_context));
108 // Invalid: 24 is not arithmetic
109 ASSERT_FALSE(TransformationAddNoContractionDecoration(24).IsApplicable(
110 context.get(), transformation_context));
111
112 // It is valid to add NoContraction to each of these ids.
113 for (uint32_t result_id : {32u, 27u, 29u, 39u}) {
114 TransformationAddNoContractionDecoration transformation(result_id);
115 ASSERT_TRUE(
116 transformation.IsApplicable(context.get(), transformation_context));
117 ApplyAndCheckFreshIds(transformation, context.get(),
118 &transformation_context);
119 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
120 context.get(), validator_options, kConsoleMessageConsumer));
121 }
122
123 std::string after_transformation = R"(
124 OpCapability Shader
125 %1 = OpExtInstImport "GLSL.std.450"
126 OpMemoryModel Logical GLSL450
127 OpEntryPoint Fragment %4 "main"
128 OpExecutionMode %4 OriginUpperLeft
129 OpSource ESSL 310
130 OpName %4 "main"
131 OpName %8 "x"
132 OpName %10 "y"
133 OpName %14 "i"
134 OpDecorate %32 NoContraction
135 OpDecorate %27 NoContraction
136 OpDecorate %29 NoContraction
137 OpDecorate %39 NoContraction
138 %2 = OpTypeVoid
139 %3 = OpTypeFunction %2
140 %6 = OpTypeFloat 32
141 %7 = OpTypePointer Function %6
142 %9 = OpConstant %6 0
143 %11 = OpConstant %6 2
144 %12 = OpTypeInt 32 1
145 %13 = OpTypePointer Function %12
146 %15 = OpConstant %12 0
147 %22 = OpConstant %12 10
148 %23 = OpTypeBool
149 %31 = OpConstant %6 3.5999999
150 %38 = OpConstant %12 1
151 %4 = OpFunction %2 None %3
152 %5 = OpLabel
153 %8 = OpVariable %7 Function
154 %10 = OpVariable %7 Function
155 %14 = OpVariable %13 Function
156 OpStore %8 %9
157 OpStore %10 %11
158 OpStore %14 %15
159 OpBranch %16
160 %16 = OpLabel
161 OpLoopMerge %18 %19 None
162 OpBranch %20
163 %20 = OpLabel
164 %21 = OpLoad %12 %14
165 %24 = OpSLessThan %23 %21 %22
166 OpBranchConditional %24 %17 %18
167 %17 = OpLabel
168 %25 = OpLoad %6 %10
169 %26 = OpLoad %6 %10
170 %27 = OpFMul %6 %25 %26
171 %28 = OpLoad %6 %8
172 %29 = OpFAdd %6 %28 %27
173 OpStore %8 %29
174 %30 = OpLoad %6 %10
175 %32 = OpFDiv %6 %30 %31
176 OpStore %10 %32
177 %33 = OpLoad %12 %14
178 %34 = OpConvertSToF %6 %33
179 %35 = OpLoad %6 %8
180 %36 = OpFAdd %6 %35 %34
181 OpStore %8 %36
182 OpBranch %19
183 %19 = OpLabel
184 %37 = OpLoad %12 %14
185 %39 = OpIAdd %12 %37 %38
186 OpStore %14 %39
187 OpBranch %16
188 %18 = OpLabel
189 OpReturn
190 OpFunctionEnd
191 )";
192
193 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
194 }
195
196 } // namespace
197 } // namespace fuzz
198 } // namespace spvtools