Skip to content

Commit 23d7a6c

Browse files
authored
[flang][Driver] Support -print-supported-cpus and associated aliases (#117199)
The aliases are -mcpu=help and -mtune=help. There is still an issue with the output which prints an example line that references clang. That is not fixed here because it is printed in llvm/MC/SubtargetInfo.cpp. Some more thought is needed to determine how best to handle this. Fixes #117010
1 parent 7530e70 commit 23d7a6c

File tree

7 files changed

+111
-12
lines changed

7 files changed

+111
-12
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5888,12 +5888,24 @@ def target : Joined<["--"], "target=">, Flags<[NoXarchOption]>,
58885888
def darwin_target_variant : Separate<["-"], "darwin-target-variant">,
58895889
Flags<[NoXarchOption]>, Visibility<[ClangOption, CLOption]>,
58905890
HelpText<"Generate code for an additional runtime variant of the deployment target">;
5891+
5892+
//===----------------------------------------------------------------------===//
5893+
// Print CPU info options (clang, clang-cl, flang)
5894+
//===----------------------------------------------------------------------===//
5895+
5896+
let Visibility = [ClangOption, CC1Option, CLOption, FlangOption, FC1Option] in {
5897+
58915898
def print_supported_cpus : Flag<["-", "--"], "print-supported-cpus">,
58925899
Group<CompileOnly_Group>,
5893-
Visibility<[ClangOption, CC1Option, CLOption]>,
5894-
HelpText<"Print supported cpu models for the given target (if target is not specified,"
5895-
" it will print the supported cpus for the default target)">,
5900+
HelpText<"Print supported cpu models for the given target (if target is not "
5901+
"specified,it will print the supported cpus for the default target)">,
58965902
MarshallingInfoFlag<FrontendOpts<"PrintSupportedCPUs">>;
5903+
5904+
def : Flag<["-"], "mcpu=help">, Alias<print_supported_cpus>;
5905+
def : Flag<["-"], "mtune=help">, Alias<print_supported_cpus>;
5906+
5907+
} // let Visibility = [ClangOption, CC1Option, CLOption, FlangOption, FC1Option]
5908+
58975909
def print_supported_extensions : Flag<["-", "--"], "print-supported-extensions">,
58985910
Visibility<[ClangOption, CC1Option, CLOption]>,
58995911
HelpText<"Print supported -march extensions (RISC-V, AArch64 and ARM only)">,
@@ -5903,8 +5915,6 @@ def print_enabled_extensions : Flag<["-", "--"], "print-enabled-extensions">,
59035915
HelpText<"Print the extensions enabled by the given target and -march/-mcpu options."
59045916
" (AArch64 and RISC-V only)">,
59055917
MarshallingInfoFlag<FrontendOpts<"PrintEnabledExtensions">>;
5906-
def : Flag<["-"], "mcpu=help">, Alias<print_supported_cpus>;
5907-
def : Flag<["-"], "mtune=help">, Alias<print_supported_cpus>;
59085918
def time : Flag<["-"], "time">,
59095919
HelpText<"Time individual commands">;
59105920
def traditional_cpp : Flag<["-", "--"], "traditional-cpp">,

clang/lib/Driver/Driver.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4417,7 +4417,8 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
44174417

44184418
// Use the -mcpu=? flag as the dummy input to cc1.
44194419
Actions.clear();
4420-
Action *InputAc = C.MakeAction<InputAction>(*A, types::TY_C);
4420+
Action *InputAc = C.MakeAction<InputAction>(
4421+
*A, IsFlangMode() ? types::TY_Fortran : types::TY_C);
44214422
Actions.push_back(
44224423
C.MakeAction<PrecompileJobAction>(InputAc, types::TY_Nothing));
44234424
for (auto &I : Inputs)
@@ -6621,8 +6622,8 @@ bool Driver::ShouldUseFlangCompiler(const JobAction &JA) const {
66216622
return false;
66226623

66236624
// And say "no" if this is not a kind of action flang understands.
6624-
if (!isa<PreprocessJobAction>(JA) && !isa<CompileJobAction>(JA) &&
6625-
!isa<BackendJobAction>(JA))
6625+
if (!isa<PreprocessJobAction>(JA) && !isa<PrecompileJobAction>(JA) &&
6626+
!isa<CompileJobAction>(JA) && !isa<BackendJobAction>(JA))
66266627
return false;
66276628

66286629
return true;

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,9 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,
747747
}
748748
} else if (isa<AssembleJobAction>(JA)) {
749749
CmdArgs.push_back("-emit-obj");
750+
} else if (isa<PrecompileJobAction>(JA)) {
751+
// The precompile job action is only needed for options such as -mcpu=help.
752+
// Those will already have been handled by the fc1 driver.
750753
} else {
751754
assert(false && "Unexpected action class for Flang tool.");
752755
}
@@ -911,8 +914,6 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,
911914
CmdArgs.push_back(Output.getFilename());
912915
}
913916

914-
assert(Input.isFilename() && "Invalid input.");
915-
916917
if (Args.getLastArg(options::OPT_save_temps_EQ))
917918
Args.AddLastArg(CmdArgs, options::OPT_save_temps_EQ);
918919

@@ -932,7 +933,18 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,
932933
}
933934
}
934935

935-
CmdArgs.push_back(Input.getFilename());
936+
// The input could be Ty_Nothing when "querying" options such as -mcpu=help
937+
// are used.
938+
ArrayRef<InputInfo> FrontendInputs = Input;
939+
if (Input.isNothing())
940+
FrontendInputs = {};
941+
942+
for (const InputInfo &Input : FrontendInputs) {
943+
if (Input.isFilename())
944+
CmdArgs.push_back(Input.getFilename());
945+
else
946+
Input.getInputArg().renderAsInput(Args, CmdArgs);
947+
}
936948

937949
const char *Exec = Args.MakeArgString(D.GetProgramPath("flang", TC));
938950
C.addCommand(std::make_unique<Command>(JA, *this,

flang/include/flang/Frontend/FrontendOptions.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ class FrontendInputFile {
236236
struct FrontendOptions {
237237
FrontendOptions()
238238
: showHelp(false), showVersion(false), instrumentedParse(false),
239-
showColors(false), needProvenanceRangeToCharBlockMappings(false) {}
239+
showColors(false), printSupportedCPUs(false),
240+
needProvenanceRangeToCharBlockMappings(false) {}
240241

241242
/// Show the -help text.
242243
unsigned showHelp : 1;
@@ -250,6 +251,9 @@ struct FrontendOptions {
250251
/// Enable color diagnostics.
251252
unsigned showColors : 1;
252253

254+
/// Print the supported cpus for the current target
255+
unsigned printSupportedCPUs : 1;
256+
253257
/// Enable Provenance to character-stream mapping. Allows e.g. IDEs to find
254258
/// symbols based on source-code location. This is not needed in regular
255259
/// compilation.

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,8 @@ static bool parseFrontendArgs(FrontendOptions &opts, llvm::opt::ArgList &args,
634634
opts.outputFile = args.getLastArgValue(clang::driver::options::OPT_o);
635635
opts.showHelp = args.hasArg(clang::driver::options::OPT_help);
636636
opts.showVersion = args.hasArg(clang::driver::options::OPT_version);
637+
opts.printSupportedCPUs =
638+
args.hasArg(clang::driver::options::OPT_print_supported_cpus);
637639

638640
// Get the input kind (from the value passed via `-x`)
639641
InputKind dashX(Language::Unknown);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
! Test --print-supported-cpus and associated aliases, -mcpu=help and
2+
! -mtune=help
3+
4+
! RUN: %if x86-registered-target %{ \
5+
! RUN: %flang --target=x86_64-unknown-linux-gnu --print-supported-cpus 2>&1 \
6+
! RUN: | FileCheck %s --check-prefixes=X86,CHECK \
7+
! RUN: %}
8+
! RUN: %if x86-registered-target %{ \
9+
! RUN: %flang --target=x86_64-unknown-linux-gnu -mcpu=help 2>&1 \
10+
! RUN: | FileCheck %s --check-prefixes=X86,CHECK \
11+
! RUN: %}
12+
! RUN: %if x86-registered-target %{ \
13+
! RUN: %flang --target=x86_64-unknown-linux-gnu -mtune=help 2>&1 \
14+
! RUN: | FileCheck %s --check-prefixes=X86,CHECK \
15+
! RUN: %}
16+
17+
! RUN: %if aarch64-registered-target %{ \
18+
! RUN: %flang --target=aarch64-unknown-linux-gnu --print-supported-cpus 2>&1 \
19+
! RUN: | FileCheck %s --check-prefixes=AARCH64,CHECK \
20+
! RUN: %}
21+
! RUN: %if x86-registered-target %{ \
22+
! RUN: %flang --target=aarch64-unknown-linux-gnu -mcpu=help 2>&1 \
23+
! RUN: | FileCheck %s --check-prefixes=AARCH64,CHECK \
24+
! RUN: %}
25+
! RUN: %if x86-registered-target %{ \
26+
! RUN: %flang --target=aarch64-unknown-linux-gnu -mtune=help 2>&1 \
27+
! RUN: | FileCheck %s --check-prefixes=AARCH64,CHECK \
28+
! RUN: %}
29+
30+
! CHECK-NOT: warning: argument unused during compilation
31+
32+
! X86: Target: x86_64-unknown-linux-gnu
33+
! X86: corei7
34+
35+
! AARCH64: Target: aarch64-unknown-linux-gnu
36+
! AARCH64: cortex-a73
37+
! AARCH64: cortex-a75
38+
39+
! The footer displayed contains a reference to clang. This should be changed to
40+
! flang, but that requires a change in llvm/MCSubtargetInfo. When that happens,
41+
! this test will need to be updated and this comment can be removed.
42+
43+
! CHECK: Use -mcpu or -mtune to specify the target's processor.
44+
! CHECK: For example, clang --target=aarch64-unknown-linux-gnu
45+
46+
end program

flang/tools/flang-driver/fc1_main.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,35 @@
2121
#include "flang/Frontend/TextDiagnosticBuffer.h"
2222
#include "flang/FrontendTool/Utils.h"
2323
#include "clang/Driver/DriverDiagnostic.h"
24+
#include "llvm/MC/TargetRegistry.h"
2425
#include "llvm/Option/Arg.h"
2526
#include "llvm/Option/ArgList.h"
2627
#include "llvm/Option/OptTable.h"
2728
#include "llvm/Support/TargetSelect.h"
29+
#include "llvm/Support/raw_ostream.h"
2830

2931
#include <cstdio>
3032

3133
using namespace Fortran::frontend;
3234

35+
/// Print supported cpus of the given target.
36+
static int printSupportedCPUs(llvm::StringRef triple) {
37+
std::string error;
38+
const llvm::Target *target =
39+
llvm::TargetRegistry::lookupTarget(triple, error);
40+
if (!target) {
41+
llvm::errs() << error;
42+
return 1;
43+
}
44+
45+
// the target machine will handle the mcpu printing
46+
llvm::TargetOptions targetOpts;
47+
std::unique_ptr<llvm::TargetMachine> targetMachine(
48+
target->createTargetMachine(triple, "", "+cpuhelp", targetOpts,
49+
std::nullopt));
50+
return 0;
51+
}
52+
3353
int fc1_main(llvm::ArrayRef<const char *> argv, const char *argv0) {
3454
// Create CompilerInstance
3555
std::unique_ptr<CompilerInstance> flang(new CompilerInstance());
@@ -58,6 +78,10 @@ int fc1_main(llvm::ArrayRef<const char *> argv, const char *argv0) {
5878
llvm::InitializeAllTargetMCs();
5979
llvm::InitializeAllAsmPrinters();
6080

81+
// --print-supported-cpus takes priority over the actual compilation.
82+
if (flang->getFrontendOpts().printSupportedCPUs)
83+
return printSupportedCPUs(flang->getInvocation().getTargetOpts().triple);
84+
6185
diagsBuffer->flushDiagnostics(flang->getDiagnostics());
6286

6387
if (!success)

0 commit comments

Comments
 (0)