| 1 |
/*========================== begin_copyright_notice ============================ |
| 2 |
|
| 3 |
Copyright (C) 2017-2021 Intel Corporation |
| 4 |
|
| 5 |
SPDX-License-Identifier: MIT |
| 6 |
|
| 7 |
============================= end_copyright_notice ===========================*/ |
| 8 |
|
| 9 |
#ifndef IGA_BACKEND_NATIVE_FIELD_HPP |
| 10 |
#define IGA_BACKEND_NATIVE_FIELD_HPP |
| 11 |
|
| 12 |
#include "../../api/iga_bxml_ops.hpp" |
| 13 |
#include "../../asserts.hpp" |
| 14 |
#include "../../bits.hpp" |
| 15 |
#include "../../strings.hpp" |
| 16 |
|
| 17 |
#include <cstdint> |
| 18 |
#include <cstring> |
| 19 |
#include <iomanip> |
| 20 |
#include <ostream> |
| 21 |
#include <string> |
| 22 |
|
| 23 |
#if (__cplusplus >= 201402L) || (defined(_MSC_VER) && (_MSVC_LANG >= 201402L)) |
| 24 |
// We assume we are at least C++11, but here we're trying to ensure we |
| 25 |
// are C++14 or better. Various constexpr functions below will then be able |
| 26 |
// to opt-in based if our compiler is C++14 or better. C++11 allows us to |
| 27 |
// return constexpr expressions only, but C++14 allows more statements. |
| 28 |
// |
| 29 |
// MSVS defines __cplusplus as 199711L, but they do use _MSVC_LANG in those |
| 30 |
// release. Maybe there's a better way. |
| 31 |
#define CONSTEXPR_IF_CPP14 constexpr |
| 32 |
#else |
| 33 |
#define CONSTEXPR_IF_CPP14 |
| 34 |
#endif |
| 35 |
|
| 36 |
namespace iga { |
| 37 |
#define INVALID_FIELD Field() |
| 38 |
|
| 39 |
// Defines a fragment of a field. A field consists of one or |
| 40 |
// more fragments. See the struct below for more definition. |
| 41 |
struct Fragment { |
| 42 |
static const int NO_OFFSET = -1; |
| 43 |
|
| 44 |
enum class Kind { |
| 45 |
INVALID = 0, |
| 46 |
ENCODED, |
| 47 |
ZERO_FILL, // takes no space in encodings |
| 48 |
ZERO_WIRES, // takes up encoding space, but must be zero |
| 49 |
// ONES_FILL (only enable if needed) |
| 50 |
// other more complicated functions (e.g. clone) |
| 51 |
} kind = Kind::ENCODED; |
| 52 |
const char *name; // e.g. "Dst.Subreg[4:3]" |
| 53 |
int offset; // offset in the instruction |
| 54 |
int length; // number of bits in this fragment |
| 55 |
|
| 56 |
constexpr Fragment(const char *_name, int _offset, int _length, |
| 57 |
Kind _kind = Kind::ENCODED) |
| 58 |
: kind(_kind), name(_name), offset(_offset), length(_length) {} |
| 59 |
constexpr Fragment() : Fragment(nullptr, NO_OFFSET, 0, Kind::INVALID) {} |
| 60 |
|
| 61 |
// We could lower this to C++11 if we really tried. |
| 62 |
// (it's a pure boolean expression underneath) |
| 63 |
CONSTEXPR_IF_CPP14 bool overlaps(const Fragment &f) const { |
| 64 |
if (!isEncoded() || !f.isEncoded()) |
| 65 |
return false; // pseudo-fragments (unmapped) don't collide |
| 66 |
|
| 67 |
// arrange intervals as: smaller or equal, and larger |
| 68 |
const Fragment &smaller = length <= f.length ? *this : f; |
| 69 |
const Fragment &larger = length <= f.length ? f : *this; |
| 70 |
int sLo = smaller.offset, sHi = smaller.offset + smaller.length - 1; |
| 71 |
int lLo = larger.offset, lHi = larger.offset + larger.length - 1; |
| 72 |
// determine if the ends of smaller fit in larger |
| 73 |
return (sLo >= lLo && sLo <= lHi) || (sHi >= lLo && sHi <= lHi); |
| 74 |
} |
| 75 |
|
| 76 |
constexpr bool isEncoded() const { return kind == Kind::ENCODED; } |
| 77 |
constexpr bool isZeroFill() const { return kind == Kind::ZERO_FILL; } |
| 78 |
constexpr bool isZeroWires() const { return kind == Kind::ZERO_WIRES; } |
| 79 |
constexpr bool isInvalid() const { return kind == Kind::INVALID; } |
| 80 |
|
| 81 |
constexpr uint64_t getMask() const { |
| 82 |
return getFieldMaskUnshifted<uint64_t>(length); |
| 83 |
} |
| 84 |
}; |
| 85 |
|
| 86 |
// A field represents a set of bits encoded in an instruction that a |
| 87 |
// processor uses. E.g. the opcode, a register number, .... |
| 88 |
// |
| 89 |
// Typically, fields are contiguous, but some are fragmented in |
| 90 |
// non-contiguous parts in the instruction. Moreover, some of these |
| 91 |
// fragments aren't even encoded, but are intrinically defined |
| 92 |
// (implicitly defined). |
| 93 |
// |
| 94 |
// One example of a fragmented field is ExDesc in the send instruction. |
| 95 |
// which has fragments scattered all over the 128b encoding. |
| 96 |
// |
| 97 |
// Another pattern is intrinsic fragments. That is, a field has bits |
| 98 |
// that are not encoding in the instruction, but are implicitly some |
| 99 |
// value (usually 0). The .kind field will be set to KIND::ZERO_FILL, |
| 100 |
// for example. |
| 101 |
// |
| 102 |
// E.g. in TGL a ternary dst register encodes Dst.Subreg[4:3]. |
| 103 |
// Dst.Subreg[2:0] don't get encoded in the instruction and are |
| 104 |
// implicitly defined as zeros. This is logically the same as |
| 105 |
// decoding the two bits containing Dst.Subreg[4:3] and |
| 106 |
// shifting it left 3 bits. In this scheme we'd define this as |
| 107 |
// A field consistening of an ENCODED fragment for [4:3] and a |
| 108 |
// |
| 109 |
// |
| 110 |
struct Field { |
| 111 |
static const int MAX_FRAGMENTS_PER_FIELD = 4; |
| 112 |
|
| 113 |
// the field name; e.g. "Dst.Reg" |
| 114 |
const char *name; |
| 115 |
|
| 116 |
// in order of low to high |
| 117 |
Fragment fragments[MAX_FRAGMENTS_PER_FIELD]; |
| 118 |
|
| 119 |
// an undefined field (zero fragments) |
| 120 |
constexpr Field() : name(nullptr) {} |
| 121 |
// a simple encoded field (single contiguous) |
| 122 |
constexpr Field(const char *_name, int offset, int length) |
| 123 |
: name(_name), fragments{Fragment(_name, offset, length)} {} |
| 124 |
// a zero fill field or must-be-zero field |
| 125 |
constexpr Field(const char *_name, int length, |
| 126 |
Fragment::Kind kind = Fragment::Kind::ZERO_FILL) |
| 127 |
: name(_name), fragments{ |
| 128 |
Fragment(_name, Fragment::NO_OFFSET, length, kind)} {} |
| 129 |
// a field with two fragments |
| 130 |
constexpr Field(const char *_name, const char *name0, int offset0, |
| 131 |
int length0, Fragment::Kind kind0, const char *name1, |
| 132 |
int offset1, int length1, Fragment::Kind kind1) |
| 133 |
: name(_name), fragments{Fragment(name0, offset0, length0, kind0), |
| 134 |
Fragment(name1, offset1, length1, kind1)} {} |
| 135 |
// a field with three fragments |
| 136 |
constexpr Field(const char *_name, const char *name0, int offset0, |
| 137 |
int length0, Fragment::Kind kind0, const char *name1, |
| 138 |
int offset1, int length1, Fragment::Kind kind1, |
| 139 |
const char *name2, int offset2, int length2, |
| 140 |
Fragment::Kind kind2) |
| 141 |
: name(_name), fragments{ |
| 142 |
Fragment(name0, offset0, length0, kind0), |
| 143 |
Fragment(name1, offset1, length1, kind1), |
| 144 |
Fragment(name2, offset2, length2, kind2), |
| 145 |
} {} |
| 146 |
// a field with two encoded fragments |
| 147 |
constexpr Field(const char *_name, const char *name0, int offset0, |
| 148 |
int length0, const char *name1, int offset1, int length1) |
| 149 |
: Field(_name, name0, offset0, length0, Fragment::Kind::ENCODED, name1, |
| 150 |
offset1, length1, Fragment::Kind::ENCODED) {} |
| 151 |
// a field with three encoded fragments |
| 152 |
constexpr Field(const char *_name, const char *name0, int offset0, |
| 153 |
int length0, const char *name1, int offset1, int length1, |
| 154 |
const char *name2, int offset2, int length2) |
| 155 |
: Field(_name, name0, offset0, length0, Fragment::Kind::ENCODED, name1, |
| 156 |
offset1, length1, Fragment::Kind::ENCODED, name2, offset2, |
| 157 |
length2, Fragment::Kind::ENCODED) {} |
| 158 |
// a field a fragment of intrinsically defined 0's and top bits encoded |
| 159 |
constexpr Field(const char *_name, const char *name0, int shift, |
| 160 |
const char *name1, int offset1, int length1) |
| 161 |
: Field(_name, name0, Fragment::NO_OFFSET, shift, |
| 162 |
Fragment::Kind::ZERO_FILL, name1, offset1, length1, |
| 163 |
Fragment::Kind::ENCODED) {} |
| 164 |
|
| 165 |
// returns the field length |
| 166 |
CONSTEXPR_IF_CPP14 int length() const { return decodedLength(); } |
| 167 |
|
| 168 |
// returns the decoded length of the entire field |
| 169 |
CONSTEXPR_IF_CPP14 int decodedLength() const { |
| 170 |
int len = 0; |
| 171 |
for (const auto &fr : fragments) { |
| 172 |
if (fr.isInvalid()) |
| 173 |
break; |
| 174 |
len += fr.length; |
| 175 |
} |
| 176 |
return len; |
| 177 |
} |
| 178 |
|
| 179 |
// returns the decoded length of the entire field |
| 180 |
CONSTEXPR_IF_CPP14 int encodedLength() const { |
| 181 |
int len = 0; |
| 182 |
for (const auto &fr : fragments) { |
| 183 |
if (fr.isInvalid()) |
| 184 |
break; |
| 185 |
else if (fr.isEncoded() || fr.isZeroWires()) |
| 186 |
len += fr.length; |
| 187 |
} |
| 188 |
return len; |
| 189 |
} |
| 190 |
|
| 191 |
constexpr bool isAtomic() const { |
| 192 |
return fragments[1].isInvalid() && |
| 193 |
// if this is MBZ field |
| 194 |
(fragments[0].isEncoded() || fragments[0].isZeroFill() || |
| 195 |
fragments[0].isZeroWires()); |
| 196 |
} |
| 197 |
|
| 198 |
// returns a reference the first fragment |
| 199 |
// asserts that the fragment is atomic |
| 200 |
const Fragment &atomFragment() const { |
| 201 |
IGA_ASSERT(isAtomic(), "fragment is not atomic"); |
| 202 |
return fragments[0]; |
| 203 |
} |
| 204 |
|
| 205 |
// implicit conversion to fragment |
| 206 |
operator const Fragment &() const { return atomFragment(); } |
| 207 |
|
| 208 |
constexpr Field fragmentToField(int ix) const { |
| 209 |
return Field(fragments[ix].name, fragments[ix].offset, |
| 210 |
fragments[ix].length); |
| 211 |
} |
| 212 |
}; // Field |
| 213 |
|
| 214 |
static inline bool operator==(const Fragment &f1, const Fragment &f2) { |
| 215 |
return f1.kind == f2.kind && f1.offset == f2.offset && |
| 216 |
f1.length == f2.length && strcmp(f1.name, f2.name) == 0; |
| 217 |
} |
| 218 |
static inline bool operator<(const Fragment &f1, const Fragment &f2) { |
| 219 |
if (f1.kind != f2.kind) { |
| 220 |
return f1.kind < f2.kind; |
| 221 |
} else if (f1.offset != f2.offset) { |
| 222 |
return f1.offset < f2.offset; |
| 223 |
} else if (f1.length != f2.length) { |
| 224 |
return f1.length < f2.length; |
| 225 |
} else { |
| 226 |
return strcmp(f1.name, f2.name) < 0; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
////////////////////////////////////////////////////////////////////////// |
| 231 |
// enables us to group various operands together |
| 232 |
struct OperandFields { |
| 233 |
const char *name; |
| 234 |
const Field &fTYPE; |
| 235 |
const Field *pfSRCMODS; |
| 236 |
const Field &fREGFILE; |
| 237 |
|
| 238 |
const Field &fREG; |
| 239 |
const Field &fSUBREG; |
| 240 |
const Field &fSPCACC; |
| 241 |
|
| 242 |
const Field *pfRGNVT; |
| 243 |
const Field *pfRGNWI; |
| 244 |
const Field &fRGNHZ; |
| 245 |
|
| 246 |
const Field *pfADDRMODE; |
| 247 |
const Field *pfADDRREG; |
| 248 |
const Field *pfADDROFF; |
| 249 |
|
| 250 |
const Field *pfSRCISIMM; |
| 251 |
const Field *pfSRCIMM32L; // also IMM16 |
| 252 |
const Field *pfSRCIMM32H; |
| 253 |
}; |
| 254 |
|
| 255 |
enum OpIx { |
| 256 |
IX_DST = 0x40, // must not overlap with other bits |
| 257 |
IX_SRC0 = 0x10, |
| 258 |
IX_SRC1 = 0x20, |
| 259 |
IX_SRC2 = 0x30, |
| 260 |
OP_IX_TYPE_MASK = 0xF0, |
| 261 |
|
| 262 |
OP_IX_MASK = 0x0F, // masks table index (unique across bas. and trn.) |
| 263 |
|
| 264 |
BAS = 0x100, |
| 265 |
BAS_DST = BAS | IX_DST | 0, |
| 266 |
BAS_SRC0 = BAS | IX_SRC0 | 1, |
| 267 |
BAS_SRC1 = BAS | IX_SRC1 | 2, |
| 268 |
|
| 269 |
TER = 0x200, |
| 270 |
TER_DST = TER | IX_DST | 3, |
| 271 |
TER_SRC0 = TER | IX_SRC0 | 4, |
| 272 |
TER_SRC1 = TER | IX_SRC1 | 5, |
| 273 |
TER_SRC2 = TER | IX_SRC2 | 6 |
| 274 |
}; |
| 275 |
static inline bool IsTernary(OpIx IX) { return (IX & OpIx::TER) == OpIx::TER; } |
| 276 |
static inline bool IsDst(OpIx IX) { |
| 277 |
return (IX & OpIx::IX_DST) == OpIx::IX_DST; |
| 278 |
} |
| 279 |
static inline int ToSrcIndex(OpIx IX) { |
| 280 |
IGA_ASSERT(!IsDst(IX), "ToSrcIndex(OpIx) on dst index"); |
| 281 |
return ((IX & 0x30) >> 4) - 1; |
| 282 |
} |
| 283 |
static inline int ToFieldOperandArrayIndex(OpIx IX) { |
| 284 |
return (IX & OpIx::OP_IX_MASK); |
| 285 |
} |
| 286 |
static inline std::string ToStringOpIx(OpIx ix) { |
| 287 |
switch (ix & OpIx::OP_IX_TYPE_MASK) { |
| 288 |
case OpIx::IX_DST: |
| 289 |
return "dst"; |
| 290 |
case OpIx::IX_SRC0: |
| 291 |
return "src0"; |
| 292 |
case OpIx::IX_SRC1: |
| 293 |
return "src1"; |
| 294 |
case OpIx::IX_SRC2: |
| 295 |
return "src2"; |
| 296 |
default: |
| 297 |
return "?"; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
// a grouping of compaction information |
| 302 |
struct CompactionMapping { |
| 303 |
const Field &index; |
| 304 |
const uint64_t *values; |
| 305 |
size_t numValues; |
| 306 |
const Field **mappings; |
| 307 |
size_t numMappings; |
| 308 |
const char **meanings; // length is numValues |
| 309 |
// this is used for "what would this compaction word mean" |
| 310 |
std::string (*format)(Op, uint64_t); |
| 311 |
|
| 312 |
// sums up with width of all the fields mapped |
| 313 |
size_t countNumBitsMapped() const { |
| 314 |
size_t entrySizeBits = 0; |
| 315 |
for (size_t k = 0; k < numMappings; k++) |
| 316 |
entrySizeBits += mappings[k]->length(); |
| 317 |
return entrySizeBits; |
| 318 |
} |
| 319 |
bool isSrcImmField() const { return mappings == nullptr; } |
| 320 |
|
| 321 |
// emits output such as "0`001`1`0`001" |
| 322 |
// for SrcImm compacted fields it just emits the value |
| 323 |
void emitBinary(std::ostream &os, uint64_t val) const { |
| 324 |
if (mappings == nullptr) { |
| 325 |
// srcimm field |
| 326 |
os << std::setw(16) << "(" << fmtHex(val) << ")"; |
| 327 |
} else { |
| 328 |
int bitOff = (int)countNumBitsMapped(); |
| 329 |
for (int mIx = 0; mIx < (int)numMappings; ++mIx) { |
| 330 |
if (mIx != 0) { |
| 331 |
os << "`"; |
| 332 |
} |
| 333 |
const Field *mf = mappings[mIx]; |
| 334 |
bitOff -= mf->length(); |
| 335 |
auto bs = iga::getBits(val, bitOff, mf->length()); |
| 336 |
fmtBinaryDigits(os, bs, mf->length()); |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
}; |
| 341 |
|
| 342 |
// groups all CompactionMappings for a given platform |
| 343 |
struct CompactionScheme { |
| 344 |
const CompactionMapping &CTRLIX_2SRC; |
| 345 |
const CompactionMapping &DTIX_2SRC; |
| 346 |
const CompactionMapping &SRIX_2SRC; |
| 347 |
const CompactionMapping &SRC0IX_2SRC; |
| 348 |
const CompactionMapping &SRC1IX_2SRC; |
| 349 |
// |
| 350 |
const CompactionMapping &CTRLIX_3SRC; |
| 351 |
const CompactionMapping &SRCIX_3SRC; |
| 352 |
const CompactionMapping &SRIX_3SRC; |
| 353 |
}; |
| 354 |
|
| 355 |
// Assumes existence of: |
| 356 |
// constexpr static Field CMP_ ## SYM ## _ ## PLATFORM = ... |
| 357 |
// std::string [namespace::]Format_ ## SYM ## _ ## PLATFORM (Op, uint64_t) |
| 358 |
// const const Field SYM ## _MAPPINGS_ ## PLATFORM [M] {...} |
| 359 |
// const const uint64_t SYM ## _VALUES_ ## PLATFORM [N] {...} |
| 360 |
// const const const char * SYM ## _MEANINGS_ ## PLATFORM [N] {...} |
| 361 |
// |
| 362 |
// For example: |
| 363 |
// MAKE_COMPACTION_MAPPING(CTRLIX_2SRC, TGL); |
| 364 |
// |
| 365 |
// utilizes all of the following: |
| 366 |
// |
| 367 |
// constexpr static Field iga::xe::CMP_CTRLIX_2SRC_TGL {...} |
| 368 |
// std::string iga::xe::Format_CTRLIX_2SRC_TGL (Op, uint64_t) |
| 369 |
// const const Field iga::xe::CTRLIX_2SRC_MAPPINGS_TGL [M] {...} |
| 370 |
// const const uint64_t iga::xe::CTRLIX_2SRC_VALUES_TGL [N] {...} |
| 371 |
// const const const char * iga::xe::CTRLIX_2SRC_MEANINGS_TGL [N] {...} |
| 372 |
#define MAKE_COMPACTION_MAPPING(SYM, PLATFORM) \ |
| 373 |
extern std::string Format_##SYM##_##PLATFORM(Op, uint64_t); \ |
| 374 |
\ |
| 375 |
static_assert(sizeof(SYM##_VALUES_##PLATFORM) / \ |
| 376 |
sizeof(SYM##_VALUES_##PLATFORM[0]) == \ |
| 377 |
sizeof(SYM##_MEANINGS_##PLATFORM) / \ |
| 378 |
sizeof(SYM##_MEANINGS_##PLATFORM[0]), \ |
| 379 |
"mismatch in table sizes"); \ |
| 380 |
static const CompactionMapping CM_##SYM##_##PLATFORM { \ |
| 381 |
CMP_##SYM##_##PLATFORM, SYM##_VALUES_##PLATFORM, \ |
| 382 |
sizeof(SYM##_VALUES_##PLATFORM) / sizeof(SYM##_VALUES_##PLATFORM[0]), \ |
| 383 |
SYM##_MAPPINGS_##PLATFORM, \ |
| 384 |
sizeof(SYM##_MAPPINGS_##PLATFORM) / \ |
| 385 |
sizeof(SYM##_MAPPINGS_##PLATFORM[0]), \ |
| 386 |
SYM##_MEANINGS_##PLATFORM, &(Format_##SYM##_##PLATFORM), \ |
| 387 |
} |
| 388 |
} // namespace iga |
| 389 |
|
| 390 |
#endif /* IGA_BACKEND_NATIVE_FIELD_HPP */ |