Path: blob/21.2-virgl/src/gallium/drivers/radeonsi/si_shader_nir.c
4570 views
/*1* Copyright 2017 Advanced Micro Devices, Inc.2* All Rights Reserved.3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* on the rights to use, copy, modify, merge, publish, distribute, sub8* license, and/or sell copies of the Software, and to permit persons to whom9* the Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL18* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,19* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR20* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE21* USE OR OTHER DEALINGS IN THE SOFTWARE.22*/2324#include "ac_nir_to_llvm.h"25#include "ac_nir.h"26#include "compiler/nir/nir.h"27#include "compiler/nir/nir_builder.h"28#include "compiler/nir/nir_deref.h"29#include "compiler/nir_types.h"30#include "si_pipe.h"31#include "si_shader_internal.h"32#include "tgsi/tgsi_from_mesa.h"3334static const nir_src *get_texture_src(nir_tex_instr *instr, nir_tex_src_type type)35{36for (unsigned i = 0; i < instr->num_srcs; i++) {37if (instr->src[i].src_type == type)38return &instr->src[i].src;39}40return NULL;41}4243static void scan_io_usage(struct si_shader_info *info, nir_intrinsic_instr *intr,44bool is_input)45{46unsigned interp = INTERP_MODE_FLAT; /* load_input uses flat shading */4748if (intr->intrinsic == nir_intrinsic_load_interpolated_input) {49nir_intrinsic_instr *baryc = nir_instr_as_intrinsic(intr->src[0].ssa->parent_instr);5051if (baryc) {52if (nir_intrinsic_infos[baryc->intrinsic].index_map[NIR_INTRINSIC_INTERP_MODE] > 0)53interp = nir_intrinsic_interp_mode(baryc);54else55unreachable("unknown barycentric intrinsic");56} else {57unreachable("unknown barycentric expression");58}59}6061unsigned mask, bit_size;62bool is_output_load;6364if (nir_intrinsic_has_write_mask(intr)) {65mask = nir_intrinsic_write_mask(intr); /* store */66bit_size = nir_src_bit_size(intr->src[0]);67is_output_load = false;68} else {69mask = nir_ssa_def_components_read(&intr->dest.ssa); /* load */70bit_size = intr->dest.ssa.bit_size;71is_output_load = !is_input;72}73assert(bit_size != 64 && !(mask & ~0xf) && "64-bit IO should have been lowered");7475/* Convert the 16-bit component mask to a 32-bit component mask except for VS inputs76* where the mask is untyped.77*/78if (bit_size == 16 && !is_input) {79unsigned new_mask = 0;80for (unsigned i = 0; i < 4; i++) {81if (mask & (1 << i))82new_mask |= 0x1 << (i / 2);83}84mask = new_mask;85}8687mask <<= nir_intrinsic_component(intr);8889nir_src offset = *nir_get_io_offset_src(intr);90bool indirect = !nir_src_is_const(offset);91if (!indirect)92assert(nir_src_as_uint(offset) == 0);9394unsigned semantic = 0;95/* VS doesn't have semantics. */96if (info->stage != MESA_SHADER_VERTEX || !is_input)97semantic = nir_intrinsic_io_semantics(intr).location;9899if (info->stage == MESA_SHADER_FRAGMENT && !is_input) {100/* Never use FRAG_RESULT_COLOR directly. */101if (semantic == FRAG_RESULT_COLOR)102semantic = FRAG_RESULT_DATA0;103semantic += nir_intrinsic_io_semantics(intr).dual_source_blend_index;104}105106unsigned driver_location = nir_intrinsic_base(intr);107unsigned num_slots = indirect ? nir_intrinsic_io_semantics(intr).num_slots : 1;108109if (is_input) {110assert(driver_location + num_slots <= ARRAY_SIZE(info->input_usage_mask));111112for (unsigned i = 0; i < num_slots; i++) {113unsigned loc = driver_location + i;114115info->input_semantic[loc] = semantic + i;116info->input_interpolate[loc] = interp;117118if (mask) {119info->input_usage_mask[loc] |= mask;120if (bit_size == 16) {121if (nir_intrinsic_io_semantics(intr).high_16bits)122info->input_fp16_lo_hi_valid[loc] |= 0x2;123else124info->input_fp16_lo_hi_valid[loc] |= 0x1;125}126info->num_inputs = MAX2(info->num_inputs, loc + 1);127}128}129} else {130/* Outputs. */131assert(driver_location + num_slots <= ARRAY_SIZE(info->output_usagemask));132assert(semantic + num_slots < ARRAY_SIZE(info->output_semantic_to_slot));133134for (unsigned i = 0; i < num_slots; i++) {135unsigned loc = driver_location + i;136137info->output_semantic[loc] = semantic + i;138info->output_semantic_to_slot[semantic + i] = loc;139140if (is_output_load) {141/* Output loads have only a few things that we need to track. */142info->output_readmask[loc] |= mask;143} else if (mask) {144/* Output stores. */145unsigned gs_streams = (uint32_t)nir_intrinsic_io_semantics(intr).gs_streams <<146(nir_intrinsic_component(intr) * 2);147unsigned new_mask = mask & ~info->output_usagemask[loc];148149for (unsigned i = 0; i < 4; i++) {150unsigned stream = (gs_streams >> (i * 2)) & 0x3;151152if (new_mask & (1 << i)) {153info->output_streams[loc] |= stream << (i * 2);154info->num_stream_output_components[stream]++;155}156}157158if (nir_intrinsic_has_src_type(intr))159info->output_type[loc] = nir_intrinsic_src_type(intr);160else if (nir_intrinsic_has_dest_type(intr))161info->output_type[loc] = nir_intrinsic_dest_type(intr);162else163info->output_type[loc] = nir_type_float32;164165info->output_usagemask[loc] |= mask;166info->num_outputs = MAX2(info->num_outputs, loc + 1);167168if (info->stage == MESA_SHADER_FRAGMENT &&169semantic >= FRAG_RESULT_DATA0 && semantic <= FRAG_RESULT_DATA7) {170unsigned index = semantic - FRAG_RESULT_DATA0;171172if (nir_intrinsic_src_type(intr) == nir_type_float16)173info->output_color_types |= SI_TYPE_FLOAT16 << (index * 2);174else if (nir_intrinsic_src_type(intr) == nir_type_int16)175info->output_color_types |= SI_TYPE_INT16 << (index * 2);176else if (nir_intrinsic_src_type(intr) == nir_type_uint16)177info->output_color_types |= SI_TYPE_UINT16 << (index * 2);178}179}180}181}182}183184static bool is_bindless_handle_indirect(nir_instr *src)185{186/* Check if the bindless handle comes from indirect load_ubo. */187if (src->type == nir_instr_type_intrinsic &&188nir_instr_as_intrinsic(src)->intrinsic == nir_intrinsic_load_ubo) {189if (!nir_src_is_const(nir_instr_as_intrinsic(src)->src[0]))190return true;191} else {192/* Some other instruction. Return the worst-case result. */193return true;194}195return false;196}197198static void scan_instruction(const struct nir_shader *nir, struct si_shader_info *info,199nir_instr *instr)200{201if (instr->type == nir_instr_type_tex) {202nir_tex_instr *tex = nir_instr_as_tex(instr);203const nir_src *handle = get_texture_src(tex, nir_tex_src_texture_handle);204205/* Gather the types of used VMEM instructions that return something. */206switch (tex->op) {207case nir_texop_tex:208case nir_texop_txb:209case nir_texop_txl:210case nir_texop_txd:211case nir_texop_lod:212case nir_texop_tg4:213info->uses_vmem_return_type_sampler_or_bvh = true;214break;215default:216info->uses_vmem_return_type_other = true;217break;218}219220if (handle) {221info->uses_bindless_samplers = true;222223if (is_bindless_handle_indirect(handle->ssa->parent_instr))224info->uses_indirect_descriptor = true;225} else {226const nir_src *deref = get_texture_src(tex, nir_tex_src_texture_deref);227228if (nir_deref_instr_has_indirect(nir_src_as_deref(*deref)))229info->uses_indirect_descriptor = true;230}231} else if (instr->type == nir_instr_type_intrinsic) {232nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);233const char *intr_name = nir_intrinsic_infos[intr->intrinsic].name;234bool is_ssbo = strstr(intr_name, "ssbo");235bool is_image = strstr(intr_name, "image_deref");236bool is_bindless_image = strstr(intr_name, "bindless_image");237238/* Gather the types of used VMEM instructions that return something. */239if (nir_intrinsic_infos[intr->intrinsic].has_dest) {240switch (intr->intrinsic) {241case nir_intrinsic_load_ubo:242if (!nir_src_is_const(intr->src[1]))243info->uses_vmem_return_type_other = true;244break;245246case nir_intrinsic_load_barycentric_at_sample: /* This loads sample positions. */247case nir_intrinsic_load_tess_level_outer: /* TES input read from memory */248case nir_intrinsic_load_tess_level_inner: /* TES input read from memory */249info->uses_vmem_return_type_other = true;250break;251252case nir_intrinsic_load_input:253case nir_intrinsic_load_input_vertex:254case nir_intrinsic_load_per_vertex_input:255if (nir->info.stage == MESA_SHADER_VERTEX ||256nir->info.stage == MESA_SHADER_TESS_EVAL)257info->uses_vmem_return_type_other = true;258break;259260default:261if (is_image ||262is_bindless_image ||263is_ssbo ||264strstr(intr_name, "global") ||265strstr(intr_name, "scratch"))266info->uses_vmem_return_type_other = true;267break;268}269}270271if (is_bindless_image)272info->uses_bindless_images = true;273274if (strstr(intr_name, "image_atomic") ||275strstr(intr_name, "image_store") ||276strstr(intr_name, "image_deref_atomic") ||277strstr(intr_name, "image_deref_store") ||278strstr(intr_name, "ssbo_atomic") ||279intr->intrinsic == nir_intrinsic_store_ssbo)280info->num_memory_stores++;281282283if (is_image && nir_deref_instr_has_indirect(nir_src_as_deref(intr->src[0])))284info->uses_indirect_descriptor = true;285286if (is_bindless_image && is_bindless_handle_indirect(intr->src[0].ssa->parent_instr))287info->uses_indirect_descriptor = true;288289if (intr->intrinsic != nir_intrinsic_store_ssbo && is_ssbo &&290!nir_src_is_const(intr->src[0]))291info->uses_indirect_descriptor = true;292293switch (intr->intrinsic) {294case nir_intrinsic_store_ssbo:295if (!nir_src_is_const(intr->src[1]))296info->uses_indirect_descriptor = true;297break;298case nir_intrinsic_load_ubo:299if (!nir_src_is_const(intr->src[0]))300info->uses_indirect_descriptor = true;301break;302case nir_intrinsic_load_local_invocation_id:303case nir_intrinsic_load_workgroup_id: {304unsigned mask = nir_ssa_def_components_read(&intr->dest.ssa);305while (mask) {306unsigned i = u_bit_scan(&mask);307308if (intr->intrinsic == nir_intrinsic_load_workgroup_id)309info->uses_block_id[i] = true;310else311info->uses_thread_id[i] = true;312}313break;314}315case nir_intrinsic_load_color0:316case nir_intrinsic_load_color1: {317unsigned index = intr->intrinsic == nir_intrinsic_load_color1;318uint8_t mask = nir_ssa_def_components_read(&intr->dest.ssa);319info->colors_read |= mask << (index * 4);320321switch (info->color_interpolate[index]) {322case INTERP_MODE_SMOOTH:323if (info->color_interpolate_loc[index] == TGSI_INTERPOLATE_LOC_SAMPLE)324info->uses_persp_sample = true;325else if (info->color_interpolate_loc[index] == TGSI_INTERPOLATE_LOC_CENTROID)326info->uses_persp_centroid = true;327else if (info->color_interpolate_loc[index] == TGSI_INTERPOLATE_LOC_CENTER)328info->uses_persp_center = true;329break;330case INTERP_MODE_NOPERSPECTIVE:331if (info->color_interpolate_loc[index] == TGSI_INTERPOLATE_LOC_SAMPLE)332info->uses_linear_sample = true;333else if (info->color_interpolate_loc[index] == TGSI_INTERPOLATE_LOC_CENTROID)334info->uses_linear_centroid = true;335else if (info->color_interpolate_loc[index] == TGSI_INTERPOLATE_LOC_CENTER)336info->uses_linear_center = true;337break;338case INTERP_MODE_COLOR:339/* We don't know the final value. This will be FLAT if flatshading is enabled340* in the rasterizer state, otherwise it will be SMOOTH.341*/342info->uses_interp_color = true;343if (info->color_interpolate_loc[index] == TGSI_INTERPOLATE_LOC_SAMPLE)344info->uses_persp_sample_color = true;345else if (info->color_interpolate_loc[index] == TGSI_INTERPOLATE_LOC_CENTROID)346info->uses_persp_centroid_color = true;347else if (info->color_interpolate_loc[index] == TGSI_INTERPOLATE_LOC_CENTER)348info->uses_persp_center_color = true;349break;350}351break;352}353case nir_intrinsic_load_barycentric_at_offset: /* uses center */354case nir_intrinsic_load_barycentric_at_sample: /* uses center */355if (nir_intrinsic_interp_mode(intr) == INTERP_MODE_FLAT)356break;357358if (nir_intrinsic_interp_mode(intr) == INTERP_MODE_NOPERSPECTIVE) {359info->uses_linear_center = true;360} else {361info->uses_persp_center = true;362}363if (intr->intrinsic == nir_intrinsic_load_barycentric_at_sample)364info->uses_interp_at_sample = true;365break;366case nir_intrinsic_load_input:367case nir_intrinsic_load_per_vertex_input:368case nir_intrinsic_load_input_vertex:369case nir_intrinsic_load_interpolated_input:370scan_io_usage(info, intr, true);371break;372case nir_intrinsic_load_output:373case nir_intrinsic_load_per_vertex_output:374case nir_intrinsic_store_output:375case nir_intrinsic_store_per_vertex_output:376scan_io_usage(info, intr, false);377break;378case nir_intrinsic_load_deref:379case nir_intrinsic_store_deref:380case nir_intrinsic_interp_deref_at_centroid:381case nir_intrinsic_interp_deref_at_sample:382case nir_intrinsic_interp_deref_at_offset:383unreachable("these opcodes should have been lowered");384break;385default:386break;387}388}389}390391void si_nir_scan_shader(const struct nir_shader *nir, struct si_shader_info *info)392{393nir_function *func;394395info->base = nir->info;396info->stage = nir->info.stage;397398if (nir->info.stage == MESA_SHADER_TESS_EVAL) {399if (info->base.tess.primitive_mode == GL_ISOLINES)400info->base.tess.primitive_mode = GL_LINES;401}402403if (nir->info.stage == MESA_SHADER_FRAGMENT) {404/* post_depth_coverage implies early_fragment_tests */405info->base.fs.early_fragment_tests |= info->base.fs.post_depth_coverage;406407info->color_interpolate[0] = nir->info.fs.color0_interp;408info->color_interpolate[1] = nir->info.fs.color1_interp;409for (unsigned i = 0; i < 2; i++) {410if (info->color_interpolate[i] == INTERP_MODE_NONE)411info->color_interpolate[i] = INTERP_MODE_COLOR;412}413414info->color_interpolate_loc[0] = nir->info.fs.color0_sample ? TGSI_INTERPOLATE_LOC_SAMPLE :415nir->info.fs.color0_centroid ? TGSI_INTERPOLATE_LOC_CENTROID :416TGSI_INTERPOLATE_LOC_CENTER;417info->color_interpolate_loc[1] = nir->info.fs.color1_sample ? TGSI_INTERPOLATE_LOC_SAMPLE :418nir->info.fs.color1_centroid ? TGSI_INTERPOLATE_LOC_CENTROID :419TGSI_INTERPOLATE_LOC_CENTER;420/* Set an invalid value. Will be determined at draw time if needed when the expected421* conditions are met.422*/423info->writes_1_if_tex_is_1 = nir->info.writes_memory ? 0 : 0xff;424}425426info->constbuf0_num_slots = nir->num_uniforms;427428if (nir->info.stage == MESA_SHADER_TESS_CTRL) {429info->tessfactors_are_def_in_all_invocs = ac_are_tessfactors_def_in_all_invocs(nir);430}431432info->uses_frontface = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_FRONT_FACE);433info->uses_instanceid = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_INSTANCE_ID);434info->uses_base_vertex = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_BASE_VERTEX);435info->uses_base_instance = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_BASE_INSTANCE);436info->uses_invocationid = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_INVOCATION_ID);437info->uses_grid_size = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_NUM_WORKGROUPS);438info->uses_subgroup_info = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_LOCAL_INVOCATION_INDEX) ||439BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_SUBGROUP_ID) ||440BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_NUM_SUBGROUPS);441info->uses_variable_block_size = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_WORKGROUP_SIZE);442info->uses_drawid = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_DRAW_ID);443info->uses_primid = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_PRIMITIVE_ID) ||444nir->info.inputs_read & VARYING_BIT_PRIMITIVE_ID;445info->reads_samplemask = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_SAMPLE_MASK_IN);446info->reads_tess_factors = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_TESS_LEVEL_INNER) ||447BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_TESS_LEVEL_OUTER);448info->uses_linear_sample = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_BARYCENTRIC_LINEAR_SAMPLE);449info->uses_linear_centroid = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_BARYCENTRIC_LINEAR_CENTROID);450info->uses_linear_center = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_BARYCENTRIC_LINEAR_PIXEL);451info->uses_persp_sample = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_BARYCENTRIC_PERSP_SAMPLE);452info->uses_persp_centroid = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_BARYCENTRIC_PERSP_CENTROID);453info->uses_persp_center = BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_BARYCENTRIC_PERSP_PIXEL);454455if (nir->info.stage == MESA_SHADER_FRAGMENT) {456info->writes_z = nir->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_DEPTH);457info->writes_stencil = nir->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_STENCIL);458info->writes_samplemask = nir->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK);459460info->colors_written = nir->info.outputs_written >> FRAG_RESULT_DATA0;461if (nir->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_COLOR)) {462info->color0_writes_all_cbufs = true;463info->colors_written |= 0x1;464}465if (nir->info.fs.color_is_dual_source)466info->colors_written |= 0x2;467} else {468info->writes_primid = nir->info.outputs_written & VARYING_BIT_PRIMITIVE_ID;469info->writes_viewport_index = nir->info.outputs_written & VARYING_BIT_VIEWPORT;470info->writes_layer = nir->info.outputs_written & VARYING_BIT_LAYER;471info->writes_psize = nir->info.outputs_written & VARYING_BIT_PSIZ;472info->writes_clipvertex = nir->info.outputs_written & VARYING_BIT_CLIP_VERTEX;473info->writes_edgeflag = nir->info.outputs_written & VARYING_BIT_EDGE;474info->writes_position = nir->info.outputs_written & VARYING_BIT_POS;475}476477memset(info->output_semantic_to_slot, -1, sizeof(info->output_semantic_to_slot));478479func = (struct nir_function *)exec_list_get_head_const(&nir->functions);480nir_foreach_block (block, func->impl) {481nir_foreach_instr (instr, block)482scan_instruction(nir, info, instr);483}484485if (nir->info.stage == MESA_SHADER_FRAGMENT) {486info->allow_flat_shading = !(info->uses_persp_center || info->uses_persp_centroid ||487info->uses_persp_sample || info->uses_linear_center ||488info->uses_linear_centroid || info->uses_linear_sample ||489info->uses_interp_at_sample || nir->info.writes_memory ||490nir->info.fs.uses_fbfetch_output ||491nir->info.fs.needs_quad_helper_invocations ||492BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_FRAG_COORD) ||493BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_POINT_COORD) ||494BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_SAMPLE_ID) ||495BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_SAMPLE_POS) ||496BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_SAMPLE_MASK_IN) ||497BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_HELPER_INVOCATION));498}499500/* Add color inputs to the list of inputs. */501if (nir->info.stage == MESA_SHADER_FRAGMENT) {502for (unsigned i = 0; i < 2; i++) {503if ((info->colors_read >> (i * 4)) & 0xf) {504info->input_semantic[info->num_inputs] = VARYING_SLOT_COL0 + i;505info->input_interpolate[info->num_inputs] = info->color_interpolate[i];506info->input_usage_mask[info->num_inputs] = info->colors_read >> (i * 4);507info->num_inputs++;508}509}510}511512/* Trim output read masks based on write masks. */513for (unsigned i = 0; i < info->num_outputs; i++)514info->output_readmask[i] &= info->output_usagemask[i];515}516517static bool si_alu_to_scalar_filter(const nir_instr *instr, const void *data)518{519struct si_screen *sscreen = (struct si_screen *)data;520521if (sscreen->options.fp16 &&522instr->type == nir_instr_type_alu) {523nir_alu_instr *alu = nir_instr_as_alu(instr);524525if (alu->dest.dest.is_ssa &&526alu->dest.dest.ssa.bit_size == 16 &&527alu->dest.dest.ssa.num_components == 2)528return false;529}530531return true;532}533534void si_nir_opts(struct si_screen *sscreen, struct nir_shader *nir, bool first)535{536bool progress;537538NIR_PASS_V(nir, nir_lower_vars_to_ssa);539NIR_PASS_V(nir, nir_lower_alu_to_scalar, si_alu_to_scalar_filter, sscreen);540NIR_PASS_V(nir, nir_lower_phis_to_scalar, false);541542do {543progress = false;544bool lower_alu_to_scalar = false;545bool lower_phis_to_scalar = false;546547if (first) {548NIR_PASS(progress, nir, nir_split_array_vars, nir_var_function_temp);549NIR_PASS(lower_alu_to_scalar, nir, nir_shrink_vec_array_vars, nir_var_function_temp);550NIR_PASS(progress, nir, nir_opt_find_array_copies);551}552NIR_PASS(progress, nir, nir_opt_copy_prop_vars);553NIR_PASS(progress, nir, nir_opt_dead_write_vars);554555NIR_PASS(lower_alu_to_scalar, nir, nir_opt_trivial_continues);556/* (Constant) copy propagation is needed for txf with offsets. */557NIR_PASS(progress, nir, nir_copy_prop);558NIR_PASS(progress, nir, nir_opt_remove_phis);559NIR_PASS(progress, nir, nir_opt_dce);560NIR_PASS(lower_phis_to_scalar, nir, nir_opt_if, true);561NIR_PASS(progress, nir, nir_opt_dead_cf);562563if (lower_alu_to_scalar)564NIR_PASS_V(nir, nir_lower_alu_to_scalar, si_alu_to_scalar_filter, sscreen);565if (lower_phis_to_scalar)566NIR_PASS_V(nir, nir_lower_phis_to_scalar, false);567progress |= lower_alu_to_scalar | lower_phis_to_scalar;568569NIR_PASS(progress, nir, nir_opt_cse);570NIR_PASS(progress, nir, nir_opt_peephole_select, 8, true, true);571572/* Needed for algebraic lowering */573NIR_PASS(progress, nir, nir_opt_algebraic);574NIR_PASS(progress, nir, nir_opt_constant_folding);575576if (!nir->info.flrp_lowered) {577unsigned lower_flrp = (nir->options->lower_flrp16 ? 16 : 0) |578(nir->options->lower_flrp32 ? 32 : 0) |579(nir->options->lower_flrp64 ? 64 : 0);580assert(lower_flrp);581bool lower_flrp_progress = false;582583NIR_PASS(lower_flrp_progress, nir, nir_lower_flrp, lower_flrp, false /* always_precise */);584if (lower_flrp_progress) {585NIR_PASS(progress, nir, nir_opt_constant_folding);586progress = true;587}588589/* Nothing should rematerialize any flrps, so we only590* need to do this lowering once.591*/592nir->info.flrp_lowered = true;593}594595NIR_PASS(progress, nir, nir_opt_undef);596NIR_PASS(progress, nir, nir_opt_conditional_discard);597if (nir->options->max_unroll_iterations) {598NIR_PASS(progress, nir, nir_opt_loop_unroll, 0);599}600601if (nir->info.stage == MESA_SHADER_FRAGMENT)602NIR_PASS_V(nir, nir_opt_move_discards_to_top);603604if (sscreen->options.fp16)605NIR_PASS(progress, nir, nir_opt_vectorize, NULL, NULL);606} while (progress);607608NIR_PASS_V(nir, nir_lower_var_copies);609}610611void si_nir_late_opts(nir_shader *nir)612{613bool more_late_algebraic = true;614while (more_late_algebraic) {615more_late_algebraic = false;616NIR_PASS(more_late_algebraic, nir, nir_opt_algebraic_late);617NIR_PASS_V(nir, nir_opt_constant_folding);618NIR_PASS_V(nir, nir_copy_prop);619NIR_PASS_V(nir, nir_opt_dce);620NIR_PASS_V(nir, nir_opt_cse);621}622}623624static void si_late_optimize_16bit_samplers(struct si_screen *sscreen, nir_shader *nir)625{626/* Optimize and fix types of image_sample sources and destinations.627*628* The image_sample constraints are:629* nir_tex_src_coord: has_a16 ? select 16 or 32 : 32630* nir_tex_src_comparator: 32631* nir_tex_src_offset: 32632* nir_tex_src_bias: 32633* nir_tex_src_lod: match coord634* nir_tex_src_min_lod: match coord635* nir_tex_src_ms_index: match coord636* nir_tex_src_ddx: has_g16 && coord == 32 ? select 16 or 32 : match coord637* nir_tex_src_ddy: match ddy638*639* coord and ddx are selected optimally. The types of the rest are legalized640* based on those two.641*/642/* TODO: The constraints can't represent the ddx constraint. */643/*bool has_g16 = sscreen->info.chip_class >= GFX10 && LLVM_VERSION_MAJOR >= 12;*/644bool has_g16 = false;645nir_tex_src_type_constraints tex_constraints = {646[nir_tex_src_comparator] = {true, 32},647[nir_tex_src_offset] = {true, 32},648[nir_tex_src_bias] = {true, 32},649[nir_tex_src_lod] = {true, 0, nir_tex_src_coord},650[nir_tex_src_min_lod] = {true, 0, nir_tex_src_coord},651[nir_tex_src_ms_index] = {true, 0, nir_tex_src_coord},652[nir_tex_src_ddx] = {!has_g16, 0, nir_tex_src_coord},653[nir_tex_src_ddy] = {true, 0, has_g16 ? nir_tex_src_ddx : nir_tex_src_coord},654};655bool changed = false;656657NIR_PASS(changed, nir, nir_fold_16bit_sampler_conversions,658(1 << nir_tex_src_coord) |659(has_g16 ? 1 << nir_tex_src_ddx : 0));660NIR_PASS(changed, nir, nir_legalize_16bit_sampler_srcs, tex_constraints);661662if (changed) {663si_nir_opts(sscreen, nir, false);664si_nir_late_opts(nir);665}666}667668static int type_size_vec4(const struct glsl_type *type, bool bindless)669{670return glsl_count_attribute_slots(type, false);671}672673static void si_nir_lower_color(nir_shader *nir)674{675nir_function_impl *entrypoint = nir_shader_get_entrypoint(nir);676677nir_builder b;678nir_builder_init(&b, entrypoint);679680nir_foreach_block (block, entrypoint) {681nir_foreach_instr_safe (instr, block) {682if (instr->type != nir_instr_type_intrinsic)683continue;684685nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);686687if (intrin->intrinsic != nir_intrinsic_load_deref)688continue;689690nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);691if (!nir_deref_mode_is(deref, nir_var_shader_in))692continue;693694b.cursor = nir_before_instr(instr);695nir_variable *var = nir_deref_instr_get_variable(deref);696nir_ssa_def *def;697698if (var->data.location == VARYING_SLOT_COL0) {699def = nir_load_color0(&b);700nir->info.fs.color0_interp = var->data.interpolation;701nir->info.fs.color0_sample = var->data.sample;702nir->info.fs.color0_centroid = var->data.centroid;703} else if (var->data.location == VARYING_SLOT_COL1) {704def = nir_load_color1(&b);705nir->info.fs.color1_interp = var->data.interpolation;706nir->info.fs.color1_sample = var->data.sample;707nir->info.fs.color1_centroid = var->data.centroid;708} else {709continue;710}711712nir_ssa_def_rewrite_uses(&intrin->dest.ssa, def);713nir_instr_remove(instr);714}715}716}717718static void si_lower_io(struct nir_shader *nir)719{720/* HW supports indirect indexing for: | Enabled in driver721* -------------------------------------------------------722* TCS inputs | Yes723* TES inputs | Yes724* GS inputs | No725* -------------------------------------------------------726* VS outputs before TCS | No727* TCS outputs | Yes728* VS/TES outputs before GS | No729*/730bool has_indirect_inputs = nir->info.stage == MESA_SHADER_TESS_CTRL ||731nir->info.stage == MESA_SHADER_TESS_EVAL;732bool has_indirect_outputs = nir->info.stage == MESA_SHADER_TESS_CTRL;733734if (!has_indirect_inputs || !has_indirect_outputs) {735NIR_PASS_V(nir, nir_lower_io_to_temporaries, nir_shader_get_entrypoint(nir),736!has_indirect_outputs, !has_indirect_inputs);737738/* Since we're doing nir_lower_io_to_temporaries late, we need739* to lower all the copy_deref's introduced by740* lower_io_to_temporaries before calling nir_lower_io.741*/742NIR_PASS_V(nir, nir_split_var_copies);743NIR_PASS_V(nir, nir_lower_var_copies);744NIR_PASS_V(nir, nir_lower_global_vars_to_local);745}746747/* The vectorization must be done after nir_lower_io_to_temporaries, because748* nir_lower_io_to_temporaries after vectorization breaks:749* piglit/bin/arb_gpu_shader5-interpolateAtOffset -auto -fbo750* TODO: It's probably a bug in nir_lower_io_to_temporaries.751*752* The vectorizer can only vectorize this:753* op src0.x, src1.x754* op src0.y, src1.y755*756* So it requires that inputs are already vectors and it must be the same757* vector between instructions. The vectorizer doesn't create vectors758* from independent scalar sources, so vectorize inputs.759*760* TODO: The pass fails this for VS: assert(b.shader->info.stage != MESA_SHADER_VERTEX);761*/762if (nir->info.stage != MESA_SHADER_VERTEX)763NIR_PASS_V(nir, nir_lower_io_to_vector, nir_var_shader_in);764765/* Vectorize outputs, so that we don't split vectors before storing outputs. */766/* TODO: The pass fails an assertion for other shader stages. */767if (nir->info.stage == MESA_SHADER_TESS_CTRL ||768nir->info.stage == MESA_SHADER_FRAGMENT)769NIR_PASS_V(nir, nir_lower_io_to_vector, nir_var_shader_out);770771if (nir->info.stage == MESA_SHADER_FRAGMENT)772si_nir_lower_color(nir);773774NIR_PASS_V(nir, nir_lower_io, nir_var_shader_out | nir_var_shader_in,775type_size_vec4, nir_lower_io_lower_64bit_to_32);776nir->info.io_lowered = true;777778/* This pass needs actual constants */779NIR_PASS_V(nir, nir_opt_constant_folding);780NIR_PASS_V(nir, nir_io_add_const_offset_to_base, nir_var_shader_in |781nir_var_shader_out);782783/* Remove dead derefs, so that nir_validate doesn't fail. */784NIR_PASS_V(nir, nir_opt_dce);785786/* Remove input and output nir_variables, because we don't need them787* anymore. Also remove uniforms, because those should have been lowered788* to UBOs already.789*/790unsigned modes = nir_var_shader_in | nir_var_shader_out | nir_var_uniform;791nir_foreach_variable_with_modes_safe(var, nir, modes) {792if (var->data.mode == nir_var_uniform &&793(glsl_type_get_image_count(var->type) ||794glsl_type_get_sampler_count(var->type)))795continue;796797exec_node_remove(&var->node);798}799}800801/**802* Perform "lowering" operations on the NIR that are run once when the shader803* selector is created.804*/805static void si_lower_nir(struct si_screen *sscreen, struct nir_shader *nir)806{807/* Perform lowerings (and optimizations) of code.808*809* Performance considerations aside, we must:810* - lower certain ALU operations811* - ensure constant offsets for texture instructions are folded812* and copy-propagated813*/814815static const struct nir_lower_tex_options lower_tex_options = {816.lower_txp = ~0u,817};818NIR_PASS_V(nir, nir_lower_tex, &lower_tex_options);819820const nir_lower_subgroups_options subgroups_options = {821.subgroup_size = 64,822.ballot_bit_size = 64,823.ballot_components = 1,824.lower_to_scalar = true,825.lower_subgroup_masks = true,826.lower_vote_trivial = false,827.lower_vote_eq = true,828.lower_elect = true,829};830NIR_PASS_V(nir, nir_lower_subgroups, &subgroups_options);831832NIR_PASS_V(nir, nir_lower_discard_or_demote,833sscreen->debug_flags & DBG(FS_CORRECT_DERIVS_AFTER_KILL));834835/* Lower load constants to scalar and then clean up the mess */836NIR_PASS_V(nir, nir_lower_load_const_to_scalar);837NIR_PASS_V(nir, nir_lower_var_copies);838NIR_PASS_V(nir, nir_opt_intrinsics);839NIR_PASS_V(nir, nir_lower_system_values);840NIR_PASS_V(nir, nir_lower_compute_system_values, NULL);841842if (nir->info.stage == MESA_SHADER_COMPUTE) {843if (nir->info.cs.derivative_group == DERIVATIVE_GROUP_QUADS) {844/* If we are shuffling local_invocation_id for quad derivatives, we845* need to derive local_invocation_index from local_invocation_id846* first, so that the value corresponds to the shuffled847* local_invocation_id.848*/849nir_lower_compute_system_values_options options = {0};850options.lower_local_invocation_index = true;851NIR_PASS_V(nir, nir_lower_compute_system_values, &options);852}853854nir_opt_cse(nir); /* CSE load_local_invocation_id */855nir_lower_compute_system_values_options options = {0};856options.shuffle_local_ids_for_quad_derivatives = true;857NIR_PASS_V(nir, nir_lower_compute_system_values, &options);858}859860if (sscreen->b.get_shader_param(&sscreen->b, PIPE_SHADER_FRAGMENT, PIPE_SHADER_CAP_FP16)) {861NIR_PASS_V(nir, nir_lower_mediump_io,862/* TODO: LLVM fails to compile this test if VS inputs are 16-bit:863* dEQP-GLES31.functional.shaders.builtin_functions.integer.bitfieldinsert.uvec3_lowp_geometry864*/865(nir->info.stage != MESA_SHADER_VERTEX ? nir_var_shader_in : 0) | nir_var_shader_out,866BITFIELD64_BIT(VARYING_SLOT_PNTC) | BITFIELD64_RANGE(VARYING_SLOT_VAR0, 32),867true);868}869870si_nir_opts(sscreen, nir, true);871872/* Lower large variables that are always constant with load_constant873* intrinsics, which get turned into PC-relative loads from a data874* section next to the shader.875*876* st/mesa calls finalize_nir twice, but we can't call this pass twice.877*/878bool changed = false;879if (!nir->constant_data) {880/* The pass crashes if there are dead temps of lowered IO interface types. */881NIR_PASS_V(nir, nir_remove_dead_variables, nir_var_function_temp, NULL);882NIR_PASS(changed, nir, nir_opt_large_constants, glsl_get_natural_size_align_bytes, 16);883}884885changed |= ac_nir_lower_indirect_derefs(nir, sscreen->info.chip_class);886if (changed)887si_nir_opts(sscreen, nir, false);888889/* Run late optimizations to fuse ffma and eliminate 16-bit conversions. */890si_nir_late_opts(nir);891892if (sscreen->b.get_shader_param(&sscreen->b, PIPE_SHADER_FRAGMENT, PIPE_SHADER_CAP_FP16))893si_late_optimize_16bit_samplers(sscreen, nir);894895NIR_PASS_V(nir, nir_remove_dead_variables, nir_var_function_temp, NULL);896}897898void si_finalize_nir(struct pipe_screen *screen, void *nirptr, bool optimize)899{900struct si_screen *sscreen = (struct si_screen *)screen;901struct nir_shader *nir = (struct nir_shader *)nirptr;902903si_lower_io(nir);904si_lower_nir(sscreen, nir);905nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));906907if (sscreen->options.inline_uniforms)908nir_find_inlinable_uniforms(nir);909}910911912