1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
// Copyright (C) 2025 NVIDIA Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "TestFixture.h"
#include "glslang/Public/ResourceLimits.h"
#include <gtest/gtest.h>
#include <regex>
#include <sstream>
#include <string>
namespace glslangtest {
class SpvPatternTest : public ::testing::Test {
protected:
void SetUp() override
{
// Set up any common test state.
}
void TearDown() override
{
// Clean up any common test state.
}
// Helper function to compile shader and get SPIR-V disassembly.
std::string compileShaderToSpirv(const std::string& shaderSource, EShLanguage stage)
{
glslang::TShader shader(stage);
glslang::TProgram program;
// Compile the shader
const char* shaderStrings = shaderSource.c_str();
shader.setStrings(&shaderStrings, 1);
if (!shader.parse(GetDefaultResources(), 450, false, EShMsgDefault)) {
return "COMPILATION_FAILED: " + std::string(shader.getInfoLog());
}
program.addShader(&shader);
if (!program.link(EShMsgDefault)) {
return "LINKING_FAILED: " + std::string(program.getInfoLog());
}
// Generate SPIR-V.
std::vector<uint32_t> spirv;
glslang::GlslangToSpv(*program.getIntermediate(stage), spirv);
// Disassemble SPIR-V to text.
std::ostringstream disassembly_stream;
spv::Disassemble(disassembly_stream, spirv);
return disassembly_stream.str();
}
// Helper function to check if the given SPIR-V string contains a specific pattern.
bool containsPattern(const std::string& spirvText, const std::string& pattern)
{
return spirvText.find(pattern) != std::string::npos;
}
// Helper function to check if the given SPIR-V string contains a UConvert instruction.
bool containsUConvert(const std::string& spirvText) { return containsPattern(spirvText, "UConvert"); }
};
// Test 1: Indexing an array with a regular int or uint should not generate a zero extension.
TEST_F(SpvPatternTest, RegularIntUintArrayIndexNoConversion)
{
const std::string shaderSource = R"(
#version 450 core
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
uint u = 150u;
int i = 100;
float arr[200];
float x = arr[u]; // Regular uint index
float y = arr[i]; // Regular int index
}
)";
std::string spirv = compileShaderToSpirv(shaderSource, EShLangCompute);
// Check that the SPIR-V does NOT contain conversion instructions for regular int/uint indices.
EXPECT_FALSE(containsUConvert(spirv))
<< "SPIR-V should not contain OpUConvert instruction for regular int/uint array indexing.\n"
<< "Generated SPIR-V:\n"
<< spirv;
}
// Test 2: Indexing an array with a variable index of type uint8_t should generate a zero extension.
TEST_F(SpvPatternTest, Uint8VariableIndexGeneratesUConvert)
{
const std::string shaderSource = R"(
#version 450 core
#extension GL_EXT_shader_explicit_arithmetic_types : enable
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
uint8_t u8 = uint8_t(150);
float arr[200];
float x = arr[u8]; // Variable uint8_t index
}
)";
std::string spirv = compileShaderToSpirv(shaderSource, EShLangCompute);
// Check that the SPIR-V contains OpUConvert instruction for variable uint8_t index.
EXPECT_TRUE(containsUConvert(spirv))
<< "SPIR-V should contain OpUConvert instruction for variable uint8_t array indexing.\n"
<< "Generated SPIR-V:\n"
<< spirv;
}
// Test 2: Indexing an array with a variable index of type uint16_t should generate a zero extension.
TEST_F(SpvPatternTest, Uint16VariableIndexGeneratesUConvert)
{
const std::string shaderSource = R"(
#version 450 core
#extension GL_EXT_shader_explicit_arithmetic_types : enable
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
uint16_t u16 = uint16_t(150);
float arr[200];
float x = arr[u16]; // Variable uint16_t index
}
)";
std::string spirv = compileShaderToSpirv(shaderSource, EShLangCompute);
// Check that the SPIR-V contains OpUConvert instruction for variable uint16_t index.
EXPECT_TRUE(containsUConvert(spirv))
<< "SPIR-V should contain OpUConvert instruction for variable uint16_t array indexing.\n"
<< "Generated SPIR-V:\n"
<< spirv;
}
// Test 3: Indexing an array with a constant index of type uint8_t should NOT generate a zero extension.
// Glslang generates small constants as regular 32-bit integers.
TEST_F(SpvPatternTest, Uint8ConstantIndexNoConversion)
{
const std::string shaderSource = R"(
#version 450 core
#extension GL_EXT_shader_explicit_arithmetic_types : enable
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
float arr[200];
float x = arr[uint8_t(150)]; // Constant uint8_t index
}
)";
std::string spirv = compileShaderToSpirv(shaderSource, EShLangCompute);
// Check that the SPIR-V does NOT contain OpUConvert instruction for constant uint8_t index.
// Glslang generates small constants as regular 32-bit integers, so no conversion is needed.
EXPECT_FALSE(containsUConvert(spirv))
<< "SPIR-V should not contain OpUConvert instruction for constant uint8_t array indexing.\n"
<< "Generated SPIR-V:\n"
<< spirv;
}
// Test 3: Indexing an array with a constant index of type uint16_t should NOT generate a zero extension.
// (Glslang generates small constants as regular 32-bit integers.)
TEST_F(SpvPatternTest, Uint16ConstantIndexNoConversion)
{
const std::string shaderSource = R"(
#version 450 core
#extension GL_EXT_shader_explicit_arithmetic_types : enable
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
float arr[200];
float x = arr[uint16_t(150)]; // Constant uint16_t index
}
)";
std::string spirv = compileShaderToSpirv(shaderSource, EShLangCompute);
// Check that the SPIR-V does NOT contain OpUConvert instruction for constant uint16_t index.
// Glslang generates small constants as regular 32-bit integers, so no conversion is needed.
EXPECT_FALSE(containsUConvert(spirv))
<< "SPIR-V should not contain OpUConvert instruction for constant uint16_t array indexing.\n"
<< "Generated SPIR-V:\n"
<< spirv;
}
} // namespace glslangtest
|