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"
20#include "llvm/Support/Error.h"
23
24namespace llvm {
25
26class Triple;
27
28namespace RISCV {
29
30struct CPUModel {
34
35 bool isValid() const { return MVendorID != 0 && MArchID != 0 && MImpID != 0; }
36
37 bool operator==(const CPUModel &Other) const {
38 return MVendorID == Other.MVendorID && MArchID == Other.MArchID &&
39 MImpID == Other.MImpID;
40 }
41};
42
51
52/// Fatal errors encountered during parsing.
53struct ParserError : public ErrorInfo<ParserError, StringError> {
55 explicit ParserError(const Twine &S)
57 LLVM_ABI static char ID;
58};
59
60/// Warnings encountered during parsing.
61struct ParserWarning : public ErrorInfo<ParserWarning, StringError> {
63 explicit ParserWarning(const Twine &S)
65 LLVM_ABI static char ID;
66};
67
68// We use 64 bits as the known part in the scalable vector types.
69static constexpr unsigned RVVBitsPerBlock = 64;
70static constexpr unsigned RVVBytesPerBlock = RVVBitsPerBlock / 8;
71
73 SmallVectorImpl<std::string> &EnabledFeatures,
74 bool NeedPlus = false);
76LLVM_ABI void
78 SmallVectorImpl<StringRef> &Directives);
79/// Parse the tune feature string with the respective processor. If \p ProcName
80/// is empty, directives are not filtered by processor.
83 SmallVectorImpl<std::string> &TuneFeatures);
84LLVM_ABI bool parseCPU(StringRef CPU, bool IsRV64);
85LLVM_ABI bool parseTuneCPU(StringRef CPU, bool IsRV64);
88 bool IsRV64);
90 bool IsRV64);
96
97} // namespace RISCV
98
99namespace RISCVCFI {
105
106// See clang::getCFBranchLabelSchemeFlagVal() for possible CFBranchLabelScheme.
114} // namespace RISCVCFI
115
116namespace RISCVVType {
127
128enum {
132};
133
134// Is this a SEW value that can be encoded into the VTYPE format.
135inline static bool isValidSEW(unsigned SEW) {
136 return isPowerOf2_32(SEW) && SEW >= 8 && SEW <= 64;
137}
138
139// Is this a LMUL value that can be encoded into the VTYPE format.
140inline static bool isValidLMUL(unsigned LMUL, bool Fractional) {
141 return isPowerOf2_32(LMUL) && LMUL <= 8 && (!Fractional || LMUL != 1);
142}
143
144LLVM_ABI unsigned encodeVTYPE(VLMUL VLMUL, unsigned SEW, bool TailAgnostic,
145 bool MaskAgnostic, bool AltFmt = false);
146
147LLVM_ABI unsigned encodeXSfmmVType(unsigned SEW, unsigned Widen, bool AltFmt);
148
149namespace IME {
150inline static bool isValidLambda(unsigned Lambda) {
151 return Lambda == 0 || (isPowerOf2_32(Lambda) && Lambda <= 64);
152}
153
154LLVM_ABI unsigned encodeLambda(unsigned Lambda);
155
156LLVM_ABI std::optional<unsigned> decodeLambda(unsigned Encoding);
157
159
160LLVM_ABI uint64_t encodeVTypeFields(unsigned XLen, unsigned Lambda,
161 bool AltFmtA, bool AltFmtB,
162 bool BlockSize16);
163
164LLVM_ABI uint64_t addVTypeFields(uint64_t VType, unsigned XLen, unsigned Lambda,
165 bool AltFmtA, bool AltFmtB, bool BlockSize16);
166
167LLVM_ABI unsigned getLambdaEncoding(uint64_t VType, unsigned XLen);
168
169LLVM_ABI std::optional<unsigned> getLambda(uint64_t VType, unsigned XLen);
170
171LLVM_ABI bool isAltFmtA(uint64_t VType, unsigned XLen);
172
173LLVM_ABI bool isAltFmtB(uint64_t VType, unsigned XLen);
174
175LLVM_ABI bool isBlockSize16(uint64_t VType, unsigned XLen);
176} // namespace IME
177
178inline static VLMUL getVLMUL(unsigned VType) {
179 unsigned VLMul = VType & 0x7;
180 return static_cast<VLMUL>(VLMul);
181}
182
183// Decode VLMUL into 1,2,4,8 and fractional indicator.
184LLVM_ABI std::pair<unsigned, bool> decodeVLMUL(VLMUL VLMul);
185
186inline static VLMUL encodeLMUL(unsigned LMUL, bool Fractional) {
187 assert(isValidLMUL(LMUL, Fractional) && "Unsupported LMUL");
188 unsigned LmulLog2 = Log2_32(LMUL);
189 return static_cast<VLMUL>(Fractional ? 8 - LmulLog2 : LmulLog2);
190}
191
192inline static unsigned decodeVSEW(unsigned VSEW) {
193 assert(VSEW < 8 && "Unexpected VSEW value");
194 return 1 << (VSEW + 3);
195}
196
197inline static unsigned encodeSEW(unsigned SEW) {
198 assert(isValidSEW(SEW) && "Unexpected SEW value");
199 return Log2_32(SEW) - 3;
200}
201
202inline static unsigned getSEW(unsigned VType) {
203 unsigned VSEW = (VType >> 3) & 0x7;
204 return decodeVSEW(VSEW);
205}
206
207inline static unsigned decodeTWiden(unsigned TWiden) {
208 assert((TWiden == 1 || TWiden == 2 || TWiden == 3) &&
209 "Unexpected TWiden value");
210 return 1 << (TWiden - 1);
211}
212
213inline static bool hasXSfmmWiden(unsigned VType) {
214 unsigned TWiden = (VType >> 9) & 0x3;
215 return TWiden != 0;
216}
217
218inline static unsigned getXSfmmWiden(unsigned VType) {
219 unsigned TWiden = (VType >> 9) & 0x3;
220 assert(TWiden != 0 && "Invalid widen value");
221 return 1 << (TWiden - 1);
222}
223
224inline static bool isTailAgnostic(unsigned VType) { return VType & 0x40; }
225
226inline static bool isMaskAgnostic(unsigned VType) { return VType & 0x80; }
227
228inline static bool isAltFmt(unsigned VType) { return VType & 0x100; }
229
230inline static bool isValidVType(unsigned VType) {
231 return getSEW(VType) <= 64 && getVLMUL(VType) != LMUL_RESERVED &&
232 (!isAltFmt(VType) || getSEW(VType) < 32);
233}
234
235static inline bool isValidXSfmmVType(unsigned VTypeI) {
236 return (VTypeI & ~0x738) == 0 && RISCVVType::hasXSfmmWiden(VTypeI) &&
237 RISCVVType::getSEW(VTypeI) * RISCVVType::getXSfmmWiden(VTypeI) <= 64 &&
238 isValidVType(VTypeI);
239}
240
241LLVM_ABI void printVType(unsigned VType, raw_ostream &OS);
242
243LLVM_ABI void printXSfmmVType(unsigned VType, raw_ostream &OS);
244
245LLVM_ABI unsigned getSEWLMULRatio(unsigned SEW, VLMUL VLMul);
246
247LLVM_ABI std::optional<VLMUL> getSameRatioLMUL(unsigned Ratio, unsigned EEW);
248} // namespace RISCVVType
249
250} // namespace llvm
251
252#endif
static SDValue Widen(SelectionDAG *CurDAG, SDValue N)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition Compiler.h:215
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
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:888
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
A switch()-like statement whose cases are string literals.
StringSwitch & Case(StringLiteral S, T Value)
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
ZicfilpLabelSchemeKind getZicfilpLabelScheme(const StringRef CFBranchLabelScheme)
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.
RelativeUniformCounterPtr Values
Definition InstrProf.h:91
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
static LLVM_ABI char ID