clang  12.0.0git
RISCV.cpp
Go to the documentation of this file.
1 //===--- RISCV.cpp - RISCV 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 "RISCV.h"
10 #include "clang/Basic/CharInfo.h"
11 #include "clang/Driver/Driver.h"
13 #include "clang/Driver/Options.h"
14 #include "llvm/Option/ArgList.h"
15 #include "llvm/ADT/Optional.h"
16 #include "llvm/Support/TargetParser.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include "ToolChains/CommonArgs.h"
19 
20 using namespace clang::driver;
21 using namespace clang::driver::tools;
22 using namespace clang;
23 using namespace llvm::opt;
24 
25 namespace {
26 // Represents the major and version number components of a RISC-V extension
27 struct RISCVExtensionVersion {
28  StringRef Major;
29  StringRef Minor;
30 };
31 } // end anonymous namespace
32 
33 static StringRef getExtensionTypeDesc(StringRef Ext) {
34  if (Ext.startswith("sx"))
35  return "non-standard supervisor-level extension";
36  if (Ext.startswith("s"))
37  return "standard supervisor-level extension";
38  if (Ext.startswith("x"))
39  return "non-standard user-level extension";
40  if (Ext.startswith("z"))
41  return "standard user-level extension";
42  return StringRef();
43 }
44 
45 static StringRef getExtensionType(StringRef Ext) {
46  if (Ext.startswith("sx"))
47  return "sx";
48  if (Ext.startswith("s"))
49  return "s";
50  if (Ext.startswith("x"))
51  return "x";
52  if (Ext.startswith("z"))
53  return "z";
54  return StringRef();
55 }
56 
57 // If the extension is supported as experimental, return the version of that
58 // extension that the compiler currently supports.
60 isExperimentalExtension(StringRef Ext) {
61  if (Ext == "b" || Ext == "zbb" || Ext == "zbc" || Ext == "zbe" ||
62  Ext == "zbf" || Ext == "zbm" || Ext == "zbp" || Ext == "zbr" ||
63  Ext == "zbs" || Ext == "zbt" || Ext == "zbproposedc")
64  return RISCVExtensionVersion{"0", "92"};
65  if (Ext == "v")
66  return RISCVExtensionVersion{"0", "9"};
67  if (Ext == "zfh")
68  return RISCVExtensionVersion{"0", "1"};
69  return None;
70 }
71 
72 static bool isSupportedExtension(StringRef Ext) {
73  // LLVM supports "z" extensions which are marked as experimental.
74  if (isExperimentalExtension(Ext))
75  return true;
76 
77  // LLVM does not support "sx", "s" nor "x" extensions.
78  return false;
79 }
80 
81 // Extensions may have a version number, and may be separated by
82 // an underscore '_' e.g.: rv32i2_m2.
83 // Version number is divided into major and minor version numbers,
84 // separated by a 'p'. If the minor version is 0 then 'p0' can be
85 // omitted from the version string. E.g., rv32i2p0, rv32i2, rv32i2p1.
86 static bool getExtensionVersion(const Driver &D, const ArgList &Args,
87  StringRef MArch, StringRef Ext, StringRef In,
88  std::string &Major, std::string &Minor) {
89  Major = std::string(In.take_while(isDigit));
90  In = In.substr(Major.size());
91 
92  if (Major.size() && In.consume_front("p")) {
93  Minor = std::string(In.take_while(isDigit));
94  In = In.substr(Major.size() + 1);
95 
96  // Expected 'p' to be followed by minor version number.
97  if (Minor.empty()) {
98  std::string Error =
99  "minor version number missing after 'p' for extension";
100  D.Diag(diag::err_drv_invalid_riscv_ext_arch_name)
101  << MArch << Error << Ext;
102  return false;
103  }
104  }
105 
106  // Expected multi-character extension with version number to have no
107  // subsequent characters (i.e. must either end string or be followed by
108  // an underscore).
109  if (Ext.size() > 1 && In.size()) {
110  std::string Error =
111  "multi-character extensions must be separated by underscores";
112  D.Diag(diag::err_drv_invalid_riscv_ext_arch_name) << MArch << Error << In;
113  return false;
114  }
115 
116  // If experimental extension, require use of current version number number
117  if (auto ExperimentalExtension = isExperimentalExtension(Ext)) {
118  if (!Args.hasArg(options::OPT_menable_experimental_extensions)) {
119  std::string Error =
120  "requires '-menable-experimental-extensions' for experimental extension";
121  D.Diag(diag::err_drv_invalid_riscv_ext_arch_name)
122  << MArch << Error << Ext;
123  return false;
124  } else if (Major.empty() && Minor.empty()) {
125  std::string Error =
126  "experimental extension requires explicit version number";
127  D.Diag(diag::err_drv_invalid_riscv_ext_arch_name)
128  << MArch << Error << Ext;
129  return false;
130  }
131  auto SupportedVers = *ExperimentalExtension;
132  if (Major != SupportedVers.Major || Minor != SupportedVers.Minor) {
133  std::string Error =
134  "unsupported version number " + Major;
135  if (!Minor.empty())
136  Error += "." + Minor;
137  Error += " for experimental extension (this compiler supports "
138  + SupportedVers.Major.str() + "."
139  + SupportedVers.Minor.str() + ")";
140 
141  D.Diag(diag::err_drv_invalid_riscv_ext_arch_name)
142  << MArch << Error << Ext;
143  return false;
144  }
145  return true;
146  }
147 
148  // Allow extensions to declare no version number
149  if (Major.empty() && Minor.empty())
150  return true;
151 
152  // TODO: Handle supported extensions with version number.
153  std::string Error = "unsupported version number " + Major;
154  if (!Minor.empty())
155  Error += "." + Minor;
156  Error += " for extension";
157  D.Diag(diag::err_drv_invalid_riscv_ext_arch_name) << MArch << Error << Ext;
158 
159  return false;
160 }
161 
162 // Handle other types of extensions other than the standard
163 // general purpose and standard user-level extensions.
164 // Parse the ISA string containing non-standard user-level
165 // extensions, standard supervisor-level extensions and
166 // non-standard supervisor-level extensions.
167 // These extensions start with 'z', 'x', 's', 'sx' prefixes, follow a
168 // canonical order, might have a version number (major, minor)
169 // and are separated by a single underscore '_'.
170 // Set the hardware features for the extensions that are supported.
171 static void getExtensionFeatures(const Driver &D,
172  const ArgList &Args,
173  std::vector<StringRef> &Features,
174  StringRef &MArch, StringRef &Exts) {
175  if (Exts.empty())
176  return;
177 
178  // Multi-letter extensions are seperated by a single underscore
179  // as described in RISC-V User-Level ISA V2.2.
181  Exts.split(Split, StringRef("_"));
182 
183  SmallVector<StringRef, 4> Prefix{"z", "x", "s", "sx"};
184  auto I = Prefix.begin();
185  auto E = Prefix.end();
186 
188 
189  for (StringRef Ext : Split) {
190  if (Ext.empty()) {
191  D.Diag(diag::err_drv_invalid_riscv_arch_name) << MArch
192  << "extension name missing after separator '_'";
193  return;
194  }
195 
196  StringRef Type = getExtensionType(Ext);
197  StringRef Desc = getExtensionTypeDesc(Ext);
198  auto Pos = Ext.find_if(isDigit);
199  StringRef Name(Ext.substr(0, Pos));
200  StringRef Vers(Ext.substr(Pos));
201 
202  if (Type.empty()) {
203  D.Diag(diag::err_drv_invalid_riscv_ext_arch_name)
204  << MArch << "invalid extension prefix" << Ext;
205  return;
206  }
207 
208  // Check ISA extensions are specified in the canonical order.
209  while (I != E && *I != Type)
210  ++I;
211 
212  if (I == E) {
213  std::string Error = std::string(Desc);
214  Error += " not given in canonical order";
215  D.Diag(diag::err_drv_invalid_riscv_ext_arch_name)
216  << MArch << Error << Ext;
217  return;
218  }
219 
220  // The order is OK, do not advance I to the next prefix
221  // to allow repeated extension type, e.g.: rv32ixabc_xdef.
222 
223  if (Name.size() == Type.size()) {
224  std::string Error = std::string(Desc);
225  Error += " name missing after";
226  D.Diag(diag::err_drv_invalid_riscv_ext_arch_name)
227  << MArch << Error << Type;
228  return;
229  }
230 
231  std::string Major, Minor;
232  if (!getExtensionVersion(D, Args, MArch, Name, Vers, Major, Minor))
233  return;
234 
235  // Check if duplicated extension.
236  if (llvm::is_contained(AllExts, Name)) {
237  std::string Error = "duplicated ";
238  Error += Desc;
239  D.Diag(diag::err_drv_invalid_riscv_ext_arch_name)
240  << MArch << Error << Name;
241  return;
242  }
243 
244  // Extension format is correct, keep parsing the extensions.
245  // TODO: Save Type, Name, Major, Minor to avoid parsing them later.
246  AllExts.push_back(Name);
247  }
248 
249  // Set target features.
250  // TODO: Hardware features to be handled in Support/TargetParser.cpp.
251  // TODO: Use version number when setting target features.
252  for (auto Ext : AllExts) {
253  if (!isSupportedExtension(Ext)) {
254  StringRef Desc = getExtensionTypeDesc(getExtensionType(Ext));
255  std::string Error = "unsupported ";
256  Error += Desc;
257  D.Diag(diag::err_drv_invalid_riscv_ext_arch_name)
258  << MArch << Error << Ext;
259  return;
260  }
261  if (isExperimentalExtension(Ext))
262  Features.push_back(Args.MakeArgString("+experimental-" + Ext));
263  else
264  Features.push_back(Args.MakeArgString("+" + Ext));
265  }
266 }
267 
268 // Returns false if an error is diagnosed.
269 static bool getArchFeatures(const Driver &D, StringRef MArch,
270  std::vector<StringRef> &Features,
271  const ArgList &Args) {
272  // RISC-V ISA strings must be lowercase.
273  if (llvm::any_of(MArch, [](char c) { return isupper(c); })) {
274  D.Diag(diag::err_drv_invalid_riscv_arch_name)
275  << MArch << "string must be lowercase";
276  return false;
277  }
278 
279  // ISA string must begin with rv32 or rv64.
280  if (!(MArch.startswith("rv32") || MArch.startswith("rv64")) ||
281  (MArch.size() < 5)) {
282  D.Diag(diag::err_drv_invalid_riscv_arch_name)
283  << MArch << "string must begin with rv32{i,e,g} or rv64{i,g}";
284  return false;
285  }
286 
287  bool HasRV64 = MArch.startswith("rv64");
288 
289  // The canonical order specified in ISA manual.
290  // Ref: Table 22.1 in RISC-V User-Level ISA V2.2
291  StringRef StdExts = "mafdqlcbjtpvn";
292  bool HasF = false, HasD = false;
293  char Baseline = MArch[4];
294 
295  // First letter should be 'e', 'i' or 'g'.
296  switch (Baseline) {
297  default:
298  D.Diag(diag::err_drv_invalid_riscv_arch_name)
299  << MArch << "first letter should be 'e', 'i' or 'g'";
300  return false;
301  case 'e': {
302  StringRef Error;
303  // Currently LLVM does not support 'e'.
304  // Extension 'e' is not allowed in rv64.
305  if (HasRV64)
306  Error = "standard user-level extension 'e' requires 'rv32'";
307  else
308  Error = "unsupported standard user-level extension 'e'";
309  D.Diag(diag::err_drv_invalid_riscv_arch_name) << MArch << Error;
310  return false;
311  }
312  case 'i':
313  break;
314  case 'g':
315  // g = imafd
316  StdExts = StdExts.drop_front(4);
317  Features.push_back("+m");
318  Features.push_back("+a");
319  Features.push_back("+f");
320  Features.push_back("+d");
321  HasF = true;
322  HasD = true;
323  break;
324  }
325 
326  // Skip rvxxx
327  StringRef Exts = MArch.substr(5);
328 
329  // Remove multi-letter standard extensions, non-standard extensions and
330  // supervisor-level extensions. They have 'z', 'x', 's', 'sx' prefixes.
331  // Parse them at the end.
332  // Find the very first occurrence of 's', 'x' or 'z'.
333  StringRef OtherExts;
334  size_t Pos = Exts.find_first_of("zsx");
335  if (Pos != StringRef::npos) {
336  OtherExts = Exts.substr(Pos);
337  Exts = Exts.substr(0, Pos);
338  }
339 
340  std::string Major, Minor;
341  if (!getExtensionVersion(D, Args, MArch, std::string(1, Baseline), Exts,
342  Major, Minor))
343  return false;
344 
345  // Consume the base ISA version number and any '_' between rvxxx and the
346  // first extension
347  Exts = Exts.drop_front(Major.size());
348  if (!Minor.empty())
349  Exts = Exts.drop_front(Minor.size() + 1 /*'p'*/);
350  Exts.consume_front("_");
351 
352  // TODO: Use version number when setting target features
353 
354  auto StdExtsItr = StdExts.begin();
355  auto StdExtsEnd = StdExts.end();
356 
357  for (auto I = Exts.begin(), E = Exts.end(); I != E; ) {
358  char c = *I;
359 
360  // Check ISA extensions are specified in the canonical order.
361  while (StdExtsItr != StdExtsEnd && *StdExtsItr != c)
362  ++StdExtsItr;
363 
364  if (StdExtsItr == StdExtsEnd) {
365  // Either c contains a valid extension but it was not given in
366  // canonical order or it is an invalid extension.
367  StringRef Error;
368  if (StdExts.contains(c))
369  Error = "standard user-level extension not given in canonical order";
370  else
371  Error = "invalid standard user-level extension";
372  D.Diag(diag::err_drv_invalid_riscv_ext_arch_name)
373  << MArch << Error << std::string(1, c);
374  return false;
375  }
376 
377  // Move to next char to prevent repeated letter.
378  ++StdExtsItr;
379 
380  std::string Next, Major, Minor;
381  if (std::next(I) != E)
382  Next = std::string(std::next(I), E);
383  if (!getExtensionVersion(D, Args, MArch, std::string(1, c), Next, Major,
384  Minor))
385  return false;
386 
387  // The order is OK, then push it into features.
388  // TODO: Use version number when setting target features
389  switch (c) {
390  default:
391  // Currently LLVM supports only "mafdc".
392  D.Diag(diag::err_drv_invalid_riscv_ext_arch_name)
393  << MArch << "unsupported standard user-level extension"
394  << std::string(1, c);
395  return false;
396  case 'm':
397  Features.push_back("+m");
398  break;
399  case 'a':
400  Features.push_back("+a");
401  break;
402  case 'f':
403  Features.push_back("+f");
404  HasF = true;
405  break;
406  case 'd':
407  Features.push_back("+d");
408  HasD = true;
409  break;
410  case 'c':
411  Features.push_back("+c");
412  break;
413  case 'b':
414  Features.push_back("+experimental-b");
415  break;
416  case 'v':
417  Features.push_back("+experimental-v");
418  break;
419  }
420 
421  // Consume full extension name and version, including any optional '_'
422  // between this extension and the next
423  ++I;
424  I += Major.size();
425  if (Minor.size())
426  I += Minor.size() + 1 /*'p'*/;
427  if (*I == '_')
428  ++I;
429  }
430 
431  // Dependency check.
432  // It's illegal to specify the 'd' (double-precision floating point)
433  // extension without also specifying the 'f' (single precision
434  // floating-point) extension.
435  if (HasD && !HasF) {
436  D.Diag(diag::err_drv_invalid_riscv_arch_name)
437  << MArch << "d requires f extension to also be specified";
438  return false;
439  }
440 
441  // Additional dependency checks.
442  // TODO: The 'q' extension requires rv64.
443  // TODO: It is illegal to specify 'e' extensions with 'f' and 'd'.
444 
445  // Handle all other types of extensions.
446  getExtensionFeatures(D, Args, Features, MArch, OtherExts);
447 
448  return true;
449 }
450 
451 // Get features except standard extension feature
452 static void getRISCFeaturesFromMcpu(const Driver &D, const llvm::Triple &Triple,
453  const llvm::opt::ArgList &Args,
454  const llvm::opt::Arg *A, StringRef Mcpu,
455  std::vector<StringRef> &Features) {
456  bool Is64Bit = (Triple.getArch() == llvm::Triple::riscv64);
457  llvm::RISCV::CPUKind CPUKind = llvm::RISCV::parseCPUKind(Mcpu);
458  if (!llvm::RISCV::checkCPUKind(CPUKind, Is64Bit) ||
459  !llvm::RISCV::getCPUFeaturesExceptStdExt(CPUKind, Features)) {
460  D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
461  }
462 }
463 
464 void riscv::getRISCVTargetFeatures(const Driver &D, const llvm::Triple &Triple,
465  const ArgList &Args,
466  std::vector<StringRef> &Features) {
467  StringRef MArch = getRISCVArch(Args, Triple);
468 
469  if (!getArchFeatures(D, MArch, Features, Args))
470  return;
471 
472  // If users give march and mcpu, get std extension feature from MArch
473  // and other features (ex. mirco architecture feature) from mcpu
474  if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
475  getRISCFeaturesFromMcpu(D, Triple, Args, A, A->getValue(), Features);
476 
477  // Handle features corresponding to "-ffixed-X" options
478  if (Args.hasArg(options::OPT_ffixed_x1))
479  Features.push_back("+reserve-x1");
480  if (Args.hasArg(options::OPT_ffixed_x2))
481  Features.push_back("+reserve-x2");
482  if (Args.hasArg(options::OPT_ffixed_x3))
483  Features.push_back("+reserve-x3");
484  if (Args.hasArg(options::OPT_ffixed_x4))
485  Features.push_back("+reserve-x4");
486  if (Args.hasArg(options::OPT_ffixed_x5))
487  Features.push_back("+reserve-x5");
488  if (Args.hasArg(options::OPT_ffixed_x6))
489  Features.push_back("+reserve-x6");
490  if (Args.hasArg(options::OPT_ffixed_x7))
491  Features.push_back("+reserve-x7");
492  if (Args.hasArg(options::OPT_ffixed_x8))
493  Features.push_back("+reserve-x8");
494  if (Args.hasArg(options::OPT_ffixed_x9))
495  Features.push_back("+reserve-x9");
496  if (Args.hasArg(options::OPT_ffixed_x10))
497  Features.push_back("+reserve-x10");
498  if (Args.hasArg(options::OPT_ffixed_x11))
499  Features.push_back("+reserve-x11");
500  if (Args.hasArg(options::OPT_ffixed_x12))
501  Features.push_back("+reserve-x12");
502  if (Args.hasArg(options::OPT_ffixed_x13))
503  Features.push_back("+reserve-x13");
504  if (Args.hasArg(options::OPT_ffixed_x14))
505  Features.push_back("+reserve-x14");
506  if (Args.hasArg(options::OPT_ffixed_x15))
507  Features.push_back("+reserve-x15");
508  if (Args.hasArg(options::OPT_ffixed_x16))
509  Features.push_back("+reserve-x16");
510  if (Args.hasArg(options::OPT_ffixed_x17))
511  Features.push_back("+reserve-x17");
512  if (Args.hasArg(options::OPT_ffixed_x18))
513  Features.push_back("+reserve-x18");
514  if (Args.hasArg(options::OPT_ffixed_x19))
515  Features.push_back("+reserve-x19");
516  if (Args.hasArg(options::OPT_ffixed_x20))
517  Features.push_back("+reserve-x20");
518  if (Args.hasArg(options::OPT_ffixed_x21))
519  Features.push_back("+reserve-x21");
520  if (Args.hasArg(options::OPT_ffixed_x22))
521  Features.push_back("+reserve-x22");
522  if (Args.hasArg(options::OPT_ffixed_x23))
523  Features.push_back("+reserve-x23");
524  if (Args.hasArg(options::OPT_ffixed_x24))
525  Features.push_back("+reserve-x24");
526  if (Args.hasArg(options::OPT_ffixed_x25))
527  Features.push_back("+reserve-x25");
528  if (Args.hasArg(options::OPT_ffixed_x26))
529  Features.push_back("+reserve-x26");
530  if (Args.hasArg(options::OPT_ffixed_x27))
531  Features.push_back("+reserve-x27");
532  if (Args.hasArg(options::OPT_ffixed_x28))
533  Features.push_back("+reserve-x28");
534  if (Args.hasArg(options::OPT_ffixed_x29))
535  Features.push_back("+reserve-x29");
536  if (Args.hasArg(options::OPT_ffixed_x30))
537  Features.push_back("+reserve-x30");
538  if (Args.hasArg(options::OPT_ffixed_x31))
539  Features.push_back("+reserve-x31");
540 
541  // -mrelax is default, unless -mno-relax is specified.
542  if (Args.hasFlag(options::OPT_mrelax, options::OPT_mno_relax, true))
543  Features.push_back("+relax");
544  else
545  Features.push_back("-relax");
546 
547  // GCC Compatibility: -mno-save-restore is default, unless -msave-restore is
548  // specified.
549  if (Args.hasFlag(options::OPT_msave_restore, options::OPT_mno_save_restore, false))
550  Features.push_back("+save-restore");
551  else
552  Features.push_back("-save-restore");
553 
554  // Now add any that the user explicitly requested on the command line,
555  // which may override the defaults.
556  handleTargetFeaturesGroup(Args, Features, options::OPT_m_riscv_Features_Group);
557 }
558 
559 StringRef riscv::getRISCVABI(const ArgList &Args, const llvm::Triple &Triple) {
560  assert((Triple.getArch() == llvm::Triple::riscv32 ||
561  Triple.getArch() == llvm::Triple::riscv64) &&
562  "Unexpected triple");
563 
564  // GCC's logic around choosing a default `-mabi=` is complex. If GCC is not
565  // configured using `--with-abi=`, then the logic for the default choice is
566  // defined in config.gcc. This function is based on the logic in GCC 9.2.0.
567  //
568  // The logic used in GCC 9.2.0 is the following, in order:
569  // 1. Explicit choices using `--with-abi=`
570  // 2. A default based on `--with-arch=`, if provided
571  // 3. A default based on the target triple's arch
572  //
573  // The logic in config.gcc is a little circular but it is not inconsistent.
574  //
575  // Clang does not have `--with-arch=` or `--with-abi=`, so we use `-march=`
576  // and `-mabi=` respectively instead.
577  //
578  // In order to make chosing logic more clear, Clang uses the following logic,
579  // in order:
580  // 1. Explicit choices using `-mabi=`
581  // 2. A default based on the architecture as determined by getRISCVArch
582  // 3. Choose a default based on the triple
583 
584  // 1. If `-mabi=` is specified, use it.
585  if (const Arg *A = Args.getLastArg(options::OPT_mabi_EQ))
586  return A->getValue();
587 
588  // 2. Choose a default based on the target architecture.
589  //
590  // rv32g | rv32*d -> ilp32d
591  // rv32e -> ilp32e
592  // rv32* -> ilp32
593  // rv64g | rv64*d -> lp64d
594  // rv64* -> lp64
595  StringRef MArch = getRISCVArch(Args, Triple);
596 
597  if (MArch.startswith_lower("rv32")) {
598  // FIXME: parse `March` to find `D` extension properly
599  if (MArch.substr(4).contains_lower("d") || MArch.startswith_lower("rv32g"))
600  return "ilp32d";
601  else if (MArch.startswith_lower("rv32e"))
602  return "ilp32e";
603  else
604  return "ilp32";
605  } else if (MArch.startswith_lower("rv64")) {
606  // FIXME: parse `March` to find `D` extension properly
607  if (MArch.substr(4).contains_lower("d") || MArch.startswith_lower("rv64g"))
608  return "lp64d";
609  else
610  return "lp64";
611  }
612 
613  // 3. Choose a default based on the triple
614  //
615  // We deviate from GCC's defaults here:
616  // - On `riscv{XLEN}-unknown-elf` we use the integer calling convention only.
617  // - On all other OSs we use the double floating point calling convention.
618  if (Triple.getArch() == llvm::Triple::riscv32) {
619  if (Triple.getOS() == llvm::Triple::UnknownOS)
620  return "ilp32";
621  else
622  return "ilp32d";
623  } else {
624  if (Triple.getOS() == llvm::Triple::UnknownOS)
625  return "lp64";
626  else
627  return "lp64d";
628  }
629 }
630 
631 StringRef riscv::getRISCVArch(const llvm::opt::ArgList &Args,
632  const llvm::Triple &Triple) {
633  assert((Triple.getArch() == llvm::Triple::riscv32 ||
634  Triple.getArch() == llvm::Triple::riscv64) &&
635  "Unexpected triple");
636 
637  // GCC's logic around choosing a default `-march=` is complex. If GCC is not
638  // configured using `--with-arch=`, then the logic for the default choice is
639  // defined in config.gcc. This function is based on the logic in GCC 9.2.0. We
640  // deviate from GCC's default on additional `-mcpu` option (GCC does not
641  // support `-mcpu`) and baremetal targets (UnknownOS) where neither `-march`
642  // nor `-mabi` is specified.
643  //
644  // The logic used in GCC 9.2.0 is the following, in order:
645  // 1. Explicit choices using `--with-arch=`
646  // 2. A default based on `--with-abi=`, if provided
647  // 3. A default based on the target triple's arch
648  //
649  // The logic in config.gcc is a little circular but it is not inconsistent.
650  //
651  // Clang does not have `--with-arch=` or `--with-abi=`, so we use `-march=`
652  // and `-mabi=` respectively instead.
653  //
654  // Clang uses the following logic, in order:
655  // 1. Explicit choices using `-march=`
656  // 2. Based on `-mcpu` if the target CPU has a default ISA string
657  // 3. A default based on `-mabi`, if provided
658  // 4. A default based on the target triple's arch
659  //
660  // Clang does not yet support MULTILIB_REUSE, so we use `rv{XLEN}imafdc`
661  // instead of `rv{XLEN}gc` though they are (currently) equivalent.
662 
663  // 1. If `-march=` is specified, use it.
664  if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
665  return A->getValue();
666 
667  // 2. Get march (isa string) based on `-mcpu=`
668  if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
669  StringRef MArch = llvm::RISCV::getMArchFromMcpu(A->getValue());
670  // Bypass if target cpu's default march is empty.
671  if (MArch != "")
672  return MArch;
673  }
674 
675  // 3. Choose a default based on `-mabi=`
676  //
677  // ilp32e -> rv32e
678  // ilp32 | ilp32f | ilp32d -> rv32imafdc
679  // lp64 | lp64f | lp64d -> rv64imafdc
680  if (const Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
681  StringRef MABI = A->getValue();
682 
683  if (MABI.equals_lower("ilp32e"))
684  return "rv32e";
685  else if (MABI.startswith_lower("ilp32"))
686  return "rv32imafdc";
687  else if (MABI.startswith_lower("lp64"))
688  return "rv64imafdc";
689  }
690 
691  // 4. Choose a default based on the triple
692  //
693  // We deviate from GCC's defaults here:
694  // - On `riscv{XLEN}-unknown-elf` we default to `rv{XLEN}imac`
695  // - On all other OSs we use `rv{XLEN}imafdc` (equivalent to `rv{XLEN}gc`)
696  if (Triple.getArch() == llvm::Triple::riscv32) {
697  if (Triple.getOS() == llvm::Triple::UnknownOS)
698  return "rv32imac";
699  else
700  return "rv32imafdc";
701  } else {
702  if (Triple.getOS() == llvm::Triple::UnknownOS)
703  return "rv64imac";
704  else
705  return "rv64imafdc";
706  }
707 }
void handleTargetFeaturesGroup(const llvm::opt::ArgList &Args, std::vector< StringRef > &Features, llvm::opt::OptSpecifier Group)
Iterate Args and convert -mxxx to +xxx and -mno-xxx to -xxx and append it to Features.
The base class of the type hierarchy.
Definition: Type.h:1478
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:109
static void getExtensionFeatures(const Driver &D, const ArgList &Args, std::vector< StringRef > &Features, StringRef &MArch, StringRef &Exts)
Definition: RISCV.cpp:171
StringRef getRISCVArch(const llvm::opt::ArgList &Args, const llvm::Triple &Triple)
Definition: RISCV.cpp:631
llvm::Error Error
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:59
static bool getExtensionVersion(const Driver &D, const ArgList &Args, StringRef MArch, StringRef Ext, StringRef In, std::string &Major, std::string &Minor)
Definition: RISCV.cpp:86
StringRef getRISCVABI(const llvm::opt::ArgList &Args, const llvm::Triple &Triple)
static Optional< RISCVExtensionVersion > isExperimentalExtension(StringRef Ext)
Definition: RISCV.cpp:60
static StringRef getExtensionTypeDesc(StringRef Ext)
Definition: RISCV.cpp:33
static void getRISCFeaturesFromMcpu(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args, const llvm::opt::Arg *A, StringRef Mcpu, std::vector< StringRef > &Features)
Definition: RISCV.cpp:452
static bool getArchFeatures(const Driver &D, StringRef MArch, std::vector< StringRef > &Features, const ArgList &Args)
Definition: RISCV.cpp:269
Dataflow Directional Tag Classes.
LLVM_READONLY bool isDigit(unsigned char c)
Return true if this character is an ASCII digit: [0-9].
Definition: CharInfo.h:93
static StringRef getExtensionType(StringRef Ext)
Definition: RISCV.cpp:45
static bool isSupportedExtension(StringRef Ext)
Definition: RISCV.cpp:72
void getRISCVTargetFeatures(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args, std::vector< llvm::StringRef > &Features)
__device__ __2f16 float c