Debian Codegraph @

test/link/function_variants.cpp

raw
1 // Copyright 2025 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 <string>
16
17 #include "gmock/gmock.h"
18 #include "test/link/linker_fixture.h"
19
20 namespace spvtools {
21 namespace {
22
23 using FunctionVariants = spvtest::LinkerTest;
24
25 TEST_F(FunctionVariants, Dot4) {
26 constexpr const char* const dot4_fp32 = R"(
27 OpCapability Kernel
28 OpCapability Addresses
29 OpCapability Int8
30 OpCapability Int64
31 OpCapability Linkage
32 %1 = OpExtInstImport "OpenCL.std"
33 OpMemoryModel Physical64 OpenCL
34 OpEntryPoint Kernel %2 "dot4" %3
35 OpExecutionMode %2 ContractionOff
36 OpSource OpenCL_C 102000
37 OpName %2 "dot4"
38 OpName %3 "__spirv_BuiltInGlobalInvocationId"
39 OpName %4 "entry"
40 %5 = OpTypeFloat 32
41 %6 = OpTypePointer CrossWorkgroup %5
42 %7 = OpTypeInt 8 0
43 %8 = OpTypePointer CrossWorkgroup %7
44 %9 = OpTypeVoid
45 %10 = OpTypeFunction %9 %6 %6 %8
46 %11 = OpTypeInt 64 0
47 %12 = OpTypeInt 32 0
48 %13 = OpTypeVector %11 3
49 %14 = OpTypePointer Input %13
50 %15 = OpTypeVector %5 4
51 %16 = OpConstant %11 30
52 %17 = OpConstant %11 32
53 %18 = OpConstant %12 2
54 %3 = OpVariable %14 Input
55 %2 = OpFunction %9 None %10
56 %19 = OpFunctionParameter %6
57 %20 = OpFunctionParameter %6
58 %21 = OpFunctionParameter %8
59 %4 = OpLabel
60 %22 = OpLoad %13 %3 Aligned 1
61 %23 = OpCompositeExtract %11 %22 0
62 %24 = OpUConvert %12 %23
63 %25 = OpShiftLeftLogical %12 %24 %18
64 %26 = OpSConvert %11 %25
65 %27 = OpExtInst %15 %1 vloadn %26 %19 4
66 %28 = OpExtInst %15 %1 vloadn %26 %20 4
67 %29 = OpDot %5 %27 %28
68 %30 = OpShiftLeftLogical %11 %23 %17
69 %31 = OpShiftRightArithmetic %11 %30 %16
70 %32 = OpInBoundsPtrAccessChain %8 %21 %31
71 %33 = OpBitcast %6 %32
72 OpStore %33 %29 Aligned 4
73 OpReturn
74 OpFunctionEnd
75 )";
76
77 constexpr const char* const dot4_fp16 = R"(
78 OpCapability Kernel
79 OpCapability Addresses
80 OpCapability Int8
81 OpCapability Int64
82 OpCapability Float16
83 OpCapability Linkage
84 %1 = OpExtInstImport "OpenCL.std"
85 OpMemoryModel Physical64 OpenCL
86 OpEntryPoint Kernel %2 "dot4" %3
87 OpExecutionMode %2 ContractionOff
88 OpSource OpenCL_C 102000
89 OpName %2 "dot4"
90 OpName %3 "__spirv_BuiltInGlobalInvocationId"
91 OpName %4 "entry"
92 %5 = OpTypeFloat 16
93 %6 = OpTypePointer CrossWorkgroup %5
94 %7 = OpTypeInt 8 0
95 %8 = OpTypePointer CrossWorkgroup %7
96 %9 = OpTypeVoid
97 %10 = OpTypeFunction %9 %6 %6 %8
98 %11 = OpTypeInt 64 0
99 %12 = OpTypeInt 32 0
100 %13 = OpTypeVector %11 3
101 %14 = OpTypePointer Input %13
102 %15 = OpTypeVector %5 4
103 %16 = OpConstant %11 31
104 %17 = OpConstant %11 32
105 %18 = OpConstant %12 2
106 %3 = OpVariable %14 Input
107 %2 = OpFunction %9 None %10
108 %19 = OpFunctionParameter %6
109 %20 = OpFunctionParameter %6
110 %21 = OpFunctionParameter %8
111 %4 = OpLabel
112 %22 = OpLoad %13 %3 Aligned 1
113 %23 = OpCompositeExtract %11 %22 0
114 %24 = OpUConvert %12 %23
115 %25 = OpShiftLeftLogical %12 %24 %18
116 %26 = OpSConvert %11 %25
117 %27 = OpExtInst %15 %1 vloadn %26 %19 4
118 %28 = OpExtInst %15 %1 vloadn %26 %20 4
119 %29 = OpDot %5 %27 %28
120 %30 = OpShiftLeftLogical %11 %23 %17
121 %31 = OpShiftRightArithmetic %11 %30 %16
122 %32 = OpInBoundsPtrAccessChain %8 %21 %31
123 %33 = OpBitcast %6 %32
124 OpStore %33 %29 Aligned 4
125 OpReturn
126 OpFunctionEnd
127 )";
128
129 // clang-format off
130 const std::vector<const char*> expected_lines = {
131 "OpCapability FunctionVariantsINTEL",
132 "OpCapability SpecConditionalINTEL",
133 "OpCapability Kernel",
134 "OpCapability Addresses",
135 "OpCapability Int8",
136 "OpCapability Int64",
137 "OpCapability Linkage",
138 "OpConditionalCapabilityINTEL %dot4_fp16_spv Float16",
139 "OpExtension \"SPV_INTEL_function_variants\"",
140 "OpConditionalEntryPointINTEL %dot4_fp32_spv Kernel %dot4 \"dot4\" %__spirv_BuiltInGlobalInvocationId",
141 "OpConditionalEntryPointINTEL %dot4_fp16_spv Kernel %dot4_0 \"dot4\" %__spirv_BuiltInGlobalInvocationId_0",
142 "OpModuleProcessed \"SPV_INTEL_function_variants registry version 0\"",
143 "OpDecorate %ulong_30 ConditionalINTEL %dot4_fp32_spv",
144 "OpDecorate %ulong_32 ConditionalINTEL %dot4_fp32_spv",
145 "OpDecorate %uint_2 ConditionalINTEL %dot4_fp32_spv",
146 "OpDecorate %__spirv_BuiltInGlobalInvocationId ConditionalINTEL %dot4_fp32_spv",
147 "OpDecorate %dot4 ConditionalINTEL %dot4_fp32_spv",
148 "OpDecorate %ulong_31 ConditionalINTEL %dot4_fp16_spv",
149 "OpDecorate %ulong_32_0 ConditionalINTEL %dot4_fp16_spv",
150 "OpDecorate %uint_2_0 ConditionalINTEL %dot4_fp16_spv",
151 "OpDecorate %__spirv_BuiltInGlobalInvocationId_0 ConditionalINTEL %dot4_fp16_spv",
152 "OpDecorate %dot4_0 ConditionalINTEL %dot4_fp16_spv",
153 "OpDecorate %float ConditionalINTEL %dot4_fp32_spv",
154 "OpDecorate %_ptr_CrossWorkgroup_float ConditionalINTEL %dot4_fp32_spv",
155 "OpDecorate %18 ConditionalINTEL %dot4_fp32_spv",
156 "OpDecorate %v4float ConditionalINTEL %dot4_fp32_spv",
157 "OpDecorate %half ConditionalINTEL %dot4_fp16_spv",
158 "OpDecorate %_ptr_CrossWorkgroup_half ConditionalINTEL %dot4_fp16_spv",
159 "OpDecorate %22 ConditionalINTEL %dot4_fp16_spv",
160 "OpDecorate %v4half ConditionalINTEL %dot4_fp16_spv",
161 "%float = OpTypeFloat 32",
162 "%_ptr_CrossWorkgroup_float = OpTypePointer CrossWorkgroup %float",
163 "%18 = OpTypeFunction %void %_ptr_CrossWorkgroup_float %_ptr_CrossWorkgroup_float %_ptr_CrossWorkgroup_uchar",
164 "%v4float = OpTypeVector %float 4",
165 "%ulong_30 = OpConstant %ulong 30",
166 "%ulong_32 = OpConstant %ulong 32",
167 "%uint_2 = OpConstant %uint 2",
168 "%__spirv_BuiltInGlobalInvocationId = OpVariable %_ptr_Input_v3ulong Input",
169 "%bool = OpTypeBool",
170 "%32 = OpSpecConstantArchitectureINTEL %bool 2 3 174 0",
171 "%33 = OpSpecConstantTargetINTEL %bool 7",
172 "%34 = OpSpecConstantTargetINTEL %bool 8",
173 "%35 = OpSpecConstantCapabilitiesINTEL %bool Addresses Linkage Kernel Int64 Int8",
174 "%36 = OpSpecConstantOp %bool LogicalOr %33 %34",
175 "%37 = OpSpecConstantOp %bool LogicalAnd %35 %32",
176 "%38 = OpSpecConstantOp %bool LogicalAnd %37 %36",
177 "%half = OpTypeFloat 16",
178 "%_ptr_CrossWorkgroup_half = OpTypePointer CrossWorkgroup %half",
179 "%22 = OpTypeFunction %void %_ptr_CrossWorkgroup_half %_ptr_CrossWorkgroup_half %_ptr_CrossWorkgroup_uchar",
180 "%v4half = OpTypeVector %half 4",
181 "%ulong_31 = OpConstant %ulong 31",
182 "%ulong_32_0 = OpConstant %ulong 32",
183 "%uint_2_0 = OpConstant %uint 2",
184 "%__spirv_BuiltInGlobalInvocationId_0 = OpVariable %_ptr_Input_v3ulong Input",
185 "%39 = OpSpecConstantArchitectureINTEL %bool 2 3 174 4",
186 "%40 = OpSpecConstantTargetINTEL %bool 7",
187 "%41 = OpSpecConstantTargetINTEL %bool 8",
188 "%42 = OpSpecConstantCapabilitiesINTEL %bool Addresses Linkage Kernel Float16 Int64 Int8",
189 "%43 = OpSpecConstantOp %bool LogicalOr %40 %41",
190 "%44 = OpSpecConstantOp %bool LogicalAnd %42 %39",
191 "%dot4_fp16_spv = OpSpecConstantOp %bool LogicalAnd %44 %43",
192 "%45 = OpSpecConstantOp %bool LogicalNot %dot4_fp16_spv",
193 "%dot4_fp32_spv = OpSpecConstantOp %bool LogicalAnd %38 %45",
194 "%dot4 = OpFunction %void None %18",
195 "%56 = OpDot %float %54 %55",
196 "%dot4_0 = OpFunction %void None %22",
197 "%71 = OpDot %half %69 %70",
198 };
199 // clang-format on
200
201 const std::string targets_csv =
202 "module,target,features\n"
203 "dot4_fp32.spv,7,\n"
204 "dot4_fp32.spv,8,\n"
205 "dot4_fp16.spv,7,\n"
206 "dot4_fp16.spv,8,\n";
207 const std::string architectures_csv =
208 "module,category,family,op,architecture\n"
209 "dot4_fp32.spv,2,3,174,0\n"
210 "dot4_fp16.spv,2,3,174,4\n";
211 const std::vector<std::string> sources = {dot4_fp32, dot4_fp16};
212 const std::vector<std::string> in_files = {"dot4_fp32.spv", "dot4_fp16.spv"};
213
214 LinkerOptions options;
215 options.SetInFiles(in_files);
216 options.SetFnVarTargetsCsv(targets_csv);
217 options.SetFnVarArchitecturesCsv(architectures_csv);
218 options.SetHasFnVarCapabilities(true);
219 options.SetCreateLibrary(true);
220 options.SetVerifyIds(true);
221
222 spvtest::Binary linked_binary;
223 spv_result_t res = AssembleAndLink(sources, &linked_binary, options);
224 EXPECT_EQ(SPV_SUCCESS, res) << GetErrorMessage();
225 EXPECT_THAT(GetErrorMessage(), std::string());
226 EXPECT_TRUE(Validate(linked_binary));
227
228 std::string linked_asm;
229 res = Disassemble(linked_binary, &linked_asm);
230 EXPECT_EQ(SPV_SUCCESS, res) << GetErrorMessage();
231 for (const auto& expected : expected_lines) {
232 EXPECT_THAT(linked_asm, testing::HasSubstr(expected));
233 }
234 }
235
236 TEST_F(FunctionVariants, FAddAsm) {
237 constexpr const char* const foo_base = R"(
238 OpCapability Kernel
239 OpCapability Addresses
240 OpCapability Int8
241 OpCapability Int64
242 OpCapability Linkage
243 %1 = OpExtInstImport "OpenCL.std"
244 OpMemoryModel Physical64 OpenCL
245 OpSource OpenCL_CPP 100000
246 OpName %2 "foo"
247 OpName %3 "add"
248 OpName %4 "entry"
249 OpName %5 "work"
250 OpName %6 "call"
251 OpName %7 "entry"
252 OpDecorate %2 LinkageAttributes "foo" Export
253 OpDecorate %5 LinkageAttributes "work" Export
254 %8 = OpTypeFloat 32
255 %9 = OpTypePointer Function %8
256 %10 = OpTypeFunction %8 %9 %9 %9
257 %11 = OpTypeInt 8 0
258 %12 = OpTypePointer Function %11
259 %13 = OpTypeInt 64 0
260 %14 = OpConstant %13 28
261 %15 = OpConstant %13 24
262 %16 = OpConstant %13 20
263 %17 = OpConstant %13 16
264 %18 = OpConstant %13 12
265 %19 = OpConstant %13 8
266 %20 = OpConstant %13 4
267 %2 = OpFunction %8 DontInline %10
268 %21 = OpFunctionParameter %9
269 %22 = OpFunctionParameter %9
270 %23 = OpFunctionParameter %9
271 %4 = OpLabel
272 %24 = OpLoad %8 %21 Aligned 4
273 %25 = OpLoad %8 %22 Aligned 4
274 %3 = OpFAdd %8 %24 %25
275 OpStore %23 %3 Aligned 4
276 OpReturnValue %3
277 OpFunctionEnd
278 %5 = OpFunction %8 None %10
279 %26 = OpFunctionParameter %9
280 %27 = OpFunctionParameter %9
281 %28 = OpFunctionParameter %9
282 %7 = OpLabel
283 %6 = OpFunctionCall %8 %2 %26 %27 %28
284 %29 = OpFDiv %8 %6 %6
285 %30 = OpFDiv %8 %29 %6
286 OpReturnValue %30
287 OpFunctionEnd
288 )";
289
290 constexpr const char* const foo_asm = R"(
291 OpCapability Kernel
292 OpCapability Addresses
293 OpCapability Linkage
294 OpCapability AsmINTEL
295 OpExtension "SPV_INTEL_inline_assembly"
296 %1 = OpExtInstImport "OpenCL.std"
297 OpMemoryModel Physical64 OpenCL
298 OpSource OpenCL_CPP 100000
299 OpName %2 "a"
300 OpName %3 "b"
301 OpName %4 "c"
302 OpName %5 "foo"
303 OpName %6 "add"
304 OpName %7 "entry"
305 OpDecorate %2 FuncParamAttr NoWrite
306 OpDecorate %2 FuncParamAttr NoAlias
307 OpDecorate %3 FuncParamAttr NoWrite
308 OpDecorate %3 FuncParamAttr NoAlias
309 OpDecorate %4 FuncParamAttr NoAlias
310 OpDecorate %5 LinkageAttributes "foo" Export
311 OpDecorate %8 SideEffectsINTEL
312 %9 = OpTypeFloat 32
313 %10 = OpTypePointer Function %9
314 %11 = OpTypeFunction %9 %10 %10 %10
315 %12 = OpTypeVoid
316 %13 = OpTypeFunction %12
317 %14 = OpAsmTargetINTEL "spirv64-unknown-unknown"
318 %8 = OpAsmINTEL %12 %13 %14 "nop1" ""
319 %5 = OpFunction %9 None %11
320 %2 = OpFunctionParameter %10
321 %3 = OpFunctionParameter %10
322 %4 = OpFunctionParameter %10
323 %7 = OpLabel
324 %15 = OpLoad %9 %2 Aligned 4
325 %16 = OpLoad %9 %3 Aligned 4
326 %6 = OpFSub %9 %15 %16
327 OpStore %4 %6 Aligned 4
328 %17 = OpAsmCallINTEL %12 %8
329 OpReturnValue %6
330 OpFunctionEnd
331 )";
332
333 // same as foo_asm, just with OpFMul and a different assembly string in
334 // OpAsmINTEL
335 constexpr const char* const foo_asm2 = R"(
336 OpCapability Kernel
337 OpCapability Addresses
338 OpCapability Linkage
339 OpCapability AsmINTEL
340 OpExtension "SPV_INTEL_inline_assembly"
341 %1 = OpExtInstImport "OpenCL.std"
342 OpMemoryModel Physical64 OpenCL
343 OpSource OpenCL_CPP 100000
344 OpName %2 "a"
345 OpName %3 "b"
346 OpName %4 "c"
347 OpName %5 "foo"
348 OpName %6 "add"
349 OpName %7 "entry"
350 OpDecorate %2 FuncParamAttr NoWrite
351 OpDecorate %2 FuncParamAttr NoAlias
352 OpDecorate %3 FuncParamAttr NoWrite
353 OpDecorate %3 FuncParamAttr NoAlias
354 OpDecorate %4 FuncParamAttr NoAlias
355 OpDecorate %5 LinkageAttributes "foo" Export
356 OpDecorate %8 SideEffectsINTEL
357 %9 = OpTypeFloat 32
358 %10 = OpTypePointer Function %9
359 %11 = OpTypeFunction %9 %10 %10 %10
360 %12 = OpTypeVoid
361 %13 = OpTypeFunction %12
362 %14 = OpAsmTargetINTEL "spirv64-unknown-unknown"
363 %8 = OpAsmINTEL %12 %13 %14 "nop2" ""
364 %5 = OpFunction %9 None %11
365 %2 = OpFunctionParameter %10
366 %3 = OpFunctionParameter %10
367 %4 = OpFunctionParameter %10
368 %7 = OpLabel
369 %15 = OpLoad %9 %2 Aligned 4
370 %16 = OpLoad %9 %3 Aligned 4
371 %6 = OpFMul %9 %15 %16
372 OpStore %4 %6 Aligned 4
373 %17 = OpAsmCallINTEL %12 %8
374 OpReturnValue %6
375 OpFunctionEnd
376 )";
377
378 // clang-format off
379 const std::vector<const char*> expected_lines = {
380 "OpCapability FunctionVariantsINTEL",
381 "OpCapability SpecConditionalINTEL",
382 "OpCapability Kernel",
383 "OpCapability Addresses",
384 "OpConditionalCapabilityINTEL %foo_spv Int8",
385 "OpConditionalCapabilityINTEL %foo_spv Int64",
386 "OpCapability Linkage",
387 "OpConditionalCapabilityINTEL %2 AsmINTEL",
388 "OpExtension \"SPV_INTEL_function_variants\"",
389 "OpConditionalExtensionINTEL %2 \"SPV_INTEL_inline_assembly\"",
390 "OpModuleProcessed \"SPV_INTEL_function_variants registry version 0\"",
391 "OpDecorate %foo LinkageAttributes \"foo\" Export",
392 "OpDecorate %work LinkageAttributes \"work\" Export",
393 "OpDecorate %ulong_28 ConditionalINTEL %foo_spv",
394 "OpDecorate %ulong_24 ConditionalINTEL %foo_spv",
395 "OpDecorate %ulong_20 ConditionalINTEL %foo_spv",
396 "OpDecorate %ulong_16 ConditionalINTEL %foo_spv",
397 "OpDecorate %ulong_12 ConditionalINTEL %foo_spv",
398 "OpDecorate %ulong_8 ConditionalINTEL %foo_spv",
399 "OpDecorate %ulong_4 ConditionalINTEL %foo_spv",
400 "OpDecorate %foo ConditionalINTEL %foo_spv",
401 "OpDecorate %foo_0 LinkageAttributes \"foo\" Export",
402 "OpDecorate %32 ConditionalINTEL %foo_asm_spv",
403 "OpDecorate %31 ConditionalINTEL %foo_asm_spv",
404 "OpDecorate %foo_0 ConditionalINTEL %foo_asm_spv",
405 "OpDecorate %foo_1 LinkageAttributes \"foo\" Export",
406 "OpDecorate %34 ConditionalINTEL %foo_asm2_spv",
407 "OpDecorate %33 ConditionalINTEL %foo_asm2_spv",
408 "OpDecorate %foo_1 ConditionalINTEL %foo_asm2_spv",
409 "OpDecorate %call ConditionalINTEL %foo_spv",
410 "OpDecorate %35 ConditionalINTEL %foo_asm_spv",
411 "OpDecorate %36 ConditionalINTEL %foo_asm2_spv",
412 "OpDecorate %uchar ConditionalINTEL %foo_spv",
413 "OpDecorate %_ptr_Function_uchar ConditionalINTEL %foo_spv",
414 "OpDecorate %ulong ConditionalINTEL %foo_spv",
415 "OpDecorate %void ConditionalINTEL %2",
416 "OpDecorate %41 ConditionalINTEL %2",
417 "%ulong_28 = OpConstant %ulong 28",
418 "%ulong_24 = OpConstant %ulong 24",
419 "%ulong_20 = OpConstant %ulong 20",
420 "%ulong_16 = OpConstant %ulong 16",
421 "%ulong_12 = OpConstant %ulong 12",
422 "%ulong_8 = OpConstant %ulong 8",
423 "%ulong_4 = OpConstant %ulong 4",
424 "%bool = OpTypeBool",
425 "%46 = OpSpecConstantTargetINTEL %bool 4",
426 "%47 = OpSpecConstantCapabilitiesINTEL %bool Addresses Linkage Kernel Int64 Int8",
427 "%48 = OpSpecConstantOp %bool LogicalAnd %47 %46",
428 "%31 = OpAsmINTEL %void %41 %32 \"nop1\" \"\"",
429 "%49 = OpSpecConstantArchitectureINTEL %bool 1 1 170 1",
430 "%50 = OpSpecConstantTargetINTEL %bool 4 9 10",
431 "%51 = OpSpecConstantCapabilitiesINTEL %bool Addresses Linkage Kernel AsmINTEL",
432 "%52 = OpSpecConstantOp %bool LogicalAnd %51 %49",
433 "%foo_asm_spv = OpSpecConstantOp %bool LogicalAnd %52 %50",
434 "%33 = OpAsmINTEL %void %41 %34 \"nop2\" \"\"",
435 "%53 = OpSpecConstantArchitectureINTEL %bool 1 7 174 1",
436 "%54 = OpSpecConstantArchitectureINTEL %bool 1 7 178 3",
437 "%55 = OpSpecConstantArchitectureINTEL %bool 1 8 170 1",
438 "%56 = OpSpecConstantArchitectureINTEL %bool 1 9 174 1",
439 "%57 = OpSpecConstantArchitectureINTEL %bool 1 9 178 3",
440 "%58 = OpSpecConstantTargetINTEL %bool 5 2 4 5",
441 "%59 = OpSpecConstantTargetINTEL %bool 6 2 4 5",
442 "%60 = OpSpecConstantCapabilitiesINTEL %bool Addresses Linkage Kernel AsmINTEL",
443 "%61 = OpSpecConstantOp %bool LogicalAnd %53 %54",
444 "%62 = OpSpecConstantOp %bool LogicalAnd %56 %57",
445 "%63 = OpSpecConstantOp %bool LogicalOr %61 %55",
446 "%64 = OpSpecConstantOp %bool LogicalOr %63 %62",
447 "%65 = OpSpecConstantOp %bool LogicalOr %58 %59",
448 "%66 = OpSpecConstantOp %bool LogicalAnd %60 %64",
449 "%foo_asm2_spv = OpSpecConstantOp %bool LogicalAnd %66 %65",
450 "%67 = OpSpecConstantOp %bool LogicalOr %foo_asm_spv %foo_asm2_spv",
451 "%68 = OpSpecConstantOp %bool LogicalNot %67",
452 "%foo_spv = OpSpecConstantOp %bool LogicalAnd %48 %68",
453 "%2 = OpSpecConstantOp %bool LogicalOr %foo_asm_spv %foo_asm2_spv",
454 "%foo = OpFunction %float DontInline %44",
455 "%add = OpFAdd %float %72 %73",
456 "%work = OpFunction %float None %44",
457 "%call = OpFunctionCall %float %foo %74 %75 %76",
458 "%35 = OpFunctionCall %float %foo_0 %74 %75 %76",
459 "%36 = OpFunctionCall %float %foo_1 %74 %75 %76",
460 "%77 = OpConditionalCopyObjectINTEL %float %foo_spv %call %foo_asm_spv %35 %foo_asm2_spv %36",
461 "%78 = OpFDiv %float %77 %77",
462 "%79 = OpFDiv %float %78 %77",
463 "OpReturnValue %79",
464 "%foo_0 = OpFunction %float None %44",
465 "%add_0 = OpFSub %float %80 %81",
466 "%82 = OpAsmCallINTEL %void %31",
467 "%foo_1 = OpFunction %float None %44",
468 "%add_1 = OpFMul %float %83 %84",
469 "%85 = OpAsmCallINTEL %void %33",
470 };
471 // clang-format on
472
473 const std::string targets_csv =
474 "module,target,features\n"
475 "foo.spv,04,\n" // test leading zeros
476 "foo_asm.spv,4,9/0010\n" // test leading zeros
477 "foo_asm2.spv,5,2/4/5\n"
478 "foo_asm2.spv,6,2/4/5\n";
479 const std::string architectures_csv =
480 "module,category,family,op,architecture\n"
481 "foo_asm.spv,1,1,170,1\n"
482 "foo_asm2.spv,1,7,174,1\n"
483 "foo_asm2.spv,1,7,178,3\n"
484 "foo_asm2.spv,1,8,170,1\n"
485 "foo_asm2.spv,1,9,174,1\n"
486 "foo_asm2.spv,1,9,178,3\n";
487 const std::vector<std::string> sources = {foo_base, foo_asm, foo_asm2};
488 const std::vector<std::string> in_files = {"foo.spv", "foo_asm.spv",
489 "foo_asm2.spv"};
490
491 LinkerOptions options;
492 options.SetInFiles(in_files);
493 options.SetFnVarTargetsCsv(targets_csv);
494 options.SetFnVarArchitecturesCsv(architectures_csv);
495 options.SetHasFnVarCapabilities(true);
496 options.SetCreateLibrary(true);
497 options.SetVerifyIds(true);
498
499 spvtest::Binary linked_binary;
500 spv_result_t res = AssembleAndLink(sources, &linked_binary, options);
501 EXPECT_EQ(SPV_SUCCESS, res) << GetErrorMessage();
502 EXPECT_THAT(GetErrorMessage(), std::string());
503 EXPECT_TRUE(Validate(linked_binary));
504
505 std::string linked_asm;
506 res = Disassemble(linked_binary, &linked_asm);
507 EXPECT_EQ(SPV_SUCCESS, res) << GetErrorMessage();
508 for (const auto& expected : expected_lines) {
509 EXPECT_THAT(linked_asm, testing::HasSubstr(expected));
510 }
511 }
512
513 TEST_F(FunctionVariants, InvalidNumber1) {
514 const std::string targets_csv =
515 "module,target,features\n"
516 "foo.spv,-4,9/10\n";
517 const std::string architectures_csv = "";
518 const std::vector<std::string> sources = {""};
519 const std::vector<std::string> in_files = {"foo.spv"};
520
521 LinkerOptions options;
522 options.SetInFiles(in_files);
523 options.SetFnVarTargetsCsv(targets_csv);
524 options.SetFnVarArchitecturesCsv(architectures_csv);
525 options.SetHasFnVarCapabilities(true);
526 options.SetCreateLibrary(true);
527 options.SetVerifyIds(true);
528
529 spvtest::Binary linked_binary;
530 spv_result_t res = AssembleAndLink(sources, &linked_binary, options);
531 EXPECT_EQ(SPV_ERROR_FNVAR, res) << GetErrorMessage();
532 EXPECT_THAT(GetErrorMessage(), "ERROR: 0: Error converting -4 to target.");
533 }
534
535 TEST_F(FunctionVariants, InvalidNumber2) {
536 const std::string targets_csv =
537 "module,target,features\n"
538 "foo.spv,4,9/-10\n";
539 const std::string architectures_csv = "";
540 const std::vector<std::string> sources = {""};
541 const std::vector<std::string> in_files = {"foo.spv"};
542
543 LinkerOptions options;
544 options.SetInFiles(in_files);
545 options.SetFnVarTargetsCsv(targets_csv);
546 options.SetFnVarArchitecturesCsv(architectures_csv);
547 options.SetHasFnVarCapabilities(true);
548 options.SetCreateLibrary(true);
549 options.SetVerifyIds(true);
550
551 spvtest::Binary linked_binary;
552 spv_result_t res = AssembleAndLink(sources, &linked_binary, options);
553 EXPECT_EQ(SPV_ERROR_FNVAR, res) << GetErrorMessage();
554 EXPECT_THAT(GetErrorMessage(),
555 "ERROR: 0: Error converting -10 in 9/-10 to target feature.");
556 }
557
558 TEST_F(FunctionVariants, InvalidNumber3) {
559 const std::string targets_csv =
560 "module,target,features\n"
561 "foo.spv,4.0,9/10\n";
562 const std::string architectures_csv = "";
563 const std::vector<std::string> sources = {""};
564 const std::vector<std::string> in_files = {"foo.spv"};
565
566 LinkerOptions options;
567 options.SetInFiles(in_files);
568 options.SetFnVarTargetsCsv(targets_csv);
569 options.SetFnVarArchitecturesCsv(architectures_csv);
570 options.SetHasFnVarCapabilities(true);
571 options.SetCreateLibrary(true);
572 options.SetVerifyIds(true);
573
574 spvtest::Binary linked_binary;
575 spv_result_t res = AssembleAndLink(sources, &linked_binary, options);
576 EXPECT_EQ(SPV_ERROR_FNVAR, res) << GetErrorMessage();
577 EXPECT_THAT(GetErrorMessage(), "ERROR: 0: Error converting 4.0 to target.");
578 }
579
580 } // namespace
581 } // namespace spvtools