LLVM 22.0.0git
RISCVISAInfo.cpp
Go to the documentation of this file.
1//===-- RISCVISAInfo.cpp - RISC-V Arch String Parser ----------------------===//
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
10#include "llvm/ADT/STLExtras.h"
12#include "llvm/ADT/StringRef.h"
13#include "llvm/Support/Errc.h"
14#include "llvm/Support/Error.h"
16
17#include <array>
18#include <atomic>
19#include <optional>
20#include <string>
21#include <vector>
22
23using namespace llvm;
24
25namespace {
26
27struct RISCVSupportedExtension {
28 const char *Name;
29 /// Supported version.
30 RISCVISAUtils::ExtensionVersion Version;
31
32 bool operator<(const RISCVSupportedExtension &RHS) const {
33 return StringRef(Name) < StringRef(RHS.Name);
34 }
35};
36
37struct RISCVProfile {
38 StringLiteral Name;
39 StringLiteral MArch;
40
41 bool operator<(const RISCVProfile &RHS) const {
42 return StringRef(Name) < StringRef(RHS.Name);
43 }
44};
45
46} // end anonymous namespace
47
48static const char *RISCVGImplications[] = {"i", "m", "a", "f", "d"};
49static const char *RISCVGImplicationsZi[] = {"zicsr", "zifencei"};
50
51#define GET_SUPPORTED_EXTENSIONS
52#include "llvm/TargetParser/RISCVTargetParserDef.inc"
53
54#define GET_SUPPORTED_PROFILES
55#include "llvm/TargetParser/RISCVTargetParserDef.inc"
56
57static void verifyTables() {
58#ifndef NDEBUG
59 static std::atomic<bool> TableChecked(false);
60 if (!TableChecked.load(std::memory_order_relaxed)) {
61 assert(llvm::is_sorted(SupportedExtensions) &&
62 "Extensions are not sorted by name");
63 assert(llvm::is_sorted(SupportedExperimentalExtensions) &&
64 "Experimental extensions are not sorted by name");
65 assert(llvm::is_sorted(SupportedProfiles) &&
66 "Profiles are not sorted by name");
67 assert(llvm::is_sorted(SupportedExperimentalProfiles) &&
68 "Experimental profiles are not sorted by name");
69 TableChecked.store(true, std::memory_order_relaxed);
70 }
71#endif
72}
73
74static void PrintExtension(StringRef Name, StringRef Version,
75 StringRef Description) {
76 outs().indent(4);
77 unsigned VersionWidth = Description.empty() ? 0 : 10;
78 outs() << left_justify(Name, 21) << left_justify(Version, VersionWidth)
79 << Description << "\n";
80}
81
83 outs() << "All available -march extensions for RISC-V\n\n";
84 PrintExtension("Name", "Version", (DescMap.empty() ? "" : "Description"));
85
87 for (const auto &E : SupportedExtensions)
88 ExtMap[E.Name] = {E.Version.Major, E.Version.Minor};
89 for (const auto &E : ExtMap) {
90 std::string Version =
91 std::to_string(E.second.Major) + "." + std::to_string(E.second.Minor);
92 PrintExtension(E.first, Version, DescMap[E.first]);
93 }
94
95 outs() << "\nExperimental extensions\n";
96 ExtMap.clear();
97 for (const auto &E : SupportedExperimentalExtensions)
98 ExtMap[E.Name] = {E.Version.Major, E.Version.Minor};
99 for (const auto &E : ExtMap) {
100 std::string Version =
101 std::to_string(E.second.Major) + "." + std::to_string(E.second.Minor);
102 PrintExtension(E.first, Version, DescMap["experimental-" + E.first]);
103 }
104
105 outs() << "\nSupported Profiles\n";
106 for (const auto &P : SupportedProfiles)
107 outs().indent(4) << P.Name << "\n";
108
109 outs() << "\nExperimental Profiles\n";
110 for (const auto &P : SupportedExperimentalProfiles)
111 outs().indent(4) << P.Name << "\n";
112
113 outs() << "\nUse -march to specify the target's extension.\n"
114 "For example, clang -march=rv32i_v1p0\n";
115}
116
118 bool IsRV64, std::set<StringRef> &EnabledFeatureNames,
119 StringMap<StringRef> &DescMap) {
120 outs() << "Extensions enabled for the given RISC-V target\n\n";
121 PrintExtension("Name", "Version", (DescMap.empty() ? "" : "Description"));
122
125 for (const auto &E : SupportedExtensions)
126 if (EnabledFeatureNames.count(E.Name) != 0) {
127 FullExtMap[E.Name] = {E.Version.Major, E.Version.Minor};
128 ExtMap[E.Name] = {E.Version.Major, E.Version.Minor};
129 }
130 for (const auto &E : ExtMap) {
131 std::string Version =
132 std::to_string(E.second.Major) + "." + std::to_string(E.second.Minor);
133 PrintExtension(E.first, Version, DescMap[E.first]);
134 }
135
136 outs() << "\nExperimental extensions\n";
137 ExtMap.clear();
138 for (const auto &E : SupportedExperimentalExtensions) {
139 StringRef Name(E.Name);
140 if (EnabledFeatureNames.count("experimental-" + Name.str()) != 0) {
141 FullExtMap[E.Name] = {E.Version.Major, E.Version.Minor};
142 ExtMap[E.Name] = {E.Version.Major, E.Version.Minor};
143 }
144 }
145 for (const auto &E : ExtMap) {
146 std::string Version =
147 std::to_string(E.second.Major) + "." + std::to_string(E.second.Minor);
148 PrintExtension(E.first, Version, DescMap["experimental-" + E.first]);
149 }
150
151 unsigned XLen = IsRV64 ? 64 : 32;
152 if (auto ISAString = RISCVISAInfo::createFromExtMap(XLen, FullExtMap))
153 outs() << "\nISA String: " << ISAString.get()->toString() << "\n";
154}
155
157 return Ext.consume_front("experimental-");
158}
159
160// This function finds the last character that doesn't belong to a version
161// (e.g. zba1p0 is extension 'zba' of version '1p0'). So the function will
162// consume [0-9]*p[0-9]* starting from the backward. An extension name will not
163// end with a digit or the letter 'p', so this function will parse correctly.
164// NOTE: This function is NOT able to take empty strings or strings that only
165// have version numbers and no extension name. It assumes the extension name
166// will be at least more than one character.
168 assert(!Ext.empty() &&
169 "Already guarded by if-statement in ::parseArchString");
170
171 int Pos = Ext.size() - 1;
172 while (Pos > 0 && isDigit(Ext[Pos]))
173 Pos--;
174 if (Pos > 0 && Ext[Pos] == 'p' && isDigit(Ext[Pos - 1])) {
175 Pos--;
176 while (Pos > 0 && isDigit(Ext[Pos]))
177 Pos--;
178 }
179 return Pos;
180}
181
182namespace {
183struct LessExtName {
184 bool operator()(const RISCVSupportedExtension &LHS, StringRef RHS) {
185 return StringRef(LHS.Name) < RHS;
186 }
187 bool operator()(StringRef LHS, const RISCVSupportedExtension &RHS) {
188 return LHS < StringRef(RHS.Name);
189 }
190};
191} // namespace
192
193static std::optional<RISCVISAUtils::ExtensionVersion>
195 // Find default version of an extension.
196 // TODO: We might set default version based on profile or ISA spec.
197 for (auto &ExtInfo : {ArrayRef(SupportedExtensions),
198 ArrayRef(SupportedExperimentalExtensions)}) {
199 auto I = llvm::lower_bound(ExtInfo, ExtName, LessExtName());
200
201 if (I == ExtInfo.end() || I->Name != ExtName)
202 continue;
203
204 return I->Version;
205 }
206 return std::nullopt;
207}
208
210 if (Ext.starts_with('s'))
211 return "standard supervisor-level extension";
212 if (Ext.starts_with('x'))
213 return "non-standard user-level extension";
214 if (Ext.starts_with('z'))
215 return "standard user-level extension";
216 return StringRef();
217}
218
220 if (Ext.starts_with('s'))
221 return "s";
222 if (Ext.starts_with('x'))
223 return "x";
224 if (Ext.starts_with('z'))
225 return "z";
226 return StringRef();
227}
228
229static std::optional<RISCVISAUtils::ExtensionVersion>
231 auto I =
232 llvm::lower_bound(SupportedExperimentalExtensions, Ext, LessExtName());
233 if (I == std::end(SupportedExperimentalExtensions) || I->Name != Ext)
234 return std::nullopt;
235
236 return I->Version;
237}
238
240 bool IsExperimental = stripExperimentalPrefix(Ext);
241
243 IsExperimental ? ArrayRef(SupportedExperimentalExtensions)
244 : ArrayRef(SupportedExtensions);
245
246 auto I = llvm::lower_bound(ExtInfo, Ext, LessExtName());
247 return I != ExtInfo.end() && I->Name == Ext;
248}
249
251 verifyTables();
252
253 for (auto ExtInfo : {ArrayRef(SupportedExtensions),
254 ArrayRef(SupportedExperimentalExtensions)}) {
255 auto I = llvm::lower_bound(ExtInfo, Ext, LessExtName());
256 if (I != ExtInfo.end() && I->Name == Ext)
257 return true;
258 }
259
260 return false;
261}
262
263bool RISCVISAInfo::isSupportedExtension(StringRef Ext, unsigned MajorVersion,
264 unsigned MinorVersion) {
265 for (auto ExtInfo : {ArrayRef(SupportedExtensions),
266 ArrayRef(SupportedExperimentalExtensions)}) {
267 auto Range =
268 std::equal_range(ExtInfo.begin(), ExtInfo.end(), Ext, LessExtName());
269 for (auto I = Range.first, E = Range.second; I != E; ++I)
270 if (I->Version.Major == MajorVersion && I->Version.Minor == MinorVersion)
271 return true;
272 }
273
274 return false;
275}
276
279
280 if (!isSupportedExtension(Ext))
281 return false;
282
283 return Exts.count(Ext.str()) != 0;
284}
285
286std::vector<std::string> RISCVISAInfo::toFeatures(bool AddAllExtensions,
287 bool IgnoreUnknown) const {
288 std::vector<std::string> Features;
289 for (const auto &[ExtName, _] : Exts) {
290 if (IgnoreUnknown && !isSupportedExtension(ExtName))
291 continue;
292
293 if (isExperimentalExtension(ExtName)) {
294 Features.push_back((llvm::Twine("+experimental-") + ExtName).str());
295 } else {
296 Features.push_back((llvm::Twine("+") + ExtName).str());
297 }
298 }
299 if (AddAllExtensions) {
300 for (const RISCVSupportedExtension &Ext : SupportedExtensions) {
301 if (Exts.count(Ext.Name))
302 continue;
303 Features.push_back((llvm::Twine("-") + Ext.Name).str());
304 }
305
306 for (const RISCVSupportedExtension &Ext : SupportedExperimentalExtensions) {
307 if (Exts.count(Ext.Name))
308 continue;
309 Features.push_back((llvm::Twine("-experimental-") + Ext.Name).str());
310 }
311 }
312 return Features;
313}
314
315static Error getError(const Twine &Message) {
317}
318
320 if (ExtName.size() == 1) {
321 return getError("unsupported standard user-level extension '" + ExtName +
322 "'");
323 }
324 return getError("unsupported " + getExtensionTypeDesc(ExtName) + " '" +
325 ExtName + "'");
326}
327
328// Extensions may have a version number, and may be separated by
329// an underscore '_' e.g.: rv32i2_m2.
330// Version number is divided into major and minor version numbers,
331// separated by a 'p'. If the minor version is 0 then 'p0' can be
332// omitted from the version string. E.g., rv32i2p0, rv32i2, rv32i2p1.
333static Error getExtensionVersion(StringRef Ext, StringRef In, unsigned &Major,
334 unsigned &Minor, unsigned &ConsumeLength,
335 bool EnableExperimentalExtension,
336 bool ExperimentalExtensionVersionCheck) {
337 StringRef MajorStr, MinorStr;
338 Major = 0;
339 Minor = 0;
340 ConsumeLength = 0;
341 MajorStr = In.take_while(isDigit);
342 In = In.substr(MajorStr.size());
343
344 if (!MajorStr.empty() && In.consume_front("p")) {
345 MinorStr = In.take_while(isDigit);
346 In = In.substr(MajorStr.size() + MinorStr.size() - 1);
347
348 // Expected 'p' to be followed by minor version number.
349 if (MinorStr.empty()) {
350 return getError("minor version number missing after 'p' for extension '" +
351 Ext + "'");
352 }
353 }
354
355 if (!MajorStr.empty() && MajorStr.getAsInteger(10, Major))
356 return getError("Failed to parse major version number for extension '" +
357 Ext + "'");
358
359 if (!MinorStr.empty() && MinorStr.getAsInteger(10, Minor))
360 return getError("Failed to parse minor version number for extension '" +
361 Ext + "'");
362
363 ConsumeLength = MajorStr.size();
364
365 if (!MinorStr.empty())
366 ConsumeLength += MinorStr.size() + 1 /*'p'*/;
367
368 // Expected multi-character extension with version number to have no
369 // subsequent characters (i.e. must either end string or be followed by
370 // an underscore).
371 if (Ext.size() > 1 && In.size())
372 return getError(
373 "multi-character extensions must be separated by underscores");
374
375 // If experimental extension, require use of current version number
376 if (auto ExperimentalExtension = isExperimentalExtension(Ext)) {
377 if (!EnableExperimentalExtension)
378 return getError("requires '-menable-experimental-extensions' "
379 "for experimental extension '" +
380 Ext + "'");
381
382 if (ExperimentalExtensionVersionCheck &&
383 (MajorStr.empty() && MinorStr.empty()))
384 return getError(
385 "experimental extension requires explicit version number `" + Ext +
386 "`");
387
388 auto SupportedVers = *ExperimentalExtension;
389 if (ExperimentalExtensionVersionCheck &&
390 (Major != SupportedVers.Major || Minor != SupportedVers.Minor)) {
391 std::string Error = "unsupported version number " + MajorStr.str();
392 if (!MinorStr.empty())
393 Error += "." + MinorStr.str();
394 Error += " for experimental extension '" + Ext.str() +
395 "' (this compiler supports " + utostr(SupportedVers.Major) +
396 "." + utostr(SupportedVers.Minor) + ")";
397 return getError(Error);
398 }
399 return Error::success();
400 }
401
402 // Exception rule for `g`, we don't have clear version scheme for that on
403 // ISA spec.
404 if (Ext == "g")
405 return Error::success();
406
407 if (MajorStr.empty() && MinorStr.empty()) {
408 if (auto DefaultVersion = findDefaultVersion(Ext)) {
409 Major = DefaultVersion->Major;
410 Minor = DefaultVersion->Minor;
411 }
412 // No matter found or not, return success, assume other place will
413 // verify.
414 return Error::success();
415 }
416
417 if (RISCVISAInfo::isSupportedExtension(Ext, Major, Minor))
418 return Error::success();
419
421 return getErrorForInvalidExt(Ext);
422
423 std::string Error = "unsupported version number " + MajorStr.str();
424 if (!MinorStr.empty())
425 Error += "." + MinorStr.str();
426 Error += " for extension '" + Ext.str() + "'";
427 return getError(Error);
428}
429
433 assert(XLen == 32 || XLen == 64);
434 std::unique_ptr<RISCVISAInfo> ISAInfo(new RISCVISAInfo(XLen));
435
436 ISAInfo->Exts = Exts;
437
438 return RISCVISAInfo::postProcessAndChecking(std::move(ISAInfo));
439}
440
443 const std::vector<std::string> &Features) {
444 assert(XLen == 32 || XLen == 64);
445 std::unique_ptr<RISCVISAInfo> ISAInfo(new RISCVISAInfo(XLen));
446
447 for (StringRef ExtName : Features) {
448 assert(ExtName.size() > 1 && (ExtName[0] == '+' || ExtName[0] == '-'));
449 bool Add = ExtName[0] == '+';
450 ExtName = ExtName.drop_front(1); // Drop '+' or '-'
451 bool Experimental = stripExperimentalPrefix(ExtName);
452 auto ExtensionInfos = Experimental
453 ? ArrayRef(SupportedExperimentalExtensions)
454 : ArrayRef(SupportedExtensions);
455 auto ExtensionInfoIterator =
456 llvm::lower_bound(ExtensionInfos, ExtName, LessExtName());
457
458 // Not all features is related to ISA extension, like `relax` or
459 // `save-restore`, skip those feature.
460 if (ExtensionInfoIterator == ExtensionInfos.end() ||
461 ExtensionInfoIterator->Name != ExtName)
462 continue;
463
464 if (Add)
465 ISAInfo->Exts[ExtName.str()] = ExtensionInfoIterator->Version;
466 else
467 ISAInfo->Exts.erase(ExtName.str());
468 }
469
470 return RISCVISAInfo::postProcessAndChecking(std::move(ISAInfo));
471}
472
475 // RISC-V ISA strings must be [a-z0-9_]
476 if (!llvm::all_of(
477 Arch, [](char C) { return isDigit(C) || isLower(C) || C == '_'; }))
478 return getError("string may only contain [a-z0-9_]");
479
480 // Must start with a valid base ISA name.
481 unsigned XLen = 0;
482 if (Arch.consume_front("rv32"))
483 XLen = 32;
484 else if (Arch.consume_front("rv64"))
485 XLen = 64;
486
487 if (XLen == 0 || Arch.empty() || (Arch[0] != 'i' && Arch[0] != 'e'))
488 return getError("arch string must begin with valid base ISA");
489
490 std::unique_ptr<RISCVISAInfo> ISAInfo(new RISCVISAInfo(XLen));
491
492 // Each extension is of the form ${name}${major_version}p${minor_version}
493 // and separated by _. Split by _ and then extract the name and version
494 // information for each extension.
495 while (!Arch.empty()) {
496 if (Arch[0] == '_') {
497 if (Arch.size() == 1 || Arch[1] == '_')
498 return getError("extension name missing after separator '_'");
499 Arch = Arch.drop_front();
500 }
501
502 size_t Idx = Arch.find('_');
503 StringRef Ext = Arch.slice(0, Idx);
504 Arch = Arch.substr(Idx);
505
506 StringRef Prefix, MinorVersionStr;
507 std::tie(Prefix, MinorVersionStr) = Ext.rsplit('p');
508 if (MinorVersionStr.empty())
509 return getError("extension lacks version in expected format");
510 unsigned MajorVersion, MinorVersion;
511 if (MinorVersionStr.getAsInteger(10, MinorVersion))
512 return getError("failed to parse minor version number");
513
514 // Split Prefix into the extension name and the major version number
515 // (the trailing digits of Prefix).
516 size_t VersionStart = Prefix.size();
517 while (VersionStart != 0) {
518 if (!isDigit(Prefix[VersionStart - 1]))
519 break;
520 --VersionStart;
521 }
522 if (VersionStart == Prefix.size())
523 return getError("extension lacks version in expected format");
524
525 if (VersionStart == 0)
526 return getError("missing extension name");
527
528 StringRef ExtName = Prefix.slice(0, VersionStart);
529 StringRef MajorVersionStr = Prefix.substr(VersionStart);
530 if (MajorVersionStr.getAsInteger(10, MajorVersion))
531 return getError("failed to parse major version number");
532
533 if ((ExtName[0] == 'z' || ExtName[0] == 's' || ExtName[0] == 'x') &&
534 (ExtName.size() == 1 || isDigit(ExtName[1])))
535 return getError("'" + Twine(ExtName[0]) +
536 "' must be followed by a letter");
537
538 if (!ISAInfo->Exts
539 .emplace(
540 ExtName.str(),
541 RISCVISAUtils::ExtensionVersion{MajorVersion, MinorVersion})
542 .second)
543 return getError("duplicate extension '" + ExtName + "'");
544 }
545 ISAInfo->updateImpliedLengths();
546 return std::move(ISAInfo);
547}
548
550RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
551 bool ExperimentalExtensionVersionCheck) {
552 // RISC-V ISA strings must be [a-z0-9_]
553 if (!llvm::all_of(
554 Arch, [](char C) { return isDigit(C) || isLower(C) || C == '_'; }))
555 return getError("string may only contain [a-z0-9_]");
556
557 // ISA string must begin with rv32, rv64, or a profile.
558 unsigned XLen = 0;
559 if (Arch.consume_front("rv32")) {
560 XLen = 32;
561 } else if (Arch.consume_front("rv64")) {
562 XLen = 64;
563 } else {
564 // Try parsing as a profile.
565 auto ProfileCmp = [](StringRef Arch, const RISCVProfile &Profile) {
566 return Arch < Profile.Name;
567 };
568 auto I = llvm::upper_bound(SupportedProfiles, Arch, ProfileCmp);
569 bool FoundProfile = I != std::begin(SupportedProfiles) &&
570 Arch.starts_with(std::prev(I)->Name);
571 if (!FoundProfile) {
572 I = llvm::upper_bound(SupportedExperimentalProfiles, Arch, ProfileCmp);
573 FoundProfile = (I != std::begin(SupportedExperimentalProfiles) &&
574 Arch.starts_with(std::prev(I)->Name));
575 if (FoundProfile && !EnableExperimentalExtension) {
576 return getError("requires '-menable-experimental-extensions' "
577 "for profile '" +
578 std::prev(I)->Name + "'");
579 }
580 }
581 if (FoundProfile) {
582 --I;
583 std::string NewArch = I->MArch.str();
584 StringRef ArchWithoutProfile = Arch.drop_front(I->Name.size());
585 if (!ArchWithoutProfile.empty()) {
586 if (ArchWithoutProfile.front() != '_')
587 return getError("additional extensions must be after separator '_'");
588 NewArch += ArchWithoutProfile.str();
589 }
590 return parseArchString(NewArch, EnableExperimentalExtension,
591 ExperimentalExtensionVersionCheck);
592 }
593 }
594
595 if (XLen == 0 || Arch.empty())
596 return getError(
597 "string must begin with rv32{i,e,g}, rv64{i,e,g}, or a supported "
598 "profile name");
599
600 std::unique_ptr<RISCVISAInfo> ISAInfo(new RISCVISAInfo(XLen));
601
602 // The canonical order specified in ISA manual.
603 // Ref: Table 22.1 in RISC-V User-Level ISA V2.2
604 char Baseline = Arch.front();
605 // Skip the baseline.
606 Arch = Arch.drop_front();
607
608 unsigned Major, Minor, ConsumeLength;
609
610 // First letter should be 'e', 'i' or 'g'.
611 switch (Baseline) {
612 default:
613 return getError("first letter after \'rv" + Twine(XLen) +
614 "\' should be 'e', 'i' or 'g'");
615 case 'e':
616 case 'i':
617 // Baseline is `i` or `e`
618 if (auto E = getExtensionVersion(
619 StringRef(&Baseline, 1), Arch, Major, Minor, ConsumeLength,
620 EnableExperimentalExtension, ExperimentalExtensionVersionCheck))
621 return std::move(E);
622
623 ISAInfo->Exts[std::string(1, Baseline)] = {Major, Minor};
624 break;
625 case 'g':
626 // g expands to extensions in RISCVGImplications.
627 if (!Arch.empty() && isDigit(Arch.front()))
628 return getError("version not supported for 'g'");
629
630 // Versions for g are disallowed, and this was checked for previously.
631 ConsumeLength = 0;
632
633 // No matter which version is given to `g`, we always set imafd to default
634 // version since the we don't have clear version scheme for that on
635 // ISA spec.
636 for (const char *Ext : RISCVGImplications) {
637 auto Version = findDefaultVersion(Ext);
638 assert(Version && "Default extension version not found?");
639 ISAInfo->Exts[std::string(Ext)] = {Version->Major, Version->Minor};
640 }
641 break;
642 }
643
644 // Consume the base ISA version number and any '_' between rvxxx and the
645 // first extension
646 Arch = Arch.drop_front(ConsumeLength);
647
648 while (!Arch.empty()) {
649 if (Arch.front() == '_') {
650 if (Arch.size() == 1 || Arch[1] == '_')
651 return getError("extension name missing after separator '_'");
652 Arch = Arch.drop_front();
653 }
654
655 size_t Idx = Arch.find('_');
656 StringRef Ext = Arch.slice(0, Idx);
657 Arch = Arch.substr(Idx);
658
659 do {
660 StringRef Name, Vers, Desc;
661 if (RISCVISAUtils::AllStdExts.contains(Ext.front())) {
662 Name = Ext.take_front(1);
663 Ext = Ext.drop_front();
664 Vers = Ext;
665 Desc = "standard user-level extension";
666 } else if (Ext.front() == 'z' || Ext.front() == 's' ||
667 Ext.front() == 'x') {
668 // Handle other types of extensions other than the standard
669 // general purpose and standard user-level extensions.
670 // Parse the ISA string containing non-standard user-level
671 // extensions, standard supervisor-level extensions and
672 // non-standard supervisor-level extensions.
673 // These extensions start with 'z', 's', 'x' prefixes, might have a
674 // version number (major, minor) and are separated by a single
675 // underscore '_'. We do not enforce a canonical order for them.
678 auto Pos = findLastNonVersionCharacter(Ext) + 1;
679 Name = Ext.substr(0, Pos);
680 Vers = Ext.substr(Pos);
681 Ext = StringRef();
682
683 assert(!Type.empty() && "Empty type?");
684 if (Name.size() == Type.size())
685 return getError(Desc + " name missing after '" + Type + "'");
686 } else {
687 return getError("invalid standard user-level extension '" +
688 Twine(Ext.front()) + "'");
689 }
690
691 unsigned Major, Minor, ConsumeLength;
692 if (auto E = getExtensionVersion(Name, Vers, Major, Minor, ConsumeLength,
693 EnableExperimentalExtension,
694 ExperimentalExtensionVersionCheck))
695 return E;
696
697 if (Name.size() == 1)
698 Ext = Ext.substr(ConsumeLength);
699
701 return getErrorForInvalidExt(Name);
702
703 // Insert and error for duplicates.
704 if (!ISAInfo->Exts
705 .emplace(Name.str(),
707 .second)
708 return getError("duplicated " + Desc + " '" + Name + "'");
709
710 } while (!Ext.empty());
711 }
712
713 // We add Zicsr/Zifenci as final to allow duplicated "zicsr"/"zifencei" like
714 // "rv64g_zicsr_zifencei".
715 if (Baseline == 'g') {
716 for (const char *Ext : RISCVGImplicationsZi) {
717 if (ISAInfo->Exts.count(Ext))
718 continue;
719
720 auto Version = findDefaultVersion(Ext);
721 assert(Version && "Default extension version not found?");
722 ISAInfo->Exts[std::string(Ext)] = {Version->Major, Version->Minor};
723 }
724 }
725
726 return RISCVISAInfo::postProcessAndChecking(std::move(ISAInfo));
727}
728
730 return getError("'" + Ext1 + "' and '" + Ext2 +
731 "' extensions are incompatible");
732}
733
735 return getError("'" + Ext + "' requires '" + ReqExt +
736 "' extension to also be specified");
737}
738
739Error RISCVISAInfo::checkDependency() {
740 bool HasE = Exts.count("e") != 0;
741 bool HasI = Exts.count("i") != 0;
742 bool HasC = Exts.count("c") != 0;
743 bool HasF = Exts.count("f") != 0;
744 bool HasD = Exts.count("d") != 0;
745 bool HasZfinx = Exts.count("zfinx") != 0;
746 bool HasVector = Exts.count("zve32x") != 0;
747 bool HasZvl = MinVLen != 0;
748 bool HasZcmp = Exts.count("zcmp") != 0;
749 bool HasXqccmp = Exts.count("xqccmp") != 0;
750
751 static constexpr StringLiteral XqciExts[] = {
752 {"xqcia"}, {"xqciac"}, {"xqcibi"}, {"xqcibm"}, {"xqcicli"},
753 {"xqcicm"}, {"xqcics"}, {"xqcicsr"}, {"xqciint"}, {"xqciio"},
754 {"xqcilb"}, {"xqcili"}, {"xqcilia"}, {"xqcilo"}, {"xqcilsm"},
755 {"xqcisim"}, {"xqcisls"}, {"xqcisync"}};
756 static constexpr StringLiteral ZcdOverlaps[] = {
757 {"zcmt"}, {"zcmp"}, {"xqccmp"}, {"xqciac"}, {"xqcicm"}};
758
759 if (HasI && HasE)
760 return getIncompatibleError("i", "e");
761
762 if (HasF && HasZfinx)
763 return getIncompatibleError("f", "zfinx");
764
765 if (HasZvl && !HasVector)
766 return getExtensionRequiresError("zvl*b", "v' or 'zve*");
767
768 if (HasD && (HasC || Exts.count("zcd")))
769 for (auto Ext : ZcdOverlaps)
770 if (Exts.count(Ext.str()))
771 return getError(
772 Twine("'") + Ext + "' extension is incompatible with '" +
773 (HasC ? "c" : "zcd") + "' extension when 'd' extension is enabled");
774
775 if (XLen != 32 && Exts.count("zcf"))
776 return getError("'zcf' is only supported for 'rv32'");
777
778 if (Exts.count("xwchc") != 0) {
779 if (XLen != 32)
780 return getError("'xwchc' is only supported for 'rv32'");
781
782 if (HasD)
783 return getIncompatibleError("d", "xwchc");
784
785 if (Exts.count("zcb") != 0)
786 return getIncompatibleError("xwchc", "zcb");
787 }
788
789 if (Exts.count("zclsd") != 0) {
790 if (XLen != 32)
791 return getError("'zclsd' is only supported for 'rv32'");
792
793 if (Exts.count("zcf") != 0)
794 return getIncompatibleError("zclsd", "zcf");
795 }
796
797 if (XLen != 32 && Exts.count("zilsd") != 0)
798 return getError("'zilsd' is only supported for 'rv32'");
799
800 for (auto Ext : XqciExts)
801 if (Exts.count(Ext.str()) && (XLen != 32))
802 return getError("'" + Twine(Ext) + "'" + " is only supported for 'rv32'");
803
804 if (HasZcmp && HasXqccmp)
805 return getIncompatibleError("zcmp", "xqccmp");
806
807 return Error::success();
808}
809
812 const char *ImpliedExt;
813
814 bool operator<(const ImpliedExtsEntry &Other) const {
815 return Name < Other.Name;
816 }
817};
818
820 return LHS.Name < RHS;
821}
822
824 return LHS < RHS.Name;
825}
826
827#define GET_IMPLIED_EXTENSIONS
828#include "llvm/TargetParser/RISCVTargetParserDef.inc"
829
830void RISCVISAInfo::updateImplication() {
831 assert(llvm::is_sorted(ImpliedExts) && "Table not sorted by Name");
832
833 // This loop may execute over 1 iteration since implication can be layered
834 // Exits loop if no more implication is applied
836 for (auto const &Ext : Exts)
837 WorkList.push_back(Ext.first);
838
839 while (!WorkList.empty()) {
840 StringRef ExtName = WorkList.pop_back_val();
841 auto Range = std::equal_range(std::begin(ImpliedExts),
842 std::end(ImpliedExts), ExtName);
843 for (const ImpliedExtsEntry &Implied : llvm::make_range(Range)) {
844 const char *ImpliedExt = Implied.ImpliedExt;
845 auto [It, Inserted] = Exts.try_emplace(ImpliedExt);
846 if (!Inserted)
847 continue;
848 auto Version = findDefaultVersion(ImpliedExt);
849 It->second = *Version;
850 WorkList.push_back(ImpliedExt);
851 }
852 }
853
854 // Add Zcd if C and D are enabled.
855 if (Exts.count("c") && Exts.count("d") && !Exts.count("zcd")) {
856 auto Version = findDefaultVersion("zcd");
857 Exts["zcd"] = *Version;
858 }
859
860 // Add Zcf if C and F are enabled on RV32.
861 if (XLen == 32 && Exts.count("c") && Exts.count("f") && !Exts.count("zcf")) {
862 auto Version = findDefaultVersion("zcf");
863 Exts["zcf"] = *Version;
864 }
865
866 // Add Zcf if Zce and F are enabled on RV32.
867 if (XLen == 32 && Exts.count("zce") && Exts.count("f") &&
868 !Exts.count("zcf")) {
869 auto Version = findDefaultVersion("zcf");
870 Exts["zcf"] = *Version;
871 }
872
873 // Handle I/E after implications have been resolved, in case either
874 // of them was implied by another extension.
875 bool HasE = Exts.count("e") != 0;
876 bool HasI = Exts.count("i") != 0;
877
878 // If not in e extension and i extension does not exist, i extension is
879 // implied
880 if (!HasE && !HasI) {
881 auto Version = findDefaultVersion("i");
882 Exts["i"] = *Version;
883 }
884
885 if (HasE && HasI)
886 Exts.erase("i");
887}
888
889static constexpr StringLiteral CombineIntoExts[] = {
890 {"b"}, {"zk"}, {"zkn"}, {"zks"}, {"zvkn"},
891 {"zvknc"}, {"zvkng"}, {"zvks"}, {"zvksc"}, {"zvksg"},
892};
893
894void RISCVISAInfo::updateCombination() {
895 bool MadeChange = false;
896 do {
897 MadeChange = false;
898 for (StringRef CombineExt : CombineIntoExts) {
899 if (Exts.count(CombineExt.str()))
900 continue;
901
902 // Look up the extension in the ImpliesExt table to find everything it
903 // depends on.
904 auto Range = std::equal_range(std::begin(ImpliedExts),
905 std::end(ImpliedExts), CombineExt);
906 bool HasAllRequiredFeatures = std::all_of(
907 Range.first, Range.second, [&](const ImpliedExtsEntry &Implied) {
908 return Exts.count(Implied.ImpliedExt);
909 });
910 if (HasAllRequiredFeatures) {
911 auto Version = findDefaultVersion(CombineExt);
912 Exts[CombineExt.str()] = *Version;
913 MadeChange = true;
914 }
915 }
916 } while (MadeChange);
917}
918
919void RISCVISAInfo::updateImpliedLengths() {
920 assert(FLen == 0 && MaxELenFp == 0 && MaxELen == 0 && MinVLen == 0 &&
921 "Expected lengths to be initialied to zero");
922
923 if (Exts.count("q"))
924 FLen = 128;
925 else if (Exts.count("d"))
926 FLen = 64;
927 else if (Exts.count("f"))
928 FLen = 32;
929
930 if (Exts.count("v")) {
931 MaxELenFp = std::max(MaxELenFp, 64u);
932 MaxELen = std::max(MaxELen, 64u);
933 }
934
935 for (auto const &Ext : Exts) {
936 StringRef ExtName = Ext.first;
937 // Infer MaxELen and MaxELenFp from Zve(32/64)(x/f/d)
938 if (ExtName.consume_front("zve")) {
939 unsigned ZveELen;
940 if (ExtName.consumeInteger(10, ZveELen))
941 continue;
942
943 if (ExtName == "f")
944 MaxELenFp = std::max(MaxELenFp, 32u);
945 else if (ExtName == "d")
946 MaxELenFp = std::max(MaxELenFp, 64u);
947 else if (ExtName != "x")
948 continue;
949
950 MaxELen = std::max(MaxELen, ZveELen);
951 continue;
952 }
953
954 // Infer MinVLen from zvl*b.
955 if (ExtName.consume_front("zvl")) {
956 unsigned ZvlLen;
957 if (ExtName.consumeInteger(10, ZvlLen))
958 continue;
959
960 if (ExtName != "b")
961 continue;
962
963 MinVLen = std::max(MinVLen, ZvlLen);
964 continue;
965 }
966 }
967}
968
969std::string RISCVISAInfo::toString() const {
970 std::string Buffer;
971 raw_string_ostream Arch(Buffer);
972
973 Arch << "rv" << XLen;
974
975 ListSeparator LS("_");
976 for (auto const &Ext : Exts) {
977 StringRef ExtName = Ext.first;
978 auto ExtInfo = Ext.second;
979 Arch << LS << ExtName;
980 Arch << ExtInfo.Major << "p" << ExtInfo.Minor;
981 }
982
983 return Arch.str();
984}
985
987RISCVISAInfo::postProcessAndChecking(std::unique_ptr<RISCVISAInfo> &&ISAInfo) {
988 ISAInfo->updateImplication();
989 ISAInfo->updateCombination();
990 ISAInfo->updateImpliedLengths();
991
992 if (Error Result = ISAInfo->checkDependency())
993 return std::move(Result);
994 return std::move(ISAInfo);
995}
996
998 if (XLen == 32) {
999 if (Exts.count("e"))
1000 return "ilp32e";
1001 if (Exts.count("d"))
1002 return "ilp32d";
1003 if (Exts.count("f"))
1004 return "ilp32f";
1005 return "ilp32";
1006 } else if (XLen == 64) {
1007 if (Exts.count("e"))
1008 return "lp64e";
1009 if (Exts.count("d"))
1010 return "lp64d";
1011 if (Exts.count("f"))
1012 return "lp64f";
1013 return "lp64";
1014 }
1015 llvm_unreachable("Invalid XLEN");
1016}
1017
1019 if (Ext.empty())
1020 return false;
1021
1022 auto Pos = findLastNonVersionCharacter(Ext) + 1;
1023 StringRef Name = Ext.substr(0, Pos);
1024 StringRef Vers = Ext.substr(Pos);
1025 if (Vers.empty())
1026 return false;
1027
1028 unsigned Major, Minor, ConsumeLength;
1029 if (auto E = getExtensionVersion(Name, Vers, Major, Minor, ConsumeLength,
1030 true, true)) {
1031 consumeError(std::move(E));
1032 return false;
1033 }
1034
1035 return true;
1036}
1037
1039 if (Ext.empty())
1040 return std::string();
1041
1042 auto Pos = findLastNonVersionCharacter(Ext) + 1;
1043 StringRef Name = Ext.substr(0, Pos);
1044
1045 if (Pos != Ext.size() && !isSupportedExtensionWithVersion(Ext))
1046 return std::string();
1047
1048 if (!isSupportedExtension(Name))
1049 return std::string();
1050
1051 return isExperimentalExtension(Name) ? "experimental-" + Name.str()
1052 : Name.str();
1053}
1054
1060
1062 const char *Name;
1063 unsigned GroupID;
1064 unsigned BitPosition;
1065};
1066
1067#define GET_RISCVExtensionBitmaskTable_IMPL
1068#include "llvm/TargetParser/RISCVTargetParserDef.inc"
1069
1071 // Note that this code currently accepts mixed case extension names, but
1072 // does not handle extension versions at all. That's probably fine because
1073 // there's only one extension version in the __riscv_feature_bits vector.
1074 for (auto E : ExtensionBitmask)
1075 if (Ext.equals_insensitive(E.Name))
1076 return std::make_pair(E.GroupID, E.BitPosition);
1077 return std::make_pair(-1, -1);
1078}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define _
#define I(x, y, z)
Definition MD5.cpp:58
Load MIR Sample Profile
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
#define P(N)
static void verifyTables()
static StringRef getExtensionTypeDesc(StringRef Ext)
static const char * RISCVGImplicationsZi[]
static std::optional< RISCVISAUtils::ExtensionVersion > findDefaultVersion(StringRef ExtName)
static size_t findLastNonVersionCharacter(StringRef Ext)
static Error getExtensionRequiresError(StringRef Ext, StringRef ReqExt)
static StringRef getExtensionType(StringRef Ext)
static Error getErrorForInvalidExt(StringRef ExtName)
static constexpr StringLiteral CombineIntoExts[]
static bool stripExperimentalPrefix(StringRef &Ext)
static std::optional< RISCVISAUtils::ExtensionVersion > isExperimentalExtension(StringRef Ext)
static void PrintExtension(StringRef Name, StringRef Version, StringRef Description)
static Error getExtensionVersion(StringRef Ext, StringRef In, unsigned &Major, unsigned &Minor, unsigned &ConsumeLength, bool EnableExperimentalExtension, bool ExperimentalExtensionVersionCheck)
static Error getIncompatibleError(StringRef Ext1, StringRef Ext2)
static Error getError(const Twine &Message)
static const char * RISCVGImplications[]
This file contains some templates that are useful if you are working with the STL at all.
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:480
This file contains some functions that are useful when dealing with strings.
static void verifyTables()
Value * RHS
Value * LHS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
iterator end() const
Definition ArrayRef.h:136
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
A helper class to return the specified delimiter string after the first invocation of operator String...
static LLVM_ABI bool isSupportedExtensionFeature(StringRef Ext)
static LLVM_ABI std::string getTargetFeatureForExtension(StringRef Ext)
static LLVM_ABI llvm::Expected< std::unique_ptr< RISCVISAInfo > > parseNormalizedArchString(StringRef Arch)
Parse RISC-V ISA info from an arch string that is already in normalized form (as defined in the psABI...
LLVM_ABI bool hasExtension(StringRef Ext) const
RISCVISAInfo(const RISCVISAInfo &)=delete
LLVM_ABI std::string toString() const
LLVM_ABI StringRef computeDefaultABI() const
static LLVM_ABI llvm::Expected< std::unique_ptr< RISCVISAInfo > > parseArchString(StringRef Arch, bool EnableExperimentalExtension, bool ExperimentalExtensionVersionCheck=true)
Parse RISC-V ISA info from arch string.
static LLVM_ABI bool isSupportedExtension(StringRef Ext)
static LLVM_ABI void printEnabledExtensions(bool IsRV64, std::set< StringRef > &EnabledFeatureNames, StringMap< StringRef > &DescMap)
static LLVM_ABI void printSupportedExtensions(StringMap< StringRef > &DescMap)
static LLVM_ABI llvm::Expected< std::unique_ptr< RISCVISAInfo > > parseFeatures(unsigned XLen, const std::vector< std::string > &Features)
Parse RISC-V ISA info from feature vector.
static LLVM_ABI std::pair< int, int > getRISCVFeaturesBitsInfo(StringRef Ext)
Return the group id and bit position of __riscv_feature_bits.
static LLVM_ABI llvm::Expected< std::unique_ptr< RISCVISAInfo > > createFromExtMap(unsigned XLen, const RISCVISAUtils::OrderedExtensionMap &Exts)
LLVM_ABI std::vector< std::string > toFeatures(bool AddAllExtensions=false, bool IgnoreUnknown=true) const
Convert RISC-V ISA info to a feature vector.
static LLVM_ABI bool isSupportedExtensionWithVersion(StringRef Ext)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition StringRef.h:862
bool empty() const
Definition StringMap.h:108
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:133
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
bool consumeInteger(unsigned Radix, T &Result)
Parse the current string as an integer of the specified radix.
Definition StringRef.h:509
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition StringRef.h:480
std::string str() const
str - Get the contents as an std::string.
Definition StringRef.h:233
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:581
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition StringRef.h:269
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:151
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition StringRef.h:619
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition StringRef.h:694
constexpr size_t size() const
size - Get the string size.
Definition StringRef.h:154
char front() const
front - Get the first character in the string.
Definition StringRef.h:157
bool consume_front(StringRef Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
Definition StringRef.h:645
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition StringRef.h:301
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
A raw_ostream that writes to an std::string.
std::string & str()
Returns the string's reference.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
constexpr StringLiteral AllStdExts
std::map< std::string, ExtensionVersion, ExtensionComparator > OrderedExtensionMap
OrderedExtensionMap is std::map, it's specialized to keep entries in canonical order of extension.
This is an optimization pass for GlobalISel generic memory operations.
bool operator<(int64_t V1, const APSInt &V2)
Definition APSInt.h:362
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1707
bool isLower(char C)
Checks if character C is a lowercase letter as classified by "C" locale.
LLVM_ABI raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
std::string utostr(uint64_t X, bool isNeg=false)
auto upper_bound(R &&Range, T &&Value)
Provide wrappers to std::upper_bound which take ranges instead of having to pass begin/end explicitly...
Definition STLExtras.h:1989
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1305
Op::Description Desc
@ invalid_argument
Definition Errc.h:56
FunctionAddr VTableAddr uintptr_t uintptr_t Version
Definition InstrProf.h:302
bool isDigit(char C)
Checks if character C is one of the 10 decimal digits.
bool is_sorted(R &&Range, Compare C)
Wrapper function around std::is_sorted to check if elements in a range R are sorted with respect to a...
Definition STLExtras.h:1902
@ Other
Any other memory.
Definition ModRef.h:68
FormattedString left_justify(StringRef Str, unsigned Width)
left_justify - append spaces after string so total output is Width characters.
Definition Format.h:147
auto lower_bound(R &&Range, T &&Value)
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
Definition STLExtras.h:1976
@ Add
Sum of integers.
ArrayRef(const T &OneElt) -> ArrayRef< T >
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1083
StringLiteral Name
bool operator<(const ImpliedExtsEntry &Other) const
const char * ImpliedExt
const StringLiteral ext
Represents the major and version number components of a RISC-V extension.