Skip to content

Commit 6d6baef

Browse files
authored
[X86] Support CFE flags for APX features (#74199)
Positive options: -mapx-features=<comma-separated-features> Negative options: -mno-apx-features=<comma-separated-features> -m[no-]apx-features is designed to be able to control separate APX features. Besides, we also support the flag -m[no-]apxf, which can be used like an alias of -m[no-]apx-features=< all APX features covered by CPUID APX_F> Behaviour when positive and negative options are used together: For boolean flags, the last one wins -mapxf -mno-apxf -> -mno-apxf -mno-apxf -mapxf -> -mapxf For flags that take a set as arguments, it sets the mask by order of the flags -mapx-features=egpr,ndd -mno-apx-features=egpr -> -egpr,+ndd -mapx-features=egpr -mno-apx-features=egpr,ndd -> -egpr,-ndd -mno-apx-features=egpr -mapx-features=egpr,ndd -> +egpr,+ndd -mno-apx-features=egpr,ndd -mapx-features=egpr -> -ndd,+egpr The design is aligned with gcc https://gcc.gnu.org/pipermail/gcc-patches/2023-August/628905.html
1 parent 853682c commit 6d6baef

File tree

10 files changed

+140
-0
lines changed

10 files changed

+140
-0
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5999,6 +5999,15 @@ def mno_gather : Flag<["-"], "mno-gather">, Group<m_Group>,
59995999
HelpText<"Disable generation of gather instructions in auto-vectorization(x86 only)">;
60006000
def mno_scatter : Flag<["-"], "mno-scatter">, Group<m_Group>,
60016001
HelpText<"Disable generation of scatter instructions in auto-vectorization(x86 only)">;
6002+
def mapx_features_EQ : CommaJoined<["-"], "mapx-features=">, Group<m_x86_Features_Group>,
6003+
HelpText<"Enable features of APX">, Values<"egpr,push2pop2,ppx,ndd,ccmp,cf">;
6004+
def mno_apx_features_EQ : CommaJoined<["-"], "mno-apx-features=">, Group<m_x86_Features_Group>,
6005+
HelpText<"Disable features of APX">, Values<"egpr,push2pop2,ppx,ndd,ccmp,cf">;
6006+
// Features egpr, push2pop2 and ppx are validated with llvm-test-suite && cpu2017 on Intel SDE.
6007+
// For stability, we turn on these features only for -mapxf. After a feature pass the validation,
6008+
// we will add it to -mapxf.
6009+
def mapxf : Flag<["-"], "mapxf">, Alias<mapx_features_EQ>, AliasArgs<["egpr","push2pop2","ppx"]>;
6010+
def mno_apxf : Flag<["-"], "mno-apxf">, Alias<mno_apx_features_EQ>, AliasArgs<["egpr","push2pop2","ppx"]>;
60026011
} // let Flags = [TargetSpecific]
60036012

60046013
// VE feature flags

clang/lib/Basic/Targets/X86.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,18 @@ bool X86TargetInfo::handleTargetFeatures(std::vector<std::string> &Features,
428428
HasX87 = true;
429429
} else if (Feature == "+fullbf16") {
430430
HasFullBFloat16 = true;
431+
} else if (Feature == "+egpr") {
432+
HasEGPR = true;
433+
} else if (Feature == "+push2pop2") {
434+
HasPush2Pop2 = true;
435+
} else if (Feature == "+ppx") {
436+
HasPPX = true;
437+
} else if (Feature == "+ndd") {
438+
HasNDD = true;
439+
} else if (Feature == "+ccmp") {
440+
HasCCMP = true;
441+
} else if (Feature == "+cf") {
442+
HasCF = true;
431443
}
432444

433445
X86SSEEnum Level = llvm::StringSwitch<X86SSEEnum>(Feature)
@@ -927,6 +939,18 @@ void X86TargetInfo::getTargetDefines(const LangOptions &Opts,
927939
Builder.defineMacro("__USERMSR__");
928940
if (HasCRC32)
929941
Builder.defineMacro("__CRC32__");
942+
if (HasEGPR)
943+
Builder.defineMacro("__EGPR__");
944+
if (HasPush2Pop2)
945+
Builder.defineMacro("__PUSH2POP2__");
946+
if (HasPPX)
947+
Builder.defineMacro("__PPX__");
948+
if (HasNDD)
949+
Builder.defineMacro("__NDD__");
950+
if (HasCCMP)
951+
Builder.defineMacro("__CCMP__");
952+
if (HasCF)
953+
Builder.defineMacro("__CF__");
930954

931955
// Each case falls through to the previous one here.
932956
switch (SSELevel) {
@@ -1122,6 +1146,12 @@ bool X86TargetInfo::isValidFeatureName(StringRef Name) const {
11221146
.Case("xsavec", true)
11231147
.Case("xsaves", true)
11241148
.Case("xsaveopt", true)
1149+
.Case("egpr", true)
1150+
.Case("push2pop2", true)
1151+
.Case("ppx", true)
1152+
.Case("ndd", true)
1153+
.Case("ccmp", true)
1154+
.Case("cf", true)
11251155
.Default(false);
11261156
}
11271157

@@ -1238,6 +1268,12 @@ bool X86TargetInfo::hasFeature(StringRef Feature) const {
12381268
.Case("xsaves", HasXSAVES)
12391269
.Case("xsaveopt", HasXSAVEOPT)
12401270
.Case("fullbf16", HasFullBFloat16)
1271+
.Case("egpr", HasEGPR)
1272+
.Case("push2pop2", HasPush2Pop2)
1273+
.Case("ppx", HasPPX)
1274+
.Case("ndd", HasNDD)
1275+
.Case("ccmp", HasCCMP)
1276+
.Case("cf", HasCF)
12411277
.Default(false);
12421278
}
12431279

clang/lib/Basic/Targets/X86.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ class LLVM_LIBRARY_VISIBILITY X86TargetInfo : public TargetInfo {
168168
bool HasUINTR = false;
169169
bool HasCRC32 = false;
170170
bool HasX87 = false;
171+
bool HasEGPR = false;
172+
bool HasPush2Pop2 = false;
173+
bool HasPPX = false;
174+
bool HasNDD = false;
175+
bool HasCCMP = false;
176+
bool HasCF = false;
171177

172178
protected:
173179
llvm::X86::CPUKind CPU = llvm::X86::CK_None;

clang/lib/Driver/ToolChains/Arch/X86.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,21 @@ void x86::getX86TargetFeatures(const Driver &D, const llvm::Triple &Triple,
270270
}
271271

272272
bool IsNegative = Name.startswith("no-");
273+
if (A->getOption().matches(options::OPT_mapx_features_EQ) ||
274+
A->getOption().matches(options::OPT_mno_apx_features_EQ)) {
275+
276+
for (StringRef Value : A->getValues()) {
277+
if (Value == "egpr" || Value == "push2pop2" || Value == "ppx" ||
278+
Value == "ndd" || Value == "ccmp" || Value == "cf") {
279+
Features.push_back(
280+
Args.MakeArgString((IsNegative ? "-" : "+") + Value));
281+
continue;
282+
}
283+
D.Diag(clang::diag::err_drv_unsupported_option_argument)
284+
<< A->getSpelling() << Value;
285+
}
286+
continue;
287+
}
273288
if (IsNegative)
274289
Name = Name.substr(3);
275290
Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));

clang/test/Driver/x86-target-features.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,3 +422,42 @@
422422

423423
// RUN: touch %t.o
424424
// RUN: %clang -fdriver-only -Werror --target=x86_64-pc-linux-gnu -mharden-sls=all %t.o -o /dev/null 2>&1 | count 0
425+
426+
// RUN: %clang -target x86_64-unknown-linux-gnu -mapxf %s -### -o %t.o 2>&1 | FileCheck -check-prefix=APXF %s
427+
// RUN: %clang -target x86_64-unknown-linux-gnu -mno-apxf %s -### -o %t.o 2>&1 | FileCheck -check-prefix=NO-APXF %s
428+
// RUN: %clang -target x86_64-unknown-linux-gnu -mno-apxf -mapxf %s -### -o %t.o 2>&1 | FileCheck -check-prefix=APXF %s
429+
// RUN: %clang -target x86_64-unknown-linux-gnu -mapxf -mno-apxf %s -### -o %t.o 2>&1 | FileCheck -check-prefix=NO-APXF %s
430+
//
431+
// APXF: "-target-feature" "+egpr" "-target-feature" "+push2pop2" "-target-feature" "+ppx"
432+
// NO-APXF: "-target-feature" "-egpr" "-target-feature" "-push2pop2" "-target-feature" "-ppx"
433+
434+
// RUN: %clang -target x86_64-unknown-linux-gnu -mapx-features=egpr %s -### -o %t.o 2>&1 | FileCheck -check-prefix=EGPR %s
435+
// RUN: %clang -target x86_64-unknown-linux-gnu -mapx-features=push2pop2 %s -### -o %t.o 2>&1 | FileCheck -check-prefix=PUSH2POP2 %s
436+
// RUN: %clang -target x86_64-unknown-linux-gnu -mapx-features=ppx %s -### -o %t.o 2>&1 | FileCheck -check-prefix=PPX %s
437+
// RUN: %clang -target x86_64-unknown-linux-gnu -mapx-features=ndd %s -### -o %t.o 2>&1 | FileCheck -check-prefix=NDD %s
438+
// RUN: %clang -target x86_64-unknown-linux-gnu -mapx-features=ccmp %s -### -o %t.o 2>&1 | FileCheck -check-prefix=CCMP %s
439+
// RUN: %clang -target x86_64-unknown-linux-gnu -mapx-features=cf %s -### -o %t.o 2>&1 | FileCheck -check-prefix=CF %s
440+
// EGPR: "-target-feature" "+egpr"
441+
// PUSH2POP2: "-target-feature" "+push2pop2"
442+
// PPX: "-target-feature" "+ppx"
443+
// NDD: "-target-feature" "+ndd"
444+
// CCMP: "-target-feature" "+ccmp"
445+
// CF: "-target-feature" "+cf"
446+
447+
// RUN: %clang -target x86_64-unknown-linux-gnu -mapx-features=egpr,ndd %s -### -o %t.o 2>&1 | FileCheck -check-prefix=EGPR-NDD %s
448+
// RUN: %clang -target x86_64-unknown-linux-gnu -mapx-features=egpr -mapx-features=ndd %s -### -o %t.o 2>&1 | FileCheck -check-prefix=EGPR-NDD %s
449+
// RUN: %clang -target x86_64-unknown-linux-gnu -mno-apx-features=egpr -mno-apx-features=ndd %s -### -o %t.o 2>&1 | FileCheck -check-prefix=NO-EGPR-NO-NDD %s
450+
// RUN: %clang -target x86_64-unknown-linux-gnu -mno-apx-features=egpr -mapx-features=egpr,ndd %s -### -o %t.o 2>&1 | FileCheck -check-prefix=EGPR-NDD %s
451+
// RUN: %clang -target x86_64-unknown-linux-gnu -mno-apx-features=egpr,ndd -mapx-features=egpr %s -### -o %t.o 2>&1 | FileCheck -check-prefix=EGPR-NO-NDD %s
452+
// RUN: %clang -target x86_64-unknown-linux-gnu -mapx-features=egpr,ndd -mno-apx-features=egpr %s -### -o %t.o 2>&1 | FileCheck -check-prefix=NO-EGPR-NDD %s
453+
// RUN: %clang -target x86_64-unknown-linux-gnu -mapx-features=egpr -mno-apx-features=egpr,ndd %s -### -o %t.o 2>&1 | FileCheck -check-prefix=NO-EGPR-NO-NDD %s
454+
//
455+
// EGPR-NDD: "-target-feature" "+egpr" "-target-feature" "+ndd"
456+
// EGPR-NO-NDD: "-target-feature" "-ndd" "-target-feature" "+egpr"
457+
// NO-EGPR-NDD: "-target-feature" "+ndd" "-target-feature" "-egpr"
458+
// NO-EGPR-NO-NDD: "-target-feature" "-egpr" "-target-feature" "-ndd"
459+
460+
// RUN: not %clang -target x86_64-unknown-linux-gnu -mapx-features=egpr,foo,bar %s -### -o %t.o 2>&1 | FileCheck -check-prefix=ERROR %s
461+
//
462+
// ERROR: unsupported argument 'foo' to option '-mapx-features='
463+
// ERROR: unsupported argument 'bar' to option '-mapx-features='

clang/test/Preprocessor/x86_target_features.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,3 +796,17 @@
796796
// RUN: %clang -target i386-unknown-linux-gnu -march=i386 -mno-rdpru -x c -E -dM -o - %s | FileCheck -check-prefix=NORDPRU %s
797797

798798
// NORDPRU-NOT: #define __RDPRU__ 1
799+
800+
// RUN: %clang -target x86_64-unknown-unknown -march=x86-64 -mapx-features=egpr -x c -E -dM -o - %s | FileCheck --check-prefix=EGPR %s
801+
// RUN: %clang -target x86_64-unknown-unknown -march=x86-64 -mapx-features=push2pop2 -x c -E -dM -o - %s | FileCheck --check-prefix=PUSH2POP2 %s
802+
// RUN: %clang -target x86_64-unknown-unknown -march=x86-64 -mapx-features=ppx -x c -E -dM -o - %s | FileCheck --check-prefix=PPX %s
803+
// RUN: %clang -target x86_64-unknown-unknown -march=x86-64 -mapx-features=ndd -x c -E -dM -o - %s | FileCheck --check-prefix=NDD %s
804+
// RUN: %clang -target x86_64-unknown-unknown -march=x86-64 -mapx-features=ccmp -x c -E -dM -o - %s | FileCheck --check-prefix=CCMP %s
805+
// RUN: %clang -target x86_64-unknown-unknown -march=x86-64 -mapx-features=cf -x c -E -dM -o - %s | FileCheck --check-prefix=CF %s
806+
// RUN: %clang -target x86_64-unknown-unknown -march=x86-64 -mapxf -x c -E -dM -o - %s | FileCheck --check-prefixes=EGPR,PUSH2POP2,PPX %s
807+
// EGPR: #define __EGPR__ 1
808+
// PPX: #define __PPX__ 1
809+
// PUSH2POP2: #define __PUSH2POP2__ 1
810+
// NDD: #define __NDD__ 1
811+
// CCMP: #define __CCMP__ 1
812+
// CF: #define __CF__ 1

llvm/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ Changes to the X86 Backend
156156
* Support ISA of ``USER_MSR``.
157157
* Support ISA of ``AVX10.1-256`` and ``AVX10.1-512``.
158158
* ``-mcpu=pantherlake`` and ``-mcpu=clearwaterforest`` are now supported.
159+
* ``-mapxf`` is supported.
159160

160161
Changes to the OCaml bindings
161162
-----------------------------

llvm/include/llvm/TargetParser/X86TargetParser.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,12 @@ X86_FEATURE (EVEX512, "evex512")
246246
X86_FEATURE (AVX10_1, "avx10.1-256")
247247
X86_FEATURE (AVX10_1_512, "avx10.1-512")
248248
X86_FEATURE (USERMSR, "usermsr")
249+
X86_FEATURE (EGPR, "egpr")
250+
X86_FEATURE (Push2Pop2, "push2pop2")
251+
X86_FEATURE (PPX, "ppx")
252+
X86_FEATURE (NDD, "ndd")
253+
X86_FEATURE (CCMP, "ccmp")
254+
X86_FEATURE (CF, "cf")
249255
// These features aren't really CPU features, but the frontend can set them.
250256
X86_FEATURE (RETPOLINE_EXTERNAL_THUNK, "retpoline-external-thunk")
251257
X86_FEATURE (RETPOLINE_INDIRECT_BRANCHES, "retpoline-indirect-branches")

llvm/lib/Target/X86/X86.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,12 @@ def FeaturePush2Pop2 : SubtargetFeature<"push2pop2", "HasPush2Pop2", "true",
347347
"Support PUSH2/POP2 instructions">;
348348
def FeaturePPX : SubtargetFeature<"ppx", "HasPPX", "true",
349349
"Support Push-Pop Acceleration">;
350+
def FeatureNDD : SubtargetFeature<"ndd", "HasNDD", "true",
351+
"Support non-destructive destination">;
352+
def FeatureCCMP : SubtargetFeature<"ccmp", "HasCCMP", "true",
353+
"Support conditional cmp & test instructions">;
354+
def FeatureCF : SubtargetFeature<"cf", "HasCF", "true",
355+
"Support conditional faulting">;
350356

351357
// Ivy Bridge and newer processors have enhanced REP MOVSB and STOSB (aka
352358
// "string operations"). See "REP String Enhancement" in the Intel Software

llvm/lib/TargetParser/X86TargetParser.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,14 @@ constexpr FeatureBitset ImpliedFeaturesAVX10_1 =
628628
constexpr FeatureBitset ImpliedFeaturesAVX10_1_512 =
629629
FeatureAVX10_1 | FeatureEVEX512;
630630

631+
// APX Features
632+
constexpr FeatureBitset ImpliedFeaturesEGPR = {};
633+
constexpr FeatureBitset ImpliedFeaturesPush2Pop2 = {};
634+
constexpr FeatureBitset ImpliedFeaturesPPX = {};
635+
constexpr FeatureBitset ImpliedFeaturesNDD = {};
636+
constexpr FeatureBitset ImpliedFeaturesCCMP = {};
637+
constexpr FeatureBitset ImpliedFeaturesCF = {};
638+
631639
constexpr FeatureInfo FeatureInfos[X86::CPU_FEATURE_MAX] = {
632640
#define X86_FEATURE(ENUM, STR) {{"+" STR}, ImpliedFeatures##ENUM},
633641
#include "llvm/TargetParser/X86TargetParser.def"

0 commit comments

Comments
 (0)