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
)
468 case nir_intrinsic_emit_vertex
:
470 case nir_intrinsic_end_primitive
:
473 ERROR("couldn't get operation for nir_intrinsic_op %u\n", op
);
480 Converter::preOperationNeeded(nir_op op
)
492 Converter::getSubOp(nir_op op
)
495 case nir_op_imul_high
:
496 case nir_op_umul_high
:
497 return NV50_IR_SUBOP_MUL_HIGH
;
504 Converter::getSubOp(nir_intrinsic_op op
)
507 case nir_intrinsic_vote_all
:
508 return NV50_IR_SUBOP_VOTE_ALL
;
509 case nir_intrinsic_vote_any
:
510 return NV50_IR_SUBOP_VOTE_ANY
;
511 case nir_intrinsic_vote_ieq
:
512 return NV50_IR_SUBOP_VOTE_UNI
;
519 Converter::getCondCode(nir_op op
)
538 ERROR("couldn't get CondCode for op %s\n", nir_op_infos
[op
].name
);
545 Converter::convert(nir_alu_dest
*dest
)
547 return convert(&dest
->dest
);
551 Converter::convert(nir_dest
*dest
)
554 return convert(&dest
->ssa
);
555 if (dest
->reg
.indirect
) {
556 ERROR("no support for indirects.");
559 return convert(dest
->reg
.reg
);
563 Converter::convert(nir_register
*reg
)
565 NirDefMap::iterator it
= regDefs
.find(reg
->index
);
566 if (it
!= regDefs
.end())
569 LValues
newDef(reg
->num_components
);
570 for (uint8_t i
= 0; i
< reg
->num_components
; i
++)
571 newDef
[i
] = getScratch(std::max(4, reg
->bit_size
/ 8));
572 return regDefs
[reg
->index
] = newDef
;
576 Converter::convert(nir_ssa_def
*def
)
578 NirDefMap::iterator it
= ssaDefs
.find(def
->index
);
579 if (it
!= ssaDefs
.end())
582 LValues
newDef(def
->num_components
);
583 for (uint8_t i
= 0; i
< def
->num_components
; i
++)
584 newDef
[i
] = getSSA(std::max(4, def
->bit_size
/ 8));
585 return ssaDefs
[def
->index
] = newDef
;
589 Converter::getSrc(nir_alu_src
*src
, uint8_t component
)
591 if (src
->abs
|| src
->negate
) {
592 ERROR("modifiers currently not supported on nir_alu_src\n");
595 return getSrc(&src
->src
, src
->swizzle
[component
]);
599 Converter::getSrc(nir_register
*reg
, uint8_t idx
)
601 NirDefMap::iterator it
= regDefs
.find(reg
->index
);
602 if (it
== regDefs
.end())
603 return convert(reg
)[idx
];
604 return it
->second
[idx
];
608 Converter::getSrc(nir_src
*src
, uint8_t idx
, bool indirect
)
611 return getSrc(src
->ssa
, idx
);
613 if (src
->reg
.indirect
) {
615 return getSrc(src
->reg
.indirect
, idx
);
616 ERROR("no support for indirects.");
621 return getSrc(src
->reg
.reg
, idx
);
625 Converter::getSrc(nir_ssa_def
*src
, uint8_t idx
)
627 NirDefMap::iterator it
= ssaDefs
.find(src
->index
);
628 if (it
== ssaDefs
.end()) {
629 ERROR("SSA value %u not found\n", src
->index
);
633 return it
->second
[idx
];
637 Converter::getIndirect(nir_src
*src
, uint8_t idx
, Value
*&indirect
)
639 nir_const_value
*offset
= nir_src_as_const_value(*src
);
643 return offset
->u32
[0];
646 indirect
= getSrc(src
, idx
, true);
651 Converter::getIndirect(nir_intrinsic_instr
*insn
, uint8_t s
, uint8_t c
, Value
*&indirect
)
653 int32_t idx
= nir_intrinsic_base(insn
) + getIndirect(&insn
->src
[s
], c
, indirect
);
655 indirect
= mkOp2v(OP_SHL
, TYPE_U32
, getSSA(4, FILE_ADDRESS
), indirect
, loadImm(NULL
, 4));
660 vert_attrib_to_tgsi_semantic(gl_vert_attrib slot
, unsigned *name
, unsigned *index
)
662 assert(name
&& index
);
664 if (slot
>= VERT_ATTRIB_MAX
) {
665 ERROR("invalid varying slot %u\n", slot
);
670 if (slot
>= VERT_ATTRIB_GENERIC0
&&
671 slot
< VERT_ATTRIB_GENERIC0
+ VERT_ATTRIB_GENERIC_MAX
) {
672 *name
= TGSI_SEMANTIC_GENERIC
;
673 *index
= slot
- VERT_ATTRIB_GENERIC0
;
677 if (slot
>= VERT_ATTRIB_TEX0
&&
678 slot
< VERT_ATTRIB_TEX0
+ VERT_ATTRIB_TEX_MAX
) {
679 *name
= TGSI_SEMANTIC_TEXCOORD
;
680 *index
= slot
- VERT_ATTRIB_TEX0
;
685 case VERT_ATTRIB_COLOR0
:
686 *name
= TGSI_SEMANTIC_COLOR
;
689 case VERT_ATTRIB_COLOR1
:
690 *name
= TGSI_SEMANTIC_COLOR
;
693 case VERT_ATTRIB_EDGEFLAG
:
694 *name
= TGSI_SEMANTIC_EDGEFLAG
;
697 case VERT_ATTRIB_FOG
:
698 *name
= TGSI_SEMANTIC_FOG
;
701 case VERT_ATTRIB_NORMAL
:
702 *name
= TGSI_SEMANTIC_NORMAL
;
705 case VERT_ATTRIB_POS
:
706 *name
= TGSI_SEMANTIC_POSITION
;
709 case VERT_ATTRIB_POINT_SIZE
:
710 *name
= TGSI_SEMANTIC_PSIZE
;
714 ERROR("unknown vert attrib slot %u\n", slot
);
721 varying_slot_to_tgsi_semantic(gl_varying_slot slot
, unsigned *name
, unsigned *index
)
723 assert(name
&& index
);
725 if (slot
>= VARYING_SLOT_TESS_MAX
) {
726 ERROR("invalid varying slot %u\n", slot
);
731 if (slot
>= VARYING_SLOT_PATCH0
) {
732 *name
= TGSI_SEMANTIC_PATCH
;
733 *index
= slot
- VARYING_SLOT_PATCH0
;
737 if (slot
>= VARYING_SLOT_VAR0
) {
738 *name
= TGSI_SEMANTIC_GENERIC
;
739 *index
= slot
- VARYING_SLOT_VAR0
;
743 if (slot
>= VARYING_SLOT_TEX0
&& slot
<= VARYING_SLOT_TEX7
) {
744 *name
= TGSI_SEMANTIC_TEXCOORD
;
745 *index
= slot
- VARYING_SLOT_TEX0
;
750 case VARYING_SLOT_BFC0
:
751 *name
= TGSI_SEMANTIC_BCOLOR
;
754 case VARYING_SLOT_BFC1
:
755 *name
= TGSI_SEMANTIC_BCOLOR
;
758 case VARYING_SLOT_CLIP_DIST0
:
759 *name
= TGSI_SEMANTIC_CLIPDIST
;
762 case VARYING_SLOT_CLIP_DIST1
:
763 *name
= TGSI_SEMANTIC_CLIPDIST
;
766 case VARYING_SLOT_CLIP_VERTEX
:
767 *name
= TGSI_SEMANTIC_CLIPVERTEX
;
770 case VARYING_SLOT_COL0
:
771 *name
= TGSI_SEMANTIC_COLOR
;
774 case VARYING_SLOT_COL1
:
775 *name
= TGSI_SEMANTIC_COLOR
;
778 case VARYING_SLOT_EDGE
:
779 *name
= TGSI_SEMANTIC_EDGEFLAG
;
782 case VARYING_SLOT_FACE
:
783 *name
= TGSI_SEMANTIC_FACE
;
786 case VARYING_SLOT_FOGC
:
787 *name
= TGSI_SEMANTIC_FOG
;
790 case VARYING_SLOT_LAYER
:
791 *name
= TGSI_SEMANTIC_LAYER
;
794 case VARYING_SLOT_PNTC
:
795 *name
= TGSI_SEMANTIC_PCOORD
;
798 case VARYING_SLOT_POS
:
799 *name
= TGSI_SEMANTIC_POSITION
;
802 case VARYING_SLOT_PRIMITIVE_ID
:
803 *name
= TGSI_SEMANTIC_PRIMID
;
806 case VARYING_SLOT_PSIZ
:
807 *name
= TGSI_SEMANTIC_PSIZE
;
810 case VARYING_SLOT_TESS_LEVEL_INNER
:
811 *name
= TGSI_SEMANTIC_TESSINNER
;
814 case VARYING_SLOT_TESS_LEVEL_OUTER
:
815 *name
= TGSI_SEMANTIC_TESSOUTER
;
818 case VARYING_SLOT_VIEWPORT
:
819 *name
= TGSI_SEMANTIC_VIEWPORT_INDEX
;
823 ERROR("unknown varying slot %u\n", slot
);
830 frag_result_to_tgsi_semantic(unsigned slot
, unsigned *name
, unsigned *index
)
832 if (slot
>= FRAG_RESULT_DATA0
) {
833 *name
= TGSI_SEMANTIC_COLOR
;
834 *index
= slot
- FRAG_RESULT_COLOR
- 2; // intentional
839 case FRAG_RESULT_COLOR
:
840 *name
= TGSI_SEMANTIC_COLOR
;
843 case FRAG_RESULT_DEPTH
:
844 *name
= TGSI_SEMANTIC_POSITION
;
847 case FRAG_RESULT_SAMPLE_MASK
:
848 *name
= TGSI_SEMANTIC_SAMPLEMASK
;
852 ERROR("unknown frag result slot %u\n", slot
);
858 // copy of _mesa_sysval_to_semantic
860 system_val_to_tgsi_semantic(unsigned val
, unsigned *name
, unsigned *index
)
865 case SYSTEM_VALUE_VERTEX_ID
:
866 *name
= TGSI_SEMANTIC_VERTEXID
;
868 case SYSTEM_VALUE_INSTANCE_ID
:
869 *name
= TGSI_SEMANTIC_INSTANCEID
;
871 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
:
872 *name
= TGSI_SEMANTIC_VERTEXID_NOBASE
;
874 case SYSTEM_VALUE_BASE_VERTEX
:
875 *name
= TGSI_SEMANTIC_BASEVERTEX
;
877 case SYSTEM_VALUE_BASE_INSTANCE
:
878 *name
= TGSI_SEMANTIC_BASEINSTANCE
;
880 case SYSTEM_VALUE_DRAW_ID
:
881 *name
= TGSI_SEMANTIC_DRAWID
;
885 case SYSTEM_VALUE_INVOCATION_ID
:
886 *name
= TGSI_SEMANTIC_INVOCATIONID
;
890 case SYSTEM_VALUE_FRAG_COORD
:
891 *name
= TGSI_SEMANTIC_POSITION
;
893 case SYSTEM_VALUE_FRONT_FACE
:
894 *name
= TGSI_SEMANTIC_FACE
;
896 case SYSTEM_VALUE_SAMPLE_ID
:
897 *name
= TGSI_SEMANTIC_SAMPLEID
;
899 case SYSTEM_VALUE_SAMPLE_POS
:
900 *name
= TGSI_SEMANTIC_SAMPLEPOS
;
902 case SYSTEM_VALUE_SAMPLE_MASK_IN
:
903 *name
= TGSI_SEMANTIC_SAMPLEMASK
;
905 case SYSTEM_VALUE_HELPER_INVOCATION
:
906 *name
= TGSI_SEMANTIC_HELPER_INVOCATION
;
909 // Tessellation shader
910 case SYSTEM_VALUE_TESS_COORD
:
911 *name
= TGSI_SEMANTIC_TESSCOORD
;
913 case SYSTEM_VALUE_VERTICES_IN
:
914 *name
= TGSI_SEMANTIC_VERTICESIN
;
916 case SYSTEM_VALUE_PRIMITIVE_ID
:
917 *name
= TGSI_SEMANTIC_PRIMID
;
919 case SYSTEM_VALUE_TESS_LEVEL_OUTER
:
920 *name
= TGSI_SEMANTIC_TESSOUTER
;
922 case SYSTEM_VALUE_TESS_LEVEL_INNER
:
923 *name
= TGSI_SEMANTIC_TESSINNER
;
927 case SYSTEM_VALUE_LOCAL_INVOCATION_ID
:
928 *name
= TGSI_SEMANTIC_THREAD_ID
;
930 case SYSTEM_VALUE_WORK_GROUP_ID
:
931 *name
= TGSI_SEMANTIC_BLOCK_ID
;
933 case SYSTEM_VALUE_NUM_WORK_GROUPS
:
934 *name
= TGSI_SEMANTIC_GRID_SIZE
;
936 case SYSTEM_VALUE_LOCAL_GROUP_SIZE
:
937 *name
= TGSI_SEMANTIC_BLOCK_SIZE
;
941 case SYSTEM_VALUE_SUBGROUP_SIZE
:
942 *name
= TGSI_SEMANTIC_SUBGROUP_SIZE
;
944 case SYSTEM_VALUE_SUBGROUP_INVOCATION
:
945 *name
= TGSI_SEMANTIC_SUBGROUP_INVOCATION
;
947 case SYSTEM_VALUE_SUBGROUP_EQ_MASK
:
948 *name
= TGSI_SEMANTIC_SUBGROUP_EQ_MASK
;
950 case SYSTEM_VALUE_SUBGROUP_GE_MASK
:
951 *name
= TGSI_SEMANTIC_SUBGROUP_GE_MASK
;
953 case SYSTEM_VALUE_SUBGROUP_GT_MASK
:
954 *name
= TGSI_SEMANTIC_SUBGROUP_GT_MASK
;
956 case SYSTEM_VALUE_SUBGROUP_LE_MASK
:
957 *name
= TGSI_SEMANTIC_SUBGROUP_LE_MASK
;
959 case SYSTEM_VALUE_SUBGROUP_LT_MASK
:
960 *name
= TGSI_SEMANTIC_SUBGROUP_LT_MASK
;
964 ERROR("unknown system value %u\n", val
);
971 Converter::setInterpolate(nv50_ir_varying
*var
,
977 case INTERP_MODE_FLAT
:
980 case INTERP_MODE_NONE
:
981 if (semantic
== TGSI_SEMANTIC_COLOR
)
983 else if (semantic
== TGSI_SEMANTIC_POSITION
)
986 case INTERP_MODE_NOPERSPECTIVE
:
989 case INTERP_MODE_SMOOTH
:
992 var
->centroid
= centroid
;
996 calcSlots(const glsl_type
*type
, Program::Type stage
, const shader_info
&info
,
997 bool input
, const nir_variable
*var
)
999 if (!type
->is_array())
1000 return type
->count_attribute_slots(false);
1004 case Program::TYPE_GEOMETRY
:
1005 slots
= type
->uniform_locations();
1007 slots
/= info
.gs
.vertices_in
;
1009 case Program::TYPE_TESSELLATION_CONTROL
:
1010 case Program::TYPE_TESSELLATION_EVAL
:
1011 // remove first dimension
1012 if (var
->data
.patch
|| (!input
&& stage
== Program::TYPE_TESSELLATION_EVAL
))
1013 slots
= type
->uniform_locations();
1015 slots
= type
->fields
.array
->uniform_locations();
1018 slots
= type
->count_attribute_slots(false);
1025 bool Converter::assignSlots() {
1029 info
->io
.viewportId
= -1;
1030 info
->numInputs
= 0;
1032 // we have to fixup the uniform locations for arrays
1033 unsigned numImages
= 0;
1034 nir_foreach_variable(var
, &nir
->uniforms
) {
1035 const glsl_type
*type
= var
->type
;
1036 if (!type
->without_array()->is_image())
1038 var
->data
.driver_location
= numImages
;
1039 numImages
+= type
->is_array() ? type
->arrays_of_arrays_size() : 1;
1042 nir_foreach_variable(var
, &nir
->inputs
) {
1043 const glsl_type
*type
= var
->type
;
1044 int slot
= var
->data
.location
;
1045 uint16_t slots
= calcSlots(type
, prog
->getType(), nir
->info
, true, var
);
1046 uint32_t comp
= type
->is_array() ? type
->without_array()->component_slots()
1047 : type
->component_slots();
1048 uint32_t frac
= var
->data
.location_frac
;
1049 uint32_t vary
= var
->data
.driver_location
;
1051 if (glsl_base_type_is_64bit(type
->without_array()->base_type
)) {
1056 assert(vary
+ slots
<= PIPE_MAX_SHADER_INPUTS
);
1058 switch(prog
->getType()) {
1059 case Program::TYPE_FRAGMENT
:
1060 varying_slot_to_tgsi_semantic((gl_varying_slot
)slot
, &name
, &index
);
1061 for (uint16_t i
= 0; i
< slots
; ++i
) {
1062 setInterpolate(&info
->in
[vary
+ i
], var
->data
.interpolation
,
1063 var
->data
.centroid
| var
->data
.sample
, name
);
1066 case Program::TYPE_GEOMETRY
:
1067 varying_slot_to_tgsi_semantic((gl_varying_slot
)slot
, &name
, &index
);
1069 case Program::TYPE_TESSELLATION_CONTROL
:
1070 case Program::TYPE_TESSELLATION_EVAL
:
1071 varying_slot_to_tgsi_semantic((gl_varying_slot
)slot
, &name
, &index
);
1072 if (var
->data
.patch
&& name
== TGSI_SEMANTIC_PATCH
)
1073 info
->numPatchConstants
= MAX2(info
->numPatchConstants
, index
+ slots
);
1075 case Program::TYPE_VERTEX
:
1076 vert_attrib_to_tgsi_semantic((gl_vert_attrib
)slot
, &name
, &index
);
1078 case TGSI_SEMANTIC_EDGEFLAG
:
1079 info
->io
.edgeFlagIn
= vary
;
1086 ERROR("unknown shader type %u in assignSlots\n", prog
->getType());
1090 for (uint16_t i
= 0u; i
< slots
; ++i
, ++vary
) {
1091 info
->in
[vary
].id
= vary
;
1092 info
->in
[vary
].patch
= var
->data
.patch
;
1093 info
->in
[vary
].sn
= name
;
1094 info
->in
[vary
].si
= index
+ i
;
1095 if (glsl_base_type_is_64bit(type
->without_array()->base_type
))
1097 info
->in
[vary
].mask
|= (((1 << (comp
* 2)) - 1) << (frac
* 2) >> 0x4);
1099 info
->in
[vary
].mask
|= (((1 << (comp
* 2)) - 1) << (frac
* 2) & 0xf);
1101 info
->in
[vary
].mask
|= ((1 << comp
) - 1) << frac
;
1103 info
->numInputs
= std::max
<uint8_t>(info
->numInputs
, vary
);
1106 info
->numOutputs
= 0;
1107 nir_foreach_variable(var
, &nir
->outputs
) {
1108 const glsl_type
*type
= var
->type
;
1109 int slot
= var
->data
.location
;
1110 uint16_t slots
= calcSlots(type
, prog
->getType(), nir
->info
, false, var
);
1111 uint32_t comp
= type
->is_array() ? type
->without_array()->component_slots()
1112 : type
->component_slots();
1113 uint32_t frac
= var
->data
.location_frac
;
1114 uint32_t vary
= var
->data
.driver_location
;
1116 if (glsl_base_type_is_64bit(type
->without_array()->base_type
)) {
1121 assert(vary
< PIPE_MAX_SHADER_OUTPUTS
);
1123 switch(prog
->getType()) {
1124 case Program::TYPE_FRAGMENT
:
1125 frag_result_to_tgsi_semantic((gl_frag_result
)slot
, &name
, &index
);
1127 case TGSI_SEMANTIC_COLOR
:
1128 if (!var
->data
.fb_fetch_output
)
1129 info
->prop
.fp
.numColourResults
++;
1130 info
->prop
.fp
.separateFragData
= true;
1131 // sometimes we get FRAG_RESULT_DATAX with data.index 0
1132 // sometimes we get FRAG_RESULT_DATA0 with data.index X
1133 index
= index
== 0 ? var
->data
.index
: index
;
1135 case TGSI_SEMANTIC_POSITION
:
1136 info
->io
.fragDepth
= vary
;
1137 info
->prop
.fp
.writesDepth
= true;
1139 case TGSI_SEMANTIC_SAMPLEMASK
:
1140 info
->io
.sampleMask
= vary
;
1146 case Program::TYPE_GEOMETRY
:
1147 case Program::TYPE_TESSELLATION_CONTROL
:
1148 case Program::TYPE_TESSELLATION_EVAL
:
1149 case Program::TYPE_VERTEX
:
1150 varying_slot_to_tgsi_semantic((gl_varying_slot
)slot
, &name
, &index
);
1152 if (var
->data
.patch
&& name
!= TGSI_SEMANTIC_TESSINNER
&&
1153 name
!= TGSI_SEMANTIC_TESSOUTER
)
1154 info
->numPatchConstants
= MAX2(info
->numPatchConstants
, index
+ slots
);
1157 case TGSI_SEMANTIC_CLIPDIST
:
1158 info
->io
.genUserClip
= -1;
1160 case TGSI_SEMANTIC_CLIPVERTEX
:
1161 clipVertexOutput
= vary
;
1163 case TGSI_SEMANTIC_EDGEFLAG
:
1164 info
->io
.edgeFlagOut
= vary
;
1166 case TGSI_SEMANTIC_POSITION
:
1167 if (clipVertexOutput
< 0)
1168 clipVertexOutput
= vary
;
1175 ERROR("unknown shader type %u in assignSlots\n", prog
->getType());
1179 for (uint16_t i
= 0u; i
< slots
; ++i
, ++vary
) {
1180 info
->out
[vary
].id
= vary
;
1181 info
->out
[vary
].patch
= var
->data
.patch
;
1182 info
->out
[vary
].sn
= name
;
1183 info
->out
[vary
].si
= index
+ i
;
1184 if (glsl_base_type_is_64bit(type
->without_array()->base_type
))
1186 info
->out
[vary
].mask
|= (((1 << (comp
* 2)) - 1) << (frac
* 2) >> 0x4);
1188 info
->out
[vary
].mask
|= (((1 << (comp
* 2)) - 1) << (frac
* 2) & 0xf);
1190 info
->out
[vary
].mask
|= ((1 << comp
) - 1) << frac
;
1192 if (nir
->info
.outputs_read
& 1ll << slot
)
1193 info
->out
[vary
].oread
= 1;
1195 info
->numOutputs
= std::max
<uint8_t>(info
->numOutputs
, vary
);
1198 info
->numSysVals
= 0;
1199 for (uint8_t i
= 0; i
< 64; ++i
) {
1200 if (!(nir
->info
.system_values_read
& 1ll << i
))
1203 system_val_to_tgsi_semantic(i
, &name
, &index
);
1204 info
->sv
[info
->numSysVals
].sn
= name
;
1205 info
->sv
[info
->numSysVals
].si
= index
;
1206 info
->sv
[info
->numSysVals
].input
= 0; // TODO inferSysValDirection(sn);
1209 case SYSTEM_VALUE_INSTANCE_ID
:
1210 info
->io
.instanceId
= info
->numSysVals
;
1212 case SYSTEM_VALUE_TESS_LEVEL_INNER
:
1213 case SYSTEM_VALUE_TESS_LEVEL_OUTER
:
1214 info
->sv
[info
->numSysVals
].patch
= 1;
1216 case SYSTEM_VALUE_VERTEX_ID
:
1217 info
->io
.vertexId
= info
->numSysVals
;
1223 info
->numSysVals
+= 1;
1226 if (info
->io
.genUserClip
> 0) {
1227 info
->io
.clipDistances
= info
->io
.genUserClip
;
1229 const unsigned int nOut
= (info
->io
.genUserClip
+ 3) / 4;
1231 for (unsigned int n
= 0; n
< nOut
; ++n
) {
1232 unsigned int i
= info
->numOutputs
++;
1233 info
->out
[i
].id
= i
;
1234 info
->out
[i
].sn
= TGSI_SEMANTIC_CLIPDIST
;
1235 info
->out
[i
].si
= n
;
1236 info
->out
[i
].mask
= ((1 << info
->io
.clipDistances
) - 1) >> (n
* 4);
1240 return info
->assignSlots(info
) == 0;
1244 Converter::getSlotAddress(nir_intrinsic_instr
*insn
, uint8_t idx
, uint8_t slot
)
1247 int offset
= nir_intrinsic_component(insn
);
1250 if (nir_intrinsic_infos
[insn
->intrinsic
].has_dest
)
1251 ty
= getDType(insn
);
1253 ty
= getSType(insn
->src
[0], false, false);
1255 switch (insn
->intrinsic
) {
1256 case nir_intrinsic_load_input
:
1257 case nir_intrinsic_load_interpolated_input
:
1258 case nir_intrinsic_load_per_vertex_input
:
1261 case nir_intrinsic_load_output
:
1262 case nir_intrinsic_load_per_vertex_output
:
1263 case nir_intrinsic_store_output
:
1264 case nir_intrinsic_store_per_vertex_output
:
1268 ERROR("unknown intrinsic in getSlotAddress %s",
1269 nir_intrinsic_infos
[insn
->intrinsic
].name
);
1275 if (typeSizeof(ty
) == 8) {
1287 assert(!input
|| idx
< PIPE_MAX_SHADER_INPUTS
);
1288 assert(input
|| idx
< PIPE_MAX_SHADER_OUTPUTS
);
1290 const nv50_ir_varying
*vary
= input
? info
->in
: info
->out
;
1291 return vary
[idx
].slot
[slot
] * 4;
1295 Converter::loadFrom(DataFile file
, uint8_t i
, DataType ty
, Value
*def
,
1296 uint32_t base
, uint8_t c
, Value
*indirect0
,
1297 Value
*indirect1
, bool patch
)
1299 unsigned int tySize
= typeSizeof(ty
);
1302 (file
== FILE_MEMORY_CONST
|| file
== FILE_MEMORY_BUFFER
|| indirect0
)) {
1303 Value
*lo
= getSSA();
1304 Value
*hi
= getSSA();
1307 mkLoad(TYPE_U32
, lo
,
1308 mkSymbol(file
, i
, TYPE_U32
, base
+ c
* tySize
),
1310 loi
->setIndirect(0, 1, indirect1
);
1311 loi
->perPatch
= patch
;
1314 mkLoad(TYPE_U32
, hi
,
1315 mkSymbol(file
, i
, TYPE_U32
, base
+ c
* tySize
+ 4),
1317 hii
->setIndirect(0, 1, indirect1
);
1318 hii
->perPatch
= patch
;
1320 return mkOp2(OP_MERGE
, ty
, def
, lo
, hi
);
1323 mkLoad(ty
, def
, mkSymbol(file
, i
, ty
, base
+ c
* tySize
), indirect0
);
1324 ld
->setIndirect(0, 1, indirect1
);
1325 ld
->perPatch
= patch
;
1331 Converter::storeTo(nir_intrinsic_instr
*insn
, DataFile file
, operation op
,
1332 DataType ty
, Value
*src
, uint8_t idx
, uint8_t c
,
1333 Value
*indirect0
, Value
*indirect1
)
1335 uint8_t size
= typeSizeof(ty
);
1336 uint32_t address
= getSlotAddress(insn
, idx
, c
);
1338 if (size
== 8 && indirect0
) {
1340 mkSplit(split
, 4, src
);
1342 if (op
== OP_EXPORT
) {
1343 split
[0] = mkMov(getSSA(), split
[0], ty
)->getDef(0);
1344 split
[1] = mkMov(getSSA(), split
[1], ty
)->getDef(0);
1347 mkStore(op
, TYPE_U32
, mkSymbol(file
, 0, TYPE_U32
, address
), indirect0
,
1348 split
[0])->perPatch
= info
->out
[idx
].patch
;
1349 mkStore(op
, TYPE_U32
, mkSymbol(file
, 0, TYPE_U32
, address
+ 4), indirect0
,
1350 split
[1])->perPatch
= info
->out
[idx
].patch
;
1352 if (op
== OP_EXPORT
)
1353 src
= mkMov(getSSA(size
), src
, ty
)->getDef(0);
1354 mkStore(op
, ty
, mkSymbol(file
, 0, ty
, address
), indirect0
,
1355 src
)->perPatch
= info
->out
[idx
].patch
;
1360 Converter::parseNIR()
1362 info
->bin
.tlsSpace
= 0;
1363 info
->io
.clipDistances
= nir
->info
.clip_distance_array_size
;
1364 info
->io
.cullDistances
= nir
->info
.cull_distance_array_size
;
1366 switch(prog
->getType()) {
1367 case Program::TYPE_COMPUTE
:
1368 info
->prop
.cp
.numThreads
[0] = nir
->info
.cs
.local_size
[0];
1369 info
->prop
.cp
.numThreads
[1] = nir
->info
.cs
.local_size
[1];
1370 info
->prop
.cp
.numThreads
[2] = nir
->info
.cs
.local_size
[2];
1371 info
->bin
.smemSize
= nir
->info
.cs
.shared_size
;
1373 case Program::TYPE_FRAGMENT
:
1374 info
->prop
.fp
.earlyFragTests
= nir
->info
.fs
.early_fragment_tests
;
1375 info
->prop
.fp
.persampleInvocation
=
1376 (nir
->info
.system_values_read
& SYSTEM_BIT_SAMPLE_ID
) ||
1377 (nir
->info
.system_values_read
& SYSTEM_BIT_SAMPLE_POS
);
1378 info
->prop
.fp
.postDepthCoverage
= nir
->info
.fs
.post_depth_coverage
;
1379 info
->prop
.fp
.readsSampleLocations
=
1380 (nir
->info
.system_values_read
& SYSTEM_BIT_SAMPLE_POS
);
1381 info
->prop
.fp
.usesDiscard
= nir
->info
.fs
.uses_discard
;
1382 info
->prop
.fp
.usesSampleMaskIn
=
1383 !!(nir
->info
.system_values_read
& SYSTEM_BIT_SAMPLE_MASK_IN
);
1385 case Program::TYPE_GEOMETRY
:
1386 info
->prop
.gp
.inputPrim
= nir
->info
.gs
.input_primitive
;
1387 info
->prop
.gp
.instanceCount
= nir
->info
.gs
.invocations
;
1388 info
->prop
.gp
.maxVertices
= nir
->info
.gs
.vertices_out
;
1389 info
->prop
.gp
.outputPrim
= nir
->info
.gs
.output_primitive
;
1391 case Program::TYPE_TESSELLATION_CONTROL
:
1392 case Program::TYPE_TESSELLATION_EVAL
:
1393 if (nir
->info
.tess
.primitive_mode
== GL_ISOLINES
)
1394 info
->prop
.tp
.domain
= GL_LINES
;
1396 info
->prop
.tp
.domain
= nir
->info
.tess
.primitive_mode
;
1397 info
->prop
.tp
.outputPatchSize
= nir
->info
.tess
.tcs_vertices_out
;
1398 info
->prop
.tp
.outputPrim
=
1399 nir
->info
.tess
.point_mode
? PIPE_PRIM_POINTS
: PIPE_PRIM_TRIANGLES
;
1400 info
->prop
.tp
.partitioning
= (nir
->info
.tess
.spacing
+ 1) % 3;
1401 info
->prop
.tp
.winding
= !nir
->info
.tess
.ccw
;
1403 case Program::TYPE_VERTEX
:
1404 info
->prop
.vp
.usesDrawParameters
=
1405 (nir
->info
.system_values_read
& BITFIELD64_BIT(SYSTEM_VALUE_BASE_VERTEX
)) ||
1406 (nir
->info
.system_values_read
& BITFIELD64_BIT(SYSTEM_VALUE_BASE_INSTANCE
)) ||
1407 (nir
->info
.system_values_read
& BITFIELD64_BIT(SYSTEM_VALUE_DRAW_ID
));
1417 Converter::visit(nir_function
*function
)
1419 // we only support emiting the main function for now
1420 assert(!strcmp(function
->name
, "main"));
1421 assert(function
->impl
);
1423 // usually the blocks will set everything up, but main is special
1424 BasicBlock
*entry
= new BasicBlock(prog
->main
);
1425 exit
= new BasicBlock(prog
->main
);
1426 blocks
[nir_start_block(function
->impl
)->index
] = entry
;
1427 prog
->main
->setEntry(entry
);
1428 prog
->main
->setExit(exit
);
1430 setPosition(entry
, true);
1432 if (info
->io
.genUserClip
> 0) {
1433 for (int c
= 0; c
< 4; ++c
)
1434 clipVtx
[c
] = getScratch();
1437 switch (prog
->getType()) {
1438 case Program::TYPE_TESSELLATION_CONTROL
:
1440 OP_SUB
, TYPE_U32
, getSSA(),
1441 mkOp1v(OP_RDSV
, TYPE_U32
, getSSA(), mkSysVal(SV_LANEID
, 0)),
1442 mkOp1v(OP_RDSV
, TYPE_U32
, getSSA(), mkSysVal(SV_INVOCATION_ID
, 0)));
1444 case Program::TYPE_FRAGMENT
: {
1445 Symbol
*sv
= mkSysVal(SV_POSITION
, 3);
1446 fragCoord
[3] = mkOp1v(OP_RDSV
, TYPE_F32
, getSSA(), sv
);
1447 fp
.position
= mkOp1v(OP_RCP
, TYPE_F32
, fragCoord
[3], fragCoord
[3]);
1454 nir_foreach_register(reg
, &function
->impl
->registers
) {
1455 if (reg
->num_array_elems
) {
1456 // TODO: packed variables would be nice, but MemoryOpt fails
1457 // replace 4 with reg->num_components
1458 uint32_t size
= 4 * reg
->num_array_elems
* (reg
->bit_size
/ 8);
1459 regToLmemOffset
[reg
->index
] = info
->bin
.tlsSpace
;
1460 info
->bin
.tlsSpace
+= size
;
1464 nir_index_ssa_defs(function
->impl
);
1465 foreach_list_typed(nir_cf_node
, node
, node
, &function
->impl
->body
) {
1470 bb
->cfg
.attach(&exit
->cfg
, Graph::Edge::TREE
);
1471 setPosition(exit
, true);
1473 if (info
->io
.genUserClip
> 0)
1474 handleUserClipPlanes();
1476 // TODO: for non main function this needs to be a OP_RETURN
1477 mkOp(OP_EXIT
, TYPE_NONE
, NULL
)->terminator
= 1;
1482 Converter::visit(nir_cf_node
*node
)
1484 switch (node
->type
) {
1485 case nir_cf_node_block
:
1486 return visit(nir_cf_node_as_block(node
));
1487 case nir_cf_node_if
:
1488 return visit(nir_cf_node_as_if(node
));
1489 case nir_cf_node_loop
:
1490 return visit(nir_cf_node_as_loop(node
));
1492 ERROR("unknown nir_cf_node type %u\n", node
->type
);
1498 Converter::visit(nir_block
*block
)
1500 if (!block
->predecessors
->entries
&& block
->instr_list
.is_empty())
1503 BasicBlock
*bb
= convert(block
);
1505 setPosition(bb
, true);
1506 nir_foreach_instr(insn
, block
) {
1514 Converter::visit(nir_if
*nif
)
1516 DataType sType
= getSType(nif
->condition
, false, false);
1517 Value
*src
= getSrc(&nif
->condition
, 0);
1519 nir_block
*lastThen
= nir_if_last_then_block(nif
);
1520 nir_block
*lastElse
= nir_if_last_else_block(nif
);
1522 assert(!lastThen
->successors
[1]);
1523 assert(!lastElse
->successors
[1]);
1525 BasicBlock
*ifBB
= convert(nir_if_first_then_block(nif
));
1526 BasicBlock
*elseBB
= convert(nir_if_first_else_block(nif
));
1528 bb
->cfg
.attach(&ifBB
->cfg
, Graph::Edge::TREE
);
1529 bb
->cfg
.attach(&elseBB
->cfg
, Graph::Edge::TREE
);
1531 // we only insert joinats, if both nodes end up at the end of the if again.
1532 // the reason for this to not happens are breaks/continues/ret/... which
1533 // have their own handling
1534 if (lastThen
->successors
[0] == lastElse
->successors
[0])
1535 bb
->joinAt
= mkFlow(OP_JOINAT
, convert(lastThen
->successors
[0]),
1538 mkFlow(OP_BRA
, elseBB
, CC_EQ
, src
)->setType(sType
);
1540 foreach_list_typed(nir_cf_node
, node
, node
, &nif
->then_list
) {
1544 setPosition(convert(lastThen
), true);
1545 if (!bb
->getExit() ||
1546 !bb
->getExit()->asFlow() ||
1547 bb
->getExit()->asFlow()->op
== OP_JOIN
) {
1548 BasicBlock
*tailBB
= convert(lastThen
->successors
[0]);
1549 mkFlow(OP_BRA
, tailBB
, CC_ALWAYS
, NULL
);
1550 bb
->cfg
.attach(&tailBB
->cfg
, Graph::Edge::FORWARD
);
1553 foreach_list_typed(nir_cf_node
, node
, node
, &nif
->else_list
) {
1557 setPosition(convert(lastElse
), true);
1558 if (!bb
->getExit() ||
1559 !bb
->getExit()->asFlow() ||
1560 bb
->getExit()->asFlow()->op
== OP_JOIN
) {
1561 BasicBlock
*tailBB
= convert(lastElse
->successors
[0]);
1562 mkFlow(OP_BRA
, tailBB
, CC_ALWAYS
, NULL
);
1563 bb
->cfg
.attach(&tailBB
->cfg
, Graph::Edge::FORWARD
);
1566 if (lastThen
->successors
[0] == lastElse
->successors
[0]) {
1567 setPosition(convert(lastThen
->successors
[0]), true);
1568 mkFlow(OP_JOIN
, NULL
, CC_ALWAYS
, NULL
)->fixed
= 1;
1575 Converter::visit(nir_loop
*loop
)
1578 func
->loopNestingBound
= std::max(func
->loopNestingBound
, curLoopDepth
);
1580 BasicBlock
*loopBB
= convert(nir_loop_first_block(loop
));
1581 BasicBlock
*tailBB
=
1582 convert(nir_cf_node_as_block(nir_cf_node_next(&loop
->cf_node
)));
1583 bb
->cfg
.attach(&loopBB
->cfg
, Graph::Edge::TREE
);
1585 mkFlow(OP_PREBREAK
, tailBB
, CC_ALWAYS
, NULL
);
1586 setPosition(loopBB
, false);
1587 mkFlow(OP_PRECONT
, loopBB
, CC_ALWAYS
, NULL
);
1589 foreach_list_typed(nir_cf_node
, node
, node
, &loop
->body
) {
1593 Instruction
*insn
= bb
->getExit();
1594 if (bb
->cfg
.incidentCount() != 0) {
1595 if (!insn
|| !insn
->asFlow()) {
1596 mkFlow(OP_CONT
, loopBB
, CC_ALWAYS
, NULL
);
1597 bb
->cfg
.attach(&loopBB
->cfg
, Graph::Edge::BACK
);
1598 } else if (insn
&& insn
->op
== OP_BRA
&& !insn
->getPredicate() &&
1599 tailBB
->cfg
.incidentCount() == 0) {
1600 // RA doesn't like having blocks around with no incident edge,
1601 // so we create a fake one to make it happy
1602 bb
->cfg
.attach(&tailBB
->cfg
, Graph::Edge::TREE
);
1612 Converter::visit(nir_instr
*insn
)
1614 switch (insn
->type
) {
1615 case nir_instr_type_alu
:
1616 return visit(nir_instr_as_alu(insn
));
1617 case nir_instr_type_intrinsic
:
1618 return visit(nir_instr_as_intrinsic(insn
));
1619 case nir_instr_type_jump
:
1620 return visit(nir_instr_as_jump(insn
));
1621 case nir_instr_type_load_const
:
1622 return visit(nir_instr_as_load_const(insn
));
1623 case nir_instr_type_ssa_undef
:
1624 return visit(nir_instr_as_ssa_undef(insn
));
1625 case nir_instr_type_tex
:
1626 return visit(nir_instr_as_tex(insn
));
1628 ERROR("unknown nir_instr type %u\n", insn
->type
);
1635 Converter::convert(nir_intrinsic_op intr
)
1638 case nir_intrinsic_load_base_vertex
:
1639 return SV_BASEVERTEX
;
1640 case nir_intrinsic_load_base_instance
:
1641 return SV_BASEINSTANCE
;
1642 case nir_intrinsic_load_draw_id
:
1644 case nir_intrinsic_load_front_face
:
1646 case nir_intrinsic_load_helper_invocation
:
1647 return SV_THREAD_KILL
;
1648 case nir_intrinsic_load_instance_id
:
1649 return SV_INSTANCE_ID
;
1650 case nir_intrinsic_load_invocation_id
:
1651 return SV_INVOCATION_ID
;
1652 case nir_intrinsic_load_local_group_size
:
1654 case nir_intrinsic_load_local_invocation_id
:
1656 case nir_intrinsic_load_num_work_groups
:
1658 case nir_intrinsic_load_patch_vertices_in
:
1659 return SV_VERTEX_COUNT
;
1660 case nir_intrinsic_load_primitive_id
:
1661 return SV_PRIMITIVE_ID
;
1662 case nir_intrinsic_load_sample_id
:
1663 return SV_SAMPLE_INDEX
;
1664 case nir_intrinsic_load_sample_mask_in
:
1665 return SV_SAMPLE_MASK
;
1666 case nir_intrinsic_load_sample_pos
:
1667 return SV_SAMPLE_POS
;
1668 case nir_intrinsic_load_subgroup_eq_mask
:
1669 return SV_LANEMASK_EQ
;
1670 case nir_intrinsic_load_subgroup_ge_mask
:
1671 return SV_LANEMASK_GE
;
1672 case nir_intrinsic_load_subgroup_gt_mask
:
1673 return SV_LANEMASK_GT
;
1674 case nir_intrinsic_load_subgroup_le_mask
:
1675 return SV_LANEMASK_LE
;
1676 case nir_intrinsic_load_subgroup_lt_mask
:
1677 return SV_LANEMASK_LT
;
1678 case nir_intrinsic_load_subgroup_invocation
:
1680 case nir_intrinsic_load_tess_coord
:
1681 return SV_TESS_COORD
;
1682 case nir_intrinsic_load_tess_level_inner
:
1683 return SV_TESS_INNER
;
1684 case nir_intrinsic_load_tess_level_outer
:
1685 return SV_TESS_OUTER
;
1686 case nir_intrinsic_load_vertex_id
:
1687 return SV_VERTEX_ID
;
1688 case nir_intrinsic_load_work_group_id
:
1691 ERROR("unknown SVSemantic for nir_intrinsic_op %s\n",
1692 nir_intrinsic_infos
[intr
].name
);
1699 Converter::visit(nir_intrinsic_instr
*insn
)
1701 nir_intrinsic_op op
= insn
->intrinsic
;
1704 case nir_intrinsic_load_uniform
: {
1705 LValues
&newDefs
= convert(&insn
->dest
);
1706 const DataType dType
= getDType(insn
);
1708 uint32_t coffset
= getIndirect(insn
, 0, 0, indirect
);
1709 for (uint8_t i
= 0; i
< insn
->num_components
; ++i
) {
1710 loadFrom(FILE_MEMORY_CONST
, 0, dType
, newDefs
[i
], 16 * coffset
, i
, indirect
);
1714 case nir_intrinsic_store_output
:
1715 case nir_intrinsic_store_per_vertex_output
: {
1717 DataType dType
= getSType(insn
->src
[0], false, false);
1718 uint32_t idx
= getIndirect(insn
, op
== nir_intrinsic_store_output
? 1 : 2, 0, indirect
);
1720 for (uint8_t i
= 0u; i
< insn
->num_components
; ++i
) {
1721 if (!((1u << i
) & nir_intrinsic_write_mask(insn
)))
1725 Value
*src
= getSrc(&insn
->src
[0], i
);
1726 switch (prog
->getType()) {
1727 case Program::TYPE_FRAGMENT
: {
1728 if (info
->out
[idx
].sn
== TGSI_SEMANTIC_POSITION
) {
1729 // TGSI uses a different interface than NIR, TGSI stores that
1730 // value in the z component, NIR in X
1732 src
= mkOp1v(OP_SAT
, TYPE_F32
, getScratch(), src
);
1736 case Program::TYPE_VERTEX
: {
1737 if (info
->io
.genUserClip
> 0 && idx
== clipVertexOutput
) {
1738 mkMov(clipVtx
[i
], src
);
1747 storeTo(insn
, FILE_SHADER_OUTPUT
, OP_EXPORT
, dType
, src
, idx
, i
+ offset
, indirect
);
1751 case nir_intrinsic_load_input
:
1752 case nir_intrinsic_load_interpolated_input
:
1753 case nir_intrinsic_load_output
: {
1754 LValues
&newDefs
= convert(&insn
->dest
);
1757 if (prog
->getType() == Program::TYPE_FRAGMENT
&&
1758 op
== nir_intrinsic_load_output
) {
1759 std::vector
<Value
*> defs
, srcs
;
1762 srcs
.push_back(getSSA());
1763 srcs
.push_back(getSSA());
1764 Value
*x
= mkOp1v(OP_RDSV
, TYPE_F32
, getSSA(), mkSysVal(SV_POSITION
, 0));
1765 Value
*y
= mkOp1v(OP_RDSV
, TYPE_F32
, getSSA(), mkSysVal(SV_POSITION
, 1));
1766 mkCvt(OP_CVT
, TYPE_U32
, srcs
[0], TYPE_F32
, x
)->rnd
= ROUND_Z
;
1767 mkCvt(OP_CVT
, TYPE_U32
, srcs
[1], TYPE_F32
, y
)->rnd
= ROUND_Z
;
1769 srcs
.push_back(mkOp1v(OP_RDSV
, TYPE_U32
, getSSA(), mkSysVal(SV_LAYER
, 0)));
1770 srcs
.push_back(mkOp1v(OP_RDSV
, TYPE_U32
, getSSA(), mkSysVal(SV_SAMPLE_INDEX
, 0)));
1772 for (uint8_t i
= 0u; i
< insn
->num_components
; ++i
) {
1773 defs
.push_back(newDefs
[i
]);
1777 TexInstruction
*texi
= mkTex(OP_TXF
, TEX_TARGET_2D_MS_ARRAY
, 0, 0, defs
, srcs
);
1778 texi
->tex
.levelZero
= 1;
1779 texi
->tex
.mask
= mask
;
1780 texi
->tex
.useOffsets
= 0;
1781 texi
->tex
.r
= 0xffff;
1782 texi
->tex
.s
= 0xffff;
1784 info
->prop
.fp
.readsFramebuffer
= true;
1788 const DataType dType
= getDType(insn
);
1790 bool input
= op
!= nir_intrinsic_load_output
;
1794 uint32_t idx
= getIndirect(insn
, op
== nir_intrinsic_load_interpolated_input
? 1 : 0, 0, indirect
);
1795 nv50_ir_varying
& vary
= input
? info
->in
[idx
] : info
->out
[idx
];
1797 // see load_barycentric_* handling
1798 if (prog
->getType() == Program::TYPE_FRAGMENT
) {
1799 mode
= translateInterpMode(&vary
, nvirOp
);
1800 if (op
== nir_intrinsic_load_interpolated_input
) {
1801 ImmediateValue immMode
;
1802 if (getSrc(&insn
->src
[0], 1)->getUniqueInsn()->src(0).getImmediate(immMode
))
1803 mode
|= immMode
.reg
.data
.u32
;
1807 for (uint8_t i
= 0u; i
< insn
->num_components
; ++i
) {
1808 uint32_t address
= getSlotAddress(insn
, idx
, i
);
1809 Symbol
*sym
= mkSymbol(input
? FILE_SHADER_INPUT
: FILE_SHADER_OUTPUT
, 0, dType
, address
);
1810 if (prog
->getType() == Program::TYPE_FRAGMENT
) {
1812 if (typeSizeof(dType
) == 8) {
1813 Value
*lo
= getSSA();
1814 Value
*hi
= getSSA();
1815 Instruction
*interp
;
1817 interp
= mkOp1(nvirOp
, TYPE_U32
, lo
, sym
);
1818 if (nvirOp
== OP_PINTERP
)
1819 interp
->setSrc(s
++, fp
.position
);
1820 if (mode
& NV50_IR_INTERP_OFFSET
)
1821 interp
->setSrc(s
++, getSrc(&insn
->src
[0], 0));
1822 interp
->setInterpolate(mode
);
1823 interp
->setIndirect(0, 0, indirect
);
1825 Symbol
*sym1
= mkSymbol(input
? FILE_SHADER_INPUT
: FILE_SHADER_OUTPUT
, 0, dType
, address
+ 4);
1826 interp
= mkOp1(nvirOp
, TYPE_U32
, hi
, sym1
);
1827 if (nvirOp
== OP_PINTERP
)
1828 interp
->setSrc(s
++, fp
.position
);
1829 if (mode
& NV50_IR_INTERP_OFFSET
)
1830 interp
->setSrc(s
++, getSrc(&insn
->src
[0], 0));
1831 interp
->setInterpolate(mode
);
1832 interp
->setIndirect(0, 0, indirect
);
1834 mkOp2(OP_MERGE
, dType
, newDefs
[i
], lo
, hi
);
1836 Instruction
*interp
= mkOp1(nvirOp
, dType
, newDefs
[i
], sym
);
1837 if (nvirOp
== OP_PINTERP
)
1838 interp
->setSrc(s
++, fp
.position
);
1839 if (mode
& NV50_IR_INTERP_OFFSET
)
1840 interp
->setSrc(s
++, getSrc(&insn
->src
[0], 0));
1841 interp
->setInterpolate(mode
);
1842 interp
->setIndirect(0, 0, indirect
);
1845 mkLoad(dType
, newDefs
[i
], sym
, indirect
)->perPatch
= vary
.patch
;
1850 case nir_intrinsic_load_barycentric_at_offset
:
1851 case nir_intrinsic_load_barycentric_at_sample
:
1852 case nir_intrinsic_load_barycentric_centroid
:
1853 case nir_intrinsic_load_barycentric_pixel
:
1854 case nir_intrinsic_load_barycentric_sample
: {
1855 LValues
&newDefs
= convert(&insn
->dest
);
1858 if (op
== nir_intrinsic_load_barycentric_centroid
||
1859 op
== nir_intrinsic_load_barycentric_sample
) {
1860 mode
= NV50_IR_INTERP_CENTROID
;
1861 } else if (op
== nir_intrinsic_load_barycentric_at_offset
) {
1863 for (uint8_t c
= 0; c
< 2; c
++) {
1864 offs
[c
] = getScratch();
1865 mkOp2(OP_MIN
, TYPE_F32
, offs
[c
], getSrc(&insn
->src
[0], c
), loadImm(NULL
, 0.4375f
));
1866 mkOp2(OP_MAX
, TYPE_F32
, offs
[c
], offs
[c
], loadImm(NULL
, -0.5f
));
1867 mkOp2(OP_MUL
, TYPE_F32
, offs
[c
], offs
[c
], loadImm(NULL
, 4096.0f
));
1868 mkCvt(OP_CVT
, TYPE_S32
, offs
[c
], TYPE_F32
, offs
[c
]);
1870 mkOp3v(OP_INSBF
, TYPE_U32
, newDefs
[0], offs
[1], mkImm(0x1010), offs
[0]);
1872 mode
= NV50_IR_INTERP_OFFSET
;
1873 } else if (op
== nir_intrinsic_load_barycentric_pixel
) {
1874 mode
= NV50_IR_INTERP_DEFAULT
;
1875 } else if (op
== nir_intrinsic_load_barycentric_at_sample
) {
1876 info
->prop
.fp
.readsSampleLocations
= true;
1877 mkOp1(OP_PIXLD
, TYPE_U32
, newDefs
[0], getSrc(&insn
->src
[0], 0))->subOp
= NV50_IR_SUBOP_PIXLD_OFFSET
;
1878 mode
= NV50_IR_INTERP_OFFSET
;
1880 unreachable("all intrinsics already handled above");
1883 loadImm(newDefs
[1], mode
);
1886 case nir_intrinsic_discard
:
1887 mkOp(OP_DISCARD
, TYPE_NONE
, NULL
);
1889 case nir_intrinsic_discard_if
: {
1890 Value
*pred
= getSSA(1, FILE_PREDICATE
);
1891 if (insn
->num_components
> 1) {
1892 ERROR("nir_intrinsic_discard_if only with 1 component supported!\n");
1896 mkCmp(OP_SET
, CC_NE
, TYPE_U8
, pred
, TYPE_U32
, getSrc(&insn
->src
[0], 0), zero
);
1897 mkOp(OP_DISCARD
, TYPE_NONE
, NULL
)->setPredicate(CC_P
, pred
);
1900 case nir_intrinsic_load_base_vertex
:
1901 case nir_intrinsic_load_base_instance
:
1902 case nir_intrinsic_load_draw_id
:
1903 case nir_intrinsic_load_front_face
:
1904 case nir_intrinsic_load_helper_invocation
:
1905 case nir_intrinsic_load_instance_id
:
1906 case nir_intrinsic_load_invocation_id
:
1907 case nir_intrinsic_load_local_group_size
:
1908 case nir_intrinsic_load_local_invocation_id
:
1909 case nir_intrinsic_load_num_work_groups
:
1910 case nir_intrinsic_load_patch_vertices_in
:
1911 case nir_intrinsic_load_primitive_id
:
1912 case nir_intrinsic_load_sample_id
:
1913 case nir_intrinsic_load_sample_mask_in
:
1914 case nir_intrinsic_load_sample_pos
:
1915 case nir_intrinsic_load_subgroup_eq_mask
:
1916 case nir_intrinsic_load_subgroup_ge_mask
:
1917 case nir_intrinsic_load_subgroup_gt_mask
:
1918 case nir_intrinsic_load_subgroup_le_mask
:
1919 case nir_intrinsic_load_subgroup_lt_mask
:
1920 case nir_intrinsic_load_subgroup_invocation
:
1921 case nir_intrinsic_load_tess_coord
:
1922 case nir_intrinsic_load_tess_level_inner
:
1923 case nir_intrinsic_load_tess_level_outer
:
1924 case nir_intrinsic_load_vertex_id
:
1925 case nir_intrinsic_load_work_group_id
: {
1926 const DataType dType
= getDType(insn
);
1927 SVSemantic sv
= convert(op
);
1928 LValues
&newDefs
= convert(&insn
->dest
);
1930 for (uint8_t i
= 0u; i
< insn
->num_components
; ++i
) {
1932 if (typeSizeof(dType
) == 8)
1937 if (sv
== SV_TID
&& info
->prop
.cp
.numThreads
[i
] == 1) {
1940 Symbol
*sym
= mkSysVal(sv
, i
);
1941 Instruction
*rdsv
= mkOp1(OP_RDSV
, TYPE_U32
, def
, sym
);
1942 if (sv
== SV_TESS_OUTER
|| sv
== SV_TESS_INNER
)
1946 if (typeSizeof(dType
) == 8)
1947 mkOp2(OP_MERGE
, dType
, newDefs
[i
], def
, loadImm(getSSA(), 0u));
1952 case nir_intrinsic_load_subgroup_size
: {
1953 LValues
&newDefs
= convert(&insn
->dest
);
1954 loadImm(newDefs
[0], 32u);
1957 case nir_intrinsic_vote_all
:
1958 case nir_intrinsic_vote_any
:
1959 case nir_intrinsic_vote_ieq
: {
1960 LValues
&newDefs
= convert(&insn
->dest
);
1961 Value
*pred
= getScratch(1, FILE_PREDICATE
);
1962 mkCmp(OP_SET
, CC_NE
, TYPE_U32
, pred
, TYPE_U32
, getSrc(&insn
->src
[0], 0), zero
);
1963 mkOp1(OP_VOTE
, TYPE_U32
, pred
, pred
)->subOp
= getSubOp(op
);
1964 mkCvt(OP_CVT
, TYPE_U32
, newDefs
[0], TYPE_U8
, pred
);
1967 case nir_intrinsic_ballot
: {
1968 LValues
&newDefs
= convert(&insn
->dest
);
1969 Value
*pred
= getSSA(1, FILE_PREDICATE
);
1970 mkCmp(OP_SET
, CC_NE
, TYPE_U32
, pred
, TYPE_U32
, getSrc(&insn
->src
[0], 0), zero
);
1971 mkOp1(OP_VOTE
, TYPE_U32
, newDefs
[0], pred
)->subOp
= NV50_IR_SUBOP_VOTE_ANY
;
1974 case nir_intrinsic_read_first_invocation
:
1975 case nir_intrinsic_read_invocation
: {
1976 LValues
&newDefs
= convert(&insn
->dest
);
1977 const DataType dType
= getDType(insn
);
1978 Value
*tmp
= getScratch();
1980 if (op
== nir_intrinsic_read_first_invocation
) {
1981 mkOp1(OP_VOTE
, TYPE_U32
, tmp
, mkImm(1))->subOp
= NV50_IR_SUBOP_VOTE_ANY
;
1982 mkOp2(OP_EXTBF
, TYPE_U32
, tmp
, tmp
, mkImm(0x2000))->subOp
= NV50_IR_SUBOP_EXTBF_REV
;
1983 mkOp1(OP_BFIND
, TYPE_U32
, tmp
, tmp
)->subOp
= NV50_IR_SUBOP_BFIND_SAMT
;
1985 tmp
= getSrc(&insn
->src
[1], 0);
1987 for (uint8_t i
= 0; i
< insn
->num_components
; ++i
) {
1988 mkOp3(OP_SHFL
, dType
, newDefs
[i
], getSrc(&insn
->src
[0], i
), tmp
, mkImm(0x1f))
1989 ->subOp
= NV50_IR_SUBOP_SHFL_IDX
;
1993 case nir_intrinsic_load_per_vertex_input
: {
1994 const DataType dType
= getDType(insn
);
1995 LValues
&newDefs
= convert(&insn
->dest
);
1996 Value
*indirectVertex
;
1997 Value
*indirectOffset
;
1998 uint32_t baseVertex
= getIndirect(&insn
->src
[0], 0, indirectVertex
);
1999 uint32_t idx
= getIndirect(insn
, 1, 0, indirectOffset
);
2001 Value
*vtxBase
= mkOp2v(OP_PFETCH
, TYPE_U32
, getSSA(4, FILE_ADDRESS
),
2002 mkImm(baseVertex
), indirectVertex
);
2003 for (uint8_t i
= 0u; i
< insn
->num_components
; ++i
) {
2004 uint32_t address
= getSlotAddress(insn
, idx
, i
);
2005 loadFrom(FILE_SHADER_INPUT
, 0, dType
, newDefs
[i
], address
, 0,
2006 indirectOffset
, vtxBase
, info
->in
[idx
].patch
);
2010 case nir_intrinsic_emit_vertex
:
2011 case nir_intrinsic_end_primitive
: {
2012 uint32_t idx
= nir_intrinsic_stream_id(insn
);
2013 mkOp1(getOperation(op
), TYPE_U32
, NULL
, mkImm(idx
))->fixed
= 1;
2017 ERROR("unknown nir_intrinsic_op %s\n", nir_intrinsic_infos
[op
].name
);
2025 Converter::visit(nir_jump_instr
*insn
)
2027 switch (insn
->type
) {
2028 case nir_jump_return
:
2029 // TODO: this only works in the main function
2030 mkFlow(OP_BRA
, exit
, CC_ALWAYS
, NULL
);
2031 bb
->cfg
.attach(&exit
->cfg
, Graph::Edge::CROSS
);
2033 case nir_jump_break
:
2034 case nir_jump_continue
: {
2035 bool isBreak
= insn
->type
== nir_jump_break
;
2036 nir_block
*block
= insn
->instr
.block
;
2037 assert(!block
->successors
[1]);
2038 BasicBlock
*target
= convert(block
->successors
[0]);
2039 mkFlow(isBreak
? OP_BREAK
: OP_CONT
, target
, CC_ALWAYS
, NULL
);
2040 bb
->cfg
.attach(&target
->cfg
, isBreak
? Graph::Edge::CROSS
: Graph::Edge::BACK
);
2044 ERROR("unknown nir_jump_type %u\n", insn
->type
);
2052 Converter::visit(nir_load_const_instr
*insn
)
2054 assert(insn
->def
.bit_size
<= 64);
2056 LValues
&newDefs
= convert(&insn
->def
);
2057 for (int i
= 0; i
< insn
->def
.num_components
; i
++) {
2058 switch (insn
->def
.bit_size
) {
2060 loadImm(newDefs
[i
], insn
->value
.u64
[i
]);
2063 loadImm(newDefs
[i
], insn
->value
.u32
[i
]);
2066 loadImm(newDefs
[i
], insn
->value
.u16
[i
]);
2069 loadImm(newDefs
[i
], insn
->value
.u8
[i
]);
2076 #define DEFAULT_CHECKS \
2077 if (insn->dest.dest.ssa.num_components > 1) { \
2078 ERROR("nir_alu_instr only supported with 1 component!\n"); \
2081 if (insn->dest.write_mask != 1) { \
2082 ERROR("nir_alu_instr only with write_mask of 1 supported!\n"); \
2086 Converter::visit(nir_alu_instr
*insn
)
2088 const nir_op op
= insn
->op
;
2089 const nir_op_info
&info
= nir_op_infos
[op
];
2090 DataType dType
= getDType(insn
);
2091 const std::vector
<DataType
> sTypes
= getSTypes(insn
);
2093 Instruction
*oldPos
= this->bb
->getExit();
2105 case nir_op_fddx_coarse
:
2106 case nir_op_fddx_fine
:
2108 case nir_op_fddy_coarse
:
2109 case nir_op_fddy_fine
:
2128 case nir_op_imul_high
:
2129 case nir_op_umul_high
:
2136 case nir_op_pack_64_2x32_split
:
2154 LValues
&newDefs
= convert(&insn
->dest
);
2155 operation preOp
= preOperationNeeded(op
);
2156 if (preOp
!= OP_NOP
) {
2157 assert(info
.num_inputs
< 2);
2158 Value
*tmp
= getSSA(typeSizeof(dType
));
2159 Instruction
*i0
= mkOp(preOp
, dType
, tmp
);
2160 Instruction
*i1
= mkOp(getOperation(op
), dType
, newDefs
[0]);
2161 if (info
.num_inputs
) {
2162 i0
->setSrc(0, getSrc(&insn
->src
[0]));
2165 i1
->subOp
= getSubOp(op
);
2167 Instruction
*i
= mkOp(getOperation(op
), dType
, newDefs
[0]);
2168 for (unsigned s
= 0u; s
< info
.num_inputs
; ++s
) {
2169 i
->setSrc(s
, getSrc(&insn
->src
[s
]));
2171 i
->subOp
= getSubOp(op
);
2175 case nir_op_ifind_msb
:
2176 case nir_op_ufind_msb
: {
2178 LValues
&newDefs
= convert(&insn
->dest
);
2180 mkOp1(getOperation(op
), dType
, newDefs
[0], getSrc(&insn
->src
[0]));
2183 case nir_op_fround_even
: {
2185 LValues
&newDefs
= convert(&insn
->dest
);
2186 mkCvt(OP_CVT
, dType
, newDefs
[0], dType
, getSrc(&insn
->src
[0]))->rnd
= ROUND_NI
;
2189 // convert instructions
2203 case nir_op_u2u64
: {
2205 LValues
&newDefs
= convert(&insn
->dest
);
2206 Instruction
*i
= mkOp1(getOperation(op
), dType
, newDefs
[0], getSrc(&insn
->src
[0]));
2207 if (op
== nir_op_f2i32
|| op
== nir_op_f2i64
|| op
== nir_op_f2u32
|| op
== nir_op_f2u64
)
2209 i
->sType
= sTypes
[0];
2212 // compare instructions
2222 case nir_op_ine32
: {
2224 LValues
&newDefs
= convert(&insn
->dest
);
2225 Instruction
*i
= mkCmp(getOperation(op
),
2230 getSrc(&insn
->src
[0]),
2231 getSrc(&insn
->src
[1]));
2232 if (info
.num_inputs
== 3)
2233 i
->setSrc(2, getSrc(&insn
->src
[2]));
2234 i
->sType
= sTypes
[0];
2237 // those are weird ALU ops and need special handling, because
2238 // 1. they are always componend based
2239 // 2. they basically just merge multiple values into one data type
2242 if (!insn
->dest
.dest
.is_ssa
&& insn
->dest
.dest
.reg
.reg
->num_array_elems
) {
2243 nir_reg_dest
& reg
= insn
->dest
.dest
.reg
;
2244 uint32_t goffset
= regToLmemOffset
[reg
.reg
->index
];
2245 uint8_t comps
= reg
.reg
->num_components
;
2246 uint8_t size
= reg
.reg
->bit_size
/ 8;
2247 uint8_t csize
= 4 * size
; // TODO after fixing MemoryOpts: comps * size;
2248 uint32_t aoffset
= csize
* reg
.base_offset
;
2249 Value
*indirect
= NULL
;
2252 indirect
= mkOp2v(OP_MUL
, TYPE_U32
, getSSA(4, FILE_ADDRESS
),
2253 getSrc(reg
.indirect
, 0), mkImm(csize
));
2255 for (uint8_t i
= 0u; i
< comps
; ++i
) {
2256 if (!((1u << i
) & insn
->dest
.write_mask
))
2259 Symbol
*sym
= mkSymbol(FILE_MEMORY_LOCAL
, 0, dType
, goffset
+ aoffset
+ i
* size
);
2260 mkStore(OP_STORE
, dType
, sym
, indirect
, getSrc(&insn
->src
[0], i
));
2263 } else if (!insn
->src
[0].src
.is_ssa
&& insn
->src
[0].src
.reg
.reg
->num_array_elems
) {
2264 LValues
&newDefs
= convert(&insn
->dest
);
2265 nir_reg_src
& reg
= insn
->src
[0].src
.reg
;
2266 uint32_t goffset
= regToLmemOffset
[reg
.reg
->index
];
2267 // uint8_t comps = reg.reg->num_components;
2268 uint8_t size
= reg
.reg
->bit_size
/ 8;
2269 uint8_t csize
= 4 * size
; // TODO after fixing MemoryOpts: comps * size;
2270 uint32_t aoffset
= csize
* reg
.base_offset
;
2271 Value
*indirect
= NULL
;
2274 indirect
= mkOp2v(OP_MUL
, TYPE_U32
, getSSA(4, FILE_ADDRESS
), getSrc(reg
.indirect
, 0), mkImm(csize
));
2276 for (uint8_t i
= 0u; i
< newDefs
.size(); ++i
)
2277 loadFrom(FILE_MEMORY_LOCAL
, 0, dType
, newDefs
[i
], goffset
+ aoffset
, i
, indirect
);
2281 LValues
&newDefs
= convert(&insn
->dest
);
2282 for (LValues::size_type c
= 0u; c
< newDefs
.size(); ++c
) {
2283 mkMov(newDefs
[c
], getSrc(&insn
->src
[0], c
), dType
);
2290 LValues
&newDefs
= convert(&insn
->dest
);
2291 for (LValues::size_type c
= 0u; c
< newDefs
.size(); ++c
) {
2292 mkMov(newDefs
[c
], getSrc(&insn
->src
[c
]), dType
);
2297 case nir_op_pack_64_2x32
: {
2298 LValues
&newDefs
= convert(&insn
->dest
);
2299 Instruction
*merge
= mkOp(OP_MERGE
, dType
, newDefs
[0]);
2300 merge
->setSrc(0, getSrc(&insn
->src
[0], 0));
2301 merge
->setSrc(1, getSrc(&insn
->src
[0], 1));
2304 case nir_op_pack_half_2x16_split
: {
2305 LValues
&newDefs
= convert(&insn
->dest
);
2306 Value
*tmpH
= getSSA();
2307 Value
*tmpL
= getSSA();
2309 mkCvt(OP_CVT
, TYPE_F16
, tmpL
, TYPE_F32
, getSrc(&insn
->src
[0]));
2310 mkCvt(OP_CVT
, TYPE_F16
, tmpH
, TYPE_F32
, getSrc(&insn
->src
[1]));
2311 mkOp3(OP_INSBF
, TYPE_U32
, newDefs
[0], tmpH
, mkImm(0x1010), tmpL
);
2314 case nir_op_unpack_half_2x16_split_x
:
2315 case nir_op_unpack_half_2x16_split_y
: {
2316 LValues
&newDefs
= convert(&insn
->dest
);
2317 Instruction
*cvt
= mkCvt(OP_CVT
, TYPE_F32
, newDefs
[0], TYPE_F16
, getSrc(&insn
->src
[0]));
2318 if (op
== nir_op_unpack_half_2x16_split_y
)
2322 case nir_op_unpack_64_2x32
: {
2323 LValues
&newDefs
= convert(&insn
->dest
);
2324 mkOp1(OP_SPLIT
, dType
, newDefs
[0], getSrc(&insn
->src
[0]))->setDef(1, newDefs
[1]);
2327 case nir_op_unpack_64_2x32_split_x
: {
2328 LValues
&newDefs
= convert(&insn
->dest
);
2329 mkOp1(OP_SPLIT
, dType
, newDefs
[0], getSrc(&insn
->src
[0]))->setDef(1, getSSA());
2332 case nir_op_unpack_64_2x32_split_y
: {
2333 LValues
&newDefs
= convert(&insn
->dest
);
2334 mkOp1(OP_SPLIT
, dType
, getSSA(), getSrc(&insn
->src
[0]))->setDef(1, newDefs
[0]);
2337 // special instructions
2339 case nir_op_isign
: {
2342 if (::isFloatType(dType
))
2347 LValues
&newDefs
= convert(&insn
->dest
);
2348 LValue
*val0
= getScratch();
2349 LValue
*val1
= getScratch();
2350 mkCmp(OP_SET
, CC_GT
, iType
, val0
, dType
, getSrc(&insn
->src
[0]), zero
);
2351 mkCmp(OP_SET
, CC_LT
, iType
, val1
, dType
, getSrc(&insn
->src
[0]), zero
);
2353 if (dType
== TYPE_F64
) {
2354 mkOp2(OP_SUB
, iType
, val0
, val0
, val1
);
2355 mkCvt(OP_CVT
, TYPE_F64
, newDefs
[0], iType
, val0
);
2356 } else if (dType
== TYPE_S64
|| dType
== TYPE_U64
) {
2357 mkOp2(OP_SUB
, iType
, val0
, val1
, val0
);
2358 mkOp2(OP_SHR
, iType
, val1
, val0
, loadImm(NULL
, 31));
2359 mkOp2(OP_MERGE
, dType
, newDefs
[0], val0
, val1
);
2360 } else if (::isFloatType(dType
))
2361 mkOp2(OP_SUB
, iType
, newDefs
[0], val0
, val1
);
2363 mkOp2(OP_SUB
, iType
, newDefs
[0], val1
, val0
);
2367 case nir_op_b32csel
: {
2369 LValues
&newDefs
= convert(&insn
->dest
);
2370 mkCmp(OP_SLCT
, CC_NE
, dType
, newDefs
[0], sTypes
[0], getSrc(&insn
->src
[1]), getSrc(&insn
->src
[2]), getSrc(&insn
->src
[0]));
2373 case nir_op_ibitfield_extract
:
2374 case nir_op_ubitfield_extract
: {
2376 Value
*tmp
= getSSA();
2377 LValues
&newDefs
= convert(&insn
->dest
);
2378 mkOp3(OP_INSBF
, dType
, tmp
, getSrc(&insn
->src
[2]), loadImm(NULL
, 0x808), getSrc(&insn
->src
[1]));
2379 mkOp2(OP_EXTBF
, dType
, newDefs
[0], getSrc(&insn
->src
[0]), tmp
);
2384 LValues
&newDefs
= convert(&insn
->dest
);
2385 mkOp3(OP_INSBF
, dType
, newDefs
[0], getSrc(&insn
->src
[0]), loadImm(NULL
, 0x808), getSrc(&insn
->src
[1]));
2388 case nir_op_bitfield_insert
: {
2390 LValues
&newDefs
= convert(&insn
->dest
);
2391 LValue
*temp
= getSSA();
2392 mkOp3(OP_INSBF
, TYPE_U32
, temp
, getSrc(&insn
->src
[3]), mkImm(0x808), getSrc(&insn
->src
[2]));
2393 mkOp3(OP_INSBF
, dType
, newDefs
[0], getSrc(&insn
->src
[1]), temp
, getSrc(&insn
->src
[0]));
2396 case nir_op_bit_count
: {
2398 LValues
&newDefs
= convert(&insn
->dest
);
2399 mkOp2(OP_POPCNT
, dType
, newDefs
[0], getSrc(&insn
->src
[0]), getSrc(&insn
->src
[0]));
2402 case nir_op_bitfield_reverse
: {
2404 LValues
&newDefs
= convert(&insn
->dest
);
2405 mkOp2(OP_EXTBF
, TYPE_U32
, newDefs
[0], getSrc(&insn
->src
[0]), mkImm(0x2000))->subOp
= NV50_IR_SUBOP_EXTBF_REV
;
2408 case nir_op_find_lsb
: {
2410 LValues
&newDefs
= convert(&insn
->dest
);
2411 Value
*tmp
= getSSA();
2412 mkOp2(OP_EXTBF
, TYPE_U32
, tmp
, getSrc(&insn
->src
[0]), mkImm(0x2000))->subOp
= NV50_IR_SUBOP_EXTBF_REV
;
2413 mkOp1(OP_BFIND
, TYPE_U32
, newDefs
[0], tmp
)->subOp
= NV50_IR_SUBOP_BFIND_SAMT
;
2416 // boolean conversions
2417 case nir_op_b2f32
: {
2419 LValues
&newDefs
= convert(&insn
->dest
);
2420 mkOp2(OP_AND
, TYPE_U32
, newDefs
[0], getSrc(&insn
->src
[0]), loadImm(NULL
, 1.0f
));
2423 case nir_op_b2f64
: {
2425 LValues
&newDefs
= convert(&insn
->dest
);
2426 Value
*tmp
= getSSA(4);
2427 mkOp2(OP_AND
, TYPE_U32
, tmp
, getSrc(&insn
->src
[0]), loadImm(NULL
, 0x3ff00000));
2428 mkOp2(OP_MERGE
, TYPE_U64
, newDefs
[0], loadImm(NULL
, 0), tmp
);
2432 case nir_op_i2b32
: {
2434 LValues
&newDefs
= convert(&insn
->dest
);
2436 if (typeSizeof(sTypes
[0]) == 8) {
2437 src1
= loadImm(getSSA(8), 0.0);
2441 CondCode cc
= op
== nir_op_f2b32
? CC_NEU
: CC_NE
;
2442 mkCmp(OP_SET
, cc
, TYPE_U32
, newDefs
[0], sTypes
[0], getSrc(&insn
->src
[0]), src1
);
2445 case nir_op_b2i32
: {
2447 LValues
&newDefs
= convert(&insn
->dest
);
2448 mkOp2(OP_AND
, TYPE_U32
, newDefs
[0], getSrc(&insn
->src
[0]), loadImm(NULL
, 1));
2451 case nir_op_b2i64
: {
2453 LValues
&newDefs
= convert(&insn
->dest
);
2454 LValue
*def
= getScratch();
2455 mkOp2(OP_AND
, TYPE_U32
, def
, getSrc(&insn
->src
[0]), loadImm(NULL
, 1));
2456 mkOp2(OP_MERGE
, TYPE_S64
, newDefs
[0], def
, loadImm(NULL
, 0));
2460 ERROR("unknown nir_op %s\n", info
.name
);
2465 oldPos
= this->bb
->getEntry();
2466 oldPos
->precise
= insn
->exact
;
2469 if (unlikely(!oldPos
))
2472 while (oldPos
->next
) {
2473 oldPos
= oldPos
->next
;
2474 oldPos
->precise
= insn
->exact
;
2476 oldPos
->saturate
= insn
->dest
.saturate
;
2480 #undef DEFAULT_CHECKS
2483 Converter::visit(nir_ssa_undef_instr
*insn
)
2485 LValues
&newDefs
= convert(&insn
->def
);
2486 for (uint8_t i
= 0u; i
< insn
->def
.num_components
; ++i
) {
2487 mkOp(OP_NOP
, TYPE_NONE
, newDefs
[i
]);
2492 #define CASE_SAMPLER(ty) \
2493 case GLSL_SAMPLER_DIM_ ## ty : \
2494 if (isArray && !isShadow) \
2495 return TEX_TARGET_ ## ty ## _ARRAY; \
2496 else if (!isArray && isShadow) \
2497 return TEX_TARGET_## ty ## _SHADOW; \
2498 else if (isArray && isShadow) \
2499 return TEX_TARGET_## ty ## _ARRAY_SHADOW; \
2501 return TEX_TARGET_ ## ty
2504 Converter::convert(glsl_sampler_dim dim
, bool isArray
, bool isShadow
)
2510 case GLSL_SAMPLER_DIM_3D
:
2511 return TEX_TARGET_3D
;
2512 case GLSL_SAMPLER_DIM_MS
:
2514 return TEX_TARGET_2D_MS_ARRAY
;
2515 return TEX_TARGET_2D_MS
;
2516 case GLSL_SAMPLER_DIM_RECT
:
2518 return TEX_TARGET_RECT_SHADOW
;
2519 return TEX_TARGET_RECT
;
2520 case GLSL_SAMPLER_DIM_BUF
:
2521 return TEX_TARGET_BUFFER
;
2522 case GLSL_SAMPLER_DIM_EXTERNAL
:
2523 return TEX_TARGET_2D
;
2525 ERROR("unknown glsl_sampler_dim %u\n", dim
);
2527 return TEX_TARGET_COUNT
;
2533 Converter::applyProjection(Value
*src
, Value
*proj
)
2537 return mkOp2v(OP_MUL
, TYPE_F32
, getScratch(), src
, proj
);
2541 Converter::visit(nir_tex_instr
*insn
)
2545 case nir_texop_query_levels
:
2547 case nir_texop_texture_samples
:
2552 case nir_texop_txf_ms
:
2554 case nir_texop_txs
: {
2555 LValues
&newDefs
= convert(&insn
->dest
);
2556 std::vector
<Value
*> srcs
;
2557 std::vector
<Value
*> defs
;
2558 std::vector
<nir_src
*> offsets
;
2562 TexInstruction::Target target
= convert(insn
->sampler_dim
, insn
->is_array
, insn
->is_shadow
);
2563 operation op
= getOperation(insn
->op
);
2566 int biasIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_bias
);
2567 int compIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_comparator
);
2568 int coordsIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_coord
);
2569 int ddxIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_ddx
);
2570 int ddyIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_ddy
);
2571 int msIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_ms_index
);
2572 int lodIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_lod
);
2573 int offsetIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_offset
);
2574 int projIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_projector
);
2575 int sampOffIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_sampler_offset
);
2576 int texOffIdx
= nir_tex_instr_src_index(insn
, nir_tex_src_texture_offset
);
2579 proj
= mkOp1v(OP_RCP
, TYPE_F32
, getScratch(), getSrc(&insn
->src
[projIdx
].src
, 0));
2581 srcs
.resize(insn
->coord_components
);
2582 for (uint8_t i
= 0u; i
< insn
->coord_components
; ++i
)
2583 srcs
[i
] = applyProjection(getSrc(&insn
->src
[coordsIdx
].src
, i
), proj
);
2585 // sometimes we get less args than target.getArgCount, but codegen expects the latter
2586 if (insn
->coord_components
) {
2587 uint32_t argCount
= target
.getArgCount();
2592 for (uint32_t i
= 0u; i
< (argCount
- insn
->coord_components
); ++i
)
2593 srcs
.push_back(getSSA());
2596 if (insn
->op
== nir_texop_texture_samples
)
2597 srcs
.push_back(zero
);
2598 else if (!insn
->num_srcs
)
2599 srcs
.push_back(loadImm(NULL
, 0));
2601 srcs
.push_back(getSrc(&insn
->src
[biasIdx
].src
, 0));
2603 srcs
.push_back(getSrc(&insn
->src
[lodIdx
].src
, 0));
2604 else if (op
== OP_TXF
)
2607 srcs
.push_back(getSrc(&insn
->src
[msIdx
].src
, 0));
2608 if (offsetIdx
!= -1)
2609 offsets
.push_back(&insn
->src
[offsetIdx
].src
);
2611 srcs
.push_back(applyProjection(getSrc(&insn
->src
[compIdx
].src
, 0), proj
));
2612 if (texOffIdx
!= -1) {
2613 srcs
.push_back(getSrc(&insn
->src
[texOffIdx
].src
, 0));
2614 texOffIdx
= srcs
.size() - 1;
2616 if (sampOffIdx
!= -1) {
2617 srcs
.push_back(getSrc(&insn
->src
[sampOffIdx
].src
, 0));
2618 sampOffIdx
= srcs
.size() - 1;
2621 r
= insn
->texture_index
;
2622 s
= insn
->sampler_index
;
2624 defs
.resize(newDefs
.size());
2625 for (uint8_t d
= 0u; d
< newDefs
.size(); ++d
) {
2626 defs
[d
] = newDefs
[d
];
2629 if (target
.isMS() || (op
== OP_TEX
&& prog
->getType() != Program::TYPE_FRAGMENT
))
2632 TexInstruction
*texi
= mkTex(op
, target
.getEnum(), r
, s
, defs
, srcs
);
2633 texi
->tex
.levelZero
= lz
;
2634 texi
->tex
.mask
= mask
;
2636 if (texOffIdx
!= -1)
2637 texi
->tex
.rIndirectSrc
= texOffIdx
;
2638 if (sampOffIdx
!= -1)
2639 texi
->tex
.sIndirectSrc
= sampOffIdx
;
2643 if (!target
.isShadow())
2644 texi
->tex
.gatherComp
= insn
->component
;
2647 texi
->tex
.query
= TXQ_DIMS
;
2649 case nir_texop_texture_samples
:
2650 texi
->tex
.mask
= 0x4;
2651 texi
->tex
.query
= TXQ_TYPE
;
2653 case nir_texop_query_levels
:
2654 texi
->tex
.mask
= 0x8;
2655 texi
->tex
.query
= TXQ_DIMS
;
2661 texi
->tex
.useOffsets
= offsets
.size();
2662 if (texi
->tex
.useOffsets
) {
2663 for (uint8_t s
= 0; s
< texi
->tex
.useOffsets
; ++s
) {
2664 for (uint32_t c
= 0u; c
< 3; ++c
) {
2665 uint8_t s2
= std::min(c
, target
.getDim() - 1);
2666 texi
->offset
[s
][c
].set(getSrc(offsets
[s
], s2
));
2667 texi
->offset
[s
][c
].setInsn(texi
);
2672 if (ddxIdx
!= -1 && ddyIdx
!= -1) {
2673 for (uint8_t c
= 0u; c
< target
.getDim() + target
.isCube(); ++c
) {
2674 texi
->dPdx
[c
].set(getSrc(&insn
->src
[ddxIdx
].src
, c
));
2675 texi
->dPdy
[c
].set(getSrc(&insn
->src
[ddyIdx
].src
, c
));
2682 ERROR("unknown nir_texop %u\n", insn
->op
);
2693 if (prog
->dbgFlags
& NV50_IR_DEBUG_VERBOSE
)
2694 nir_print_shader(nir
, stderr
);
2696 struct nir_lower_subgroups_options subgroup_options
= {
2697 .subgroup_size
= 32,
2698 .ballot_bit_size
= 32,
2701 NIR_PASS_V(nir
, nir_lower_io
, nir_var_all
, type_size
, (nir_lower_io_options
)0);
2702 NIR_PASS_V(nir
, nir_lower_subgroups
, &subgroup_options
);
2703 NIR_PASS_V(nir
, nir_lower_regs_to_ssa
);
2704 NIR_PASS_V(nir
, nir_lower_load_const_to_scalar
);
2705 NIR_PASS_V(nir
, nir_lower_vars_to_ssa
);
2706 NIR_PASS_V(nir
, nir_lower_alu_to_scalar
);
2707 NIR_PASS_V(nir
, nir_lower_phis_to_scalar
);
2711 NIR_PASS(progress
, nir
, nir_copy_prop
);
2712 NIR_PASS(progress
, nir
, nir_opt_remove_phis
);
2713 NIR_PASS(progress
, nir
, nir_opt_trivial_continues
);
2714 NIR_PASS(progress
, nir
, nir_opt_cse
);
2715 NIR_PASS(progress
, nir
, nir_opt_algebraic
);
2716 NIR_PASS(progress
, nir
, nir_opt_constant_folding
);
2717 NIR_PASS(progress
, nir
, nir_copy_prop
);
2718 NIR_PASS(progress
, nir
, nir_opt_dce
);
2719 NIR_PASS(progress
, nir
, nir_opt_dead_cf
);
2722 NIR_PASS_V(nir
, nir_lower_bool_to_int32
);
2723 NIR_PASS_V(nir
, nir_lower_locals_to_regs
);
2724 NIR_PASS_V(nir
, nir_remove_dead_variables
, nir_var_function_temp
);
2725 NIR_PASS_V(nir
, nir_convert_from_ssa
, true);
2727 // Garbage collect dead instructions
2731 ERROR("Couldn't prase NIR!\n");
2735 if (!assignSlots()) {
2736 ERROR("Couldn't assign slots!\n");
2740 if (prog
->dbgFlags
& NV50_IR_DEBUG_BASIC
)
2741 nir_print_shader(nir
, stderr
);
2743 nir_foreach_function(function
, nir
) {
2744 if (!visit(function
))
2751 } // unnamed namespace
2756 Program::makeFromNIR(struct nv50_ir_prog_info
*info
)
2758 nir_shader
*nir
= (nir_shader
*)info
->bin
.source
;
2759 Converter
converter(this, nir
, info
);
2760 bool result
= converter
.run();
2763 LoweringHelper lowering
;
2765 tlsSize
= info
->bin
.tlsSpace
;
2769 } // namespace nv50_ir