From: Asaf Badouh Date: Thu, 31 Dec 2015 14:14:07 +0000 (+0000) Subject: [X86][PKU] add clang intrinsic for {RD|WR}PKRU X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d9c8a628c1dd52a71b6aa7c22cc94ee74ca609c3;p=clang [X86][PKU] add clang intrinsic for {RD|WR}PKRU Differential Revision: http://reviews.llvm.org/D15837 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@256672 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/BuiltinsX86.def b/include/clang/Basic/BuiltinsX86.def index 91111f6cbf..64fa4da9bb 100644 --- a/include/clang/Basic/BuiltinsX86.def +++ b/include/clang/Basic/BuiltinsX86.def @@ -917,6 +917,9 @@ TARGET_BUILTIN(__builtin_ia32_xtest, "i", "", "rtm") BUILTIN(__builtin_ia32_rdpmc, "ULLii", "") BUILTIN(__builtin_ia32_rdtsc, "ULLi", "") BUILTIN(__builtin_ia32_rdtscp, "ULLiUi*", "") +// PKU +TARGET_BUILTIN(__builtin_ia32_rdpkru, "Ui", "", "pku") +TARGET_BUILTIN(__builtin_ia32_wrpkru, "vUi", "", "pku") // AVX-512 TARGET_BUILTIN(__builtin_ia32_sqrtpd512_mask, "V8dV8dV8dUcIi", "", "avx512f") diff --git a/include/clang/Driver/Options.td b/include/clang/Driver/Options.td index 3dbe43f107..7eb4a46db8 100644 --- a/include/clang/Driver/Options.td +++ b/include/clang/Driver/Options.td @@ -1369,6 +1369,7 @@ def mno_xsave : Flag<["-"], "mno-xsave">, Group; def mno_xsaveopt : Flag<["-"], "mno-xsaveopt">, Group; def mno_xsavec : Flag<["-"], "mno-xsavec">, Group; def mno_xsaves : Flag<["-"], "mno-xsaves">, Group; +def mno_pku : Flag<["-"], "mno-pku">, Group; def munaligned_access : Flag<["-"], "munaligned-access">, Group, HelpText<"Allow memory accesses to be unaligned (AArch32/AArch64 only)">; @@ -1520,6 +1521,7 @@ def mf16c : Flag<["-"], "mf16c">, Group; def mrtm : Flag<["-"], "mrtm">, Group; def mprfchw : Flag<["-"], "mprfchw">, Group; def mrdseed : Flag<["-"], "mrdseed">, Group; +def mpku : Flag<["-"], "mpku">, Group; def madx : Flag<["-"], "madx">, Group; def msha : Flag<["-"], "msha">, Group; def mcx16 : Flag<["-"], "mcx16">, Group; diff --git a/lib/Basic/Targets.cpp b/lib/Basic/Targets.cpp index 893bd7c498..b36fddd056 100644 --- a/lib/Basic/Targets.cpp +++ b/lib/Basic/Targets.cpp @@ -2095,6 +2095,7 @@ class X86TargetInfo : public TargetInfo { bool HasXSAVEOPT = false; bool HasXSAVEC = false; bool HasXSAVES = false; + bool HasPKU = false; /// \brief Enumeration of all of the X86 CPUs supported by Clang. /// @@ -2596,6 +2597,7 @@ bool X86TargetInfo::initFeatureMap( setFeatureEnabledImpl(Features, "avx512vl", true); setFeatureEnabledImpl(Features, "xsavec", true); setFeatureEnabledImpl(Features, "xsaves", true); + setFeatureEnabledImpl(Features, "pku", true); // FALLTHROUGH case CK_Broadwell: setFeatureEnabledImpl(Features, "rdseed", true); @@ -3021,6 +3023,8 @@ bool X86TargetInfo::handleTargetFeatures(std::vector &Features, HasXSAVEC = true; } else if (Feature == "+xsaves") { HasXSAVES = true; + } else if (Feature == "+pku") { + HasPKU = true; } X86SSEEnum Level = llvm::StringSwitch(Feature) @@ -3322,7 +3326,8 @@ void X86TargetInfo::getTargetDefines(const LangOptions &Opts, Builder.defineMacro("__XSAVEC__"); if (HasXSAVES) Builder.defineMacro("__XSAVES__"); - + if (HasPKU) + Builder.defineMacro("__PKU__"); if (HasCX16) Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_16"); @@ -3440,6 +3445,7 @@ bool X86TargetInfo::hasFeature(StringRef Feature) const { .Case("xsavec", HasXSAVEC) .Case("xsaves", HasXSAVES) .Case("xsaveopt", HasXSAVEOPT) + .Case("pku", HasPKU) .Default(false); } diff --git a/lib/Headers/CMakeLists.txt b/lib/Headers/CMakeLists.txt index 9393f69d41..bbe0688be6 100644 --- a/lib/Headers/CMakeLists.txt +++ b/lib/Headers/CMakeLists.txt @@ -12,6 +12,7 @@ set(files avx512vlintrin.h avx512dqintrin.h avx512vldqintrin.h + pkuintrin.h avxintrin.h bmi2intrin.h bmiintrin.h diff --git a/lib/Headers/immintrin.h b/lib/Headers/immintrin.h index f3c6d1914d..6376461226 100644 --- a/lib/Headers/immintrin.h +++ b/lib/Headers/immintrin.h @@ -79,6 +79,8 @@ _mm256_cvtph_ps(__m128i __a) #include +#include + static __inline__ int __attribute__((__always_inline__, __nodebug__, __target__("rdrnd"))) _rdrand16_step(unsigned short *__p) { diff --git a/lib/Headers/pkuintrin.h b/lib/Headers/pkuintrin.h new file mode 100644 index 0000000000..ad123481cf --- /dev/null +++ b/lib/Headers/pkuintrin.h @@ -0,0 +1,48 @@ +/*===------------- pkuintrin.h - PKU intrinsics ------------------=== + * + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + *===-----------------------------------------------------------------------=== + */ +#ifndef __IMMINTRIN_H +#error "Never use directly; include instead." +#endif + +#ifndef __PKUINTRIN_H +#define __PKUINTRIN_H + +/* Define the default attributes for the functions in this file. */ +#define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__, __target__("pku"))) + +static __inline__ unsigned int __DEFAULT_FN_ATTRS +_rdpkru_u32(void) +{ + return __builtin_ia32_rdpkru(); +} + +static __inline__ void __DEFAULT_FN_ATTRS +_wrpkru(unsigned int val) +{ + return __builtin_ia32_wrpkru(val); +} + +#undef __DEFAULT_FN_ATTRS + +#endif diff --git a/test/CodeGen/pku.c b/test/CodeGen/pku.c new file mode 100644 index 0000000000..6db40c4871 --- /dev/null +++ b/test/CodeGen/pku.c @@ -0,0 +1,18 @@ +// RUN: %clang_cc1 %s -triple=x86_64-apple-darwin -target-feature +pku -emit-llvm -o - -Werror | FileCheck %s + +// Don't include mm_malloc.h, it's system specific. +#define __MM_MALLOC_H + +#include + +unsigned int test_rdpkru_u32() { + // CHECK-LABEL: @test_rdpkru_u32 + // CHECK: @llvm.x86.rdpkru + return _rdpkru_u32(); +} +void test_wrpkru(unsigned int __A) { + // CHECK-LABEL: @test_wrpkru + // CHECK: @llvm.x86.wrpkru + _wrpkru(__A); + return ; +}