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

Introduce support of the LLVM SPIR-V Backend to translate LLVM IR to SPIR-V #3229

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

VyacheslavLevytskyy
Copy link

@VyacheslavLevytskyy VyacheslavLevytskyy commented Jan 22, 2025

This PR is to introduce support of the LLVM SPIR-V Backend to translate LLVM IR to SPIR-V. The backend will be preferred over Khronos Translator only when both conditions are satisfied:

  • the SPIR-V Backend is present in the deployment of LLVM,
  • there is an environmental variable TRITON_USE_SPIRV_BACKEND present and set to a non-zero integer value.

Khronos Translator remains the default tool to translate LLVM IR to SPIR-V.

@etiotto
Copy link
Contributor

etiotto commented Jan 27, 2025

Would be interesting to see what is the functional pass rate after switching to use the official LLVM SPIRV BE.

@VyacheslavLevytskyy
Copy link
Author

Would be interesting to see what is the functional pass rate after switching to use the official LLVM SPIRV BE.

Just a point to remember that we will not see actual pass rates in the CI -- active development means that CI results are always outdated.

@VyacheslavLevytskyy VyacheslavLevytskyy marked this pull request as ready for review January 27, 2025 14:49
scripts/compile-triton.sh Outdated Show resolved Hide resolved
add_mlir_translation_library(TritonSPIRV
SPIRVTranslation.cpp
# Check if there is the LLVM SPIR-V backend.
is_llvm_target_library("SPIRV" spirv_present_result INCLUDED_TARGETS)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When would SPIRV backend not exists after SPIRV backend moved out of experimental?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% until I check, but I'd expect that, for example, LLVM_TARGETS_TO_BUILD allows to narrow a list of targets and exclude any existing target from actually installed.

@VyacheslavLevytskyy VyacheslavLevytskyy force-pushed the introduce-llvm-spirv-backend branch from 57c6e8f to 51425ea Compare January 29, 2025 14:19
static const std::string DefaultTriple = "spirv64v1.6-unknown-unknown";
static const std::vector<std::string> Opts{
"--avoid-spirv-capabilities", "Shader", "--translator-compatibility-mode",
"--spirv-ext=all", "- O3"};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"--spirv-ext=all", "- O3"};
"--spirv-ext=all", "-O3"};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A fun fact is that "- O3" is a legit option to pass in a legacy SPIR-V Backend API call. Please see a comment in lines 22-31 of third_party/intel/lib/Target/SPIRV/SPIRVTranslation.cpp for a wider perspective of how we are going to switch to a newer API call when it's available in openai llvm build.

In brief, this "- O3" will disappear in favor of explicit CodeGenOptLevel::Aggressive. For now, though, llvm has -O reserved for itself, so SPIR-V Backend API engaged a slightly weird spelling of - O to be able to pass opt level into the API call in a style of command line arguments. Appreciate your attention to these details in the review!

third_party/intel/lib/Target/SPIRV/SPIRVTranslation.cpp Outdated Show resolved Hide resolved
@whitneywhtsang
Copy link
Contributor

FYI @VyacheslavLevytskyy, LLVM commit is updated in #3316, it now contains llvm/llvm-project@cda81b1.

@VyacheslavLevytskyy VyacheslavLevytskyy force-pushed the introduce-llvm-spirv-backend branch from b529394 to f554c4f Compare January 31, 2025 13:04
@VyacheslavLevytskyy
Copy link
Author

FYI @VyacheslavLevytskyy, LLVM commit is updated in #3316, it now contains llvm/llvm-project@cda81b1.

Thank you @whitneywhtsang , this simplifies the patch even further, updated.

@VyacheslavLevytskyy
Copy link
Author

VyacheslavLevytskyy commented Jan 31, 2025

@whitneywhtsang Looks like pre-built LLVM used by failed Github Actions doesn't contain SPIRV as a target.

In scripts/compile-triton.sh we are able to explicitly list targets via -DLLVM_TARGETS_TO_BUILD="X86;NVPTX;AMDGPU;SPIRV". What should be updated to avoid the following complaint?

  CMake Error at /home/runner/.triton/llvm/llvm-c1188642-ubuntu-x64/lib/cmake/llvm/LLVM-Config.cmake:271 (message):
    Library 'SPIRVCodeGen' is a direct reference to a target library for an
    omitted target.

@pbchekin Can you please suggest what else should be updated for the prebuilt /home/runner/.triton/llvm/ to be configured according to -DLLVM_TARGETS_TO_BUILD="X86;NVPTX;AMDGPU;SPIRV" ?

@pbchekin
Copy link
Contributor

@pbchekin Can you please suggest what else should be updated for the prebuilt /home/runner/.triton/llvm/ to be configured according to -DLLVM_TARGETS_TO_BUILD="X86;NVPTX;AMDGPU;SPIRV" ?

We currently use LLVM prebuilt by OpenAI (https://github.com/triton-lang/triton/blob/main/.github/workflows/llvm-build.yml), not sure if they are ready to add SPIRV target right now to their build workflow. So we have two options:

  • Start using our own llvm builds with SPIRV target. We did this in the past so it is possible.
  • Update this PR to detect if llvm has SPIRV target or to introduce a build-time option so we can continue using the existing OpenAI builds and use a custom built llvm with SPIRV in a special CI workflow that I am going to add shortly.

@VyacheslavLevytskyy VyacheslavLevytskyy force-pushed the introduce-llvm-spirv-backend branch from 8a0a03e to 33556bd Compare January 31, 2025 20:27
@VyacheslavLevytskyy
Copy link
Author

@pbchekin Can you please suggest what else should be updated for the prebuilt /home/runner/.triton/llvm/ to be configured according to -DLLVM_TARGETS_TO_BUILD="X86;NVPTX;AMDGPU;SPIRV" ?

We currently use LLVM prebuilt by OpenAI (https://github.com/triton-lang/triton/blob/main/.github/workflows/llvm-build.yml), not sure if they are ready to add SPIRV target right now to their build workflow. So we have two options:

  • Start using our own llvm builds with SPIRV target. We did this in the past so it is possible.
  • Update this PR to detect if llvm has SPIRV target or to introduce a build-time option so we can continue using the existing OpenAI builds and use a custom built llvm with SPIRV in a special CI workflow that I am going to add shortly.

Good point, thank you. Then I revert back to the initial logic that doesn't rely and always checks presence of SPIR-V Backend.

@VyacheslavLevytskyy VyacheslavLevytskyy force-pushed the introduce-llvm-spirv-backend branch from 33556bd to fbb5d89 Compare January 31, 2025 20:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants