Skip to content

Commit e43ee12

Browse files
committed
[ET-VK][ez] Retry with no optimization during shader compilation
## Context In some cases, a shader can be compiled successfully with `glslc`, but if an optimization flag such as `-O` or `-Os` is used, the compilation will fail with a message similar to ``` internal error: compilation succeeded but failed to optimize: Invalid use of 8- or 16-bit result\n %133 = OpExtInst %half %1 Pow %125 %132 ``` Which will cause the shader compilation script to fail. This diff introduces a check that if `glslc` complains about not being able to optimize the SPIR-V, then retry compilation without optimization arguments. Differential Revision: [D70906097](https://our.internmc.facebook.com/intern/diff/D70906097/) ghstack-source-id: 270796203 Pull Request resolved: #9096
1 parent 51901f3 commit e43ee12

File tree

1 file changed

+34
-22
lines changed

1 file changed

+34
-22
lines changed

backends/vulkan/runtime/gen_vulkan_spv.py

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,12 @@ def __init__(
549549

550550
self.env = env
551551
self.glslc_path = glslc_path
552-
self.glslc_flags = glslc_flags
552+
self.glslc_flags = glslc_flags.split()
553+
self.glslc_flags_no_opt = self.glslc_flags.copy()
554+
if "-O" in self.glslc_flags_no_opt:
555+
self.glslc_flags_no_opt.remove("-O")
556+
if "-Os" in self.glslc_flags_no_opt:
557+
self.glslc_flags_no_opt.remove("-Os")
553558
self.replace_u16vecn = replace_u16vecn
554559

555560
self.glsl_src_files: Dict[str, str] = {}
@@ -751,30 +756,37 @@ def process_shader(shader_paths_pair):
751756
if self.glslc_path is not None:
752757
spv_out_path = os.path.join(output_dir, f"{shader_name}.spv")
753758

754-
cmd = (
755-
[
756-
self.glslc_path,
757-
"-fshader-stage=compute",
758-
glsl_out_path,
759-
"-o",
760-
spv_out_path,
761-
"--target-env=vulkan1.1",
762-
"-Werror",
763-
]
764-
+ [
765-
arg
766-
for src_dir_path in self.src_dir_paths
767-
for arg in ["-I", src_dir_path]
768-
]
769-
+ self.glslc_flags.split()
770-
)
759+
cmd_base = [
760+
self.glslc_path,
761+
"-fshader-stage=compute",
762+
glsl_out_path,
763+
"-o",
764+
spv_out_path,
765+
"--target-env=vulkan1.1",
766+
"-Werror",
767+
] + [
768+
arg
769+
for src_dir_path in self.src_dir_paths
770+
for arg in ["-I", src_dir_path]
771+
]
772+
cmd = cmd_base + self.glslc_flags
771773

772774
try:
773-
subprocess.check_call(cmd)
775+
subprocess.run(cmd, check=True, capture_output=True, text=True)
774776
except subprocess.CalledProcessError as e:
775-
raise RuntimeError(
776-
f"Failed to compile {os.getcwd()}/{glsl_out_path}"
777-
) from e
777+
opt_fail = "compilation succeeded but failed to optimize"
778+
err_msg_base = f"Failed to compile {os.getcwd()}/{glsl_out_path}: "
779+
if opt_fail in e.stderr or opt_fail in e.stdout:
780+
cmd_no_opt = cmd_base + self.glslc_flags_no_opt
781+
try:
782+
subprocess.run(cmd_no_opt, check=True, capture_output=True)
783+
except subprocess.CalledProcessError as e_no_opt:
784+
raise RuntimeError(
785+
f"{err_msg_base} {e_no_opt.stderr}"
786+
) from e_no_opt
787+
788+
else:
789+
raise RuntimeError(f"{err_msg_base} {e.stderr}") from e
778790

779791
return (spv_out_path, glsl_out_path)
780792

0 commit comments

Comments
 (0)