Debian Codegraph @

visa/iga/IGALibrary/IR/Kernel.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 #include "Kernel.hpp"
10 #include "../IR/Messages.hpp"
11
12 #include <set>
13
14 using namespace iga;
15
16 Kernel::Kernel(const Model &model) : m_model(model), m_mem(4096) {}
17
18 Kernel::~Kernel() {
19 // Since in a kernel blocks are allocated using the memory pool,
20 // when the Kernel is freed, the memory pool is deleted and destructors
21 // for Blocks are never called. This means the InstList's memory pools
22 // was never deleted and we need to do it here.
23 //
24 // Also, Instruction's fields with destructors need running
25 // (e.g. m_comment)
26 for (Block *bb : m_blocks) {
27 bb->~Block();
28 }
29 }
30
31 void Kernel::resetIds() {
32 // algorithm to set the IDs
33 int blockIndex = 0, instIndex = 0;
34 for (Block *b : getBlockList()) {
35 b->setID(blockIndex);
36 blockIndex++;
37 for (Instruction *i : b->getInstList()) {
38 i->setID(instIndex);
39 instIndex++;
40 }
41 }
42 }
43
44 size_t Kernel::getInstructionCount() const {
45 size_t n = 0;
46 for (const Block *b : getBlockList()) {
47 n += b->getInstList().size();
48 for (Instruction *i : b->getInstList()) {
49 if (i->hasInstOpt(InstOpt::CACHELINEALIGN)) {
50 n += 2;
51 }
52 }
53 }
54 return n;
55 }
56
57 Block *Kernel::createBlock() { return new (&m_mem) Block(); }
58
59 void Kernel::appendBlock(Block *blk) { m_blocks.push_back(blk); }
60
61 Instruction *
62 Kernel::createBasicInstruction(const OpSpec &os, const Predication &predOpnd,
63 const RegRef &freg, ExecSize execSize,
64 ChannelOffset chOff, MaskCtrl mc,
65 FlagModifier condMod, Subfunction sf) {
66 Instruction *inst = new (&m_mem) Instruction(os, execSize, chOff, mc);
67 inst->setSubfunction(sf);
68
69 inst->setPredication(predOpnd);
70 inst->setFlagModifier(condMod);
71 inst->setFlagReg(freg);
72
73 return inst;
74 }
75
76 Instruction *Kernel::createBranchInstruction(
77 const OpSpec &os, const Predication &predOpnd, const RegRef &flagReg,
78 ExecSize execSize, ChannelOffset chOff, MaskCtrl ectr, Subfunction sf) {
79 Instruction *inst = new (&m_mem) Instruction(os, execSize, chOff, ectr);
80 if (os.supportsSubfunction())
81 inst->setSubfunction(sf); // only branch control for now
82
83 inst->setPredication(predOpnd);
84 inst->setFlagReg(flagReg);
85
86 return inst;
87 }
88
89 Instruction *Kernel::createSendInstruction(const OpSpec &op, SFID sfid,
90 const Predication &predOpnd,
91 const RegRef &flagReg,
92 ExecSize execSize,
93 ChannelOffset chOff, MaskCtrl ectr,
94 uint32_t exDescImm,
95 const SendDesc &exDesc,
96 const SendDesc &desc) {
97 Instruction *inst = new (&m_mem) Instruction(op, execSize, chOff, ectr);
98 inst->setSubfunction(sfid);
99
100 inst->setPredication(predOpnd);
101 inst->setFlagReg(flagReg);
102
103 inst->setExtImmOffDesc(exDescImm);
104 inst->setExtMsgDesc(exDesc);
105 inst->setMsgDesc(desc);
106
107 ///////////////////////////////////////////////////////////////////////////
108 // make a best effort to set payload lengths
109 int dstLen = -1, src0Len = -1, src1Len = -1;
110 //
111 Platform p = inst->getOpSpec().platform;
112 bool immDescsHaveLens = desc.isImm();
113 if (immDescsHaveLens) {
114 dstLen = (int)((desc.imm >> 20) & 0x1F);
115 } else if (desc.isImm()) {
116 // if we at least have the SFID and an immediate descriptor
117 // try and deduce it from descriptor details we make a
118 // best-effort for exDesc, but may not need it specifically
119 PayloadLengths lens(p, sfid, inst->getExecSize(), desc.imm);
120 dstLen = lens.dstLen;
121 }
122 inst->setDstLength(dstLen);
123 //
124 if (src0Len < 0 && immDescsHaveLens) {
125 src0Len = (int)((desc.imm >> 25) & 0xF);
126 }
127 inst->setSrc0Length(src0Len);
128 //
129 if (src1Len < 0) {
130 // copy Src1.Length if no one else set it
131 bool immExDescHasSrc1Len = exDesc.isImm();
132 // ExBSO case will be handled by decoder/parser
133 //
134 // Src1.Length is imm xlen ExDesc[10:6] when ...:
135 // - ICL/TGL/XeHP: IsImm()
136 // - XeHPG+: never (always EU[96:92] for imm descs)
137 immExDescHasSrc1Len &= p < Platform::XE_HPG;
138 if (immExDescHasSrc1Len) {
139 src1Len = (exDesc.imm >> 6) & 0x1F;
140 }
141 }
142 inst->setSrc1Length(src1Len);
143
144 return inst;
145 }
146
147 Instruction *Kernel::createSendgInstruction(
148 const OpSpec &op, SFID sfid, const Predication &predOpnd,
149 const RegRef &flagReg, ExecSize execSize, ChannelOffset chOff,
150 MaskCtrl ectr, int src0Len, int src1Len, RegRef id0Reg, RegRef id1Reg,
151 uint64_t desc) {
152 Instruction *inst = new (&m_mem) Instruction(op, execSize, chOff, ectr);
153 inst->setSubfunction(sfid);
154
155 inst->setPredication(predOpnd);
156 inst->setFlagReg(flagReg);
157 IGA_ASSERT(src0Len >= 0, "Src0.Len is explicit on this platform");
158 inst->setSrc0Length(src0Len);
159 IGA_ASSERT(src1Len >= 0, "Src1.Len is explicit on this platform");
160 inst->setSrc1Length(src1Len); // use 0 for sendg or null src1
161
162 inst->setSendgIndDesc0(id0Reg);
163 inst->setSendgIndDesc1(id1Reg);
164 inst->setSendgDesc(desc);
165
166 return inst;
167 }
168 Instruction* Kernel::createThryldInstruction() {
169 Instruction *inst =
170 new (&m_mem) Instruction(m_model.lookupOpSpec(Op::THRYLD), ExecSize::SIMD1,
171 ChannelOffset::M0, MaskCtrl::NORMAL);
172 const Predication predOpnd;
173 inst->setPredication(predOpnd);
174 inst->setFlagModifier(FlagModifier::NONE);
175 inst->setFlagReg(REGREF_ZERO_ZERO);
176
177 return inst;
178 }
179
180 Instruction *Kernel::createNopInstruction() {
181 Instruction *inst =
182 new (&m_mem) Instruction(m_model.lookupOpSpec(Op::NOP), ExecSize::SIMD1,
183 ChannelOffset::M0, MaskCtrl::NORMAL);
184
185 const Predication predOpnd;
186 inst->setPredication(predOpnd);
187 inst->setFlagModifier(FlagModifier::NONE);
188 inst->setFlagReg(REGREF_ZERO_ZERO);
189
190 return inst;
191 }
192
193 Instruction *Kernel::createInlineBinaryInstruction(
194 const Instruction::InlineBinaryType &binary) {
195 Instruction *inst = createIllegalInstruction();
196 inst->setInlineBinary(binary);
197 return inst;
198 }
199
200 Instruction *Kernel::createIllegalInstruction() {
201 Instruction *inst = new (&m_mem)
202 Instruction(m_model.lookupOpSpec(Op::ILLEGAL), ExecSize::SIMD1,
203 ChannelOffset::M0, MaskCtrl::NORMAL);
204
205 const Predication predOpnd;
206 inst->setPredication(predOpnd);
207 inst->setFlagModifier(FlagModifier::NONE);
208 inst->setFlagReg(REGREF_ZERO_ZERO);
209
210 return inst;
211 }
212
213 static Instruction *createSyncInstruction(SWSB &sw, const OpSpec &ops,
214 SyncFC sf, MemManager &mem) {
215 Instruction *inst = new (&mem)
216 Instruction(ops, ExecSize::SIMD1, ChannelOffset::M0, MaskCtrl::NOMASK);
217 inst->setSubfunction(sf);
218
219 const Predication predOpnd;
220 inst->setPredication(predOpnd);
221 inst->setFlagModifier(FlagModifier::NONE);
222 inst->setFlagReg(REGREF_ZERO_ZERO);
223 inst->setSWSB(sw);
224 inst->setSource(SourceIndex::SRC0, Operand::SRC_REG_NULL_UD);
225
226 return inst;
227 }
228 Instruction *Kernel::createSyncNopInstruction(SWSB sw) {
229 return createSyncInstruction(sw, m_model.lookupOpSpec(Op::SYNC), SyncFC::NOP,
230 m_mem);
231 }
232 Instruction *Kernel::createSyncAllRdInstruction(SWSB sw) {
233 return createSyncInstruction(sw, m_model.lookupOpSpec(Op::SYNC),
234 SyncFC::ALLRD, m_mem);
235 }
236 Instruction *Kernel::createSyncAllWrInstruction(SWSB sw) {
237 return createSyncInstruction(sw, m_model.lookupOpSpec(Op::SYNC),
238 SyncFC::ALLWR, m_mem);
239 }