1//===--- LoongArch.cpp - LoongArch Helpers for Tools ------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "LoongArch.h"
10#include "../Clang.h"
11#include "clang/Basic/DiagnosticDriver.h"
12#include "clang/Driver/CommonArgs.h"
13#include "clang/Driver/Driver.h"
14#include "clang/Options/Options.h"
15#include "llvm/TargetParser/Host.h"
16#include "llvm/TargetParser/LoongArchTargetParser.h"
17
18using namespace clang::driver;
19using namespace clang::driver::tools;
20using namespace clang;
21using namespace llvm::opt;
22
23StringRef loongarch::getLoongArchABI(const Driver &D, const ArgList &Args,
24 const llvm::Triple &Triple) {
25 assert((Triple.getArch() == llvm::Triple::loongarch32 ||
26 Triple.getArch() == llvm::Triple::loongarch64) &&
27 "Unexpected triple");
28 bool IsLA32 = Triple.getArch() == llvm::Triple::loongarch32;
29
30 // Record -mabi value for later use.
31 const Arg *MABIArg = Args.getLastArg(Ids: options::OPT_mabi_EQ);
32 StringRef MABIValue;
33 if (MABIArg) {
34 MABIValue = MABIArg->getValue();
35 }
36
37 // Parse -mfpu value for later use.
38 const Arg *MFPUArg = Args.getLastArg(Ids: options::OPT_mfpu_EQ);
39 int FPU = -1;
40 if (MFPUArg) {
41 StringRef V = MFPUArg->getValue();
42 if (V == "64")
43 FPU = 64;
44 else if (V == "32")
45 FPU = 32;
46 else if (V == "0" || V == "none")
47 FPU = 0;
48 else
49 D.Diag(DiagID: diag::err_drv_loongarch_invalid_mfpu_EQ) << V;
50 }
51
52 // Check -m*-float firstly since they have highest priority.
53 if (const Arg *A = Args.getLastArg(Ids: options::OPT_mdouble_float,
54 Ids: options::OPT_msingle_float,
55 Ids: options::OPT_msoft_float)) {
56 StringRef ImpliedABI;
57 int ImpliedFPU = -1;
58 if (A->getOption().matches(ID: options::OPT_mdouble_float)) {
59 ImpliedABI = IsLA32 ? "ilp32d" : "lp64d";
60 ImpliedFPU = 64;
61 }
62 if (A->getOption().matches(ID: options::OPT_msingle_float)) {
63 ImpliedABI = IsLA32 ? "ilp32f" : "lp64f";
64 ImpliedFPU = 32;
65 }
66 if (A->getOption().matches(ID: options::OPT_msoft_float)) {
67 ImpliedABI = IsLA32 ? "ilp32s" : "lp64s";
68 ImpliedFPU = 0;
69 }
70
71 // Check `-mabi=` and `-mfpu=` settings and report if they conflict with
72 // the higher-priority settings implied by -m*-float.
73 //
74 // ImpliedABI and ImpliedFPU are guaranteed to have valid values because
75 // one of the match arms must match if execution can arrive here at all.
76 if (!MABIValue.empty() && ImpliedABI != MABIValue)
77 D.Diag(DiagID: diag::warn_drv_loongarch_conflicting_implied_val)
78 << MABIArg->getAsString(Args) << A->getAsString(Args) << ImpliedABI;
79
80 if (FPU != -1 && ImpliedFPU != FPU)
81 D.Diag(DiagID: diag::warn_drv_loongarch_conflicting_implied_val)
82 << MFPUArg->getAsString(Args) << A->getAsString(Args) << ImpliedFPU;
83
84 return ImpliedABI;
85 }
86
87 // If `-mabi=` is specified, use it.
88 if (!MABIValue.empty())
89 return MABIValue;
90
91 // Select abi based on -mfpu=xx.
92 switch (FPU) {
93 case 64:
94 return IsLA32 ? "ilp32d" : "lp64d";
95 case 32:
96 return IsLA32 ? "ilp32f" : "lp64f";
97 case 0:
98 return IsLA32 ? "ilp32s" : "lp64s";
99 }
100
101 // Choose a default based on the triple.
102 // Honor the explicit ABI modifier suffix in triple's environment part if
103 // present, falling back to {ILP32,LP64}D otherwise.
104 switch (Triple.getEnvironment()) {
105 case llvm::Triple::GNUSF:
106 case llvm::Triple::MuslSF:
107 return IsLA32 ? "ilp32s" : "lp64s";
108 case llvm::Triple::GNUF32:
109 case llvm::Triple::MuslF32:
110 return IsLA32 ? "ilp32f" : "lp64f";
111 case llvm::Triple::UnknownEnvironment: {
112 // Without an environment, the triple implies no ABI. Use the widest
113 // floating-point ABI that the architecture's FPU supports, which is also
114 // what the backend picks when it is given no ABI.
115 std::string ArchName;
116 if (const Arg *A = Args.getLastArg(Ids: options::OPT_march_EQ))
117 ArchName = A->getValue();
118 std::vector<StringRef> ArchFeatures;
119 llvm::LoongArch::getArchFeatures(
120 Arch: postProcessTargetCPUString(CPU: ArchName, Triple), Features&: ArchFeatures);
121 if (llvm::is_contained(Range&: ArchFeatures, Element: "+d"))
122 return IsLA32 ? "ilp32d" : "lp64d";
123 if (llvm::is_contained(Range&: ArchFeatures, Element: "+f"))
124 return IsLA32 ? "ilp32f" : "lp64f";
125 return IsLA32 ? "ilp32s" : "lp64s";
126 }
127 case llvm::Triple::GNUF64:
128 // This was originally permitted (and indeed the canonical way) to
129 // represent the {ILP32,LP64}D ABIs, but in Feb 2023 Loongson decided to
130 // drop the explicit suffix in favor of unmarked `-gnu` for the
131 // "general-purpose" ABIs, among other non-technical reasons.
132 //
133 // The spec change did not mention whether existing usages of "gnuf64"
134 // shall remain valid or not, so we are going to continue recognizing it
135 // for some time, until it is clear that everyone else has migrated away
136 // from it.
137 [[fallthrough]];
138 case llvm::Triple::GNU:
139 default:
140 return IsLA32 ? "ilp32d" : "lp64d";
141 }
142}
143
144void loongarch::getLoongArchTargetFeatures(const Driver &D,
145 const llvm::Triple &Triple,
146 const ArgList &Args,
147 std::vector<StringRef> &Features) {
148 // Enable the `lsx` feature on 64-bit LoongArch by default.
149 if (Triple.isLoongArch64() && (!Args.hasArgNoClaim(Ids: options::OPT_march_EQ)))
150 Features.push_back(x: "+lsx");
151
152 // -mrelax is default, unless -mno-relax is specified.
153 // FIXME: Only for loongarch64, loongarch32 has not been fully verified.
154 if (Args.hasFlag(Pos: options::OPT_mrelax, Neg: options::OPT_mno_relax,
155 Default: Triple.isLoongArch64() ? true : false))
156 Features.push_back(x: "+relax");
157 else if (Args.getLastArg(Ids: options::OPT_mno_relax))
158 Features.push_back(x: "-relax");
159
160 std::string ArchName;
161 const Arg *MArch = Args.getLastArg(Ids: options::OPT_march_EQ);
162 if (MArch)
163 ArchName = MArch->getValue();
164 ArchName = postProcessTargetCPUString(CPU: ArchName, Triple);
165 llvm::LoongArch::getArchFeatures(Arch: ArchName, Features);
166 if (MArch && StringRef(MArch->getValue()) == "native")
167 for (auto &F : llvm::sys::getHostCPUFeatures())
168 Features.push_back(
169 x: Args.MakeArgString(Str: (F.second ? "+" : "-") + F.first()));
170
171 // Select floating-point features determined by -mdouble-float,
172 // -msingle-float, -msoft-float and -mfpu.
173 // Note: -m*-float wins any other options.
174 if (const Arg *A = Args.getLastArg(Ids: options::OPT_mdouble_float,
175 Ids: options::OPT_msingle_float,
176 Ids: options::OPT_msoft_float)) {
177 if (A->getOption().matches(ID: options::OPT_mdouble_float)) {
178 Features.push_back(x: "+f");
179 Features.push_back(x: "+d");
180 } else if (A->getOption().matches(ID: options::OPT_msingle_float)) {
181 Features.push_back(x: "+f");
182 Features.push_back(x: "-d");
183 Features.push_back(x: "-lsx");
184 } else /*Soft-float*/ {
185 Features.push_back(x: "-f");
186 Features.push_back(x: "-d");
187 Features.push_back(x: "-lsx");
188 }
189 } else if (const Arg *A = Args.getLastArg(Ids: options::OPT_mfpu_EQ)) {
190 StringRef FPU = A->getValue();
191 if (FPU == "64") {
192 Features.push_back(x: "+f");
193 Features.push_back(x: "+d");
194 } else if (FPU == "32") {
195 Features.push_back(x: "+f");
196 Features.push_back(x: "-d");
197 Features.push_back(x: "-lsx");
198 } else if (FPU == "0" || FPU == "none") {
199 Features.push_back(x: "-f");
200 Features.push_back(x: "-d");
201 Features.push_back(x: "-lsx");
202 } else {
203 D.Diag(DiagID: diag::err_drv_loongarch_invalid_mfpu_EQ) << FPU;
204 }
205 }
206
207 // Accept but warn about these TargetSpecific options.
208 if (Arg *A = Args.getLastArgNoClaim(Ids: options::OPT_mabi_EQ))
209 A->ignoreTargetSpecific();
210 if (Arg *A = Args.getLastArgNoClaim(Ids: options::OPT_mfpu_EQ))
211 A->ignoreTargetSpecific();
212 if (Arg *A = Args.getLastArgNoClaim(Ids: options::OPT_msimd_EQ))
213 A->ignoreTargetSpecific();
214
215 // Select lsx/lasx feature determined by -msimd=.
216 // Option -msimd= precedes -m[no-]lsx and -m[no-]lasx.
217 if (const Arg *A = Args.getLastArg(Ids: options::OPT_msimd_EQ)) {
218 StringRef MSIMD = A->getValue();
219 if (MSIMD == "lsx") {
220 // Option -msimd=lsx depends on 64-bit FPU.
221 // -m*-float and -mfpu=none/0/32 conflict with -msimd=lsx.
222 if (llvm::is_contained(Range&: Features, Element: "-d"))
223 D.Diag(DiagID: diag::err_drv_loongarch_wrong_fpu_width) << /*LSX*/ 0;
224 else
225 Features.push_back(x: "+lsx");
226 } else if (MSIMD == "lasx") {
227 // Option -msimd=lasx depends on 64-bit FPU and LSX.
228 // -m*-float, -mfpu=none/0/32 and -mno-lsx conflict with -msimd=lasx.
229 if (llvm::is_contained(Range&: Features, Element: "-d"))
230 D.Diag(DiagID: diag::err_drv_loongarch_wrong_fpu_width) << /*LASX*/ 1;
231 else if (llvm::is_contained(Range&: Features, Element: "-lsx"))
232 D.Diag(DiagID: diag::err_drv_loongarch_invalid_simd_option_combination);
233
234 // The command options do not contain -mno-lasx.
235 if (!Args.getLastArg(Ids: options::OPT_mno_lasx)) {
236 Features.push_back(x: "+lsx");
237 Features.push_back(x: "+lasx");
238 }
239 } else if (MSIMD == "none") {
240 if (llvm::is_contained(Range&: Features, Element: "+lsx"))
241 Features.push_back(x: "-lsx");
242 if (llvm::is_contained(Range&: Features, Element: "+lasx"))
243 Features.push_back(x: "-lasx");
244 } else {
245 D.Diag(DiagID: diag::err_drv_loongarch_invalid_msimd_EQ) << MSIMD;
246 }
247 }
248
249 // Select lsx feature determined by -m[no-]lsx.
250 if (const Arg *A = Args.getLastArg(Ids: options::OPT_mlsx, Ids: options::OPT_mno_lsx)) {
251 // LSX depends on 64-bit FPU.
252 // -m*-float and -mfpu=none/0/32 conflict with -mlsx.
253 if (A->getOption().matches(ID: options::OPT_mlsx)) {
254 if (llvm::find(Range&: Features, Val: "-d") != Features.end())
255 D.Diag(DiagID: diag::err_drv_loongarch_wrong_fpu_width) << /*LSX*/ 0;
256 else /*-mlsx*/
257 Features.push_back(x: "+lsx");
258 } else /*-mno-lsx*/ {
259 Features.push_back(x: "-lsx");
260 Features.push_back(x: "-lasx");
261 }
262 }
263
264 // Select lasx feature determined by -m[no-]lasx.
265 if (const Arg *A =
266 Args.getLastArg(Ids: options::OPT_mlasx, Ids: options::OPT_mno_lasx)) {
267 // LASX depends on 64-bit FPU and LSX.
268 // -mno-lsx conflicts with -mlasx.
269 if (A->getOption().matches(ID: options::OPT_mlasx)) {
270 if (llvm::find(Range&: Features, Val: "-d") != Features.end())
271 D.Diag(DiagID: diag::err_drv_loongarch_wrong_fpu_width) << /*LASX*/ 1;
272 else { /*-mlasx*/
273 Features.push_back(x: "+lsx");
274 Features.push_back(x: "+lasx");
275 }
276 } else /*-mno-lasx*/
277 Features.push_back(x: "-lasx");
278 }
279
280 AddTargetFeature(Args, Features, OnOpt: options::OPT_mno_strict_align,
281 OffOpt: options::OPT_mstrict_align, FeatureName: "ual");
282 AddTargetFeature(Args, Features, OnOpt: options::OPT_mno_strict_align,
283 OffOpt: options::OPT_mstrict_align, FeatureName: "ual");
284 AddTargetFeature(Args, Features, OnOpt: options::OPT_mfrecipe,
285 OffOpt: options::OPT_mno_frecipe, FeatureName: "frecipe");
286 AddTargetFeature(Args, Features, OnOpt: options::OPT_mlam_bh,
287 OffOpt: options::OPT_mno_lam_bh, FeatureName: "lam-bh");
288 AddTargetFeature(Args, Features, OnOpt: options::OPT_mlamcas,
289 OffOpt: options::OPT_mno_lamcas, FeatureName: "lamcas");
290 AddTargetFeature(Args, Features, OnOpt: options::OPT_mld_seq_sa,
291 OffOpt: options::OPT_mno_ld_seq_sa, FeatureName: "ld-seq-sa");
292 AddTargetFeature(Args, Features, OnOpt: options::OPT_mdiv32,
293 OffOpt: options::OPT_mno_div32, FeatureName: "div32");
294 AddTargetFeature(Args, Features, OnOpt: options::OPT_mscq, OffOpt: options::OPT_mno_scq,
295 FeatureName: "scq");
296}
297
298std::string loongarch::postProcessTargetCPUString(const std::string &CPU,
299 const llvm::Triple &Triple) {
300 std::string CPUString = CPU;
301 if (CPUString == "native") {
302 CPUString = llvm::sys::getHostCPUName();
303 if (CPUString == "generic")
304 CPUString = llvm::LoongArch::getDefaultArch(Is64Bit: Triple.isLoongArch64());
305 }
306 if (CPUString.empty())
307 CPUString = llvm::LoongArch::getDefaultArch(Is64Bit: Triple.isLoongArch64());
308 return CPUString;
309}
310
311std::string loongarch::getLoongArchTargetCPU(const llvm::opt::ArgList &Args,
312 const llvm::Triple &Triple) {
313 std::string CPU;
314 std::string Arch;
315 // If we have -march, use that.
316 if (const Arg *A = Args.getLastArg(Ids: options::OPT_march_EQ)) {
317 Arch = A->getValue();
318 if (Arch == "la64v1.0" || Arch == "la64v1.1" || Arch == "la32v1.0" ||
319 Arch == "la32rv1.0")
320 CPU = llvm::LoongArch::getDefaultArch(Is64Bit: Triple.isLoongArch64());
321 else
322 CPU = Arch;
323 }
324 return postProcessTargetCPUString(CPU, Triple);
325}
326