Skip to content

Commit f1d3fe7

Browse files
authored
Add basic -mtune support (#98517)
Initial implementation for the -mtune flag in Flang. This PR is a clean version of PR #96688, which is a re-land of PR #95043
1 parent 266a784 commit f1d3fe7

File tree

26 files changed

+186
-14
lines changed

26 files changed

+186
-14
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5456,6 +5456,7 @@ def module_file_info : Flag<["-"], "module-file-info">, Flags<[]>,
54565456
HelpText<"Provide information about a particular module file">;
54575457
def mthumb : Flag<["-"], "mthumb">, Group<m_Group>;
54585458
def mtune_EQ : Joined<["-"], "mtune=">, Group<m_Group>,
5459+
Visibility<[ClangOption, FlangOption]>,
54595460
HelpText<"Only supported on AArch64, PowerPC, RISC-V, SPARC, SystemZ, and X86">;
54605461
def multi__module : Flag<["-"], "multi_module">;
54615462
def multiply__defined__unused : Separate<["-"], "multiply_defined_unused">;
@@ -6783,9 +6784,6 @@ def emit_hlfir : Flag<["-"], "emit-hlfir">, Group<Action_Group>,
67836784

67846785
let Visibility = [CC1Option, CC1AsOption] in {
67856786

6786-
def tune_cpu : Separate<["-"], "tune-cpu">,
6787-
HelpText<"Tune for a specific cpu type">,
6788-
MarshallingInfoString<TargetOpts<"TuneCPU">>;
67896787
def target_abi : Separate<["-"], "target-abi">,
67906788
HelpText<"Target a particular ABI type">,
67916789
MarshallingInfoString<TargetOpts<"ABI">>;
@@ -6812,6 +6810,9 @@ def darwin_target_variant_triple : Separate<["-"], "darwin-target-variant-triple
68126810

68136811
let Visibility = [CC1Option, CC1AsOption, FC1Option] in {
68146812

6813+
def tune_cpu : Separate<["-"], "tune-cpu">,
6814+
HelpText<"Tune for a specific cpu type">,
6815+
MarshallingInfoString<TargetOpts<"TuneCPU">>;
68156816
def target_cpu : Separate<["-"], "target-cpu">,
68166817
HelpText<"Target a specific cpu type">,
68176818
MarshallingInfoString<TargetOpts<"CPU">>;

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "llvm/Frontend/Debug/Options.h"
1616
#include "llvm/Support/FileSystem.h"
1717
#include "llvm/Support/Path.h"
18+
#include "llvm/TargetParser/Host.h"
1819
#include "llvm/TargetParser/RISCVISAInfo.h"
1920
#include "llvm/TargetParser/RISCVTargetParser.h"
2021

@@ -419,6 +420,13 @@ void Flang::addTargetOptions(const ArgList &Args,
419420
}
420421

421422
// TODO: Add target specific flags, ABI, mtune option etc.
423+
if (const Arg *A = Args.getLastArg(options::OPT_mtune_EQ)) {
424+
CmdArgs.push_back("-tune-cpu");
425+
if (A->getValue() == StringRef{"native"})
426+
CmdArgs.push_back(Args.MakeArgString(llvm::sys::getHostCPUName()));
427+
else
428+
CmdArgs.push_back(A->getValue());
429+
}
422430
}
423431

424432
void Flang::addOffloadOptions(Compilation &C, const InputInfoList &Inputs,
@@ -815,7 +823,7 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,
815823
case CodeGenOptions::FramePointerKind::None:
816824
FPKeepKindStr = "-mframe-pointer=none";
817825
break;
818-
case CodeGenOptions::FramePointerKind::Reserved:
826+
case CodeGenOptions::FramePointerKind::Reserved:
819827
FPKeepKindStr = "-mframe-pointer=reserved";
820828
break;
821829
case CodeGenOptions::FramePointerKind::NonLeaf:

flang/include/flang/Frontend/TargetOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class TargetOptions {
3232
/// If given, the name of the target CPU to generate code for.
3333
std::string cpu;
3434

35+
/// If given, the name of the target CPU to tune code for.
36+
std::string cpuToTuneFor;
37+
3538
/// The list of target specific features to enable or disable, as written on
3639
/// the command line.
3740
std::vector<std::string> featuresAsWritten;

flang/include/flang/Lower/Bridge.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ class LoweringBridge {
6565
const Fortran::lower::LoweringOptions &loweringOptions,
6666
const std::vector<Fortran::lower::EnvironmentDefault> &envDefaults,
6767
const Fortran::common::LanguageFeatureControl &languageFeatures,
68-
const llvm::TargetMachine &targetMachine) {
68+
const llvm::TargetMachine &targetMachine, llvm::StringRef tuneCPU) {
6969
return LoweringBridge(ctx, semanticsContext, defaultKinds, intrinsics,
7070
targetCharacteristics, allCooked, triple, kindMap,
7171
loweringOptions, envDefaults, languageFeatures,
72-
targetMachine);
72+
targetMachine, tuneCPU);
7373
}
7474

7575
//===--------------------------------------------------------------------===//
@@ -148,7 +148,7 @@ class LoweringBridge {
148148
const Fortran::lower::LoweringOptions &loweringOptions,
149149
const std::vector<Fortran::lower::EnvironmentDefault> &envDefaults,
150150
const Fortran::common::LanguageFeatureControl &languageFeatures,
151-
const llvm::TargetMachine &targetMachine);
151+
const llvm::TargetMachine &targetMachine, const llvm::StringRef tuneCPU);
152152
LoweringBridge() = delete;
153153
LoweringBridge(const LoweringBridge &) = delete;
154154

flang/include/flang/Optimizer/CodeGen/CGPasses.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ def FIRToLLVMLowering : Pass<"fir-to-llvm-ir", "mlir::ModuleOp"> {
3131
"Override module's data layout.">,
3232
Option<"forcedTargetCPU", "target-cpu", "std::string", /*default=*/"",
3333
"Override module's target CPU.">,
34+
Option<"forcedTuneCPU", "tune-cpu", "std::string", /*default=*/"",
35+
"Override module's tune CPU.">,
3436
Option<"forcedTargetFeatures", "target-features", "std::string",
3537
/*default=*/"", "Override module's target features.">,
3638
Option<"applyTBAA", "apply-tbaa", "bool", /*default=*/"false",
@@ -68,6 +70,8 @@ def TargetRewritePass : Pass<"target-rewrite", "mlir::ModuleOp"> {
6870
"Override module's target triple.">,
6971
Option<"forcedTargetCPU", "target-cpu", "std::string", /*default=*/"",
7072
"Override module's target CPU.">,
73+
Option<"forcedTuneCPU", "tune-cpu", "std::string", /*default=*/"",
74+
"Override module's tune CPU.">,
7175
Option<"forcedTargetFeatures", "target-features", "std::string",
7276
/*default=*/"", "Override module's target features.">,
7377
Option<"noCharacterConversion", "no-character-conversion",

flang/include/flang/Optimizer/CodeGen/Target.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,29 @@ class CodeGenSpecifics {
7676
llvm::StringRef targetCPU, mlir::LLVM::TargetFeaturesAttr targetFeatures,
7777
const mlir::DataLayout &dl);
7878

79+
static std::unique_ptr<CodeGenSpecifics>
80+
get(mlir::MLIRContext *ctx, llvm::Triple &&trp, KindMapping &&kindMap,
81+
llvm::StringRef targetCPU, mlir::LLVM::TargetFeaturesAttr targetFeatures,
82+
const mlir::DataLayout &dl, llvm::StringRef tuneCPU);
83+
7984
static TypeAndAttr getTypeAndAttr(mlir::Type t) { return TypeAndAttr{t, {}}; }
8085

8186
CodeGenSpecifics(mlir::MLIRContext *ctx, llvm::Triple &&trp,
8287
KindMapping &&kindMap, llvm::StringRef targetCPU,
8388
mlir::LLVM::TargetFeaturesAttr targetFeatures,
8489
const mlir::DataLayout &dl)
8590
: context{*ctx}, triple{std::move(trp)}, kindMap{std::move(kindMap)},
86-
targetCPU{targetCPU}, targetFeatures{targetFeatures}, dataLayout{&dl} {}
91+
targetCPU{targetCPU}, targetFeatures{targetFeatures}, dataLayout{&dl},
92+
tuneCPU{""} {}
93+
94+
CodeGenSpecifics(mlir::MLIRContext *ctx, llvm::Triple &&trp,
95+
KindMapping &&kindMap, llvm::StringRef targetCPU,
96+
mlir::LLVM::TargetFeaturesAttr targetFeatures,
97+
const mlir::DataLayout &dl, llvm::StringRef tuneCPU)
98+
: context{*ctx}, triple{std::move(trp)}, kindMap{std::move(kindMap)},
99+
targetCPU{targetCPU}, targetFeatures{targetFeatures}, dataLayout{&dl},
100+
tuneCPU{tuneCPU} {}
101+
87102
CodeGenSpecifics() = delete;
88103
virtual ~CodeGenSpecifics() {}
89104

@@ -165,6 +180,7 @@ class CodeGenSpecifics {
165180
virtual unsigned char getCIntTypeWidth() const = 0;
166181

167182
llvm::StringRef getTargetCPU() const { return targetCPU; }
183+
llvm::StringRef getTuneCPU() const { return tuneCPU; }
168184

169185
mlir::LLVM::TargetFeaturesAttr getTargetFeatures() const {
170186
return targetFeatures;
@@ -182,6 +198,7 @@ class CodeGenSpecifics {
182198
llvm::StringRef targetCPU;
183199
mlir::LLVM::TargetFeaturesAttr targetFeatures;
184200
const mlir::DataLayout *dataLayout = nullptr;
201+
llvm::StringRef tuneCPU;
185202
};
186203

187204
} // namespace fir

flang/include/flang/Optimizer/Dialect/Support/FIRContext.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ void setTargetCPU(mlir::ModuleOp mod, llvm::StringRef cpu);
5858
/// Get the target CPU string from the Module or return a null reference.
5959
llvm::StringRef getTargetCPU(mlir::ModuleOp mod);
6060

61+
/// Set the tune CPU for the module. `cpu` must not be deallocated while
62+
/// module `mod` is still live.
63+
void setTuneCPU(mlir::ModuleOp mod, llvm::StringRef cpu);
64+
65+
/// Get the tune CPU string from the Module or return a null reference.
66+
llvm::StringRef getTuneCPU(mlir::ModuleOp mod);
67+
6168
/// Set the target features for the module.
6269
void setTargetFeatures(mlir::ModuleOp mod, llvm::StringRef features);
6370

flang/include/flang/Optimizer/Transforms/Passes.td

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,10 @@ def FunctionAttr : Pass<"function-attr", "mlir::func::FuncOp"> {
411411
Option<"unsafeFPMath", "unsafe-fp-math",
412412
"bool", /*default=*/"false",
413413
"Set the unsafe-fp-math attribute on functions in the module.">,
414-
];
414+
Option<"tuneCPU", "tune-cpu",
415+
"llvm::StringRef", /*default=*/"llvm::StringRef{}",
416+
"Set the tune-cpu attribute on functions in the module.">,
417+
];
415418
}
416419

417420
def AssumedRankOpConversion : Pass<"fir-assumed-rank-op", "mlir::ModuleOp"> {

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,10 @@ static void parseTargetArgs(TargetOptions &opts, llvm::opt::ArgList &args) {
431431
args.getLastArg(clang::driver::options::OPT_target_cpu))
432432
opts.cpu = a->getValue();
433433

434+
if (const llvm::opt::Arg *a =
435+
args.getLastArg(clang::driver::options::OPT_tune_cpu))
436+
opts.cpuToTuneFor = a->getValue();
437+
434438
for (const llvm::opt::Arg *currentArg :
435439
args.filtered(clang::driver::options::OPT_target_feature))
436440
opts.featuresAsWritten.emplace_back(currentArg->getValue());

flang/lib/Frontend/FrontendActions.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,8 @@ bool CodeGenAction::beginSourceFileAction() {
297297
ci.getParsing().allCooked(), ci.getInvocation().getTargetOpts().triple,
298298
kindMap, ci.getInvocation().getLoweringOpts(),
299299
ci.getInvocation().getFrontendOpts().envDefaults,
300-
ci.getInvocation().getFrontendOpts().features, targetMachine);
300+
ci.getInvocation().getFrontendOpts().features, targetMachine,
301+
ci.getInvocation().getTargetOpts().cpuToTuneFor);
301302

302303
// Fetch module from lb, so we can set
303304
mlirModule = std::make_unique<mlir::ModuleOp>(lb.getModule());

0 commit comments

Comments
 (0)