2 * Copyright © 2018 Valve Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
34 #include "ac_binary.h"
35 #include "amd_family.h"
36 #include "aco_opcodes.h"
39 struct radv_nir_compiler_options
;
40 struct radv_shader_info
;
44 extern uint64_t debug_flags
;
48 DEBUG_VALIDATE_RA
= 0x2,
53 * Representation of the instruction's microcode encoding format
54 * Note: Some Vector ALU Formats can be combined, such that:
55 * - VOP2* | VOP3A represents a VOP2 instruction in VOP3A encoding
56 * - VOP2* | DPP represents a VOP2 instruction with data parallel primitive.
57 * - VOP2* | SDWA represents a VOP2 instruction with sub-dword addressing.
59 * (*) The same is applicable for VOP1 and VOPC instructions.
61 enum class Format
: std::uint16_t {
62 /* Pseudo Instruction Format */
64 /* Scalar ALU & Control Formats */
70 /* Scalar Memory Format */
74 /* Vector Memory Buffer Formats */
77 /* Vector Memory Image Format */
88 PSEUDO_REDUCTION
= 18,
90 /* Vector ALU Formats */
98 /* Vector Parameter Interpolation Format */
104 enum barrier_interaction
{
106 barrier_buffer
= 0x1,
108 barrier_atomic
= 0x4,
109 barrier_shared
= 0x8,
113 constexpr Format
asVOP3(Format format
) {
114 return (Format
) ((uint32_t) Format::VOP3
| (uint32_t) format
);
142 /* these are used for WWM and spills to vgpr */
143 v1_linear
= v1
| (1 << 6),
144 v2_linear
= v2
| (1 << 6),
147 RegClass() = default;
148 constexpr RegClass(RC rc
)
150 constexpr RegClass(RegType type
, unsigned size
)
151 : rc((RC
) ((type
== RegType::vgpr
? 1 << 5 : 0) | size
)) {}
153 constexpr operator RC() const { return rc
; }
154 explicit operator bool() = delete;
156 constexpr RegType
type() const { return rc
<= RC::s16
? RegType::sgpr
: RegType::vgpr
; }
157 constexpr unsigned size() const { return (unsigned) rc
& 0x1F; }
158 constexpr bool is_linear() const { return rc
<= RC::s16
|| rc
& (1 << 6); }
159 constexpr RegClass
as_linear() const { return RegClass((RC
) (rc
| (1 << 6))); }
165 /* transitional helper expressions */
166 static constexpr RegClass s1
{RegClass::s1
};
167 static constexpr RegClass s2
{RegClass::s2
};
168 static constexpr RegClass s3
{RegClass::s3
};
169 static constexpr RegClass s4
{RegClass::s4
};
170 static constexpr RegClass s8
{RegClass::s8
};
171 static constexpr RegClass s16
{RegClass::s16
};
172 static constexpr RegClass v1
{RegClass::v1
};
173 static constexpr RegClass v2
{RegClass::v2
};
174 static constexpr RegClass v3
{RegClass::v3
};
175 static constexpr RegClass v4
{RegClass::v4
};
176 static constexpr RegClass v5
{RegClass::v5
};
177 static constexpr RegClass v6
{RegClass::v6
};
178 static constexpr RegClass v7
{RegClass::v7
};
179 static constexpr RegClass v8
{RegClass::v8
};
183 * Each temporary virtual register has a
184 * register class (i.e. size and type)
189 constexpr Temp(uint32_t id
, RegClass cls
) noexcept
190 : id_(id
), reg_class(cls
) {}
192 constexpr uint32_t id() const noexcept
{ return id_
; }
193 constexpr RegClass
regClass() const noexcept
{ return reg_class
; }
195 constexpr unsigned size() const noexcept
{ return reg_class
.size(); }
196 constexpr RegType
type() const noexcept
{ return reg_class
.type(); }
197 constexpr bool is_linear() const noexcept
{ return reg_class
.is_linear(); }
199 constexpr bool operator <(Temp other
) const noexcept
{ return id() < other
.id(); }
200 constexpr bool operator==(Temp other
) const noexcept
{ return id() == other
.id(); }
201 constexpr bool operator!=(Temp other
) const noexcept
{ return id() != other
.id(); }
210 * Represents the physical register for each
211 * Operand and Definition.
214 constexpr PhysReg() = default;
215 explicit constexpr PhysReg(unsigned r
) : reg(r
) {}
216 constexpr operator unsigned() const { return reg
; }
221 /* helper expressions for special registers */
222 static constexpr PhysReg m0
{124};
223 static constexpr PhysReg vcc
{106};
224 static constexpr PhysReg sgpr_null
{125}; /* GFX10+ */
225 static constexpr PhysReg exec
{126};
226 static constexpr PhysReg exec_lo
{126};
227 static constexpr PhysReg exec_hi
{127};
228 static constexpr PhysReg scc
{253};
232 * Initially, each Operand refers to either
233 * a temporary virtual register
234 * or to a constant value
235 * Temporary registers get mapped to physical register during RA
236 * Constant values are inlined into the instruction sequence.
242 : reg_(PhysReg
{128}), isTemp_(false), isFixed_(true), isConstant_(false),
243 isKill_(false), isUndef_(true), isFirstKill_(false), is64BitConst_(false) {}
245 explicit Operand(Temp r
) noexcept
252 setFixed(PhysReg
{128});
255 explicit Operand(uint32_t v
) noexcept
260 setFixed(PhysReg
{128 + v
});
261 else if (v
>= 0xFFFFFFF0) /* [-16 .. -1] */
262 setFixed(PhysReg
{192 - v
});
263 else if (v
== 0x3f000000) /* 0.5 */
264 setFixed(PhysReg
{240});
265 else if (v
== 0xbf000000) /* -0.5 */
266 setFixed(PhysReg
{241});
267 else if (v
== 0x3f800000) /* 1.0 */
268 setFixed(PhysReg
{242});
269 else if (v
== 0xbf800000) /* -1.0 */
270 setFixed(PhysReg
{243});
271 else if (v
== 0x40000000) /* 2.0 */
272 setFixed(PhysReg
{244});
273 else if (v
== 0xc0000000) /* -2.0 */
274 setFixed(PhysReg
{245});
275 else if (v
== 0x40800000) /* 4.0 */
276 setFixed(PhysReg
{246});
277 else if (v
== 0xc0800000) /* -4.0 */
278 setFixed(PhysReg
{247});
279 else if (v
== 0x3e22f983) /* 1/(2*PI) */
280 setFixed(PhysReg
{248});
281 else /* Literal Constant */
282 setFixed(PhysReg
{255});
284 explicit Operand(uint64_t v
) noexcept
287 is64BitConst_
= true;
289 setFixed(PhysReg
{128 + (uint32_t) v
});
290 else if (v
>= 0xFFFFFFFFFFFFFFF0) /* [-16 .. -1] */
291 setFixed(PhysReg
{192 - (uint32_t) v
});
292 else if (v
== 0x3FE0000000000000) /* 0.5 */
293 setFixed(PhysReg
{240});
294 else if (v
== 0xBFE0000000000000) /* -0.5 */
295 setFixed(PhysReg
{241});
296 else if (v
== 0x3FF0000000000000) /* 1.0 */
297 setFixed(PhysReg
{242});
298 else if (v
== 0xBFF0000000000000) /* -1.0 */
299 setFixed(PhysReg
{243});
300 else if (v
== 0x4000000000000000) /* 2.0 */
301 setFixed(PhysReg
{244});
302 else if (v
== 0xC000000000000000) /* -2.0 */
303 setFixed(PhysReg
{245});
304 else if (v
== 0x4010000000000000) /* 4.0 */
305 setFixed(PhysReg
{246});
306 else if (v
== 0xC010000000000000) /* -4.0 */
307 setFixed(PhysReg
{247});
308 else if (v
== 0x3fc45f306dc9c882) /* 1/(2*PI) */
309 setFixed(PhysReg
{248});
310 else { /* Literal Constant: we don't know if it is a long or double.*/
312 assert(false && "attempt to create a 64-bit literal constant");
315 explicit Operand(RegClass type
) noexcept
318 data_
.temp
= Temp(0, type
);
319 setFixed(PhysReg
{128});
321 explicit Operand(PhysReg reg
, RegClass type
) noexcept
323 data_
.temp
= Temp(0, type
);
327 constexpr bool isTemp() const noexcept
332 constexpr void setTemp(Temp t
) noexcept
{
333 assert(!isConstant_
);
338 constexpr Temp
getTemp() const noexcept
343 constexpr uint32_t tempId() const noexcept
345 return data_
.temp
.id();
348 constexpr bool hasRegClass() const noexcept
350 return isTemp() || isUndefined();
353 constexpr RegClass
regClass() const noexcept
355 return data_
.temp
.regClass();
358 constexpr unsigned size() const noexcept
361 return is64BitConst_
? 2 : 1;
363 return data_
.temp
.size();
366 constexpr bool isFixed() const noexcept
371 constexpr PhysReg
physReg() const noexcept
376 constexpr void setFixed(PhysReg reg
) noexcept
378 isFixed_
= reg
!= unsigned(-1);
382 constexpr bool isConstant() const noexcept
387 constexpr bool isLiteral() const noexcept
389 return isConstant() && reg_
== 255;
392 constexpr bool isUndefined() const noexcept
397 constexpr uint32_t constantValue() const noexcept
402 constexpr bool constantEquals(uint32_t cmp
) const noexcept
404 return isConstant() && constantValue() == cmp
;
407 constexpr void setKill(bool flag
) noexcept
414 constexpr bool isKill() const noexcept
416 return isKill_
|| isFirstKill();
419 constexpr void setFirstKill(bool flag
) noexcept
426 /* When there are multiple operands killing the same temporary,
427 * isFirstKill() is only returns true for the first one. */
428 constexpr bool isFirstKill() const noexcept
437 Temp temp
= Temp(0, s1
);
444 uint8_t isConstant_
:1;
447 uint8_t isFirstKill_
:1;
448 uint8_t is64BitConst_
:1;
450 /* can't initialize bit-fields in c++11, so work around using a union */
451 uint8_t control_
= 0;
457 * Definitions are the results of Instructions
458 * and refer to temporary virtual registers
459 * which are later mapped to physical registers
461 class Definition final
464 constexpr Definition() : temp(Temp(0, s1
)), reg_(0), isFixed_(0), hasHint_(0), isKill_(0) {}
465 Definition(uint32_t index
, RegClass type
) noexcept
466 : temp(index
, type
) {}
467 explicit Definition(Temp tmp
) noexcept
469 Definition(PhysReg reg
, RegClass type
) noexcept
470 : temp(Temp(0, type
))
474 Definition(uint32_t tmpId
, PhysReg reg
, RegClass type
) noexcept
475 : temp(Temp(tmpId
, type
))
480 constexpr bool isTemp() const noexcept
485 constexpr Temp
getTemp() const noexcept
490 constexpr uint32_t tempId() const noexcept
495 constexpr void setTemp(Temp t
) noexcept
{
499 constexpr RegClass
regClass() const noexcept
501 return temp
.regClass();
504 constexpr unsigned size() const noexcept
509 constexpr bool isFixed() const noexcept
514 constexpr PhysReg
physReg() const noexcept
519 constexpr void setFixed(PhysReg reg
) noexcept
525 constexpr void setHint(PhysReg reg
) noexcept
531 constexpr bool hasHint() const noexcept
536 constexpr void setKill(bool flag
) noexcept
541 constexpr bool isKill() const noexcept
547 Temp temp
= Temp(0, s1
);
555 /* can't initialize bit-fields in c++11, so work around using a union */
556 uint8_t control_
= 0;
567 aco::span
<Operand
> operands
;
568 aco::span
<Definition
> definitions
;
570 constexpr bool isVALU() const noexcept
572 return ((uint16_t) format
& (uint16_t) Format::VOP1
) == (uint16_t) Format::VOP1
573 || ((uint16_t) format
& (uint16_t) Format::VOP2
) == (uint16_t) Format::VOP2
574 || ((uint16_t) format
& (uint16_t) Format::VOPC
) == (uint16_t) Format::VOPC
575 || ((uint16_t) format
& (uint16_t) Format::VOP3A
) == (uint16_t) Format::VOP3A
576 || ((uint16_t) format
& (uint16_t) Format::VOP3B
) == (uint16_t) Format::VOP3B
577 || ((uint16_t) format
& (uint16_t) Format::VOP3P
) == (uint16_t) Format::VOP3P
;
580 constexpr bool isSALU() const noexcept
582 return format
== Format::SOP1
||
583 format
== Format::SOP2
||
584 format
== Format::SOPC
||
585 format
== Format::SOPK
||
586 format
== Format::SOPP
;
589 constexpr bool isVMEM() const noexcept
591 return format
== Format::MTBUF
||
592 format
== Format::MUBUF
||
593 format
== Format::MIMG
;
596 constexpr bool isDPP() const noexcept
598 return (uint16_t) format
& (uint16_t) Format::DPP
;
601 constexpr bool isVOP3() const noexcept
603 return ((uint16_t) format
& (uint16_t) Format::VOP3A
) ||
604 ((uint16_t) format
& (uint16_t) Format::VOP3B
) ||
605 format
== Format::VOP3P
;
608 constexpr bool isSDWA() const noexcept
610 return (uint16_t) format
& (uint16_t) Format::SDWA
;
613 constexpr bool isFlatOrGlobal() const noexcept
615 return format
== Format::FLAT
|| format
== Format::GLOBAL
;
618 constexpr bool usesModifiers() const noexcept
;
620 constexpr bool reads_exec() const noexcept
622 for (const Operand
& op
: operands
) {
623 if (op
.isFixed() && op
.physReg() == exec
)
630 struct SOPK_instruction
: public Instruction
{
634 struct SOPP_instruction
: public Instruction
{
639 struct SOPC_instruction
: public Instruction
{
642 struct SOP1_instruction
: public Instruction
{
645 struct SOP2_instruction
: public Instruction
{
649 * Scalar Memory Format:
650 * For s_(buffer_)load_dword*:
651 * Operand(0): SBASE - SGPR-pair which provides base address
652 * Operand(1): Offset - immediate (un)signed offset or SGPR
653 * Operand(2) / Definition(0): SDATA - SGPR for read / write result
654 * Operand(n-1): SOffset - SGPR offset (Vega only)
656 * Having no operands is also valid for instructions such as s_dcache_inv.
659 struct SMEM_instruction
: public Instruction
{
660 bool glc
; /* VI+: globally coherent */
661 bool dlc
; /* NAVI: device level coherent */
662 bool nv
; /* VEGA only: Non-volatile */
665 barrier_interaction barrier
;
668 struct VOP1_instruction
: public Instruction
{
671 struct VOP2_instruction
: public Instruction
{
674 struct VOPC_instruction
: public Instruction
{
677 struct VOP3A_instruction
: public Instruction
{
686 * Data Parallel Primitives Format:
687 * This format can be used for VOP1, VOP2 or VOPC instructions.
688 * The swizzle applies to the src0 operand.
691 struct DPP_instruction
: public Instruction
{
700 struct Interp_instruction
: public Instruction
{
706 * Local and Global Data Sharing instructions
707 * Operand(0): ADDR - VGPR which supplies the address.
708 * Operand(1): DATA0 - First data VGPR.
709 * Operand(2): DATA1 - Second data VGPR.
710 * Operand(n-1): M0 - LDS size.
711 * Definition(0): VDST - Destination VGPR when results returned to VGPRs.
714 struct DS_instruction
: public Instruction
{
721 * Vector Memory Untyped-buffer Instructions
722 * Operand(0): VADDR - Address source. Can carry an index and/or offset
723 * Operand(1): SRSRC - Specifies which SGPR supplies T# (resource constant)
724 * Operand(2): SOFFSET - SGPR to supply unsigned byte offset. (SGPR, M0, or inline constant)
725 * Operand(3) / Definition(0): VDATA - Vector GPR for write result / read data
728 struct MUBUF_instruction
: public Instruction
{
729 unsigned offset
; /* Unsigned byte offset - 12 bit */
730 bool offen
; /* Supply an offset from VGPR (VADDR) */
731 bool idxen
; /* Supply an index from VGPR (VADDR) */
732 bool glc
; /* globally coherent */
733 bool dlc
; /* NAVI: device level coherent */
734 bool slc
; /* system level coherent */
735 bool tfe
; /* texture fail enable */
736 bool lds
; /* Return read-data to LDS instead of VGPRs */
737 bool disable_wqm
; /* Require an exec mask without helper invocations */
739 barrier_interaction barrier
;
743 * Vector Memory Typed-buffer Instructions
744 * Operand(0): VADDR - Address source. Can carry an index and/or offset
745 * Operand(1): SRSRC - Specifies which SGPR supplies T# (resource constant)
746 * Operand(2): SOFFSET - SGPR to supply unsigned byte offset. (SGPR, M0, or inline constant)
747 * Operand(3) / Definition(0): VDATA - Vector GPR for write result / read data
750 struct MTBUF_instruction
: public Instruction
{
751 uint8_t dfmt
: 4; /* Data Format of data in memory buffer */
752 uint8_t nfmt
: 3; /* Numeric format of data in memory */
753 unsigned offset
; /* Unsigned byte offset - 12 bit */
754 bool offen
; /* Supply an offset from VGPR (VADDR) */
755 bool idxen
; /* Supply an index from VGPR (VADDR) */
756 bool glc
; /* globally coherent */
757 bool dlc
; /* NAVI: device level coherent */
758 bool slc
; /* system level coherent */
759 bool tfe
; /* texture fail enable */
760 bool disable_wqm
; /* Require an exec mask without helper invocations */
762 barrier_interaction barrier
;
766 * Vector Memory Image Instructions
767 * Operand(0): VADDR - Address source. Can carry an offset or an index.
768 * Operand(1): SRSRC - Scalar GPR that specifies the resource constant.
769 * Operand(2): SSAMP - Scalar GPR that specifies sampler constant.
770 * Operand(3) / Definition(0): VDATA - Vector GPR for read / write result.
773 struct MIMG_instruction
: public Instruction
{
774 unsigned dmask
; /* Data VGPR enable mask */
775 unsigned dim
; /* NAVI: dimensionality */
776 bool unrm
; /* Force address to be un-normalized */
777 bool dlc
; /* NAVI: device level coherent */
778 bool glc
; /* globally coherent */
779 bool slc
; /* system level coherent */
780 bool tfe
; /* texture fail enable */
781 bool da
; /* declare an array */
782 bool lwe
; /* Force data to be un-normalized */
783 bool r128
; /* NAVI: Texture resource size */
784 bool a16
; /* VEGA, NAVI: Address components are 16-bits */
785 bool d16
; /* Convert 32-bit data to 16-bit data */
786 bool disable_wqm
; /* Require an exec mask without helper invocations */
788 barrier_interaction barrier
;
792 * Flat/Scratch/Global Instructions
795 * Operand(2) / Definition(0): DATA/VDST
798 struct FLAT_instruction
: public Instruction
{
799 uint16_t offset
; /* Vega only */
800 bool slc
; /* system level coherent */
801 bool glc
; /* globally coherent */
802 bool dlc
; /* NAVI: device level coherent */
807 struct Export_instruction
: public Instruction
{
808 unsigned enabled_mask
;
815 struct Pseudo_instruction
: public Instruction
{
817 PhysReg scratch_sgpr
; /* might not be valid if it's not needed */
820 struct Pseudo_branch_instruction
: public Instruction
{
821 /* target[0] is the block index of the branch target.
822 * For conditional branches, target[1] contains the fall-through alternative.
823 * A value of 0 means the target has not been initialized (BB0 cannot be a branch target).
828 struct Pseudo_barrier_instruction
: public Instruction
{
845 gfx10_wave64_bpermute
849 * Subgroup Reduction Instructions, everything except for the data to be
850 * reduced and the result as inserted by setup_reduce_temp().
851 * Operand(0): data to be reduced
852 * Operand(1): reduce temporary
853 * Operand(2): vector temporary
854 * Definition(0): result
855 * Definition(1): scalar temporary
856 * Definition(2): scalar identity temporary (not used to store identity on GFX10)
857 * Definition(3): scc clobber
858 * Definition(4): vcc clobber
861 struct Pseudo_reduction_instruction
: public Instruction
{
863 unsigned cluster_size
; // must be 0 for scans
866 struct instr_deleter_functor
{
867 void operator()(void* p
) {
873 using aco_ptr
= std::unique_ptr
<T
, instr_deleter_functor
>;
876 T
* create_instruction(aco_opcode opcode
, Format format
, uint32_t num_operands
, uint32_t num_definitions
)
878 std::size_t size
= sizeof(T
) + num_operands
* sizeof(Operand
) + num_definitions
* sizeof(Definition
);
879 char *data
= (char*) calloc(1, size
);
882 inst
->opcode
= opcode
;
883 inst
->format
= format
;
885 inst
->operands
= aco::span
<Operand
>((Operand
*)(data
+ sizeof(T
)), num_operands
);
886 inst
->definitions
= aco::span
<Definition
>((Definition
*)inst
->operands
.end(), num_definitions
);
891 constexpr bool Instruction::usesModifiers() const noexcept
893 if (isDPP() || isSDWA())
897 const VOP3A_instruction
*vop3
= static_cast<const VOP3A_instruction
*>(this);
898 for (unsigned i
= 0; i
< operands
.size(); i
++) {
899 if (vop3
->abs
[i
] || vop3
->opsel
[i
] || vop3
->neg
[i
])
902 return vop3
->opsel
[3] || vop3
->clamp
|| vop3
->omod
;
905 constexpr bool is_phi(Instruction
* instr
)
907 return instr
->opcode
== aco_opcode::p_phi
|| instr
->opcode
== aco_opcode::p_linear_phi
;
910 static inline bool is_phi(aco_ptr
<Instruction
>& instr
)
912 return is_phi(instr
.get());
915 constexpr barrier_interaction
get_barrier_interaction(Instruction
* instr
)
917 switch (instr
->format
) {
919 return static_cast<SMEM_instruction
*>(instr
)->barrier
;
921 return static_cast<MUBUF_instruction
*>(instr
)->barrier
;
923 return static_cast<MIMG_instruction
*>(instr
)->barrier
;
926 return barrier_buffer
;
928 return barrier_shared
;
935 /* uniform indicates that leaving this block,
936 * all actives lanes stay active */
937 block_kind_uniform
= 1 << 0,
938 block_kind_top_level
= 1 << 1,
939 block_kind_loop_preheader
= 1 << 2,
940 block_kind_loop_header
= 1 << 3,
941 block_kind_loop_exit
= 1 << 4,
942 block_kind_continue
= 1 << 5,
943 block_kind_break
= 1 << 6,
944 block_kind_continue_or_break
= 1 << 7,
945 block_kind_discard
= 1 << 8,
946 block_kind_branch
= 1 << 9,
947 block_kind_merge
= 1 << 10,
948 block_kind_invert
= 1 << 11,
949 block_kind_uses_discard_if
= 1 << 12,
950 block_kind_needs_lowering
= 1 << 13,
951 block_kind_uses_demote
= 1 << 14,
955 struct RegisterDemand
{
956 constexpr RegisterDemand() = default;
957 constexpr RegisterDemand(const int16_t v
, const int16_t s
) noexcept
958 : vgpr
{v
}, sgpr
{s
} {}
962 constexpr friend bool operator==(const RegisterDemand a
, const RegisterDemand b
) noexcept
{
963 return a
.vgpr
== b
.vgpr
&& a
.sgpr
== b
.sgpr
;
966 constexpr bool exceeds(const RegisterDemand other
) const noexcept
{
967 return vgpr
> other
.vgpr
|| sgpr
> other
.sgpr
;
970 constexpr RegisterDemand
operator+(const Temp t
) const noexcept
{
971 if (t
.type() == RegType::sgpr
)
972 return RegisterDemand( vgpr
, sgpr
+ t
.size() );
974 return RegisterDemand( vgpr
+ t
.size(), sgpr
);
977 constexpr RegisterDemand
operator+(const RegisterDemand other
) const noexcept
{
978 return RegisterDemand(vgpr
+ other
.vgpr
, sgpr
+ other
.sgpr
);
981 constexpr RegisterDemand
operator-(const RegisterDemand other
) const noexcept
{
982 return RegisterDemand(vgpr
- other
.vgpr
, sgpr
- other
.sgpr
);
985 constexpr RegisterDemand
& operator+=(const RegisterDemand other
) noexcept
{
991 constexpr RegisterDemand
& operator-=(const RegisterDemand other
) noexcept
{
997 constexpr RegisterDemand
& operator+=(const Temp t
) noexcept
{
998 if (t
.type() == RegType::sgpr
)
1005 constexpr RegisterDemand
& operator-=(const Temp t
) noexcept
{
1006 if (t
.type() == RegType::sgpr
)
1013 constexpr void update(const RegisterDemand other
) noexcept
{
1014 vgpr
= std::max(vgpr
, other
.vgpr
);
1015 sgpr
= std::max(sgpr
, other
.sgpr
);
1023 unsigned offset
= 0;
1024 std::vector
<aco_ptr
<Instruction
>> instructions
;
1025 std::vector
<unsigned> logical_preds
;
1026 std::vector
<unsigned> linear_preds
;
1027 std::vector
<unsigned> logical_succs
;
1028 std::vector
<unsigned> linear_succs
;
1029 RegisterDemand register_demand
= RegisterDemand();
1030 uint16_t loop_nest_depth
= 0;
1032 int logical_idom
= -1;
1033 int linear_idom
= -1;
1034 Temp live_out_exec
= Temp();
1036 /* this information is needed for predecessors to blocks with phis when
1037 * moving out of ssa */
1038 bool scc_live_out
= false;
1039 PhysReg scratch_sgpr
= PhysReg(); /* only needs to be valid if scc_live_out != false */
1041 Block(unsigned idx
) : index(idx
) {}
1042 Block() : index(0) {}
1045 using Stage
= uint16_t;
1047 /* software stages */
1048 static constexpr Stage sw_vs
= 1 << 0;
1049 static constexpr Stage sw_gs
= 1 << 1;
1050 static constexpr Stage sw_tcs
= 1 << 2;
1051 static constexpr Stage sw_tes
= 1 << 3;
1052 static constexpr Stage sw_fs
= 1 << 4;
1053 static constexpr Stage sw_cs
= 1 << 5;
1054 static constexpr Stage sw_mask
= 0x3f;
1056 /* hardware stages (can't be OR'd, just a mask for convenience when testing multiple) */
1057 static constexpr Stage hw_vs
= 1 << 6;
1058 static constexpr Stage hw_es
= 1 << 7; /* not on GFX9. combined into GS on GFX9 (and GFX10/legacy). */
1059 static constexpr Stage hw_gs
= 1 << 8;
1060 static constexpr Stage hw_ls
= 1 << 9; /* not on GFX9. combined into HS on GFX9 (and GFX10/legacy). */
1061 static constexpr Stage hw_hs
= 1 << 10;
1062 static constexpr Stage hw_fs
= 1 << 11;
1063 static constexpr Stage hw_cs
= 1 << 12;
1064 static constexpr Stage hw_mask
= 0x7f << 6;
1066 /* possible settings of Program::stage */
1067 static constexpr Stage vertex_vs
= sw_vs
| hw_vs
;
1068 static constexpr Stage fragment_fs
= sw_fs
| hw_fs
;
1069 static constexpr Stage compute_cs
= sw_cs
| hw_cs
;
1070 static constexpr Stage tess_eval_vs
= sw_tes
| hw_vs
;
1072 static constexpr Stage ngg_vertex_gs
= sw_vs
| hw_gs
;
1073 static constexpr Stage ngg_vertex_geometry_gs
= sw_vs
| sw_gs
| hw_gs
;
1074 static constexpr Stage ngg_tess_eval_geometry_gs
= sw_tes
| sw_gs
| hw_gs
;
1075 static constexpr Stage ngg_vertex_tess_control_hs
= sw_vs
| sw_tcs
| hw_hs
;
1076 /* GFX9 (and GFX10 if NGG isn't used) */
1077 static constexpr Stage vertex_geometry_gs
= sw_vs
| sw_gs
| hw_gs
;
1078 static constexpr Stage vertex_tess_control_hs
= sw_vs
| sw_tcs
| hw_hs
;
1079 static constexpr Stage tess_eval_geometry_gs
= sw_tes
| sw_gs
| hw_gs
;
1081 static constexpr Stage vertex_ls
= sw_vs
| hw_ls
; /* vertex before tesselation control */
1082 static constexpr Stage vertex_es
= sw_vs
| hw_es
; /* vertex before geometry */
1083 static constexpr Stage tess_control_hs
= sw_tcs
| hw_hs
;
1084 static constexpr Stage tess_eval_es
= sw_tes
| hw_gs
; /* tesselation evaluation before geometry */
1085 static constexpr Stage geometry_gs
= sw_gs
| hw_gs
;
1087 class Program final
{
1089 std::vector
<Block
> blocks
;
1090 RegisterDemand max_reg_demand
= RegisterDemand();
1091 uint16_t num_waves
= 0;
1092 uint16_t max_waves
= 0; /* maximum number of waves, regardless of register usage */
1093 ac_shader_config
* config
;
1094 struct radv_shader_info
*info
;
1095 enum chip_class chip_class
;
1096 enum radeon_family family
;
1098 Stage stage
; /* Stage */
1099 bool needs_exact
= false; /* there exists an instruction with disable_wqm = true */
1100 bool needs_wqm
= false; /* there exists a p_wqm instruction */
1101 bool wb_smem_l1_on_end
= false;
1103 std::vector
<uint8_t> constant_data
;
1104 Temp private_segment_buffer
;
1105 Temp scratch_offset
;
1107 uint16_t lds_alloc_granule
;
1108 uint32_t lds_limit
; /* in bytes */
1109 uint16_t vgpr_limit
;
1110 uint16_t sgpr_limit
;
1111 uint16_t physical_sgprs
;
1112 uint16_t sgpr_alloc_granule
; /* minus one. must be power of two */
1114 bool needs_vcc
= false;
1115 bool needs_xnack_mask
= false;
1116 bool needs_flat_scr
= false;
1118 uint32_t allocateId()
1120 assert(allocationID
<= 16777215);
1121 return allocationID
++;
1124 uint32_t peekAllocationId()
1126 return allocationID
;
1129 void setAllocationId(uint32_t id
)
1134 Block
* create_and_insert_block() {
1135 blocks
.emplace_back(blocks
.size());
1136 return &blocks
.back();
1139 Block
* insert_block(Block
&& block
) {
1140 block
.index
= blocks
.size();
1141 blocks
.emplace_back(std::move(block
));
1142 return &blocks
.back();
1146 uint32_t allocationID
= 1;
1150 /* live temps out per block */
1151 std::vector
<std::set
<Temp
>> live_out
;
1152 /* register demand (sgpr/vgpr) per instruction per block */
1153 std::vector
<std::vector
<RegisterDemand
>> register_demand
;
1156 void select_program(Program
*program
,
1157 unsigned shader_count
,
1158 struct nir_shader
*const *shaders
,
1159 ac_shader_config
* config
,
1160 struct radv_shader_info
*info
,
1161 struct radv_nir_compiler_options
*options
);
1163 void lower_wqm(Program
* program
, live
& live_vars
,
1164 const struct radv_nir_compiler_options
*options
);
1165 void lower_bool_phis(Program
* program
);
1166 void update_vgpr_sgpr_demand(Program
* program
, const RegisterDemand new_demand
);
1167 live
live_var_analysis(Program
* program
, const struct radv_nir_compiler_options
*options
);
1168 std::vector
<uint16_t> dead_code_analysis(Program
*program
);
1169 void dominator_tree(Program
* program
);
1170 void insert_exec_mask(Program
*program
);
1171 void value_numbering(Program
* program
);
1172 void optimize(Program
* program
);
1173 void setup_reduce_temp(Program
* program
);
1174 void lower_to_cssa(Program
* program
, live
& live_vars
, const struct radv_nir_compiler_options
*options
);
1175 void register_allocation(Program
*program
, std::vector
<std::set
<Temp
>> live_out_per_block
);
1176 void ssa_elimination(Program
* program
);
1177 void lower_to_hw_instr(Program
* program
);
1178 void schedule_program(Program
* program
, live
& live_vars
);
1179 void spill(Program
* program
, live
& live_vars
, const struct radv_nir_compiler_options
*options
);
1180 void insert_wait_states(Program
* program
);
1181 void insert_NOPs(Program
* program
);
1182 unsigned emit_program(Program
* program
, std::vector
<uint32_t>& code
);
1183 void print_asm(Program
*program
, std::vector
<uint32_t>& binary
,
1184 unsigned exec_size
, std::ostream
& out
);
1185 void validate(Program
* program
, FILE *output
);
1186 bool validate_ra(Program
* program
, const struct radv_nir_compiler_options
*options
, FILE *output
);
1188 void perfwarn(bool cond
, const char *msg
, Instruction
*instr
=NULL
);
1190 #define perfwarn(program, cond, msg, ...)
1193 void aco_print_instr(Instruction
*instr
, FILE *output
);
1194 void aco_print_program(Program
*program
, FILE *output
);
1196 /* number of sgprs that need to be allocated but might notbe addressable as s0-s105 */
1197 uint16_t get_extra_sgprs(Program
*program
);
1199 /* get number of sgprs allocated required to address a number of sgprs */
1200 uint16_t get_sgpr_alloc(Program
*program
, uint16_t addressable_sgprs
);
1202 /* return number of addressable SGPRs for max_waves */
1203 uint16_t get_addr_sgpr_from_waves(Program
*program
, uint16_t max_waves
);
1206 const int16_t opcode_gfx9
[static_cast<int>(aco_opcode::num_opcodes
)];
1207 const int16_t opcode_gfx10
[static_cast<int>(aco_opcode::num_opcodes
)];
1208 const std::bitset
<static_cast<int>(aco_opcode::num_opcodes
)> can_use_input_modifiers
;
1209 const std::bitset
<static_cast<int>(aco_opcode::num_opcodes
)> can_use_output_modifiers
;
1210 const char *name
[static_cast<int>(aco_opcode::num_opcodes
)];
1211 const aco::Format format
[static_cast<int>(aco_opcode::num_opcodes
)];
1214 extern const Info instr_info
;
1218 #endif /* ACO_IR_H */