| 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 |
#include "Interface.hpp" |
| 10 |
#include "../../strings.hpp" |
| 11 |
#include "../BitProcessor.hpp" |
| 12 |
#include "InstCompactor.hpp" |
| 13 |
#include "InstEncoder.hpp" |
| 14 |
|
| 15 |
#include <vector> |
| 16 |
|
| 17 |
using namespace iga; |
| 18 |
|
| 19 |
/////////////////////////////////////////////////////////////////////////// |
| 20 |
// |
| 21 |
// BTS (Big Theory Statement) |
| 22 |
// |
| 23 |
// In this scheme there are two encoders: |
| 24 |
// * a "parent" encoder, which is concerned with how we iterate over |
| 25 |
// the kernel and order instructions etc... Examples of this are a |
| 26 |
// a serial or a parallel encoder. E.g. SerialEncoder |
| 27 |
// |
| 28 |
// * A "child" or instruction encoder (InstEncoder), concerned with |
| 29 |
// encoding a single instruction at a time only. It is blissfully |
| 30 |
// unaware of any clever parallelism or other tricks. |
| 31 |
// |
| 32 |
// The general encoding algorithm consists of two phases: |
| 33 |
// |
| 34 |
// 1. Encoding. Initial encoding encodes most instruction fields |
| 35 |
// but may not be able to resolve backpatches since later instructions |
| 36 |
// may not have known sizes at the moment. |
| 37 |
// |
| 38 |
// 2. Backpatching. After all instructions have a determined size, |
| 39 |
// we iterate through all backpatches which required this information. |
| 40 |
// Typically, this just involves patching a jump field or two. |
| 41 |
// |
| 42 |
// The parent encoding algorithm in this file calls into |
| 43 |
// InstEncoder::encodeInstruction<P> |
| 44 |
// with whatever platform P that is being targeted. This class takes the |
| 45 |
// instruction and a pointer the output and returns the encoded size. |
| 46 |
// It may also register backpatches for something that needs resolution |
| 47 |
// later (labels). |
| 48 |
// |
| 49 |
// Specific encoders for various platform instances are instances of the |
| 50 |
// InstEncoder::encodeInstruction<P> method. Typically these will redirect |
| 51 |
// into lower-level modules with specific information about where fields are |
| 52 |
// located (e.g. see Native/PreG12 for older encodings). |
| 53 |
// |
| 54 |
/////////////////////////////////////////////////////////////////////////// |
| 55 |
static size_t encodeInst(InstEncoder &enc, const EncoderOpts &opts, |
| 56 |
int ix, // instruction's index in the output array |
| 57 |
Instruction *inst, MInst *bits) { |
| 58 |
bool mustCompact = inst->hasInstOpt(InstOpt::COMPACTED); |
| 59 |
bool mustntCompact = inst->hasInstOpt(InstOpt::NOCOMPACT); |
| 60 |
|
| 61 |
CompactionDebugInfo *cbdi = nullptr; |
| 62 |
CompactionDebugInfo mustCompactDebugInfo; |
| 63 |
if (mustCompact || (opts.autoCompact && !mustntCompact)) { |
| 64 |
// if {Compacted} is on we will raise a warning or error, we hope |
| 65 |
// to give them good info on why this failed |
| 66 |
cbdi = &mustCompactDebugInfo; |
| 67 |
} |
| 68 |
|
| 69 |
// encode native |
| 70 |
enc.encodeInstruction(ix, *inst, bits); |
| 71 |
|
| 72 |
if (mustCompact || (opts.autoCompact && !mustntCompact)) { |
| 73 |
// attempt compaction |
| 74 |
InstCompactor ic(enc, enc.getModel()); |
| 75 |
MathFC mfc = inst->is(Op::MATH) ? inst->getMathFc() : MathFC::INVALID; |
| 76 |
|
| 77 |
auto cr = ic.tryToCompact(&inst->getOpSpec(), mfc, *bits, bits, cbdi); |
| 78 |
switch (cr) { |
| 79 |
case CompactionResult::CR_MISS: |
| 80 |
case CompactionResult::CR_NO_FORMAT: { |
| 81 |
std::stringstream ss; |
| 82 |
ss << "unable to compact instruction with {Compact} option: "; |
| 83 |
if (cr == CompactionResult::CR_NO_FORMAT) { |
| 84 |
ss << "not a compactable format"; |
| 85 |
} else { |
| 86 |
// "foo, bar, and baz all miss" |
| 87 |
// "foo and bar miss" |
| 88 |
size_t len = cbdi->fieldMisses.size(); |
| 89 |
for (size_t i = 0; i < len; i++) { |
| 90 |
const CompactionMapping *cIx = cbdi->fieldMisses[i]; |
| 91 |
if (i > 0) { |
| 92 |
ss << ","; |
| 93 |
} |
| 94 |
if (i == len - 1 && len >= 3) { |
| 95 |
ss << " and "; |
| 96 |
} else { |
| 97 |
ss << " "; |
| 98 |
} |
| 99 |
uint64_t mappedValue = cbdi->fieldMapping[i]; |
| 100 |
ss << "for index " << cIx->index.name << " lacks"; |
| 101 |
if (cIx->format) { |
| 102 |
ss << " ("; |
| 103 |
ss << cIx->format(inst->getOp(), cbdi->fieldMapping[i]); |
| 104 |
ss << "): "; |
| 105 |
} |
| 106 |
ss << "0x" << std::hex << mappedValue << ": "; |
| 107 |
// convert the field mapping value to separated form |
| 108 |
// e.g. 00`0110`... |
| 109 |
// mapped fields are from high bits to low bits |
| 110 |
cIx->emitBinary(ss, mappedValue); |
| 111 |
} |
| 112 |
// TODO: could emit the closest mappings if there's only |
| 113 |
// a couple with a low enough hamming distance |
| 114 |
// (see -Xdcmp for that logic) |
| 115 |
} |
| 116 |
if (opts.explicitCompactMissIsWarning) { |
| 117 |
enc.warningAtT(inst->getLoc(), ss.str()); |
| 118 |
} else { |
| 119 |
enc.errorAtT(inst->getLoc(), ss.str()); |
| 120 |
} |
| 121 |
break; |
| 122 |
} |
| 123 |
default: |
| 124 |
break; // CR_SUCCESS or CR_NO_COMPACT |
| 125 |
} |
| 126 |
} // not trying to compact |
| 127 |
|
| 128 |
if (bits->isCompact()) { |
| 129 |
return 8; |
| 130 |
} else { |
| 131 |
return 16; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
struct SerialEncoder : BitProcessor { |
| 136 |
const EncoderOpts &opts; |
| 137 |
const Model &model; |
| 138 |
uint8_t *instBufBase = nullptr; |
| 139 |
int instBufTotalBytes = |
| 140 |
0; // valid number of bytes in the buffer to be returned |
| 141 |
|
| 142 |
InstEncoder instEncoder; |
| 143 |
std::vector<MInst *> |
| 144 |
encodedInsts; // pointers into where each instruction starts |
| 145 |
|
| 146 |
SerialEncoder(ErrorHandler &errHandler, const EncoderOpts &_opts, |
| 147 |
const Model &_model) |
| 148 |
: BitProcessor(errHandler), opts(_opts), model(_model), |
| 149 |
instEncoder(_opts, *this, _model) {} |
| 150 |
|
| 151 |
void resolveBackpatches() { |
| 152 |
// resolve backpatches |
| 153 |
for (auto &bp : instEncoder.getBackpatches()) { |
| 154 |
instEncoder.resolveBackpatch(bp, encodedInsts[bp.state.instIndex]); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
void encodeKernel(Kernel &k) { |
| 159 |
#ifndef IGA_DISABLE_ENCODER_EXCEPTIONS |
| 160 |
try { |
| 161 |
#endif |
| 162 |
instEncoder.getBackpatches().clear(); |
| 163 |
encodedInsts.clear(); |
| 164 |
encodedInsts.reserve(k.getInstructionCount()); |
| 165 |
|
| 166 |
// preallocate buffer to return |
| 167 |
size_t allocLen = k.getInstructionCount() * UNCOMPACTED_SIZE; |
| 168 |
if (allocLen == 0) // for empty kernel case, allocate something |
| 169 |
allocLen = sizeof(MInst); |
| 170 |
instBufBase = (uint8_t *)k.getMemManager().alloc(allocLen); |
| 171 |
if (!instBufBase) { |
| 172 |
fatalAtT(0, "failed to allocate memory for kernel binary"); |
| 173 |
return; |
| 174 |
} |
| 175 |
|
| 176 |
// walk through the instructions encoding each one at a time |
| 177 |
uint8_t *instBufCurr = instBufBase; |
| 178 |
int instIx = 0; |
| 179 |
for (auto blk : k.getBlockList()) { |
| 180 |
blk->setPC((PC)(instBufCurr - instBufBase)); |
| 181 |
for (auto i : blk->getInstList()) { |
| 182 |
encodedInsts.push_back((MInst *)instBufCurr); |
| 183 |
i->setPC((PC)(instBufCurr - instBufBase)); |
| 184 |
size_t iLen = |
| 185 |
encodeInst(instEncoder, opts, instIx++, i, (MInst *)instBufCurr); |
| 186 |
instBufCurr += iLen; |
| 187 |
} |
| 188 |
} |
| 189 |
instBufTotalBytes = (int)(instBufCurr - instBufBase); |
| 190 |
|
| 191 |
resolveBackpatches(); |
| 192 |
#ifndef IGA_DISABLE_ENCODER_EXCEPTIONS |
| 193 |
} catch (const iga::FatalError &) { |
| 194 |
// error is already reported |
| 195 |
} |
| 196 |
#endif |
| 197 |
} |
| 198 |
}; |
| 199 |
|
| 200 |
static void EncodeSerial(const Model &model, const EncoderOpts &opts, |
| 201 |
ErrorHandler &eh, Kernel &k, void *&bits, |
| 202 |
size_t &bitsLen) { |
| 203 |
SerialEncoder se(eh, opts, model); |
| 204 |
se.encodeKernel(k); |
| 205 |
if (!eh.hasErrors()) { |
| 206 |
bits = se.instBufBase; |
| 207 |
bitsLen = (size_t)se.instBufTotalBytes; |
| 208 |
} else { |
| 209 |
bits = nullptr; |
| 210 |
bitsLen = 0; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
#if 0 |
| 215 |
// index each instruction |
| 216 |
// keep a base pointer with the minimum compacted instruction |
| 217 |
// each checks to see if their block is the current minimum when committing |
| 218 |
// if so, copy out the block and bump that pointer |
| 219 |
|
| 220 |
static const size_t CHUNK_SIZE = 8; |
| 221 |
struct EncoderWorker |
| 222 |
{ |
| 223 |
const Model &model; |
| 224 |
const EncoderOpts &opts; |
| 225 |
|
| 226 |
std::vector<Backpatch> patches; |
| 227 |
volatile bool done = false; |
| 228 |
volatile bool &hasFatalError; |
| 229 |
|
| 230 |
std::vector<Instruction *> &instQueue; |
| 231 |
|
| 232 |
EncoderWorker( |
| 233 |
const Model &_model, |
| 234 |
const EncoderOpts &_opts, |
| 235 |
std::vector<Instruction *> &_instQueue, |
| 236 |
volatile bool &_hasFatalError |
| 237 |
) |
| 238 |
: model(_model) |
| 239 |
, opts(_opts) |
| 240 |
, instQueue(_instQueue) |
| 241 |
, hasFatalError(_hasFatalError) |
| 242 |
{ |
| 243 |
} |
| 244 |
EncoderWorker(const EncoderWorker&) = delete; |
| 245 |
|
| 246 |
void run() |
| 247 |
{ |
| 248 |
// continuously grab an instruction and run it |
| 249 |
std::vector<Instruction *> chunk; |
| 250 |
chunk.reserve(8); |
| 251 |
|
| 252 |
// shared condition variable for the lock so only one thread is |
| 253 |
// changing the lock at a time |
| 254 |
ErrorHandler eh; |
| 255 |
BitProcessor bp(eh); |
| 256 |
while (!done && |
| 257 |
eh.hasFatalError()) |
| 258 |
{ |
| 259 |
// lock |
| 260 |
if (!instQueue.empty()) { |
| 261 |
Instruction *inst = nullptr; |
| 262 |
{ |
| 263 |
inst = instQueue.back(); |
| 264 |
instQueue.pop_back(); |
| 265 |
} |
| 266 |
for (Instruction *inst : chunk) { |
| 267 |
encodeInst(inst, bp); |
| 268 |
} |
| 269 |
chunk.clear(); |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
void encodeInst(Instruction *i, BitProcessor &bp) |
| 274 |
{ |
| 275 |
bp.setCurrInst(i); |
| 276 |
// TODO: call into the serial algorithm |
| 277 |
} |
| 278 |
}; |
| 279 |
|
| 280 |
static void EncodeParallel( |
| 281 |
const Model &model, |
| 282 |
EncoderOpts &opts, |
| 283 |
ErrorHandler &eh, |
| 284 |
Kernel &k, |
| 285 |
void *&bits, |
| 286 |
int &bitsLen) |
| 287 |
{ |
| 288 |
std::vector<Instruction *> instQueue; |
| 289 |
instQueue.reserve(256); |
| 290 |
volatile bool fatalError; |
| 291 |
|
| 292 |
EncoderWorker worker(model, opts, instQueue, fatalError); |
| 293 |
#ifndef IGA_DISABLE_ENCODER_EXCEPTIONS |
| 294 |
try { |
| 295 |
#endif |
| 296 |
// would start the threads here |
| 297 |
for (auto blk : k.getBlockList()) { |
| 298 |
for (auto i : blk->getInstList()) { |
| 299 |
instQueue.emplace_back(i); |
| 300 |
} |
| 301 |
} |
| 302 |
#ifndef IGA_DISABLE_ENCODER_EXCEPTIONS |
| 303 |
} catch (const iga::FatalError&) { |
| 304 |
// error is already reported |
| 305 |
} |
| 306 |
// TODO: kill any threads |
| 307 |
#endif |
| 308 |
} |
| 309 |
#endif |
| 310 |
bool iga::native::IsEncodeSupported(const Model &m, const EncoderOpts &) { |
| 311 |
switch (m.platform) { |
| 312 |
case Platform::FUTURE: |
| 313 |
default: |
| 314 |
break; |
| 315 |
} |
| 316 |
return false; |
| 317 |
} |
| 318 |
|
| 319 |
void iga::native::Encode(const Model &model, const EncoderOpts &opts, |
| 320 |
ErrorHandler &eh, Kernel &k, void *&bits, |
| 321 |
size_t &bitsLen) { |
| 322 |
IGA_ASSERT(IsEncodeSupported(model, opts), |
| 323 |
"platform not supported; " |
| 324 |
"caller should have checked via iga::native::IsEncodeSupported"); |
| 325 |
// |
| 326 |
// EncodeParallel(model, opts, eh, k, bits, bitsLen); |
| 327 |
EncodeSerial(model, opts, eh, k, bits, bitsLen); |
| 328 |
} |
| 329 |
|
| 330 |
/////////////////////////////////////////////////////////////////////////////// |
| 331 |
// decoding interfaces |
| 332 |
bool iga::native::IsDecodeSupported(const Model &m, const DecoderOpts &) { |
| 333 |
switch (m.platform) { |
| 334 |
case Platform::FUTURE: |
| 335 |
break; |
| 336 |
default: |
| 337 |
break; |
| 338 |
} |
| 339 |
return false; |
| 340 |
} |
| 341 |
|
| 342 |
Kernel *iga::native::Decode(const Model &m, const DecoderOpts &dopts, |
| 343 |
ErrorHandler &eh, const void *bits, |
| 344 |
size_t bitsLen) { |
| 345 |
IGA_ASSERT(IsDecodeSupported(m, dopts), |
| 346 |
"invalid platform for decode; " |
| 347 |
"caller should have checked via iga::native::IsDecodeSupported"); |
| 348 |
eh.reportError(Loc(0), "feature unavailable"); |
| 349 |
return nullptr; |
| 350 |
} |
| 351 |
|
| 352 |
void iga::native::DecodeFields(Loc loc, const Model &m, const void *bits, |
| 353 |
FragmentList &fields, ErrorHandler &eh) { |
| 354 |
DecoderOpts dopts; |
| 355 |
IGA_ASSERT(IsDecodeSupported(m, dopts), |
| 356 |
"invalid platform for decode; " |
| 357 |
"caller should have checked via iga::native::IsDecodeSupported"); |
| 358 |
// |
| 359 |
eh.reportError(loc, "feature unavailable"); |
| 360 |
} |
| 361 |
|
| 362 |
CompactionResult iga::native::DebugCompaction(const Model &m, |
| 363 |
const void *inputBits, |
| 364 |
void *compactedOutput, // optional |
| 365 |
CompactionDebugInfo &info) { |
| 366 |
DecoderOpts dopts; |
| 367 |
IGA_ASSERT(IsDecodeSupported(m, dopts), |
| 368 |
"invalid platform for decode; " |
| 369 |
"caller should have checked via iga::native::IsDecodeSupported"); |
| 370 |
|
| 371 |
MInst *mi = (MInst *)inputBits; |
| 372 |
if (mi->isCompact()) { |
| 373 |
// already compact, no need to try and compact it |
| 374 |
// do copy it out though |
| 375 |
if (compactedOutput) { |
| 376 |
memcpy(compactedOutput, inputBits, 8); |
| 377 |
} |
| 378 |
return CompactionResult::CR_SUCCESS; |
| 379 |
} |
| 380 |
|
| 381 |
OpSpecMissInfo missInfo; |
| 382 |
const OpSpec &os = m.lookupOpSpecFromBits(inputBits, missInfo); |
| 383 |
if (!os.isValid()) { |
| 384 |
return CompactionResult::CR_NO_FORMAT; |
| 385 |
} |
| 386 |
ErrorHandler eh; |
| 387 |
BitProcessor bp(eh); |
| 388 |
InstCompactor ic(bp, m); |
| 389 |
MInst dummyOutput; |
| 390 |
MInst *outputBits = |
| 391 |
compactedOutput == nullptr ? &dummyOutput : (MInst *)compactedOutput; |
| 392 |
return ic.tryToCompact(&os, *mi, outputBits, &info); |
| 393 |
} |