LLVM 19.0.0git
AMDKernelCodeTUtils.cpp
Go to the documentation of this file.
1//===- AMDKernelCodeTUtils.cpp --------------------------------------------===//
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 - utility functions to parse/print amd_kernel_code_t structure
10//
11//===----------------------------------------------------------------------===//
12
13#include "AMDKernelCodeTUtils.h"
14#include "AMDKernelCodeT.h"
15#include "SIDefines.h"
16#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/StringMap.h"
18#include "llvm/ADT/StringRef.h"
22
23using namespace llvm;
24
26 static StringRef const Table[] = {
27 "", // not found placeholder
28#define RECORD(name, altName, print, parse) #name
29#include "AMDKernelCodeTInfo.h"
30#undef RECORD
31 };
32 return ArrayRef(Table);
33}
34
36 static StringRef const Table[] = {
37 "", // not found placeholder
38#define RECORD(name, altName, print, parse) #altName
39#include "AMDKernelCodeTInfo.h"
40#undef RECORD
41 };
42 return ArrayRef(Table);
43}
44
46 const ArrayRef<StringRef> &altNames) {
48 assert(names.size() == altNames.size());
49 for (unsigned i = 0; i < names.size(); ++i) {
50 map.insert(std::pair(names[i], i));
51 map.insert(std::pair(altNames[i], i));
52 }
53 return map;
54}
55
57 static const auto map = createIndexMap(get_amd_kernel_code_t_FldNames(),
59 return map.lookup(name) - 1; // returns -1 if not found
60}
61
63 return get_amd_kernel_code_t_FldNames()[index + 1];
64}
65
66// Field printing
67
69 return OS << Name << " = ";
70}
71
72template <typename T, T amd_kernel_code_t::*ptr>
74 raw_ostream &OS) {
75 printName(OS, Name) << (int)(C.*ptr);
76}
77
78template <typename T, T amd_kernel_code_t::*ptr, int shift, int width = 1>
80 raw_ostream &OS) {
81 const auto Mask = (static_cast<T>(1) << width) - 1;
82 printName(OS, Name) << (int)((c.*ptr >> shift) & Mask);
83}
84
85using PrintFx = void(*)(StringRef, const amd_kernel_code_t &, raw_ostream &);
86
88 static const PrintFx Table[] = {
89#define RECORD(name, altName, print, parse) print
90#include "AMDKernelCodeTInfo.h"
91#undef RECORD
92 };
93 return ArrayRef(Table);
94}
95
97 int FldIndex,
98 raw_ostream &OS) {
99 auto Printer = getPrinterTable()[FldIndex];
100 if (Printer)
102}
103
106 const char *tab) {
107 const int Size = getPrinterTable().size();
108 for (int i = 0; i < Size; ++i) {
109 OS << tab;
111 OS << '\n';
112 }
113}
114
115// Field parsing
116
117static bool expectAbsExpression(MCAsmParser &MCParser, int64_t &Value, raw_ostream& Err) {
118
119 if (MCParser.getLexer().isNot(AsmToken::Equal)) {
120 Err << "expected '='";
121 return false;
122 }
123 MCParser.getLexer().Lex();
124
125 if (MCParser.parseAbsoluteExpression(Value)) {
126 Err << "integer absolute expression expected";
127 return false;
128 }
129 return true;
130}
131
132template <typename T, T amd_kernel_code_t::*ptr>
134 raw_ostream &Err) {
135 int64_t Value = 0;
136 if (!expectAbsExpression(MCParser, Value, Err))
137 return false;
138 C.*ptr = (T)Value;
139 return true;
140}
141
142template <typename T, T amd_kernel_code_t::*ptr, int shift, int width = 1>
144 raw_ostream &Err) {
145 int64_t Value = 0;
146 if (!expectAbsExpression(MCParser, Value, Err))
147 return false;
148 const uint64_t Mask = ((UINT64_C(1) << width) - 1) << shift;
149 C.*ptr &= (T)~Mask;
150 C.*ptr |= (T)((Value << shift) & Mask);
151 return true;
152}
153
155 raw_ostream &Err);
156
158 static const ParseFx Table[] = {
159#define RECORD(name, altName, print, parse) parse
160#include "AMDKernelCodeTInfo.h"
161#undef RECORD
162 };
163 return ArrayRef(Table);
164}
165
167 MCAsmParser &MCParser,
169 raw_ostream &Err) {
171 if (Idx < 0) {
172 Err << "unexpected amd_kernel_code_t field name " << ID;
173 return false;
174 }
175 auto Parser = getParserTable()[Idx];
176 return Parser ? Parser(C, MCParser, Err) : false;
177}
This file defines the StringMap class.
static ArrayRef< StringRef > get_amd_kernel_code_t_FldAltNames()
static ArrayRef< PrintFx > getPrinterTable()
static void printBitField(StringRef Name, const amd_kernel_code_t &c, raw_ostream &OS)
void(*)(StringRef, const amd_kernel_code_t &, raw_ostream &) PrintFx
static raw_ostream & printName(raw_ostream &OS, StringRef Name)
static bool parseBitField(amd_kernel_code_t &C, MCAsmParser &MCParser, raw_ostream &Err)
static bool expectAbsExpression(MCAsmParser &MCParser, int64_t &Value, raw_ostream &Err)
static void printField(StringRef Name, const amd_kernel_code_t &C, raw_ostream &OS)
static StringMap< int > createIndexMap(const ArrayRef< StringRef > &names, const ArrayRef< StringRef > &altNames)
bool(*)(amd_kernel_code_t &, MCAsmParser &MCParser, raw_ostream &Err) ParseFx
static ArrayRef< StringRef > get_amd_kernel_code_t_FldNames()
static bool parseField(amd_kernel_code_t &C, MCAsmParser &MCParser, raw_ostream &Err)
static StringRef get_amd_kernel_code_t_FieldName(int index)
static ArrayRef< ParseFx > getParserTable()
static int get_amd_kernel_code_t_FieldIndex(StringRef name)
dxil pretty DXIL Metadata Pretty Printer
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
std::string Name
uint64_t Size
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const char * name
Definition: SMEABIPass.cpp:49
raw_pwrite_stream & OS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
bool isNot(AsmToken::TokenKind K) const
Check if the current token has kind K.
Definition: MCAsmLexer.h:144
const AsmToken & Lex()
Consume the next token from the input stream and return it.
Definition: MCAsmLexer.h:79
Generic assembler parser interface, for use by target specific assembly parsers.
Definition: MCAsmParser.h:123
virtual MCAsmLexer & getLexer()=0
virtual bool parseAbsoluteExpression(int64_t &Res)=0
Parse an expression which must evaluate to an absolute value.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:128
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:307
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
LLVM Value Representation.
Definition: Value.h:74
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void dumpAmdKernelCode(const amd_kernel_code_t *C, raw_ostream &OS, const char *tab)
void printAmdKernelCodeField(const amd_kernel_code_t &C, int FldIndex, raw_ostream &OS)
bool parseAmdKernelCodeField(StringRef ID, MCAsmParser &Parser, amd_kernel_code_t &C, raw_ostream &Err)
AMD Kernel Code Object (amd_kernel_code_t).