Skip to content

Commit 233a54a

Browse files
authored
fix: LLVM_Util::supports_isa is not thread-safe (#2029)
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 <[email protected]>
1 parent 7c77f05 commit 233a54a

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/liboslexec/llvm_util.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,16 @@ LLVM_Util::supports_isa(TargetISA target)
14431443
continue;
14441444
}
14451445
OSL_DEV_ONLY(std::cout << "Testing for cpu feature:" << f << std::endl);
1446-
if (sCpuFeatures[f] == false) {
1446+
// The required CPU feature for the specified target might not be in
1447+
// the sCpuFeatures. This happens, for example, when the code is probing
1448+
// the best target ISA when the requested one is UNKNOWN. In this case
1449+
// it is possible that this function is called for the TargetISA::AVX512
1450+
// on an ARM CPU.
1451+
// This function might be called from multiple threads, so it is important
1452+
// to keep the access to sCpuFeatures read-only.
1453+
const auto cpu_feature_it = sCpuFeatures.find(f);
1454+
if (cpu_feature_it == sCpuFeatures.end()
1455+
|| cpu_feature_it->second == false) {
14471456
OSL_DEV_ONLY(std::cout << "MISSING cpu feature:" << f << std::endl);
14481457
return false;
14491458
}

0 commit comments

Comments
 (0)