Skip to content

Commit

Permalink
Merging simplified HAL bindings branch. (#18366)
Browse files Browse the repository at this point in the history
This removes the concept of descriptor sets and pipeline layouts from
the HAL and switches programs to using a flat list of bindings per
dispatch as real programs rarely benefited and will do so less as
command buffer reuse is enabled. Command buffer recording is now
stateless as the information in push constants and push descriptor set
commands are carried per dispatch and there's no need to track pipeline
layouts. Pipeline layouts are still present in a reduced form in the
compiler IR in order to handle dispatch ABI in a normalized way but it's
up to the TargetBackends to encode them. Encoded metadata for pipeline
layouts is now embedded in the target-specific executables for targets
that require them (Metal/Vulkan/WebGPU/D3D12) and a reduced set of
information is embedded for others. This simplifies the HAL API quite a
bit, makes implementing the HAL easier as targets have more freedom in
how constants and bindings are mapped to lower-level implementations,
and in practice improves command buffer recording latency as there are
fewer VM calls per dispatch on average.

Since the flatbuffers needed to change to include the new metadata that
previously was handled via the HAL APIs this branch also modernizes and
normalizes the flatbuffers across targets to both better match the
implementation and support features like multiple shader modules/kernel
libraries/etc per HAL executable (even if the compiler isn't linking
them yet). This reorganization is required to effectively manage cached
resources - before the compiler would deduplicate pipeline layouts
across all executables but now that each executable is responsible for
that having 1000 executables means that there will be 1000 pipeline
layouts even if most are the same. Perhaps this will serve as good
motivation to finally finish linking in all backends :) Debug info is
also consistently added for all targets and processed for tracing and
factored such that new debug info can be added per-exported function
without needing to change per-target code.

There are many IR changes here and many test updates: most of the tests
that were updated are in codegen and should not be using HAL ops at all.
As codegen test cleanup continues to switch from HAL ops to basic
functions future changes to the HAL IR will be easier. Notable changes
include:
* Renamed `push_constants` to `constants` (as there is no longer a
`push_constants` API)
* Dropped `#hal.descriptor_set.layout`
* Removed ordinal from `#hal.descriptor_set.binding` (as ordinals are
now implicit)
* Renamed `#hal.descriptor_set.binding` to `#hal.pipeline.binding`
* Removed `set` from `hal.interface.binding.subspan`
* Removed `#hal.interface.binding` and the spooky action at a distance
`hal.interface.binding` attr now that ordinals are implicit

Metal/CUDA/Vulkan/HIP/CPU have all been updated to the new binding
model. WebGPU has had some changes applied but needs some significant
specialized work due to its existing push constant emulation requiring
compiler-side descriptor sets. That's left for future work when that
experimental backend is revived.

This bumps the HAL version to 5 (types removed and methods changed) and
the CPU executable library version to 5 (added reserved per-export
fields for future use).

Fixes #18154.
  • Loading branch information
benvanik authored Aug 27, 2024
2 parents 7566680 + 1acbedc commit c77a5f5
Show file tree
Hide file tree
Showing 466 changed files with 11,813 additions and 19,115 deletions.
6 changes: 4 additions & 2 deletions build_tools/bazel/iree_flatcc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ def iree_flatbuffer_c_library(
name,
srcs,
flatcc_args = ["--common", "--reader"],
includes = [],
testonly = False,
**kwargs):
flatcc = "@com_github_dvidelabs_flatcc//:flatcc"

flags = [
"-o$(RULEDIR)",
"-I runtime/src",
] + flatcc_args

out_stem = "%s" % (srcs[0].replace(".fbs", ""))
Expand All @@ -34,10 +36,10 @@ def iree_flatbuffer_c_library(

native.genrule(
name = name + "_gen",
srcs = srcs,
srcs = srcs + includes,
outs = outs,
tools = [flatcc],
cmd = "$(location %s) %s $(SRCS)" % (flatcc, " ".join(flags)),
cmd = "$(location %s) %s %s" % (flatcc, " ".join(flags), " ".join(["$(location {})".format(src) for src in srcs])),
testonly = testonly,
)
native.cc_library(
Expand Down
4 changes: 3 additions & 1 deletion build_tools/bazel_to_cmake/bazel_to_cmake_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,16 +662,18 @@ def iree_bytecode_module(
f" PUBLIC\n)\n\n"
)

def iree_flatbuffer_c_library(self, name, srcs, flatcc_args=None):
def iree_flatbuffer_c_library(self, name, srcs, flatcc_args=None, includes=None):
name_block = self._convert_string_arg_block("NAME", name, quote=False)
srcs_block = self._convert_srcs_block(srcs)
flatcc_args_block = self._convert_string_list_block("FLATCC_ARGS", flatcc_args)
includes_block = self._convert_srcs_block(includes, block_name="INCLUDES")

self._converter.body += (
f"flatbuffer_c_library(\n"
f"{name_block}"
f"{srcs_block}"
f"{flatcc_args_block}"
f"{includes_block}"
f" PUBLIC\n)\n\n"
)

Expand Down
3 changes: 2 additions & 1 deletion build_tools/cmake/flatbuffer_c_library.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function(flatbuffer_c_library)
cmake_parse_arguments(_RULE
"PUBLIC;TESTONLY"
"NAME"
"SRCS;FLATCC_ARGS"
"SRCS;FLATCC_ARGS;INCLUDES"
${ARGN}
)

Expand Down Expand Up @@ -94,6 +94,7 @@ function(flatbuffer_c_library)
iree-flatcc-cli
-o "${CMAKE_CURRENT_BINARY_DIR}"
-I "${IREE_ROOT_DIR}"
-I "${IREE_ROOT_DIR}/runtime/src"
${_RULE_FLATCC_ARGS}
"${_RULE_SRCS}"
WORKING_DIRECTORY
Expand Down
2 changes: 2 additions & 0 deletions compiler/plugins/target/CUDA/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ iree_compiler_cc_library(
"//compiler/src/iree/compiler/Codegen/LLVMGPU",
"//compiler/src/iree/compiler/Codegen/Utils",
"//compiler/src/iree/compiler/Dialect/HAL/Target",
"//compiler/src/iree/compiler/Dialect/HAL/Utils:ExecutableDebugInfoUtils",
"//compiler/src/iree/compiler/Dialect/HAL/Utils:LLVMLinkerUtils",
"//compiler/src/iree/compiler/PluginAPI",
"//compiler/src/iree/compiler/Utils",
"//runtime/src/iree/base/internal/flatcc:building",
"//runtime/src/iree/schemas:cuda_executable_def_c_fbs",
"//runtime/src/iree/schemas:executable_debug_info_c_fbs",
"@iree_cuda//:libdevice_embedded",
"@llvm-project//llvm:Analysis",
"@llvm-project//llvm:BitReader",
Expand Down
2 changes: 2 additions & 0 deletions compiler/plugins/target/CUDA/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ iree_cc_library(
iree::compiler::Codegen::LLVMGPU
iree::compiler::Codegen::Utils
iree::compiler::Dialect::HAL::Target
iree::compiler::Dialect::HAL::Utils::ExecutableDebugInfoUtils
iree::compiler::Dialect::HAL::Utils::LLVMLinkerUtils
iree::compiler::PluginAPI
iree::compiler::Utils
iree::schemas::cuda_executable_def_c_fbs
iree::schemas::executable_debug_info_c_fbs
iree_cuda::libdevice_embedded
PUBLIC
)
Expand Down
Loading

0 comments on commit c77a5f5

Please sign in to comment.