Path: blob/21.2-virgl/src/freedreno/ir3/ir3_legalize.c
4565 views
/*1* Copyright (C) 2014 Rob Clark <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*22* Authors:23* Rob Clark <[email protected]>24*/2526#include "util/ralloc.h"27#include "util/u_math.h"2829#include "ir3.h"30#include "ir3_shader.h"3132/*33* Legalize:34*35* The legalize pass handles ensuring sufficient nop's and sync flags for36* correct execution.37*38* 1) Iteratively determine where sync ((sy)/(ss)) flags are needed,39* based on state flowing out of predecessor blocks until there is40* no further change. In some cases this requires inserting nops.41* 2) Mark (ei) on last varying input, and (ul) on last use of a0.x42* 3) Final nop scheduling for instruction latency43* 4) Resolve jumps and schedule blocks, marking potential convergence44* points with (jp)45*/4647struct ir3_legalize_ctx {48struct ir3_compiler *compiler;49struct ir3_shader_variant *so;50gl_shader_stage type;51int max_bary;52bool early_input_release;53};5455struct ir3_legalize_state {56regmask_t needs_ss;57regmask_t needs_ss_war; /* write after read */58regmask_t needs_sy;59};6061struct ir3_legalize_block_data {62bool valid;63struct ir3_legalize_state state;64};6566/* We want to evaluate each block from the position of any other67* predecessor block, in order that the flags set are the union of68* all possible program paths.69*70* To do this, we need to know the output state (needs_ss/ss_war/sy)71* of all predecessor blocks. The tricky thing is loops, which mean72* that we can't simply recursively process each predecessor block73* before legalizing the current block.74*75* How we handle that is by looping over all the blocks until the76* results converge. If the output state of a given block changes77* in a given pass, this means that all successor blocks are not78* yet fully legalized.79*/8081static bool82legalize_block(struct ir3_legalize_ctx *ctx, struct ir3_block *block)83{84struct ir3_legalize_block_data *bd = block->data;8586if (bd->valid)87return false;8889struct ir3_instruction *last_rel = NULL;90struct ir3_instruction *last_n = NULL;91struct list_head instr_list;92struct ir3_legalize_state prev_state = bd->state;93struct ir3_legalize_state *state = &bd->state;94bool last_input_needs_ss = false;95bool has_tex_prefetch = false;96bool mergedregs = ctx->so->mergedregs;9798/* our input state is the OR of all predecessor blocks' state: */99for (unsigned i = 0; i < block->predecessors_count; i++) {100struct ir3_block *predecessor = block->predecessors[i];101struct ir3_legalize_block_data *pbd = predecessor->data;102struct ir3_legalize_state *pstate = &pbd->state;103104/* Our input (ss)/(sy) state is based on OR'ing the output105* state of all our predecessor blocks106*/107regmask_or(&state->needs_ss, &state->needs_ss, &pstate->needs_ss);108regmask_or(&state->needs_ss_war, &state->needs_ss_war,109&pstate->needs_ss_war);110regmask_or(&state->needs_sy, &state->needs_sy, &pstate->needs_sy);111}112113unsigned input_count = 0;114115foreach_instr (n, &block->instr_list) {116if (is_input(n)) {117input_count++;118}119}120121unsigned inputs_remaining = input_count;122123/* Either inputs are in the first block or we expect inputs to be released124* with the end of the program.125*/126assert(input_count == 0 || !ctx->early_input_release ||127block == ir3_start_block(block->shader));128129/* remove all the instructions from the list, we'll be adding130* them back in as we go131*/132list_replace(&block->instr_list, &instr_list);133list_inithead(&block->instr_list);134135foreach_instr_safe (n, &instr_list) {136unsigned i;137138n->flags &= ~(IR3_INSTR_SS | IR3_INSTR_SY);139140/* _meta::tex_prefetch instructions removed later in141* collect_tex_prefetches()142*/143if (is_meta(n) && (n->opc != OPC_META_TEX_PREFETCH))144continue;145146if (is_input(n)) {147struct ir3_register *inloc = n->srcs[0];148assert(inloc->flags & IR3_REG_IMMED);149ctx->max_bary = MAX2(ctx->max_bary, inloc->iim_val);150}151152if (last_n && is_barrier(last_n)) {153n->flags |= IR3_INSTR_SS | IR3_INSTR_SY;154last_input_needs_ss = false;155regmask_init(&state->needs_ss_war, mergedregs);156regmask_init(&state->needs_ss, mergedregs);157regmask_init(&state->needs_sy, mergedregs);158}159160if (last_n && (last_n->opc == OPC_PREDT)) {161n->flags |= IR3_INSTR_SS;162regmask_init(&state->needs_ss_war, mergedregs);163regmask_init(&state->needs_ss, mergedregs);164}165166/* NOTE: consider dst register too.. it could happen that167* texture sample instruction (for example) writes some168* components which are unused. A subsequent instruction169* that writes the same register can race w/ the sam instr170* resulting in undefined results:171*/172for (i = 0; i < n->dsts_count + n->srcs_count; i++) {173struct ir3_register *reg;174if (i < n->dsts_count)175reg = n->dsts[i];176else177reg = n->srcs[i - n->dsts_count];178179if (reg_gpr(reg)) {180181/* TODO: we probably only need (ss) for alu182* instr consuming sfu result.. need to make183* some tests for both this and (sy)..184*/185if (regmask_get(&state->needs_ss, reg)) {186n->flags |= IR3_INSTR_SS;187last_input_needs_ss = false;188regmask_init(&state->needs_ss_war, mergedregs);189regmask_init(&state->needs_ss, mergedregs);190}191192if (regmask_get(&state->needs_sy, reg)) {193n->flags |= IR3_INSTR_SY;194regmask_init(&state->needs_sy, mergedregs);195}196}197198/* TODO: is it valid to have address reg loaded from a199* relative src (ie. mova a0, c<a0.x+4>)? If so, the200* last_rel check below should be moved ahead of this:201*/202if (reg->flags & IR3_REG_RELATIV)203last_rel = n;204}205206foreach_dst (reg, n) {207if (regmask_get(&state->needs_ss_war, reg)) {208n->flags |= IR3_INSTR_SS;209last_input_needs_ss = false;210regmask_init(&state->needs_ss_war, mergedregs);211regmask_init(&state->needs_ss, mergedregs);212}213214if (last_rel && (reg->num == regid(REG_A0, 0))) {215last_rel->flags |= IR3_INSTR_UL;216last_rel = NULL;217}218}219220/* cat5+ does not have an (ss) bit, if needed we need to221* insert a nop to carry the sync flag. Would be kinda222* clever if we were aware of this during scheduling, but223* this should be a pretty rare case:224*/225if ((n->flags & IR3_INSTR_SS) && (opc_cat(n->opc) >= 5)) {226struct ir3_instruction *nop;227nop = ir3_NOP(block);228nop->flags |= IR3_INSTR_SS;229n->flags &= ~IR3_INSTR_SS;230}231232/* need to be able to set (ss) on first instruction: */233if (list_is_empty(&block->instr_list) && (opc_cat(n->opc) >= 5))234ir3_NOP(block);235236if (ctx->compiler->samgq_workaround && ctx->type == MESA_SHADER_VERTEX &&237n->opc == OPC_SAMGQ) {238struct ir3_instruction *samgp;239240list_delinit(&n->node);241242for (i = 0; i < 4; i++) {243samgp = ir3_instr_clone(n);244samgp->opc = OPC_SAMGP0 + i;245if (i > 1)246samgp->flags |= IR3_INSTR_SY;247}248} else {249list_delinit(&n->node);250list_addtail(&n->node, &block->instr_list);251}252253if (is_sfu(n))254regmask_set(&state->needs_ss, n->dsts[0]);255256if (is_tex_or_prefetch(n)) {257regmask_set(&state->needs_sy, n->dsts[0]);258if (n->opc == OPC_META_TEX_PREFETCH)259has_tex_prefetch = true;260} else if (n->opc == OPC_RESINFO) {261regmask_set(&state->needs_ss, n->dsts[0]);262ir3_NOP(block)->flags |= IR3_INSTR_SS;263last_input_needs_ss = false;264} else if (is_load(n)) {265/* seems like ldlv needs (ss) bit instead?? which is odd but266* makes a bunch of flat-varying tests start working on a4xx.267*/268if ((n->opc == OPC_LDLV) || (n->opc == OPC_LDL) ||269(n->opc == OPC_LDLW))270regmask_set(&state->needs_ss, n->dsts[0]);271else272regmask_set(&state->needs_sy, n->dsts[0]);273} else if (is_atomic(n->opc)) {274if (n->flags & IR3_INSTR_G) {275if (ctx->compiler->gpu_id >= 600) {276/* New encoding, returns result via second src: */277regmask_set(&state->needs_sy, n->srcs[2]);278} else {279regmask_set(&state->needs_sy, n->dsts[0]);280}281} else {282regmask_set(&state->needs_ss, n->dsts[0]);283}284}285286if (is_ssbo(n->opc) || (is_atomic(n->opc) && (n->flags & IR3_INSTR_G)))287ctx->so->has_ssbo = true;288289/* both tex/sfu appear to not always immediately consume290* their src register(s):291*/292if (is_tex(n) || is_sfu(n) || is_mem(n)) {293foreach_src (reg, n) {294if (reg_gpr(reg))295regmask_set(&state->needs_ss_war, reg);296}297}298299if (ctx->early_input_release && is_input(n)) {300last_input_needs_ss |= (n->opc == OPC_LDLV);301302assert(inputs_remaining > 0);303inputs_remaining--;304if (inputs_remaining == 0) {305/* This is the last input. We add the (ei) flag to release306* varying memory after this executes. If it's an ldlv,307* however, we need to insert a dummy bary.f on which we can308* set the (ei) flag. We may also need to insert an (ss) to309* guarantee that all ldlv's have finished fetching their310* results before releasing the varying memory.311*/312struct ir3_instruction *last_input = n;313if (n->opc == OPC_LDLV) {314struct ir3_instruction *baryf;315316/* (ss)bary.f (ei)r63.x, 0, r0.x */317baryf = ir3_instr_create(block, OPC_BARY_F, 1, 2);318ir3_dst_create(baryf, regid(63, 0), 0);319ir3_src_create(baryf, 0, IR3_REG_IMMED)->iim_val = 0;320ir3_src_create(baryf, regid(0, 0), 0);321322last_input = baryf;323}324325last_input->dsts[0]->flags |= IR3_REG_EI;326if (last_input_needs_ss) {327last_input->flags |= IR3_INSTR_SS;328regmask_init(&state->needs_ss_war, mergedregs);329regmask_init(&state->needs_ss, mergedregs);330}331}332}333334last_n = n;335}336337assert(inputs_remaining == 0 || !ctx->early_input_release);338339if (has_tex_prefetch && input_count == 0) {340/* texture prefetch, but *no* inputs.. we need to insert a341* dummy bary.f at the top of the shader to unblock varying342* storage:343*/344struct ir3_instruction *baryf;345346/* (ss)bary.f (ei)r63.x, 0, r0.x */347baryf = ir3_instr_create(block, OPC_BARY_F, 1, 2);348ir3_dst_create(baryf, regid(63, 0), 0)->flags |= IR3_REG_EI;349ir3_src_create(baryf, 0, IR3_REG_IMMED)->iim_val = 0;350ir3_src_create(baryf, regid(0, 0), 0);351352/* insert the dummy bary.f at head: */353list_delinit(&baryf->node);354list_add(&baryf->node, &block->instr_list);355}356357if (last_rel)358last_rel->flags |= IR3_INSTR_UL;359360bd->valid = true;361362if (memcmp(&prev_state, state, sizeof(*state))) {363/* our output state changed, this invalidates all of our364* successors:365*/366for (unsigned i = 0; i < ARRAY_SIZE(block->successors); i++) {367if (!block->successors[i])368break;369struct ir3_legalize_block_data *pbd = block->successors[i]->data;370pbd->valid = false;371}372}373374return true;375}376377/* Expands dsxpp and dsypp macros to:378*379* dsxpp.1 dst, src380* dsxpp.1.p dst, src381*382* We apply this after flags syncing, as we don't want to sync in between the383* two (which might happen if dst == src). We do it before nop scheduling384* because that needs to count actual instructions.385*/386static bool387apply_fine_deriv_macro(struct ir3_legalize_ctx *ctx, struct ir3_block *block)388{389struct list_head instr_list;390391/* remove all the instructions from the list, we'll be adding392* them back in as we go393*/394list_replace(&block->instr_list, &instr_list);395list_inithead(&block->instr_list);396397foreach_instr_safe (n, &instr_list) {398list_addtail(&n->node, &block->instr_list);399400if (n->opc == OPC_DSXPP_MACRO || n->opc == OPC_DSYPP_MACRO) {401n->opc = (n->opc == OPC_DSXPP_MACRO) ? OPC_DSXPP_1 : OPC_DSYPP_1;402403struct ir3_instruction *op_p = ir3_instr_clone(n);404op_p->flags = IR3_INSTR_P;405406ctx->so->need_fine_derivatives = true;407}408}409410return true;411}412413/* NOTE: branch instructions are always the last instruction(s)414* in the block. We take advantage of this as we resolve the415* branches, since "if (foo) break;" constructs turn into416* something like:417*418* block3 {419* ...420* 0029:021: mov.s32s32 r62.x, r1.y421* 0082:022: br !p0.x, target=block5422* 0083:023: br p0.x, target=block4423* // succs: if _[0029:021: mov.s32s32] block4; else block5;424* }425* block4 {426* 0084:024: jump, target=block6427* // succs: block6;428* }429* block5 {430* 0085:025: jump, target=block7431* // succs: block7;432* }433*434* ie. only instruction in block4/block5 is a jump, so when435* resolving branches we can easily detect this by checking436* that the first instruction in the target block is itself437* a jump, and setup the br directly to the jump's target438* (and strip back out the now unreached jump)439*440* TODO sometimes we end up with things like:441*442* br !p0.x, #2443* br p0.x, #12444* add.u r0.y, r0.y, 1445*446* If we swapped the order of the branches, we could drop one.447*/448static struct ir3_block *449resolve_dest_block(struct ir3_block *block)450{451/* special case for last block: */452if (!block->successors[0])453return block;454455/* NOTE that we may or may not have inserted the jump456* in the target block yet, so conditions to resolve457* the dest to the dest block's successor are:458*459* (1) successor[1] == NULL &&460* (2) (block-is-empty || only-instr-is-jump)461*/462if (block->successors[1] == NULL) {463if (list_is_empty(&block->instr_list)) {464return block->successors[0];465} else if (list_length(&block->instr_list) == 1) {466struct ir3_instruction *instr =467list_first_entry(&block->instr_list, struct ir3_instruction, node);468if (instr->opc == OPC_JUMP) {469/* If this jump is backwards, then we will probably convert470* the jump being resolved to a backwards jump, which will471* change a loop-with-continue or loop-with-if into a472* doubly-nested loop and change the convergence behavior.473* Disallow this here.474*/475if (block->successors[0]->index <= block->index)476return block;477return block->successors[0];478}479}480}481return block;482}483484static void485remove_unused_block(struct ir3_block *old_target)486{487list_delinit(&old_target->node);488489/* cleanup dangling predecessors: */490for (unsigned i = 0; i < ARRAY_SIZE(old_target->successors); i++) {491if (old_target->successors[i]) {492struct ir3_block *succ = old_target->successors[i];493ir3_block_remove_predecessor(succ, old_target);494}495}496}497498static bool499retarget_jump(struct ir3_instruction *instr, struct ir3_block *new_target)500{501struct ir3_block *old_target = instr->cat0.target;502struct ir3_block *cur_block = instr->block;503504/* update current blocks successors to reflect the retargetting: */505if (cur_block->successors[0] == old_target) {506cur_block->successors[0] = new_target;507} else {508debug_assert(cur_block->successors[1] == old_target);509cur_block->successors[1] = new_target;510}511512/* update new target's predecessors: */513ir3_block_add_predecessor(new_target, cur_block);514515/* and remove old_target's predecessor: */516ir3_block_remove_predecessor(old_target, cur_block);517518instr->cat0.target = new_target;519520if (old_target->predecessors_count == 0) {521remove_unused_block(old_target);522return true;523}524525return false;526}527528static bool529opt_jump(struct ir3 *ir)530{531bool progress = false;532533unsigned index = 0;534foreach_block (block, &ir->block_list)535block->index = index++;536537foreach_block (block, &ir->block_list) {538foreach_instr (instr, &block->instr_list) {539if (!is_flow(instr) || !instr->cat0.target)540continue;541542struct ir3_block *tblock = resolve_dest_block(instr->cat0.target);543if (tblock != instr->cat0.target) {544progress = true;545546/* Exit early if we deleted a block to avoid iterator547* weirdness/assert fails548*/549if (retarget_jump(instr, tblock))550return true;551}552}553554/* Detect the case where the block ends either with:555* - A single unconditional jump to the next block.556* - Two jump instructions with opposite conditions, and one of the557* them jumps to the next block.558* We can remove the one that jumps to the next block in either case.559*/560if (list_is_empty(&block->instr_list))561continue;562563struct ir3_instruction *jumps[2] = {NULL, NULL};564jumps[0] =565list_last_entry(&block->instr_list, struct ir3_instruction, node);566if (!list_is_singular(&block->instr_list))567jumps[1] =568list_last_entry(&jumps[0]->node, struct ir3_instruction, node);569570if (jumps[0]->opc == OPC_JUMP)571jumps[1] = NULL;572else if (jumps[0]->opc != OPC_B || !jumps[1] || jumps[1]->opc != OPC_B)573continue;574575for (unsigned i = 0; i < 2; i++) {576if (!jumps[i])577continue;578579struct ir3_block *tblock = jumps[i]->cat0.target;580if (&tblock->node == block->node.next) {581list_delinit(&jumps[i]->node);582progress = true;583break;584}585}586}587588return progress;589}590591static void592resolve_jumps(struct ir3 *ir)593{594foreach_block (block, &ir->block_list)595foreach_instr (instr, &block->instr_list)596if (is_flow(instr) && instr->cat0.target) {597struct ir3_instruction *target = list_first_entry(598&instr->cat0.target->instr_list, struct ir3_instruction, node);599600instr->cat0.immed = (int)target->ip - (int)instr->ip;601}602}603604static void605mark_jp(struct ir3_block *block)606{607struct ir3_instruction *target =608list_first_entry(&block->instr_list, struct ir3_instruction, node);609target->flags |= IR3_INSTR_JP;610}611612/* Mark points where control flow converges or diverges.613*614* Divergence points could actually be re-convergence points where615* "parked" threads are recoverged with threads that took the opposite616* path last time around. Possibly it is easier to think of (jp) as617* "the execution mask might have changed".618*/619static void620mark_xvergence_points(struct ir3 *ir)621{622foreach_block (block, &ir->block_list) {623if (block->predecessors_count > 1) {624/* if a block has more than one possible predecessor, then625* the first instruction is a convergence point.626*/627mark_jp(block);628} else if (block->predecessors_count == 1) {629/* If a block has one predecessor, which has multiple possible630* successors, it is a divergence point.631*/632for (unsigned i = 0; i < block->predecessors_count; i++) {633struct ir3_block *predecessor = block->predecessors[i];634if (predecessor->successors[1]) {635mark_jp(block);636}637}638}639}640}641642/* Insert the branch/jump instructions for flow control between blocks.643* Initially this is done naively, without considering if the successor644* block immediately follows the current block (ie. so no jump required),645* but that is cleaned up in opt_jump().646*647* TODO what ensures that the last write to p0.x in a block is the648* branch condition? Have we been getting lucky all this time?649*/650static void651block_sched(struct ir3 *ir)652{653foreach_block (block, &ir->block_list) {654if (block->successors[1]) {655/* if/else, conditional branches to "then" or "else": */656struct ir3_instruction *br1, *br2;657658if (block->brtype == IR3_BRANCH_GETONE) {659/* getone can't be inverted, and it wouldn't even make sense660* to follow it with an inverted branch, so follow it by an661* unconditional branch.662*/663debug_assert(!block->condition);664br1 = ir3_GETONE(block);665br1->cat0.target = block->successors[1];666667br2 = ir3_JUMP(block);668br2->cat0.target = block->successors[0];669} else {670debug_assert(block->condition);671672/* create "else" branch first (since "then" block should673* frequently/always end up being a fall-thru):674*/675br1 = ir3_instr_create(block, OPC_B, 0, 1);676ir3_src_create(br1, regid(REG_P0, 0), 0)->def =677block->condition->dsts[0];678br1->cat0.inv1 = true;679br1->cat0.target = block->successors[1];680681/* "then" branch: */682br2 = ir3_instr_create(block, OPC_B, 0, 1);683ir3_src_create(br2, regid(REG_P0, 0), 0)->def =684block->condition->dsts[0];685br2->cat0.target = block->successors[0];686687switch (block->brtype) {688case IR3_BRANCH_COND:689br1->cat0.brtype = br2->cat0.brtype = BRANCH_PLAIN;690break;691case IR3_BRANCH_ALL:692br1->cat0.brtype = BRANCH_ANY;693br2->cat0.brtype = BRANCH_ALL;694break;695case IR3_BRANCH_ANY:696br1->cat0.brtype = BRANCH_ALL;697br2->cat0.brtype = BRANCH_ANY;698break;699case IR3_BRANCH_GETONE:700unreachable("can't get here");701}702}703} else if (block->successors[0]) {704/* otherwise unconditional jump to next block: */705struct ir3_instruction *jmp;706707jmp = ir3_JUMP(block);708jmp->cat0.target = block->successors[0];709}710}711}712713/* Here we workaround the fact that kill doesn't actually kill the thread as714* GL expects. The last instruction always needs to be an end instruction,715* which means that if we're stuck in a loop where kill is the only way out,716* then we may have to jump out to the end. kill may also have the d3d717* semantics of converting the thread to a helper thread, rather than setting718* the exec mask to 0, in which case the helper thread could get stuck in an719* infinite loop.720*721* We do this late, both to give the scheduler the opportunity to reschedule722* kill instructions earlier and to avoid having to create a separate basic723* block.724*725* TODO: Assuming that the wavefront doesn't stop as soon as all threads are726* killed, we might benefit by doing this more aggressively when the remaining727* part of the program after the kill is large, since that would let us728* skip over the instructions when there are no non-killed threads left.729*/730static void731kill_sched(struct ir3 *ir, struct ir3_shader_variant *so)732{733/* True if we know that this block will always eventually lead to the end734* block:735*/736bool always_ends = true;737bool added = false;738struct ir3_block *last_block =739list_last_entry(&ir->block_list, struct ir3_block, node);740741foreach_block_rev (block, &ir->block_list) {742for (unsigned i = 0; i < 2 && block->successors[i]; i++) {743if (block->successors[i]->start_ip <= block->end_ip)744always_ends = false;745}746747if (always_ends)748continue;749750foreach_instr_safe (instr, &block->instr_list) {751if (instr->opc != OPC_KILL)752continue;753754struct ir3_instruction *br = ir3_instr_create(block, OPC_B, 0, 1);755ir3_src_create(br, instr->srcs[0]->num, instr->srcs[0]->flags)->wrmask =7561;757br->cat0.target =758list_last_entry(&ir->block_list, struct ir3_block, node);759760list_del(&br->node);761list_add(&br->node, &instr->node);762763added = true;764}765}766767if (added) {768/* I'm not entirely sure how the branchstack works, but we probably769* need to add at least one entry for the divergence which is resolved770* at the end:771*/772so->branchstack++;773774/* We don't update predecessors/successors, so we have to do this775* manually:776*/777mark_jp(last_block);778}779}780781/* Insert nop's required to make this a legal/valid shader program: */782static void783nop_sched(struct ir3 *ir, struct ir3_shader_variant *so)784{785foreach_block (block, &ir->block_list) {786struct ir3_instruction *last = NULL;787struct list_head instr_list;788789/* remove all the instructions from the list, we'll be adding790* them back in as we go791*/792list_replace(&block->instr_list, &instr_list);793list_inithead(&block->instr_list);794795foreach_instr_safe (instr, &instr_list) {796unsigned delay = ir3_delay_calc_exact(block, instr, so->mergedregs);797798/* NOTE: I think the nopN encoding works for a5xx and799* probably a4xx, but not a3xx. So far only tested on800* a6xx.801*/802803if ((delay > 0) && (ir->compiler->gpu_id >= 600) && last &&804((opc_cat(last->opc) == 2) || (opc_cat(last->opc) == 3)) &&805(last->repeat == 0)) {806/* the previous cat2/cat3 instruction can encode at most 3 nop's: */807unsigned transfer = MIN2(delay, 3 - last->nop);808last->nop += transfer;809delay -= transfer;810}811812if ((delay > 0) && last && (last->opc == OPC_NOP)) {813/* the previous nop can encode at most 5 repeats: */814unsigned transfer = MIN2(delay, 5 - last->repeat);815last->repeat += transfer;816delay -= transfer;817}818819if (delay > 0) {820debug_assert(delay <= 6);821ir3_NOP(block)->repeat = delay - 1;822}823824list_addtail(&instr->node, &block->instr_list);825last = instr;826}827}828}829830bool831ir3_legalize(struct ir3 *ir, struct ir3_shader_variant *so, int *max_bary)832{833struct ir3_legalize_ctx *ctx = rzalloc(ir, struct ir3_legalize_ctx);834bool mergedregs = so->mergedregs;835bool progress;836837ctx->so = so;838ctx->max_bary = -1;839ctx->compiler = ir->compiler;840ctx->type = ir->type;841842/* allocate per-block data: */843foreach_block (block, &ir->block_list) {844struct ir3_legalize_block_data *bd =845rzalloc(ctx, struct ir3_legalize_block_data);846847regmask_init(&bd->state.needs_ss_war, mergedregs);848regmask_init(&bd->state.needs_ss, mergedregs);849regmask_init(&bd->state.needs_sy, mergedregs);850851block->data = bd;852}853854ir3_remove_nops(ir);855856/* We may have failed to pull all input loads into the first block.857* In such case at the moment we aren't able to find a better place858* to for (ei) than the end of the program.859* a5xx and a6xx do automatically release varying storage at the end.860*/861ctx->early_input_release = true;862struct ir3_block *start_block = ir3_start_block(ir);863foreach_block (block, &ir->block_list) {864foreach_instr (instr, &block->instr_list) {865if (is_input(instr) && block != start_block) {866ctx->early_input_release = false;867break;868}869}870}871872assert(ctx->early_input_release || ctx->compiler->gpu_id > 500);873874/* process each block: */875do {876progress = false;877foreach_block (block, &ir->block_list) {878progress |= legalize_block(ctx, block);879}880} while (progress);881882*max_bary = ctx->max_bary;883884block_sched(ir);885if (so->type == MESA_SHADER_FRAGMENT)886kill_sched(ir, so);887888foreach_block (block, &ir->block_list) {889progress |= apply_fine_deriv_macro(ctx, block);890}891892nop_sched(ir, so);893894while (opt_jump(ir))895;896897ir3_count_instructions(ir);898resolve_jumps(ir);899900mark_xvergence_points(ir);901902ralloc_free(ctx);903904return true;905}906907908