Debian Codegraph @

visa/iga/IGALibrary/IR/Instruction.hpp

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 #ifndef _IGA_INSTRUCTION_HPP_
10 #define _IGA_INSTRUCTION_HPP_
11
12 // WARNING: the IR is subject to change without any notice. External tools
13 // should use the official interfaces in the external API. Those interfaces
14 // are tested between releases and maintained even with changes to the IR.
15
16 #include "../MemManager/MemManager.hpp"
17
18 #include "../EnumBitset.hpp"
19 #include "../Models/Models.hpp"
20 #include "../asserts.hpp"
21 #include "Loc.hpp"
22 #include "Operand.hpp"
23 #include "Types.hpp"
24
25 #include <string>
26
27 namespace iga {
28 enum class SourceIndex {
29 SRC0 = 0,
30 SRC1 = 1,
31 SRC2 = 2,
32 SRC3 = 3,
33 SRC4 = 4, // bdpas uses src3 and src4
34 };
35
36 // represents a GEN instruction
37 class Instruction {
38 public:
39 // inlineBinaryType - Inline binary value container
40 // inlineBinaryType[0]: bit[127:96]
41 // inlineBinaryType[1]: bit[95:64]
42 // inlineBinaryType[2]: bit[63:32]
43 // inlineBinaryType[3]: bit[31:0]
44 typedef std::array<uint32_t, 4> InlineBinaryType;
45
46 public:
47 Instruction(int id, PC pc, Loc loc, const OpSpec &os)
48 : Instruction(os, ExecSize::INVALID, ChannelOffset::M0,
49 MaskCtrl::NORMAL) {
50 setID(id);
51 setLoc(loc);
52 setPC(pc);
53 }
54
55 // TODO: phase this constructor out
56 Instruction(const OpSpec &os, ExecSize execSize, ChannelOffset chOff,
57 MaskCtrl mc)
58 : m_opSpec(os), m_maskCtrl(mc), m_execSize(execSize), m_chOff(chOff) {}
59 Instruction(const Instruction &) = delete;
60 Instruction& operator=(const Instruction&) = delete;
61
62 // for placement allocation
63 void operator delete(void *, MemManager *) {}
64 void *operator new(size_t sz, MemManager *m) { return m->alloc(sz); }
65
66 ///////////////////////////////////////////////////////////////////////
67 // operations that set instruction state
68 // (these are kept roughly in syntax order;
69 // e.g. predication before instruction options)
70 //
71 void setLoc(const Loc &loc) { m_instLoc = loc; }
72 void setPC(int32_t pc) { m_pc = pc; }
73 void setID(int id) { m_instId = id; }
74 void setMaskCtrl(MaskCtrl mc) { m_maskCtrl = mc; }
75 void setExecSize(ExecSize es) { m_execSize = es; }
76 void setChannelOffset(ChannelOffset co) { m_chOff = co; }
77 void setFlagModifier(FlagModifier flagModifier) {
78 m_flagModifier = flagModifier;
79 }
80 void setFlagReg(RegRef reg) { m_flagReg = reg; }
81 void setPredication(const Predication &predOpnd) { m_pred = predOpnd; }
82
83 void setSubfunction(Subfunction sf);
84
85 void setDirectDestination(DstModifier dstMod, RegName r, RegRef reg,
86 Region::Horz rgn, Type type);
87 void setMacroDestination(DstModifier dstMod, RegName r, RegRef reg,
88 MathMacroExt mme, Region::Horz rgn, Type type);
89 void setIndirectDestination(DstModifier dstMod, RegRef addrReg,
90 int16_t addrImmOff, Region::Horz rgnH,
91 Type type);
92 void setDirectSource(SourceIndex srcIx, SrcModifier srcMod, RegName rType,
93 RegRef reg, Region rgn, Type type);
94 void setIndirectSource(SourceIndex srcIx, SrcModifier srcMod,
95 RegName regName, RegRef reg, int16_t m_immOffset,
96 Region rgn, Type type);
97 void setMacroSource(SourceIndex srcIx, SrcModifier srcMod, RegName r,
98 RegRef reg, MathMacroExt acc, Region rgn, Type type);
99 void setImmediateSource(SourceIndex srcIx, const ImmVal &val, Type type);
100 void setLabelSource(SourceIndex srcIx, Block *jip, Type type = Type::INVALID);
101 void setLabelSource(SourceIndex srcIx, int32_t pc, Type type = Type::INVALID);
102 // e.g. pass Operand::NULL_UD_SRC
103 void setSource(SourceIndex srcIx, const Operand &op);
104
105 void setExtImmOffDesc(uint32_t imm);
106 void setExtMsgDesc(const SendDesc &msg);
107 void setMsgDesc(const SendDesc &msg);
108
109 void setSendgDesc(uint64_t imm);
110 void setSendgIndDesc0(RegRef rr);
111 void setSendgIndDesc1(RegRef rr);
112
113 void setDstLength(int dstLength) { m_sendDstLength = dstLength; }
114 void setSrc0Length(int src0Length) { m_sendSrc0Length = src0Length; }
115 void setSrc1Length(int src1Length) { m_sendSrc1Length = src1Length; }
116
117 void setSWSB(SWSB swsb) { m_depInfo = swsb; }
118
119 void addInstOpt(const InstOpt &opt) { m_instOpts.add(opt); }
120 void addInstOpts(const InstOptSet &opts) { m_instOpts.add(opts); }
121
122 // associates an optional comment with instruction
123 void setComment(const std::string& comment) { m_comment = comment; }
124
125 ///////////////////////////////////////////////////////////////////////
126 // name lacks get*** for consistency with other classes that all use
127 // just "platform()"
128 Platform platform() const { return m_opSpec.platform; }
129 const Model &model() const;
130
131 ///////////////////////////////////////////////////////////////////////
132 // operations that get instruction state
133 //
134 // the source or binary location
135 const Loc &getLoc() const { return m_instLoc; }
136 int getID() const { return m_instId; }
137 PC getPC() const { return m_pc; }
138
139 // returns the instruction op specification
140 const OpSpec &getOpSpec() const { return m_opSpec; }
141 // opcode, math function control, and branch control
142 Op getOp() const { return m_opSpec.op; }
143 bool is(Op op) const { return m_opSpec.is(op); }
144
145 // WrEn (NoMask)
146 MaskCtrl getMaskCtrl() const { return m_maskCtrl; }
147 // predication
148 bool hasPredication() const { return m_pred.function != PredCtrl::NONE; }
149 const Predication &getPredication() const { return m_pred; }
150
151 ///////////////////////////////////////////////////////////
152 // subfunction accessors
153 Subfunction getSubfunction() const { return m_sf; }
154 // specific subfunction accessors
155 // TODO: tantrum (assert) if they call one on the wrong op?
156 BranchCntrl getBranchCtrl() const { return m_sf.branch; }
157 MathFC getMathFc() const { return m_sf.math; }
158 SFID getSendFc() const { return m_sf.send; }
159 SyncFC getSyncFc() const { return m_sf.sync; }
160 BfnFC getBfnFc() const { return m_sf.bfn; }
161 DpasFC getDpasFc() const { return m_sf.dpas; }
162 ShuffleFC getShuffleFc() const { return m_sf.shuffle; }
163 LfsrFC getLfsrFc() const { return m_sf.lfsr; }
164 DnsclFC getDnsclFc() const { return m_sf.dnscl; }
165
166 // true for madm or math.invm and math.rsqrtm
167 bool isMacro() const;
168 // true if any of dst or src uses DF type
169 bool isDF() const;
170 // execution width info
171 ExecSize getExecSize() const { return m_execSize; }
172 ChannelOffset getChannelOffset() const { return m_chOff; }
173
174 // returns if the instruction has a flag (condition) modifier
175 bool hasFlagModifier() const { return m_flagModifier != FlagModifier::NONE; }
176 FlagModifier getFlagModifier() const { return m_flagModifier; }
177
178 // flag register is shared by predication and flag modfier
179 const RegRef &getFlagReg() const { return m_flagReg; }
180
181 const Operand &getDestination() const { return m_dst; }
182 Operand &getDestination() { return m_dst; }
183 const Operand &getSource(size_t srcNum) const { return m_srcs[srcNum]; }
184 Operand &getSource(size_t srcNum) { return m_srcs[srcNum]; }
185 const Operand &getSource(SourceIndex srcNum) const {
186 return m_srcs[(int)srcNum];
187 }
188 Operand &getSource(SourceIndex srcNum) { return m_srcs[(int)srcNum]; }
189
190 unsigned getSourceCount() const;
191
192 uint32_t getExtImmOffDescriptor() const;
193 SendDesc getExtMsgDescriptor() const;
194 SendDesc getMsgDescriptor() const;
195
196 uint64_t getSendgDesc() const;
197 RegRef getSendgIndDesc0Reg() const;
198 RegRef getSendgIndDesc1Reg() const;
199
200 // (For send messages) this returns the dst payload length in registers
201 // as encoded by the descriptors (if known).
202 int getDstLength() const { return m_sendDstLength; }
203 // (For send messages) this returns the src0 payload length in registers
204 // as encoded by the descriptors (if known).
205 int getSrc0Length() const { return m_sendSrc0Length; }
206 // (For send messages) this returns the src1 payload length in registers
207 // as encoded by the descriptors (if known).
208 int getSrc1Length() const { return m_sendSrc1Length; }
209
210 const InstOptSet &getInstOpts() const { return m_instOpts; }
211 bool hasInstOpt(InstOpt opt) const { return m_instOpts.contains(opt); }
212 void removeInstOpt(InstOpt opt) { m_instOpts.remove(opt); }
213
214 const Block *getJIP() const { return m_srcs[0].getTargetBlock(); }
215 const Block *getUIP() const { return m_srcs[1].getTargetBlock(); }
216 const std::string &getComment() const { return m_comment; }
217 SWSB getSWSB() const { return m_depInfo; }
218 bool isBranching() const { return getOpSpec().isBranching(); }
219
220 bool isMovWithLabel() const;
221 // check if an instruction is an indirect send
222 bool isGatherSend() const;
223
224 void setInlineBinary(const InlineBinaryType &binary);
225 bool isInlineBinaryInstruction() const { return m_isInlineBinaryInst; }
226 const InlineBinaryType &getInlineBinary() const { return m_inlineBinary; }
227
228 SWSB::InstType getSWSBInstType(SWSB_ENCODE_MODE mode) const;
229
230 void validate() const; // asserts on malformed IR
231 std::string str() const; // returns syntax of this instruction
232
233 private:
234 const OpSpec &m_opSpec; // information about the specific inst op
235 // SFID, e.g. MathFC::INV for math.inv, SFID::DC0 for send.dc0
236 Subfunction m_sf = InvalidFC::INVALID;
237
238 MaskCtrl m_maskCtrl; // (W) WrEn (write enable / NoMask)
239 Predication m_pred; // predication function (and logical sign)
240 RegRef m_flagReg = REGREF_ZERO_ZERO; // shared by m_pred and m_flagModifier
241
242 ExecSize m_execSize;
243 ChannelOffset m_chOff;
244 Operand m_dst;
245 // bdpas has 5 source operands
246 Operand m_srcs[5];
247
248 // conditional-modifier function
249 FlagModifier m_flagModifier = FlagModifier::NONE;
250
251 // Xe2: given an reg ExDesc, this holds the extra bits for the
252 // immediate offset part. In the case of an immediate ExDesc
253 // (below); this should be 0 (the immediate offset is part of m_desc
254 // as in earlier platforms for that case).
255 uint32_t m_exImmOffDesc = 0;
256 SendDesc m_exDesc;
257 SendDesc m_desc;
258
259 // replaces m_exDesc+m_desc with immediate-only field
260 uint64_t m_sendgDesc = 0x0;
261 RegRef m_sendgIndDesc0Reg = REGREF_INVALID; // REGREF_INVALID if not set
262 RegRef m_sendgIndDesc1Reg = REGREF_INVALID; // REGREF_INVALID if not set
263
264 ///////////////////////////////////////////////////////////////////////
265 // Regarding Src1.Length
266 //
267 // For XeHP to XeHPC, we encode Src1.Length and CPS
268 // sometimes we use the ExDesc a0.# reg for these fields and
269 // sometimes they are encoded in the EU ISA bits (as immediate)
270 //
271 // +-----------+------------+-------------------------------------------
272 // | Platform | ExDesc ... | Src1.Length/CPS
273 // +-----------+------------+-------------------------------------------
274 // | XE | ExDesc[10:6] always holds Src1.Length
275 // | | Imm. | EU[103:99,35] (this::imm[11:6])
276 // | | send ... (SRC1LEN<<6) ...
277 // | | Reg. | ExDesc[11:6] (this::reg a0.#[11:6])
278 // | | send ... a0.2 ...
279 // +-----------+------------+-------------------------------------------
280 // | XE_HP | Sampler bindless accesses need more surface offset bits;
281 // | | thus ExDesc[11:6] are now low surface offest bits.
282 // | | Thus in that case, Src1.Length [10:6] and CPS [11]
283 // | | must be placed in the EU encoding.
284 // | +------------------------------------------------
285 // | | Imm. | EU[103:99,35] (as this::imm[11:6])
286 // | | send ... src1 (SRC1LEN<<6) ...
287 // | +------------------------------------------------
288 // | | Reg. | Reg[11:6]
289 // | | send ... src1 a0.# ...
290 // | +------------------------------------------------
291 // | | Reg.+ExBSO | EU[103:99,35] (as this::imm[11:6])
292 // | | send ... src1:SRC1LEN a0.# ... {[CPS]}
293 // +-----------+------------+-------------------------------------------
294 // | XE_HPG | Src1.Length always comes from the EU ISA, regardless
295 // | | if a0.# is used.
296 // | | Imm. | EU[103:99,35] (as this::src1Length,this::cps)
297 // | | send ... src1:SRC1LEN imm ... {[CPS]}
298 // | | Reg.+ExBSO | EU[103:99,35] (as this::imm[11:6])
299 // | | send ... src1:SRC1LEN a0.# ... {[CPS]}
300 // +-----------+------------+-------------------------------------------
301 // | XE_HPC | Same as XE_HPG, but since the sampler is absent
302 // | | there won't be a use of ExBSO and CPS.
303 // +-----------+------------+-------------------------------------------
304 // | XE2 | Similar to XE_HPC/XE_HPG except
305 // | | CPS is gone (now ExDescImm[11])
306 // | | ExBSO is hardwired to 1, but the field persists.
307 // | | There is an ExDescImm available even with reg ExDesc
308 // +-----------+--------------------------------------------------------
309 // | XE3 Send | Same as Xe2 regarding descriptors
310 // +-----------+--------------------------------------------------------
311 // | XE3P Send | Same as Xe3 except supports Gather Send functionality.
312 // | | Thus this field overlaps with Src0.SubReg (s0 subreg).
313 // | XE3P Sendg | Descriptors are combined into single entity of around
314 // | new fmt | now 42b or 47b. Two new indirect descriptors are added
315 // | | "ID0" and "ID1" (indirect descriptor). These will
316 // | | optionally hold s0 QW registers (each is has a
317 // | | "is present" bit).
318 // | |
319 // +-----------+--------------------------------------------------------
320 //
321
322 // These fields are best-effort decodes of the message lengths
323 // E.g. if the fields are buried in an immediate send descriptor,
324 // extract them here. In some cases (e.g. reg descriptors), we can't
325 // help you. Oppose architects that propose instruction definitions
326 // that are taken form registers.
327 int m_sendDstLength = -1; // -1 if unknown
328 int m_sendSrc0Length = -1; // -1 if unknown
329 int m_sendSrc1Length = -1; // -1 if unknown
330
331 InstOptSet m_instOpts; // miscellaneous instruction attributes
332 SWSB m_depInfo;
333
334 // Binary for inline binary instruction.
335 InlineBinaryType m_inlineBinary = {0};
336 bool m_isInlineBinaryInst = false;
337
338 int m_instId = 0xFFFFFFFF; // unique id for this instruction
339 // (unique in the kernel)
340
341 int32_t m_pc = 0; // the encode/decode PC for this instruction
342
343 // source location info we keep this
344 // separate from the PC since we
345 // use both during assembly
346 Loc m_instLoc = Loc::INVALID;
347
348 // e.g. for illegal instruction extended info (e.g. decode errors)
349 // enables us to emit things like
350 // illegal {Compacted} // failed to uncompact ....
351 // ^^^^^^^^^^^^^^^^^^^
352 std::string m_comment;
353 }; // class Instruction
354 } // namespace iga
355 #endif //_IGA_INSTRUCTION_HPP_