Skip to content

Commit 4cbceb7

Browse files
committed
[X86] Add basic support for -mtune command line option in clang
Building on the backend support from D85165. This parses the command line option in the driver, passes it on to CC1 and adds a function attribute. -Still need to support tune on the target attribute. -Need to use "generic" as the tuning by default. But need to change generic in the backend first. -Need to set tune if march is specified and mtune isn't. -May need to disable getHostCPUName's ability to guess CPU name from features when it doesn't have a family/model match for mtune=native. That's what gcc appears to do. Differential Revision: https://reviews.llvm.org/D85384
1 parent 2f01785 commit 4cbceb7

File tree

7 files changed

+64
-1
lines changed

7 files changed

+64
-1
lines changed

clang/include/clang/Basic/TargetOptions.h

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

38+
/// If given, the name of the target CPU to tune code for.
39+
std::string TuneCPU;
40+
3841
/// If given, the unit to use for floating point math.
3942
std::string FPMath;
4043

clang/include/clang/Driver/Options.td

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2715,7 +2715,7 @@ def module_file_info : Flag<["-"], "module-file-info">, Flags<[DriverOption,CC1O
27152715
HelpText<"Provide information about a particular module file">;
27162716
def mthumb : Flag<["-"], "mthumb">, Group<m_Group>;
27172717
def mtune_EQ : Joined<["-"], "mtune=">, Group<m_Group>,
2718-
HelpText<"Accepted for compatibility with GCC. Currently has no effect.">;
2718+
HelpText<"Only supported on X86. Otherwise accepted for compatibility with GCC.">;
27192719
def multi__module : Flag<["-"], "multi_module">;
27202720
def multiply__defined__unused : Separate<["-"], "multiply_defined_unused">;
27212721
def multiply__defined : Separate<["-"], "multiply_defined">;
@@ -3490,6 +3490,8 @@ let Flags = [CC1Option, CC1AsOption, NoDriverOption] in {
34903490

34913491
def target_cpu : Separate<["-"], "target-cpu">,
34923492
HelpText<"Target a specific cpu type">;
3493+
def tune_cpu : Separate<["-"], "tune-cpu">,
3494+
HelpText<"Tune for a specific cpu type">;
34933495
def target_feature : Separate<["-"], "target-feature">,
34943496
HelpText<"Target specific attributes">;
34953497
def triple : Separate<["-"], "triple">,

clang/lib/Basic/Targets.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,16 @@ TargetInfo::CreateTargetInfo(DiagnosticsEngine &Diags,
652652
return nullptr;
653653
}
654654

655+
// Check the TuneCPU name if specified.
656+
if (!Opts->TuneCPU.empty() && !Target->isValidCPUName(Opts->TuneCPU)) {
657+
Diags.Report(diag::err_target_unknown_cpu) << Opts->TuneCPU;
658+
SmallVector<StringRef, 32> ValidList;
659+
Target->fillValidCPUList(ValidList);
660+
if (!ValidList.empty())
661+
Diags.Report(diag::note_valid_options) << llvm::join(ValidList, ", ");
662+
return nullptr;
663+
}
664+
655665
// Set the target ABI if specified.
656666
if (!Opts->ABI.empty() && !Target->setABI(Opts->ABI)) {
657667
Diags.Report(diag::err_target_unknown_abi) << Opts->ABI;

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1749,6 +1749,7 @@ bool CodeGenModule::GetCPUAndFeaturesAttributes(GlobalDecl GD,
17491749
// we have a decl for the function and it has a target attribute then
17501750
// parse that and add it to the feature set.
17511751
StringRef TargetCPU = getTarget().getTargetOpts().CPU;
1752+
StringRef TuneCPU = getTarget().getTargetOpts().TuneCPU;
17521753
std::vector<std::string> Features;
17531754
const auto *FD = dyn_cast_or_null<FunctionDecl>(GD.getDecl());
17541755
FD = FD ? FD->getMostRecentDecl() : FD;
@@ -1783,6 +1784,10 @@ bool CodeGenModule::GetCPUAndFeaturesAttributes(GlobalDecl GD,
17831784
Attrs.addAttribute("target-cpu", TargetCPU);
17841785
AddedAttr = true;
17851786
}
1787+
if (TuneCPU != "") {
1788+
Attrs.addAttribute("tune-cpu", TuneCPU);
1789+
AddedAttr = true;
1790+
}
17861791
if (!Features.empty()) {
17871792
llvm::sort(Features);
17881793
Attrs.addAttribute("target-features", llvm::join(Features, ","));

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "llvm/Support/Compiler.h"
4040
#include "llvm/Support/Compression.h"
4141
#include "llvm/Support/FileSystem.h"
42+
#include "llvm/Support/Host.h"
4243
#include "llvm/Support/Path.h"
4344
#include "llvm/Support/Process.h"
4445
#include "llvm/Support/TargetParser.h"
@@ -2071,6 +2072,18 @@ void Clang::AddX86TargetArgs(const ArgList &Args,
20712072
CmdArgs.push_back("soft");
20722073
CmdArgs.push_back("-mstack-alignment=4");
20732074
}
2075+
2076+
// Handle -mtune.
2077+
// FIXME: We should default to "generic" unless -march is set to match gcc.
2078+
if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)) {
2079+
StringRef Name = A->getValue();
2080+
2081+
if (Name == "native")
2082+
Name = llvm::sys::getHostCPUName();
2083+
2084+
CmdArgs.push_back("-tune-cpu");
2085+
CmdArgs.push_back(Args.MakeArgString(Name));
2086+
}
20742087
}
20752088

20762089
void Clang::AddHexagonTargetArgs(const ArgList &Args,

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3658,6 +3658,7 @@ static void ParseTargetArgs(TargetOptions &Opts, ArgList &Args,
36583658
Opts.EABIVersion = EABIVersion;
36593659
}
36603660
Opts.CPU = std::string(Args.getLastArgValue(OPT_target_cpu));
3661+
Opts.TuneCPU = std::string(Args.getLastArgValue(OPT_tune_cpu));
36613662
Opts.FPMath = std::string(Args.getLastArgValue(OPT_mfpmath));
36623663
Opts.FeaturesAsWritten = Args.getAllArgValues(OPT_target_feature);
36633664
Opts.LinkerVersion =

clang/test/Misc/target-invalid-cpu-note.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
// AARCH64: note: valid target CPU values are:
99
// AARCH64-SAME: cortex-a35,
1010

11+
// RUN: not %clang_cc1 -triple arm64--- -tune-cpu not-a-cpu -fsyntax-only %s 2>&1 | FileCheck %s --check-prefix TUNE_AARCH64
12+
// TUNE_AARCH64: error: unknown target CPU 'not-a-cpu'
13+
// TUNE_AARCH64: note: valid target CPU values are:
14+
// TUNE_AARCH64-SAME: cortex-a35,
15+
1116
// RUN: not %clang_cc1 -triple i386--- -target-cpu not-a-cpu -fsyntax-only %s 2>&1 | FileCheck %s --check-prefix X86
1217
// X86: error: unknown target CPU 'not-a-cpu'
1318
// X86: note: valid target CPU values are: i386, i486, winchip-c6, winchip2, c3,
@@ -32,6 +37,30 @@
3237
// X86_64-SAME: athlon64-sse3, opteron-sse3, amdfam10, barcelona, btver1,
3338
// X86_64-SAME: btver2, bdver1, bdver2, bdver3, bdver4, znver1, znver2, x86-64
3439

40+
// RUN: not %clang_cc1 -triple i386--- -tune-cpu not-a-cpu -fsyntax-only %s 2>&1 | FileCheck %s --check-prefix TUNE_X86
41+
// TUNE_X86: error: unknown target CPU 'not-a-cpu'
42+
// TUNE_X86: note: valid target CPU values are: i386, i486, winchip-c6, winchip2, c3,
43+
// TUNE_X86-SAME: i586, pentium, pentium-mmx, pentiumpro, i686, pentium2, pentium3,
44+
// TUNE_X86-SAME: pentium3m, pentium-m, c3-2, yonah, pentium4, pentium4m, prescott,
45+
// TUNE_X86-SAME: nocona, core2, penryn, bonnell, atom, silvermont, slm, goldmont, goldmont-plus, tremont,
46+
// TUNE_X86-SAME: nehalem, corei7, westmere, sandybridge, corei7-avx, ivybridge,
47+
// TUNE_X86-SAME: core-avx-i, haswell, core-avx2, broadwell, skylake, skylake-avx512,
48+
// TUNE_X86-SAME: skx, cascadelake, cooperlake, cannonlake, icelake-client, icelake-server, tigerlake, knl, knm, lakemont, k6, k6-2, k6-3,
49+
// TUNE_X86-SAME: athlon, athlon-tbird, athlon-xp, athlon-mp, athlon-4, k8, athlon64,
50+
// TUNE_X86-SAME: athlon-fx, opteron, k8-sse3, athlon64-sse3, opteron-sse3, amdfam10,
51+
// TUNE_X86-SAME: barcelona, btver1, btver2, bdver1, bdver2, bdver3, bdver4, znver1, znver2,
52+
// TUNE_X86-SAME: x86-64, geode
53+
54+
// RUN: not %clang_cc1 -triple x86_64--- -tune-cpu not-a-cpu -fsyntax-only %s 2>&1 | FileCheck %s --check-prefix TUNE_X86_64
55+
// TUNE_X86_64: error: unknown target CPU 'not-a-cpu'
56+
// TUNE_X86_64: note: valid target CPU values are: nocona, core2, penryn, bonnell,
57+
// TUNE_X86_64-SAME: atom, silvermont, slm, goldmont, goldmont-plus, tremont, nehalem, corei7, westmere,
58+
// TUNE_X86_64-SAME: sandybridge, corei7-avx, ivybridge, core-avx-i, haswell,
59+
// TUNE_X86_64-SAME: core-avx2, broadwell, skylake, skylake-avx512, skx, cascadelake, cooperlake, cannonlake,
60+
// TUNE_X86_64-SAME: icelake-client, icelake-server, tigerlake, knl, knm, k8, athlon64, athlon-fx, opteron, k8-sse3,
61+
// TUNE_X86_64-SAME: athlon64-sse3, opteron-sse3, amdfam10, barcelona, btver1,
62+
// TUNE_X86_64-SAME: btver2, bdver1, bdver2, bdver3, bdver4, znver1, znver2, x86-64
63+
3564
// RUN: not %clang_cc1 -triple nvptx--- -target-cpu not-a-cpu -fsyntax-only %s 2>&1 | FileCheck %s --check-prefix NVPTX
3665
// NVPTX: error: unknown target CPU 'not-a-cpu'
3766
// NVPTX: note: valid target CPU values are: sm_20, sm_21, sm_30, sm_32, sm_35,

0 commit comments

Comments
 (0)