Skip to content

Commit 62ca791

Browse files
committed
[X86][1/2] Support PREFETCHI instructions
For more details about these instructions, please refer to the latest ISE document: https://www.intel.com/content/www/us/en/develop/download/intel-architecture-instruction-set-extensions-programming-reference.html Reviewed By: craig.topper Differential Revision: https://reviews.llvm.org/D136040
1 parent 859b614 commit 62ca791

File tree

22 files changed

+203
-2
lines changed

22 files changed

+203
-2
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,7 @@ X86 Support in Clang
587587
- Support ``-mindirect-branch-cs-prefix`` for call and jmp to indirect thunk.
588588
- Fix 32-bit ``__fastcall`` and ``__vectorcall`` ABI mismatch with MSVC.
589589
- Switch ``AVX512-BF16`` intrinsics types from ``short`` to ``__bf16``.
590+
- Add support for ``PREFETCHI`` instructions.
590591

591592
DWARF Support in Clang
592593
----------------------

clang/include/clang/Basic/BuiltinsX86_64.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ TARGET_BUILTIN(__builtin_ia32_tdpbuud, "vIUcIUcIUc", "n", "amx-int8")
133133
TARGET_BUILTIN(__builtin_ia32_tdpbf16ps, "vIUcIUcIUc", "n", "amx-bf16")
134134
TARGET_BUILTIN(__builtin_ia32_ptwrite64, "vUOi", "n", "ptwrite")
135135

136+
TARGET_BUILTIN(__builtin_ia32_prefetchi, "vvC*Ui", "nc", "prefetchi")
137+
136138
#undef BUILTIN
137139
#undef TARGET_BUILTIN
138140
#undef TARGET_HEADER_BUILTIN

clang/include/clang/Driver/Options.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4647,6 +4647,8 @@ def mpconfig : Flag<["-"], "mpconfig">, Group<m_x86_Features_Group>;
46474647
def mno_pconfig : Flag<["-"], "mno-pconfig">, Group<m_x86_Features_Group>;
46484648
def mpopcnt : Flag<["-"], "mpopcnt">, Group<m_x86_Features_Group>;
46494649
def mno_popcnt : Flag<["-"], "mno-popcnt">, Group<m_x86_Features_Group>;
4650+
def mprefetchi : Flag<["-"], "mprefetchi">, Group<m_x86_Features_Group>;
4651+
def mno_prefetchi : Flag<["-"], "mno-prefetchi">, Group<m_x86_Features_Group>;
46504652
def mprefetchwt1 : Flag<["-"], "mprefetchwt1">, Group<m_x86_Features_Group>;
46514653
def mno_prefetchwt1 : Flag<["-"], "mno-prefetchwt1">, Group<m_x86_Features_Group>;
46524654
def mprfchw : Flag<["-"], "mprfchw">, Group<m_x86_Features_Group>;

clang/lib/Basic/Targets/X86.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@ bool X86TargetInfo::handleTargetFeatures(std::vector<std::string> &Features,
290290
HasCLWB = true;
291291
} else if (Feature == "+wbnoinvd") {
292292
HasWBNOINVD = true;
293+
} else if (Feature == "+prefetchi") {
294+
HasPREFETCHI = true;
293295
} else if (Feature == "+prefetchwt1") {
294296
HasPREFETCHWT1 = true;
295297
} else if (Feature == "+clzero") {
@@ -738,6 +740,8 @@ void X86TargetInfo::getTargetDefines(const LangOptions &Opts,
738740
Builder.defineMacro("__SHSTK__");
739741
if (HasSGX)
740742
Builder.defineMacro("__SGX__");
743+
if (HasPREFETCHI)
744+
Builder.defineMacro("__PREFETCHI__");
741745
if (HasPREFETCHWT1)
742746
Builder.defineMacro("__PREFETCHWT1__");
743747
if (HasCLZERO)
@@ -929,6 +933,7 @@ bool X86TargetInfo::isValidFeatureName(StringRef Name) const {
929933
.Case("pconfig", true)
930934
.Case("pku", true)
931935
.Case("popcnt", true)
936+
.Case("prefetchi", true)
932937
.Case("prefetchwt1", true)
933938
.Case("prfchw", true)
934939
.Case("ptwrite", true)
@@ -1025,6 +1030,7 @@ bool X86TargetInfo::hasFeature(StringRef Feature) const {
10251030
.Case("pconfig", HasPCONFIG)
10261031
.Case("pku", HasPKU)
10271032
.Case("popcnt", HasPOPCNT)
1033+
.Case("prefetchi", HasPREFETCHI)
10281034
.Case("prefetchwt1", HasPREFETCHWT1)
10291035
.Case("prfchw", HasPRFCHW)
10301036
.Case("ptwrite", HasPTWRITE)

clang/lib/Basic/Targets/X86.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ class LLVM_LIBRARY_VISIBILITY X86TargetInfo : public TargetInfo {
123123
bool HasCLFLUSHOPT = false;
124124
bool HasCLWB = false;
125125
bool HasMOVBE = false;
126+
bool HasPREFETCHI = false;
126127
bool HasPREFETCHWT1 = false;
127128
bool HasRDPID = false;
128129
bool HasRDPRU = false;

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15479,6 +15479,11 @@ Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID,
1547915479
static constexpr int Mask[] = {0, 5, 6, 7};
1548015480
return Builder.CreateShuffleVector(Call, Ops[2], Mask);
1548115481
}
15482+
case X86::BI__builtin_ia32_prefetchi:
15483+
return Builder.CreateCall(
15484+
CGM.getIntrinsic(Intrinsic::prefetch, Ops[0]->getType()),
15485+
{Ops[0], llvm::ConstantInt::get(Int32Ty, 0), Ops[1],
15486+
llvm::ConstantInt::get(Int32Ty, 0)});
1548215487
}
1548315488
}
1548415489

clang/lib/Headers/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ set(x86_files
174174
pkuintrin.h
175175
pmmintrin.h
176176
popcntintrin.h
177+
prfchiintrin.h
177178
prfchwintrin.h
178179
ptwriteintrin.h
179180
rdpruintrin.h

clang/lib/Headers/cpuid.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@
204204
#define bit_AVX512BF16 0x00000020
205205
#define bit_HRESET 0x00400000
206206

207+
/* Features in %edx for leaf 7 sub-leaf 1 */
208+
#define bit_PREFETCHI 0x00004000
209+
207210
/* Features in %eax for leaf 13 sub-leaf 1 */
208211
#define bit_XSAVEOPT 0x00000001
209212
#define bit_XSAVEC 0x00000002

clang/lib/Headers/prfchiintrin.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*===---- prfchiintrin.h - PREFETCHI intrinsic -----------------------------===
2+
*
3+
* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
* See https://llvm.org/LICENSE.txt for license information.
5+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
*
7+
*===-----------------------------------------------------------------------===
8+
*/
9+
10+
#ifndef __PRFCHIINTRIN_H
11+
#define __PRFCHIINTRIN_H
12+
13+
#ifdef __x86_64__
14+
15+
/* Define the default attributes for the functions in this file. */
16+
#define __DEFAULT_FN_ATTRS \
17+
__attribute__((__always_inline__, __nodebug__, __target__("prefetchi")))
18+
19+
/// Loads an instruction sequence containing the specified memory address into
20+
/// all level cache.
21+
///
22+
/// Note that the effect of this intrinsic is dependent on the processor
23+
/// implementation.
24+
///
25+
/// \headerfile <x86intrin.h>
26+
///
27+
/// This intrinsic corresponds to the \c PREFETCHIT0 instruction.
28+
///
29+
/// \param __P
30+
/// A pointer specifying the memory address to be prefetched.
31+
static __inline__ void __DEFAULT_FN_ATTRS
32+
_m_prefetchit0(volatile const void *__P) {
33+
#pragma clang diagnostic push
34+
#pragma clang diagnostic ignored "-Wcast-qual"
35+
__builtin_ia32_prefetchi((const void *)__P, 3 /* _MM_HINT_T0 */);
36+
#pragma clang diagnostic pop
37+
}
38+
39+
/// Loads an instruction sequence containing the specified memory address into
40+
/// all but the first-level cache.
41+
///
42+
/// Note that the effect of this intrinsic is dependent on the processor
43+
/// implementation.
44+
///
45+
/// \headerfile <x86intrin.h>
46+
///
47+
/// This intrinsic corresponds to the \c PREFETCHIT1 instruction.
48+
///
49+
/// \param __P
50+
/// A pointer specifying the memory address to be prefetched.
51+
static __inline__ void __DEFAULT_FN_ATTRS
52+
_m_prefetchit1(volatile const void *__P) {
53+
#pragma clang diagnostic push
54+
#pragma clang diagnostic ignored "-Wcast-qual"
55+
__builtin_ia32_prefetchi((const void *)__P, 2 /* _MM_HINT_T1 */);
56+
#pragma clang diagnostic pop
57+
}
58+
#endif /* __x86_64__ */
59+
#undef __DEFAULT_FN_ATTRS
60+
61+
#endif /* __PRFCHWINTRIN_H */

clang/lib/Headers/x86gprintrin.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
#include <crc32intrin.h>
2626
#endif
2727

28+
#if !(defined(_MSC_VER) || defined(__SCE__)) || __has_feature(modules) || \
29+
defined(__PRFCHI__)
30+
#include <prfchiintrin.h>
31+
#endif
32+
2833
#if defined(__i386__)
2934
#define __SAVE_GPRBX "mov {%%ebx, %%eax |eax, ebx};"
3035
#define __RESTORE_GPRBX "mov {%%eax, %%ebx |ebx, eax};"

0 commit comments

Comments
 (0)