Hello all,
The HLSL compiler community is interested in contributing HLSL, DirectX and Vulkan graphics support to Clang and LLVM.
Why do we want to do this?
The existing HLSL compiler, the DirectX Shader Compiler (DXC), is a fork of LLVM/Clang 3.7 and is developed open source on GitHub (https://github.com/microsoft/DirectXShaderCompiler) by Microsoft and a diverse community of open source partners. We plan to update our compiler to the latest LLVM. This plan is motivated by our efforts to bring new C++ language features and tooling improvements to HLSL.
While we could do this in our own fork, we believe that integrating our compiler and community with the LLVM community will allow us to expand both communities, and to deliver a better compiler for our users.
What is HLSL?
High Level Shading Language (HLSL) was introduced as part of DirectX 9 for programming the (then new) programmable parts of the GPU rendering pipeline. The language began as a C-like language and has developed over time to include more and more C++ features. The latest version of HLSL, HLSL 2021, has added C+±style templates, limited operator overloading, and bitfields (Announcing HLSL 2021 - DirectX Developer Blog).
How is HLSL used?
HLSL is widely used today for graphics and general purpose GPU programming. The DirectX runtimes on Windows support HLSL either as source or pre-compiled to an intermediate representation. Additionally DXC can be used to generate SPIR-V which can then be used either with the Vulkan runtime or, through SPIR-V cross, converted to GLSL or Metal Shader Language for use with their respective APIs. HLSL is also largely source compatible with several other shading languages, and carefully written C/C++, enabling complex shaders to be constructed from code that can be written in a variety of languages.
What CodeGen targets do we support?
There are three primary code generation targets that we are interested in supporting. Our first priority is supporting the code generation targets that DXC supports today: DirectX Intermediate Language (DXIL) and SPIR-V. In the future we may additionally support DirectX Bytecode (DXBC), which is the virtual ISA supported by DirectX versions 9-11.
DXIL, the intermediate representation used for DirectX 12, is based on LLVM-3.7 IR encoded as bitcode. For many reasons that have been discussed at length within the LLVM community in the past, this is not great, but it has already shipped and is the driver interface for all DirectX 12 GPU drivers.
There have been many discussions of SPIR-V in the LLVM community. SPIR-V is a virtual ISA used for GPU programming. Since SPIR-V code generation is widely used by developers writing HLSL, this is a critically important feature for us too. There are several possible ways we could support SPIR-V, and we are looking forward to actively engaging with the community to solve this problem.
How are we proposing to do this?
For several reasons, primarily the age of our current LLVM fork, we are not proposing merging our existing compiler into modern LLVM, but rather re-implementing our compiler’s functionality in LLVM/main piece by piece.
Broadly speaking this would mean adding HLSL-specific language options to the Clang frontend as well as DirectX target support both to Clang and LLVM. The Clang DirectX target implementation would resemble the CUDA or OpenCL targets, and in LLVM we would add a DirectX target to contain our codegen passes and emit DXIL. By isolating as much of the DXIL-specific code as possible into a target we hope to minimize the cost on the community to maintain our legacy bitcode writing support.
We intend to take a different approach to implementing HLSL support in Clang from what is present in our current compiler, so while we can use the current implementation as a reference for language features and a source for test files, we will not use it as a model for implementation details.
Assuming that this proposal is acceptable the first few patches are ready to start being posted for review immediately. As we move forward, we’d like to start an HLSL working group which will have regular meetings to discuss and track progress and coordinate efforts across contributors. Microsoft is making a commitment to bring Clang up to feature parity with DXC, but the HLSL community is supporting this effort and we expect to fully shift development over to LLVM/main after HLSL support becomes feature complete.