Skip to content

Commit c56514f

Browse files
committed
Reland "[LoongArch] Support -march=native and -mtune="
As described in [1][2], `-mtune=` is used to select the type of target microarchitecture, defaults to the value of `-march`. The set of possible values should be a superset of `-march` values. Currently possible values of `-march=` and `-mtune=` are `native`, `loongarch64` and `la464`. D136146 has supported `-march={loongarch64,la464}` and this patch adds support for `-march=native` and `-mtune=`. A new ProcessorModel called `loongarch64` is defined in LoongArch.td to support `-mtune=loongarch64`. `llvm::sys::getHostCPUName()` returns `generic` on unknown or future LoongArch CPUs, e.g. the not yet added `la664`, leading to `llvm::LoongArch::isValidArchName()` failing to parse the arch name. In this case, use `loongarch64` as the default arch name for 64-bit CPUs. And these two preprocessor macros are defined: - __loongarch_arch - __loongarch_tune [1]: https://github.com/loongson/LoongArch-Documentation/blob/2023.04.20/docs/LoongArch-toolchain-conventions-EN.adoc [2]: https://github.com/loongson/la-softdev-convention/blob/v0.1/la-softdev-convention.adoc Reviewed By: xen0n, wangleiat Differential Revision: https://reviews.llvm.org/D155824
1 parent 4553dc4 commit c56514f

File tree

13 files changed

+171
-13
lines changed

13 files changed

+171
-13
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ Windows Support
139139
LoongArch Support
140140
^^^^^^^^^^^^^^^^^
141141

142+
- The ``-march=native`` ``-mtune=`` options and ``__loongarch_{arch,tune}``
143+
macros are now supported.
144+
142145
RISC-V Support
143146
^^^^^^^^^^^^^^
144147

clang/lib/Basic/Targets/LoongArch.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include "clang/Basic/MacroBuilder.h"
1616
#include "clang/Basic/TargetBuiltins.h"
1717
#include "llvm/Support/raw_ostream.h"
18-
#include "llvm/TargetParser/TargetParser.h"
18+
#include "llvm/TargetParser/LoongArchTargetParser.h"
1919

2020
using namespace clang;
2121
using namespace clang::targets;
@@ -198,7 +198,19 @@ void LoongArchTargetInfo::getTargetDefines(const LangOptions &Opts,
198198
else
199199
Builder.defineMacro("__loongarch_frlen", "0");
200200

201-
// TODO: define __loongarch_arch and __loongarch_tune.
201+
// Define __loongarch_arch.
202+
StringRef Arch = llvm::LoongArch::getArch();
203+
if (Arch.empty())
204+
Arch = llvm::LoongArch::getDefaultArch(GRLen == 64);
205+
if (!Arch.empty())
206+
Builder.defineMacro("__loongarch_arch", Arch);
207+
208+
// Define __loongarch_tune.
209+
StringRef TuneCPU = llvm::LoongArch::getTuneCPU();
210+
if (TuneCPU.empty())
211+
TuneCPU = Arch;
212+
if (!TuneCPU.empty())
213+
Builder.defineMacro("__loongarch_tune", TuneCPU);
202214

203215
StringRef ABI = getABI();
204216
if (ABI == "lp64d" || ABI == "lp64f" || ABI == "lp64s")
@@ -270,3 +282,12 @@ bool LoongArchTargetInfo::handleTargetFeatures(
270282
}
271283
return true;
272284
}
285+
286+
bool LoongArchTargetInfo::isValidTuneCPUName(StringRef Name) const {
287+
return llvm::LoongArch::isValidTuneCPUName(Name);
288+
}
289+
290+
void LoongArchTargetInfo::fillValidTuneCPUList(
291+
SmallVectorImpl<StringRef> &Values) const {
292+
llvm::LoongArch::fillValidTuneCPUList(Values);
293+
}

clang/lib/Basic/Targets/LoongArch.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ class LLVM_LIBRARY_VISIBILITY LoongArchTargetInfo : public TargetInfo {
8080
const std::vector<std::string> &FeaturesVec) const override;
8181

8282
bool hasFeature(StringRef Feature) const override;
83+
84+
bool isValidTuneCPUName(StringRef Name) const override;
85+
void fillValidTuneCPUList(SmallVectorImpl<StringRef> &Values) const override;
8386
};
8487

8588
class LLVM_LIBRARY_VISIBILITY LoongArch32TargetInfo

clang/lib/Driver/ToolChains/Arch/LoongArch.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "clang/Driver/Driver.h"
1313
#include "clang/Driver/DriverDiagnostic.h"
1414
#include "clang/Driver/Options.h"
15+
#include "llvm/TargetParser/Host.h"
1516
#include "llvm/TargetParser/LoongArchTargetParser.h"
1617

1718
using namespace clang::driver;
@@ -128,21 +129,29 @@ void loongarch::getLoongArchTargetFeatures(const Driver &D,
128129
std::vector<StringRef> &Features) {
129130
StringRef ArchName;
130131
if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
131-
if (!llvm::LoongArch::isValidArchName(A->getValue())) {
132+
ArchName = A->getValue();
133+
134+
// Handle -march=native.
135+
if (ArchName == "native") {
136+
ArchName = llvm::sys::getHostCPUName();
137+
if (ArchName == "generic")
138+
ArchName = llvm::LoongArch::getDefaultArch(Triple.isLoongArch64());
139+
}
140+
141+
if (!llvm::LoongArch::isValidArchName(ArchName)) {
132142
D.Diag(clang::diag::err_drv_invalid_arch_name) << A->getAsString(Args);
133143
return;
134144
}
135-
ArchName = A->getValue();
136145
}
137146

138-
// TODO: handle -march=native and -mtune=xx.
139-
140147
// Select a default arch name.
141-
if (ArchName.empty() && Triple.isLoongArch64())
142-
ArchName = "loongarch64";
148+
if (ArchName.empty())
149+
ArchName = llvm::LoongArch::getDefaultArch(Triple.isLoongArch64());
143150

144-
if (!ArchName.empty())
151+
if (!ArchName.empty()) {
145152
llvm::LoongArch::getArchFeatures(ArchName, Features);
153+
llvm::LoongArch::setArch(ArchName);
154+
}
146155

147156
// Select floating-point features determined by -mdouble-float,
148157
// -msingle-float, -msoft-float and -mfpu.

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#include "llvm/Support/YAMLParser.h"
5757
#include "llvm/TargetParser/ARMTargetParserCommon.h"
5858
#include "llvm/TargetParser/Host.h"
59+
#include "llvm/TargetParser/LoongArchTargetParser.h"
5960
#include "llvm/TargetParser/RISCVTargetParser.h"
6061
#include <cctype>
6162

@@ -1853,10 +1854,25 @@ void Clang::AddAArch64TargetArgs(const ArgList &Args,
18531854

18541855
void Clang::AddLoongArchTargetArgs(const ArgList &Args,
18551856
ArgStringList &CmdArgs) const {
1857+
const llvm::Triple &Triple = getToolChain().getTriple();
1858+
18561859
CmdArgs.push_back("-target-abi");
1857-
CmdArgs.push_back(loongarch::getLoongArchABI(getToolChain().getDriver(), Args,
1858-
getToolChain().getTriple())
1859-
.data());
1860+
CmdArgs.push_back(
1861+
loongarch::getLoongArchABI(getToolChain().getDriver(), Args, Triple)
1862+
.data());
1863+
1864+
// Handle -mtune.
1865+
if (const Arg *A = Args.getLastArg(options::OPT_mtune_EQ)) {
1866+
StringRef TuneCPU = A->getValue();
1867+
if (TuneCPU == "native") {
1868+
TuneCPU = llvm::sys::getHostCPUName();
1869+
if (TuneCPU == "generic")
1870+
TuneCPU = llvm::LoongArch::getDefaultArch(Triple.isLoongArch64());
1871+
}
1872+
CmdArgs.push_back("-tune-cpu");
1873+
CmdArgs.push_back(Args.MakeArgString(TuneCPU));
1874+
llvm::LoongArch::setTuneCPU(TuneCPU);
1875+
}
18601876
}
18611877

18621878
void Clang::AddMIPSTargetArgs(const ArgList &Args,
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: not %clang --target=loongarch64 -mtune=invalidcpu -fsyntax-only %s 2>&1 | FileCheck %s
2+
// RUN: not %clang --target=loongarch64 -mtune=generic -fsyntax-only %s 2>&1 | FileCheck %s
3+
// RUN: not %clang --target=loongarch64 -mtune=generic-la64 -fsyntax-only %s 2>&1 | FileCheck %s
4+
5+
// CHECK: error: unknown target CPU '{{.*}}'
6+
// CHECK-NEXT: note: valid target CPU values are: {{.*}}

clang/test/Driver/loongarch-mtune.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %clang --target=loongarch64 -mtune=loongarch64 -fsyntax-only %s -### 2>&1 | \
2+
// RUN: FileCheck %s --check-prefix=CC1ARG -DCPU=loongarch64
3+
// RUN: %clang --target=loongarch64 -mtune=loongarch64 -S -emit-llvm %s -o - | \
4+
// RUN: FileCheck %s --check-prefix=IRATTR -DCPU=loongarch64
5+
//
6+
// RUN: %clang --target=loongarch64 -mtune=la464 -fsyntax-only %s -### 2>&1 | \
7+
// RUN: FileCheck %s --check-prefix=CC1ARG -DCPU=la464
8+
// RUN: %clang --target=loongarch64 -mtune=la464 -S -emit-llvm %s -o - | \
9+
// RUN: FileCheck %s --check-prefix=IRATTR -DCPU=la464
10+
11+
// CC1ARG: "-tune-cpu" "[[CPU]]"
12+
// IRATTR: "tune-cpu"="[[CPU]]"
13+
14+
int foo(void) {
15+
return 3;
16+
}

clang/test/Preprocessor/init-loongarch.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,3 +787,23 @@
787787
// LA64-FPU0-LP64S: #define __loongarch_lp64 1
788788
// LA64-FPU0-LP64S-NOT: #define __loongarch_single_float
789789
// LA64-FPU0-LP64S: #define __loongarch_soft_float 1
790+
791+
/// Check __loongarch_arch and __loongarch_tune.
792+
793+
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - | \
794+
// RUN: FileCheck --match-full-lines --check-prefix=ARCH-TUNE -DARCH=loongarch64 -DTUNE=loongarch64 %s
795+
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=loongarch64 | \
796+
// RUN: FileCheck --match-full-lines --check-prefix=ARCH-TUNE -DARCH=loongarch64 -DTUNE=loongarch64 %s
797+
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=la464 | \
798+
// RUN: FileCheck --match-full-lines --check-prefix=ARCH-TUNE -DARCH=la464 -DTUNE=la464 %s
799+
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -mtune=loongarch64 | \
800+
// RUN: FileCheck --match-full-lines --check-prefix=ARCH-TUNE -DARCH=loongarch64 -DTUNE=loongarch64 %s
801+
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -mtune=la464 | \
802+
// RUN: FileCheck --match-full-lines --check-prefix=ARCH-TUNE -DARCH=loongarch64 -DTUNE=la464 %s
803+
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=loongarch64 -mtune=la464 | \
804+
// RUN: FileCheck --match-full-lines --check-prefix=ARCH-TUNE -DARCH=loongarch64 -DTUNE=la464 %s
805+
// RUN: %clang --target=loongarch64 -x c -E -dM %s -o - -march=la464 -mtune=loongarch64 | \
806+
// RUN: FileCheck --match-full-lines --check-prefix=ARCH-TUNE -DARCH=la464 -DTUNE=loongarch64 %s
807+
808+
// ARCH-TUNE: #define __loongarch_arch [[ARCH]]
809+
// ARCH-TUNE: #define __loongarch_tune [[TUNE]]

llvm/include/llvm/TargetParser/LoongArchTargetParser.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,16 @@ struct ArchInfo {
6666

6767
bool isValidArchName(StringRef Arch);
6868
bool getArchFeatures(StringRef Arch, std::vector<StringRef> &Features);
69+
bool isValidTuneCPUName(StringRef TuneCPU);
70+
void fillValidTuneCPUList(SmallVectorImpl<StringRef> &Values);
71+
StringRef getDefaultArch(bool Is64Bit);
72+
void setArch(StringRef Arch);
73+
StringRef getArch();
74+
void setTuneCPU(StringRef TuneCPU);
75+
StringRef getTuneCPU();
6976

7077
} // namespace LoongArch
7178

7279
} // namespace llvm
7380

74-
#endif // LLVM_SUPPORT_LOONGARCHTARGETPARSER_H
81+
#endif // LLVM_TARGETPARSER_LOONGARCHTARGETPARSER_H

llvm/lib/Target/LoongArch/LoongArch.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ include "LoongArchInstrInfo.td"
117117
def : ProcessorModel<"generic-la32", NoSchedModel, [Feature32Bit]>;
118118
def : ProcessorModel<"generic-la64", NoSchedModel, [Feature64Bit, FeatureUAL]>;
119119

120+
// Generic 64-bit processor with double-precision floating-point support.
121+
def : ProcessorModel<"loongarch64", NoSchedModel, [Feature64Bit,
122+
FeatureUAL,
123+
FeatureBasicD]>;
124+
120125
// Support generic for compatibility with other targets. The triple will be used
121126
// to change to the appropriate la32/la64 version.
122127
def : ProcessorModel<"generic", NoSchedModel, []>;

llvm/lib/TargetParser/LoongArchTargetParser.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
using namespace llvm;
1717
using namespace llvm::LoongArch;
1818

19+
StringRef Arch;
20+
StringRef TuneCPU;
21+
1922
const FeatureInfo AllFeatures[] = {
2023
#define LOONGARCH_FEATURE(NAME, KIND) {NAME, KIND},
2124
#include "llvm/TargetParser/LoongArchTargetParser.def"
@@ -46,3 +49,25 @@ bool LoongArch::getArchFeatures(StringRef Arch,
4649
}
4750
return false;
4851
}
52+
53+
bool LoongArch::isValidTuneCPUName(StringRef TuneCPU) {
54+
return isValidArchName(TuneCPU);
55+
}
56+
57+
void LoongArch::fillValidTuneCPUList(SmallVectorImpl<StringRef> &Values) {
58+
for (const auto A : AllArchs)
59+
Values.emplace_back(A.Name);
60+
}
61+
62+
StringRef LoongArch::getDefaultArch(bool Is64Bit) {
63+
// TODO: use a real 32-bit arch name.
64+
return Is64Bit ? "loongarch64" : "";
65+
}
66+
67+
void LoongArch::setArch(StringRef Name) { Arch = Name; }
68+
69+
StringRef LoongArch::getArch() { return Arch; }
70+
71+
void LoongArch::setTuneCPU(StringRef Name) { TuneCPU = Name; }
72+
73+
StringRef LoongArch::getTuneCPU() { return TuneCPU; }
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
; RUN: llc < %s --mtriple=loongarch64 --mattr=+64bit --mcpu=invalidcpu 2>&1 | FileCheck %s
2+
3+
; CHECK: {{.*}} is not a recognized processor for this target
4+
5+
define void @f() {
6+
ret void
7+
}

llvm/test/CodeGen/LoongArch/cpus.ll

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
;; This tests that llc accepts all valid LoongArch CPUs.
2+
;; Note the 'generic' names have been tested in cpu-name-generic.ll.
3+
4+
; RUN: llc < %s --mtriple=loongarch64 --mcpu=loongarch64 2>&1 | FileCheck %s
5+
; RUN: llc < %s --mtriple=loongarch64 --mcpu=la464 2>&1 | FileCheck %s
6+
; RUN: llc < %s --mtriple=loongarch64 2>&1 | FileCheck %s
7+
8+
; CHECK-NOT: {{.*}} is not a recognized processor for this target
9+
10+
define void @f() {
11+
ret void
12+
}
13+
14+
define void @tune_cpu_loongarch64() "tune-cpu"="loongarch64" {
15+
ret void
16+
}
17+
18+
define void @tune_cpu_la464() "tune-cpu"="la464" {
19+
ret void
20+
}

0 commit comments

Comments
 (0)