Possible miscompilation on a single-valued array

Test settings

System:    Host: - Kernel: 5.11.0-25-generic x86_64 bits: 64 compiler: gcc v: 10.2.1 Desktop: GNOME 3.38.4 
           tk: GTK 3.24.25 wm: gnome-shell dm: GDM3 Distro: Ubuntu 21.04 (Hirsute Hippo) 
CPU:       Info: 6-Core model: Intel Core i7-8750H bits: 64 type: MT MCP arch: Kaby Lake note: check rev: A L2 cache: 9 MiB 
           flags: avx avx2 lm nx pae sse sse2 sse3 sse4_1 sse4_2 ssse3 vmx bogomips: 52799 
           Speed: 2800 MHz min/max: 800/4100 MHz Core speeds (MHz): 1: 2800 2: 2800 3: 2800 4: 2130 5: 2800 6: 2642 7: 2742 
           8: 2761 9: 2782 10: 2628 11: 2691 12: 2710 
Graphics:  Device-1: Intel CoffeeLake-H GT2 [UHD Graphics 630] vendor: ASUSTeK driver: i915 v: kernel bus ID: 00:02.0 
           chip ID: 8086:3e9b 
           Device-2: NVIDIA GP107M [GeForce GTX 1050 Ti Mobile] vendor: ASUSTeK driver: nvidia v: 470.57.02 bus ID: 01:00.0 
           chip ID: 10de:1c8c 
           Device-3: IMC Networks USB2.0 HD UVC WebCam type: USB driver: uvcvideo bus ID: 1-7:3 chip ID: 13d3:5666 
           Display: x11 server: X.Org 1.20.11 compositor: gnome-shell driver: loaded: modesetting,nvidia 
           unloaded: fbdev,nouveau,vesa resolution: 1: 1920x1080~120Hz 2: 1920x1080~60Hz s-dpi: 96 
           OpenGL: renderer: NVIDIA GeForce GTX 1050 Ti/PCIe/SSE2 v: 4.6.0 NVIDIA 470.57.02 direct render: Yes 

Description

The following code containing an empty switch body does not seem to execute the side-effecting operation in the switch statement.

#version 310 es

layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
layout(std430, binding = 0) buffer buffer_0 {
 uint ext_0[3]; // 2 1 0
};

void main()
{
 bool var_0[1] = bool[1](false);
 var_0[(ext_0[0] < ext_0[1] ? 3 : 2) % 1] = bool(true);
 ext_0[2] = var_0[0] ? 1u : 2u;
}

NVIDIA output:

buffer_0 2 1 2

Expected output:

buffer_0 2 1 1

Thinking process:

  • The ternary operator is false, the value picked for index is 2
  • The value is taken modulo 1 (to get it in-bound) so var_0[0] is set to true.
  • ext_0[2] = 1u is executed

Code link

For the related code in the shadertrap test format, see: shader14.shadertrap (685 Bytes)

Shadertrap is a testing framework. To use the shadetrap code,

  1. Get or build Shadertrap:
    1. A build version of Shadertrap for linux can be found on Shadertrap release repository.
    2. To build from source, please refer to Shadertrap repository
  2. Ge the shader file
  3. Execute the Shadertrap command
bin/shadertrap shader14.shadertrap
  1. Collect the results, one file per buffer