Build: Fix threading issue in OSL

This change ports the PR from upstream to the build system:
https://github.com/AcademySoftwareFoundation/OpenShadingLanguage/pull/2029

Ref #147642

Pull Request: https://projects.blender.org/blender/blender/pulls/147727
This commit is contained in:
Sergey Sharybin
2025-10-15 16:07:45 +02:00
committed by Sergey Sharybin
parent 7b3737f1e9
commit 1621950441
2 changed files with 55 additions and 3 deletions

View File

@@ -77,9 +77,13 @@ ExternalProject_Add(external_osl
URL_HASH ${OSL_HASH_TYPE}=${OSL_HASH} URL_HASH ${OSL_HASH_TYPE}=${OSL_HASH}
PREFIX ${BUILD_DIR}/osl PREFIX ${BUILD_DIR}/osl
PATCH_COMMAND ${PATCH_CMD} -p 1 -d PATCH_COMMAND
${BUILD_DIR}/osl/src/external_osl < ${PATCH_CMD} -p 1 -d
${PATCH_DIR}/osl.diff ${BUILD_DIR}/osl/src/external_osl <
${PATCH_DIR}/osl.diff &&
${PATCH_CMD} -p 1 -d
${BUILD_DIR}/osl/src/external_osl <
${PATCH_DIR}/osl_supports_isa_thread.diff
CMAKE_ARGS CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX=${LIBDIR}/osl -DCMAKE_INSTALL_PREFIX=${LIBDIR}/osl

View File

@@ -0,0 +1,48 @@
From 69d11b6eebcfb1501193184333280ebd5985564e Mon Sep 17 00:00:00 2001
From: Sergey Sharybin <sergey@blender.org>
Date: Thu, 9 Oct 2025 12:25:09 +0200
Subject: [PATCH] Fix: LLVM_Util::supports_isa is not thread-safe
The supports_isa() function might be called from threads, and prior
to this change it might have added entries to the global sCpuFeatures
variable. This happened when detect_cpu_features() is called with
TargetISA::UNKNOWN and the code was looking for the best ISA. This
could lead to situation when the host CPU is ARM, and the probing
happens for AVX512.
This change ensures that the supports_isa() accesses sCpuFeatures
in the read-only manner.
This was originally noticed as unreliable OSL render tests in Blender
reported at https://projects.blender.org/blender/blender/issues/147642
Signed-off-by: Sergey Sharybin <sergey@blender.org>
---
src/liboslexec/llvm_util.cpp | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/src/liboslexec/llvm_util.cpp b/src/liboslexec/llvm_util.cpp
index f9b1296d..2d952567 100644
--- a/src/liboslexec/llvm_util.cpp
+++ b/src/liboslexec/llvm_util.cpp
@@ -1443,7 +1443,16 @@ LLVM_Util::supports_isa(TargetISA target)
continue;
}
OSL_DEV_ONLY(std::cout << "Testing for cpu feature:" << f << std::endl);
- if (sCpuFeatures[f] == false) {
+ // The required CPU feature for the specified target might not be in
+ // the sCpuFeatures. This happens, for example, when the code is probing
+ // the best target ISA when the requested one is UNKNOWN. In this case
+ // it is possible that this function is called for the TargetISA::AVX512
+ // on an ARM CPU.
+ // This function might be called from multiple threads, so it is important
+ // to keep the access to sCpuFeatures read-only.
+ const auto cpu_feature_it = sCpuFeatures.find(f);
+ if (cpu_feature_it == sCpuFeatures.end()
+ || cpu_feature_it->second == false) {
OSL_DEV_ONLY(std::cout << "MISSING cpu feature:" << f << std::endl);
return false;
}
--
2.51.0