| 1 |
/*========================== begin_copyright_notice ============================ |
| 2 |
|
| 3 |
Copyright (C) 2022 Intel Corporation |
| 4 |
|
| 5 |
SPDX-License-Identifier: MIT |
| 6 |
|
| 7 |
============================= end_copyright_notice ===========================*/ |
| 8 |
#include "../../../bits.hpp" |
| 9 |
#include "../MessageDecoder.hpp" |
| 10 |
|
| 11 |
#include <functional> |
| 12 |
#include <sstream> |
| 13 |
|
| 14 |
namespace iga { |
| 15 |
// field description for table-based lookups |
| 16 |
struct ValidValue { |
| 17 |
uint32_t encoding; |
| 18 |
const char *desc, *sym; |
| 19 |
constexpr ValidValue(uint32_t enc, const char *_sym, const char *_desc) |
| 20 |
: encoding(enc), sym(_sym), desc(_desc) {} |
| 21 |
}; // ValidValue<T> |
| 22 |
template <typename T> struct SimpleValidValue : ValidValue { |
| 23 |
T value; |
| 24 |
constexpr SimpleValidValue(uint32_t enc, T val, const char *desc, |
| 25 |
const char *sym) |
| 26 |
: ValidValue(enc, desc, sym), value(val) {} |
| 27 |
}; |
| 28 |
struct LscCacheValidValue : ValidValue { |
| 29 |
CacheOpt l1, l2, l3; |
| 30 |
constexpr LscCacheValidValue(uint32_t enc, CacheOpt _l1, CacheOpt _l2, CacheOpt _l3, |
| 31 |
const char *desc, const char *sym) |
| 32 |
: ValidValue(enc, desc, sym), l1(_l1), l2(_l2), l3(_l3) {} |
| 33 |
}; // LscCacheValidValue |
| 34 |
struct LscDataSizeValidValue : ValidValue { |
| 35 |
int bitsInMem, bitsInReg; |
| 36 |
constexpr LscDataSizeValidValue(uint32_t enc, int bim, int bir, |
| 37 |
const char *desc, const char *sym) |
| 38 |
: ValidValue(enc, desc, sym), bitsInMem(bim), bitsInReg(bir) {} |
| 39 |
}; // LscDataSizeValidValue |
| 40 |
|
| 41 |
static const int DEFAULT_EXEC_SIZE = 32; |
| 42 |
static const int BYTES_PER_REGISTER = 64; |
| 43 |
static const int BITS_PER_REGISTER = 8 * BYTES_PER_REGISTER; |
| 44 |
|
| 45 |
struct MessageDecoderE64 { |
| 46 |
// inputs |
| 47 |
const Model &decodeModel; |
| 48 |
const SFID sfid; |
| 49 |
ExecSize execSize; |
| 50 |
int src0Len, src1Len; |
| 51 |
// |
| 52 |
uint64_t desc; |
| 53 |
RegRef indDesc0, indDesc1; |
| 54 |
|
| 55 |
// outputs |
| 56 |
DecodeResult &result; |
| 57 |
|
| 58 |
MessageDecoderE64(Platform _platform, SFID _sfid, ExecSize _instExecSize, |
| 59 |
int _src0Len, int _src1Len, uint64_t _desc, RegRef id0, |
| 60 |
RegRef id1, DecodeResult &_result); |
| 61 |
|
| 62 |
Platform platform() const { return model().platform; } |
| 63 |
|
| 64 |
const Model &model() const { return decodeModel; } |
| 65 |
|
| 66 |
bool platformInRange(Platform lo, Platform hi) const { |
| 67 |
return platform() >= lo && platform() <= hi; |
| 68 |
} |
| 69 |
|
| 70 |
void setDoc(const char *doc0, const char *doc1 = nullptr, |
| 71 |
const char *doc2 = nullptr, const char *doc3 = nullptr) { |
| 72 |
addDoc(DocRef::INVALID, nullptr, doc0); |
| 73 |
addDoc(DocRef::INVALID, nullptr, doc1); |
| 74 |
addDoc(DocRef::INVALID, nullptr, doc2); |
| 75 |
addDoc(DocRef::INVALID, nullptr, doc3); |
| 76 |
} |
| 77 |
// appends a documentation note to the next slot |
| 78 |
void addDoc(DocRef::Kind k, const char *name, const char *link) { |
| 79 |
if (k == DocRef::INVALID && !name && !link) |
| 80 |
return; |
| 81 |
result.info.refs.emplace_back(k, name, link); |
| 82 |
} |
| 83 |
void addDocs( |
| 84 |
DocRef::Kind k0, const char *nm0, const char *lnk0, |
| 85 |
DocRef::Kind k1, const char *nm1, const char *lnk1, |
| 86 |
DocRef::Kind k2 = DocRef::INVALID, |
| 87 |
const char *nm2 = nullptr, const char *lnk2 = nullptr, |
| 88 |
DocRef::Kind k3 = DocRef::INVALID, |
| 89 |
const char *nm3 = nullptr, const char *lnk3 = nullptr, |
| 90 |
DocRef::Kind k4 = DocRef::INVALID, |
| 91 |
const char *nm4 = nullptr, const char *lnk4 = nullptr) |
| 92 |
{ |
| 93 |
addDoc(k0, nm0, lnk0); |
| 94 |
addDoc(k1, nm1, lnk1); |
| 95 |
addDoc(k2, nm2, lnk2); |
| 96 |
addDoc(k3, nm3, lnk3); |
| 97 |
addDoc(k4, nm4, lnk4); |
| 98 |
} |
| 99 |
|
| 100 |
///////////////////////////////////////////////////////////// |
| 101 |
// shared fields between LSC and other units |
| 102 |
const LscCacheValidValue *decodeCacheControlValue(SendOp sop); |
| 103 |
std::string decodeVectorSizeCmask(const char SYN[4]); // return syntax (.e.g ".xy") |
| 104 |
// |
| 105 |
// the following are used in LSC and sampler |
| 106 |
void decodeSsIdx(); |
| 107 |
void decodeSmsIdx(); |
| 108 |
void decodeUvrOffsets(); |
| 109 |
std::string decodeSurfaceTypeHint(); |
| 110 |
|
| 111 |
void constructSurfaceSyntax(); |
| 112 |
|
| 113 |
void checkBytesLen(const char *what, int decBytes, int expectRegs) { |
| 114 |
if (decBytes < 0 || expectRegs < 0) |
| 115 |
return; |
| 116 |
int decRegs = |
| 117 |
decBytes == 0 ? 0 : std::max(1, decBytes / BYTES_PER_REGISTER); |
| 118 |
if (decRegs != expectRegs) { |
| 119 |
std::stringstream ss; |
| 120 |
ss << "this message takes " << decRegs << " " << what << " reg"; |
| 121 |
if (decRegs != 1) |
| 122 |
ss << "s"; |
| 123 |
error(0, 6, ss.str()); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
///////////////////////////////////////////////////////////// |
| 128 |
template <typename T> |
| 129 |
const T *lookupFromTable(const char *field, int off, int len, const T *table, |
| 130 |
size_t tableLen) { |
| 131 |
const auto enc = getDescBits(off, len); |
| 132 |
for (size_t i = 0; i < tableLen; i++) { |
| 133 |
if (table[i].encoding == enc) { |
| 134 |
addField(field, off, len, enc, table[i].desc); |
| 135 |
return &table[i]; |
| 136 |
} |
| 137 |
} |
| 138 |
error(off, len, std::string("invalid ") + field); |
| 139 |
return nullptr; |
| 140 |
} |
| 141 |
|
| 142 |
///////////////////////////////////////////////////////////// |
| 143 |
// diagnostics |
| 144 |
template <typename T1, typename T2 = const char *, typename T3 = const char *> |
| 145 |
void addDiag(DiagnosticList &dl, int off, int len, T1 t1, T2 t2 = "", |
| 146 |
T3 t3 = "") { |
| 147 |
std::stringstream ss; |
| 148 |
ss << t1 << t2 << t3; |
| 149 |
dl.emplace_back(DescField(off, len), ss.str()); |
| 150 |
} |
| 151 |
template <typename T1, typename T2 = const char *, typename T3 = const char *> |
| 152 |
void warning(int off, int len, T1 t1, T2 t2 = "", T3 t3 = "") { |
| 153 |
addDiag(result.warnings, off, len, t1, t2, t3); |
| 154 |
} |
| 155 |
template <typename T1, typename T2 = const char *, typename T3 = const char *> |
| 156 |
void error(int off, int len, T1 t1, T2 t2 = "", T3 t3 = "") { |
| 157 |
addDiag(result.errors, off, len, t1, t2, t3); |
| 158 |
} |
| 159 |
|
| 160 |
///////////////////////////////////////////////////////////// |
| 161 |
// peeks at a field without adding it |
| 162 |
uint32_t getDescBits(int off, int len) const { |
| 163 |
IGA_ASSERT(len <= 32, "field too large (use getDescBits64)"); |
| 164 |
return (uint32_t)getDescBits64(off, len); |
| 165 |
} |
| 166 |
int32_t getDescBitsSigned(int off, int len) const { |
| 167 |
return getSignedBits<int32_t>(getDescBits(off, len), 0, len); |
| 168 |
} |
| 169 |
uint64_t getDescBits64(int off, int len) const { |
| 170 |
uint64_t bits = desc; |
| 171 |
uint64_t mask = len == 64 ? 0xFFFFFFFFFFFFFFFFull : ((1ull << len) - 1); |
| 172 |
return ((bits >> off) & mask); |
| 173 |
} |
| 174 |
|
| 175 |
uint32_t getDescBit(int off) const { return getDescBits(off, 1) != 0; } |
| 176 |
|
| 177 |
// normally use getDescBitsField, but in cases where you've already |
| 178 |
// decoded, the meaning and just want to record the result |
| 179 |
void addField(const char *fieldName, int off, int len, uint64_t val, |
| 180 |
const std::string& meaning) { |
| 181 |
Fragment f(fieldName, off, len); |
| 182 |
addFrag(f, val, meaning); |
| 183 |
} |
| 184 |
void addReserved(int off, int len) { |
| 185 |
Fragment f("Reserved", off, len); |
| 186 |
addFrag(f, getDescBits64(off, len), ""); |
| 187 |
} |
| 188 |
void addFrag(Fragment f, uint64_t val, const std::string& meaning) { |
| 189 |
for (const auto &fvs : result.fields) { |
| 190 |
const auto &f1 = std::get<0>(fvs); |
| 191 |
if (f1.overlaps(f)) { |
| 192 |
// uncomment for debugging |
| 193 |
std::stringstream ss; |
| 194 |
ss << "overlapped fields: " << f1.name << " and " << f.name; |
| 195 |
warning(f.offset, f.length, ss.str()); |
| 196 |
// IGA_ASSERT_FALSE(ss.str().c_str()); |
| 197 |
return; // replicated access (don't record again) |
| 198 |
} |
| 199 |
} |
| 200 |
result.fields.emplace_back(f, (uint32_t)val, meaning); |
| 201 |
} |
| 202 |
|
| 203 |
/////////////////////////////////////////////////////////////////////////// |
| 204 |
// decoder helpers |
| 205 |
template <typename T> |
| 206 |
T decodeField(const char *fieldName, int off, int len, |
| 207 |
std::function<T(uint32_t, std::stringstream &)> dec) { |
| 208 |
auto enc = getDescBits(off, len); |
| 209 |
std::stringstream ss; |
| 210 |
T v = dec(enc, ss); |
| 211 |
addField(fieldName, off, len, enc, ss.str()); |
| 212 |
return v; |
| 213 |
} |
| 214 |
void decodeField(const char *fieldName, int off, int len, |
| 215 |
std::function<void(uint32_t, std::stringstream &)> dec) { |
| 216 |
auto enc = getDescBits(off, len); |
| 217 |
std::stringstream ss; |
| 218 |
dec(enc, ss); |
| 219 |
addField(fieldName, off, len, enc, ss.str()); |
| 220 |
} |
| 221 |
bool decodeExpected(int off, int len, const char *fieldName, |
| 222 |
uint32_t expected) { |
| 223 |
auto val = getDescBits(off, len); |
| 224 |
if (val != expected) { |
| 225 |
warning(off, len, "field should be ", expected); |
| 226 |
} |
| 227 |
addField(fieldName, off, len, (uint32_t)val, ""); |
| 228 |
return val == expected; |
| 229 |
} |
| 230 |
}; // MessageDecoderE64 |
| 231 |
|
| 232 |
// MessageDecoderLscE64.cpp |
| 233 |
void DecodeMessageLscE64(Platform _platform, SFID _sfid, ExecSize _execSize, |
| 234 |
int src0Len, int src1Len, uint64_t _desc, RegRef id0, |
| 235 |
RegRef id1, DecodeResult &_result); |
| 236 |
// MessageDecoderSamplerE64.cpp |
| 237 |
void DecodeMessageSamplerE64(Platform _platform, SFID _sfid, ExecSize _execSize, |
| 238 |
int src0Len, int src1Len, uint64_t _desc, |
| 239 |
RegRef id0, RegRef id1, DecodeResult &_result); |
| 240 |
void DecodeMessageOtherE64(Platform _platform, SFID _sfid, ExecSize _execSize, |
| 241 |
int src0Len, int src1Len, uint64_t _desc, |
| 242 |
RegRef id0, RegRef id1, DecodeResult &_result); |
| 243 |
} // namespace iga |