1//===-- RISCVTargetParser.cpp - Parser for target features ------*- 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// This file implements a target parser to recognise hardware features
10// for RISC-V CPUs.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/TargetParser/RISCVTargetParser.h"
15#include "llvm/ADT/SetOperations.h"
16#include "llvm/ADT/SmallSet.h"
17#include "llvm/ADT/SmallVector.h"
18#include "llvm/ADT/StringExtras.h"
19#include "llvm/ADT/StringSwitch.h"
20#include "llvm/ADT/StringTable.h"
21#include "llvm/TargetParser/RISCVISAInfo.h"
22
23namespace llvm {
24namespace RISCV {
25
26char ParserError::ID = 0;
27char ParserWarning::ID = 0;
28
29enum CPUKind : unsigned {
30#define PROC(ENUM, NAME, DEFAULT_MARCH, FAST_SCALAR_UNALIGN, \
31 FAST_VECTOR_UNALIGN, MVENDORID, MARCHID, MIMPID) \
32 CK_##ENUM,
33#define TUNE_PROC(ENUM, NAME) CK_##ENUM,
34#include "llvm/TargetParser/RISCVTargetParserDef.inc"
35};
36
37constexpr CPUInfo RISCVCPUInfo[] = {
38#define PROC(ENUM, NAME, DEFAULT_MARCH, FAST_SCALAR_UNALIGN, \
39 FAST_VECTOR_UNALIGN, MVENDORID, MARCHID, MIMPID) \
40 { \
41 NAME, \
42 DEFAULT_MARCH, \
43 FAST_SCALAR_UNALIGN, \
44 FAST_VECTOR_UNALIGN, \
45 {MVENDORID, MARCHID, MIMPID}, \
46 },
47#include "llvm/TargetParser/RISCVTargetParserDef.inc"
48};
49
50static const CPUInfo *getCPUInfoByName(StringRef CPU) {
51 for (auto &C : RISCVCPUInfo)
52 if (C.Name == CPU)
53 return &C;
54 return nullptr;
55}
56
57bool hasFastScalarUnalignedAccess(StringRef CPU) {
58 const CPUInfo *Info = getCPUInfoByName(CPU);
59 return Info && Info->FastScalarUnalignedAccess;
60}
61
62bool hasFastVectorUnalignedAccess(StringRef CPU) {
63 const CPUInfo *Info = getCPUInfoByName(CPU);
64 return Info && Info->FastVectorUnalignedAccess;
65}
66
67bool hasValidCPUModel(StringRef CPU) { return getCPUModel(CPU).isValid(); }
68
69CPUModel getCPUModel(StringRef CPU) {
70 const CPUInfo *Info = getCPUInfoByName(CPU);
71 if (!Info)
72 return {.MVendorID: 0, .MArchID: 0, .MImpID: 0};
73 return Info->Model;
74}
75
76StringRef getCPUNameFromCPUModel(const CPUModel &Model) {
77 if (!Model.isValid())
78 return "";
79
80 for (auto &C : RISCVCPUInfo)
81 if (C.Model == Model)
82 return C.Name;
83 return "";
84}
85
86bool parseCPU(StringRef CPU, bool IsRV64) {
87 const CPUInfo *Info = getCPUInfoByName(CPU);
88
89 if (!Info)
90 return false;
91 return Info->is64Bit() == IsRV64;
92}
93
94bool parseTuneCPU(StringRef TuneCPU, bool IsRV64) {
95 std::optional<CPUKind> Kind =
96 llvm::StringSwitch<std::optional<CPUKind>>(TuneCPU)
97#define TUNE_PROC(ENUM, NAME) .Case(NAME, CK_##ENUM)
98 #include "llvm/TargetParser/RISCVTargetParserDef.inc"
99 .Default(Value: std::nullopt);
100
101 if (Kind.has_value())
102 return true;
103
104 // Fallback to parsing as a CPU.
105 return parseCPU(CPU: TuneCPU, IsRV64);
106}
107
108StringRef getMArchFromMcpu(StringRef CPU) {
109 const CPUInfo *Info = getCPUInfoByName(CPU);
110 if (!Info)
111 return "";
112 return Info->DefaultMarch;
113}
114
115void fillValidCPUArchList(SmallVectorImpl<StringRef> &Values, bool IsRV64) {
116 for (const auto &C : RISCVCPUInfo) {
117 if (IsRV64 == C.is64Bit())
118 Values.emplace_back(Args: C.Name);
119 }
120
121 llvm::sort(C&: Values,
122 Comp: [](StringRef A, StringRef B) { return A.compare_numeric(RHS: B) < 0; });
123}
124
125void fillValidTuneCPUArchList(SmallVectorImpl<StringRef> &Values, bool IsRV64) {
126 for (const auto &C : RISCVCPUInfo) {
127 if (IsRV64 == C.is64Bit())
128 Values.emplace_back(Args: C.Name);
129 }
130#define TUNE_PROC(ENUM, NAME) Values.emplace_back(StringRef(NAME));
131#include "llvm/TargetParser/RISCVTargetParserDef.inc"
132
133 llvm::sort(C&: Values,
134 Comp: [](StringRef A, StringRef B) { return A.compare_numeric(RHS: B) < 0; });
135}
136
137// This function is currently used by IREE, so it's not dead code.
138void getFeaturesForCPU(StringRef CPU,
139 SmallVectorImpl<std::string> &EnabledFeatures,
140 bool NeedPlus) {
141 StringRef MarchFromCPU = llvm::RISCV::getMArchFromMcpu(CPU);
142 if (MarchFromCPU == "")
143 return;
144
145 EnabledFeatures.clear();
146 auto RII = RISCVISAInfo::parseArchString(
147 Arch: MarchFromCPU, /* EnableExperimentalExtension */ true);
148
149 if (llvm::errorToBool(Err: RII.takeError()))
150 return;
151
152 std::vector<std::string> FeatStrings =
153 (*RII)->toFeatures(/* AddAllExtensions */ false);
154 for (const auto &F : FeatStrings)
155 if (NeedPlus)
156 EnabledFeatures.push_back(Elt: F);
157 else
158 EnabledFeatures.push_back(Elt: F.substr(pos: 1));
159}
160
161namespace {
162class RISCVTuneFeatureLookupTable {
163 struct RISCVTuneFeature {
164 unsigned PosIdx;
165 unsigned NegIdx;
166 unsigned FeatureIdx;
167 };
168
169 struct RISCVImpliedTuneFeature {
170 unsigned FeatureIdx;
171 unsigned ImpliedFeatureIdx;
172 };
173
174 struct RISCVConfigurableTuneFeatures {
175 StringRef Processor;
176 unsigned DirectiveIdx;
177
178 bool operator<(const RISCVConfigurableTuneFeatures &RHS) const {
179 return Processor < RHS.Processor;
180 }
181 };
182
183#define GET_TUNE_FEATURES
184#define GET_CONFIGURABLE_TUNE_FEATURES
185#include "llvm/TargetParser/RISCVTargetParserDef.inc"
186
187 // Positive directive name -> Feature name
188 StringMap<StringRef> PositiveMap;
189 // Negative directive name -> Feature name
190 StringMap<StringRef> NegativeMap;
191
192 StringMap<SmallVector<StringRef>> ImpliedFeatureMap;
193 StringMap<SmallVector<StringRef>> InvImpliedFeatureMap;
194
195public:
196 using SmallStringSet = SmallSet<StringRef, 4>;
197
198 static void getAllTuneFeatures(SmallVectorImpl<StringRef> &Features) {
199 for (const auto &TuneFeature : TuneFeatures)
200 Features.push_back(Elt: TuneFeatureStrings[TuneFeature.FeatureIdx]);
201 }
202
203 static void getConfigurableFeatures(StringRef ProcName,
204 SmallStringSet &Directives) {
205 // Entries for the same processor are always put together.
206 auto [ItFirst, ItEnd] =
207 std::equal_range(first: std::begin(arr: ConfigurableTuneFeatures),
208 last: std::end(arr: ConfigurableTuneFeatures),
209 val: RISCVConfigurableTuneFeatures{.Processor: ProcName, .DirectiveIdx: 0});
210 for (; ItFirst != ItEnd; ++ItFirst)
211 Directives.insert(V: TuneFeatureStrings[ItFirst->DirectiveIdx]);
212 }
213
214 RISCVTuneFeatureLookupTable() {
215 for (const auto &TuneFeature : TuneFeatures) {
216 StringRef PosDirective = TuneFeatureStrings[TuneFeature.PosIdx];
217 StringRef NegDirective = TuneFeatureStrings[TuneFeature.NegIdx];
218 StringRef FeatureName = TuneFeatureStrings[TuneFeature.FeatureIdx];
219 PositiveMap[PosDirective] = FeatureName;
220 NegativeMap[NegDirective] = FeatureName;
221 }
222
223 for (const auto &Imp : ImpliedTuneFeatures) {
224 StringRef Feature = TuneFeatureStrings[Imp.FeatureIdx];
225 StringRef ImpliedFeature = TuneFeatureStrings[Imp.ImpliedFeatureIdx];
226 ImpliedFeatureMap[Feature].push_back(Elt: ImpliedFeature);
227 InvImpliedFeatureMap[ImpliedFeature].push_back(Elt: Feature);
228 }
229 }
230
231 /// Returns {Feature name, Is positive or not}, or empty feature name
232 /// if not found.
233 std::pair<StringRef, bool> getFeature(StringRef DirectiveName) const {
234 auto It = PositiveMap.find(Key: DirectiveName);
235 if (It != PositiveMap.end())
236 return {It->getValue(), /*IsPositive=*/true};
237
238 return {NegativeMap.lookup(Key: DirectiveName), /*IsPositive=*/false};
239 }
240
241 /// Returns the implied features, or empty ArrayRef if not found. Note:
242 /// ImpliedFeatureMap / InvImpliedFeatureMap are the owners of these implied
243 /// feature lists, so we can just return the ArrayRef.
244 ArrayRef<StringRef> featureImplies(StringRef FeatureName,
245 bool Inverse = false) const {
246 const auto &Map = Inverse ? InvImpliedFeatureMap : ImpliedFeatureMap;
247 auto It = Map.find(Key: FeatureName);
248 if (It == Map.end())
249 return {};
250 return It->second;
251 }
252};
253} // namespace
254
255void getAllTuneFeatures(SmallVectorImpl<StringRef> &Features) {
256 RISCVTuneFeatureLookupTable::getAllTuneFeatures(Features);
257}
258
259Error parseTuneFeatureString(StringRef ProcName, StringRef TFString,
260 SmallVectorImpl<std::string> &ResFeatures) {
261 RISCVTuneFeatureLookupTable TFLookup;
262 using SmallStringSet = RISCVTuneFeatureLookupTable::SmallStringSet;
263
264 // Do not create ParserWarning right away. Instead, we store the warning
265 // message until the last moment.
266 std::string WarningMsg;
267
268 TFString = TFString.trim();
269 if (TFString.empty())
270 return Error::success();
271
272 // Note: StringSet is not really ergonomic to use in this case here.
273 SmallStringSet PositiveFeatures;
274 SmallStringSet NegativeFeatures;
275 SmallStringSet PerProcDirectives;
276 RISCVTuneFeatureLookupTable::getConfigurableFeatures(ProcName,
277 Directives&: PerProcDirectives);
278 if (PerProcDirectives.empty() && !ProcName.empty())
279 return make_error<ParserError>(Args: "Processor '" + Twine(ProcName) +
280 "' has no "
281 "configurable tuning features");
282
283 // Phase 1: Collect explicit features.
284 StringRef DirectiveStr;
285 do {
286 std::tie(args&: DirectiveStr, args&: TFString) = TFString.split(Separator: ",");
287 auto [FeatureName, IsPositive] = TFLookup.getFeature(DirectiveName: DirectiveStr);
288 if (FeatureName.empty()) {
289 raw_string_ostream SS(WarningMsg);
290 SS << "unrecognized tune feature directive '" << DirectiveStr << "'";
291 continue;
292 }
293
294 auto &Features = IsPositive ? PositiveFeatures : NegativeFeatures;
295 if (!Features.insert(V: FeatureName).second)
296 return make_error<ParserError>(
297 Args: "cannot specify more than one instance of '" + Twine(DirectiveStr) +
298 "'");
299
300 if (!PerProcDirectives.count(V: DirectiveStr) && !ProcName.empty())
301 return make_error<ParserError>(Args: "Directive '" + Twine(DirectiveStr) +
302 "' is not "
303 "allowed to be used with processor '" +
304 Twine(ProcName) + "'");
305 } while (!TFString.empty());
306
307 auto Intersection =
308 llvm::set_intersection(S1: PositiveFeatures, S2: NegativeFeatures);
309 if (!Intersection.empty()) {
310 std::string IntersectedStr = join(R&: Intersection, Separator: "', '");
311 return make_error<ParserError>(Args: "Feature(s) '" + Twine(IntersectedStr) +
312 "' cannot appear in both "
313 "positive and negative directives");
314 }
315
316 // Phase 2: Derive implied features.
317 SmallStringSet DerivedPosFeatures;
318 SmallStringSet DerivedNegFeatures;
319 for (StringRef PF : PositiveFeatures) {
320 if (auto FeatureList = TFLookup.featureImplies(FeatureName: PF); !FeatureList.empty())
321 DerivedPosFeatures.insert_range(R&: FeatureList);
322 }
323 for (StringRef NF : NegativeFeatures) {
324 if (auto FeatureList = TFLookup.featureImplies(FeatureName: NF, /*Inverse=*/true);
325 !FeatureList.empty())
326 DerivedNegFeatures.insert_range(R&: FeatureList);
327 }
328 PositiveFeatures.insert_range(R&: DerivedPosFeatures);
329 NegativeFeatures.insert_range(R&: DerivedNegFeatures);
330
331 Intersection = llvm::set_intersection(S1: PositiveFeatures, S2: NegativeFeatures);
332 if (!Intersection.empty()) {
333 std::string IntersectedStr = join(R&: Intersection, Separator: "', '");
334 return make_error<ParserError>(Args: "Feature(s) '" + Twine(IntersectedStr) +
335 "' were implied by both "
336 "positive and negative directives");
337 }
338
339 // Export the result.
340 const std::string PosPrefix("+");
341 const std::string NegPrefix("-");
342 for (StringRef PF : PositiveFeatures)
343 ResFeatures.emplace_back(Args: PosPrefix + PF.str());
344 for (StringRef NF : NegativeFeatures)
345 ResFeatures.emplace_back(Args: NegPrefix + NF.str());
346
347 if (WarningMsg.empty())
348 return Error::success();
349
350 return make_error<ParserWarning>(Args&: WarningMsg);
351}
352
353void getCPUConfigurableTuneFeatures(StringRef CPU,
354 SmallVectorImpl<StringRef> &Directives) {
355 RISCVTuneFeatureLookupTable::SmallStringSet DirectiveSet;
356 RISCVTuneFeatureLookupTable::getConfigurableFeatures(ProcName: CPU, Directives&: DirectiveSet);
357 Directives.assign(in_start: DirectiveSet.begin(), in_end: DirectiveSet.end());
358}
359} // namespace RISCV
360
361namespace RISCVVType {
362// Encode VTYPE into the binary format used by the the VSETVLI instruction which
363// is used by our MC layer representation.
364//
365// Bits | Name | Description
366// -----+------------+------------------------------------------------
367// 8 | altfmt | Alternative format for bf16/ofp8
368// 7 | vma | Vector mask agnostic
369// 6 | vta | Vector tail agnostic
370// 5:3 | vsew[2:0] | Standard element width (SEW) setting
371// 2:0 | vlmul[2:0] | Vector register group multiplier (LMUL) setting
372unsigned encodeVTYPE(VLMUL VLMul, unsigned SEW, bool TailAgnostic,
373 bool MaskAgnostic, bool AltFmt) {
374 assert(isValidSEW(SEW) && "Invalid SEW");
375 unsigned VLMulBits = static_cast<unsigned>(VLMul);
376 unsigned VSEWBits = encodeSEW(SEW);
377 unsigned VTypeI = (VSEWBits << 3) | (VLMulBits & 0x7);
378 if (TailAgnostic)
379 VTypeI |= 0x40;
380 if (MaskAgnostic)
381 VTypeI |= 0x80;
382 if (AltFmt)
383 VTypeI |= 0x100;
384
385 return VTypeI;
386}
387
388unsigned encodeXSfmmVType(unsigned SEW, unsigned Widen, bool AltFmt) {
389 assert(isValidSEW(SEW) && "Invalid SEW");
390 assert((Widen == 1 || Widen == 2 || Widen == 4) && "Invalid Widen");
391 unsigned VSEWBits = encodeSEW(SEW);
392 unsigned TWiden = Log2_32(Value: Widen) + 1;
393 unsigned VTypeI = (VSEWBits << 3) | AltFmt << 8 | TWiden << 9;
394 return VTypeI;
395}
396
397namespace IME {
398static void assertValidXLenForVType(unsigned XLen) {
399 assert((XLen == 32 || XLen == 64) && "Invalid XLEN");
400}
401
402static unsigned getLambdaShift(unsigned XLen) {
403 assertValidXLenForVType(XLen);
404 return XLen - 4;
405}
406
407static unsigned getAltFmtAShift(unsigned XLen) {
408 assertValidXLenForVType(XLen);
409 return XLen - 6;
410}
411
412static unsigned getAltFmtBShift(unsigned XLen) {
413 assertValidXLenForVType(XLen);
414 return XLen - 7;
415}
416
417static unsigned getBSShift(unsigned XLen) {
418 assertValidXLenForVType(XLen);
419 return XLen - 5;
420}
421
422unsigned encodeLambda(unsigned Lambda) {
423 assert(isValidLambda(Lambda) && "Invalid IME lambda");
424 if (Lambda == 0)
425 return 0;
426 return Log2_32(Value: Lambda) + 1;
427}
428
429std::optional<unsigned> decodeLambda(unsigned Encoding) {
430 assert(Encoding < 8 && "Invalid IME lambda encoding");
431 if (Encoding == 0)
432 return std::nullopt;
433 return 1U << (Encoding - 1);
434}
435
436uint64_t getVTypeFieldsMask(unsigned XLen) {
437 assertValidXLenForVType(XLen);
438 return (0x7ULL << getLambdaShift(XLen)) | (1ULL << getAltFmtAShift(XLen)) |
439 (1ULL << getAltFmtBShift(XLen)) | (1ULL << getBSShift(XLen));
440}
441
442uint64_t encodeVTypeFields(unsigned XLen, unsigned Lambda, bool AltFmtA,
443 bool AltFmtB, bool BlockSize16) {
444 assertValidXLenForVType(XLen);
445 uint64_t VType = uint64_t(encodeLambda(Lambda)) << getLambdaShift(XLen);
446 if (AltFmtA)
447 VType |= 1ULL << getAltFmtAShift(XLen);
448 if (AltFmtB)
449 VType |= 1ULL << getAltFmtBShift(XLen);
450 if (BlockSize16)
451 VType |= 1ULL << getBSShift(XLen);
452 return VType;
453}
454
455uint64_t addVTypeFields(uint64_t VType, unsigned XLen, unsigned Lambda,
456 bool AltFmtA, bool AltFmtB, bool BlockSize16) {
457 return (VType & ~getVTypeFieldsMask(XLen)) |
458 encodeVTypeFields(XLen, Lambda, AltFmtA, AltFmtB, BlockSize16);
459}
460
461unsigned getLambdaEncoding(uint64_t VType, unsigned XLen) {
462 assertValidXLenForVType(XLen);
463 return (VType >> getLambdaShift(XLen)) & 0x7;
464}
465
466std::optional<unsigned> getLambda(uint64_t VType, unsigned XLen) {
467 return decodeLambda(Encoding: getLambdaEncoding(VType, XLen));
468}
469
470bool isAltFmtA(uint64_t VType, unsigned XLen) {
471 assertValidXLenForVType(XLen);
472 return VType & (1ULL << getAltFmtAShift(XLen));
473}
474
475bool isAltFmtB(uint64_t VType, unsigned XLen) {
476 assertValidXLenForVType(XLen);
477 return VType & (1ULL << getAltFmtBShift(XLen));
478}
479
480bool isBlockSize16(uint64_t VType, unsigned XLen) {
481 assertValidXLenForVType(XLen);
482 return VType & (1ULL << getBSShift(XLen));
483}
484} // namespace IME
485
486std::pair<unsigned, bool> decodeVLMUL(VLMUL VLMul) {
487 switch (VLMul) {
488 default:
489 llvm_unreachable("Unexpected LMUL value!");
490 case LMUL_1:
491 case LMUL_2:
492 case LMUL_4:
493 case LMUL_8:
494 return std::make_pair(x: 1 << static_cast<unsigned>(VLMul), y: false);
495 case LMUL_F2:
496 case LMUL_F4:
497 case LMUL_F8:
498 return std::make_pair(x: 1 << (8 - static_cast<unsigned>(VLMul)), y: true);
499 }
500}
501
502void printVType(unsigned VType, raw_ostream &OS) {
503 unsigned Sew = getSEW(VType);
504 OS << "e" << Sew;
505
506 bool AltFmt = RISCVVType::isAltFmt(VType);
507 if (AltFmt)
508 OS << "alt";
509
510 unsigned LMul;
511 bool Fractional;
512 std::tie(args&: LMul, args&: Fractional) = decodeVLMUL(VLMul: getVLMUL(VType));
513
514 if (Fractional)
515 OS << ", mf";
516 else
517 OS << ", m";
518 OS << LMul;
519
520 if (isTailAgnostic(VType))
521 OS << ", ta";
522 else
523 OS << ", tu";
524
525 if (isMaskAgnostic(VType))
526 OS << ", ma";
527 else
528 OS << ", mu";
529}
530
531void printXSfmmVType(unsigned VType, raw_ostream &OS) {
532 OS << "e" << getSEW(VType) << ", w" << getXSfmmWiden(VType);
533}
534
535unsigned getSEWLMULRatio(unsigned SEW, VLMUL VLMul) {
536 unsigned LMul;
537 bool Fractional;
538 std::tie(args&: LMul, args&: Fractional) = decodeVLMUL(VLMul);
539
540 // Convert LMul to a fixed point value with 3 fractional bits.
541 LMul = Fractional ? (8 / LMul) : (LMul * 8);
542
543 assert(SEW >= 8 && "Unexpected SEW value");
544 return (SEW * 8) / LMul;
545}
546
547std::optional<VLMUL> getSameRatioLMUL(unsigned Ratio, unsigned EEW) {
548 unsigned EMULFixedPoint = (EEW * 8) / Ratio;
549 bool Fractional = EMULFixedPoint < 8;
550 unsigned EMUL = Fractional ? 8 / EMULFixedPoint : EMULFixedPoint / 8;
551 if (!isValidLMUL(LMUL: EMUL, Fractional))
552 return std::nullopt;
553 return RISCVVType::encodeLMUL(LMUL: EMUL, Fractional);
554}
555
556} // namespace RISCVVType
557
558} // namespace llvm
559