LLVM 24.0.0git
PISATargetStreamer.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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/// \file
10/// This file declares the PISA target streamer.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_LIB_TARGET_PISA_MCTARGETDESC_PISATARGETSTREAMER_H
15#define LLVM_LIB_TARGET_PISA_MCTARGETDESC_PISATARGETSTREAMER_H
16
17#include "PISADefines.h"
18#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/StringRef.h"
23#include "llvm/MC/MCStreamer.h"
24#include <cassert>
25#include <tuple>
26#include <utility>
27#include <variant>
28
29namespace llvm {
30
31namespace PISA {
32
35
43StorageSpace mapAddrSpaceToStorageSpace(unsigned AS);
44
49
52 std::variant<std::vector<uint32_t>, std::string, std::vector<std::string>,
55};
56
57// PISA function and directive name
59 // Whether to emit a kernel or function directive.
60 bool IsKernel = false;
61 // External linkage.
63 // Function name.
64 std::string Name;
65 // An optional return LLT for non-kernel functions.
67 // An optional list of kernel attributes.
69};
70
74
79
82 std::string Prefix;
83 unsigned Idx;
84};
85
87 unsigned Size = 0;
88 unsigned Align = 0;
89 unsigned PtrAlign = 0;
90 unsigned AS = ~0;
91 std::string ArgName;
92 std::string TypeName;
93 std::string TypeQualifier;
94
95 bool hasAlign() const { return Align != 0; }
96 bool hasPtrAlign() const { return PtrAlign != 0; }
97 bool hasAS() const { return AS != (unsigned)~0; }
98};
99// PISA header info
104// PISA function signature
112// PISA function register declarations
113struct RegDcls {
114 MapVector<std::tuple</*NumElts=*/unsigned, /*BitWidth=*/unsigned,
115 /*Type=*/unsigned>,
116 std::vector<std::pair</*Prefix=*/std::string, /*Id=*/unsigned>>>
118};
119
120// PISA register declaration types
121struct DataType {
122 unsigned NumElts;
123 unsigned EltSize;
124 unsigned RegType;
125 DataType(unsigned N, unsigned B, unsigned R)
126 : NumElts(N), EltSize(B), RegType(R) {}
127};
128struct TypeInfo {
129 unsigned RegStart;
130 unsigned RegCounter;
132 std::string Prefix;
133};
135private:
136 using TypeInfoMap =
137 MapVector<std::tuple</*NumElts=*/unsigned, /*BitWidth=*/unsigned,
138 /*Type=*/unsigned>,
139 TypeInfo>;
140 TypeInfoMap TypeInfos;
141
142 std::tuple<unsigned, unsigned, unsigned> tupleDT(DataType DT) {
143 return std::make_tuple(DT.NumElts, DT.EltSize, DT.RegType);
144 }
145
146public:
147 static DataType getTypeFromLLT(LLT Ty);
148 static LLT getLLTFromType(const DataType &Ty);
149 static std::string getPrefixFromLLT(LLT Ty);
150 static DataType getTypeFromPrefix(StringRef Prefix);
151
152 TypeInfoMap::const_iterator begin() const { return TypeInfos.begin(); }
153 TypeInfoMap::const_iterator end() const { return TypeInfos.end(); }
154
156 TypeInfo &getInfo(unsigned NumElts, unsigned EltSize, unsigned RegType) {
157 return getInfo(DataType(NumElts, EltSize, RegType));
158 }
160 TypeInfoMap::iterator It = TypeInfos.find(tupleDT(DT));
161 if (It == TypeInfos.end())
162 llvm_unreachable("Expect that requested DataType is already present in "
163 "the TypeInfos map");
164 return It->second;
165 }
166
167 TypeInfo &emplaceInfo(unsigned NumElts, unsigned EltSize, unsigned RegType) {
168 return emplaceInfo(DataType(NumElts, EltSize, RegType));
169 }
172 LLT Ty = getLLTFromType(DT);
173 std::pair<TypeInfoMap::iterator, bool> InsertResult = TypeInfos.try_emplace(
174 tupleDT(DT), TypeInfo{0, 0, Ty, getPrefixFromLLT(Ty)});
175 return InsertResult.first->second;
176 }
177
178 void insertInfo(unsigned NumElts, unsigned EltSize, unsigned RegType,
179 TypeInfo TI) {
180 insertInfo(DataType(NumElts, EltSize, RegType), std::move(TI));
181 }
183 std::pair<TypeInfoMap::iterator, bool> InsertResult =
184 TypeInfos.try_emplace(tupleDT(DT), std::move(TI));
185 assert(InsertResult.second);
186 }
187
188 // Once all function parameter declarations have been processed, call this
189 // to set all RegStart to the register after the last parameter of that Type
190 // Ex. if last parameter of Type Double-Word is %d3, then RegStart for the
191 // Double-Word TypeInfo should be 4. Note that RegCounter is not changed
192 // (in this example, it should still be 4 for d0-d3).
194 for (TypeInfoMap::value_type &Entry : TypeInfos)
195 Entry.second.RegStart = Entry.second.RegCounter;
196 }
197};
198
200 // External linkage.
205 // Variable name.
206 std::string Name;
208 int StackIndex = -1;
209 std::string Section;
210 std::string HostAccessName;
211};
212
219 struct GlobalExpr {
220 // e.g., "@f+8"
221 std::string Name;
222 int64_t Offset = 0;
223 };
224 struct Zeros {
225 // e.g., .zeros 7
227 };
228 using SpecialEntry = std::variant<GlobalExpr, Zeros>;
229 // This notes that the index in the initializer is actually the given
230 // global or ".zero" directive rather than the immediate value there.
232};
233
234// Variable declaration and initialization
239
240// Local variable declaration in private and shared space
245
246// Global variable declaration in global and const space
251
252} // namespace PISA
253
255public:
258
259 /// Emit header directives (.version, .target). Returns the resolved target
260 /// CPU string, which may differ from the initial CPU if a .target directive
261 /// was present in the input.
266 virtual void emitRegDcls(const PISA::RegDcls &, const PISA::DataTypes &) = 0;
268 virtual void emitFuncBodyStart() = 0;
269 virtual void emitFuncBodyEnd() = 0;
270
271 // Register names stored as <Prefix, Id>
272 using RegName = std::tuple<std::string, unsigned>;
273
274 // Array of register names for immutable APIs.
276
277 // If MustAdd is true, addRegDcl would assert if This reg is already added.
278 virtual void addRegDcl(unsigned NumElts, unsigned EltSize, unsigned RegType,
279 RegNamesRange RegNames, bool MustAdd = true) = 0;
281
282 void changeSection(const MCSection *CurSection, MCSection *Section,
283 uint32_t SubSection, raw_ostream &OS) override {}
284
285 // Only Asm stream subclass returns true
286 virtual bool isAsmStreamer() const { return false; }
287 virtual int getCurLine() const { return -1; }
288};
289} // namespace llvm
290
291#endif // LLVM_LIB_TARGET_PISA_MCTARGETDESC_PISATARGETSTREAMER_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
DXIL Finalize Linkage
Implement a low-level type suitable for MachineInstr level instruction selection.
This file declares definitions shared across the PISA backend.
This file defines the SmallVector class.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition MCSection.h:577
Streaming machine code generation interface.
Definition MCStreamer.h:221
MCTargetStreamer(MCStreamer &S)
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:39
typename SmallVector< std::pair< std::tuple< unsigned, unsigned, unsigned >, TypeInfo >, 0 >::const_iterator const_iterator
Definition MapVector.h:46
typename SmallVector< std::pair< std::tuple< unsigned, unsigned, unsigned >, TypeInfo >, 0 >::iterator iterator
Definition MapVector.h:45
typename SmallVector< std::pair< std::tuple< unsigned, unsigned, unsigned >, TypeInfo >, 0 >::value_type value_type
Definition MapVector.h:42
virtual void addRegDcl(unsigned NumElts, unsigned EltSize, unsigned RegType, RegNamesRange RegNames, bool MustAdd=true)=0
virtual void emitFuncBodyStart()=0
virtual void emitFunctionDeclaration(const PISA::FunctionDeclaration &)=0
virtual bool isAsmStreamer() const
virtual void emitFuncBodyEnd()=0
virtual void emitRegDcls(const PISA::RegDcls &, const PISA::DataTypes &)=0
~PISATargetStreamer() override
virtual void addLocalVariableDecl(const PISA::LocalVariableDcl &)=0
virtual void emitGlobalVariable(const PISA::GlobalVariableDcl &)=0
std::tuple< std::string, unsigned > RegName
ArrayRef< RegName > RegNamesRange
virtual StringRef emitHeader(const PISA::HeaderDcl &)=0
Emit header directives (.version, .target).
virtual void emitFunctionSignature(const PISA::FunctionSignature &)=0
virtual int getCurLine() const
virtual void emitLocalVariableDcls(const PISA::LocalVariableDcls &)=0
void changeSection(const MCSection *CurSection, MCSection *Section, uint32_t SubSection, raw_ostream &OS) override
Update streamer for a new active section.
TypeInfoMap::const_iterator end() const
void insertInfo(DataType DT, TypeInfo TI)
TypeInfo & emplaceInfo(DataType DT)
TypeInfo & getInfo(DataType DT)
static DataType getTypeFromPrefix(StringRef Prefix)
void insertInfo(unsigned NumElts, unsigned EltSize, unsigned RegType, TypeInfo TI)
TypeInfo & emplaceInfo(unsigned NumElts, unsigned EltSize, unsigned RegType)
TypeInfo & getInfo(LLT Ty)
static DataType getTypeFromLLT(LLT Ty)
static LLT getLLTFromType(const DataType &Ty)
TypeInfo & getInfo(unsigned NumElts, unsigned EltSize, unsigned RegType)
TypeInfo & emplaceInfo(LLT Ty)
static std::string getPrefixFromLLT(LLT Ty)
TypeInfoMap::const_iterator begin() const
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:28
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
StorageSpace mapAddrSpaceToStorageSpace(unsigned AS)
VariableDcl LocalVariableDcl
VariableDclInit GlobalVariableDcl
StringRef getLinkageTyName(LinkageTy Linkage)
This is an optimization pass for GlobalISel generic memory operations.
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
DataType(unsigned N, unsigned B, unsigned R)
SmallVector< FunctionDeclParam > FunctionParams
SmallVector< KernelAttribute > KernelAttrs
FunctionDirectiveAndName DN
SmallVector< KernelParameter > KernelParams
SmallVector< FunctionParameter > FunctionParams
SmallVector< GlobalVariableDcl > Vars
KernelAttributeType KernelAttrType
std::variant< std::vector< uint32_t >, std::string, std::vector< std::string >, uint32_t > KernelAttrValues
SmallVector< LocalVariableDcl > Vars
MapVector< std::tuple< unsigned, unsigned, unsigned >, std::vector< std::pair< std::string, unsigned > > > Regs
std::variant< GlobalExpr, Zeros > SpecialEntry
DenseMap< uint64_t, SpecialEntry > Exprs
SmallVector< InitElement > Initializer