google / google/shaderc

Optimization issue when specifying --target-env

Open
#1,403 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
2.2k
Forks
445
Avg merge
11h 6m
Merged PRs (30d)
6

Description

Hello, I noticed a performance degradations when calling some heavy non-side-effected GLSL function a few times, without using its result. Simplifying the code and checking that issue still exists, I ended up with this code:

```GLSL
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_EXT_nonuniform_qualifier : enable

struct SampleStruct {
int field;
};

layout (set = 0, binding = 1) uniform GlobalDSet0 {
SampleStruct[16768] elems;
} buffers[];

SampleStruct read_buf() { return buffers[0].elems[0]; }

void main() {
read_buf();
}
```

Which compiles (`glslc shader.frag -O --target-env=vulkan1.2`) into:
```
; SPIR-V
; Version: 1.5
; Generator: Google Shaderc over Glslang; 11
; Bound: 37
; Schema: 0
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %4 "main" %19
OpExecutionMode %4 OriginUpperLeft
OpMemberDecorate %_struct_11 0 Offset 0
OpDecorate %_arr__struct_11_uint_16768 ArrayStride 16
OpMemberDecorate %_struct_15 0 Offset 0
OpDecorate %_struct_15 Block
OpDecorate %19 DescriptorSet 0
OpDecorate %19 Binding 1
%void = OpTypeVoid
%3 = OpTypeFunction %void
%int = OpTypeInt 32 1
%_struct_7 = OpTypeStruct %int
%_struct_11 = OpTypeStruct %int
%uint = OpTypeInt 32 0
%uint_16768 = OpConstant %uint 16768
%_arr__struct_11_uint_16768 = OpTypeArray %_struct_11 %uint_16768
%_struct_15 = OpTypeStruct %_arr__struct_11_uint_16768
%uint_1 = OpConstant %uint 1
%_arr__struct_15_uint_1 = OpTypeArray %_struct_15 %uint_1
%_ptr_Uniform__arr__struct_15_uint_1 = OpTypePointer Uniform %_arr__struct_15_uint_1
%19 = OpVariable %_ptr_Uniform__arr__struct_15_uint_1 Uniform
%int_0 = OpConstant %int 0
%_ptr_Uniform__struct_11 = OpTypePointer Uniform %_struct_11
%4 = OpFunction %void None %3
%5 = OpLabel
%34 = OpAccessChain %_ptr_Uniform__struct_11 %19 %int_0 %int_0 %int_0
%35 = OpLoad %_struct_11 %34
%36 = OpCopyLogical %_struct_7 %35
OpReturn
OpFunctionEnd
```

As I can see, `read_buf()` is a pure function, but calling it generates additional code in `main()`.

This issue occures when I set `--target-env` to `vulkan1.2` or `vulkan1.3` , but clears out at `vulkan1.0`, `vulkan1.1`, or without specifying target env at all.

Version:
glslc from VulkanSDK 1.3.268.0
```
$ glslc --version
shaderc v2023.7 v2023.7
spirv-tools v2023.5 v2022.4-352-g360d469b
glslang 11.1.0-803-g48f9ed8b

Target: SPIR-V 1.0
```

Am I missing something about these Vulkan versions, or is this an optimizer bug?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.