google / shaderc

A collection of tools, libraries, and tests for Vulkan shader compilation.
Other
1.86k stars 363 forks source link

Compiler Can Emit Multiple Precision Decorations Causing Optimization to Fail With Internal Error #1438

Open benn-geomagical opened 4 months ago

benn-geomagical commented 4 months ago

Vulkan SDK 1.3.290,0 shaderc v2023.8 v2024.1-9-g3ac03b8 spirv-tools v2024.3 v2022.4-513-g0cfe9e72 glslang 11.1.0-1009-gfa9c3deb Windows 11 / Intel x86_64

Issue first occurred using Vulkan SDK 1.3.283.0

When constants are defined after precision statements that are not highp the compiler will emit multiple precision decorations causing optimization to fail with an internal error.

shaderc: internal error: compilation succeeded but failed to optimize: ID '22' decorated with RelaxedPrecision multiple times is not allowed.
  %float_0 = OpConstant %float 0

This script repros the issue.

#!/usr/bin/env bash

cat << EOF > repro.vert
#version 450

#if !defined(SHOW_CASE)
# define SHOW_CASE 0
#endif

#if SHOW_CASE == 0
  // error case
  precision mediump float;
  precision mediump int;
#elif SHOW_CASE == 1
  // workaround 1
  precision highp float;
  precision highp int;
#endif

const float kfVal = 0;
const float kfValCopy = kfVal;
const uint kuVal = 0;
const uint kuValCopy = kuVal;

#if SHOW_CASE == 2
  // workaround 2
  precision mediump float;
  precision mediump int;
#endif

in vec4 aPos;

void main() {
  gl_Position = aPos;
}
EOF

function compile() {
  glslc \
    --target-env=vulkan1.1 \
    -fauto-map-locations \
    -g -O \
    -o repro.vert.spv \
    -DSHOW_CASE=${1} \
    repro.vert
}

echo -n "Compiles without error..."
compile 1
echo "done."

echo -n "Compiles without error..."
compile 2
echo "done."

echo "Compils with error:"
compile 0