Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions Source/JavaScriptCore/assembler/MacroAssemblerX86_64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ namespace JSC {

JSC_DECLARE_NOEXCEPT_JIT_OPERATION(ctiMasmProbeTrampoline, void, ());
JSC_ANNOTATE_JIT_OPERATION_PROBE(ctiMasmProbeTrampoline);
#if !OS(DARWIN)
JSC_DECLARE_NOEXCEPT_JIT_OPERATION(ctiMasmProbeTrampolineAVX, void, ());
JSC_ANNOTATE_JIT_OPERATION_PROBE(ctiMasmProbeTrampolineAVX);
#endif

// The following are offsets for Probe::State fields accessed by the ctiMasmProbeTrampoline stub.

Expand Down Expand Up @@ -193,7 +191,6 @@ static_assert(sizeof(Probe::State) == PROBE_SIZE, "Probe::State::size's matches
"vmovaps " STRINGIZE_VALUE_OF(PROBE_CPU_XMM14_VECTOR_OFFSET) "(%rbp), %xmm14" "\n" \
"vmovaps " STRINGIZE_VALUE_OF(PROBE_CPU_XMM15_VECTOR_OFFSET) "(%rbp), %xmm15" "\n"

#if !OS(DARWIN)
#define PROBE_XMM_SAVE_MOVAPS \
"movaps %xmm0, " STRINGIZE_VALUE_OF(PROBE_CPU_XMM0_VECTOR_OFFSET) "(%rsp)" "\n" \
"movaps %xmm1, " STRINGIZE_VALUE_OF(PROBE_CPU_XMM1_VECTOR_OFFSET) "(%rsp)" "\n" \
Expand Down Expand Up @@ -229,7 +226,6 @@ static_assert(sizeof(Probe::State) == PROBE_SIZE, "Probe::State::size's matches
"movaps " STRINGIZE_VALUE_OF(PROBE_CPU_XMM13_VECTOR_OFFSET) "(%rbp), %xmm13" "\n" \
"movaps " STRINGIZE_VALUE_OF(PROBE_CPU_XMM14_VECTOR_OFFSET) "(%rbp), %xmm14" "\n" \
"movaps " STRINGIZE_VALUE_OF(PROBE_CPU_XMM15_VECTOR_OFFSET) "(%rbp), %xmm15" "\n"
#endif // !OS(DARWIN)

#if COMPILER(MSVC)
#define ASM_PREVIOUS_SECTION
Expand Down Expand Up @@ -404,19 +400,13 @@ __asm__( \
ASM_PREVIOUS_SECTION \
);

#if OS(DARWIN)
// On macOS, all x86_64 CPUs support AVX. Use vmovaps unconditionally.
DEFINE_PROBE_TRAMPOLINE(ctiMasmProbeTrampoline, PROBE_XMM_SAVE_VMOVAPS, PROBE_XMM_RESTORE_VMOVAPS,
ctiMasmProbeTrampolineCopyLoop, ctiMasmProbeTrampolineProbeStateIsSafe, ctiMasmProbeTrampolineRestoreRegisters)
#else
// On Linux/Windows, AVX may not be available. Provide two trampolines:
// AVX may not be available. Provide two trampolines:
// - ctiMasmProbeTrampoline: uses movaps (SSE, always available on x86_64)
// - ctiMasmProbeTrampolineAVX: uses vmovaps (requires AVX)
DEFINE_PROBE_TRAMPOLINE(ctiMasmProbeTrampoline, PROBE_XMM_SAVE_MOVAPS, PROBE_XMM_RESTORE_MOVAPS,
ctiMasmProbeTrampolineCopyLoop, ctiMasmProbeTrampolineProbeStateIsSafe, ctiMasmProbeTrampolineRestoreRegisters)
DEFINE_PROBE_TRAMPOLINE(ctiMasmProbeTrampolineAVX, PROBE_XMM_SAVE_VMOVAPS, PROBE_XMM_RESTORE_VMOVAPS,
ctiMasmProbeTrampolineAVXCopyLoop, ctiMasmProbeTrampolineAVXProbeStateIsSafe, ctiMasmProbeTrampolineAVXRestoreRegisters)
#endif

// What code is emitted for the probe?
// ==================================
Expand Down Expand Up @@ -464,14 +454,10 @@ void MacroAssembler::probe(Probe::Function function, void* arg)
push(RegisterID::eax);
push(RegisterID::eax);

#if OS(DARWIN)
move(TrustedImmPtr(reinterpret_cast<void*>(ctiMasmProbeTrampoline)), RegisterID::eax);
#else
if (supportsAVX())
move(TrustedImmPtr(reinterpret_cast<void*>(ctiMasmProbeTrampolineAVX)), RegisterID::eax);
else
move(TrustedImmPtr(reinterpret_cast<void*>(ctiMasmProbeTrampoline)), RegisterID::eax);
#endif

push(RegisterID::edx);
move(TrustedImmPtr(reinterpret_cast<void*>(function)), RegisterID::edx);
Expand Down Expand Up @@ -517,7 +503,16 @@ void MacroAssemblerX86_64::collectCPUFeatures()
s_sse4_2CheckState = (cpuid[2] & (1 << 20)) ? CPUIDCheckState::Set : CPUIDCheckState::Clear;
s_popcntCheckState = (cpuid[2] & (1 << 23)) ? CPUIDCheckState::Set : CPUIDCheckState::Clear;
#if OS(DARWIN)
s_avxCheckState = CPUIDCheckState::Set;
// Every Mac Apple shipped with an x86_64 CPU newer than 2011 has AVX,
// but Westmere Mac Pros (5,1) run macOS 14 via OpenCore and do not.
// Ask the OS rather than assuming; the SSE fallbacks behind
// supportsAVX() already exist and are what those machines need.
{
uint32_t avxVal = 0;
size_t avxValSize = sizeof(avxVal);
int avxRc = sysctlbyname("hw.optional.avx1_0", &avxVal, &avxValSize, nullptr, 0);
s_avxCheckState = (avxRc >= 0 && avxVal) ? CPUIDCheckState::Set : CPUIDCheckState::Clear;
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
#else
// Per the Intel SDM, AVX is usable only when:
// 1. CPUID.1:ECX.AVX[bit 28] = 1 (CPU supports AVX)
Expand Down