LLVM  8.0.1
AMDKernelCodeTUtils.cpp
Go to the documentation of this file.
1 //===- AMDKernelCodeTUtils.cpp --------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// \file - utility functions to parse/print amd_kernel_code_t structure
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "AMDKernelCodeTUtils.h"
15 #include "SIDefines.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/StringRef.h"
22 #include <cassert>
23 #include <cstdint>
24 #include <utility>
25 
26 using namespace llvm;
27 
28 static ArrayRef<StringRef> get_amd_kernel_code_t_FldNames() {
29  static StringRef const Table[] = {
30  "", // not found placeholder
31 #define RECORD(name, altName, print, parse) #name
32 #include "AMDKernelCodeTInfo.h"
33 #undef RECORD
34  };
35  return makeArrayRef(Table);
36 }
37 
38 static ArrayRef<StringRef> get_amd_kernel_code_t_FldAltNames() {
39  static StringRef const Table[] = {
40  "", // not found placeholder
41 #define RECORD(name, altName, print, parse) #altName
42 #include "AMDKernelCodeTInfo.h"
43 #undef RECORD
44  };
45  return makeArrayRef(Table);
46 }
47 
49  const ArrayRef<StringRef> &altNames) {
50  StringMap<int> map;
51  assert(names.size() == altNames.size());
52  for (unsigned i = 0; i < names.size(); ++i) {
53  map.insert(std::make_pair(names[i], i));
54  map.insert(std::make_pair(altNames[i], i));
55  }
56  return map;
57 }
58 
60  static const auto map = createIndexMap(get_amd_kernel_code_t_FldNames(),
61  get_amd_kernel_code_t_FldAltNames());
62  return map.lookup(name) - 1; // returns -1 if not found
63 }
64 
66  return get_amd_kernel_code_t_FldNames()[index + 1];
67 }
68 
69 // Field printing
70 
72  return OS << Name << " = ";
73 }
74 
75 template <typename T, T amd_kernel_code_t::*ptr>
77  raw_ostream &OS) {
78  printName(OS, Name) << (int)(C.*ptr);
79 }
80 
81 template <typename T, T amd_kernel_code_t::*ptr, int shift, int width = 1>
83  raw_ostream &OS) {
84  const auto Mask = (static_cast<T>(1) << width) - 1;
85  printName(OS, Name) << (int)((c.*ptr >> shift) & Mask);
86 }
87 
88 using PrintFx = void(*)(StringRef, const amd_kernel_code_t &, raw_ostream &);
89 
90 static ArrayRef<PrintFx> getPrinterTable() {
91  static const PrintFx Table[] = {
92 #define RECORD(name, altName, print, parse) print
93 #include "AMDKernelCodeTInfo.h"
94 #undef RECORD
95  };
96  return makeArrayRef(Table);
97 }
98 
99 void llvm::printAmdKernelCodeField(const amd_kernel_code_t &C,
100  int FldIndex,
101  raw_ostream &OS) {
102  auto Printer = getPrinterTable()[FldIndex];
103  if (Printer)
104  Printer(get_amd_kernel_code_t_FieldName(FldIndex), C, OS);
105 }
106 
107 void llvm::dumpAmdKernelCode(const amd_kernel_code_t *C,
108  raw_ostream &OS,
109  const char *tab) {
110  const int Size = getPrinterTable().size();
111  for (int i = 0; i < Size; ++i) {
112  OS << tab;
113  printAmdKernelCodeField(*C, i, OS);
114  OS << '\n';
115  }
116 }
117 
118 // Field parsing
119 
120 static bool expectAbsExpression(MCAsmParser &MCParser, int64_t &Value, raw_ostream& Err) {
121 
122  if (MCParser.getLexer().isNot(AsmToken::Equal)) {
123  Err << "expected '='";
124  return false;
125  }
126  MCParser.getLexer().Lex();
127 
128  if (MCParser.parseAbsoluteExpression(Value)) {
129  Err << "integer absolute expression expected";
130  return false;
131  }
132  return true;
133 }
134 
135 template <typename T, T amd_kernel_code_t::*ptr>
136 static bool parseField(amd_kernel_code_t &C, MCAsmParser &MCParser,
137  raw_ostream &Err) {
138  int64_t Value = 0;
139  if (!expectAbsExpression(MCParser, Value, Err))
140  return false;
141  C.*ptr = (T)Value;
142  return true;
143 }
144 
145 template <typename T, T amd_kernel_code_t::*ptr, int shift, int width = 1>
146 static bool parseBitField(amd_kernel_code_t &C, MCAsmParser &MCParser,
147  raw_ostream &Err) {
148  int64_t Value = 0;
149  if (!expectAbsExpression(MCParser, Value, Err))
150  return false;
151  const uint64_t Mask = ((UINT64_C(1) << width) - 1) << shift;
152  C.*ptr &= (T)~Mask;
153  C.*ptr |= (T)((Value << shift) & Mask);
154  return true;
155 }
156 
157 using ParseFx = bool(*)(amd_kernel_code_t &, MCAsmParser &MCParser,
158  raw_ostream &Err);
159 
160 static ArrayRef<ParseFx> getParserTable() {
161  static const ParseFx Table[] = {
162 #define RECORD(name, altName, print, parse) parse
163 #include "AMDKernelCodeTInfo.h"
164 #undef RECORD
165  };
166  return makeArrayRef(Table);
167 }
168 
170  MCAsmParser &MCParser,
171  amd_kernel_code_t &C,
172  raw_ostream &Err) {
173  const int Idx = get_amd_kernel_code_t_FieldIndex(ID);
174  if (Idx < 0) {
175  Err << "unexpected amd_kernel_code_t field name " << ID;
176  return false;
177  }
178  auto Parser = getParserTable()[Idx];
179  return Parser ? Parser(C, MCParser, Err) : false;
180 }
uint64_t CallInst * C
static StringMap< int > createIndexMap(const ArrayRef< StringRef > &names, const ArrayRef< StringRef > &altNames)
This class represents lattice values for constants.
Definition: AllocatorList.h:24
Generic assembler parser interface, for use by target specific assembly parsers.
Definition: MCAsmParser.h:110
print alias Alias Set Printer
bool parseAmdKernelCodeField(StringRef ID, MCAsmParser &Parser, amd_kernel_code_t &C, raw_ostream &Err)
bool(*)(amd_kernel_code_t &, MCAsmParser &MCParser, raw_ostream &Err) ParseFx
amdgpu Simplify well known AMD library false Value Value const Twine & Name
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:451
AMD Kernel Code Object (amd_kernel_code_t).
static bool parseBitField(amd_kernel_code_t &C, MCAsmParser &MCParser, raw_ostream &Err)
#define T
void(*)(StringRef, const amd_kernel_code_t &, raw_ostream &) PrintFx
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
static void printField(StringRef Name, const amd_kernel_code_t &C, raw_ostream &OS)
static void printBitField(StringRef Name, const amd_kernel_code_t &c, raw_ostream &OS)
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
virtual MCAsmLexer & getLexer()=0
static raw_ostream & printName(raw_ostream &OS, StringRef Name)
void printAmdKernelCodeField(const amd_kernel_code_t &C, int FldIndex, raw_ostream &OS)
static int get_amd_kernel_code_t_FieldIndex(StringRef name)
static bool expectAbsExpression(MCAsmParser &MCParser, int64_t &Value, raw_ostream &Err)
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:366
const AsmToken & Lex()
Consume the next token from the input stream and return it.
Definition: MCAsmLexer.h:74
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:220
static StringRef get_amd_kernel_code_t_FieldName(int index)
virtual bool parseAbsoluteExpression(int64_t &Res)=0
Parse an expression which must evaluate to an absolute value.
uint32_t Size
Definition: Profile.cpp:47
bool isNot(AsmToken::TokenKind K) const
Check if the current token has kind K.
Definition: MCAsmLexer.h:139
void dumpAmdKernelCode(const amd_kernel_code_t *C, raw_ostream &OS, const char *tab)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
static const char * name
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:81
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
static bool parseField(amd_kernel_code_t &C, MCAsmParser &MCParser, raw_ostream &Err)