Skip to content

Commit 664a0c5

Browse files
author
Tarun Prabhu
committed
[flang][Driver] Support -print-supported-cpus and associated aliases
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 14bdcef commit 664a0c5

File tree

8 files changed

+100
-10
lines changed

8 files changed

+100
-10
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5890,7 +5890,7 @@ def darwin_target_variant : Separate<["-"], "darwin-target-variant">,
58905890
HelpText<"Generate code for an additional runtime variant of the deployment target">;
58915891
def print_supported_cpus : Flag<["-", "--"], "print-supported-cpus">,
58925892
Group<CompileOnly_Group>,
5893-
Visibility<[ClangOption, CC1Option, CLOption]>,
5893+
Visibility<[ClangOption, CC1Option, CLOption, FlangOption, FC1Option]>,
58945894
HelpText<"Print supported cpu models for the given target (if target is not specified,"
58955895
" it will print the supported cpus for the default target)">,
58965896
MarshallingInfoFlag<FrontendOpts<"PrintSupportedCPUs">>;
@@ -5903,8 +5903,10 @@ def print_enabled_extensions : Flag<["-", "--"], "print-enabled-extensions">,
59035903
HelpText<"Print the extensions enabled by the given target and -march/-mcpu options."
59045904
" (AArch64 and RISC-V only)">,
59055905
MarshallingInfoFlag<FrontendOpts<"PrintEnabledExtensions">>;
5906-
def : Flag<["-"], "mcpu=help">, Alias<print_supported_cpus>;
5907-
def : Flag<["-"], "mtune=help">, Alias<print_supported_cpus>;
5906+
def : Flag<["-"], "mcpu=help">, Alias<print_supported_cpus>,
5907+
Visibility<[ClangOption, CC1Option, CLOption, FlangOption, FC1Option]>;
5908+
def : Flag<["-"], "mtune=help">, Alias<print_supported_cpus>,
5909+
Visibility<[ClangOption, CC1Option, CLOption, FlangOption, FC1Option]>;
59085910
def time : Flag<["-"], "time">,
59095911
HelpText<"Time individual commands">;
59105912
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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
! Test --print-supported-cpus and associated aliases, -mcpu=help and
2+
! -mtune=help on AArch64.
3+
4+
! REQUIRES: aarch64-registered-target
5+
6+
! RUN: %flang --target=aarch64-unknown-linux-gnu --print-supported-cpus 2>&1 | \
7+
! RUN: FileCheck %s
8+
! RUN: %flang --target=aarch64-unknown-linux-gnu -mcpu=help 2>&1 | \
9+
! RUN: FileCheck %s
10+
! RUN: %flang --target=aarch64-unknown-linux-gnu -mtune=help 2>&1 | \
11+
! RUN: FileCheck %s
12+
13+
! CHECK-NOT: warning: argument unused during compilation
14+
15+
! CHECK: Target: aarch64-unknown-linux-gnu
16+
! CHECK: cortex-a73
17+
! CHECK: cortex-a75
18+
19+
! TODO: This is a line that is printed at the end of the output. The full line
20+
! also includes an example that references clang. That needs to be fixed and a
21+
! a check added here to make sure that it references flang, not clang.
22+
23+
! CHECK: Use -mcpu or -mtune to specify the target's processor.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
! Test --print-supported-cpus and associated aliases, -mcpu=help and
2+
! -mtune=help on X86.
3+
4+
! REQUIRES: x86-registered-target
5+
6+
! RUN: %flang --target=x86_64-unknown-linux-gnu --print-supported-cpus 2>&1 | \
7+
! RUN: FileCheck %s
8+
! RUN: %flang --target=x86_64-unknown-linux-gnu -mcpu=help 2>&1 | \
9+
! RUN: FileCheck %s
10+
! RUN: %flang --target=x86_64-unknown-linux-gnu -mtune=help 2>&1 | \
11+
! RUN: FileCheck %s
12+
13+
! CHECK-NOT: warning: argument unused during compilation
14+
15+
! CHECK: Target: x86_64-unknown-linux-gnu
16+
! CHECK: corei7
17+
18+
! TODO: This is a line that is printed at the end of the output. The full line
19+
! also includes an example that references clang. That needs to be fixed and a
20+
! a check added here to make sure that it references flang, not clang.
21+
22+
! CHECK: Use -mcpu or -mtune to specify the target's processor.

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)