|
| 1 | +// Copyright (c) 2026 Google 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 | +// Tests location integer rollover is correctly handles |
| 16 | + |
| 17 | +#include <string> |
| 18 | + |
| 19 | +#include "gmock/gmock.h" |
| 20 | +#include "test/unit_spirv.h" |
| 21 | +#include "test/val/val_fixtures.h" |
| 22 | + |
| 23 | +namespace spvtools { |
| 24 | +namespace val { |
| 25 | +namespace { |
| 26 | + |
| 27 | +using ::testing::HasSubstr; |
| 28 | + |
| 29 | +using ValidateLocationRollover = spvtest::ValidateBase<bool>; |
| 30 | + |
| 31 | +TEST_F(ValidateLocationRollover, VulkanLocationRollover) { |
| 32 | + std::string text = R"( |
| 33 | +OpCapability Shader |
| 34 | +OpMemoryModel Logical GLSL450 |
| 35 | +OpEntryPoint Fragment %1 "func" %var1 %var2 |
| 36 | +OpExecutionMode %1 OriginUpperLeft |
| 37 | +OpDecorate %var1 Location 0 |
| 38 | +OpDecorate %var2 Location 1073741824 |
| 39 | +%2 = OpTypeVoid |
| 40 | +%3 = OpTypeInt 32 0 |
| 41 | +%4 = OpTypePointer Output %3 |
| 42 | +%var1 = OpVariable %4 Output |
| 43 | +%var2 = OpVariable %4 Output |
| 44 | +%6 = OpTypeFunction %2 |
| 45 | +%1 = OpFunction %2 None %6 |
| 46 | +%7 = OpLabel |
| 47 | +OpReturn |
| 48 | +OpFunctionEnd |
| 49 | +)"; |
| 50 | + |
| 51 | + // 1073741824 * 4 = 4294967296 (2^32), which overflows to 0 in 32-bit. |
| 52 | + // This would cause it to be treated as Location 0 and conflict with %var1. |
| 53 | + |
| 54 | + CompileSuccessfully(text, SPV_ENV_VULKAN_1_2); |
| 55 | + EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2)) |
| 56 | + << getDiagnosticString(); |
| 57 | +} |
| 58 | + |
| 59 | +TEST_F(ValidateLocationRollover, VulkanLocationRolloverStructMember) { |
| 60 | + std::string text = R"( |
| 61 | +OpCapability Shader |
| 62 | +OpMemoryModel Logical GLSL450 |
| 63 | +OpEntryPoint Fragment %1 "func" %var |
| 64 | +OpExecutionMode %1 OriginUpperLeft |
| 65 | +OpMemberDecorate %struct 0 Location 0 |
| 66 | +OpMemberDecorate %struct 1 Location 1073741824 |
| 67 | +OpDecorate %struct Block |
| 68 | +%2 = OpTypeVoid |
| 69 | +%3 = OpTypeInt 32 0 |
| 70 | +%struct = OpTypeStruct %3 %3 |
| 71 | +%4 = OpTypePointer Output %struct |
| 72 | +%var = OpVariable %4 Output |
| 73 | +%6 = OpTypeFunction %2 |
| 74 | +%1 = OpFunction %2 None %6 |
| 75 | +%7 = OpLabel |
| 76 | +OpReturn |
| 77 | +OpFunctionEnd |
| 78 | +)"; |
| 79 | + |
| 80 | + CompileSuccessfully(text, SPV_ENV_VULKAN_1_2); |
| 81 | + EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2)) |
| 82 | + << getDiagnosticString(); |
| 83 | +} |
| 84 | + |
| 85 | +TEST_F(ValidateLocationRollover, VulkanLocationRolloverStructMemberStrided) { |
| 86 | + std::string text = R"( |
| 87 | +OpCapability Shader |
| 88 | +OpMemoryModel Logical GLSL450 |
| 89 | +OpEntryPoint Fragment %1 "func" %var |
| 90 | +OpExecutionMode %1 OriginUpperLeft |
| 91 | +OpMemberDecorate %struct 0 Location 0 |
| 92 | +OpMemberDecorate %struct 1 Location 1073741824 |
| 93 | +OpDecorate %struct Block |
| 94 | +%void = OpTypeVoid |
| 95 | +%u32 = OpTypeInt 32 0 |
| 96 | +%u32_1 = OpConstant %u32 1 |
| 97 | +%u32_2 = OpTypeVector %u32 2 |
| 98 | +%array = OpTypeArray %u32_2 %u32_1 |
| 99 | +%struct = OpTypeStruct %u32 %array |
| 100 | +%ptr = OpTypePointer Output %struct |
| 101 | +%var = OpVariable %ptr Output |
| 102 | +%func_type = OpTypeFunction %void |
| 103 | +%1 = OpFunction %void None %func_type |
| 104 | +%label = OpLabel |
| 105 | +OpReturn |
| 106 | +OpFunctionEnd |
| 107 | +)"; |
| 108 | + |
| 109 | + CompileSuccessfully(text, SPV_ENV_VULKAN_1_2); |
| 110 | + EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2)) |
| 111 | + << getDiagnosticString(); |
| 112 | +} |
| 113 | + |
| 114 | +} // namespace |
| 115 | +} // namespace val |
| 116 | +} // namespace spvtools |
0 commit comments