Bug 71659 - _xgetbv intrinsic missing
Summary: _xgetbv intrinsic missing
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 6.0
: P3 enhancement
Target Milestone: 9.0
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks: 88918
  Show dependency treegraph
 
Reported: 2016-06-25 18:18 UTC by Andi Kleen
Modified: 2019-01-21 12:29 UTC (History)
6 users (show)

See Also:
Host:
Target: x86_64-linux
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andi Kleen 2016-06-25 18:18:49 UTC
icc and microsoft have a _xgetbv intrinsic for the XGETBV instruction, which is needed to check if AVX or MPX are supported by the kernel.

gcc is missing an intrinsic for that, so everyone has to write inline assembler. Should add one.
Comment 1 H.J. Lu 2016-06-25 20:17:40 UTC
You need more than an intrinsic to know if AVX or MPX is available.
So far, run-time implementation has no issue with inline asm statement.
Comment 2 postmaster 2017-02-28 13:47:29 UTC
Portability is one main reason to add missing intrinsics... with combination of cpuid check and _xgetbv() we can cleanly check if AVX or MPX is available at run-time. We can also check specific instructions during configure process to see if we need to add workarounds for bad or missing functions/intrinsics.

Some developers think that cleanliness of the code is more important than need to reduplicate hand-written assembler code every time for optimal performance.

We have to remember that gcc is not only used for BSD-like operating systems, including OS/X, Linux, *BSD etc, but for Cygwin, MSYS/MSYS2 and MinGW which benefit from gcc being as close as possible compiler of Visual C++ regarding intrinsics support.
Comment 3 Jeffrey Walton 2018-06-18 18:32:30 UTC
(In reply to postmaster from comment #2)
> Portability is one main reason to add missing intrinsics... with combination
> of cpuid check and _xgetbv() we can cleanly check if AVX or MPX is available
> at run-time. We can also check specific instructions during configure
> process to see if we need to add workarounds for bad or missing
> functions/intrinsics.

+1. We were trying to use Intel's algorithm described at https://software.intel.com/en-us/blogs/2011/04/14/is-avx-enabled . We should only need __get_cpuid and _xgetbv. We should not need that nasty GCC inline assembly.
Comment 4 Daniel Fruzynski 2019-01-17 14:39:47 UTC
This intrinsics was added in gcc 8. Initial implementation was buggy (see r85684) and was fixed in 8.2 However there is one more issue here: Intel Intrinsics Guide says that it should be available by including <immintrin.h>, however in gcc you need to include <xsaveintrin.h>.

Additionally there are no defines for XFEATURE_ENABLED_MASK and possible output values.
Comment 5 Daniel Fruzynski 2019-01-17 14:41:11 UTC
I meant pr85684
Comment 6 hjl@gcc.gnu.org 2019-01-21 12:24:24 UTC
Author: hjl
Date: Mon Jan 21 12:23:49 2019
New Revision: 268113

URL: https://gcc.gnu.org/viewcvs?rev=268113&root=gcc&view=rev
Log:
i386: Move Intel intrinsics head files to <immintrin.h>

According to Intel Intrinsics Guide:

https://software.intel.com/sites/landingpage/IntrinsicsGuide/

Intel intrinsics should be available by including <immintrin.h>.  This
patch moves remaining Intel intrinsics head files from <x86intrin.h> to
<immintrin.h>.

	PR target/71659
	* config/i386/adxintrin.h: Just check _IMMINTRIN_H_INCLUDED.
	* config/i386/clflushoptintrin.h: Check _IMMINTRIN_H_INCLUDED
	instead of _X86INTRIN_H_INCLUDED.
	* onfig/i386/clwbintrin.h: Likewise.
	* config/i386/pkuintrin.h: Likewise.
	* config/i386/prfchwintrin.h: Likewise.
	* config/i386/rdseedintrin.h: Likewise.
	* config/i386/wbnoinvdintrin.h: Likewise.
	* config/i386/xsavecintrin.h: Likewise.
	* config/i386/xsavesintrin.h: Likewise.
	* config/i386/fxsrintrin.h: Enable _IMMINTRIN_H_INCLUDED check.
	* config/i386/xsaveintrin.h: Likewise.
	* config/i386/xsaveoptintrin.h: Likewise.
	* config/i386/x86intrin.h: Move "#include" <rdseedintrin.h>,
	<prfchwintrin.h>, <fxsrintrin.h>, <xsaveintrin.h>,
	<xsaveoptintrin.h>, <adxintrin.h>, <clwbintrin.h>,
	<clflushoptintrin.h>, <xsavesintrin.h>, <xsavecintrin.h>,
	<wbnoinvdintrin.h> and <pkuintrin.h> to ...
	* config/i386/immintrin.h: Here.

Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/config/i386/adxintrin.h
    trunk/gcc/config/i386/clflushoptintrin.h
    trunk/gcc/config/i386/clwbintrin.h
    trunk/gcc/config/i386/fxsrintrin.h
    trunk/gcc/config/i386/immintrin.h
    trunk/gcc/config/i386/pkuintrin.h
    trunk/gcc/config/i386/prfchwintrin.h
    trunk/gcc/config/i386/rdseedintrin.h
    trunk/gcc/config/i386/wbnoinvdintrin.h
    trunk/gcc/config/i386/x86intrin.h
    trunk/gcc/config/i386/xsavecintrin.h
    trunk/gcc/config/i386/xsaveintrin.h
    trunk/gcc/config/i386/xsaveoptintrin.h
    trunk/gcc/config/i386/xsavesintrin.h
Comment 7 H.J. Lu 2019-01-21 12:29:06 UTC
(In reply to Daniel Fruzynski from comment #4)
> This intrinsics was added in gcc 8. Initial implementation was buggy (see
> r85684) and was fixed in 8.2 However there is one more issue here: Intel
> Intrinsics Guide says that it should be available by including
> <immintrin.h>, however in gcc you need to include <xsaveintrin.h>.

Fixed for GCC 9.

> Additionally there are no defines for XFEATURE_ENABLED_MASK and possible
> output values.

I will investigate it.