LLVM 24.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
26namespace RISCV {
27
28struct CPUModel {
32
33 bool isValid() const { return MVendorID != 0 && MArchID != 0 && MImpID != 0; }
34
35 bool operator==(const CPUModel &Other) const {
36 return MVendorID == Other.MVendorID && MArchID == Other.MArchID &&
37 MImpID == Other.MImpID;
38 }
39};
40
49
50/// Fatal errors encountered during parsing.
51struct ParserError : public ErrorInfo<ParserError, StringError> {
53 explicit ParserError(const Twine &S)
55 LLVM_ABI static char ID;
56};
57
58/// Warnings encountered during parsing.
59struct ParserWarning : public ErrorInfo<ParserWarning, StringError> {
61 explicit ParserWarning(const Twine &S)
63 LLVM_ABI static char ID;
64};
65
66// We use 64 bits as the known part in the scalable vector types.
67static constexpr unsigned RVVBitsPerBlock = 64;
68static constexpr unsigned RVVBytesPerBlock = RVVBitsPerBlock / 8;
69
71 SmallVectorImpl<std::string> &EnabledFeatures,
72 bool NeedPlus = false);
74LLVM_ABI void
76 SmallVectorImpl<StringRef> &Directives);
77/// Parse the tune feature string with the respective processor. If \p ProcName
78/// is empty, directives are not filtered by processor.
81 SmallVectorImpl<std::string> &TuneFeatures);
82LLVM_ABI bool parseCPU(StringRef CPU, bool IsRV64);
83LLVM_ABI bool parseTuneCPU(StringRef CPU, bool IsRV64);
86 bool IsRV64);
88 bool IsRV64);
94
95} // namespace RISCV
96
97namespace RISCVCFI {
103
104// See clang::getCFBranchLabelSchemeFlagVal() for possible CFBranchLabelScheme.
112} // namespace RISCVCFI
113
114namespace RISCVVType {
125
126enum {
130};
131
132// Is this a SEW value that can be encoded into the VTYPE format.
133inline static bool isValidSEW(unsigned SEW) {
134 return isPowerOf2_32(SEW) && SEW >= 8 && SEW <= 64;
135}
136
137// Is this a LMUL value that can be encoded into the VTYPE format.
138inline static bool isValidLMUL(unsigned LMUL, bool Fractional) {
139 return isPowerOf2_32(LMUL) && LMUL <= 8 && (!Fractional || LMUL != 1);
140}
141
142LLVM_ABI unsigned encodeVTYPE(VLMUL VLMUL, unsigned SEW, bool TailAgnostic,
143 bool MaskAgnostic, bool AltFmt = false);
144
145LLVM_ABI unsigned encodeXSfmmVType(unsigned SEW, unsigned Widen, bool AltFmt);
146
147namespace IME {
148inline static bool isValidLambda(unsigned Lambda) {
149 return Lambda == 0 || (isPowerOf2_32(Lambda) && Lambda <= 64);
150}
151
152LLVM_ABI unsigned encodeLambda(unsigned Lambda);
153
154LLVM_ABI std::optional<unsigned> decodeLambda(unsigned Encoding);
155
157
158LLVM_ABI uint64_t encodeVTypeFields(unsigned XLen, unsigned Lambda,
159 bool AltFmtA, bool AltFmtB,
160 bool BlockSize16);
161
162LLVM_ABI uint64_t addVTypeFields(uint64_t VType, unsigned XLen, unsigned Lambda,
163 bool AltFmtA, bool AltFmtB, bool BlockSize16);
164
165LLVM_ABI unsigned getLambdaEncoding(uint64_t VType, unsigned XLen);
166
167LLVM_ABI std::optional<unsigned> getLambda(uint64_t VType, unsigned XLen);
168
169LLVM_ABI bool isAltFmtA(uint64_t VType, unsigned XLen);
170
171LLVM_ABI bool isAltFmtB(uint64_t VType, unsigned XLen);
172
173LLVM_ABI bool isBlockSize16(uint64_t VType, unsigned XLen);
174} // namespace IME
175
176inline static VLMUL getVLMUL(unsigned VType) {
177 unsigned VLMul = VType & 0x7;
178 return static_cast<VLMUL>(VLMul);
179}
180
181// Decode VLMUL into 1,2,4,8 and fractional indicator.
182LLVM_ABI std::pair<unsigned, bool> decodeVLMUL(VLMUL VLMul);
183
184inline static VLMUL encodeLMUL(unsigned LMUL, bool Fractional) {
185 assert(isValidLMUL(LMUL, Fractional) && "Unsupported LMUL");
186 unsigned LmulLog2 = Log2_32(LMUL);
187 return static_cast<VLMUL>(Fractional ? 8 - LmulLog2 : LmulLog2);
188}
189
190inline static unsigned decodeVSEW(unsigned VSEW) {
191 assert(VSEW < 8 && "Unexpected VSEW value");
192 return 1 << (VSEW + 3);
193}
194
195inline static unsigned encodeSEW(unsigned SEW) {
196 assert(isValidSEW(SEW) && "Unexpected SEW value");
197 return Log2_32(SEW) - 3;
198}
199
200inline static unsigned getSEW(unsigned VType) {
201 unsigned VSEW = (VType >> 3) & 0x7;
202 return decodeVSEW(VSEW);
203}
204
205inline static unsigned decodeTWiden(unsigned TWiden) {
206 assert((TWiden == 1 || TWiden == 2 || TWiden == 3) &&
207 "Unexpected TWiden value");
208 return 1 << (TWiden - 1);
209}
210
211inline static bool hasXSfmmWiden(unsigned VType) {
212 unsigned TWiden = (VType >> 9) & 0x3;
213 return TWiden != 0;
214}
215
216inline static unsigned getXSfmmWiden(unsigned VType) {
217 unsigned TWiden = (VType >> 9) & 0x3;
218 assert(TWiden != 0 && "Invalid widen value");
219 return 1 << (TWiden - 1);
220}
221
222inline static bool isTailAgnostic(unsigned VType) { return VType & 0x40; }
223
224inline static bool isMaskAgnostic(unsigned VType) { return VType & 0x80; }
225
226inline static bool isAltFmt(unsigned VType) { return VType & 0x100; }
227
228inline static bool isValidVType(unsigned VType) {
229 return getSEW(VType) <= 64 && getVLMUL(VType) != LMUL_RESERVED &&
230 (!isAltFmt(VType) || getSEW(VType) < 32);
231}
232
233static inline bool isValidXSfmmVType(unsigned VTypeI) {
234 return (VTypeI & ~0x738) == 0 && RISCVVType::hasXSfmmWiden(VTypeI) &&
235 RISCVVType::getSEW(VTypeI) * RISCVVType::getXSfmmWiden(VTypeI) <= 64 &&
236 isValidVType(VTypeI);
237}
238
239LLVM_ABI void printVType(unsigned VType, raw_ostream &OS);
240
241LLVM_ABI void printXSfmmVType(unsigned VType, raw_ostream &OS);
242
243LLVM_ABI unsigned getSEWLMULRatio(unsigned SEW, VLMUL VLMul);
244
245LLVM_ABI std::optional<VLMUL> getSameRatioLMUL(unsigned Ratio, unsigned EEW);
246} // namespace RISCVVType
247
248} // namespace llvm
249
250#endif
static SDValue Widen(SelectionDAG *CurDAG, SDValue N)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
#define LLVM_ABI
Definition Compiler.h:211
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:880
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)
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:326
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:280
@ Other
Any other memory.
Definition ModRef.h:68
bool operator==(const CPUModel &Other) const
static LLVM_ABI char ID