2 * Copyright 2017 Red Hat Inc.
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 shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
22 * Authors: Karol Herbst <kherbst@redhat.com>
25 #include "compiler/nir/nir.h"
27 #include "util/u_debug.h"
29 #include "codegen/nv50_ir.h"
30 #include "codegen/nv50_ir_from_common.h"
31 #include "codegen/nv50_ir_lowering_helper.h"
32 #include "codegen/nv50_ir_util.h"
34 #if __cplusplus >= 201103L
35 #include <unordered_map>
37 #include <tr1/unordered_map>
43 #if __cplusplus >= 201103L
45 using std::unordered_map
;
48 using std::tr1::unordered_map
;
51 using namespace nv50_ir
;
54 type_size(const struct glsl_type
*type
)
56 return glsl_count_attribute_slots(type
, false);
59 class Converter
: public ConverterCommon
62 Converter(Program
*, nir_shader
*, nv50_ir_prog_info
*);
66 typedef std::vector
<LValue
*> LValues
;
67 typedef unordered_map
<unsigned, LValues
> NirDefMap
;
68 typedef unordered_map
<unsigned, uint32_t> NirArrayLMemOffsets
;
69 typedef unordered_map
<unsigned, BasicBlock
*> NirBlockMap
;
71 TexTarget
convert(glsl_sampler_dim
, bool isArray
, bool isShadow
);
72 LValues
& convert(nir_alu_dest
*);
73 BasicBlock
* convert(nir_block
*);
74 LValues
& convert(nir_dest
*);
75 SVSemantic
convert(nir_intrinsic_op
);
76 LValues
& convert(nir_register
*);
77 LValues
& convert(nir_ssa_def
*);
79 Value
* getSrc(nir_alu_src
*, uint8_t component
= 0);
80 Value
* getSrc(nir_register
*, uint8_t);
81 Value
* getSrc(nir_src
*, uint8_t, bool indirect
= false);
82 Value
* getSrc(nir_ssa_def
*, uint8_t);
84 // returned value is the constant part of the given source (either the
85 // nir_src or the selected source component of an intrinsic). Even though
86 // this is mostly an optimization to be able to skip indirects in a few
87 // cases, sometimes we require immediate values or set some fileds on
88 // instructions (e.g. tex) in order for codegen to consume those.
89 // If the found value has not a constant part, the Value gets returned
90 // through the Value parameter.
91 uint32_t getIndirect(nir_src
*, uint8_t, Value
*&);
92 uint32_t getIndirect(nir_intrinsic_instr
*, uint8_t s
, uint8_t c
, Value
*&);
94 uint32_t getSlotAddress(nir_intrinsic_instr
*, uint8_t idx
, uint8_t slot
);
96 void setInterpolate(nv50_ir_varying
*,
101 Instruction
*loadFrom(DataFile
, uint8_t, DataType
, Value
*def
, uint32_t base
,
102 uint8_t c
, Value
*indirect0
= NULL
,
103 Value
*indirect1
= NULL
, bool patch
= false);
104 void storeTo(nir_intrinsic_instr
*, DataFile
, operation
, DataType
,
105 Value
*src
, uint8_t idx
, uint8_t c
, Value
*indirect0
= NULL
,
106 Value
*indirect1
= NULL
);
108 bool isFloatType(nir_alu_type
);
109 bool isSignedType(nir_alu_type
);
110 bool isResultFloat(nir_op
);
111 bool isResultSigned(nir_op
);
113 DataType
getDType(nir_alu_instr
*);
114 DataType
getDType(nir_intrinsic_instr
*);
115 DataType
getDType(nir_op
, uint8_t);
117 std::vector
<DataType
> getSTypes(nir_alu_instr
*);
118 DataType
getSType(nir_src
&, bool isFloat
, bool isSigned
);
120 operation
getOperation(nir_intrinsic_op
);
121 operation
getOperation(nir_op
);
122 operation
getOperation(nir_texop
);
123 operation
preOperationNeeded(nir_op
);
125 int getSubOp(nir_intrinsic_op
);
126 int getSubOp(nir_op
);
128 CondCode
getCondCode(nir_op
);
133 bool visit(nir_alu_instr
*);
134 bool visit(nir_block
*);
135 bool visit(nir_cf_node
*);
136 bool visit(nir_function
*);
137 bool visit(nir_if
*);
138 bool visit(nir_instr
*);
139 bool visit(nir_intrinsic_instr
*);
140 bool visit(nir_jump_instr
*);
141 bool visit(nir_load_const_instr
*);
142 bool visit(nir_loop
*);
143 bool visit(nir_ssa_undef_instr
*);
144 bool visit(nir_tex_instr
*);
147 Value
* applyProjection(Value
*src
, Value
*proj
);
153 NirArrayLMemOffsets regToLmemOffset
;
155 unsigned int curLoopDepth
;
160 int clipVertexOutput
;
169 Converter::Converter(Program
*prog
, nir_shader
*nir
, nv50_ir_prog_info
*info
)
170 : ConverterCommon(prog
, info
),
175 zero
= mkImm((uint32_t)0);
179 Converter::convert(nir_block
*block
)
181 NirBlockMap::iterator it
= blocks
.find(block
->index
);
182 if (it
!= blocks
.end())
185 BasicBlock
*bb
= new BasicBlock(func
);
186 blocks
[block
->index
] = bb
;
191 Converter::isFloatType(nir_alu_type type
)
193 return nir_alu_type_get_base_type(type
) == nir_type_float
;
197 Converter::isSignedType(nir_alu_type type
)
199 return nir_alu_type_get_base_type(type
) == nir_type_int
;
203 Converter::isResultFloat(nir_op op
)
205 const nir_op_info
&info
= nir_op_infos
[op
];
206 if (info
.output_type
!= nir_type_invalid
)
207 return isFloatType(info
.output_type
);
209 ERROR("isResultFloat not implemented for %s\n", nir_op_infos
[op
].name
);
215 Converter::isResultSigned(nir_op op
)
218 // there is no umul and we get wrong results if we treat all muls as signed
223 const nir_op_info
&info
= nir_op_infos
[op
];
224 if (info
.output_type
!= nir_type_invalid
)
225 return isSignedType(info
.output_type
);
226 ERROR("isResultSigned not implemented for %s\n", nir_op_infos
[op
].name
);
233 Converter::getDType(nir_alu_instr
*insn
)
235 if (insn
->dest
.dest
.is_ssa
)
236 return getDType(insn
->op
, insn
->dest
.dest
.ssa
.bit_size
);
238 return getDType(insn
->op
, insn
->dest
.dest
.reg
.reg
->bit_size
);
242 Converter::getDType(nir_intrinsic_instr
*insn
)
244 if (insn
->dest
.is_ssa
)
245 return typeOfSize(insn
->dest
.ssa
.bit_size
/ 8, false, false);
247 return typeOfSize(insn
->dest
.reg
.reg
->bit_size
/ 8, false, false);
251 Converter::getDType(nir_op op
, uint8_t bitSize
)
253 DataType ty
= typeOfSize(bitSize
/ 8, isResultFloat(op
), isResultSigned(op
));
254 if (ty
== TYPE_NONE
) {
255 ERROR("couldn't get Type for op %s with bitSize %u\n", nir_op_infos
[op
].name
, bitSize
);
261 std::vector
<DataType
>
262 Converter::getSTypes(nir_alu_instr
*insn
)
264 const nir_op_info
&info
= nir_op_infos
[insn
->op
];
265 std::vector
<DataType
> res(info
.num_inputs
);
267 for (uint8_t i
= 0; i
< info
.num_inputs
; ++i
) {
268 if (info
.input_types
[i
] != nir_type_invalid
) {
269 res
[i
] = getSType(insn
->src
[i
].src
, isFloatType(info
.input_types
[i
]), isSignedType(info
.input_types
[i
]));
271 ERROR("getSType not implemented for %s idx %u\n", info
.name
, i
);
282 Converter::getSType(nir_src
&src
, bool isFloat
, bool isSigned
)
286 bitSize
= src
.ssa
->bit_size
;
288 bitSize
= src
.reg
.reg
->bit_size
;
290 DataType ty
= typeOfSize(bitSize
/ 8, isFloat
, isSigned
);
291 if (ty
== TYPE_NONE
) {
299 ERROR("couldn't get Type for %s with bitSize %u\n", str
, bitSize
);
306 Converter::getOperation(nir_op op
)
309 // basic ops with float and int variants
319 case nir_op_ifind_msb
:
320 case nir_op_ufind_msb
:
342 case nir_op_fddx_coarse
:
343 case nir_op_fddx_fine
:
346 case nir_op_fddy_coarse
:
347 case nir_op_fddy_fine
:
365 case nir_op_pack_64_2x32_split
:
379 case nir_op_imul_high
:
380 case nir_op_umul_high
:
428 ERROR("couldn't get operation for op %s\n", nir_op_infos
[op
].name
);
435 Converter::getOperation(nir_texop op
)
447 case nir_texop_txf_ms
:
453 case nir_texop_query_levels
:
454 case nir_texop_texture_samples
:
458 ERROR("couldn't get operation for nir_texop %u\n", op
);
465 Converter::getOperation(nir_intrinsic_op op
)
469 ERROR("couldn't get operation for nir_intrinsic_op %u\n", op
);
476 Converter::preOperationNeeded(nir_op op
)
488 Converter::getSubOp(nir_op op
)
491 case nir_op_imul_high
:
492 case nir_op_umul_high
:
493 return NV50_IR_SUBOP_MUL_HIGH
;
500 Converter::getSubOp(nir_intrinsic_op op
)
503 case nir_intrinsic_vote_all
:
504 return NV50_IR_SUBOP_VOTE_ALL
;
505 case nir_intrinsic_vote_any
:
506 return NV50_IR_SUBOP_VOTE_ANY
;
507 case nir_intrinsic_vote_ieq
:
508 return NV50_IR_SUBOP_VOTE_UNI
;
515 Converter::getCondCode(nir_op op
)
534 ERROR("couldn't get CondCode for op %s\n", nir_op_infos
[op
].name
);
541 Converter::convert(nir_alu_dest
*dest
)
543 return convert(&dest
->dest
);
547 Converter::convert(nir_dest
*dest
)
550 return convert(&dest
->ssa
);
551 if (dest
->reg
.indirect
) {
552 ERROR("no support for indirects.");
555 return convert(dest
->reg
.reg
);
559 Converter::convert(nir_register
*reg
)
561 NirDefMap::iterator it
= regDefs
.find(reg
->index
);
562 if (it
!= regDefs
.end())
565 LValues
newDef(reg
->num_components
);
566 for (uint8_t i
= 0; i
< reg
->num_components
; i
++)
567 newDef
[i
] = getScratch(std::max(4, reg
->bit_size
/ 8));
568 return regDefs
[reg
->index
] = newDef
;
572 Converter::convert(nir_ssa_def
*def
)
574 NirDefMap::iterator it
= ssaDefs
.find(def
->index
);
575 if (it
!= ssaDefs
.end())
578 LValues
newDef(def
->num_components
);
579 for (uint8_t i
= 0; i
< def
->num_components
; i
++)
580 newDef
[i
] = getSSA(std::max(4, def
->bit_size
/ 8));
581 return ssaDefs
[def
->index
] = newDef
;
585 Converter::getSrc(nir_alu_src
*src
, uint8_t component
)
587 if (src
->abs
|| src
->negate
) {
588 ERROR("modifiers currently not supported on nir_alu_src\n");
591 return getSrc(&src
->src
, src
->swizzle
[component
]);
595 Converter::getSrc(nir_register
*reg
, uint8_t idx
)
597 NirDefMap::iterator it
= regDefs
.find(reg
->index
);
598 if (it
== regDefs
.end())
599 return convert(reg
)[idx
];
600 return it
->second
[idx
];
604 Converter::getSrc(nir_src
*src
, uint8_t idx
, bool indirect
)
607 return getSrc(src
->ssa
, idx
);
609 if (src
->reg
.indirect
) {
611 return getSrc(src
->reg
.indirect
, idx
);
612 ERROR("no support for indirects.");
617 return getSrc(src
->reg
.reg
, idx
);
621 Converter::getSrc(nir_ssa_def
*src
, uint8_t idx
)
623 NirDefMap::iterator it
= ssaDefs
.find(src
->index
);
624 if (it
== ssaDefs
.end()) {
625 ERROR("SSA value %u not found\n", src
->index
);
629 return it
->second
[idx
];
633 Converter::getIndirect(nir_src
*src
, uint8_t idx
, Value
*&indirect
)
635 nir_const_value
*offset
= nir_src_as_const_value(*src
);
639 return offset
->u32
[0];
642 indirect
= getSrc(src
, idx
, true);
647 Converter::getIndirect(nir_intrinsic_instr
*insn
, uint8_t s
, uint8_t c
, Value
*&indirect
)
649 int32_t idx
= nir_intrinsic_base(insn
) + getIndirect(&insn
->src
[s
], c
, indirect
);
651 indirect
= mkOp2v(OP_SHL
, TYPE_U32
, getSSA(4, FILE_ADDRESS
), indirect
, loadImm(NULL
, 4));
656 vert_attrib_to_tgsi_semantic(gl_vert_attrib slot
, unsigned *name
, unsigned *index
)
658 assert(name
&& index
);
660 if (slot
>= VERT_ATTRIB_MAX
) {
661 ERROR("invalid varying slot %u\n", slot
);
666 if (slot
>= VERT_ATTRIB_GENERIC0
&&
667 slot
< VERT_ATTRIB_GENERIC0
+ VERT_ATTRIB_GENERIC_MAX
) {
668 *name
= TGSI_SEMANTIC_GENERIC
;
669 *index
= slot
- VERT_ATTRIB_GENERIC0
;
673 if (slot
>= VERT_ATTRIB_TEX0
&&
674 slot
< VERT_ATTRIB_TEX0
+ VERT_ATTRIB_TEX_MAX
) {
675 *name
= TGSI_SEMANTIC_TEXCOORD
;
676 *index
= slot
- VERT_ATTRIB_TEX0
;
681 case VERT_ATTRIB_COLOR0
:
682 *name
= TGSI_SEMANTIC_COLOR
;
685 case VERT_ATTRIB_COLOR1
:
686 *name
= TGSI_SEMANTIC_COLOR
;
689 case VERT_ATTRIB_EDGEFLAG
:
690 *name
= TGSI_SEMANTIC_EDGEFLAG
;
693 case VERT_ATTRIB_FOG
:
694 *name
= TGSI_SEMANTIC_FOG
;
697 case VERT_ATTRIB_NORMAL
:
698 *name
= TGSI_SEMANTIC_NORMAL
;
701 case VERT_ATTRIB_POS
:
702 *name
= TGSI_SEMANTIC_POSITION
;
705 case VERT_ATTRIB_POINT_SIZE
:
706 *name
= TGSI_SEMANTIC_PSIZE
;
710 ERROR("unknown vert attrib slot %u\n", slot
);
717 varying_slot_to_tgsi_semantic(gl_varying_slot slot
, unsigned *name
, unsigned *index
)
719 assert(name
&& index
);
721 if (slot
>= VARYING_SLOT_TESS_MAX
) {
722 ERROR("invalid varying slot %u\n", slot
);
727 if (slot
>= VARYING_SLOT_PATCH0
) {
728 *name
= TGSI_SEMANTIC_PATCH
;
729 *index
= slot
- VARYING_SLOT_PATCH0
;
733 if (slot
>= VARYING_SLOT_VAR0
) {
734 *name
= TGSI_SEMANTIC_GENERIC
;
735 *index
= slot
- VARYING_SLOT_VAR0
;
739 if (slot
>= VARYING_SLOT_TEX0
&& slot
<= VARYING_SLOT_TEX7
) {
740 *name
= TGSI_SEMANTIC_TEXCOORD
;
741 *index
= slot
- VARYING_SLOT_TEX0
;
746 case VARYING_SLOT_BFC0
:
747 *name
= TGSI_SEMANTIC_BCOLOR
;
750 case VARYING_SLOT_BFC1
:
751 *name
= TGSI_SEMANTIC_BCOLOR
;
754 case VARYING_SLOT_CLIP_DIST0
:
755 *name
= TGSI_SEMANTIC_CLIPDIST
;
758 case VARYING_SLOT_CLIP_DIST1
:
759 *name
= TGSI_SEMANTIC_CLIPDIST
;
762 case VARYING_SLOT_CLIP_VERTEX
:
763 *name
= TGSI_SEMANTIC_CLIPVERTEX
;
766 case VARYING_SLOT_COL0
:
767 *name
= TGSI_SEMANTIC_COLOR
;
770 case VARYING_SLOT_COL1
:
771 *name
= TGSI_SEMANTIC_COLOR
;
774 case VARYING_SLOT_EDGE
:
775 *name
= TGSI_SEMANTIC_EDGEFLAG
;
778 case VARYING_SLOT_FACE
:
779 *name
= TGSI_SEMANTIC_FACE
;
782 case VARYING_SLOT_FOGC
:
783 *name
= TGSI_SEMANTIC_FOG
;
786 case VARYING_SLOT_LAYER
:
787 *name
= TGSI_SEMANTIC_LAYER
;
790 case VARYING_SLOT_PNTC
:
791 *name
= TGSI_SEMANTIC_PCOORD
;
794 case VARYING_SLOT_POS
:
795 *name
= TGSI_SEMANTIC_POSITION
;
798 case VARYING_SLOT_PRIMITIVE_ID
:
799 *name
= TGSI_SEMANTIC_PRIMID
;
802 case VARYING_SLOT_PSIZ
:
803 *name
= TGSI_SEMANTIC_PSIZE
;
806 case VARYING_SLOT_TESS_LEVEL_INNER
:
807 *name
= TGSI_SEMANTIC_TESSINNER
;
810 case VARYING_SLOT_TESS_LEVEL_OUTER
:
811 *name
= TGSI_SEMANTIC_TESSOUTER
;
814 case VARYING_SLOT_VIEWPORT
:
815 *name
= TGSI_SEMANTIC_VIEWPORT_INDEX
;
819 ERROR("unknown varying slot %u\n", slot
);
826 frag_result_to_tgsi_semantic(unsigned slot
, unsigned *name
, unsigned *index
)
828 if (slot
>= FRAG_RESULT_DATA0
) {
829 *name
= TGSI_SEMANTIC_COLOR
;
830 *index
= slot
- FRAG_RESULT_COLOR
- 2; // intentional
835 case FRAG_RESULT_COLOR
:
836 *name
= TGSI_SEMANTIC_COLOR
;
839 case FRAG_RESULT_DEPTH
:
840 *name
= TGSI_SEMANTIC_POSITION
;
843 case FRAG_RESULT_SAMPLE_MASK
:
844 *name
= TGSI_SEMANTIC_SAMPLEMASK
;
848 ERROR("unknown frag result slot %u\n", slot
);
854 // copy of _mesa_sysval_to_semantic
856 system_val_to_tgsi_semantic(unsigned val
, unsigned *name
, unsigned *index
)
861 case SYSTEM_VALUE_VERTEX_ID
:
862 *name
= TGSI_SEMANTIC_VERTEXID
;
864 case SYSTEM_VALUE_INSTANCE_ID
:
865 *name
= TGSI_SEMANTIC_INSTANCEID
;
867 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
:
868 *name
= TGSI_SEMANTIC_VERTEXID_NOBASE
;
870 case SYSTEM_VALUE_BASE_VERTEX
:
871 *name
= TGSI_SEMANTIC_BASEVERTEX
;
873 case SYSTEM_VALUE_BASE_INSTANCE
:
874 *name
= TGSI_SEMANTIC_BASEINSTANCE
;
876 case SYSTEM_VALUE_DRAW_ID
:
877 *name
= TGSI_SEMANTIC_DRAWID
;
881 case SYSTEM_VALUE_INVOCATION_ID
:
882 *name
= TGSI_SEMANTIC_INVOCATIONID
;
886 case SYSTEM_VALUE_FRAG_COORD
:
887 *name
= TGSI_SEMANTIC_POSITION
;
889 case SYSTEM_VALUE_FRONT_FACE
:
890 *name
= TGSI_SEMANTIC_FACE
;
892 case SYSTEM_VALUE_SAMPLE_ID
:
893 *name
= TGSI_SEMANTIC_SAMPLEID
;
895 case SYSTEM_VALUE_SAMPLE_POS
:
896 *name
= TGSI_SEMANTIC_SAMPLEPOS
;
898 case SYSTEM_VALUE_SAMPLE_MASK_IN
:
899 *name
= TGSI_SEMANTIC_SAMPLEMASK
;
901 case SYSTEM_VALUE_HELPER_INVOCATION
:
902 *name
= TGSI_SEMANTIC_HELPER_INVOCATION
;
905 // Tessellation shader
906 case SYSTEM_VALUE_TESS_COORD
:
907 *name
= TGSI_SEMANTIC_TESSCOORD
;
909 case SYSTEM_VALUE_VERTICES_IN
:
910 *name
= TGSI_SEMANTIC_VERTICESIN
;
912 case SYSTEM_VALUE_PRIMITIVE_ID
:
913 *name
= TGSI_SEMANTIC_PRIMID
;
915 case SYSTEM_VALUE_TESS_LEVEL_OUTER
:
916 *name
= TGSI_SEMANTIC_TESSOUTER
;
918 case SYSTEM_VALUE_TESS_LEVEL_INNER
:
919 *name
= TGSI_SEMANTIC_TESSINNER
;
923 case SYSTEM_VALUE_LOCAL_INVOCATION_ID
:
924 *name
= TGSI_SEMANTIC_THREAD_ID
;
926 case SYSTEM_VALUE_WORK_GROUP_ID
:
927 *name
= TGSI_SEMANTIC_BLOCK_ID
;
929 case SYSTEM_VALUE_NUM_WORK_GROUPS
:
930 *name
= TGSI_SEMANTIC_GRID_SIZE
;
932 case SYSTEM_VALUE_LOCAL_GROUP_SIZE
:
933 *name
= TGSI_SEMANTIC_BLOCK_SIZE
;
937 case SYSTEM_VALUE_SUBGROUP_SIZE
:
938 *name
= TGSI_SEMANTIC_SUBGROUP_SIZE
;
940 case SYSTEM_VALUE_SUBGROUP_INVOCATION
:
941 *name
= TGSI_SEMANTIC_SUBGROUP_INVOCATION
;
943 case SYSTEM_VALUE_SUBGROUP_EQ_MASK
:
944 *name
= TGSI_SEMANTIC_SUBGROUP_EQ_MASK
;
946 case SYSTEM_VALUE_SUBGROUP_GE_MASK
:
947 *name
= TGSI_SEMANTIC_SUBGROUP_GE_MASK
;
949 case SYSTEM_VALUE_SUBGROUP_GT_MASK
:
950 *name
= TGSI_SEMANTIC_SUBGROUP_GT_MASK
;
952 case SYSTEM_VALUE_SUBGROUP_LE_MASK
:
953 *name
= TGSI_SEMANTIC_SUBGROUP_LE_MASK
;
955 case SYSTEM_VALUE_SUBGROUP_LT_MASK
:
956 *name
= TGSI_SEMANTIC_SUBGROUP_LT_MASK
;
960 ERROR("unknown system value %u\n", val
);
967 Converter::setInterpolate(nv50_ir_varying
*var
,
973 case INTERP_MODE_FLAT
:
976 case INTERP_MODE_NONE
:
977 if (semantic
== TGSI_SEMANTIC_COLOR
)
979 else if (semantic
== TGSI_SEMANTIC_POSITION
)
982 case INTERP_MODE_NOPERSPECTIVE
:
985 case INTERP_MODE_SMOOTH
:
988 var
->centroid
= centroid
;
992 calcSlots(const glsl_type
*type
, Program::Type stage
, const shader_info
&info
,
993 bool input
, const nir_variable
*var
)
995 if (!type
->is_array())
996 return type
->count_attribute_slots(false);
1000 case Program::TYPE_GEOMETRY
:
1001 slots
= type
->uniform_locations();
1003 slots
/= info
.gs
.vertices_in
;
1005 case Program::TYPE_TESSELLATION_CONTROL
:
1006 case Program::TYPE_TESSELLATION_EVAL
:
1007 // remove first dimension
1008 if (var
->data
.patch
|| (!input
&& stage
== Program::TYPE_TESSELLATION_EVAL
))
1009 slots
= type
->uniform_locations();
1011 slots
= type
->fields
.array
->uniform_locations();
1014 slots
= type
->count_attribute_slots(false);
1021 bool Converter::assignSlots() {
1025 info
->io
.viewportId
= -1;
1026 info
->numInputs
= 0;
1028 // we have to fixup the uniform locations for arrays
1029 unsigned numImages
= 0;
1030 nir_foreach_variable(var
, &nir
->uniforms
) {
1031 const glsl_type
*type
= var
->type
;
1032 if (!type
->without_array()->is_image())
1034 var
->data
.driver_location
= numImages
;
1035 numImages
+= type
->is_array() ? type
->arrays_of_arrays_size() : 1;
1038 nir_foreach_variable(var
, &nir
->inputs
) {
1039 const glsl_type
*type
= var
->type
;
1040 int slot
= var
->data
.location
;
1041 uint16_t slots
= calcSlots(type
, prog
->getType(), nir
->info
, true, var
);
1042 uint32_t comp
= type
->is_array() ? type
->without_array()->component_slots()
1043 : type
->component_slots();
1044 uint32_t frac
= var
->data
.location_frac
;
1045 uint32_t vary
= var
->data
.driver_location
;
1047 if (glsl_base_type_is_64bit(type
->without_array()->base_type
)) {
1052 assert(vary
+ slots
<= PIPE_MAX_SHADER_INPUTS
);
1054 switch(prog
->getType()) {
1055 case Program::TYPE_FRAGMENT
:
1056 varying_slot_to_tgsi_semantic((gl_varying_slot
)slot
, &name
, &index
);
1057 for (uint16_t i
= 0; i
< slots
; ++i
) {
1058 setInterpolate(&info
->in
[vary
+ i
], var
->data
.interpolation
,
1059 var
->data
.centroid
| var
->data
.sample
, name
);
1062 case Program::TYPE_GEOMETRY
:
1063 varying_slot_to_tgsi_semantic((gl_varying_slot
)slot
, &name
, &index
);
1065 case Program::TYPE_TESSELLATION_CONTROL
:
1066 case Program::TYPE_TESSELLATION_EVAL
:
1067 varying_slot_to_tgsi_semantic((gl_varying_slot
)slot
, &name
, &index
);
1068 if (var
->data
.patch
&& name
== TGSI_SEMANTIC_PATCH
)
1069 info
->numPatchConstants
= MAX2(info
->numPatchConstants
, index
+ slots
);
1071 case Program::TYPE_VERTEX
:
1072 vert_attrib_to_tgsi_semantic((gl_vert_attrib
)slot
, &name
, &index
);
1074 case TGSI_SEMANTIC_EDGEFLAG
:
1075 info
->io
.edgeFlagIn
= vary
;
1082 ERROR("unknown shader type %u in assignSlots\n", prog
->getType());
1086 for (uint16_t i
= 0u; i
< slots
; ++i
, ++vary
) {
1087 info
->in
[vary
].id
= vary
;
1088 info
->in
[vary
].patch
= var
->data
.patch
;
1089 info
->in
[vary
].sn
= name
;
1090 info
->in
[vary
].si
= index
+ i
;
1091 if (glsl_base_type_is_64bit(type
->without_array()->base_type
))
1093 info
->in
[vary
].mask
|= (((1 << (comp
* 2)) - 1) << (frac
* 2) >> 0x4);
1095 info
->in
[vary
].mask
|= (((1 << (comp
* 2)) - 1) << (frac
* 2) & 0xf);
1097 info
->in
[vary
].mask
|= ((1 << comp
) - 1) << frac
;
1099 info
->numInputs
= std::max
<uint8_t>(info
->numInputs
, vary
);
1102 info
->numOutputs
= 0;
1103 nir_foreach_variable(var
, &nir
->outputs
) {
1104 const glsl_type
*type
= var
->type
;
1105 int slot
= var
->data
.location
;
1106 uint16_t slots
= calcSlots(type
, prog
->getType(), nir
->info
, false, var
);
1107 uint32_t comp
= type
->is_array() ? type
->without_array()->component_slots()
1108 : type
->component_slots();
1109 uint32_t frac
= var
->data
.location_frac
;
1110 uint32_t vary
= var
->data
.driver_location
;
1112 if (glsl_base_type_is_64bit(type
->without_array()->base_type
)) {
1117 assert(vary
< PIPE_MAX_SHADER_OUTPUTS
);
1119 switch(prog
->getType()) {
1120 case Program::TYPE_FRAGMENT
:
1121 frag_result_to_tgsi_semantic((gl_frag_result
)slot
, &name
, &index
);
1123 case TGSI_SEMANTIC_COLOR
:
1124 if (!var
->data
.fb_fetch_output
)
1125 info
->prop
.fp
.numColourResults
++;
1126 info
->prop
.fp
.separateFragData
= true;
1127 // sometimes we get FRAG_RESULT_DATAX with data.index 0
1128 // sometimes we get FRAG_RESULT_DATA0 with data.index X
1129 index
= index
== 0 ? var
->data
.index
: index
;
1131 case TGSI_SEMANTIC_POSITION
:
1132 info
->io
.fragDepth
= vary
;
1133 info
->prop
.fp
.writesDepth
= true;
1135 case TGSI_SEMANTIC_SAMPLEMASK
:
1136 info
->io
.sampleMask
= vary
;
1142 case Program::TYPE_GEOMETRY
:
1143 case Program::TYPE_TESSELLATION_CONTROL
:
1144 case Program::TYPE_TESSELLATION_EVAL
:
1145 case Program::TYPE_VERTEX
:
1146 varying_slot_to_tgsi_semantic((gl_varying_slot
)slot
, &name
, &index
);
1148 if (var
->data
.patch
&& name
!= TGSI_SEMANTIC_TESSINNER
&&
1149 name
!= TGSI_SEMANTIC_TESSOUTER
)
1150 info
->numPatchConstants
= MAX2(info
->numPatchConstants
, index
+ slots
);
1153 case TGSI_SEMANTIC_CLIPDIST
:
1154 info
->io
.genUserClip
= -1;
1156 case TGSI_SEMANTIC_CLIPVERTEX
:
1157 clipVertexOutput
= vary
;
1159 case TGSI_SEMANTIC_EDGEFLAG
:
1160 info
->io
.edgeFlagOut
= vary
;
1162 case TGSI_SEMANTIC_POSITION
:
1163 if (clipVertexOutput
< 0)
1164 clipVertexOutput
= vary
;
1171 ERROR("unknown shader type %u in assignSlots\n", prog
->getType());
1175 for (uint16_t i
= 0u; i
< slots
; ++i
, ++vary
) {
1176 info
->out
[vary
].id
= vary
;
1177 info
->out
[vary
].patch
= var
->data
.patch
;
1178 info
->out
[vary
].sn
= name
;
1179 info
->out
[vary
].si
= index
+ i
;
1180 if (glsl_base_type_is_64bit(type
->without_array()->base_type
))
1182 info
->out
[vary
].mask
|= (((1 << (comp
* 2)) - 1) << (frac
* 2) >> 0x4);
1184 info
->out
[vary
].mask
|= (((1 << (comp
* 2)) - 1) << (frac
* 2) & 0xf);
1186 info
->out
[vary
].mask
|= ((1 << comp
) - 1) << frac
;
1188 if (nir
->info
.outputs_read
& 1ll << slot
)
1189 info
->out
[vary
].oread
= 1;
1191 info
->numOutputs
= std::max
<uint8_t>(info
->numOutputs
, vary
);
1194 info
->numSysVals
= 0;
1195 for (uint8_t i
= 0; i
< 64; ++i
) {
1196 if (!(nir
->info
.system_values_read
& 1ll << i
))
1199 system_val_to_tgsi_semantic(i
, &name
, &index
);
1200 info
->sv
[info
->numSysVals
].sn
= name
;
1201 info
->sv
[info
->numSysVals
].si
= index
;
1202 info
->sv
[info
->numSysVals
].input
= 0; // TODO inferSysValDirection(sn);
1205 case SYSTEM_VALUE_INSTANCE_ID
:
1206 info
->io
.instanceId
= info
->numSysVals
;
1208 case SYSTEM_VALUE_TESS_LEVEL_INNER
:
1209 case SYSTEM_VALUE_TESS_LEVEL_OUTER
:
1210 info
->sv
[info
->numSysVals
].patch
= 1;
1212 case SYSTEM_VALUE_VERTEX_ID
:
1213 info
->io
.vertexId
= info
->numSysVals
;
1219 info
->numSysVals
+= 1;
1222 if (info
->io
.genUserClip
> 0) {
1223 info
->io
.clipDistances
= info
->io
.genUserClip
;
1225 const unsigned int nOut
= (info
->io
.genUserClip
+ 3) / 4;
1227 for (unsigned int n
= 0; n
< nOut
; ++n
) {
1228 unsigned int i
= info
->numOutputs
++;
1229 info
->out
[i
].id
= i
;
1230 info
->out
[i
].sn
= TGSI_SEMANTIC_CLIPDIST
;
1231 info
->out
[i
].si
= n
;
1232 info
->out
[i
].mask
= ((1 << info
->io
.clipDistances
) - 1) >> (n
* 4);
1236 return info
->assignSlots(info
) == 0;
1240 Converter::getSlotAddress(nir_intrinsic_instr
*insn
, uint8_t idx
, uint8_t slot
)
1243 int offset
= nir_intrinsic_component(insn
);
1246 if (nir_intrinsic_infos
[insn
->intrinsic
].has_dest
)
1247 ty
= getDType(insn
);
1249 ty
= getSType(insn
->src
[0], false, false);
1251 switch (insn
->intrinsic
) {
1252 case nir_intrinsic_load_input
:
1253 case nir_intrinsic_load_interpolated_input
:
1254 case nir_intrinsic_load_per_vertex_input
:
1257 case nir_intrinsic_load_output
:
1258 case nir_intrinsic_load_per_vertex_output
:
1259 case nir_intrinsic_store_output
:
1260 case nir_intrinsic_store_per_vertex_output
:
1264 ERROR("unknown intrinsic in getSlotAddress %s",
1265 nir_intrinsic_infos
[insn
->intrinsic
].name
);
1271 if (typeSizeof(ty
) == 8) {
1283 assert(!input
|| idx
< PIPE_MAX_SHADER_INPUTS
);
1284 assert(input
|| idx
< PIPE_MAX_SHADER_OUTPUTS
);
1286 const nv50_ir_varying
*vary
= input
? info
->in
: info
->out
;
1287 return vary
[idx
].slot
[slot
] * 4;
1291 Converter::loadFrom(DataFile file
, uint8_t i
, DataType ty
, Value
*def
,
1292 uint32_t base
, uint8_t c
, Value
*indirect0
,
1293 Value
*indirect1
, bool patch
)
1295 unsigned int tySize
= typeSizeof(ty
);
1298 (file
== FILE_MEMORY_CONST
|| file
== FILE_MEMORY_BUFFER
|| indirect0
)) {
1299 Value
*lo
= getSSA();
1300 Value
*hi
= getSSA();
1303 mkLoad(TYPE_U32
, lo
,
1304 mkSymbol(file
, i
, TYPE_U32
, base
+ c
* tySize
),
1306 loi
->setIndirect(0, 1, indirect1
);
1307 loi
->perPatch
= patch
;
1310 mkLoad(TYPE_U32
, hi
,
1311 mkSymbol(file
, i
, TYPE_U32
, base
+ c
* tySize
+ 4),
1313 hii
->setIndirect(0, 1, indirect1
);
1314 hii
->perPatch
= patch
;
1316 return mkOp2(OP_MERGE
, ty
, def
, lo
, hi
);
1319 mkLoad(ty
, def
, mkSymbol(file
, i
, ty
, base
+ c
* tySize
), indirect0
);
1320 ld
->setIndirect(0, 1, indirect1
);
1321 ld
->perPatch
= patch
;
1327 Converter::storeTo(nir_intrinsic_instr
*insn
, DataFile file
, operation op
,
1328 DataType ty
, Value
*src
, uint8_t idx
, uint8_t c
,
1329 Value
*indirect0
, Value
*indirect1
)
1331 uint8_t size
= typeSizeof(ty
);
1332 uint32_t address
= getSlotAddress(insn
, idx
, c
);
1334 if (size
== 8 && indirect0
) {
1336 mkSplit(split
, 4, src
);
1338 if (op
== OP_EXPORT
) {
1339 split
[0] = mkMov(getSSA(), split
[0], ty
)->getDef(0);
1340 split
[1] = mkMov(getSSA(), split
[1], ty
)->getDef(0);
1343 mkStore(op
, TYPE_U32
, mkSymbol(file
, 0, TYPE_U32
, address
), indirect0
,
1344 split
[0])->perPatch
= info
->out
[idx
].patch
;
1345 mkStore(op
, TYPE_U32
, mkSymbol(file
, 0, TYPE_U32
, address
+ 4), indirect0
,
1346 split
[1])->perPatch
= info
->out
[idx
].patch
;
1348 if (op
== OP_EXPORT
)
1349 src
= mkMov(getSSA(size
), src
, ty
)->getDef(0);
1350 mkStore(op
, ty
, mkSymbol(file
, 0, ty
, address
), indirect0
,
1351 src
)->perPatch
= info
->out
[idx
].patch
;
1356 Converter::parseNIR()
1358 info
->bin
.tlsSpace
= 0;
1359 info
->io
.clipDistances
= nir
->info
.clip_distance_array_size
;
1360 info
->io
.cullDistances
= nir
->info
.cull_distance_array_size
;
1362 switch(prog
->getType()) {
1363 case Program::TYPE_COMPUTE
:
1364 info
->prop
.cp
.numThreads
[0] = nir
->info
.cs
.local_size
[0];
1365 info
->prop
.cp
.numThreads
[1] = nir
->info
.cs
.local_size
[1];
1366 info
->prop
.cp
.numThreads
[2] = nir
->info
.cs
.local_size
[2];
1367 info
->bin
.smemSize
= nir
->info
.cs
.shared_size
;
1369 case Program::TYPE_FRAGMENT
:
1370 info
->prop
.fp
.earlyFragTests
= nir
->info
.fs
.early_fragment_tests
;
1371 info
->prop
.fp
.persampleInvocation
=
1372 (nir
->info
.system_values_read
& SYSTEM_BIT_SAMPLE_ID
) ||
1373 (nir
->info
.system_values_read
& SYSTEM_BIT_SAMPLE_POS
);
1374 info
->prop
.fp
.postDepthCoverage
= nir
->info
.fs
.post_depth_coverage
;
1375 info
->prop
.fp
.readsSampleLocations
=
1376 (nir
->info
.system_values_read
& SYSTEM_BIT_SAMPLE_POS
);
1377 info
->prop
.fp
.usesDiscard
= nir
->info
.fs
.uses_discard
;
1378 info
->prop
.fp
.usesSampleMaskIn
=
1379 !!(nir
->info
.system_values_read
& SYSTEM_BIT_SAMPLE_MASK_IN
);
1381 case Program::TYPE_GEOMETRY
:
1382 info
->prop
.gp
.inputPrim
= nir
->info
.gs
.input_primitive
;
1383 info
->prop
.gp
.instanceCount
= nir
->info
.gs
.invocations
;
1384 info
->prop
.gp
.maxVertices
= nir
->info
.gs
.vertices_out
;
1385 info
->prop
.gp
.outputPrim
= nir
->info
.gs
.output_primitive
;
1387 case Program::TYPE_TESSELLATION_CONTROL
:
1388 case Program::TYPE_TESSELLATION_EVAL
:
1389 if (nir
->info
.tess
.primitive_mode
== GL_ISOLINES
)
1390 info
->prop
.tp
.domain
= GL_LINES
;
1392 info
->prop
.tp
.domain
= nir
->info
.tess
.primitive_mode
;
1393 info
->prop
.tp
.outputPatchSize
= nir
->info
.tess
.tcs_vertices_out
;
1394 info
->prop
.tp
.outputPrim
=
1395 nir
->info
.tess
.point_mode
? PIPE_PRIM_POINTS
: PIPE_PRIM_TRIANGLES
;
1396 info
->prop
.tp
.partitioning
= (nir
->info
.tess
.spacing
+ 1) % 3;
1397 info
->prop
.tp
.winding
= !nir
->info
.tess
.ccw
;
1399 case Program::TYPE_VERTEX
:
1400 info
->prop
.vp
.usesDrawParameters
=
1401 (nir
->info
.system_values_read
& BITFIELD64_BIT(SYSTEM_VALUE_BASE_VERTEX
)) ||
1402 (nir
->info
.system_values_read
& BITFIELD64_BIT(SYSTEM_VALUE_BASE_INSTANCE
)) ||
1403 (nir
->info
.system_values_read
& BITFIELD64_BIT(SYSTEM_VALUE_DRAW_ID
));
1413 Converter::visit(nir_function
*function
)
1415 // we only support emiting the main function for now
1416 assert(!strcmp(function
->name
, "main"));
1417 assert(function
->impl
);
1419 // usually the blocks will set everything up, but main is special
1420 BasicBlock
*entry
= new BasicBlock(prog
->main
);
1421 exit
= new BasicBlock(prog
->main
);
1422 blocks
[nir_start_block(function
->impl
)->index
] = entry
;
1423 prog
->main
->setEntry(entry
);
1424 prog
->main
->setExit(exit
);
1426 setPosition(entry
, true);
1428 if (info
->io
.genUserClip
> 0) {
1429 for (int c
= 0; c
< 4; ++c
)
1430 clipVtx
[c
] = getScratch();
1433 switch (prog
->getType()) {
1434 case Program::TYPE_TESSELLATION_CONTROL
:
1436 OP_SUB
, TYPE_U32
, getSSA(),
1437 mkOp1v(OP_RDSV
, TYPE_U32
, getSSA(), mkSysVal(SV_LANEID
, 0)),
1438 mkOp1v(OP_RDSV
, TYPE_U32
, getSSA(), mkSysVal(SV_INVOCATION_ID
, 0)));
1440 case Program::TYPE_FRAGMENT
: {
1441 Symbol
*sv
= mkSysVal(SV_POSITION
, 3);
1442 fragCoord
[3] = mkOp1v(OP_RDSV
, TYPE_F32
, getSSA(), sv
);
1443 fp
.position
= mkOp1v(OP_RCP
, TYPE_F32
, fragCoord
[3], fragCoord
[3]);
1450 nir_foreach_register(reg
, &function
->impl
->registers
) {
1451 if (reg
->num_array_elems
) {
1452 // TODO: packed variables would be nice, but MemoryOpt fails
1453 // replace 4 with reg->num_components
1454 uint32_t size
= 4 * reg
->num_array_elems
* (reg
->bit_size
/ 8);
1455 regToLmemOffset
[reg
->index
] = info
->bin
.tlsSpace
;
1456 info
->bin
.tlsSpace
+= size
;
1460 nir_index_ssa_defs(function
->impl
);
1461 foreach_list_typed(nir_cf_node
, node
, node
, &function
->impl
->body
) {
1466 bb
->cfg
.attach(&exit
->cfg
, Graph::Edge::TREE
);
1467 setPosition(exit
, true);
1469 if (info
->io
.genUserClip
> 0)
1470 handleUserClipPlanes();
1472 // TODO: for non main function this needs to be a OP_RETURN
1473 mkOp(OP_EXIT
, TYPE_NONE
, NULL
)->terminator
= 1;
1478 Converter::visit(nir_cf_node
*node
)
1480 switch (node
->type
) {
1481 case nir_cf_node_block
:
1482 return visit(nir_cf_node_as_block(node
));
1483 case nir_cf_node_if
:
1484 return visit(nir_cf_node_as_if(node
));
1485 case nir_cf_node_loop
:
1486 return visit(nir_cf_node_as_loop(node
));
1488 ERROR("unknown nir_cf_node type %u\n", node
->type
);
1494 Converter::visit(nir_block
*block
)
1496 if (!block
->predecessors
->entries
&& block
->instr_list
.is_empty())
1499 BasicBlock
*bb
= convert(block
);
1501 setPosition(bb
, true);
1502 nir_foreach_instr(insn
, block
) {
1510 Converter::visit(nir_if
*nif
)
1512 DataType sType
= getSType(nif
->condition
, false, false);
1513 Value
*src
= getSrc(&nif
->condition
, 0);
1515 nir_block
*lastThen
= nir_if_last_then_block(nif
);
1516 nir_block
*lastElse
= nir_if_last_else_block(nif
);
1518 assert(!lastThen
->successors
[1]);
1519 assert(!lastElse
->successors
[1]);
1521 BasicBlock
*ifBB
= convert(nir_if_first_then_block(nif
));
1522 BasicBlock
*elseBB
= convert(nir_if_first_else_block(nif
));
1524 bb
->cfg
.attach(&ifBB
->cfg
, Graph::Edge::TREE
);
1525 bb
->cfg
.attach(&elseBB
->cfg
, Graph::Edge::TREE
);
1527 // we only insert joinats, if both nodes end up at the end of the if again.
1528 // the reason for this to not happens are breaks/continues/ret/... which
1529 // have their own handling
1530 if (lastThen
->successors
[0] == lastElse
->successors
[0])
1531 bb
->joinAt
= mkFlow(OP_JOINAT
, convert(lastThen
->successors
[0]),
1534 mkFlow(OP_BRA
, elseBB
, CC_EQ
, src
)->setType(sType
);
1536 foreach_list_typed(nir_cf_node
, node
, node
, &nif
->then_list
) {
1540 setPosition(convert(lastThen
), true);
1541 if (!bb
->getExit() ||
1542 !bb
->getExit()->asFlow() ||
1543 bb
->getExit()->asFlow()->op
== OP_JOIN
) {
1544 BasicBlock
*tailBB
= convert(lastThen
->successors
[0]);
1545 mkFlow(OP_BRA
, tailBB
, CC_ALWAYS
, NULL
);
1546 bb
->cfg
.attach(&tailBB
->cfg
, Graph::Edge::FORWARD
);
1549 foreach_list_typed(nir_cf_node
, node
, node
, &nif
->else_list
) {
1553 setPosition(convert(lastElse
), true);
1554 if (!bb
->getExit() ||
1555 !bb
->getExit()->asFlow() ||
1556 bb
->getExit()->asFlow()->op
== OP_JOIN
) {
1557 BasicBlock
*tailBB
= convert(lastElse
->successors
[0]);
1558 mkFlow(OP_BRA
, tailBB
, CC_ALWAYS
, NULL
);
1559 bb
->cfg
.attach(&tailBB
->cfg
, Graph::Edge::FORWARD
);
1562 if (lastThen
->successors
[0] == lastElse
->successors
[0]) {
1563 setPosition(convert(lastThen
->successors
[0]), true);
1564 mkFlow(OP_JOIN
, NULL
, CC_ALWAYS
, NULL
)->fixed
= 1;
1571 Converter::visit(nir_loop
*loop
)
1574 func
->loopNestingBound
= std::max(func
->loopNestingBound
, curLoopDepth
);
1576 BasicBlock
*loopBB
= convert(nir_loop_first_block(loop
));
1577 BasicBlock
*tailBB
=
1578 convert(nir_cf_node_as_block(nir_cf_node_next(&loop
->cf_node
)));
1579 bb
->cfg
.attach(&loopBB
->cfg
, Graph::Edge::TREE
);
1581 mkFlow(OP_PREBREAK
, tailBB
, CC_ALWAYS
, NULL
);
1582 setPosition(loopBB
, false);
1583 mkFlow(OP_PRECONT
, loopBB
, CC_ALWAYS
, NULL
);
1585 foreach_list_typed(nir_cf_node
, node
, node
, &loop
->body
) {
1589 Instruction
*insn
= bb
->getExit();
1590 if (bb
->cfg
.incidentCount() != 0) {
1591 if (!insn
|| !insn
->asFlow()) {
1592 mkFlow(OP_CONT
, loopBB
, CC_ALWAYS
, NULL
);
1593 bb
->cfg
.attach(&loopBB
->cfg
, Graph::Edge::BACK
);
1594 } else if (insn
&& insn
->op
== OP_BRA
&& !insn
->getPredicate() &&
1595 tailBB
->cfg
.incidentCount() == 0) {
1596 // RA doesn't like having blocks around with no incident edge,
1597 // so we create a fake one to make it happy
1598 bb
->cfg
.attach(&tailBB
->cfg
, Graph::Edge::TREE
);
1608 Converter::visit(nir_instr
*insn
)
1610 switch (insn
->type
) {
1611 case nir_instr_type_alu
:
1612 return visit(nir_instr_as_alu(insn
));
1613 case nir_instr_type_intrinsic
:
1614 return visit(nir_instr_as_intrinsic(insn
));
1615 case nir_instr_type_jump
:
1616 return visit(nir_instr_as_jump(insn
));
1617 case nir_instr_type_load_const
:
1618 return visit(nir_instr_as_load_const(insn
));
1619 case nir_instr_type_ssa_undef
:
1620 return visit(nir_instr_as_ssa_undef(insn
));
1621 case nir_instr_type_tex
:
1622 return visit(nir_instr_as_tex(insn
));
1624 ERROR("unknown nir_instr type %u\n", insn
->type
);
1631 Converter::convert(nir_intrinsic_op intr
)
1634 case nir_intrinsic_load_base_vertex
:
1635 return SV_BASEVERTEX
;
1636 case nir_intrinsic_load_base_instance
:
1637 return SV_BASEINSTANCE
;
1638 case nir_intrinsic_load_draw_id
:
1640 case nir_intrinsic_load_front_face
:
1642 case nir_intrinsic_load_helper_invocation
:
1643 return SV_THREAD_KILL
;
1644 case nir_intrinsic_load_instance_id
:
1645 return SV_INSTANCE_ID
;
1646 case nir_intrinsic_load_invocation_id
:
1647 return SV_INVOCATION_ID
;
1648 case nir_intrinsic_load_local_group_size
:
1650 case nir_intrinsic_load_local_invocation_id
:
1652 case nir_intrinsic_load_num_work_groups
:
1654 case nir_intrinsic_load_patch_vertices_in
:
1655 return SV_VERTEX_COUNT
;
1656 case nir_intrinsic_load_primitive_id
:
1657 return SV_PRIMITIVE_ID
;
1658 case nir_intrinsic_load_sample_id
:
1659 return SV_SAMPLE_INDEX
;
1660 case nir_intrinsic_load_sample_mask_in
:
1661 return SV_SAMPLE_MASK
;
1662 case nir_intrinsic_load_sample_pos
:
1663 return SV_SAMPLE_POS
;
1664 case nir_intrinsic_load_subgroup_eq_mask
:
1665 return SV_LANEMASK_EQ
;
1666 case nir_intrinsic_load_subgroup_ge_mask
:
1667 return SV_LANEMASK_GE
;
1668 case nir_intrinsic_load_subgroup_gt_mask
:
1669 return SV_LANEMASK_GT
;
1670 case nir_intrinsic_load_subgroup_le_mask
:
1671 return SV_LANEMASK_LE
;
1672 case nir_intrinsic_load_subgroup_lt_mask
:
1673 return SV_LANEMASK_LT
;
1674 case nir_intrinsic_load_subgroup_invocation
:
1676 case nir_intrinsic_load_tess_coord
:
1677 return SV_TESS_COORD
;
1678 case nir_intrinsic_load_tess_level_inner
:
1679 return SV_TESS_INNER
;
1680 case nir_intrinsic_load_tess_level_outer
:
1681 return SV_TESS_OUTER
;
1682 case nir_intrinsic_load_vertex_id
:
1683 return SV_VERTEX_ID
;
1684 case nir_intrinsic_load_work_group_id
:
1687 ERROR("unknown SVSemantic for nir_intrinsic_op %s\n",
1688 nir_intrinsic_infos
[intr
].name
);
1695 Converter::visit(nir_intrinsic_instr
*insn
)
1697 nir_intrinsic_op op
= insn
->intrinsic
;
1700 case nir_intrinsic_load_uniform
: {
1701 LValues
&newDefs
= convert(&insn
->dest
);
1702 const DataType dType
= getDType(insn
);
1704 uint32_t coffset
= getIndirect(insn
, 0, 0, indirect
);
1705 for (uint8_t i
= 0; i
< insn
->num_components
; ++i
) {
1706 loadFrom(FILE_MEMORY_CONST
, 0, dType
, newDefs
[i
], 16 * coffset
, i
, indirect
);
1710 case nir_intrinsic_store_output
:
1711 case nir_intrinsic_store_per_vertex_output
: {
1713 DataType dType
= getSType(insn
->src
[0], false, false);
1714 uint32_t idx
= getIndirect(insn
, op
== nir_intrinsic_store_output
? 1 : 2, 0, indirect
);
1716 for (uint8_t i
= 0u; i
< insn
->num_components
; ++i
) {
1717 if (!((1u << i
) & nir_intrinsic_write_mask(insn
)))
1721 Value
*src
= getSrc(&insn
->src
[0], i
);
1722 switch (prog
->getType()) {
1723 case Program::TYPE_FRAGMENT
: {
1724 if (info
->out
[idx
].sn
== TGSI_SEMANTIC_POSITION
) {
1725 // TGSI uses a different interface than NIR, TGSI stores that
1726 // value in the z component, NIR in X
1728 src
= mkOp1v(OP_SAT
, TYPE_F32
, getScratch(), src
);
1732 case Program::TYPE_VERTEX
: {
1733 if (info
->io
.genUserClip
> 0 && idx
== clipVertexOutput
) {
1734 mkMov(clipVtx
[i
], src
);
1743 storeTo(insn
, FILE_SHADER_OUTPUT
, OP_EXPORT
, dType
, src
, idx
, i
+ offset
, indirect
);
1747 case nir_intrinsic_load_input
:
1748 case nir_intrinsic_load_interpolated_input
:
1749 case nir_intrinsic_load_output
: {
1750 LValues
&newDefs
= convert(&insn
->dest
);
1753 if (prog
->getType() == Program::TYPE_FRAGMENT
&&
1754 op
== nir_intrinsic_load_output
) {
1755 std::vector
<Value
*> defs
, srcs
;
1758 srcs
.push_back(getSSA());
1759 srcs
.push_back(getSSA());
1760 Value
*x
= mkOp1v(OP_RDSV
, TYPE_F32
, getSSA(), mkSysVal(SV_POSITION
, 0));
1761 Value
*y
= mkOp1v(OP_RDSV
, TYPE_F32
, getSSA(), mkSysVal(SV_POSITION
, 1));
1762 mkCvt(OP_CVT
, TYPE_U32
, srcs
[0], TYPE_F32
, x
)->rnd
= ROUND_Z
;
1763 mkCvt(OP_CVT
, TYPE_U32
, srcs
[1], TYPE_F32
, y
)->rnd
= ROUND_Z
;
1765 srcs
.push_back(mkOp1v(OP_RDSV
, TYPE_U32
, getSSA(), mkSysVal(SV_LAYER
, 0)));
1766 srcs
.push_back(mkOp1v(OP_RDSV
, TYPE_U32
, getSSA(), mkSysVal(SV_SAMPLE_INDEX
, 0)));
1768 for (uint8_t i
= 0u; i
< insn
->num_components
; ++i
) {
1769 defs
.push_back(newDefs
[i
]);
1773 TexInstruction
*texi
= mkTex(OP_TXF
, TEX_TARGET_2D_MS_ARRAY
, 0, 0, defs
, srcs
);
1774 texi
->tex
.levelZero
= 1;
1775 texi
->tex
.mask
= mask
;
1776 texi
->tex
.useOffsets
= 0;
1777 texi
->tex
.r
= 0xffff;
1778 texi
->tex
.s
= 0xffff;
1780 info
->prop
.fp
.readsFramebuffer
= true;
1784 const DataType dType
= getDType(insn
);
1786 bool input
= op
!= nir_intrinsic_load_output
;
1790 uint32_t idx
= getIndirect(insn
, op
== nir_intrinsic_load_interpolated_input
? 1 : 0, 0, indirect
);
1791 nv50_ir_varying
& vary
= input
? info
->in
[idx
] : info
->out
[idx
];
1793 // see load_barycentric_* handling
1794 if (prog
->getType() == Program::TYPE_FRAGMENT
) {
1795 mode
= translateInterpMode(&vary
, nvirOp
);
1796 if (op
== nir_intrinsic_load_interpolated_input
) {
1797 ImmediateValue immMode
;
1798 if (getSrc(&insn
->src
[0], 1)->getUniqueInsn()->src(0).getImmediate(immMode
))
1799 mode
|= immMode
.reg
.data
.u32
;
1803 for (uint8_t i
= 0u; i
< insn
->num_components
; ++i
) {
1804 uint32_t address
= getSlotAddress(insn
, idx
, i
);
1805 Symbol
*sym
= mkSymbol(input
? FILE_SHADER_INPUT
: FILE_SHADER_OUTPUT
, 0, dType
, address
);
1806 if (prog
->getType() == Program::TYPE_FRAGMENT
) {
1808 if (typeSizeof(dType
) == 8) {
1809 Value
*lo
= getSSA();
1810 Value
*hi
= getSSA();
1811 Instruction
*interp
;
1813 interp
= mkOp1(nvirOp
, TYPE_U32
, lo
, sym
);
1814 if (nvirOp
== OP_PINTERP
)
1815 interp
->setSrc(s
++, fp
.position
);
1816 if (mode
& NV50_IR_INTERP_OFFSET
)
1817 interp
->setSrc(s
++, getSrc(&insn
->src
[0], 0));
1818 interp
->setInterpolate(mode
);
1819 interp
->setIndirect(0, 0, indirect
);
1821 Symbol
*sym1
= mkSymbol(input
? FILE_SHADER_INPUT
: FILE_SHADER_OUTPUT
, 0, dType
, address
+ 4);
1822 interp
= mkOp1(nvirOp
, TYPE_U32
, hi
, sym1
);
1823 if (nvirOp
== OP_PINTERP
)
1824 interp
->setSrc(s
++, fp
.position
);
1825 if (mode
& NV50_IR_INTERP_OFFSET
)
1826 interp
->setSrc(s
++, getSrc(&insn
->src
[0], 0));
1827 interp
->setInterpolate(mode
);
1828 interp
->setIndirect(0, 0, indirect
);
1830 mkOp2(OP_MERGE
, dType
, newDefs
[i
], lo
, hi
);
1832 Instruction
*interp
= mkOp1(nvirOp
, dType
, newDefs
[i
], sym
);
1833 if (nvirOp
== OP_PINTERP
)
1834 interp
->setSrc(s
++, fp
.position
);
1835 if (mode
& NV50_IR_INTERP_OFFSET
)
1836 interp
->setSrc(s
++, getSrc(&insn
->src
[0], 0));
1837 interp
->setInterpolate(mode
);
1838 interp
->setIndirect(0, 0, indirect
);
1841 mkLoad(dType
, newDefs
[i
], sym
, indirect
)->perPatch
= vary
.patch
;
1846 case nir_intrinsic_load_barycentric_at_offset
:
1847 case nir_intrinsic_load_barycentric_at_sample
:
1848 case nir_intrinsic_load_barycentric_centroid
:
1849 case nir_intrinsic_load_barycentric_pixel
:
1850 case nir_intrinsic_load_barycentric_sample
: {
1851 LValues
&newDefs
= convert(&insn
->dest
);
1854 if (op
== nir_intrinsic_load_barycentric_centroid
||
1855 op
== nir_intrinsic_load_barycentric_sample
) {
1856 mode
= NV50_IR_INTERP_CENTROID
;
1857 } else if (op
== nir_intrinsic_load_barycentric_at_offset
) {
1859 for (uint8_t c
= 0; c
< 2; c
++) {
1860 offs
[c
] = getScratch();
1861 mkOp2(OP_MIN
, TYPE_F32
, offs
[c
], getSrc(&insn
->src
[0], c
), loadImm(NULL
, 0.4375f
));
1862 mkOp2(OP_MAX
, TYPE_F32
, offs
[c
], offs
[c
], loadImm(NULL
, -0.5f
));
1863 mkOp2(OP_MUL
, TYPE_F32
, offs
[c
], offs
[c
], loadImm(NULL
, 4096.0f
));
1864 mkCvt(OP_CVT
, TYPE_S32
, offs
[c
], TYPE_F32
, offs
[c
]);
1866 mkOp3v(OP_INSBF
, TYPE_U32
, newDefs
[0], offs
[1], mkImm(0x1010), offs
[0]);
1868 mode
= NV50_IR_INTERP_OFFSET
;
1869 } else if (op
== nir_intrinsic_load_barycentric_pixel
) {
1870 mode
= NV50_IR_INTERP_DEFAULT
;
1871 } else if (op
== nir_intrinsic_load_barycentric_at_sample
) {
1872 info
->prop
.fp
.readsSampleLocations
= true;
1873 mkOp1(OP_PIXLD
, TYPE_U32
, newDefs
[0], getSrc(&insn
->src
[0], 0))->subOp
= NV50_IR_SUBOP_PIXLD_OFFSET
;
1874 mode
= NV50_IR_INTERP_OFFSET
;
1876 unreachable("all intrinsics already handled above");
1879 loadImm(newDefs
[1], mode
);
1882 case nir_intrinsic_discard
:
1883 mkOp(OP_DISCARD
, TYPE_NONE
, NULL
);
1885 case nir_intrinsic_discard_if
: {
1886 Value
*pred
= getSSA(1, FILE_PREDICATE
);
1887 if (insn
->num_components
> 1) {
1888 ERROR("nir_intrinsic_discard_if only with 1 component supported!\n");
1892 mkCmp(OP_SET
, CC_NE
, TYPE_U8
, pred
, TYPE_U32
, getSrc(&insn
->src
[0], 0), zero
);
1893 mkOp(OP_DISCARD
, TYPE_NONE
, NULL
)->setPredicate(CC_P
, pred
);
1896 case nir_intrinsic_load_base_vertex
:
1897 case nir_intrinsic_load_base_instance
:
1898 case nir_intrinsic_load_draw_id
:
1899 case nir_intrinsic_load_front_face
:
1900 case nir_intrinsic_load_helper_invocation
:
1901 case nir_intrinsic_load_instance_id
:
1902 case nir_intrinsic_load_invocation_id
:
1903 case nir_intrinsic_load_local_group_size
:
1904 case nir_intrinsic_load_local_invocation_id
:
1905 case nir_intrinsic_load_num_work_groups
:
1906 case nir_intrinsic_load_patch_vertices_in
:
1907 case nir_intrinsic_load_primitive_id
:
1908 case nir_intrinsic_load_sample_id
:
1909 case nir_intrinsic_load_sample_mask_in
:
1910 case nir_intrinsic_load_sample_pos
:
1911 case nir_intrinsic_load_subgroup_eq_mask
:
1912 case nir_intrinsic_load_subgroup_ge_mask
:
1913 case nir_intrinsic_load_subgroup_gt_mask
:
1914 case nir_intrinsic_load_subgroup_le_mask
:
1915 case nir_intrinsic_load_subgroup_lt_mask
:
1916 case nir_intrinsic_load_subgroup_invocation
:
1917 case nir_intrinsic_load_tess_coord
:
1918 case nir_intrinsic_load_tess_level_inner
:
1919 case nir_intrinsic_load_tess_level_outer
:
1920 case nir_intrinsic_load_vertex_id
:
1921 case nir_intrinsic_load_work_group_id
: {
1922 const DataType dType
= getDType(insn
);
1923 SVSemantic sv
= convert(op
);
1924 LValues
&newDefs
= convert(&insn
->dest
);
1926 for (uint8_t i
= 0u; i
< insn
->num_components
; ++i
) {
1928 if (typeSizeof(dType
) == 8)
1933 if (sv
== SV_TID
&& info
->prop
.cp
.numThreads
[i
] == 1) {
1936 Symbol
*sym
= mkSysVal(sv
, i
);
1937 Instruction
*rdsv
= mkOp1(OP_RDSV
, TYPE_U32
, def
, sym
);
1938 if (sv
== SV_TESS_OUTER
|| sv
== SV_TESS_INNER
)
1942 if (typeSizeof(dType
) == 8)
1943 mkOp2(OP_MERGE
, dType
, newDefs
[i
], def
, loadImm(getSSA(), 0u));
1948 case nir_intrinsic_load_subgroup_size
: {
1949 LValues
&newDefs
= convert(&insn
->dest
);
1950 loadImm(newDefs
[0], 32u);
1953 case nir_intrinsic_vote_all
:
1954 case nir_intrinsic_vote_any
:
1955 case nir_intrinsic_vote_ieq
: {
1956 LValues
&newDefs
= convert(&insn
->dest
);
1957 Value
*pred
= getScratch(1, FILE_PREDICATE
);
1958 mkCmp(OP_SET
, CC_NE
, TYPE_U32
, pred
, TYPE_U32
, getSrc(&insn
->src
[0], 0), zero
);
1959 mkOp1(OP_VOTE
, TYPE_U32
, pred
, pred
)->subOp
= getSubOp(op
);
1960 mkCvt(OP_CVT
, TYPE_U32
, newDefs
[0], TYPE_U8
, pred
);
1963 case nir_intrinsic_ballot
: {
1964 LValues
&newDefs
= convert(&insn
->dest
);
1965 Value
*pred
= getSSA(1, FILE_PREDICATE
);
1966 mkCmp(OP_SET
, CC_NE
, TYPE_U32
, pred
, TYPE_U32
, getSrc(&insn
->src
[0], 0), zero
);
1967 mkOp1(OP_VOTE
, TYPE_U32
, newDefs
[0], pred
)->subOp
= NV50_IR_SUBOP_VOTE_ANY
;
1970 case nir_intrinsic_read_first_invocation
:
1971 case nir_intrinsic_read_invocation
: {
1972 LValues
&newDefs
= convert(&insn
->dest
);
1973 const DataType dType
= getDType(insn
);
1974 Value
*tmp
= getScratch();
1976 if (op
== nir_intrinsic_read_first_invocation
) {
1977 mkOp1(OP_VOTE
, TYPE_U32
, tmp
, mkImm(1))->subOp
= NV50_IR_SUBOP_VOTE_ANY
;
1978 mkOp2(OP_EXTBF
, TYPE_U32
, tmp
, tmp
, mkImm(0x2000))->subOp
= NV50_IR_SUBOP_EXTBF_REV
;
1979 mkOp1(OP_BFIND
, TYPE_U32
, tmp
, tmp
)->subOp
= NV50_IR_SUBOP_BFIND_SAMT
;
1981 tmp
= getSrc(&insn
->src
[1], 0);
1983 for (uint8_t i
= 0; i
< insn
->num_components
; ++i
) {
1984 mkOp3(OP_SHFL
, dType
, newDefs
[i
], getSrc(&insn
->src
[0], i
), tmp
, mkImm(0x1f))
1985 ->subOp
= NV50_IR_SUBOP_SHFL_IDX
;
1990 ERROR("unknown nir_intrinsic_op %s\n", nir_intrinsic_infos
[op
].name
);
1998 Converter::visit(nir_jump_instr
*insn
)
2000 switch (insn
->type
) {
2001 case nir_jump_return
:
2002 // TODO: this only works in the main function
2003 mkFlow(OP_BRA
, exit
, CC_ALWAYS
, NULL
);
2004 bb
->cfg
.attach(&exit
->cfg
, Graph::Edge::CROSS
);
2006 case nir_jump_break
:
2007 case nir_jump_continue
: {
2008 bool isBreak
= insn
->type
== nir_jump_break
;
2009 nir_block
*block
= insn
->instr
.block
;
2010 assert(!block
->successors
[1]);
2011 BasicBlock
*target
= convert(block
->successors
[0]);
2012 mkFlow(isBreak
? OP_BREAK
: OP_CONT
, target
, CC_ALWAYS
, NULL
);
2013 bb
->cfg
.attach(&target
->cfg
, isBreak
? Graph::Edge::CROSS
: Graph::Edge::BACK
);
2017 ERROR("unknown nir_jump_type %u\n", insn
->type
);
2025 Converter::visit(nir_load_const_instr
*insn
)
2027 assert(insn
->def
.bit_size
<= 64);
2029 LValues
&newDefs
= convert(&insn
->def
);
2030 for (int i
= 0; i
< insn
->def
.num_components
; i
++) {
2031 switch (insn
->def
.bit_size
) {
2033 loadImm(newDefs
[i
], insn
->value
.u64
[i
]);
2036 loadImm(newDefs
[i
], insn
->value
.u32
[i
]);
2039 loadImm(newDefs
[i
], insn
->value
.u16
[i
]);
2042 loadImm(newDefs
[i
], insn
->value
.u8
[i
]);
2049 #define DEFAULT_CHECKS \
2050 if (insn->dest.dest.ssa.num_components > 1) { \
2051 ERROR("nir_alu_instr only supported with 1 component!\n"); \
2054 if (insn->dest.write_mask != 1) { \
2055 ERROR("nir_alu_instr only with write_mask of 1 supported!\n"); \
2059 Converter::visit(nir_alu_instr
*insn
)
2061 const nir_op op
= insn
->op
;
2062 const nir_op_info
&info
= nir_op_infos
[op
];
2063 DataType dType
= getDType(insn
);
2064 const std::vector
<DataType
> sTypes
= getSTypes(insn
);
2066 Instruction
*oldPos
= this->bb
->getExit();
2078 case nir_op_fddx_coarse
:
2079 case nir_op_fddx_fine
:
2081 case nir_op_fddy_coarse
:
2082 case nir_op_fddy_fine
:
2101 case nir_op_imul_high
:
2102 case nir_op_umul_high
:
2109 case nir_op_pack_64_2x32_split
:
2127 LValues
&newDefs
= convert(&insn
->dest
);
2128 operation preOp
= preOperationNeeded(op
);
2129 if (preOp
!= OP_NOP
) {
2130 assert(info
.num_inputs
< 2);
2131 Value
*tmp
= getSSA(typeSizeof(dType
));
2132 Instruction
*i0
= mkOp(preOp
, dType
, tmp
);
2133 Instruction
*i1
= mkOp(getOperation(op
), dType
, newDefs
[0]);
2134 if (info
.num_inputs
) {
2135 i0
->setSrc(0, getSrc(&insn
->src
[0]));
2138 i1
->subOp
= getSubOp(op
);
2140 Instruction
*i
= mkOp(getOperation(op
), dType
, newDefs
[0]);
2141 for (unsigned s
= 0u; s
< info
.num_inputs
; ++s
) {
2142 i
->setSrc(s
, getSrc(&insn
->src
[s
]));
2144 i
->subOp
= getSubOp(op
);
2148 case nir_op_ifind_msb
:
2149 case nir_op_ufind_msb
: {
2151 LValues
&newDefs
= convert(&insn
->dest
);
2153 mkOp1(getOperation(op
), dType
, newDefs
[0], getSrc(&insn
->src
[0]));
2156 case nir_op_fround_even
: {
2158 LValues
&newDefs
= convert(&insn
->dest
);
2159 mkCvt(OP_CVT
, dType
, newDefs
[0], dType
, getSrc(&insn
->src
[0]))->rnd
= ROUND_NI
;
2162 // convert instructions
2176 case nir_op_u2u64
: {
2178 LValues
&newDefs
= convert(&insn
->dest
);
2179 Instruction
*i
= mkOp1(getOperation(op
), dType
, newDefs
[0], getSrc(&insn
->src
[0]));
2180 if (op
== nir_op_f2i32
|| op
== nir_op_f2i64
|| op
== nir_op_f2u32
|| op
== nir_op_f2u64
)
2182 i
->sType
= sTypes
[0];
2185 // compare instructions
2195 case nir_op_ine32
: {
2197 LValues
&newDefs
= convert(&insn
->dest
);
2198 Instruction
*i
= mkCmp(getOperation(op
),
2203 getSrc(&insn
->src
[0]),
2204 getSrc(&insn
->src
[1]));
2205 if (info
.num_inputs
== 3)
2206 i
->setSrc(2, getSrc(&insn
->src
[2]));
2207 i
->sType
= sTypes
[0];
2210 // those are weird ALU ops and need special handling, because
2211 // 1. they are always componend based
2212 // 2. they basically just merge multiple values into one data type
2215 if (!insn
->dest
.dest
.is_ssa
&& insn
->dest
.dest
.reg
.reg
->num_array_elems
) {
2216 nir_reg_dest
& reg
= insn
->dest
.dest
.reg
;
2217 uint32_t goffset
= regToLmemOffset
[reg
.reg
->index
];
2218 uint8_t comps
= reg
.reg
->num_components
;
2219 uint8_t size
= reg
.reg
->bit_size
/ 8;
2220 uint8_t csize
= 4 * size
; // TODO after fixing MemoryOpts: comps * size;
2221 uint32_t aoffset
= csize
* reg
.base_offset
;
2222 Value
*indirect
= NULL
;
2225 indirect
= mkOp2v(OP_MUL
, TYPE_U32
, getSSA(4, FILE_ADDRESS
),
2226 getSrc(reg
.indirect
, 0), mkImm(csize
));
2228 for (uint8_t i
= 0u; i
< comps
; ++i
) {
2229 if (!((1u << i
) & insn
->dest
.write_mask
))
2232 Symbol
*sym
= mkSymbol(FILE_MEMORY_LOCAL
, 0, dType
, goffset
+ aoffset
+ i
* size
);
2233 mkStore(OP_STORE
, dType
, sym
, indirect
, getSrc(&insn
->src
[0], i
));
2236 } else if (!insn
->src
[0].src
.is_ssa
&& insn
->src
[0].src
.reg
.reg
->num_array_elems
) {
2237 LValues
&newDefs
= convert(&insn
->dest
);
2238 nir_reg_src
& reg
= insn
->src
[0].src
.reg
;
2239 uint32_t goffset
= regToLmemOffset
[reg
.reg
->index
];
2240 // uint8_t comps = reg.reg->num_components;
2241 uint8_t size
= reg
.reg
->bit_size
/ 8;
2242 uint8_t csize
= 4 * size
; // TODO after fixing MemoryOpts: comps * size;
2243 uint32_t aoffset
= csize
* reg
.base_offset
;
2244 Value
*indirect
= NULL
;
2247 indirect
= mkOp2v(OP_MUL
, TYPE_U32
, getSSA(4, FILE_ADDRESS
), getSrc(reg
.indirect
, 0), mkImm(csize
));
2249 for (uint8_t i
= 0u; i
< newDefs
.size(); ++i
)
2250 loadFrom(FILE_MEMORY_LOCAL
, 0, dType
, newDefs
[i
], goffset
+ aoffset
, i
, indirect
);
2254 LValues
&newDefs
= convert(&insn
->dest
);
2255 for (LValues::size_type c
= 0u; c
< newDefs
.size(); ++c
) {
2256 mkMov(newDefs
[c
], getSrc(&insn
->src
[0], c
), dType
);
2263 LValues
&newDefs
= convert(&insn
->dest
);
2264 for (LValues::size_type c
= 0u; c
< newDefs
.size(); ++c
) {
2265 mkMov(newDefs
[c
], getSrc(&insn
->src
[c
]), dType
);
2270 case nir_op_pack_64_2x32
: {
2271 LValues
&newDefs
= convert(&insn
->dest
);
2272 Instruction
*merge
= mkOp(OP_MERGE
, dType
, newDefs
[0]);
2273 merge
->setSrc(0, getSrc(&insn
->src
[0], 0));
2274 merge
->setSrc(1, getSrc(&insn
->src
[0], 1));
2277 case nir_op_pack_half_2x16_split
: {
2278 LValues
&newDefs
= convert(&insn
->dest
);
2279 Value
*tmpH
= getSSA();
2280 Value
*tmpL
= getSSA();
2282 mkCvt(OP_CVT
, TYPE_F16
, tmpL
, TYPE_F32
, getSrc(&insn
->src
[0]));
2283 mkCvt(OP_CVT
, TYPE_F16
, tmpH
, TYPE_F32
, getSrc(&insn
->src
[1]));
2284 mkOp3(OP_INSBF
, TYPE_U32
, newDefs
[0], tmpH
, mkImm(0x1010), tmpL
);
2287 case nir_op_unpack_half_2x16_split_x
:
2288 case nir_op_unpack_half_2x16_split_y
: {
2289 LValues
&newDefs
= convert(&insn
->dest
);
2290 Instruction
*cvt
= mkCvt(OP_CVT
, TYPE_F32
, newDefs
[0], TYPE_F16
, getSrc(&insn
->src
[0]));
2291 if (op
== nir_op_unpack_half_2x16_split_y
)
2295 case nir_op_unpack_64_2x32
: {
2296 LValues
&newDefs
= convert(&insn
->dest
);
2297 mkOp1(OP_SPLIT
, dType
, newDefs
[0], getSrc(&insn
->src
[0]))->setDef(1, newDefs
[1]);
2300 case nir_op_unpack_64_2x32_split_x
: {
2301 LValues
&newDefs
= convert(&insn
->dest
);
2302 mkOp1(OP_SPLIT
, dType
, newDefs
[0], getSrc(&insn
->src
[0]))->setDef(1, getSSA());
2305 case nir_op_unpack_64_2x32_split_y
: {
2306 LValues
&newDefs
= convert(&insn
->dest
);
2307 mkOp1(OP_SPLIT
, dType
, getSSA(), getSrc(&insn
->src
[0]))->setDef(1, newDefs
[0]);
2310 // special instructions
2312 case nir_op_isign
: {
2315 if (::isFloatType(dType
))
2320 LValues
&newDefs
= convert(&insn
->dest
);
2321 LValue
*val0
= getScratch();
2322 LValue
*val1
= getScratch();
2323 mkCmp(OP_SET
, CC_GT
, iType
, val0
, dType
, getSrc(&insn
->src
[0]), zero
);
2324 mkCmp(OP_SET
, CC_LT
, iType
, val1
, dType
, getSrc(&insn
->src
[0]), zero
);
2326 if (dType
== TYPE_F64
) {
2327 mkOp2(OP_SUB
, iType
, val0
, val0
, val1
);
2328 mkCvt(OP_CVT
, TYPE_F64
, newDefs
[0], iType
, val0
);
2329 } else if (dType
== TYPE_S64
|| dType
== TYPE_U64
) {
2330 mkOp2(OP_SUB
, iType
, val0
, val1
, val0
);
2331 mkOp2(OP_SHR
, iType
, val1
, val0
, loadImm(NULL
, 31));
2332 mkOp2(OP_MERGE
, dType
, newDefs
[0], val0
, val1
);
2333 } else if (::isFloatType(dType
))
2334 mkOp2(OP_SUB
, iType
, newDefs
[0], val0
, val1
);
2336 mkOp2(OP_SUB
, iType
, newDefs
[0], val1
, val0
);
2340 case nir_op_b32csel
: {
2342 LValues
&newDefs
= convert(&insn
->dest
);
2343 mkCmp(OP_SLCT
, CC_NE
, dType
, newDefs
[0], sTypes
[0], getSrc(&insn
->src
[1]), getSrc(&insn
->src
[2]), getSrc(&insn
->src
[0]));
2346 case nir_op_ibitfield_extract
:
2347 case nir_op_ubitfield_extract
: {
2349 Value
*tmp
= getSSA();
2350 LValues
&newDefs
= convert(&insn
->dest
);
2351 mkOp3(OP_INSBF
, dType
, tmp
, getSrc(&insn
->src
[2]), loadImm(NULL
, 0x808), getSrc(&insn
->src
[1]));
2352 mkOp2(OP_EXTBF
, dType
, newDefs
[0], getSrc(&insn
->src
[0]), tmp
);
2357 LValues
&newDefs
= convert(&insn
->dest
);
2358 mkOp3(OP_INSBF
, dType
, newDefs
[0], getSrc(&insn
->src
[0]), loadImm(NULL
, 0x808), getSrc(&insn
->src
[1]));
2361 case nir_op_bitfield_insert
: {
2363 LValues
&newDefs
= convert(&insn
->dest
);
2364 LValue
*temp
= getSSA();
2365 mkOp3(OP_INSBF
, TYPE_U32
, temp
, getSrc(&insn
->src
[3]), mkImm(0x808), getSrc(&insn
->src
[2]));
2366 mkOp3(OP_INSBF
, dType
, newDefs
[0], getSrc(&insn
->src
[1]), temp
, getSrc(&insn
->src
[0]));
2369 case nir_op_bit_count
: {
2371 LValues
&newDefs
= convert(&insn
->dest
);
2372 mkOp2(OP_POPCNT
, dType
, newDefs
[0], getSrc(&insn
->src
[0]), getSrc(&insn
->src
[0]));
2375 case nir_op_bitfield_reverse
: {
2377 LValues
&newDefs
= convert(&insn
->dest
);
2378 mkOp2(OP_EXTBF
, TYPE_U32
, newDefs
[0], getSrc(&insn
->src
[0]), mkImm(0x2000))->subOp
= NV50_IR_SUBOP_EXTBF_REV
;
2381 case nir_op_find_lsb
: {
2383 LValues
&newDefs
= convert(&insn
->dest
);
2384 Value
*tmp
= getSSA();
2385 mkOp2(OP_EXTBF
, TYPE_U32
, tmp
, getSrc(&insn
->src
[0]), mkImm(0x2000))->subOp
= NV50_IR_SUBOP_EXTBF_REV
;
2386 mkOp1(OP_BFIND
, TYPE_U32
, newDefs
[0], tmp
)->subOp
= NV50_IR_SUBOP_BFIND_SAMT
;
2389 // boolean conversions
2390 case nir_op_b2f32
: {
2392 LValues
&newDefs
= convert(&insn
->dest
);
2393 mkOp2(OP_AND
, TYPE_U32
, newDefs
[0], getSrc(&insn
->src
[0]), loadImm(NULL
, 1.0f
));
2396 case nir_op_b2f64
: {
2398 LValues
&newDefs
= convert(&insn
->dest
);
2399 Value
*tmp
= getSSA(4);
2400 mkOp2(OP_AND
, TYPE_U32
, tmp
, getSrc(&insn
->src
[0]), loadImm(NULL
, 0x3ff00000));
2401 mkOp2(OP_MERGE
, TYPE_U64
, newDefs
[0], loadImm(NULL
, 0), tmp
);
2405 case nir_op_i2b32
: {
2407 LValues
&newDefs
= convert(&insn
->dest
);
2409 if (typeSizeof(sTypes
[0]) == 8) {
2410 src1
= loadImm(getSSA(8), 0.0);
2414 CondCode cc
= op
== nir_op_f2b32
? CC_NEU
: CC_NE
;
2415 mkCmp(OP_SET
, cc
, TYPE_U32
, newDefs
[0], sTypes
[0], getSrc(&insn
->src
[0]), src1
);
2418 case nir_op_b2i32
: {
2420 LValues
&newDefs
= convert(&insn
->dest
);
2421 mkOp2(OP_AND
, TYPE_U32
, newDefs
[0], getSrc(&insn
->src
[0]), loadImm(NULL
, 1));
2424 case nir_op_b2i64
: {
2426 LValues
&newDefs
= convert(&insn
->dest
);
2427 LValue
*def
= getScratch();
2428 mkOp2(OP_AND
, TYPE_U32
, def
, getSrc(&insn
->src
[0]), loadImm(NULL
, 1));
2429 mkOp2(OP_MERGE
, TYPE_S64
, newDefs
[0], def
, loadImm(NULL
, 0));
2433 ERROR("unknown nir_op %s\n", info
.name
);
2438 oldPos
= this->bb
->getEntry();
2439 oldPos
->precise
= insn
->exact
;
2442 if (unlikely(!oldPos
))
2445 while (oldPos
->next
) {
2446 oldPos
= oldPos
->next
;
2447 oldPos
->precise
= insn
->exact
;
2449 oldPos
->saturate
= insn
->dest
.saturate
;
2453 #undef DEFAULT_CHECKS
2456 Converter::visit(nir_ssa_undef_instr
*insn
)
2458 LValues
&newDefs
= convert(&insn
->def
);
2459 for (uint8_t i
= 0u; i
< insn
->def
.num_components
; ++i
) {
2460 mkOp(OP_NOP
, TYPE_NONE
, newDefs
[i
]);
2465 #define CASE_SAMPLER(ty) \
2466 case GLSL_SAMPLER_DIM_ ## ty : \
2467 if (isArray && !isShadow) \
2468 return TEX_TARGET_ ## ty ## _ARRAY; \
2469 else if (!isArray && isShadow) \
2470 return TEX_TARGET_## ty ## _SHADOW; \
2471 else if (isArray && isShadow) \
2472 return TEX_TARGET_## ty ## _ARRAY_SHADOW; \
2474 return TEX_TARGET_ ## ty
2477 Converter::convert(glsl_sampler_dim dim
, bool isArray
, bool isShadow
)
2483 case GLSL_SAMPLER_DIM_3D
:
2484 return TEX_TARGET_3D
;
2485 case GLSL_SAMPLER_DIM_MS
:
2487 return TEX_TARGET_2D_MS_ARRAY
;
2488 return TEX_TARGET_2D_MS
;
2489 case GLSL_SAMPLER_DIM_RECT
:
2491 return TEX_TARGET_RECT_SHADOW
;
2492 return TEX_TARGET_RECT
;
2493 case GLSL_SAMPLER_DIM_BUF
:
2494 return TEX_TARGET_BUFFER
;
2495 case GLSL_SAMPLER_DIM_EXTERNAL
:
2496 return TEX_TARGET_2D
;
2498 ERROR("unknown glsl_sampler_dim %u\n", dim
);
2500 return TEX_TARGET_COUNT
;
2506 Converter::applyProjection(Value
*src
, Value
*proj
)
2510 return mkOp2v(OP_MUL
, TYPE_F32
, getScratch(), src
, proj
);
2514 Converter::visit(nir_tex_instr
*insn
)
2518 case nir_texop_query_levels
:
2520 case nir_texop_texture_samples
:
2525 case nir_texop_txf_ms
:
2527 case nir_texop_txs
: {
2528 LValues
&newDefs
= convert(&insn
->dest
);
2529 std::vector
<Value
*> srcs
;
2530 std::vector
<Value
*> defs
;
2531 std::vector
<nir_src
*> offsets
;
2535 TexInstruction::Target target
= convert(insn
->sampler_dim
, insn
->is_array
, insn
->is_shadow
);
2536 operation op
= getOperation(insn
->op
);
2539 int biasIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_bias
);
2540 int compIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_comparator
);
2541 int coordsIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_coord
);
2542 int ddxIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_ddx
);
2543 int ddyIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_ddy
);
2544 int msIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_ms_index
);
2545 int lodIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_lod
);
2546 int offsetIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_offset
);
2547 int projIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_projector
);
2548 int sampOffIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_sampler_offset
);
2549 int texOffIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_texture_offset
);
2552 proj
= mkOp1v(OP_RCP
, TYPE_F32
, getScratch(), getSrc(&insn
->src
[projIdx
].src
, 0));
2554 srcs
.resize(insn
->coord_components
);
2555 for (uint8_t i
= 0u; i
< insn
->coord_components
; ++i
)
2556 srcs
[i
] = applyProjection(getSrc(&insn
->src
[coordsIdx
].src
, i
), proj
);
2558 // sometimes we get less args than target.getArgCount, but codegen expects the latter
2559 if (insn
->coord_components
) {
2560 uint32_t argCount
= target
.getArgCount();
2565 for (uint32_t i
= 0u; i
< (argCount
- insn
->coord_components
); ++i
)
2566 srcs
.push_back(getSSA());
2569 if (insn
->op
== nir_texop_texture_samples
)
2570 srcs
.push_back(zero
);
2571 else if (!insn
->num_srcs
)
2572 srcs
.push_back(loadImm(NULL
, 0));
2574 srcs
.push_back(getSrc(&insn
->src
[biasIdx
].src
, 0));
2576 srcs
.push_back(getSrc(&insn
->src
[lodIdx
].src
, 0));
2577 else if (op
== OP_TXF
)
2580 srcs
.push_back(getSrc(&insn
->src
[msIdx
].src
, 0));
2581 if (offsetIdx
!= -1)
2582 offsets
.push_back(&insn
->src
[offsetIdx
].src
);
2584 srcs
.push_back(applyProjection(getSrc(&insn
->src
[compIdx
].src
, 0), proj
));
2585 if (texOffIdx
!= -1) {
2586 srcs
.push_back(getSrc(&insn
->src
[texOffIdx
].src
, 0));
2587 texOffIdx
= srcs
.size() - 1;
2589 if (sampOffIdx
!= -1) {
2590 srcs
.push_back(getSrc(&insn
->src
[sampOffIdx
].src
, 0));
2591 sampOffIdx
= srcs
.size() - 1;
2594 r
= insn
->texture_index
;
2595 s
= insn
->sampler_index
;
2597 defs
.resize(newDefs
.size());
2598 for (uint8_t d
= 0u; d
< newDefs
.size(); ++d
) {
2599 defs
[d
] = newDefs
[d
];
2602 if (target
.isMS() || (op
== OP_TEX
&& prog
->getType() != Program::TYPE_FRAGMENT
))
2605 TexInstruction
*texi
= mkTex(op
, target
.getEnum(), r
, s
, defs
, srcs
);
2606 texi
->tex
.levelZero
= lz
;
2607 texi
->tex
.mask
= mask
;
2609 if (texOffIdx
!= -1)
2610 texi
->tex
.rIndirectSrc
= texOffIdx
;
2611 if (sampOffIdx
!= -1)
2612 texi
->tex
.sIndirectSrc
= sampOffIdx
;
2616 if (!target
.isShadow())
2617 texi
->tex
.gatherComp
= insn
->component
;
2620 texi
->tex
.query
= TXQ_DIMS
;
2622 case nir_texop_texture_samples
:
2623 texi
->tex
.mask
= 0x4;
2624 texi
->tex
.query
= TXQ_TYPE
;
2626 case nir_texop_query_levels
:
2627 texi
->tex
.mask
= 0x8;
2628 texi
->tex
.query
= TXQ_DIMS
;
2634 texi
->tex
.useOffsets
= offsets
.size();
2635 if (texi
->tex
.useOffsets
) {
2636 for (uint8_t s
= 0; s
< texi
->tex
.useOffsets
; ++s
) {
2637 for (uint32_t c
= 0u; c
< 3; ++c
) {
2638 uint8_t s2
= std::min(c
, target
.getDim() - 1);
2639 texi
->offset
[s
][c
].set(getSrc(offsets
[s
], s2
));
2640 texi
->offset
[s
][c
].setInsn(texi
);
2645 if (ddxIdx
!= -1 && ddyIdx
!= -1) {
2646 for (uint8_t c
= 0u; c
< target
.getDim() + target
.isCube(); ++c
) {
2647 texi
->dPdx
[c
].set(getSrc(&insn
->src
[ddxIdx
].src
, c
));
2648 texi
->dPdy
[c
].set(getSrc(&insn
->src
[ddyIdx
].src
, c
));
2655 ERROR("unknown nir_texop %u\n", insn
->op
);
2666 if (prog
->dbgFlags
& NV50_IR_DEBUG_VERBOSE
)
2667 nir_print_shader(nir
, stderr
);
2669 struct nir_lower_subgroups_options subgroup_options
= {
2670 .subgroup_size
= 32,
2671 .ballot_bit_size
= 32,
2674 NIR_PASS_V(nir
, nir_lower_io
, nir_var_all
, type_size
, (nir_lower_io_options
)0);
2675 NIR_PASS_V(nir
, nir_lower_subgroups
, &subgroup_options
);
2676 NIR_PASS_V(nir
, nir_lower_regs_to_ssa
);
2677 NIR_PASS_V(nir
, nir_lower_load_const_to_scalar
);
2678 NIR_PASS_V(nir
, nir_lower_vars_to_ssa
);
2679 NIR_PASS_V(nir
, nir_lower_alu_to_scalar
);
2680 NIR_PASS_V(nir
, nir_lower_phis_to_scalar
);
2684 NIR_PASS(progress
, nir
, nir_copy_prop
);
2685 NIR_PASS(progress
, nir
, nir_opt_remove_phis
);
2686 NIR_PASS(progress
, nir
, nir_opt_trivial_continues
);
2687 NIR_PASS(progress
, nir
, nir_opt_cse
);
2688 NIR_PASS(progress
, nir
, nir_opt_algebraic
);
2689 NIR_PASS(progress
, nir
, nir_opt_constant_folding
);
2690 NIR_PASS(progress
, nir
, nir_copy_prop
);
2691 NIR_PASS(progress
, nir
, nir_opt_dce
);
2692 NIR_PASS(progress
, nir
, nir_opt_dead_cf
);
2695 NIR_PASS_V(nir
, nir_lower_bool_to_int32
);
2696 NIR_PASS_V(nir
, nir_lower_locals_to_regs
);
2697 NIR_PASS_V(nir
, nir_remove_dead_variables
, nir_var_function_temp
);
2698 NIR_PASS_V(nir
, nir_convert_from_ssa
, true);
2700 // Garbage collect dead instructions
2704 ERROR("Couldn't prase NIR!\n");
2708 if (!assignSlots()) {
2709 ERROR("Couldn't assign slots!\n");
2713 if (prog
->dbgFlags
& NV50_IR_DEBUG_BASIC
)
2714 nir_print_shader(nir
, stderr
);
2716 nir_foreach_function(function
, nir
) {
2717 if (!visit(function
))
2724 } // unnamed namespace
2729 Program::makeFromNIR(struct nv50_ir_prog_info
*info
)
2731 nir_shader
*nir
= (nir_shader
*)info
->bin
.source
;
2732 Converter
converter(this, nir
, info
);
2733 bool result
= converter
.run();
2736 LoweringHelper lowering
;
2738 tlsSize
= info
->bin
.tlsSpace
;
2742 } // namespace nv50_ir