Skip to content

Commit fd6faee

Browse files
authored
[Driver,CodeGen] Report error when enabling 64-bit-only features on non-64-bit arch (#101151)
In front-end, now we detect for `-mapx-features=/-mapxf` and `-muintr`, which is aligned with GCC https://gcc.gnu.org/bugzilla/attachment.cgi?id=58698&action=diff In backend, we just disable these 64-bit-only features silently, so that there is no error for `-march=native -m32` on APX-supported arch. llvm-issue: #94810 GCC thread: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115978
1 parent 7231776 commit fd6faee

File tree

4 files changed

+52
-11
lines changed

4 files changed

+52
-11
lines changed

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -266,19 +266,29 @@ void x86::getX86TargetFeatures(const Driver &D, const llvm::Triple &Triple,
266266
}
267267

268268
bool IsNegative = Name.starts_with("no-");
269+
270+
bool Not64Bit = ArchType != llvm::Triple::x86_64;
271+
if (Not64Bit && Name == "uintr")
272+
D.Diag(diag::err_drv_unsupported_opt_for_target)
273+
<< A->getSpelling() << Triple.getTriple();
274+
269275
if (A->getOption().matches(options::OPT_mapx_features_EQ) ||
270276
A->getOption().matches(options::OPT_mno_apx_features_EQ)) {
271277

278+
if (Not64Bit && !IsNegative)
279+
D.Diag(diag::err_drv_unsupported_opt_for_target)
280+
<< StringRef(A->getSpelling().str() + "|-mapxf")
281+
<< Triple.getTriple();
282+
272283
for (StringRef Value : A->getValues()) {
273-
if (Value == "egpr" || Value == "push2pop2" || Value == "ppx" ||
274-
Value == "ndd" || Value == "ccmp" || Value == "nf" ||
275-
Value == "cf" || Value == "zu") {
276-
Features.push_back(
277-
Args.MakeArgString((IsNegative ? "-" : "+") + Value));
278-
continue;
279-
}
280-
D.Diag(clang::diag::err_drv_unsupported_option_argument)
281-
<< A->getSpelling() << Value;
284+
if (Value != "egpr" && Value != "push2pop2" && Value != "ppx" &&
285+
Value != "ndd" && Value != "ccmp" && Value != "nf" &&
286+
Value != "cf" && Value != "zu")
287+
D.Diag(clang::diag::err_drv_unsupported_option_argument)
288+
<< A->getSpelling() << Value;
289+
290+
Features.push_back(
291+
Args.MakeArgString((IsNegative ? "-" : "+") + Value));
282292
}
283293
continue;
284294
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@
309309
// HRESET: "-target-feature" "+hreset"
310310
// NO-HRESET: "-target-feature" "-hreset"
311311

312-
// RUN: %clang --target=i386 -march=i386 -muintr %s -### 2>&1 | FileCheck -check-prefix=UINTR %s
313-
// RUN: %clang --target=i386 -march=i386 -mno-uintr %s -### 2>&1 | FileCheck -check-prefix=NO-UINTR %s
312+
// RUN: %clang --target=x86_64 -muintr %s -### 2>&1 | FileCheck -check-prefix=UINTR %s
313+
// RUN: %clang --target=x86_64 -mno-uintr %s -### 2>&1 | FileCheck -check-prefix=NO-UINTR %s
314314
// UINTR: "-target-feature" "+uintr"
315315
// NO-UINTR: "-target-feature" "-uintr"
316316

@@ -409,6 +409,15 @@
409409
// NONX86-NEXT: warning: argument unused during compilation: '-msse4.2' [-Wunused-command-line-argument]
410410
// NONX86-NEXT: error: unsupported option '-mno-sgx' for target 'aarch64'
411411

412+
// RUN: not %clang -### --target=i386 -muintr %s 2>&1 | FileCheck --check-prefix=NON-UINTR %s
413+
// RUN: %clang -### --target=i386 -mno-uintr %s 2>&1 > /dev/null
414+
// RUN: not %clang -### --target=i386 -mapx-features=ndd %s 2>&1 | FileCheck --check-prefix=NON-APX %s
415+
// RUN: not %clang -### --target=i386 -mapxf %s 2>&1 | FileCheck --check-prefix=NON-APX %s
416+
// RUN: %clang -### --target=i386 -mno-apxf %s 2>&1 > /dev/null
417+
// NON-UINTR: error: unsupported option '-muintr' for target 'i386'
418+
// NON-APX: error: unsupported option '-mapx-features=|-mapxf' for target 'i386'
419+
// NON-APX-NOT: error: {{.*}} -mapx-features=
420+
412421
// RUN: %clang --target=i386 -march=i386 -mharden-sls=return %s -### -o %t.o 2>&1 | FileCheck -check-prefixes=SLS-RET,NO-SLS %s
413422
// RUN: %clang --target=i386 -march=i386 -mharden-sls=indirect-jmp %s -### -o %t.o 2>&1 | FileCheck -check-prefixes=SLS-IJMP,NO-SLS %s
414423
// RUN: %clang --target=i386 -march=i386 -mharden-sls=none -mharden-sls=all %s -### -o %t.o 2>&1 | FileCheck -check-prefixes=SLS-IJMP,SLS-RET %s

llvm/lib/Target/X86/X86Subtarget.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,13 @@ void X86Subtarget::initSubtargetFeatures(StringRef CPU, StringRef TuneCPU,
279279
FullFS += ",+evex512";
280280
}
281281

282+
// Disable 64-bit only features in non-64-bit mode.
283+
SmallVector<StringRef, 9> FeaturesIn64BitOnly = {
284+
"egpr", "push2pop2", "ppx", "ndd", "ccmp", "nf", "cf", "zu", "uintr"};
285+
if (FullFS.find("-64bit-mode") != std::string::npos)
286+
llvm::for_each(FeaturesIn64BitOnly,
287+
[&](StringRef F) { FullFS += ",-" + F.str(); });
288+
282289
// Parse features string and set the CPU.
283290
ParseSubtargetFeatures(CPU, TuneCPU, FullFS);
284291

llvm/test/CodeGen/X86/apx/i386-ndd.ll

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
2+
; RUN: llc -mtriple=i386-linux-gnu -mattr=+cmov,+ndd < %s | FileCheck %s
3+
define i32 @test(i1 %cmp, i32 %x, i32 %y) nounwind {
4+
; CHECK-LABEL: test:
5+
; CHECK: # %bb.0: # %entry
6+
; CHECK-NEXT: testb $1, {{[0-9]+}}(%esp)
7+
; CHECK-NEXT: leal {{[0-9]+}}(%esp), %eax
8+
; CHECK-NEXT: leal {{[0-9]+}}(%esp), %ecx
9+
; CHECK-NEXT: cmovnel %eax, %ecx
10+
; CHECK-NEXT: movl (%ecx), %eax
11+
; CHECK-NEXT: retl
12+
entry:
13+
%cmov = select i1 %cmp, i32 %x, i32 %y
14+
ret i32 %cmov
15+
}

0 commit comments

Comments
 (0)