Skip to content

[flang] Check x86 CPU and issue diagnostics #127950

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
d9bfa81
WIP
eugeneepshteyn Feb 19, 2025
79442c3
Try to not enable x86 CPU check for "-x cuda". This implementation di…
eugeneepshteyn Feb 19, 2025
696e808
New attempt, still doesn't work
eugeneepshteyn Feb 19, 2025
078c35d
Merge branch 'llvm:main' into flang-x86-check-cpu
eugeneepshteyn Feb 19, 2025
e1a112e
Reverted printSupportedCPUs() to original.
eugeneepshteyn Feb 20, 2025
d56f63b
Removed debug code, updated comments
eugeneepshteyn Feb 20, 2025
b432279
Reformatted
eugeneepshteyn Feb 20, 2025
313a897
clang-format
eugeneepshteyn Feb 20, 2025
fc2c35f
Merge branch 'llvm:main' into flang-x86-check-cpu
eugeneepshteyn Feb 20, 2025
5426481
Fixed artifact of attempted refactoring
eugeneepshteyn Feb 20, 2025
43a54b8
Formatter fix
eugeneepshteyn Feb 20, 2025
8089143
LIT test
eugeneepshteyn Feb 20, 2025
51aa228
Merge branch 'llvm:main' into flang-x86-check-cpu
eugeneepshteyn Feb 21, 2025
4044303
Merge branch 'llvm:main' into flang-x86-check-cpu
eugeneepshteyn Feb 24, 2025
15f546a
Merge branch 'llvm:main' into flang-x86-check-cpu
eugeneepshteyn Feb 24, 2025
e0e7d36
Merge branch 'llvm:main' into flang-x86-check-cpu
eugeneepshteyn Feb 25, 2025
6254e16
Merge branch 'llvm:main' into flang-x86-check-cpu
eugeneepshteyn Feb 26, 2025
f889634
Merge branch 'llvm:main' into flang-x86-check-cpu
eugeneepshteyn Feb 26, 2025
2c39e34
Merge branch 'llvm:main' into flang-x86-check-cpu
eugeneepshteyn Feb 27, 2025
c377011
Merge branch 'llvm:main' into flang-x86-check-cpu
eugeneepshteyn Feb 27, 2025
eb6f592
Merge branch 'llvm:main' into flang-x86-check-cpu
eugeneepshteyn Mar 5, 2025
4cde3bd
Merge branch 'llvm:main' into flang-x86-check-cpu
eugeneepshteyn Mar 6, 2025
7a33337
Merge branch 'llvm:main' into flang-x86-check-cpu
eugeneepshteyn Mar 6, 2025
a04a1d9
Merge branch 'llvm:main' into flang-x86-check-cpu
eugeneepshteyn Mar 7, 2025
4048e37
Merge branch 'llvm:main' into flang-x86-check-cpu
eugeneepshteyn Mar 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions flang/test/Driver/target-cpu-invalid-x86.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
! Test that invalid cpu is rejected.

! REQUIRES: x86-registered-target

! RUN: not %flang_fc1 -triple x86_64-unknown-linux-gnu -target-cpu not_valid_cpu -o - -S %s 2>&1 | FileCheck %s

! CHECK: error: unknown target CPU 'not_valid_cpu'
36 changes: 36 additions & 0 deletions flang/tools/flang-driver/fc1_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
#include "flang/Frontend/TextDiagnosticBuffer.h"
#include "flang/FrontendTool/Utils.h"
#include "clang/Driver/DriverDiagnostic.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Option/Arg.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Option/OptTable.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/TargetParser/X86TargetParser.h"

#include <cstdio>

Expand All @@ -50,6 +52,35 @@ static int printSupportedCPUs(llvm::StringRef triple) {
return 0;
}

/// Check that given CPU is valid for given target.
static bool checkSupportedCPU(clang::DiagnosticsEngine &diags,
llvm::StringRef str_cpu,
llvm::StringRef str_triple) {

llvm::Triple triple{str_triple};

// Need to check for empty CPU string, because it can be empty for some
// cases, e.g., "-x cuda" compilation.
if (triple.getArch() == llvm::Triple::x86_64 && !str_cpu.empty()) {
constexpr bool only64bit{true};
llvm::X86::CPUKind x86cpu = llvm::X86::parseArchX86(str_cpu, only64bit);
if (x86cpu == llvm::X86::CK_None) {
diags.Report(clang::diag::err_target_unknown_cpu) << str_cpu;
llvm::SmallVector<llvm::StringRef, 32> validList;
llvm::X86::fillValidCPUArchList(validList, only64bit);
if (!validList.empty())
diags.Report(clang::diag::note_valid_options)
<< llvm::join(validList, ", ");

return false;
}
}

// TODO: only support check for x86_64 for now. Anything else is considered
// as "supported".
return true;
}

int fc1_main(llvm::ArrayRef<const char *> argv, const char *argv0) {
// Create CompilerInstance
std::unique_ptr<CompilerInstance> flang(new CompilerInstance());
Expand Down Expand Up @@ -82,6 +113,11 @@ int fc1_main(llvm::ArrayRef<const char *> argv, const char *argv0) {
if (flang->getFrontendOpts().printSupportedCPUs)
return printSupportedCPUs(flang->getInvocation().getTargetOpts().triple);

// Check that requested CPU can be properly supported
success = success &&
checkSupportedCPU(diags, flang->getInvocation().getTargetOpts().cpu,
flang->getInvocation().getTargetOpts().triple);

diagsBuffer->flushDiagnostics(flang->getDiagnostics());

if (!success)
Expand Down
Loading