Skip to content

Commit

Permalink
Externals: update spv-remapper to latest glslang (14.3.0)
Browse files Browse the repository at this point in the history
  • Loading branch information
aras-p committed Sep 23, 2024
1 parent 15fcc5d commit b55ada0
Show file tree
Hide file tree
Showing 14 changed files with 3,170 additions and 1,359 deletions.
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ to improve this:
- Encoding several instructions in a more compact form, e.g. the "typical <=4 component swizzle"
shape of a VectorShuffle instruction, or sequences of MemberDecorate instructions.

A somewhat similar utility is [spirv-remap from glslang](https://github.com/KhronosGroup/glslang/blob/master/README-spirv-remap.txt).
A somewhat similar utility is [spirv-remap from glslang](https://github.com/KhronosGroup/glslang/blob/main/README-spirv-remap.txt).

See [this blog post](https://aras-p.info/blog/2016/09/01/SPIR-V-Compression/) for more information about
how I did SMOL-V.
Expand All @@ -45,7 +45,7 @@ Other functions are for development/statistics purposes, to figure out frequenci
distributions of the instructions.

There's a test + compression benchmarking suite in `testing/testmain.cpp`, using that needs adding
other files under testing/external to the build too (3rd party code: glslang remapper, Zstd, LZ4, miniz).
other files under testing/external to the build too (3rd party code: glslang remapper 14.3.0, Zstd, LZ4, miniz).

## Changelog

Expand Down Expand Up @@ -89,32 +89,32 @@ used for SMOL-V testing". Details on them:

## Results

As of 2024 Sep 23, results on 380 shaders (under `tests/spirv-dumps`) are:
As of 2024 Sep 23, results on 383 shaders (under `tests/spirv-dumps`) are:

```
Compressed with <none>:
Raw 5905.3KB 100.0%
Remapper 5805.5KB 98.3%
SmolV 2217.6KB 37.6%
Raw 5948.7KB 100.0%
Remapper 5848.8KB 98.3%
SmolV 2237.2KB 37.6%
Compressed with zlib:
Raw 1537.4KB 26.0%
Remapper 1472.7KB 24.9%
SmolV 840.2KB 14.2%
Raw 1550.6KB 26.1%
Remapper 1486.3KB 25.0%
SmolV 848.2KB 14.3%
Compressed with LZ4 HC:
Raw 1755.2KB 29.7%
Remapper 1601.5KB 27.1%
SmolV 873.8KB 14.8%
Raw 1773.1KB 29.8%
Remapper 1618.6KB 27.2%
SmolV 884.5KB 14.9%
Compressed with Zstandard:
Raw 1223.5KB 20.7%
Remapper 1045.7KB 17.7%
SmolV 682.1KB 11.6%
Raw 1234.8KB 20.8%
Remapper 1056.6KB 17.8%
SmolV 690.9KB 11.6%
Compressed with Zstandard 20:
Raw 837.1KB 14.2%
Remapper 711.2KB 12.0%
SmolV 535.0KB 9.1%
Raw 845.3KB 14.2%
Remapper 717.1KB 12.1%
SmolV 542.3KB 9.1%
```

Decoding these 380 shaders from SMOL-V back into SPIR-V takes 10.1ms (VS2022, x64 Release, AMD Ryzen 5950X, one thread).
Decoding these 383 shaders from SMOL-V back into SPIR-V takes 10.1ms (VS2022, x64 Release, AMD Ryzen 5950X, one thread).

* "Raw" is just raw SPIR-V, with no extra processing.
* "Remapper" is spirv-remap from glslang, with debug info stripping.
Expand Down
35 changes: 35 additions & 0 deletions testing/external/glslang/SPIRV/GLSL.ext.ARM.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
** Copyright (c) 2022 ARM Limited
**
** Permission is hereby granted, free of charge, to any person obtaining a copy
** of this software and/or associated documentation files (the "Materials"),
** to deal in the Materials without restriction, including without limitation
** the rights to use, copy, modify, merge, publish, distribute, sublicense,
** and/or sell copies of the Materials, and to permit persons to whom the
** Materials are 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 Materials.
**
** MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS KHRONOS
** STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS SPECIFICATIONS AND
** HEADER INFORMATION ARE LOCATED AT https://www.khronos.org/registry/
**
** THE MATERIALS ARE 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 MATERIALS OR THE USE OR OTHER DEALINGS
** IN THE MATERIALS.
*/

#ifndef GLSLextARM_H
#define GLSLextARM_H

static const int GLSLextARMVersion = 100;
static const int GLSLextARMRevision = 1;

static const char * const E_SPV_ARM_core_builtins = "SPV_ARM_core_builtins";

#endif // #ifndef GLSLextARM_H
7 changes: 7 additions & 0 deletions testing/external/glslang/SPIRV/GLSL.ext.EXT.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,12 @@ static const char* const E_SPV_EXT_shader_viewport_index_layer = "SPV_EXT_shade
static const char* const E_SPV_EXT_fragment_fully_covered = "SPV_EXT_fragment_fully_covered";
static const char* const E_SPV_EXT_fragment_invocation_density = "SPV_EXT_fragment_invocation_density";
static const char* const E_SPV_EXT_demote_to_helper_invocation = "SPV_EXT_demote_to_helper_invocation";
static const char* const E_SPV_EXT_shader_atomic_float_add = "SPV_EXT_shader_atomic_float_add";
static const char* const E_SPV_EXT_shader_atomic_float16_add = "SPV_EXT_shader_atomic_float16_add";
static const char* const E_SPV_EXT_shader_atomic_float_min_max = "SPV_EXT_shader_atomic_float_min_max";
static const char* const E_SPV_EXT_shader_image_int64 = "SPV_EXT_shader_image_int64";
static const char* const E_SPV_EXT_shader_tile_image = "SPV_EXT_shader_tile_image";
static const char* const E_SPV_EXT_mesh_shader = "SPV_EXT_mesh_shader";
static const char* const E_SPV_ARM_cooperative_matrix_layouts = "SPV_ARM_cooperative_matrix_layouts";

#endif // #ifndef GLSLextEXT_H
23 changes: 21 additions & 2 deletions testing/external/glslang/SPIRV/GLSL.ext.KHR.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/*
** Copyright (c) 2014-2016 The Khronos Group Inc.
** Copyright (c) 2014-2020 The Khronos Group Inc.
** Copyright (C) 2022-2024 Arm Limited.
** Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved.
**
** Permission is hereby granted, free of charge, to any person obtaining a copy
** of this software and/or associated documentation files (the "Materials"),
Expand Down Expand Up @@ -28,7 +30,7 @@
#define GLSLextKHR_H

static const int GLSLextKHRVersion = 100;
static const int GLSLextKHRRevision = 2;
static const int GLSLextKHRRevision = 3;

static const char* const E_SPV_KHR_shader_ballot = "SPV_KHR_shader_ballot";
static const char* const E_SPV_KHR_subgroup_vote = "SPV_KHR_subgroup_vote";
Expand All @@ -44,5 +46,22 @@ static const char* const E_SPV_EXT_physical_storage_buffer = "SPV_EXT_physi
static const char* const E_SPV_KHR_physical_storage_buffer = "SPV_KHR_physical_storage_buffer";
static const char* const E_SPV_EXT_fragment_shader_interlock = "SPV_EXT_fragment_shader_interlock";
static const char* const E_SPV_KHR_shader_clock = "SPV_KHR_shader_clock";
static const char* const E_SPV_KHR_non_semantic_info = "SPV_KHR_non_semantic_info";
static const char* const E_SPV_KHR_ray_tracing = "SPV_KHR_ray_tracing";
static const char* const E_SPV_KHR_ray_query = "SPV_KHR_ray_query";
static const char* const E_SPV_KHR_fragment_shading_rate = "SPV_KHR_fragment_shading_rate";
static const char* const E_SPV_KHR_terminate_invocation = "SPV_KHR_terminate_invocation";
static const char* const E_SPV_KHR_workgroup_memory_explicit_layout = "SPV_KHR_workgroup_memory_explicit_layout";
static const char* const E_SPV_KHR_subgroup_uniform_control_flow = "SPV_KHR_subgroup_uniform_control_flow";
static const char* const E_SPV_KHR_fragment_shader_barycentric = "SPV_KHR_fragment_shader_barycentric";
static const char* const E_SPV_KHR_quad_control = "SPV_KHR_quad_control";
static const char* const E_SPV_AMD_shader_early_and_late_fragment_tests = "SPV_AMD_shader_early_and_late_fragment_tests";
static const char* const E_SPV_KHR_ray_tracing_position_fetch = "SPV_KHR_ray_tracing_position_fetch";
static const char* const E_SPV_KHR_cooperative_matrix = "SPV_KHR_cooperative_matrix";
static const char* const E_SPV_KHR_maximal_reconvergence = "SPV_KHR_maximal_reconvergence";
static const char* const E_SPV_KHR_subgroup_rotate = "SPV_KHR_subgroup_rotate";
static const char* const E_SPV_KHR_expect_assume = "SPV_KHR_expect_assume";
static const char* const E_SPV_EXT_replicated_composites = "SPV_EXT_replicated_composites";
static const char* const E_SPV_KHR_relaxed_extended_instruction = "SPV_KHR_relaxed_extended_instruction";

#endif // #ifndef GLSLextKHR_H
12 changes: 12 additions & 0 deletions testing/external/glslang/SPIRV/GLSL.ext.NV.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ const char* const E_SPV_NV_mesh_shader = "SPV_NV_mesh_shader";
//SPV_NV_raytracing
const char* const E_SPV_NV_ray_tracing = "SPV_NV_ray_tracing";

//SPV_NV_ray_tracing_motion_blur
const char* const E_SPV_NV_ray_tracing_motion_blur = "SPV_NV_ray_tracing_motion_blur";

//SPV_NV_shading_rate
const char* const E_SPV_NV_shading_rate = "SPV_NV_shading_rate";

Expand All @@ -78,4 +81,13 @@ const char* const E_SPV_NV_cooperative_matrix = "SPV_NV_cooperative_matrix";
//SPV_NV_shader_sm_builtins
const char* const E_SPV_NV_shader_sm_builtins = "SPV_NV_shader_sm_builtins";

//SPV_NV_shader_execution_reorder
const char* const E_SPV_NV_shader_invocation_reorder = "SPV_NV_shader_invocation_reorder";

//SPV_NV_displacement_micromap
const char* const E_SPV_NV_displacement_micromap = "SPV_NV_displacement_micromap";

//SPV_NV_shader_atomic_fp16_vector
const char* const E_SPV_NV_shader_atomic_fp16_vector = "SPV_NV_shader_atomic_fp16_vector";

#endif // #ifndef GLSLextNV_H
43 changes: 43 additions & 0 deletions testing/external/glslang/SPIRV/GLSL.ext.QCOM.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
** Copyright (c) 2021 The Khronos Group Inc.
**
** Permission is hereby granted, free of charge, to any person obtaining a copy
** of this software and/or associated documentation files (the "Materials"),
** to deal in the Materials without restriction, including without limitation
** the rights to use, copy, modify, merge, publish, distribute, sublicense,
** and/or sell copies of the Materials, and to permit persons to whom the
** Materials are 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 Materials.
**
** MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS KHRONOS
** STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS SPECIFICATIONS AND
** HEADER INFORMATION ARE LOCATED AT https://www.khronos.org/registry/
**
** THE MATERIALS ARE 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 MATERIALS OR THE USE OR OTHER DEALINGS
** IN THE MATERIALS.
*/

#ifndef GLSLextQCOM_H
#define GLSLextQCOM_H

enum BuiltIn;
enum Decoration;
enum Op;
enum Capability;

static const int GLSLextQCOMVersion = 100;
static const int GLSLextQCOMRevision = 1;

//SPV_QCOM_image_processing
const char* const E_SPV_QCOM_image_processing = "SPV_QCOM_image_processing";
//SPV_QCOM_image_processing2
const char* const E_SPV_QCOM_image_processing2 = "SPV_QCOM_image_processing2";

#endif // #ifndef GLSLextQCOM_H
2 changes: 1 addition & 1 deletion testing/external/glslang/SPIRV/GLSL.std.450.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
**
** MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS KHRONOS
** STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS SPECIFICATIONS AND
** HEADER INFORMATION ARE LOCATED AT https://www.khronos.org/registry/
** HEADER INFORMATION ARE LOCATED AT https://www.khronos.org/registry/
**
** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
** OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Expand Down
76 changes: 58 additions & 18 deletions testing/external/glslang/SPIRV/SPVRemapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@
#include "SPVRemapper.h"
#include "doc.h"

#if !defined (use_cpp11)
// ... not supported before C++11
#else // defined (use_cpp11)

#include <algorithm>
#include <cassert>
#include "../glslang/Include/Common.h"
Expand Down Expand Up @@ -160,15 +156,29 @@ namespace spv {
}

// Is this an opcode we should remove when using --strip?
bool spirvbin_t::isStripOp(spv::Op opCode) const
bool spirvbin_t::isStripOp(spv::Op opCode, unsigned start) const
{
switch (opCode) {
case spv::OpSource:
case spv::OpSourceExtension:
case spv::OpName:
case spv::OpMemberName:
case spv::OpLine: return true;
default: return false;
case spv::OpLine :
{
const std::string name = literalString(start + 2);

std::vector<std::string>::const_iterator it;
for (it = stripWhiteList.begin(); it < stripWhiteList.end(); it++)
{
if (name.find(*it) != std::string::npos) {
return false;
}
}

return true;
}
default :
return false;
}
}

Expand Down Expand Up @@ -297,15 +307,21 @@ namespace spv {
std::string spirvbin_t::literalString(unsigned word) const
{
std::string literal;
const spirword_t * pos = spv.data() + word;

literal.reserve(16);

const char* bytes = reinterpret_cast<const char*>(spv.data() + word);

while (bytes && *bytes)
literal += *bytes++;

return literal;
do {
spirword_t word = *pos;
for (int i = 0; i < 4; i++) {
char c = word & 0xff;
if (c == '\0')
return literal;
literal += c;
word >>= 8;
}
pos++;
} while (true);
}

void spirvbin_t::applyMap()
Expand Down Expand Up @@ -366,7 +382,7 @@ namespace spv {
process(
[&](spv::Op opCode, unsigned start) {
// remember opcodes we want to strip later
if (isStripOp(opCode))
if (isStripOp(opCode, start))
stripInst(start);
return true;
},
Expand Down Expand Up @@ -544,6 +560,9 @@ namespace spv {
// Extended instructions: currently, assume everything is an ID.
// TODO: add whatever data we need for exceptions to that
if (opCode == spv::OpExtInst) {

idFn(asId(word)); // Instruction set is an ID that also needs to be mapped

word += 2; // instruction set, and instruction from set
numOperands -= 2;

Expand Down Expand Up @@ -625,6 +644,9 @@ namespace spv {
break;
}

case spv::OperandVariableLiteralStrings:
return nextInst;

// Execution mode might have extra literal operands. Skip them.
case spv::OperandExecutionMode:
return nextInst;
Expand Down Expand Up @@ -658,6 +680,7 @@ namespace spv {
case spv::OperandKernelEnqueueFlags:
case spv::OperandKernelProfilingInfo:
case spv::OperandCapability:
case spv::OperandCooperativeMatrixOperands:
++word;
break;

Expand Down Expand Up @@ -827,7 +850,15 @@ namespace spv {
[&](spv::Id& id) {
if (thisOpCode != spv::OpNop) {
++idCounter;
const std::uint32_t hashval = opCounter[thisOpCode] * thisOpCode * 50047 + idCounter + fnId * 117;
const std::uint32_t hashval =
// Explicitly cast operands to unsigned int to avoid integer
// promotion to signed int followed by integer overflow,
// which would result in undefined behavior.
static_cast<unsigned int>(opCounter[thisOpCode])
* thisOpCode
* 50047
+ idCounter
+ static_cast<unsigned int>(fnId) * 117;

if (isOldIdUnmapped(id))
localId(id, nextUnusedId(hashval % softTypeIdLimit + firstMappedID));
Expand Down Expand Up @@ -1474,14 +1505,23 @@ namespace spv {
}

// remap from a memory image
void spirvbin_t::remap(std::vector<std::uint32_t>& in_spv, std::uint32_t opts)
void spirvbin_t::remap(std::vector<std::uint32_t>& in_spv, const std::vector<std::string>& whiteListStrings,
std::uint32_t opts)
{
stripWhiteList = whiteListStrings;
spv.swap(in_spv);
remap(opts);
spv.swap(in_spv);
}

} // namespace SPV
// remap from a memory image - legacy interface without white list
void spirvbin_t::remap(std::vector<std::uint32_t>& in_spv, std::uint32_t opts)
{
stripWhiteList.clear();
spv.swap(in_spv);
remap(opts);
spv.swap(in_spv);
}

#endif // defined (use_cpp11)
} // namespace SPV

Loading

0 comments on commit b55ada0

Please sign in to comment.