Skip to content

Commit

Permalink
Enable SPIR-V compiler optimization (#4402)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #4402

Call the SPIR-V compiler with the `-O` flag, which enables optimizations when compiling GLSL to SPIR-V. The `-Os` flag (which tries to minimize SPIR-V size) was tested as well, but resulted in (very) slightly worse performance.

Reviewed By: jorgep31415

Differential Revision: D60193514

fbshipit-source-id: 2dfb999fb1951a63a990773ab563a1a3a3c304b0
  • Loading branch information
SS-JIA authored and facebook-github-bot committed Jul 25, 2024
1 parent 77c905d commit 889e5cb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 25 deletions.
44 changes: 30 additions & 14 deletions backends/vulkan/runtime/gen_vulkan_spv.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ def __init__(
src_dir_paths: Union[str, List[str]],
env: Dict[Any, Any],
glslc_path: Optional[str],
glslc_flags: str = "",
) -> None:
if isinstance(src_dir_paths, str):
self.src_dir_paths = [src_dir_paths]
Expand All @@ -490,6 +491,7 @@ def __init__(

self.env = env
self.glslc_path = glslc_path
self.glslc_flags = glslc_flags

self.glsl_src_files: Dict[str, str] = {}
self.template_yaml_files: List[str] = []
Expand Down Expand Up @@ -668,19 +670,23 @@ def process_shader(shader_paths_pair):
if self.glslc_path is not None:
spv_out_path = os.path.join(output_dir, f"{shader_name}.spv")

cmd = [
self.glslc_path,
"-fshader-stage=compute",
glsl_out_path,
"-o",
spv_out_path,
"--target-env=vulkan1.1",
"-Werror",
] + [
arg
for src_dir_path in self.src_dir_paths
for arg in ["-I", src_dir_path]
]
cmd = (
[
self.glslc_path,
"-fshader-stage=compute",
glsl_out_path,
"-o",
spv_out_path,
"--target-env=vulkan1.1",
"-Werror",
]
+ [
arg
for src_dir_path in self.src_dir_paths
for arg in ["-I", src_dir_path]
]
+ self.glslc_flags.split()
)

subprocess.check_call(cmd)

Expand Down Expand Up @@ -966,6 +972,8 @@ def main(argv: List[str]) -> int:
parser.add_argument("-c", "--glslc-path", required=True, help="")
parser.add_argument("-t", "--tmp-dir-path", required=True, help="/tmp")
parser.add_argument("-o", "--output-path", required=True, help="")
parser.add_argument("--optimize_size", action="store_true", help="")
parser.add_argument("--optimize", action="store_true", help="")
parser.add_argument(
"--env", metavar="KEY=VALUE", nargs="*", help="Set a number of key-value pairs"
)
Expand All @@ -984,7 +992,15 @@ def main(argv: List[str]) -> int:
if not os.path.exists(options.tmp_dir_path):
os.makedirs(options.tmp_dir_path)

shader_generator = SPVGenerator(options.glsl_paths, env, options.glslc_path)
glslc_flags = ""
if options.optimize_size:
glslc_flags += "-Os"
elif options.optimize:
glslc_flags += "-O"

shader_generator = SPVGenerator(
options.glsl_paths, env, options.glslc_path, glslc_flags
)
output_spv_files = shader_generator.generateSPV(options.tmp_dir_path)

genCppFiles(
Expand Down
29 changes: 18 additions & 11 deletions backends/vulkan/targets.bzl
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
load("@fbcode_macros//build_defs:native_rules.bzl", "buck_genrule")
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")

def get_vulkan_compiler_flags():
return ["-Wno-missing-prototypes", "-Wno-global-constructors"]

def vulkan_spv_shader_lib(name, spv_filegroups, is_fbcode = False):
gen_vulkan_spv_target = "//executorch/backends/vulkan:gen_vulkan_spv_bin"
glslc_path = "//caffe2/fb/vulkan/dotslash:glslc"
gen_vulkan_spv_target = "//xplat/executorch/backends/vulkan:gen_vulkan_spv_bin"
glslc_path = "//xplat/caffe2/fb/vulkan/dotslash:glslc"

if is_fbcode:
gen_vulkan_spv_target = "//executorch/backends/vulkan:gen_vulkan_spv_bin"
glslc_path = "//caffe2/fb/vulkan/tools:glslc"

glsl_paths = []
Expand All @@ -15,21 +18,25 @@ def vulkan_spv_shader_lib(name, spv_filegroups, is_fbcode = False):
for target, subpath in spv_filegroups.items():
glsl_paths.append("$(location {})/{}".format(target, subpath))

genrule_cmd = [
"$(exe {})".format(gen_vulkan_spv_target),
"--glsl-paths {}".format(" ".join(glsl_paths)),
"--output-path $OUT",
"--glslc-path=$(exe {})".format(glslc_path),
"--tmp-dir-path=$OUT",
]
genrule_cmd = (
"$(exe {}) ".format(gen_vulkan_spv_target) +
"--glsl-paths {} ".format(" ".join(glsl_paths)) +
"--output-path $OUT " +
"--glslc-path=$(exe {}) ".format(glslc_path) +
"--tmp-dir-path=$OUT " +
select({
"DEFAULT": "",
"ovr_config//os:android": "--optimize",
})
)

genrule_name = "gen_{}_cpp".format(name)
runtime.genrule(
buck_genrule(
name = genrule_name,
outs = {
"{}.cpp".format(name): ["spv.cpp"],
},
cmd = " ".join(genrule_cmd),
cmd = genrule_cmd,
default_outs = ["."],
labels = ["uses_dotslash"],
)
Expand Down

0 comments on commit 889e5cb

Please sign in to comment.