From 561f2ab48c52c8c9fe7027dd3f57a65d4d51c5bc Mon Sep 17 00:00:00 2001 From: Whitney Tsang Date: Mon, 1 Dec 2025 03:41:38 +0000 Subject: [PATCH 1/3] Change SPV_KHR_bfloat16 workaround patch to work for different drivers Signed-off-by: Whitney Tsang --- setup.py | 9 ----- third_party/intel/cmake/3122.patch | 29 +++++++++----- .../cmake/FindSPIRVToLLVMTranslator.cmake | 40 +++++++++---------- 3 files changed, 37 insertions(+), 41 deletions(-) diff --git a/setup.py b/setup.py index 9083718fc5..fc791db4a6 100644 --- a/setup.py +++ b/setup.py @@ -489,15 +489,6 @@ def build_extension(self, ext): cmake_args.append("-DLLVM_EXTERNAL_LIT=" + lit_dir) cmake_args.extend(thirdparty_cmake_args) - result = subprocess.run(["bash", "./scripts/capture-hw-details.sh"], stdout=subprocess.PIPE, - stderr=subprocess.PIPE, check=True, text=True, env=os.environ.copy()) - agama_version = None - for line in result.stdout.splitlines(): - if line.startswith("AGAMA_VERSION="): - agama_version = line.split("=", 1)[1].strip() - break - cmake_args.append(f"-DAGAMA_VERSION={agama_version}") - # configuration cfg = get_build_type() build_args = ["--config", cfg] diff --git a/third_party/intel/cmake/3122.patch b/third_party/intel/cmake/3122.patch index 1984ee5e69..cb08f8e3af 100644 --- a/third_party/intel/cmake/3122.patch +++ b/third_party/intel/cmake/3122.patch @@ -1,20 +1,27 @@ diff --git a/lib/SPIRV/SPIRVWriter.cpp b/lib/SPIRV/SPIRVWriter.cpp -index a124ba48c..3f46b5685 100644 +index ec4ec41f..9cad08d8 100644 --- a/lib/SPIRV/SPIRVWriter.cpp +++ b/lib/SPIRV/SPIRVWriter.cpp -@@ -397,6 +397,7 @@ SPIRVType *LLVMToSPIRVBase::transType(Type *T) { - } +@@ -401,13 +401,15 @@ SPIRVType *LLVMToSPIRVBase::transType(Type *T) { } -+#if 0 if (T->isBFloatTy()) { - BM->getErrorLog().checkError( - BM->isAllowedToUseExtension(ExtensionID::SPV_KHR_bfloat16), -@@ -406,6 +407,7 @@ SPIRVType *LLVMToSPIRVBase::transType(Type *T) { - "requires this extension"); - return mapType(T, BM->addFloatType(16, FPEncodingBFloat16KHR)); +- BM->getErrorLog().checkError( +- BM->isAllowedToUseExtension(ExtensionID::SPV_KHR_bfloat16), +- SPIRVEC_RequiresExtension, +- "SPV_KHR_bfloat16\n" +- "NOTE: LLVM module contains bfloat type, translation of which " +- "requires this extension"); +- return mapType(T, BM->addFloatType(16, FPEncodingBFloat16KHR)); ++ if (BM->isAllowedToUseExtension(ExtensionID::SPV_KHR_bfloat16)) { ++ BM->getErrorLog().checkError( ++ BM->isAllowedToUseExtension(ExtensionID::SPV_KHR_bfloat16), ++ SPIRVEC_RequiresExtension, ++ "SPV_KHR_bfloat16\n" ++ "NOTE: LLVM module contains bfloat type, translation of which " ++ "requires this extension"); ++ return mapType(T, BM->addFloatType(16, FPEncodingBFloat16KHR)); ++ } } -+#endif if (T->isFloatingPointTy()) - return mapType(T, BM->addFloatType(T->getPrimitiveSizeInBits())); diff --git a/third_party/intel/cmake/FindSPIRVToLLVMTranslator.cmake b/third_party/intel/cmake/FindSPIRVToLLVMTranslator.cmake index d059f86e02..237722a60b 100644 --- a/third_party/intel/cmake/FindSPIRVToLLVMTranslator.cmake +++ b/third_party/intel/cmake/FindSPIRVToLLVMTranslator.cmake @@ -27,29 +27,27 @@ if (NOT SPIRVToLLVMTranslator_FOUND) FetchContent_MakeAvailable(spirv-llvm-translator) # FIXME: Don't apply patch when LTS driver is updated. - if(DEFINED AGAMA_VERSION AND AGAMA_VERSION STREQUAL "1146") + execute_process( + COMMAND git apply --check ${CMAKE_CURRENT_LIST_DIR}/3122.patch + WORKING_DIRECTORY ${spirv-llvm-translator_SOURCE_DIR} + ERROR_QUIET + RESULT_VARIABLE PATCH_RESULT + ) + if(PATCH_RESULT EQUAL 0) execute_process( - COMMAND git apply --check ${CMAKE_CURRENT_LIST_DIR}/3122.patch - WORKING_DIRECTORY ${spirv-llvm-translator_SOURCE_DIR} - ERROR_QUIET - RESULT_VARIABLE PATCH_RESULT + COMMAND git apply ${CMAKE_CURRENT_LIST_DIR}/3122.patch + WORKING_DIRECTORY ${spirv-llvm-translator_SOURCE_DIR} + RESULT_VARIABLE PATCH_RESULT ) - if(PATCH_RESULT EQUAL 0) - execute_process( - COMMAND git apply ${CMAKE_CURRENT_LIST_DIR}/3122.patch - WORKING_DIRECTORY ${spirv-llvm-translator_SOURCE_DIR} - RESULT_VARIABLE PATCH_RESULT - ) - else() - execute_process( # Check if the patch is already applied - COMMAND git apply --reverse --check ${CMAKE_CURRENT_LIST_DIR}/3122.patch - WORKING_DIRECTORY ${spirv-llvm-translator_SOURCE_DIR} - RESULT_VARIABLE PATCH_RESULT - ) - endif() - if(NOT PATCH_RESULT EQUAL 0) - message(FATAL_ERROR "Failed to apply 3122.patch to SPIRV-LLVM-Translator") - endif() + else() + execute_process( # Check if the patch is already applied + COMMAND git apply --reverse --check ${CMAKE_CURRENT_LIST_DIR}/3122.patch + WORKING_DIRECTORY ${spirv-llvm-translator_SOURCE_DIR} + RESULT_VARIABLE PATCH_RESULT + ) + endif() + if(NOT PATCH_RESULT EQUAL 0) + message(FATAL_ERROR "Failed to apply 3122.patch to SPIRV-LLVM-Translator") endif() # FIXME: Don't apply patch when Agama driver is updated to incorporate with the SPV_INTEL_bfloat16_arithmetic extension. From 7d3d89841882a8a0cb73577f5598017034834da3 Mon Sep 17 00:00:00 2001 From: Whitney Tsang Date: Tue, 2 Dec 2025 20:55:32 +0000 Subject: [PATCH 2/3] Check driver version in SPIRV translator patch --- third_party/intel/backend/compiler.py | 2 ++ third_party/intel/cmake/3122.patch | 8 +++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/third_party/intel/backend/compiler.py b/third_party/intel/backend/compiler.py index 7561e50eae..f552dbf8e7 100644 --- a/third_party/intel/backend/compiler.py +++ b/third_party/intel/backend/compiler.py @@ -409,6 +409,8 @@ def make_llir(cls, src, metadata, options): @classmethod @track def make_spv(cls, src, metadata, options): + driver_version = metadata["target"].arch.get("driver_version") + os.environ["INTEL_XPU_BACKEND_DRIVER_VERSION"] = driver_version spirv, name = intel.translate_to_spirv(src) metadata["name"] = name metadata.setdefault("build_flags", "") diff --git a/third_party/intel/cmake/3122.patch b/third_party/intel/cmake/3122.patch index cb08f8e3af..76fcc64be7 100644 --- a/third_party/intel/cmake/3122.patch +++ b/third_party/intel/cmake/3122.patch @@ -1,8 +1,8 @@ diff --git a/lib/SPIRV/SPIRVWriter.cpp b/lib/SPIRV/SPIRVWriter.cpp -index ec4ec41f..9cad08d8 100644 +index ec4ec41f..6d89e29c 100644 --- a/lib/SPIRV/SPIRVWriter.cpp +++ b/lib/SPIRV/SPIRVWriter.cpp -@@ -401,13 +401,15 @@ SPIRVType *LLVMToSPIRVBase::transType(Type *T) { +@@ -401,13 +401,17 @@ SPIRVType *LLVMToSPIRVBase::transType(Type *T) { } if (T->isBFloatTy()) { @@ -13,7 +13,9 @@ index ec4ec41f..9cad08d8 100644 - "NOTE: LLVM module contains bfloat type, translation of which " - "requires this extension"); - return mapType(T, BM->addFloatType(16, FPEncodingBFloat16KHR)); -+ if (BM->isAllowedToUseExtension(ExtensionID::SPV_KHR_bfloat16)) { ++ // Workaround for LTS2 driver. ++ const char *driverVersion = std::getenv("INTEL_XPU_BACKEND_DRIVER_VERSION"); ++ if (driverVersion && std::string(driverVersion) != "1.6.33578+38") { + BM->getErrorLog().checkError( + BM->isAllowedToUseExtension(ExtensionID::SPV_KHR_bfloat16), + SPIRVEC_RequiresExtension, From a5d6a6f5bf63dfe5bff05baee04418b55ee1c3d1 Mon Sep 17 00:00:00 2001 From: Whitney Tsang Date: Tue, 9 Dec 2025 03:29:00 +0000 Subject: [PATCH 3/3] address review comment --- third_party/intel/cmake/3122.patch | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/third_party/intel/cmake/3122.patch b/third_party/intel/cmake/3122.patch index 76fcc64be7..002eadf072 100644 --- a/third_party/intel/cmake/3122.patch +++ b/third_party/intel/cmake/3122.patch @@ -1,8 +1,8 @@ diff --git a/lib/SPIRV/SPIRVWriter.cpp b/lib/SPIRV/SPIRVWriter.cpp -index ec4ec41f..6d89e29c 100644 +index ec4ec41f..b481609f 100644 --- a/lib/SPIRV/SPIRVWriter.cpp +++ b/lib/SPIRV/SPIRVWriter.cpp -@@ -401,13 +401,17 @@ SPIRVType *LLVMToSPIRVBase::transType(Type *T) { +@@ -401,13 +401,23 @@ SPIRVType *LLVMToSPIRVBase::transType(Type *T) { } if (T->isBFloatTy()) { @@ -15,14 +15,20 @@ index ec4ec41f..6d89e29c 100644 - return mapType(T, BM->addFloatType(16, FPEncodingBFloat16KHR)); + // Workaround for LTS2 driver. + const char *driverVersion = std::getenv("INTEL_XPU_BACKEND_DRIVER_VERSION"); -+ if (driverVersion && std::string(driverVersion) != "1.6.33578+38") { -+ BM->getErrorLog().checkError( -+ BM->isAllowedToUseExtension(ExtensionID::SPV_KHR_bfloat16), -+ SPIRVEC_RequiresExtension, -+ "SPV_KHR_bfloat16\n" -+ "NOTE: LLVM module contains bfloat type, translation of which " -+ "requires this extension"); -+ return mapType(T, BM->addFloatType(16, FPEncodingBFloat16KHR)); ++ if (driverVersion) { ++ int v0 = 0, v1 = 0, v2 = 0, v3 = 0; ++ sscanf(driverVersion, "%d.%d.%d+%d", &v0, &v1, &v2, &v3); ++ std::tuple ver = {v0, v1, v2, v3}; ++ std::tuple minVer = {1, 6, 35096, 9}; ++ if (ver >= minVer) { ++ BM->getErrorLog().checkError( ++ BM->isAllowedToUseExtension(ExtensionID::SPV_KHR_bfloat16), ++ SPIRVEC_RequiresExtension, ++ "SPV_KHR_bfloat16\n" ++ "NOTE: LLVM module contains bfloat type, translation of which " ++ "requires this extension"); ++ return mapType(T, BM->addFloatType(16, FPEncodingBFloat16KHR)); ++ } + } }