LLVM 23.0.0git
RISCVTargetParser.h
Go to the documentation of this file.
1//===-- RISCVTargetParser - 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#ifndef LLVM_TARGETPARSER_RISCVTARGETPARSER_H
15#define LLVM_TARGETPARSER_RISCVTARGETPARSER_H
16
17#include "llvm/ADT/StringRef.h"
19#include "llvm/Support/Error.h"
22
23namespace llvm {
24
25class Triple;
26
27namespace RISCV {
28
29struct CPUModel {
33
34 bool isValid() const { return MVendorID != 0 && MArchID != 0 && MImpID != 0; }
35
36 bool operator==(const CPUModel &Other) const {
37 return MVendorID == Other.MVendorID && MArchID == Other.MArchID &&
38 MImpID == Other.MImpID;
39 }
40};
41
50
51/// Fatal errors encountered during parsing.
52struct ParserError : public ErrorInfo<ParserError, StringError> {
54 explicit ParserError(const Twine &S)
56 static char ID;
57};
58
59/// Warnings encountered during parsing.
60struct ParserWarning : public ErrorInfo<ParserWarning, StringError> {
62 explicit ParserWarning(const Twine &S)
64 static char ID;
65};
66
67// We use 64 bits as the known part in the scalable vector types.
68static constexpr unsigned RVVBitsPerBlock = 64;
69static constexpr unsigned RVVBytesPerBlock = RVVBitsPerBlock / 8;
70
72 SmallVectorImpl<std::string> &EnabledFeatures,
73 bool NeedPlus = false);
75LLVM_ABI void
77 SmallVectorImpl<StringRef> &Directives);
78/// Parse the tune feature string with the respective processor. If \p ProcName
79/// is empty, directives are not filtered by processor.
82 SmallVectorImpl<std::string> &TuneFeatures);
83LLVM_ABI bool parseCPU(StringRef CPU, bool IsRV64);
84LLVM_ABI bool parseTuneCPU(StringRef CPU, bool IsRV64);
87 bool IsRV64);
89 bool IsRV64);
95
96} // namespace RISCV
97
98namespace RISCVVType {
109
110enum {
114};
115
116// Is this a SEW value that can be encoded into the VTYPE format.
117inline static bool isValidSEW(unsigned SEW) {
118 return isPowerOf2_32(SEW) && SEW >= 8 && SEW <= 64;
119}
120
121// Is this a LMUL value that can be encoded into the VTYPE format.
122inline static bool isValidLMUL(unsigned LMUL, bool Fractional) {
123 return isPowerOf2_32(LMUL) && LMUL <= 8 && (!Fractional || LMUL != 1);
124}
125
126LLVM_ABI unsigned encodeVTYPE(VLMUL VLMUL, unsigned SEW, bool TailAgnostic,
127 bool MaskAgnostic, bool AltFmt = false);
128
129LLVM_ABI unsigned encodeXSfmmVType(unsigned SEW, unsigned Widen, bool AltFmt);
130
131namespace IME {
132inline static bool isValidLambda(unsigned Lambda) {
133 return Lambda == 0 || (isPowerOf2_32(Lambda) && Lambda <= 64);
134}
135
136LLVM_ABI unsigned encodeLambda(unsigned Lambda);
137
138LLVM_ABI std::optional<unsigned> decodeLambda(unsigned Encoding);
139
141
142LLVM_ABI uint64_t encodeVTypeFields(unsigned XLen, unsigned Lambda,
143 bool AltFmtA, bool AltFmtB,
144 bool BlockSize16);
145
146LLVM_ABI uint64_t addVTypeFields(uint64_t VType, unsigned XLen, unsigned Lambda,
147 bool AltFmtA, bool AltFmtB, bool BlockSize16);
148
149LLVM_ABI unsigned getLambdaEncoding(uint64_t VType, unsigned XLen);
150
151LLVM_ABI std::optional<unsigned> getLambda(uint64_t VType, unsigned XLen);
152
153LLVM_ABI bool isAltFmtA(uint64_t VType, unsigned XLen);
154
155LLVM_ABI bool isAltFmtB(uint64_t VType, unsigned XLen);
156
157LLVM_ABI bool isBlockSize16(uint64_t VType, unsigned XLen);
158} // namespace IME
159
160inline static VLMUL getVLMUL(unsigned VType) {
161 unsigned VLMul = VType & 0x7;
162 return static_cast<VLMUL>(VLMul);
163}
164
165// Decode VLMUL into 1,2,4,8 and fractional indicator.
166LLVM_ABI std::pair<unsigned, bool> decodeVLMUL(VLMUL VLMul);
167
168inline static VLMUL encodeLMUL(unsigned LMUL, bool Fractional) {
169 assert(isValidLMUL(LMUL, Fractional) && "Unsupported LMUL");
170 unsigned LmulLog2 = Log2_32(LMUL);
171 return static_cast<VLMUL>(Fractional ? 8 - LmulLog2 : LmulLog2);
172}
173
174inline static unsigned decodeVSEW(unsigned VSEW) {
175 assert(VSEW < 8 && "Unexpected VSEW value");
176 return 1 << (VSEW + 3);
177}
178
179inline static unsigned encodeSEW(unsigned SEW) {
180 assert(isValidSEW(SEW) && "Unexpected SEW value");
181 return Log2_32(SEW) - 3;
182}
183
184inline static unsigned getSEW(unsigned VType) {
185 unsigned VSEW = (VType >> 3) & 0x7;
186 return decodeVSEW(VSEW);
187}
188
189inline static unsigned decodeTWiden(unsigned TWiden) {
190 assert((TWiden == 1 || TWiden == 2 || TWiden == 3) &&
191 "Unexpected TWiden value");
192 return 1 << (TWiden - 1);
193}
194
195inline static bool hasXSfmmWiden(unsigned VType) {
196 unsigned TWiden = (VType >> 9) & 0x3;
197 return TWiden != 0;
198}
199
200inline static unsigned getXSfmmWiden(unsigned VType) {
201 unsigned TWiden = (VType >> 9) & 0x3;
202 assert(TWiden != 0 && "Invalid widen value");
203 return 1 << (TWiden - 1);
204}
205
206inline static bool isTailAgnostic(unsigned VType) { return VType & 0x40; }
207
208inline static bool isMaskAgnostic(unsigned VType) { return VType & 0x80; }
209
210inline static bool isAltFmt(unsigned VType) { return VType & 0x100; }
211
212inline static bool isValidVType(unsigned VType) {
213 return getSEW(VType) <= 64 && getVLMUL(VType) != LMUL_RESERVED &&
214 (!isAltFmt(VType) || getSEW(VType) < 32);
215}
216
217static inline bool isValidXSfmmVType(unsigned VTypeI) {
218 return (VTypeI & ~0x738) == 0 && RISCVVType::hasXSfmmWiden(VTypeI) &&
219 RISCVVType::getSEW(VTypeI) * RISCVVType::getXSfmmWiden(VTypeI) <= 64 &&
220 isValidVType(VTypeI);
221}
222
223LLVM_ABI void printVType(unsigned VType, raw_ostream &OS);
224
225LLVM_ABI void printXSfmmVType(unsigned VType, raw_ostream &OS);
226
227LLVM_ABI unsigned getSEWLMULRatio(unsigned SEW, VLMUL VLMul);
228
229LLVM_ABI std::optional<VLMUL> getSameRatioLMUL(unsigned Ratio, unsigned EEW);
230} // namespace RISCVVType
231
232} // namespace llvm
233
234#endif
static SDValue Widen(SelectionDAG *CurDAG, SDValue N)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition Compiler.h:213
Base class for user error types.
Definition Error.h:354
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
StringError(std::string &&S, std::error_code EC, bool PrintMsgOnly)
Definition Error.cpp:142
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition StringRef.h:882
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
LLVM_ABI bool isAltFmtB(uint64_t VType, unsigned XLen)
LLVM_ABI std::optional< unsigned > getLambda(uint64_t VType, unsigned XLen)
LLVM_ABI bool isBlockSize16(uint64_t VType, unsigned XLen)
static bool isValidLambda(unsigned Lambda)
LLVM_ABI uint64_t addVTypeFields(uint64_t VType, unsigned XLen, unsigned Lambda, bool AltFmtA, bool AltFmtB, bool BlockSize16)
LLVM_ABI bool isAltFmtA(uint64_t VType, unsigned XLen)
LLVM_ABI unsigned getLambdaEncoding(uint64_t VType, unsigned XLen)
LLVM_ABI std::optional< unsigned > decodeLambda(unsigned Encoding)
LLVM_ABI uint64_t getVTypeFieldsMask(unsigned XLen)
LLVM_ABI uint64_t encodeVTypeFields(unsigned XLen, unsigned Lambda, bool AltFmtA, bool AltFmtB, bool BlockSize16)
LLVM_ABI unsigned encodeLambda(unsigned Lambda)
static bool isTailAgnostic(unsigned VType)
static VLMUL encodeLMUL(unsigned LMUL, bool Fractional)
static unsigned decodeVSEW(unsigned VSEW)
LLVM_ABI void printXSfmmVType(unsigned VType, raw_ostream &OS)
LLVM_ABI unsigned encodeXSfmmVType(unsigned SEW, unsigned Widen, bool AltFmt)
static unsigned getXSfmmWiden(unsigned VType)
static bool isValidLMUL(unsigned LMUL, bool Fractional)
LLVM_ABI std::optional< VLMUL > getSameRatioLMUL(unsigned Ratio, unsigned EEW)
static bool isMaskAgnostic(unsigned VType)
LLVM_ABI std::pair< unsigned, bool > decodeVLMUL(VLMUL VLMul)
static bool hasXSfmmWiden(unsigned VType)
static unsigned encodeSEW(unsigned SEW)
LLVM_ABI unsigned getSEWLMULRatio(unsigned SEW, VLMUL VLMul)
static bool isValidSEW(unsigned SEW)
static bool isValidVType(unsigned VType)
LLVM_ABI void printVType(unsigned VType, raw_ostream &OS)
static bool isValidXSfmmVType(unsigned VTypeI)
static unsigned decodeTWiden(unsigned TWiden)
static bool isAltFmt(unsigned VType)
LLVM_ABI unsigned encodeVTYPE(VLMUL VLMUL, unsigned SEW, bool TailAgnostic, bool MaskAgnostic, bool AltFmt=false)
static unsigned getSEW(unsigned VType)
static VLMUL getVLMUL(unsigned VType)
LLVM_ABI bool hasFastVectorUnalignedAccess(StringRef CPU)
LLVM_ABI void getFeaturesForCPU(StringRef CPU, SmallVectorImpl< std::string > &EnabledFeatures, bool NeedPlus=false)
LLVM_ABI void fillValidTuneCPUArchList(SmallVectorImpl< StringRef > &Values, bool IsRV64)
LLVM_ABI CPUModel getCPUModel(StringRef CPU)
LLVM_ABI Error parseTuneFeatureString(StringRef ProcName, StringRef TFString, SmallVectorImpl< std::string > &TuneFeatures)
Parse the tune feature string with the respective processor.
LLVM_ABI void getAllTuneFeatures(SmallVectorImpl< StringRef > &TuneFeatures)
LLVM_ABI StringRef getMArchFromMcpu(StringRef CPU)
LLVM_ABI bool parseCPU(StringRef CPU, bool IsRV64)
LLVM_ABI bool hasFastScalarUnalignedAccess(StringRef CPU)
static constexpr unsigned RVVBitsPerBlock
LLVM_ABI bool hasValidCPUModel(StringRef CPU)
LLVM_ABI StringRef getCPUNameFromCPUModel(const CPUModel &Model)
static constexpr unsigned RVVBytesPerBlock
LLVM_ABI bool parseTuneCPU(StringRef CPU, bool IsRV64)
LLVM_ABI void fillValidCPUArchList(SmallVectorImpl< StringRef > &Values, bool IsRV64)
LLVM_ABI void getCPUConfigurableTuneFeatures(StringRef CPU, SmallVectorImpl< StringRef > &Directives)
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition Error.cpp:94
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:331
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:279
@ Other
Any other memory.
Definition ModRef.h:68
bool operator==(const CPUModel &Other) const