Index: E:/Work/LLVM/LLVMClang/tools/clang/include/clang/Basic/BuiltinsX86.def =================================================================== --- E:/Work/LLVM/LLVMClang/tools/clang/include/clang/Basic/BuiltinsX86.def +++ E:/Work/LLVM/LLVMClang/tools/clang/include/clang/Basic/BuiltinsX86.def @@ -917,6 +917,9 @@ BUILTIN(__builtin_ia32_rdpmc, "ULLii", "") BUILTIN(__builtin_ia32_rdtsc, "ULLi", "") BUILTIN(__builtin_ia32_rdtscp, "ULLiUi*", "") +// PKU +TARGET_BUILTIN(__builtin_ia32_rdpkru, "i", "", "pku") +TARGET_BUILTIN(__builtin_ia32_wrpkru, "vUi", "", "pku") // AVX-512 TARGET_BUILTIN(__builtin_ia32_sqrtpd512_mask, "V8dV8dV8dUcIi", "", "avx512f") Index: E:/Work/LLVM/LLVMClang/tools/clang/include/clang/Driver/Options.td =================================================================== --- E:/Work/LLVM/LLVMClang/tools/clang/include/clang/Driver/Options.td +++ E:/Work/LLVM/LLVMClang/tools/clang/include/clang/Driver/Options.td @@ -1369,6 +1369,7 @@ 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 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; Index: E:/Work/LLVM/LLVMClang/tools/clang/lib/Basic/Targets.cpp =================================================================== --- E:/Work/LLVM/LLVMClang/tools/clang/lib/Basic/Targets.cpp +++ E:/Work/LLVM/LLVMClang/tools/clang/lib/Basic/Targets.cpp @@ -2095,6 +2095,7 @@ bool HasXSAVEOPT = false; bool HasXSAVEC = false; bool HasXSAVES = false; + bool HasPKU = false; /// \brief Enumeration of all of the X86 CPUs supported by Clang. /// @@ -3021,6 +3022,8 @@ HasXSAVEC = true; } else if (Feature == "+xsaves") { HasXSAVES = true; + } else if (Feature == "+pku") { + HasPKU = true; } X86SSEEnum Level = llvm::StringSwitch(Feature) @@ -3322,7 +3325,8 @@ 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 +3444,7 @@ .Case("xsavec", HasXSAVEC) .Case("xsaves", HasXSAVES) .Case("xsaveopt", HasXSAVEOPT) + .Case("pku", HasPKU) .Default(false); } Index: E:/Work/LLVM/LLVMClang/tools/clang/lib/Headers/CMakeLists.txt =================================================================== --- E:/Work/LLVM/LLVMClang/tools/clang/lib/Headers/CMakeLists.txt +++ E:/Work/LLVM/LLVMClang/tools/clang/lib/Headers/CMakeLists.txt @@ -12,6 +12,7 @@ avx512vlintrin.h avx512dqintrin.h avx512vldqintrin.h + pkuintrin.h avxintrin.h bmi2intrin.h bmiintrin.h Index: E:/Work/LLVM/LLVMClang/tools/clang/lib/Headers/immintrin.h =================================================================== --- E:/Work/LLVM/LLVMClang/tools/clang/lib/Headers/immintrin.h +++ E:/Work/LLVM/LLVMClang/tools/clang/lib/Headers/immintrin.h @@ -79,6 +79,8 @@ #include +#include + static __inline__ int __attribute__((__always_inline__, __nodebug__, __target__("rdrnd"))) _rdrand16_step(unsigned short *__p) { Index: E:/Work/LLVM/LLVMClang/tools/clang/lib/Headers/pkuintrin.h =================================================================== --- E:/Work/LLVM/LLVMClang/tools/clang/lib/Headers/pkuintrin.h +++ E:/Work/LLVM/LLVMClang/tools/clang/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 Index: E:/Work/LLVM/LLVMClang/tools/clang/test/CodeGen/pku.c =================================================================== --- E:/Work/LLVM/LLVMClang/tools/clang/test/CodeGen/pku.c +++ E:/Work/LLVM/LLVMClang/tools/clang/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 ; +}