Skip to content

Commit

Permalink
[vulkan] Reduce descriptor sets, use official headers, improve alloca…
Browse files Browse the repository at this point in the history
…tor, remove module destructor (#8452)

* Reduce resource usage for large pipelines by compiling one entry point per SPIR-V module.
Use an amalgamation for the binary module returned by CodeGen Vulkan Dev.
Decode amalgamation and cache each kernel module inside of cache entry.

* Create a new emitter from scratch inside the add_kernel() method.
Hook up dump() and dump_module() methods to debug options.

* Clang format pass

* Fix ambiguous conversion from path to std::string

* Use explicit string method rather than constructor for path conversion.

* Fix file path stem to string conversion.

* Re-enable performance wrap test for Vulkan.

* Trigger CI for testing

* Mark transfer buffers with SRC & DST usage bits to allow re-use without validation errors.

* Change interal_error to user_assert for un-implemented features.

* Add note to Vulkan.md describing validation layer usage and deb packages.

* Clang format

* Switch to halide_mutex for locking Vulkan context (to match other GPU runtimes)

* Trigger CI for testing

* Reduce memory pressure by reducing default minimum block size to 4KB (from 32MB!).
Ensure allocator.collect() is called when context is destroyed.

* Clang format pass

* Modify Vulkan allocation routine to fallback to any valid memory types after attempting preferred types.
Confusingly, multiple compatible memory types may exist in multiple heaps, so to prioritize the preferred types (and corresponding heaps) first.
Cleanup error handling to propagate errors.

* Re-enable Vulkan for entire GPU allocation test.

* Fix debug messages to iterate across dimensions

* Avoid calling Vulkan API methods in module destructor, since some drivers appear to register an atexit() handler which may get invoked before the module destructor is called, which can invalidate the pointer call chain and cause segfaults.

* Update object lifetime and leak tests to manually invoke the Vulkan device_release() to force cleanup of cached resources.

* Refactor compilation cache destruction to use C-ABI methods rather than a functor object on the stack (which seems to cause different behaviour between LLVM versions).

* Don't return an error if the instance pointer is invalid, since it indicates that either a block or a region has been free'd but hasn't been collected.

* Clang tidy pass

* Fix CodeGen for small signed integers (need to be sign extended and packed to 32 bits).
Add kernel names to code module header (both encode and decode).
Use kernel name in filename for HL_DUMP_SPIRV_FILE to aid debugging.

* Remove stale mini_vulkan.h and use official Vulkan headers (setup the same as SPIR-V).
Add script to easily update Vulkan Headers to another release branch.
Add local copy of Vulkan headers (ANSI-C interfaces only).
Refactor all vulkan runtime interfaces to match differences in headers.
Don't use a global VkCommandPool associated with the context since it doesn't have a stable API type definition.
Construct command pools on demand.

* Ignore dependencies/vulkan for clang format

* Use -Isystem path for Vulkan Headers

* Clang tidy cleanup pass

* Fix formatting

* Fix makefile build (add missing runtime include path for vulkan headers).

* Use spaces not tabs

* Use cmake's find_package to locate the Vulkan Headers, defaulting to our local copy

* Add "vulkan-headers" to vcpkg.json.  Update builtin-baseline to d567b667adba0e72c5c3931ddbe745b66aa34b73

* Split Vulkan API function pointers into three groups ... loader, instance and device.
Manage instance and device API function pointers with context ... not globally.
Always lookup device function pointers in destructor to validate the intefaces before calling.
Use ScopedVulkanCommandPoolAndBuffer for managing transient command pools and buffers to avoid leaking if errors occur within a scope.

* Clang format pass

* Remove manual destruction in object lifetime tests for Vulkan

* Remove trailing comment in array

* Remove leftover print

* Add optional custom dtor to JITModuleContents to allow JIT-only destructor for modules.
Hook up halide_vulkan_release_all() as a custom dtor for shared Vulkan runtime module.
Add halide_vulkan_release_all() to HalideRuntimeVulkan.h as a public exported method.

* Trigger build to test latest drivers

* Safeguard Vulkan destructor against being called if runtime wasn't initialized

* Formatting pass.

* Disable custom JIT destructor to see if segfaults still occur outside of shutdown/cleanup.

* Test module destructor with latest driver.
Disable other Halide_TARGETS to speed up testing.

* Formatting pass

* Only disable CUDA and OpenCL

* Revert CMakeLists ... disabling targets prematurely fails the buildbot job

* Add VK_EXT_DEBUG_UTILS_EXTENSION_NAME to optional instance extensions.
Register debug callbacks to try and diagnose potential driver issues.
Update src/runtime/HalideRuntimeVulkan.h with guarded typedefs to match Vulkan header.
Add VK_KHR_MAINTENANCE_5_EXTENSION_NAME to optional device extensions.

* Make clang tidy happy

* Format & Tidy

* Revert changes to JITModule which added a custom dtor. Use standard
module dtor like all other runtimes.

* Revert changes to HalideRuntimeVulkan that added a custom dtor.
Update comment in module destructor to match latest findings.  Added
issue link.
Cleanup Vulkan handle initializations ... use VK_NULL_HANDLE.

* Re-enable GPU object lifetime management tests and leak device tests for
Vulkan.

* Formatting

* trigger buildbots

---------

Co-authored-by: Derek Gerstmann <[email protected]>
Co-authored-by: Steven Johnson <[email protected]>
  • Loading branch information
3 people authored Dec 9, 2024
1 parent 8646bc8 commit 6b57a3b
Show file tree
Hide file tree
Showing 61 changed files with 26,146 additions and 6,663 deletions.
1 change: 1 addition & 0 deletions .clang-format-ignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
# mini_webgpu.h is copied from upstream with some local mods
./src/runtime/mini_webgpu.h
./dependencies/spirv
./dependencies/vulkan
*.fbs.h
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ Halide_feature(WITH_TESTS "Halide's unit test suite" TOP_LEVEL)
Halide_feature(WITH_TUTORIALS "Halide's tutorial code" TOP_LEVEL)
Halide_feature(WITH_UTILS "Optional utility programs for Halide, including HalideTraceViz" TOP_LEVEL)


##
# Add source directories

Expand Down
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ IN THE MATERIALS.

----

src/mini_vulkan.h is Copyright (c) 2014-2017 The Khronos Group Inc.
dependencies/vulkan is Copyright (c) 2014-2017 The Khronos Group Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ HEXAGON_LLVM_CONFIG_LIB=$(if $(WITH_HEXAGON), hexagon, )
SPIRV_CXX_FLAGS=$(if $(WITH_SPIRV), -DWITH_SPIRV -isystem $(ROOT_DIR)/dependencies/spirv/include, )
SPIRV_LLVM_CONFIG_LIB=$(if $(WITH_SPIRV), , )

VULKAN_CXX_FLAGS=$(if $(WITH_VULKAN), -DWITH_VULKAN, )
VULKAN_CXX_FLAGS=$(if $(WITH_VULKAN), -DWITH_VULKAN -isystem $(ROOT_DIR)/dependencies/vulkan/include, )
VULKAN_LLVM_CONFIG_LIB=$(if $(WITH_VULKAN), , )

WEBASSEMBLY_CXX_FLAGS=$(if $(WITH_WEBASSEMBLY), -DWITH_WEBASSEMBLY, )
Expand Down Expand Up @@ -1089,7 +1089,8 @@ RUNTIME_CXX_FLAGS = \
-Wno-unused-function \
-Wvla \
-Wsign-compare \
-Wno-sync-alignment
-Wno-sync-alignment \
-isystem $(ROOT_DIR)/dependencies/vulkan/include

$(BUILD_DIR)/initmod.windows_%_x86_32.ll: $(SRC_DIR)/runtime/windows_%_x86.cpp $(BUILD_DIR)/clang_ok
@mkdir -p $(@D)
Expand Down
16 changes: 16 additions & 0 deletions dependencies/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,19 @@ build it, and extract the necessary files. It takes a single argument, the
name of the branch to clone.

[SPIR-V]: https://www.khronos.org/registry/spir-v

## Vulkan

This folder contains a copy of the officially released v1.3.296 SDK headers
for [Vulkan], obtained from the `vulkan-sdk-v1.3.296` branch
of https://github.com/KhronosGroup/Vulkan-Headers.

The directory structure within this folder matches that of the official
version's install tree, plus the upstream `LICENSE.md` notice, minus files
that Halide doesn't need.

The `update-vulkan.sh` script will automatically acquire the upstream repo,
build it, and extract the necessary files. It takes a single argument, the
name of the branch to clone.

[Vulkan]: https://www.vulkan.org/
41 changes: 41 additions & 0 deletions dependencies/update-vulkan.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash

set -eo pipefail

cd -- "$(dirname -- "$0")" || exit 1

GIT_BRANCH="$1"
if [ -z "$GIT_BRANCH" ]; then
echo "error: usage: $0 <git-branch>"
echo "remark: the current <git-branch> is vulkan-sdk-1.3.296"
exit 1
fi

mkdir -p vulkan

cleanup () {
rm -rf Vulkan-Headers
}

trap cleanup SIGINT SIGTERM EXIT

git clone https://github.com/KhronosGroup/Vulkan-Headers.git --branch "$GIT_BRANCH"
cmake -S Vulkan-Headers -B Vulkan-Headers/build -DCMAKE_BUILD_TYPE=Release "-DCMAKE_INSTALL_PREFIX=$PWD/Vulkan-Headers/_local"
cmake --build Vulkan-Headers/build --target install

# Copy license information
cp Vulkan-Headers/LICENSE.md vulkan/

# Copy headers we care about (only the ANSI-C core interfaces!)
mkdir -p vulkan/include/vulkan
cp Vulkan-Headers/_local/include/vulkan/*.h vulkan/include/vulkan/
mkdir -p vulkan/include/vk_video
cp Vulkan-Headers/_local/include/vk_video/*.h vulkan/include/vk_video/

# Copy CMake config
mkdir -p vulkan/share/
cp -R Vulkan-Headers/_local/share/cmake vulkan/share/

git add -f vulkan/

echo "Updated Vulkan-Headers to branch $GIT_BRANCH!"
18 changes: 18 additions & 0 deletions dependencies/vulkan/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Copyright 2015-2023 The Khronos Group Inc.

Files in this repository fall under one of these licenses:

- `Apache-2.0`
- `MIT`

Note: With the exception of `parse_dependency.py` the files using `MIT` license
also fall under `Apache-2.0`. Example:

```
SPDX-License-Identifier: Apache-2.0 OR MIT
```

Full license text of these licenses is available at:

* Apache-2.0: https://opensource.org/licenses/Apache-2.0
* MIT: https://opensource.org/licenses/MIT
Loading

0 comments on commit 6b57a3b

Please sign in to comment.