LLVM 19.0.0git
RISCVTargetParser.cpp
Go to the documentation of this file.
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
19
20namespace llvm {
21namespace RISCV {
22
23enum CPUKind : unsigned {
24#define PROC(ENUM, NAME, DEFAULT_MARCH, FAST_SCALAR_UNALIGN, \
25 FAST_VECTOR_UNALIGN) \
26 CK_##ENUM,
27#define TUNE_PROC(ENUM, NAME) CK_##ENUM,
28#include "llvm/TargetParser/RISCVTargetParserDef.inc"
29};
30
31struct CPUInfo {
36 bool is64Bit() const { return DefaultMarch.starts_with("rv64"); }
37};
38
39constexpr CPUInfo RISCVCPUInfo[] = {
40#define PROC(ENUM, NAME, DEFAULT_MARCH, FAST_SCALAR_UNALIGN, \
41 FAST_VECTOR_UNALIGN) \
42 {NAME, DEFAULT_MARCH, FAST_SCALAR_UNALIGN, FAST_VECTOR_UNALIGN},
43#include "llvm/TargetParser/RISCVTargetParserDef.inc"
44};
45
47 for (auto &C : RISCVCPUInfo)
48 if (C.Name == CPU)
49 return &C;
50 return nullptr;
51}
52
54 const CPUInfo *Info = getCPUInfoByName(CPU);
55 return Info && Info->FastScalarUnalignedAccess;
56}
57
59 const CPUInfo *Info = getCPUInfoByName(CPU);
60 return Info && Info->FastVectorUnalignedAccess;
61}
62
63bool parseCPU(StringRef CPU, bool IsRV64) {
64 const CPUInfo *Info = getCPUInfoByName(CPU);
65
66 if (!Info)
67 return false;
68 return Info->is64Bit() == IsRV64;
69}
70
71bool parseTuneCPU(StringRef TuneCPU, bool IsRV64) {
72 std::optional<CPUKind> Kind =
74#define TUNE_PROC(ENUM, NAME) .Case(NAME, CK_##ENUM)
75 #include "llvm/TargetParser/RISCVTargetParserDef.inc"
76 .Default(std::nullopt);
77
78 if (Kind.has_value())
79 return true;
80
81 // Fallback to parsing as a CPU.
82 return parseCPU(TuneCPU, IsRV64);
83}
84
86 const CPUInfo *Info = getCPUInfoByName(CPU);
87 if (!Info)
88 return "";
89 return Info->DefaultMarch;
90}
91
93 for (const auto &C : RISCVCPUInfo) {
94 if (IsRV64 == C.is64Bit())
95 Values.emplace_back(C.Name);
96 }
97}
98
100 for (const auto &C : RISCVCPUInfo) {
101 if (IsRV64 == C.is64Bit())
102 Values.emplace_back(C.Name);
103 }
104#define TUNE_PROC(ENUM, NAME) Values.emplace_back(StringRef(NAME));
105#include "llvm/TargetParser/RISCVTargetParserDef.inc"
106}
107
108// This function is currently used by IREE, so it's not dead code.
110 SmallVectorImpl<std::string> &EnabledFeatures,
111 bool NeedPlus) {
112 StringRef MarchFromCPU = llvm::RISCV::getMArchFromMcpu(CPU);
113 if (MarchFromCPU == "")
114 return;
115
116 EnabledFeatures.clear();
118 MarchFromCPU, /* EnableExperimentalExtension */ true);
119
120 if (llvm::errorToBool(RII.takeError()))
121 return;
122
123 std::vector<std::string> FeatStrings =
124 (*RII)->toFeatures(/* AddAllExtensions */ false);
125 for (const auto &F : FeatStrings)
126 if (NeedPlus)
127 EnabledFeatures.push_back(F);
128 else
129 EnabledFeatures.push_back(F.substr(1));
130}
131} // namespace RISCV
132
133namespace RISCVVType {
134// Encode VTYPE into the binary format used by the the VSETVLI instruction which
135// is used by our MC layer representation.
136//
137// Bits | Name | Description
138// -----+------------+------------------------------------------------
139// 7 | vma | Vector mask agnostic
140// 6 | vta | Vector tail agnostic
141// 5:3 | vsew[2:0] | Standard element width (SEW) setting
142// 2:0 | vlmul[2:0] | Vector register group multiplier (LMUL) setting
143unsigned encodeVTYPE(RISCVII::VLMUL VLMUL, unsigned SEW, bool TailAgnostic,
144 bool MaskAgnostic) {
145 assert(isValidSEW(SEW) && "Invalid SEW");
146 unsigned VLMULBits = static_cast<unsigned>(VLMUL);
147 unsigned VSEWBits = encodeSEW(SEW);
148 unsigned VTypeI = (VSEWBits << 3) | (VLMULBits & 0x7);
149 if (TailAgnostic)
150 VTypeI |= 0x40;
151 if (MaskAgnostic)
152 VTypeI |= 0x80;
153
154 return VTypeI;
155}
156
157std::pair<unsigned, bool> decodeVLMUL(RISCVII::VLMUL VLMUL) {
158 switch (VLMUL) {
159 default:
160 llvm_unreachable("Unexpected LMUL value!");
165 return std::make_pair(1 << static_cast<unsigned>(VLMUL), false);
169 return std::make_pair(1 << (8 - static_cast<unsigned>(VLMUL)), true);
170 }
171}
172
173void printVType(unsigned VType, raw_ostream &OS) {
174 unsigned Sew = getSEW(VType);
175 OS << "e" << Sew;
176
177 unsigned LMul;
178 bool Fractional;
179 std::tie(LMul, Fractional) = decodeVLMUL(getVLMUL(VType));
180
181 if (Fractional)
182 OS << ", mf";
183 else
184 OS << ", m";
185 OS << LMul;
186
187 if (isTailAgnostic(VType))
188 OS << ", ta";
189 else
190 OS << ", tu";
191
192 if (isMaskAgnostic(VType))
193 OS << ", ma";
194 else
195 OS << ", mu";
196}
197
198unsigned getSEWLMULRatio(unsigned SEW, RISCVII::VLMUL VLMul) {
199 unsigned LMul;
200 bool Fractional;
201 std::tie(LMul, Fractional) = decodeVLMUL(VLMul);
202
203 // Convert LMul to a fixed point value with 3 fractional bits.
204 LMul = Fractional ? (8 / LMul) : (LMul * 8);
205
206 assert(SEW >= 8 && "Unexpected SEW value");
207 return (SEW * 8) / LMul;
208}
209
210std::optional<RISCVII::VLMUL>
211getSameRatioLMUL(unsigned SEW, RISCVII::VLMUL VLMUL, unsigned EEW) {
212 unsigned Ratio = RISCVVType::getSEWLMULRatio(SEW, VLMUL);
213 unsigned EMULFixedPoint = (EEW * 8) / Ratio;
214 bool Fractional = EMULFixedPoint < 8;
215 unsigned EMUL = Fractional ? 8 / EMULFixedPoint : EMULFixedPoint / 8;
216 if (!isValidLMUL(EMUL, Fractional))
217 return std::nullopt;
218 return RISCVVType::encodeLMUL(EMUL, Fractional);
219}
220
221} // namespace RISCVVType
222
223} // namespace llvm
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
#define ENUM(Name,...)
Definition: ClauseT.h:61
#define F(x, y, z)
Definition: MD5.cpp:55
#define TUNE_PROC(ENUM, NAME)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This file defines the SmallVector class.
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
static llvm::Expected< std::unique_ptr< RISCVISAInfo > > parseArchString(StringRef Arch, bool EnableExperimentalExtension, bool ExperimentalExtensionVersionCheck=true)
Parse RISC-V ISA info from arch string.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:950
void push_back(const T &Elt)
Definition: SmallVector.h:426
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition: StringRef.h:838
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:250
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:44
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#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
static bool isTailAgnostic(unsigned VType)
static RISCVII::VLMUL getVLMUL(unsigned VType)
std::pair< unsigned, bool > decodeVLMUL(RISCVII::VLMUL VLMUL)
unsigned getSEWLMULRatio(unsigned SEW, RISCVII::VLMUL VLMul)
static bool isValidLMUL(unsigned LMUL, bool Fractional)
static RISCVII::VLMUL encodeLMUL(unsigned LMUL, bool Fractional)
static bool isMaskAgnostic(unsigned VType)
static unsigned encodeSEW(unsigned SEW)
static bool isValidSEW(unsigned SEW)
void printVType(unsigned VType, raw_ostream &OS)
unsigned encodeVTYPE(RISCVII::VLMUL VLMUL, unsigned SEW, bool TailAgnostic, bool MaskAgnostic)
static unsigned getSEW(unsigned VType)
std::optional< RISCVII::VLMUL > getSameRatioLMUL(unsigned SEW, RISCVII::VLMUL VLMUL, unsigned EEW)
bool hasFastVectorUnalignedAccess(StringRef CPU)
void getFeaturesForCPU(StringRef CPU, SmallVectorImpl< std::string > &EnabledFeatures, bool NeedPlus=false)
void fillValidTuneCPUArchList(SmallVectorImpl< StringRef > &Values, bool IsRV64)
static const CPUInfo * getCPUInfoByName(StringRef CPU)
constexpr CPUInfo RISCVCPUInfo[]
StringRef getMArchFromMcpu(StringRef CPU)
bool parseCPU(StringRef CPU, bool IsRV64)
bool hasFastScalarUnalignedAccess(StringRef CPU)
bool parseTuneCPU(StringRef CPU, bool IsRV64)
void fillValidCPUArchList(SmallVectorImpl< StringRef > &Values, bool IsRV64)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool errorToBool(Error Err)
Helper for converting an Error to a bool.
Definition: Error.h:1099