m4rs-mt / ILGPU

ILGPU JIT Compiler for high-performance .Net GPU programs
http://www.ilgpu.net
Other
1.38k stars 115 forks source link

DirectX Compute Shader (DirectCompute) backend support #806

Closed hez2010 closed 2 years ago

hez2010 commented 2 years ago

I would like to see a DirectX Compute Shader (aka. DirectCompute) backend in ILGPU. The DirectCompute itself is Windows only but it's cross-vendor, and it can be expanded to other platforms using dxc.

Generally, ILGPU can compile IL to HLSL, and use dxc to compile it to DXIL (for DirectX) and SPIR-V (for Vulkan). Another approach is to compile IL to DXIL (for DirectX), which is a variant of LLVM IR, and it can be converted to SPRI-V (for Vulkan).

Also note that HLSL (for both DXIL and SPIR-V target) will be integrated into LLVM.

For reference, see https://docs.microsoft.com/en-us/windows/win32/direct3d11/direct3d-11-advanced-stages-compute-shader and https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-part1

m4rs-mt commented 2 years ago

@hez2010 Thanks a lot for this feature request. We are currently thinking of integrating support for SPIR-V in the scope of the 2.0 release. We could definitely come up with a backend that emits DXIL to convert it into SPIR-V. However, Direct Compute support is not currently on our roadmap, as enterprise GPGPU developers are currently mainly using NVIDIA/AMD GPUs in their data centers (running Linux) anyway which can be targeted via the currently available backends. In particular, we are currently targeting NVIDIA GPUs through our own specialized and highly tweaked PTX backend.

Nevertheless, we are always open to suggestions and/or contributions to make this happen in a future version 🚀. @MoFtZ @Yey007 @kilngod @NullandKale What do you think about this? I think we should go for the SPIR-V backend first (if at all) to target more platforms and vendors + we could also target the Intel-specific OneAPI stuff and benefit from the updated OpenCL SPRI-V APIs, which could make the existing OpenCL backend obsolete 🤞

MoFtZ commented 2 years ago

My thoughts are to focus resources on the SPIR-V or Metal backends first. SPIR-V is a candidate because it helps to cover Linux (and indirectly, Macs) with something better than OpenCL. The Metal backend I think will have the biggest interest and potential, although it has a very specific target.

The oneAPI Level Zero specifications looks very much like OpenCL, so it may be the fastest option to support, by repurposing our existing OpenCL backend. But it would not appear to be a priority, other than to tick a box.

@hez2010 could you please highlight the benefits of supporting DirectCompute? It would allow support for AMD and Intel GPUs on Windows - this is currently covered by the OpenCL backend, and would be covered by the proposed SPIR-V backend.

Are there other benefits, like better integration with DirectX for non-compute scenarios?

Another thing to consider, @hez2010 would you have the time and ability to help build this DirectCompute backend?

hez2010 commented 2 years ago

could you please highlight the benefits of supporting DirectCompute?

It is necessary because WSL2 GPU compute is using DirectX as backend and does not support Vulkan compute. Many users are using WSL2 as their development environment.

It would allow support for AMD and Intel GPUs on Windows

DirectCompute covers all vendors (Intel, AMD and NVIDIA) on Windows.

MoFtZ commented 2 years ago

My understanding is that WSL2 already supports CUDA, and they have plans to add OpenCL, and maybe Vulkan, in the future.

I'm not sure that adding WSL2 support would be a priority, but I guess it would be an added benefit of adding DirectX support.

@m4rs-mt Any feedback from your side? This is the first time I've heard someone in the community asking for WSL2 support.

hez2010 commented 2 years ago

they have plans to add OpenCL, and maybe Vulkan, in the future

I'm afraid their approach would be something like translation layer going through dxgkrnl, like what WSA has today, if so it would have major performance degradation because of the cost of translation (from Vulkan to DirectX).

m4rs-mt commented 2 years ago

@hez2010 thank you for raising this issue. We have been discussing internally about adding dedicated support for DirectX compute shaders and have concluded that this is not currently on our feature list. However, we are definitely open (and more than happy) to accept contributions! As planned, we will be working on an improved CPU runtime with SMID instructions and a new SPIR-V Vulkan backend to support additional platforms.