Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backend] DirectX (D3D12, DXIL) Backend #5276

Open
python3kgae opened this issue Jun 28, 2022 · 5 comments
Open

[Backend] DirectX (D3D12, DXIL) Backend #5276

python3kgae opened this issue Jun 28, 2022 · 5 comments
Labels
feature request Suggest an idea on this project

Comments

@python3kgae
Copy link
Collaborator

Concisely describe the proposed feature
I would like to add a DirectX 12 backend to the compiler so that I can use new features which requires shader model >= 6.0.

Describe the solution you'd like (if any)
The plan is in 3 steps.

  1. Add llvm 15 support which is required for generate DXIL.
    Code change will be guarded with something like "#ifdef TI_LLVM_15" if llvm 15 require different implementation.
    CMake option TI_LLVM_15 will be added to control it and OFF by default.

  2. Add AOT compile first which could be tested by add c++ directx12 test without backend.
    DXIL support in llvm 15 is still in early state. It will be a long journey to support all features.
    CodeGenLLVM will be used as base class for llvm ir generation when codegen. Then some llvm passes will be added to prepare for DXIL generation and finally use DirectX backend in llvm 15 to generate DXIL.
    A kernel which has more than 1 tasks is a new thing for DXIL, no plan to support this feature at first.

  3. Add DirectX 12 backend.
    No an expert on this part :(
    Maybe start support for simple cases first.

Additional comments
I'm working on DXIL generation on llvm 15. Targeting language other then HLSL to DXIL will help the design of DXIL generation.
This is personal work, not official contribution (which requires examine and approve).

@python3kgae python3kgae added the feature request Suggest an idea on this project label Jun 28, 2022
@k-ye
Copy link
Member

k-ye commented Jun 28, 2022

Hi @python3kgae ,

Thanks for proposing this, it looks really attractive!

Add llvm 15 support which is required for generate DXIL.

Note that we are (conservatively) planning to upgrade to LLVM-12. It's probably fine for us to directly go to 15 as well. One thing we've recently noticed, is the installation speed from apt-get for clang-13 (and likely beyond) is super slow. This could potentially hurt our external contributors...

(Ive followed https://apt.llvm.org/ for clang-13 installation, not sure if you have better suggestions here.)

Then some llvm passes will be added to prepare for DXIL generation and finally use DirectX backend in llvm 15 to generate DXIL.

I see. I'm not sure how feasible it is to share the generated LLVM IR between CUDA and DXIL. Hopefully this is not a problem :-) (Taking SPIR-V as an example. Although it claims to support both Vulkan and OpenCL, they use very different Execution Model, making it nontrivial to share the same SPIR-V between these two APIs).

No an expert on this part :( Maybe start support for simple cases first.

If we can have a working codegen, D3D12 runtime API shouldn't be too hard to sort out :-)

@python3kgae
Copy link
Collaborator Author

Note that we are (conservatively) planning to upgrade to LLVM-12. It's probably fine for us to directly go to 15 as well. One thing we've recently noticed, is the installation speed from apt-get for clang-13 (and likely beyond) is super slow. This could potentially hurt our external contributors...

(Ive followed https://apt.llvm.org/ for clang-13 installation, not sure if you have better suggestions here.)

If clang is only used for COMPILE_LLVM_RUNTIME, We can keep using clang-10 first. llvm 15 should be OK to link bitcode generated with llvm 10.
And once apt.llvm.org got the issue fixed, we can update clang to higher version.

@ailzhang
Copy link
Contributor

ailzhang commented Jul 1, 2022

@python3kgae COMPILE_LLVM_RUNTIME isn't the only place that requires clang version 10, I'm afraid that https://github.com/taichi-dev/taichi/blob/master/cmake/TaichiCore.cmake#L253-L288 also requires headers from llvm-10, so existing codebase might need some effort to make it work with llvm-15.
But we'd more than happy to provide guidance if you're interested in hacking a prototype for this, how about joining our slack workspace so that we can discuss how a MVP looks like? https://taichicommunity.slack.com/join/shared_invite/zt-14ic8j6no-Fd~wKNpfskXLfqDr58Tddg#/shared-invite/email Thanks!

@python3kgae
Copy link
Collaborator Author

@ailzhang I checked https://github.com/taichi-dev/taichi/blob/master/cmake/TaichiCore.cmake#L253-L288, it is where llvm header and lib are added. it only require llvm version >= 10, llvm 15 doesn't require any change for this part.

I've got taichi repo compiled with llvm 15. Most of the changes are related to the opaque pointer feature which require add type when create LoadInst/GEP. Currently I'm hacking to get the type from llvm type, need help to get the type from taichi type instead of llvm type.

@qiao-bo
Copy link
Collaborator

qiao-bo commented Jul 4, 2022

One thing we've recently noticed, is the installation speed from apt-get for clang-13 (and likely beyond) is super slow. This could potentially hurt our external contributors...
(Ive followed https://apt.llvm.org/ for clang-13 installation, not sure if you have better suggestions here.)

This could be specific to the nightly package. I tried the release version on a Ubuntu 22.04. The time of apt-get install clang-13 seems ok. But indeed current 20.04 users will have to suffer... Long-term speaking should not be a blocker.

@ailzhang ailzhang removed their assignment Jul 4, 2022
ailzhang pushed a commit that referenced this issue Sep 15, 2022
…5998)

2 passes are added for DXIL generation.

TaichiIntrinsicLower will translate taichi intrinsic like thread_idx
into the form DirectX backend expected.

TaichiRuntimeContextLower will translate the TaichiRuntimeContext
parameter for kernel into Buffers/ConstantBuffers.
TaichiRuntimeContextLower is empty now.
It is added after inline so optimizations reduce the load/store on temp
ptr. And it is easier to know a store is on the TaichiRuntimeContext.

Related issue = #5276

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
ailzhang pushed a commit that referenced this issue Sep 15, 2022
Fix build fail and enable dx12 build for windows cpu ci to make sure it
compiles.

Related issue = #5276
ailzhang pushed a commit that referenced this issue Sep 19, 2022
Build based on release/15.x branch and cherry-picked some patch for
DX12.

Issue: #5276 

### Brief Summary
ailzhang pushed a commit that referenced this issue Sep 19, 2022
Copied from cuda codegen.

Issue: #5276

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
python3kgae added a commit that referenced this issue Sep 27, 2022
Only make sure the pipeline generate something.
No real dxil generated yet.

Move DX12 build to gpu ci which will run the aot test.

Issue: #5276

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
ailzhang pushed a commit that referenced this issue Oct 10, 2022
add DirectX-Headers as a submodule.

Issue: #5276
ailzhang pushed a commit that referenced this issue Oct 18, 2022
Add Dx12ProgramImpl.
Prepare change to launch dx12 kernel like dx11 instead of
LlvmRuntimeExecutor.
Because dx12 uses buffer instead of pointer directly.

Issue: #5276

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Suggest an idea on this project
Projects
Status: In Progress
Development

No branches or pull requests

4 participants