Debian Codegraph @

visa/iga/IGALibrary/api/igaEncoderWrapper.cpp

raw
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 // IGA headers
10 #include "igaEncoderWrapper.hpp"
11 #include "../Backend/GED/Encoder.hpp"
12
13 using namespace iga;
14
15 iga_status_t KernelEncoder::encode(std::ostream &errStr) {
16
17 ErrorHandler errHandler;
18 EncoderOpts enc_opt(m_autoCompact, true);
19 enc_opt.autoDepSet = m_enableAutoDeps;
20 enc_opt.swsbEncodeMode = m_swsbEncodeMode;
21
22 Encoder enc(m_kernel->getModel(), errHandler, enc_opt);
23 enc.encodeKernel(*m_kernel, m_kernel->getMemManager(), m_buf, m_binarySize);
24 #ifdef _DEBUG
25 if (errHandler.hasErrors()) {
26 // failed encode
27 for (const auto &e : errHandler.getErrors()) {
28 errStr << "vISA inst $" << e.at.offset << ": " << e.message << "\n";
29 }
30 return IGA_ERROR;
31 }
32 if (errHandler.hasWarnings()) {
33 for (const auto &w : errHandler.getWarnings()) {
34 std::cerr << "line " << w.at.line << ", col " << w.at.col << ": "
35 << w.message << "\n";
36 }
37 // fallthrough to IGA_SUCCESS
38 }
39 #endif // _DEBUG
40 return IGA_SUCCESS;
41 }
42
43 bool KernelEncoder::patchImmValue(const Model &model, unsigned char *binary,
44 Type type, const ImmVal &val) {
45 // check if the first instruction is compacted and get the instruction length
46 // FIXME: compact bit extract code copy from
47 // Decoder::getBitField(COMPACTION_CONTROL, 1)
48 const uint32_t *inst_start = (const uint32_t *)binary;
49 uint32_t mask = (1 << (uint32_t)1) - 1;
50 int32_t inst_len =
51 ((inst_start[COMPACTION_CONTROL / 32] >> (COMPACTION_CONTROL % 32)) &
52 mask) != 0
53 ? COMPACTED_SIZE
54 : UNCOMPACTED_SIZE;
55
56 // decode the instruction to ged_inst_t
57 ged_ins_t ged_inst;
58 memset(&ged_inst, 0, sizeof(ged_inst));
59 const GED_MODEL &ged_model = lowerPlatform(model.platform);
60 GED_RETURN_VALUE status =
61 GED_DecodeIns(ged_model, binary, (uint32_t)inst_len, &ged_inst);
62 assert(status == GED_RETURN_VALUE_SUCCESS);
63 if (status != GED_RETURN_VALUE_SUCCESS)
64 return false;
65
66 // set the imm value
67 status = GED_SetImm(&ged_inst, Encoder::typeConvesionHelper(val, type));
68 assert(status == GED_RETURN_VALUE_SUCCESS);
69 if (status != GED_RETURN_VALUE_SUCCESS)
70 return false;
71
72 // encode the instruction and write back to binary,
73 // if the instruction is compacted, then it must still be compacted now
74 if (inst_len == COMPACTED_SIZE)
75 status = GED_EncodeIns(&ged_inst, GED_INS_TYPE_COMPACT, binary);
76 else
77 status = GED_EncodeIns(&ged_inst, GED_INS_TYPE_NATIVE, binary);
78 assert(status == GED_RETURN_VALUE_SUCCESS);
79 if (status != GED_RETURN_VALUE_SUCCESS)
80 return false;
81
82 return true;
83 }