Skip to content

Commit fb7d53f

Browse files
sergeyvfxlgritz
authored andcommitted
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 0f60796 commit fb7d53f

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
@@ -1487,7 +1487,16 @@ LLVM_Util::supports_isa(TargetISA target)
14871487
continue;
14881488
}
14891489
OSL_DEV_ONLY(std::cout << "Testing for cpu feature:" << f << std::endl);
1490-
if (sCpuFeatures[f] == false) {
1490+
// The required CPU feature for the specified target might not be in
1491+
// the sCpuFeatures. This happens, for example, when the code is probing
1492+
// the best target ISA when the requested one is UNKNOWN. In this case
1493+
// it is possible that this function is called for the TargetISA::AVX512
1494+
// on an ARM CPU.
1495+
// This function might be called from multiple threads, so it is important
1496+
// to keep the access to sCpuFeatures read-only.
1497+
const auto cpu_feature_it = sCpuFeatures.find(f);
1498+
if (cpu_feature_it == sCpuFeatures.end()
1499+
|| cpu_feature_it->second == false) {
14911500
OSL_DEV_ONLY(std::cout << "MISSING cpu feature:" << f << std::endl);
14921501
return false;
14931502
}

0 commit comments

Comments
 (0)