LLVM 19.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/MapVector.h"
11#include "llvm/ADT/STLExtras.h"
12#include "llvm/ADT/SetVector.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/Support/Errc.h"
16#include "llvm/Support/Error.h"
18
19#include <array>
20#include <atomic>
21#include <optional>
22#include <string>
23#include <vector>
24
25using namespace llvm;
26
27namespace {
28
29struct RISCVSupportedExtension {
30 const char *Name;
31 /// Supported version.
33
34 bool operator<(const RISCVSupportedExtension &RHS) const {
35 return StringRef(Name) < StringRef(RHS.Name);
36 }
37};
38
39struct RISCVProfile {
41 StringLiteral MArch;
42
43 bool operator<(const RISCVProfile &RHS) const {
44 return StringRef(Name) < StringRef(RHS.Name);
45 }
46};
47
48} // end anonymous namespace
49
50static const char *RISCVGImplications[] = {
51 "i", "m", "a", "f", "d", "zicsr", "zifencei"
52};
53
54#define GET_SUPPORTED_EXTENSIONS
55#include "llvm/TargetParser/RISCVTargetParserDef.inc"
56
57#define GET_SUPPORTED_PROFILES
58#include "llvm/TargetParser/RISCVTargetParserDef.inc"
59
60static void verifyTables() {
61#ifndef NDEBUG
62 static std::atomic<bool> TableChecked(false);
63 if (!TableChecked.load(std::memory_order_relaxed)) {
64 assert(llvm::is_sorted(SupportedExtensions) &&
65 "Extensions are not sorted by name");
66 assert(llvm::is_sorted(SupportedExperimentalExtensions) &&
67 "Experimental extensions are not sorted by name");
68 assert(llvm::is_sorted(SupportedProfiles) &&
69 "Profiles are not sorted by name");
70 assert(llvm::is_sorted(SupportedExperimentalProfiles) &&
71 "Experimental profiles are not sorted by name");
72 TableChecked.store(true, std::memory_order_relaxed);
73 }
74#endif
75}
76
78 StringRef Description) {
79 outs().indent(4);
80 unsigned VersionWidth = Description.empty() ? 0 : 10;
81 outs() << left_justify(Name, 21) << left_justify(Version, VersionWidth)
82 << Description << "\n";
83}
84
86
87 outs() << "All available -march extensions for RISC-V\n\n";
88 PrintExtension("Name", "Version", (DescMap.empty() ? "" : "Description"));
89
91 for (const auto &E : SupportedExtensions)
92 ExtMap[E.Name] = {E.Version.Major, E.Version.Minor};
93 for (const auto &E : ExtMap) {
94 std::string Version =
95 std::to_string(E.second.Major) + "." + std::to_string(E.second.Minor);
96 PrintExtension(E.first, Version, DescMap[E.first]);
97 }
98
99 outs() << "\nExperimental extensions\n";
100 ExtMap.clear();
101 for (const auto &E : SupportedExperimentalExtensions)
102 ExtMap[E.Name] = {E.Version.Major, E.Version.Minor};
103 for (const auto &E : ExtMap) {
104 std::string Version =
105 std::to_string(E.second.Major) + "." + std::to_string(E.second.Minor);
106 PrintExtension(E.first, Version, DescMap["experimental-" + E.first]);
107 }
108
109 outs() << "\nSupported Profiles\n";
110 for (const auto &P : SupportedProfiles)
111 outs().indent(4) << P.Name << "\n";
112
113 outs() << "\nExperimental Profiles\n";
114 for (const auto &P : SupportedExperimentalProfiles)
115 outs().indent(4) << P.Name << "\n";
116
117 outs() << "\nUse -march to specify the target's extension.\n"
118 "For example, clang -march=rv32i_v1p0\n";
119}
120
122 return Ext.consume_front("experimental-");
123}
124
125// This function finds the last character that doesn't belong to a version
126// (e.g. zba1p0 is extension 'zba' of version '1p0'). So the function will
127// consume [0-9]*p[0-9]* starting from the backward. An extension name will not
128// end with a digit or the letter 'p', so this function will parse correctly.
129// NOTE: This function is NOT able to take empty strings or strings that only
130// have version numbers and no extension name. It assumes the extension name
131// will be at least more than one character.
133 assert(!Ext.empty() &&
134 "Already guarded by if-statement in ::parseArchString");
135
136 int Pos = Ext.size() - 1;
137 while (Pos > 0 && isDigit(Ext[Pos]))
138 Pos--;
139 if (Pos > 0 && Ext[Pos] == 'p' && isDigit(Ext[Pos - 1])) {
140 Pos--;
141 while (Pos > 0 && isDigit(Ext[Pos]))
142 Pos--;
143 }
144 return Pos;
145}
146
147namespace {
148struct LessExtName {
149 bool operator()(const RISCVSupportedExtension &LHS, StringRef RHS) {
150 return StringRef(LHS.Name) < RHS;
151 }
152 bool operator()(StringRef LHS, const RISCVSupportedExtension &RHS) {
153 return LHS < StringRef(RHS.Name);
154 }
155};
156} // namespace
157
158static std::optional<RISCVISAUtils::ExtensionVersion>
160 // Find default version of an extension.
161 // TODO: We might set default version based on profile or ISA spec.
162 for (auto &ExtInfo : {ArrayRef(SupportedExtensions),
163 ArrayRef(SupportedExperimentalExtensions)}) {
164 auto I = llvm::lower_bound(ExtInfo, ExtName, LessExtName());
165
166 if (I == ExtInfo.end() || I->Name != ExtName)
167 continue;
168
169 return I->Version;
170 }
171 return std::nullopt;
172}
173
174bool RISCVISAInfo::addExtension(StringRef ExtName,
176 return Exts.emplace(ExtName, Version).second;
177}
178
180 if (Ext.starts_with('s'))
181 return "standard supervisor-level extension";
182 if (Ext.starts_with('x'))
183 return "non-standard user-level extension";
184 if (Ext.starts_with('z'))
185 return "standard user-level extension";
186 return StringRef();
187}
188
190 if (Ext.starts_with('s'))
191 return "s";
192 if (Ext.starts_with('x'))
193 return "x";
194 if (Ext.starts_with('z'))
195 return "z";
196 return StringRef();
197}
198
199static std::optional<RISCVISAUtils::ExtensionVersion>
201 auto I =
202 llvm::lower_bound(SupportedExperimentalExtensions, Ext, LessExtName());
203 if (I == std::end(SupportedExperimentalExtensions) || I->Name != Ext)
204 return std::nullopt;
205
206 return I->Version;
207}
208
210 bool IsExperimental = stripExperimentalPrefix(Ext);
211
213 IsExperimental ? ArrayRef(SupportedExperimentalExtensions)
214 : ArrayRef(SupportedExtensions);
215
216 auto I = llvm::lower_bound(ExtInfo, Ext, LessExtName());
217 return I != ExtInfo.end() && I->Name == Ext;
218}
219
221 verifyTables();
222
223 for (auto ExtInfo : {ArrayRef(SupportedExtensions),
224 ArrayRef(SupportedExperimentalExtensions)}) {
225 auto I = llvm::lower_bound(ExtInfo, Ext, LessExtName());
226 if (I != ExtInfo.end() && I->Name == Ext)
227 return true;
228 }
229
230 return false;
231}
232
233bool RISCVISAInfo::isSupportedExtension(StringRef Ext, unsigned MajorVersion,
234 unsigned MinorVersion) {
235 for (auto ExtInfo : {ArrayRef(SupportedExtensions),
236 ArrayRef(SupportedExperimentalExtensions)}) {
237 auto Range =
238 std::equal_range(ExtInfo.begin(), ExtInfo.end(), Ext, LessExtName());
239 for (auto I = Range.first, E = Range.second; I != E; ++I)
240 if (I->Version.Major == MajorVersion && I->Version.Minor == MinorVersion)
241 return true;
242 }
243
244 return false;
245}
246
249
250 if (!isSupportedExtension(Ext))
251 return false;
252
253 return Exts.count(Ext.str()) != 0;
254}
255
256std::vector<std::string> RISCVISAInfo::toFeatures(bool AddAllExtensions,
257 bool IgnoreUnknown) const {
258 std::vector<std::string> Features;
259 for (const auto &[ExtName, _] : Exts) {
260 // i is a base instruction set, not an extension (see
261 // https://github.com/riscv/riscv-isa-manual/blob/main/src/naming.adoc#base-integer-isa)
262 // and is not recognized in clang -cc1
263 if (ExtName == "i")
264 continue;
265 if (IgnoreUnknown && !isSupportedExtension(ExtName))
266 continue;
267
268 if (isExperimentalExtension(ExtName)) {
269 Features.push_back((llvm::Twine("+experimental-") + ExtName).str());
270 } else {
271 Features.push_back((llvm::Twine("+") + ExtName).str());
272 }
273 }
274 if (AddAllExtensions) {
275 for (const RISCVSupportedExtension &Ext : SupportedExtensions) {
276 if (Exts.count(Ext.Name))
277 continue;
278 Features.push_back((llvm::Twine("-") + Ext.Name).str());
279 }
280
281 for (const RISCVSupportedExtension &Ext : SupportedExperimentalExtensions) {
282 if (Exts.count(Ext.Name))
283 continue;
284 Features.push_back((llvm::Twine("-experimental-") + Ext.Name).str());
285 }
286 }
287 return Features;
288}
289
291 if (ExtName.size() == 1) {
293 "unsupported standard user-level extension '" +
294 ExtName + "'");
295 }
297 "unsupported " + getExtensionTypeDesc(ExtName) +
298 " '" + ExtName + "'");
299}
300
301// Extensions may have a version number, and may be separated by
302// an underscore '_' e.g.: rv32i2_m2.
303// Version number is divided into major and minor version numbers,
304// separated by a 'p'. If the minor version is 0 then 'p0' can be
305// omitted from the version string. E.g., rv32i2p0, rv32i2, rv32i2p1.
306static Error getExtensionVersion(StringRef Ext, StringRef In, unsigned &Major,
307 unsigned &Minor, unsigned &ConsumeLength,
308 bool EnableExperimentalExtension,
309 bool ExperimentalExtensionVersionCheck) {
310 StringRef MajorStr, MinorStr;
311 Major = 0;
312 Minor = 0;
313 ConsumeLength = 0;
314 MajorStr = In.take_while(isDigit);
315 In = In.substr(MajorStr.size());
316
317 if (!MajorStr.empty() && In.consume_front("p")) {
318 MinorStr = In.take_while(isDigit);
319 In = In.substr(MajorStr.size() + MinorStr.size() - 1);
320
321 // Expected 'p' to be followed by minor version number.
322 if (MinorStr.empty()) {
323 return createStringError(
325 "minor version number missing after 'p' for extension '" + Ext + "'");
326 }
327 }
328
329 if (!MajorStr.empty() && MajorStr.getAsInteger(10, Major))
330 return createStringError(
332 "Failed to parse major version number for extension '" + Ext + "'");
333
334 if (!MinorStr.empty() && MinorStr.getAsInteger(10, Minor))
335 return createStringError(
337 "Failed to parse minor version number for extension '" + Ext + "'");
338
339 ConsumeLength = MajorStr.size();
340
341 if (!MinorStr.empty())
342 ConsumeLength += MinorStr.size() + 1 /*'p'*/;
343
344 // Expected multi-character extension with version number to have no
345 // subsequent characters (i.e. must either end string or be followed by
346 // an underscore).
347 if (Ext.size() > 1 && In.size())
348 return createStringError(
350 "multi-character extensions must be separated by underscores");
351
352 // If experimental extension, require use of current version number
353 if (auto ExperimentalExtension = isExperimentalExtension(Ext)) {
354 if (!EnableExperimentalExtension)
356 "requires '-menable-experimental-extensions' "
357 "for experimental extension '" +
358 Ext + "'");
359
360 if (ExperimentalExtensionVersionCheck &&
361 (MajorStr.empty() && MinorStr.empty()))
362 return createStringError(
364 "experimental extension requires explicit version number `" + Ext +
365 "`");
366
367 auto SupportedVers = *ExperimentalExtension;
368 if (ExperimentalExtensionVersionCheck &&
369 (Major != SupportedVers.Major || Minor != SupportedVers.Minor)) {
370 std::string Error = "unsupported version number " + MajorStr.str();
371 if (!MinorStr.empty())
372 Error += "." + MinorStr.str();
373 Error += " for experimental extension '" + Ext.str() +
374 "' (this compiler supports " + utostr(SupportedVers.Major) +
375 "." + utostr(SupportedVers.Minor) + ")";
377 }
378 return Error::success();
379 }
380
381 // Exception rule for `g`, we don't have clear version scheme for that on
382 // ISA spec.
383 if (Ext == "g")
384 return Error::success();
385
386 if (MajorStr.empty() && MinorStr.empty()) {
387 if (auto DefaultVersion = findDefaultVersion(Ext)) {
388 Major = DefaultVersion->Major;
389 Minor = DefaultVersion->Minor;
390 }
391 // No matter found or not, return success, assume other place will
392 // verify.
393 return Error::success();
394 }
395
396 if (RISCVISAInfo::isSupportedExtension(Ext, Major, Minor))
397 return Error::success();
398
400 return getStringErrorForInvalidExt(Ext);
401
402 std::string Error = "unsupported version number " + std::string(MajorStr);
403 if (!MinorStr.empty())
404 Error += "." + MinorStr.str();
405 Error += " for extension '" + Ext.str() + "'";
407}
408
411 const std::vector<std::string> &Features) {
412 assert(XLen == 32 || XLen == 64);
413 std::unique_ptr<RISCVISAInfo> ISAInfo(new RISCVISAInfo(XLen));
414
415 for (auto &Feature : Features) {
416 StringRef ExtName = Feature;
417 assert(ExtName.size() > 1 && (ExtName[0] == '+' || ExtName[0] == '-'));
418 bool Add = ExtName[0] == '+';
419 ExtName = ExtName.drop_front(1); // Drop '+' or '-'
420 bool Experimental = stripExperimentalPrefix(ExtName);
421 auto ExtensionInfos = Experimental
422 ? ArrayRef(SupportedExperimentalExtensions)
423 : ArrayRef(SupportedExtensions);
424 auto ExtensionInfoIterator =
425 llvm::lower_bound(ExtensionInfos, ExtName, LessExtName());
426
427 // Not all features is related to ISA extension, like `relax` or
428 // `save-restore`, skip those feature.
429 if (ExtensionInfoIterator == ExtensionInfos.end() ||
430 ExtensionInfoIterator->Name != ExtName)
431 continue;
432
433 if (Add)
434 ISAInfo->addExtension(ExtName, ExtensionInfoIterator->Version);
435 else
436 ISAInfo->Exts.erase(ExtName.str());
437 }
438
439 return RISCVISAInfo::postProcessAndChecking(std::move(ISAInfo));
440}
441
444 // RISC-V ISA strings must be [a-z0-9_]
445 if (!llvm::all_of(
446 Arch, [](char C) { return isDigit(C) || isLower(C) || C == '_'; }))
448 "string may only contain [a-z0-9_]");
449
450 // Must start with a valid base ISA name.
451 unsigned XLen = 0;
452 if (Arch.consume_front("rv32"))
453 XLen = 32;
454 else if (Arch.consume_front("rv64"))
455 XLen = 64;
456
457 if (XLen == 0 || Arch.empty() || (Arch[0] != 'i' && Arch[0] != 'e'))
459 "arch string must begin with valid base ISA");
460
461 std::unique_ptr<RISCVISAInfo> ISAInfo(new RISCVISAInfo(XLen));
462
463 // Each extension is of the form ${name}${major_version}p${minor_version}
464 // and separated by _. Split by _ and then extract the name and version
465 // information for each extension.
466 while (!Arch.empty()) {
467 if (Arch[0] == '_') {
468 if (Arch.size() == 1 || Arch[1] == '_')
470 "extension name missing after separator '_'");
471 Arch = Arch.drop_front();
472 }
473
474 size_t Idx = Arch.find('_');
475 StringRef Ext = Arch.slice(0, Idx);
476 Arch = Arch.slice(Idx, StringRef::npos);
477
478 StringRef Prefix, MinorVersionStr;
479 std::tie(Prefix, MinorVersionStr) = Ext.rsplit('p');
480 if (MinorVersionStr.empty())
482 "extension lacks version in expected format");
483 unsigned MajorVersion, MinorVersion;
484 if (MinorVersionStr.getAsInteger(10, MinorVersion))
486 "failed to parse minor version number");
487
488 // Split Prefix into the extension name and the major version number
489 // (the trailing digits of Prefix).
490 size_t VersionStart = Prefix.size();
491 while (VersionStart != 0) {
492 if (!isDigit(Prefix[VersionStart - 1]))
493 break;
494 --VersionStart;
495 }
496 if (VersionStart == Prefix.size())
498 "extension lacks version in expected format");
499
500 if (VersionStart == 0)
502 "missing extension name");
503
504 StringRef ExtName = Prefix.slice(0, VersionStart);
505 StringRef MajorVersionStr = Prefix.slice(VersionStart, StringRef::npos);
506 if (MajorVersionStr.getAsInteger(10, MajorVersion))
508 "failed to parse major version number");
509
510 if ((ExtName[0] == 'z' || ExtName[0] == 's' || ExtName[0] == 'x') &&
511 (ExtName.size() == 1 || isDigit(ExtName[1])))
513 "'" + Twine(ExtName[0]) +
514 "' must be followed by a letter");
515
516 if (!ISAInfo->addExtension(ExtName, {MajorVersion, MinorVersion}))
518 "duplicate extension '" + ExtName + "'");
519 }
520 ISAInfo->updateImpliedLengths();
521 return std::move(ISAInfo);
522}
523
525RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
526 bool ExperimentalExtensionVersionCheck,
527 bool IgnoreUnknown) {
528 // RISC-V ISA strings must be [a-z0-9_]
529 if (!llvm::all_of(
530 Arch, [](char C) { return isDigit(C) || isLower(C) || C == '_'; }))
532 "string may only contain [a-z0-9_]");
533
534 // ISA string must begin with rv32, rv64, or a profile.
535 unsigned XLen = 0;
536 if (Arch.consume_front("rv32")) {
537 XLen = 32;
538 } else if (Arch.consume_front("rv64")) {
539 XLen = 64;
540 } else {
541 // Try parsing as a profile.
542 auto ProfileCmp = [](StringRef Arch, const RISCVProfile &Profile) {
543 return Arch < Profile.Name;
544 };
545 auto I = llvm::upper_bound(SupportedProfiles, Arch, ProfileCmp);
546 bool FoundProfile = I != std::begin(SupportedProfiles) &&
547 Arch.starts_with(std::prev(I)->Name);
548 if (!FoundProfile) {
549 I = llvm::upper_bound(SupportedExperimentalProfiles, Arch, ProfileCmp);
550 FoundProfile = (I != std::begin(SupportedExperimentalProfiles) &&
551 Arch.starts_with(std::prev(I)->Name));
552 if (FoundProfile && !EnableExperimentalExtension) {
554 "requires '-menable-experimental-extensions' "
555 "for profile '" +
556 std::prev(I)->Name + "'");
557 }
558 }
559 if (FoundProfile) {
560 --I;
561 std::string NewArch = I->MArch.str();
562 StringRef ArchWithoutProfile = Arch.drop_front(I->Name.size());
563 if (!ArchWithoutProfile.empty()) {
564 if (ArchWithoutProfile.front() != '_')
565 return createStringError(
567 "additional extensions must be after separator '_'");
568 NewArch += ArchWithoutProfile.str();
569 }
570 return parseArchString(NewArch, EnableExperimentalExtension,
571 ExperimentalExtensionVersionCheck, IgnoreUnknown);
572 }
573 }
574
575 if (XLen == 0 || Arch.empty())
576 return createStringError(
578 "string must begin with rv32{i,e,g}, rv64{i,e,g}, or a supported "
579 "profile name");
580
581 std::unique_ptr<RISCVISAInfo> ISAInfo(new RISCVISAInfo(XLen));
583 std::map<std::string, unsigned>>
584 SeenExtMap;
585
586 // The canonical order specified in ISA manual.
587 // Ref: Table 22.1 in RISC-V User-Level ISA V2.2
588 char Baseline = Arch.front();
589
590 // First letter should be 'e', 'i' or 'g'.
591 switch (Baseline) {
592 default:
594 "first letter after \'rv" + Twine(XLen) +
595 "\' should be 'e', 'i' or 'g'");
596 case 'e':
597 case 'i':
598 break;
599 case 'g':
600 // g expands to extensions in RISCVGImplications.
601 if (Arch.size() > 1 && isDigit(Arch[1]))
603 "version not supported for 'g'");
604 break;
605 }
606
607 // Skip baseline.
608 StringRef Exts = Arch.drop_front(1);
609
610 unsigned Major, Minor, ConsumeLength;
611 if (Baseline == 'g') {
612 // Versions for g are disallowed, and this was checked for previously.
613 ConsumeLength = 0;
614
615 // No matter which version is given to `g`, we always set imafd to default
616 // version since the we don't have clear version scheme for that on
617 // ISA spec.
618 for (const auto *Ext : RISCVGImplications) {
619 auto Version = findDefaultVersion(Ext);
620 assert(Version && "Default extension version not found?");
621 // Postpone AddExtension until end of this function
622 SeenExtMap[Ext] = {Version->Major, Version->Minor};
623 }
624 } else {
625 // Baseline is `i` or `e`
626 if (auto E = getExtensionVersion(
627 StringRef(&Baseline, 1), Exts, Major, Minor, ConsumeLength,
628 EnableExperimentalExtension, ExperimentalExtensionVersionCheck)) {
629 if (!IgnoreUnknown)
630 return std::move(E);
631 // If IgnoreUnknown, then ignore an unrecognised version of the baseline
632 // ISA and just use the default supported version.
633 consumeError(std::move(E));
634 auto Version = findDefaultVersion(StringRef(&Baseline, 1));
635 Major = Version->Major;
636 Minor = Version->Minor;
637 }
638
639 // Postpone AddExtension until end of this function
640 SeenExtMap[StringRef(&Baseline, 1).str()] = {Major, Minor};
641 }
642
643 // Consume the base ISA version number and any '_' between rvxxx and the
644 // first extension
645 Exts = Exts.drop_front(ConsumeLength);
646
647 while (!Exts.empty()) {
648 if (Exts.front() == '_') {
649 if (Exts.size() == 1 || Exts[1] == '_')
651 "extension name missing after separator '_'");
652 Exts = Exts.drop_front();
653 }
654
655 size_t Idx = Exts.find('_');
656 StringRef Ext = Exts.slice(0, Idx);
657 Exts = Exts.slice(Idx, StringRef::npos);
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 (!IgnoreUnknown && Name.size() == Type.size())
686 Desc + " name missing after '" + Type + "'");
687 } else {
688 // FIXME: Could it be ignored by IgnoreUnknown?
690 "invalid standard user-level extension '" +
691 Twine(Ext.front()) + "'");
692 }
693
694 unsigned Major, Minor, ConsumeLength;
695 if (auto E = getExtensionVersion(Name, Vers, Major, Minor, ConsumeLength,
696 EnableExperimentalExtension,
697 ExperimentalExtensionVersionCheck)) {
698 if (IgnoreUnknown) {
699 consumeError(std::move(E));
700 if (Name.size() == 1)
701 Ext = Ext.substr(ConsumeLength);
702 continue;
703 }
704 return E;
705 }
706
707 if (Name.size() == 1)
708 Ext = Ext.substr(ConsumeLength);
709
710 // Check if duplicated extension.
711 if (!IgnoreUnknown && SeenExtMap.contains(Name.str()))
713 "duplicated " + Desc + " '" + Name + "'");
714
715 if (IgnoreUnknown && !RISCVISAInfo::isSupportedExtension(Name))
716 continue;
717
718 SeenExtMap[Name.str()] = {Major, Minor};
719 } while (!Ext.empty());
720 }
721
722 // Check all Extensions are supported.
723 for (auto &SeenExtAndVers : SeenExtMap) {
724 const std::string &ExtName = SeenExtAndVers.first;
725 RISCVISAUtils::ExtensionVersion ExtVers = SeenExtAndVers.second;
726
728 return getStringErrorForInvalidExt(ExtName);
729 ISAInfo->addExtension(ExtName, ExtVers);
730 }
731
732 return RISCVISAInfo::postProcessAndChecking(std::move(ISAInfo));
733}
734
735Error RISCVISAInfo::checkDependency() {
736 bool HasE = Exts.count("e") != 0;
737 bool HasI = Exts.count("i") != 0;
738 bool HasC = Exts.count("c") != 0;
739 bool HasF = Exts.count("f") != 0;
740 bool HasZfinx = Exts.count("zfinx") != 0;
741 bool HasVector = Exts.count("zve32x") != 0;
742 bool HasZvl = MinVLen != 0;
743 bool HasZcmt = Exts.count("zcmt") != 0;
744
745 if (HasI && HasE)
747 "'I' and 'E' extensions are incompatible");
748
749 if (HasF && HasZfinx)
751 "'f' and 'zfinx' extensions are incompatible");
752
753 if (HasZvl && !HasVector)
754 return createStringError(
756 "'zvl*b' requires 'v' or 'zve*' extension to also be specified");
757
758 if (Exts.count("zvbb") && !HasVector)
759 return createStringError(
761 "'zvbb' requires 'v' or 'zve*' extension to also be specified");
762
763 if (Exts.count("zvbc") && !Exts.count("zve64x"))
764 return createStringError(
766 "'zvbc' requires 'v' or 'zve64*' extension to also be specified");
767
768 if ((Exts.count("zvkb") || Exts.count("zvkg") || Exts.count("zvkned") ||
769 Exts.count("zvknha") || Exts.count("zvksed") || Exts.count("zvksh")) &&
770 !HasVector)
771 return createStringError(
773 "'zvk*' requires 'v' or 'zve*' extension to also be specified");
774
775 if (Exts.count("zvknhb") && !Exts.count("zve64x"))
776 return createStringError(
778 "'zvknhb' requires 'v' or 'zve64*' extension to also be specified");
779
780 if ((HasZcmt || Exts.count("zcmp")) && Exts.count("d") &&
781 (HasC || Exts.count("zcd")))
782 return createStringError(
784 Twine("'") + (HasZcmt ? "zcmt" : "zcmp") +
785 "' extension is incompatible with '" + (HasC ? "c" : "zcd") +
786 "' extension when 'd' extension is enabled");
787
788 if (XLen != 32 && Exts.count("zcf"))
790 "'zcf' is only supported for 'rv32'");
791
792 if (Exts.count("zacas") && !(Exts.count("a") || Exts.count("zaamo")))
793 return createStringError(
795 "'zacas' requires 'a' or 'zaamo' extension to also be specified");
796
797 if (Exts.count("zabha") && !(Exts.count("a") || Exts.count("zaamo")))
798 return createStringError(
800 "'zabha' requires 'a' or 'zaamo' extension to also be specified");
801
802 return Error::success();
803}
804
807 const char *ImpliedExt;
808
809 bool operator<(const ImpliedExtsEntry &Other) const {
810 return Name < Other.Name;
811 }
812};
813
814static bool operator<(const ImpliedExtsEntry &LHS, StringRef RHS) {
815 return LHS.Name < RHS;
816}
817
818static bool operator<(StringRef LHS, const ImpliedExtsEntry &RHS) {
819 return LHS < RHS.Name;
820}
821
822#define GET_IMPLIED_EXTENSIONS
823#include "llvm/TargetParser/RISCVTargetParserDef.inc"
824
825void RISCVISAInfo::updateImplication() {
826 bool HasE = Exts.count("e") != 0;
827 bool HasI = Exts.count("i") != 0;
828
829 // If not in e extension and i extension does not exist, i extension is
830 // implied
831 if (!HasE && !HasI) {
832 auto Version = findDefaultVersion("i");
833 addExtension("i", *Version);
834 }
835
836 if (HasE && HasI)
837 Exts.erase("i");
838
839 assert(llvm::is_sorted(ImpliedExts) && "Table not sorted by Name");
840
841 // This loop may execute over 1 iteration since implication can be layered
842 // Exits loop if no more implication is applied
844 for (auto const &Ext : Exts)
845 WorkList.push_back(Ext.first);
846
847 while (!WorkList.empty()) {
848 StringRef ExtName = WorkList.pop_back_val();
849 auto Range = std::equal_range(std::begin(ImpliedExts),
850 std::end(ImpliedExts), ExtName);
851 std::for_each(Range.first, Range.second,
852 [&](const ImpliedExtsEntry &Implied) {
853 const char *ImpliedExt = Implied.ImpliedExt;
854 if (Exts.count(ImpliedExt))
855 return;
856 auto Version = findDefaultVersion(ImpliedExt);
857 addExtension(ImpliedExt, *Version);
858 WorkList.push_back(ImpliedExt);
859 });
860 }
861
862 // Add Zcf if Zce and F are enabled on RV32.
863 if (XLen == 32 && Exts.count("zce") && Exts.count("f") &&
864 !Exts.count("zcf")) {
865 auto Version = findDefaultVersion("zcf");
866 addExtension("zcf", *Version);
867 }
868}
869
870static constexpr StringLiteral CombineIntoExts[] = {
871 {"zk"}, {"zkn"}, {"zks"}, {"zvkn"}, {"zvknc"},
872 {"zvkng"}, {"zvks"}, {"zvksc"}, {"zvksg"},
873};
874
875void RISCVISAInfo::updateCombination() {
876 bool MadeChange = false;
877 do {
878 MadeChange = false;
879 for (StringRef CombineExt : CombineIntoExts) {
880 if (Exts.count(CombineExt.str()))
881 continue;
882
883 // Look up the extension in the ImpliesExt table to find everything it
884 // depends on.
885 auto Range = std::equal_range(std::begin(ImpliedExts),
886 std::end(ImpliedExts), CombineExt);
887 bool HasAllRequiredFeatures = std::all_of(
888 Range.first, Range.second, [&](const ImpliedExtsEntry &Implied) {
889 return Exts.count(Implied.ImpliedExt);
890 });
891 if (HasAllRequiredFeatures) {
892 auto Version = findDefaultVersion(CombineExt);
893 addExtension(CombineExt, *Version);
894 MadeChange = true;
895 }
896 }
897 } while (MadeChange);
898}
899
900void RISCVISAInfo::updateImpliedLengths() {
901 assert(FLen == 0 && MaxELenFp == 0 && MaxELen == 0 && MinVLen == 0 &&
902 "Expected lengths to be initialied to zero");
903
904 // TODO: Handle q extension.
905 if (Exts.count("d"))
906 FLen = 64;
907 else if (Exts.count("f"))
908 FLen = 32;
909
910 if (Exts.count("v")) {
911 MaxELenFp = std::max(MaxELenFp, 64u);
912 MaxELen = std::max(MaxELen, 64u);
913 }
914
915 for (auto const &Ext : Exts) {
916 StringRef ExtName = Ext.first;
917 // Infer MaxELen and MaxELenFp from Zve(32/64)(x/f/d)
918 if (ExtName.consume_front("zve")) {
919 unsigned ZveELen;
920 if (ExtName.consumeInteger(10, ZveELen))
921 continue;
922
923 if (ExtName == "f")
924 MaxELenFp = std::max(MaxELenFp, 32u);
925 else if (ExtName == "d")
926 MaxELenFp = std::max(MaxELenFp, 64u);
927 else if (ExtName != "x")
928 continue;
929
930 MaxELen = std::max(MaxELen, ZveELen);
931 continue;
932 }
933
934 // Infer MinVLen from zvl*b.
935 if (ExtName.consume_front("zvl")) {
936 unsigned ZvlLen;
937 if (ExtName.consumeInteger(10, ZvlLen))
938 continue;
939
940 if (ExtName != "b")
941 continue;
942
943 MinVLen = std::max(MinVLen, ZvlLen);
944 continue;
945 }
946 }
947}
948
949std::string RISCVISAInfo::toString() const {
950 std::string Buffer;
951 raw_string_ostream Arch(Buffer);
952
953 Arch << "rv" << XLen;
954
955 ListSeparator LS("_");
956 for (auto const &Ext : Exts) {
957 StringRef ExtName = Ext.first;
958 auto ExtInfo = Ext.second;
959 Arch << LS << ExtName;
960 Arch << ExtInfo.Major << "p" << ExtInfo.Minor;
961 }
962
963 return Arch.str();
964}
965
967RISCVISAInfo::postProcessAndChecking(std::unique_ptr<RISCVISAInfo> &&ISAInfo) {
968 ISAInfo->updateImplication();
969 ISAInfo->updateCombination();
970 ISAInfo->updateImpliedLengths();
971
972 if (Error Result = ISAInfo->checkDependency())
973 return std::move(Result);
974 return std::move(ISAInfo);
975}
976
978 if (XLen == 32) {
979 if (Exts.count("e"))
980 return "ilp32e";
981 if (Exts.count("d"))
982 return "ilp32d";
983 if (Exts.count("f"))
984 return "ilp32f";
985 return "ilp32";
986 } else if (XLen == 64) {
987 if (Exts.count("e"))
988 return "lp64e";
989 if (Exts.count("d"))
990 return "lp64d";
991 if (Exts.count("f"))
992 return "lp64f";
993 return "lp64";
994 }
995 llvm_unreachable("Invalid XLEN");
996}
997
999 if (Ext.empty())
1000 return false;
1001
1002 auto Pos = findLastNonVersionCharacter(Ext) + 1;
1003 StringRef Name = Ext.substr(0, Pos);
1004 StringRef Vers = Ext.substr(Pos);
1005 if (Vers.empty())
1006 return false;
1007
1008 unsigned Major, Minor, ConsumeLength;
1009 if (auto E = getExtensionVersion(Name, Vers, Major, Minor, ConsumeLength,
1010 true, true)) {
1011 consumeError(std::move(E));
1012 return false;
1013 }
1014
1015 return true;
1016}
1017
1019 if (Ext.empty())
1020 return std::string();
1021
1022 auto Pos = findLastNonVersionCharacter(Ext) + 1;
1023 StringRef Name = Ext.substr(0, Pos);
1024
1025 if (Pos != Ext.size() && !isSupportedExtensionWithVersion(Ext))
1026 return std::string();
1027
1029 return std::string();
1030
1031 return isExperimentalExtension(Name) ? "experimental-" + Name.str()
1032 : Name.str();
1033}
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
std::string Name
std::optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1293
#define _
#define I(x, y, z)
Definition: MD5.cpp:58
Load MIR Sample Profile
This file implements a map that provides insertion order iteration.
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
#define P(N)
static void verifyTables()
static StringRef getExtensionTypeDesc(StringRef Ext)
static std::optional< RISCVISAUtils::ExtensionVersion > findDefaultVersion(StringRef ExtName)
static size_t findLastNonVersionCharacter(StringRef Ext)
static StringRef getExtensionType(StringRef Ext)
static constexpr StringLiteral CombineIntoExts[]
static bool stripExperimentalPrefix(StringRef &Ext)
static std::optional< RISCVISAUtils::ExtensionVersion > isExperimentalExtension(StringRef Ext)
static Error getStringErrorForInvalidExt(StringRef ExtName)
static void PrintExtension(StringRef Name, StringRef Version, StringRef Description)
static bool operator<(const ImpliedExtsEntry &LHS, StringRef RHS)
static Error getExtensionVersion(StringRef Ext, StringRef In, unsigned &Major, unsigned &Minor, unsigned &ConsumeLength, bool EnableExperimentalExtension, bool ExperimentalExtensionVersionCheck)
static const char * RISCVGImplications[]
static bool isDigit(const char C)
static bool isLower(const char C)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
This file implements a set that has insertion order iteration characteristics.
This file contains some functions that are useful when dealing with strings.
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition: Value.cpp:469
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:154
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
Tagged union holding either a T or a Error.
Definition: Error.h:481
This class implements a map that also provides access to all stored values in a deterministic order.
Definition: MapVector.h:36
static bool isSupportedExtensionFeature(StringRef Ext)
static std::string getTargetFeatureForExtension(StringRef Ext)
static 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...
bool hasExtension(StringRef Ext) const
std::string toString() const
static llvm::Expected< std::unique_ptr< RISCVISAInfo > > postProcessAndChecking(std::unique_ptr< RISCVISAInfo > &&ISAInfo)
StringRef computeDefaultABI() const
static bool isSupportedExtension(StringRef Ext)
static llvm::Expected< std::unique_ptr< RISCVISAInfo > > parseFeatures(unsigned XLen, const std::vector< std::string > &Features)
Parse RISC-V ISA info from feature vector.
std::vector< std::string > toFeatures(bool AddAllExtensions=false, bool IgnoreUnknown=true) const
Convert RISC-V ISA info to a feature vector.
static llvm::Expected< std::unique_ptr< RISCVISAInfo > > parseArchString(StringRef Arch, bool EnableExperimentalExtension, bool ExperimentalExtensionVersionCheck=true, bool IgnoreUnknown=false)
Parse RISC-V ISA info from arch string.
static bool isSupportedExtensionWithVersion(StringRef Ext)
bool empty() const
Definition: SmallVector.h:94
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition: StringRef.h:846
bool empty() const
Definition: StringMap.h:103
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:128
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
bool consumeInteger(unsigned Radix, T &Result)
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:492
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:463
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:223
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:258
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition: StringRef.h:602
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition: StringRef.h:677
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
char front() const
front - Get the first character in the string.
Definition: StringRef.h:140
bool consume_front(StringRef Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
Definition: StringRef.h:628
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:290
static constexpr size_t npos
Definition: StringRef.h:52
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
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.
Definition: raw_ostream.h:661
std::string & str()
Returns the string's reference.
Definition: raw_ostream.h:679
#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
const uint64_t Version
Definition: InstrProf.h:1108
constexpr StringLiteral AllStdExts
Definition: RISCVISAUtils.h:23
std::map< std::string, ExtensionVersion, ExtensionComparator > OrderedExtensionMap
OrderedExtensionMap is std::map, it's specialized to keep entries in canonical order of extension.
Definition: RISCVISAUtils.h:43
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool operator<(int64_t V1, const APSInt &V2)
Definition: APSInt.h:361
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:1722
raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
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:1967
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1286
Op::Description Desc
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
void riscvExtensionsHelp(StringMap< StringRef > DescMap)
FormattedString left_justify(StringRef Str, unsigned Width)
left_justify - append spaces after string so total output is Width characters.
Definition: Format.h:146
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:1954
@ Add
Sum of integers.
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1069
StringLiteral Name
bool operator<(const ImpliedExtsEntry &Other) const
const char * ImpliedExt
Description of the encoding of one expression Op.
Represents the major and version number components of a RISC-V extension.
Definition: RISCVISAUtils.h:26