Skip to content

Commit ba7cf9a

Browse files
committed
x86: Disable AVX/AVX-512 if XCR0 flags are not set
Signed-off-by: Bradley Wood <[email protected]>
1 parent fcb63d9 commit ba7cf9a

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed

runtime/compiler/env/ProcessorDetection.cpp

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,17 @@
116116
#include <strings.h>
117117
#endif
118118

119+
#if defined(OMR_OS_WINDOWS) && defined(TR_TARGET_X86)
120+
#include <intrin.h>
121+
#elif defined(TR_TARGET_X86)
122+
inline unsigned long long _xgetbv(unsigned int ecx)
123+
{
124+
unsigned int eax, edx;
125+
__asm__ __volatile__("xgetbv" : "=a"(eax), "=d"(edx) : "c"(ecx));
126+
return ((unsigned long long)edx << 32) | eax;
127+
}
128+
#endif
129+
119130
#if defined(J9ZOS390)
120131
extern "C" bool _isPSWInProblemState(); /* 390 asm stub */
121132
#endif
@@ -296,7 +307,7 @@ void
296307
TR_J9VM::initializeProcessorType()
297308
{
298309
TR_ASSERT(_compInfo,"compInfo not defined");
299-
310+
300311
if (TR::Compiler->target.cpu.isZ())
301312
{
302313
OMRProcessorDesc processorDescription = TR::Compiler->target.cpu.getProcessorDescription();
@@ -333,12 +344,42 @@ TR_J9VM::initializeProcessorType()
333344
{
334345
OMRProcessorDesc processorDescription = TR::Compiler->target.cpu.getProcessorDescription();
335346
OMRPORT_ACCESS_FROM_OMRPORT(TR::Compiler->omrPortLib);
336-
static const bool disableAVX = feGetEnv("TR_DisableAVX") != NULL;
337-
if (disableAVX)
347+
348+
bool disableAVX = true;
349+
bool disableAVX512 = true;
350+
351+
// Check XCRO register for OS support of xmm/ymm/zmm
352+
if (TRUE == omrsysinfo_processor_has_feature(&processorDescription, OMR_FEATURE_X86_OSXSAVE))
338353
{
339-
omrsysinfo_processor_set_feature(&processorDescription, OMR_FEATURE_X86_OSXSAVE, FALSE);
354+
// '6' = mask for XCR0[2:1]='11b' (XMM state and YMM state are enabled)
355+
disableAVX = ((6 & _xgetbv(0)) != 6);
356+
// 'e6' = (mask for XCR0[7:5]='111b' (Opmask, ZMM_Hi256, Hi16_ZMM) + XCR0[2:1]='11b' (XMM/YMM))
357+
disableAVX512 = ((0xe6 & _xgetbv(0)) != 0xe6);
340358
}
341-
359+
360+
if(disableAVX)
361+
{
362+
// Unset AVX/AVX2 if not enabled via CR0 or otherwise disabled
363+
omrsysinfo_processor_set_feature(&processorDescription, OMR_FEATURE_X86_AVX, FALSE);
364+
omrsysinfo_processor_set_feature(&processorDescription, OMR_FEATURE_X86_AVX2, FALSE);
365+
}
366+
367+
if (disableAVX512)
368+
{
369+
// Unset AVX-512 if not enabled via CR0 or otherwise disabled
370+
// If other AVX-512 extensions are supported in the port library, they need to be disabled here
371+
omrsysinfo_processor_set_feature(&processorDescription, OMR_FEATURE_X86_AVX512F, FALSE);
372+
omrsysinfo_processor_set_feature(&processorDescription, OMR_FEATURE_X86_AVX512VL, FALSE);
373+
omrsysinfo_processor_set_feature(&processorDescription, OMR_FEATURE_X86_AVX512BW, FALSE);
374+
omrsysinfo_processor_set_feature(&processorDescription, OMR_FEATURE_X86_AVX512CD, FALSE);
375+
omrsysinfo_processor_set_feature(&processorDescription, OMR_FEATURE_X86_AVX512DQ, FALSE);
376+
omrsysinfo_processor_set_feature(&processorDescription, OMR_FEATURE_X86_AVX512_BITALG, FALSE);
377+
omrsysinfo_processor_set_feature(&processorDescription, OMR_FEATURE_X86_AVX512_VBMI, FALSE);
378+
omrsysinfo_processor_set_feature(&processorDescription, OMR_FEATURE_X86_AVX512_VBMI2, FALSE);
379+
omrsysinfo_processor_set_feature(&processorDescription, OMR_FEATURE_X86_AVX512_VNNI, FALSE);
380+
omrsysinfo_processor_set_feature(&processorDescription, OMR_FEATURE_X86_AVX512_VPOPCNTDQ, FALSE);
381+
}
382+
342383
TR::Compiler->target.cpu = TR::CPU::customize(processorDescription);
343384

344385
const char *vendor = TR::Compiler->target.cpu.getProcessorVendorId();

0 commit comments

Comments
 (0)