Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.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
14#include "llvm/TargetParser/RISCVTargetParser.h"
15#include "llvm/ADT/SmallVector.h"
16#include "llvm/ADT/StringSwitch.h"
17#include "llvm/TargetParser/RISCVISAInfo.h"
18
19namespacellvm {
20namespaceRISCV {
21
22enumCPUKind :unsigned {
23#define PROC(ENUM, NAME, DEFAULT_MARCH, FAST_SCALAR_UNALIGN, \
24 FAST_VECTOR_UNALIGN, MVENDORID, MARCHID, MIMPID) \
25 CK_##ENUM,
26#define TUNE_PROC(ENUM, NAME) CK_##ENUM,
27#include "llvm/TargetParser/RISCVTargetParserDef.inc"
28};
29
30constexprCPUInfoRISCVCPUInfo[] = {
31#define PROC(ENUM, NAME, DEFAULT_MARCH, FAST_SCALAR_UNALIGN, \
32 FAST_VECTOR_UNALIGN, MVENDORID, MARCHID, MIMPID) \
33 { \
34 NAME, \
35 DEFAULT_MARCH, \
36 FAST_SCALAR_UNALIGN, \
37 FAST_VECTOR_UNALIGN, \
38 {MVENDORID, MARCHID, MIMPID}, \
39 },
40#include "llvm/TargetParser/RISCVTargetParserDef.inc"
41};
42
43staticconstCPUInfo *getCPUInfoByName(StringRef CPU) {
44for (auto &C :RISCVCPUInfo)
45if (C.Name == CPU)
46return &C;
47returnnullptr;
48}
49
50boolhasFastScalarUnalignedAccess(StringRef CPU) {
51constCPUInfo *Info =getCPUInfoByName(CPU);
52returnInfo &&Info->FastScalarUnalignedAccess;
53}
54
55boolhasFastVectorUnalignedAccess(StringRef CPU) {
56constCPUInfo *Info =getCPUInfoByName(CPU);
57returnInfo &&Info->FastVectorUnalignedAccess;
58}
59
60boolhasValidCPUModel(StringRef CPU) {
61constCPUModel Model =getCPUModel(CPU);
62return Model.MVendorID != 0 && Model.MArchID != 0 && Model.MImpID != 0;
63}
64
65CPUModelgetCPUModel(StringRef CPU) {
66constCPUInfo *Info =getCPUInfoByName(CPU);
67if (!Info)
68return {0, 0, 0};
69returnInfo->Model;
70}
71
72boolparseCPU(StringRef CPU,bool IsRV64) {
73constCPUInfo *Info =getCPUInfoByName(CPU);
74
75if (!Info)
76returnfalse;
77returnInfo->is64Bit() == IsRV64;
78}
79
80boolparseTuneCPU(StringRef TuneCPU,bool IsRV64) {
81 std::optional<CPUKind> Kind =
82llvm::StringSwitch<std::optional<CPUKind>>(TuneCPU)
83#defineTUNE_PROC(ENUM, NAME) .Case(NAME, CK_##ENUM)
84 #include "llvm/TargetParser/RISCVTargetParserDef.inc"
85 .Default(std::nullopt);
86
87if (Kind.has_value())
88returntrue;
89
90// Fallback to parsing as a CPU.
91returnparseCPU(TuneCPU, IsRV64);
92}
93
94StringRefgetMArchFromMcpu(StringRef CPU) {
95constCPUInfo *Info =getCPUInfoByName(CPU);
96if (!Info)
97return"";
98returnInfo->DefaultMarch;
99}
100
101voidfillValidCPUArchList(SmallVectorImpl<StringRef> &Values,bool IsRV64) {
102for (constauto &C :RISCVCPUInfo) {
103if (IsRV64 ==C.is64Bit())
104 Values.emplace_back(C.Name);
105 }
106}
107
108voidfillValidTuneCPUArchList(SmallVectorImpl<StringRef> &Values,bool IsRV64) {
109for (constauto &C :RISCVCPUInfo) {
110if (IsRV64 ==C.is64Bit())
111 Values.emplace_back(C.Name);
112 }
113#define TUNE_PROC(ENUM, NAME) Values.emplace_back(StringRef(NAME));
114#include "llvm/TargetParser/RISCVTargetParserDef.inc"
115}
116
117// This function is currently used by IREE, so it's not dead code.
118voidgetFeaturesForCPU(StringRef CPU,
119SmallVectorImpl<std::string> &EnabledFeatures,
120bool NeedPlus) {
121StringRef MarchFromCPU =llvm::RISCV::getMArchFromMcpu(CPU);
122if (MarchFromCPU =="")
123return;
124
125 EnabledFeatures.clear();
126auto RII =RISCVISAInfo::parseArchString(
127 MarchFromCPU,/* EnableExperimentalExtension */true);
128
129if (llvm::errorToBool(RII.takeError()))
130return;
131
132 std::vector<std::string> FeatStrings =
133 (*RII)->toFeatures(/* AddAllExtensions */false);
134for (constauto &F : FeatStrings)
135if (NeedPlus)
136 EnabledFeatures.push_back(F);
137else
138 EnabledFeatures.push_back(F.substr(1));
139}
140
141namespaceRISCVExtensionBitmaskTable {
142#define GET_RISCVExtensionBitmaskTable_IMPL
143#include "llvm/TargetParser/RISCVTargetParserDef.inc"
144
145}// namespace RISCVExtensionBitmaskTable
146
147namespace{
148structLessExtName {
149bool operator()(const RISCVExtensionBitmaskTable::RISCVExtensionBitmask &LHS,
150 StringRefRHS) {
151return StringRef(LHS.Name) <RHS;
152 }
153};
154}// namespace
155
156}// namespace RISCV
157
158namespaceRISCVVType {
159// Encode VTYPE into the binary format used by the the VSETVLI instruction which
160// is used by our MC layer representation.
161//
162// Bits | Name | Description
163// -----+------------+------------------------------------------------
164// 7 | vma | Vector mask agnostic
165// 6 | vta | Vector tail agnostic
166// 5:3 | vsew[2:0] | Standard element width (SEW) setting
167// 2:0 | vlmul[2:0] | Vector register group multiplier (LMUL) setting
168unsignedencodeVTYPE(RISCVII::VLMUL VLMUL,unsigned SEW,bool TailAgnostic,
169bool MaskAgnostic) {
170assert(isValidSEW(SEW) &&"Invalid SEW");
171unsigned VLMULBits =static_cast<unsigned>(VLMUL);
172unsigned VSEWBits =encodeSEW(SEW);
173unsigned VTypeI = (VSEWBits << 3) | (VLMULBits & 0x7);
174if (TailAgnostic)
175 VTypeI |= 0x40;
176if (MaskAgnostic)
177 VTypeI |= 0x80;
178
179return VTypeI;
180}
181
182std::pair<unsigned, bool>decodeVLMUL(RISCVII::VLMUL VLMUL) {
183switch (VLMUL) {
184default:
185llvm_unreachable("Unexpected LMUL value!");
186caseRISCVII::VLMUL::LMUL_1:
187caseRISCVII::VLMUL::LMUL_2:
188caseRISCVII::VLMUL::LMUL_4:
189caseRISCVII::VLMUL::LMUL_8:
190return std::make_pair(1 <<static_cast<unsigned>(VLMUL),false);
191caseRISCVII::VLMUL::LMUL_F2:
192caseRISCVII::VLMUL::LMUL_F4:
193caseRISCVII::VLMUL::LMUL_F8:
194return std::make_pair(1 << (8 -static_cast<unsigned>(VLMUL)),true);
195 }
196}
197
198voidprintVType(unsigned VType,raw_ostream &OS) {
199unsigned Sew =getSEW(VType);
200OS <<"e" << Sew;
201
202unsigned LMul;
203bool Fractional;
204 std::tie(LMul, Fractional) =decodeVLMUL(getVLMUL(VType));
205
206if (Fractional)
207OS <<", mf";
208else
209OS <<", m";
210OS << LMul;
211
212if (isTailAgnostic(VType))
213OS <<", ta";
214else
215OS <<", tu";
216
217if (isMaskAgnostic(VType))
218OS <<", ma";
219else
220OS <<", mu";
221}
222
223unsignedgetSEWLMULRatio(unsigned SEW,RISCVII::VLMUL VLMul) {
224unsigned LMul;
225bool Fractional;
226 std::tie(LMul, Fractional) =decodeVLMUL(VLMul);
227
228// Convert LMul to a fixed point value with 3 fractional bits.
229 LMul = Fractional ? (8 / LMul) : (LMul * 8);
230
231assert(SEW >= 8 &&"Unexpected SEW value");
232return (SEW * 8) / LMul;
233}
234
235std::optional<RISCVII::VLMUL>
236getSameRatioLMUL(unsigned SEW,RISCVII::VLMUL VLMUL,unsigned EEW) {
237unsigned Ratio =RISCVVType::getSEWLMULRatio(SEW, VLMUL);
238unsigned EMULFixedPoint = (EEW * 8) / Ratio;
239bool Fractional = EMULFixedPoint < 8;
240unsigned EMUL = Fractional ? 8 / EMULFixedPoint : EMULFixedPoint / 8;
241if (!isValidLMUL(EMUL, Fractional))
242return std::nullopt;
243returnRISCVVType::encodeLMUL(EMUL, Fractional);
244}
245
246}// namespace RISCVVType
247
248}// namespace llvm
Info
Analysis containing CSE Info
Definition:CSEInfo.cpp:27
ENUM
#define ENUM(Name,...)
Definition:ClauseT.h:61
F
#define F(x, y, z)
Definition:MD5.cpp:55
RISCVISAInfo.h
TUNE_PROC
#define TUNE_PROC(ENUM, NAME)
Definition:RISCVTargetParser.cpp:26
RISCVTargetParser.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
OS
raw_pwrite_stream & OS
Definition:SampleProfWriter.cpp:51
SmallVector.h
This file defines the SmallVector class.
StringSwitch.h
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
RHS
Value * RHS
Definition:X86PartialReduction.cpp:74
LHS
Value * LHS
Definition:X86PartialReduction.cpp:73
llvm::RISCVISAInfo::parseArchString
static llvm::Expected< std::unique_ptr< RISCVISAInfo > > parseArchString(StringRef Arch, bool EnableExperimentalExtension, bool ExperimentalExtensionVersionCheck=true)
Parse RISC-V ISA info from arch string.
Definition:RISCVISAInfo.cpp:557
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition:SmallVector.h:573
llvm::SmallVectorImpl::emplace_back
reference emplace_back(ArgTypes &&... Args)
Definition:SmallVector.h:937
llvm::SmallVectorImpl::clear
void clear()
Definition:SmallVector.h:610
llvm::SmallVectorTemplateBase::push_back
void push_back(const T &Elt)
Definition:SmallVector.h:413
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition:StringRef.h:51
llvm::StringSwitch
A switch()-like statement whose cases are string literals.
Definition:StringSwitch.h:44
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition:raw_ostream.h:52
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition:ErrorHandling.h:143
llvm::CallingConv::C
@ C
The default llvm calling convention, compatible with C.
Definition:CallingConv.h:34
llvm::RISCVII::VLMUL
VLMUL
Definition:RISCVTargetParser.h:69
llvm::RISCVII::LMUL_1
@ LMUL_1
Definition:RISCVTargetParser.h:70
llvm::RISCVII::LMUL_F8
@ LMUL_F8
Definition:RISCVTargetParser.h:75
llvm::RISCVII::LMUL_4
@ LMUL_4
Definition:RISCVTargetParser.h:72
llvm::RISCVII::LMUL_8
@ LMUL_8
Definition:RISCVTargetParser.h:73
llvm::RISCVII::LMUL_F4
@ LMUL_F4
Definition:RISCVTargetParser.h:76
llvm::RISCVII::LMUL_F2
@ LMUL_F2
Definition:RISCVTargetParser.h:77
llvm::RISCVII::LMUL_2
@ LMUL_2
Definition:RISCVTargetParser.h:71
llvm::RISCVVType::isTailAgnostic
static bool isTailAgnostic(unsigned VType)
Definition:RISCVTargetParser.h:130
llvm::RISCVVType::getVLMUL
static RISCVII::VLMUL getVLMUL(unsigned VType)
Definition:RISCVTargetParser.h:101
llvm::RISCVVType::decodeVLMUL
std::pair< unsigned, bool > decodeVLMUL(RISCVII::VLMUL VLMUL)
Definition:RISCVTargetParser.cpp:182
llvm::RISCVVType::getSEWLMULRatio
unsigned getSEWLMULRatio(unsigned SEW, RISCVII::VLMUL VLMul)
Definition:RISCVTargetParser.cpp:223
llvm::RISCVVType::isValidLMUL
static bool isValidLMUL(unsigned LMUL, bool Fractional)
Definition:RISCVTargetParser.h:94
llvm::RISCVVType::encodeLMUL
static RISCVII::VLMUL encodeLMUL(unsigned LMUL, bool Fractional)
Definition:RISCVTargetParser.h:109
llvm::RISCVVType::isMaskAgnostic
static bool isMaskAgnostic(unsigned VType)
Definition:RISCVTargetParser.h:132
llvm::RISCVVType::encodeSEW
static unsigned encodeSEW(unsigned SEW)
Definition:RISCVTargetParser.h:120
llvm::RISCVVType::isValidSEW
static bool isValidSEW(unsigned SEW)
Definition:RISCVTargetParser.h:89
llvm::RISCVVType::printVType
void printVType(unsigned VType, raw_ostream &OS)
Definition:RISCVTargetParser.cpp:198
llvm::RISCVVType::encodeVTYPE
unsigned encodeVTYPE(RISCVII::VLMUL VLMUL, unsigned SEW, bool TailAgnostic, bool MaskAgnostic)
Definition:RISCVTargetParser.cpp:168
llvm::RISCVVType::getSEW
static unsigned getSEW(unsigned VType)
Definition:RISCVTargetParser.h:125
llvm::RISCVVType::getSameRatioLMUL
std::optional< RISCVII::VLMUL > getSameRatioLMUL(unsigned SEW, RISCVII::VLMUL VLMUL, unsigned EEW)
Definition:RISCVTargetParser.cpp:236
llvm::RISCV::hasFastVectorUnalignedAccess
bool hasFastVectorUnalignedAccess(StringRef CPU)
Definition:RISCVTargetParser.cpp:55
llvm::RISCV::getFeaturesForCPU
void getFeaturesForCPU(StringRef CPU, SmallVectorImpl< std::string > &EnabledFeatures, bool NeedPlus=false)
Definition:RISCVTargetParser.cpp:118
llvm::RISCV::fillValidTuneCPUArchList
void fillValidTuneCPUArchList(SmallVectorImpl< StringRef > &Values, bool IsRV64)
Definition:RISCVTargetParser.cpp:108
llvm::RISCV::getCPUInfoByName
static const CPUInfo * getCPUInfoByName(StringRef CPU)
Definition:RISCVTargetParser.cpp:43
llvm::RISCV::RISCVCPUInfo
constexpr CPUInfo RISCVCPUInfo[]
Definition:RISCVTargetParser.cpp:30
llvm::RISCV::getCPUModel
CPUModel getCPUModel(StringRef CPU)
Definition:RISCVTargetParser.cpp:65
llvm::RISCV::getMArchFromMcpu
StringRef getMArchFromMcpu(StringRef CPU)
Definition:RISCVTargetParser.cpp:94
llvm::RISCV::CPUKind
CPUKind
Definition:RISCVTargetParser.cpp:22
llvm::RISCV::parseCPU
bool parseCPU(StringRef CPU, bool IsRV64)
Definition:RISCVTargetParser.cpp:72
llvm::RISCV::hasFastScalarUnalignedAccess
bool hasFastScalarUnalignedAccess(StringRef CPU)
Definition:RISCVTargetParser.cpp:50
llvm::RISCV::hasValidCPUModel
bool hasValidCPUModel(StringRef CPU)
Definition:RISCVTargetParser.cpp:60
llvm::RISCV::parseTuneCPU
bool parseTuneCPU(StringRef CPU, bool IsRV64)
Definition:RISCVTargetParser.cpp:80
llvm::RISCV::fillValidCPUArchList
void fillValidCPUArchList(SmallVectorImpl< StringRef > &Values, bool IsRV64)
Definition:RISCVTargetParser.cpp:101
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::errorToBool
bool errorToBool(Error Err)
Helper for converting an Error to a bool.
Definition:Error.h:1099
llvm::RISCV::CPUInfo
Definition:RISCVTargetParser.h:41
llvm::RISCV::CPUModel
Definition:RISCVTargetParser.h:35

Generated on Thu Jul 17 2025 16:14:36 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp