Path: blob/21.2-virgl/src/freedreno/ir3/ir3_sched.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/dag.h"27#include "util/u_math.h"2829#include "ir3.h"30#include "ir3_compiler.h"3132#ifdef DEBUG33#define SCHED_DEBUG (ir3_shader_debug & IR3_DBG_SCHEDMSGS)34#else35#define SCHED_DEBUG 036#endif37#define d(fmt, ...) \38do { \39if (SCHED_DEBUG) { \40printf("SCHED: " fmt "\n", ##__VA_ARGS__); \41} \42} while (0)4344#define di(instr, fmt, ...) \45do { \46if (SCHED_DEBUG) { \47printf("SCHED: " fmt ": ", ##__VA_ARGS__); \48ir3_print_instr(instr); \49} \50} while (0)5152/*53* Instruction Scheduling:54*55* A block-level pre-RA scheduler, which works by creating a DAG of56* instruction dependencies, and heuristically picking a DAG head57* (instruction with no unscheduled dependencies).58*59* Where possible, it tries to pick instructions that avoid nop delay60* slots, but it will prefer to pick instructions that reduce (or do61* not increase) the number of live values.62*63* If the only possible choices are instructions that increase the64* number of live values, it will try to pick the one with the earliest65* consumer (based on pre-sched program order).66*67* There are a few special cases that need to be handled, since sched68* is currently independent of register allocation. Usages of address69* register (a0.x) or predicate register (p0.x) must be serialized. Ie.70* if you have two pairs of instructions that write the same special71* register and then read it, then those pairs cannot be interleaved.72* To solve this, when we are in such a scheduling "critical section",73* and we encounter a conflicting write to a special register, we try74* to schedule any remaining instructions that use that value first.75*76* TODO we can detect too-large live_values here.. would be a good place77* to "spill" cheap things, like move from uniform/immed. (Constructing78* list of ssa def consumers before sched pass would make this easier.79* Also, in general it is general it might be best not to re-use load_immed80* across blocks.81*82* TODO we can use (abs)/(neg) src modifiers in a lot of cases to reduce83* the # of immediates in play (or at least that would help with84* dEQP-GLES31.functional.ubo.random.all_per_block_buffers.*).. probably85* do this in a nir pass that inserts fneg/etc? The cp pass should fold86* these into src modifiers..87*/8889struct ir3_sched_ctx {90struct ir3_block *block; /* the current block */91struct dag *dag;9293struct list_head unscheduled_list; /* unscheduled instructions */94struct ir3_instruction *scheduled; /* last scheduled instr */95struct ir3_instruction *addr0; /* current a0.x user, if any */96struct ir3_instruction *addr1; /* current a1.x user, if any */97struct ir3_instruction *pred; /* current p0.x user, if any */9899struct ir3_instruction *split; /* most-recently-split a0/a1/p0 producer */100101int remaining_kills;102int remaining_tex;103104bool error;105106int sfu_delay;107int tex_delay;108109/* We order the scheduled tex/SFU instructions, and keep track of the110* index of the last waited on instruction, so we can know which111* instructions are still outstanding (and therefore would require us to112* wait for all outstanding instructions before scheduling a use).113*/114int tex_index, first_outstanding_tex_index;115int sfu_index, first_outstanding_sfu_index;116};117118struct ir3_sched_node {119struct dag_node dag; /* must be first for util_dynarray_foreach */120struct ir3_instruction *instr;121122unsigned delay;123unsigned max_delay;124125unsigned tex_index;126unsigned sfu_index;127128/* For instructions that are a meta:collect src, once we schedule129* the first src of the collect, the entire vecN is live (at least130* from the PoV of the first RA pass.. the 2nd scalar pass can fill131* in some of the gaps, but often not all). So we want to help out132* RA, and realize that as soon as we schedule the first collect133* src, there is no penalty to schedule the remainder (ie. they134* don't make additional values live). In fact we'd prefer to135* schedule the rest ASAP to minimize the live range of the vecN.136*137* For instructions that are the src of a collect, we track the138* corresponding collect, and mark them as partially live as soon139* as any one of the src's is scheduled.140*/141struct ir3_instruction *collect;142bool partially_live;143144/* Is this instruction a direct or indirect dependency for a kill?145* If so, we should prioritize it when possible146*/147bool kill_path;148149/* This node represents a shader output. A semi-common pattern in150* shaders is something along the lines of:151*152* fragcolor.w = 1.0153*154* Which we'd prefer to schedule as late as possible, since it155* produces a live value that is never killed/consumed. So detect156* outputs up-front, and avoid scheduling them unless the reduce157* register pressure (or at least are neutral)158*/159bool output;160};161162#define foreach_sched_node(__n, __list) \163list_for_each_entry (struct ir3_sched_node, __n, __list, dag.link)164165static void sched_node_init(struct ir3_sched_ctx *ctx,166struct ir3_instruction *instr);167static void sched_node_add_dep(struct ir3_instruction *instr,168struct ir3_instruction *src, int i);169170static bool171is_scheduled(struct ir3_instruction *instr)172{173return !!(instr->flags & IR3_INSTR_MARK);174}175176/* check_src_cond() passing a ir3_sched_ctx. */177static bool178sched_check_src_cond(struct ir3_instruction *instr,179bool (*cond)(struct ir3_instruction *,180struct ir3_sched_ctx *),181struct ir3_sched_ctx *ctx)182{183foreach_ssa_src (src, instr) {184/* meta:split/collect aren't real instructions, the thing that185* we actually care about is *their* srcs186*/187if ((src->opc == OPC_META_SPLIT) || (src->opc == OPC_META_COLLECT)) {188if (sched_check_src_cond(src, cond, ctx))189return true;190} else {191if (cond(src, ctx))192return true;193}194}195196return false;197}198199/* Is this a prefetch or tex that hasn't been waited on yet? */200201static bool202is_outstanding_tex_or_prefetch(struct ir3_instruction *instr,203struct ir3_sched_ctx *ctx)204{205if (!is_tex_or_prefetch(instr))206return false;207208/* The sched node is only valid within the same block, we cannot209* really say anything about src's from other blocks210*/211if (instr->block != ctx->block)212return true;213214struct ir3_sched_node *n = instr->data;215return n->tex_index >= ctx->first_outstanding_tex_index;216}217218static bool219is_outstanding_sfu(struct ir3_instruction *instr, struct ir3_sched_ctx *ctx)220{221if (!is_sfu(instr))222return false;223224/* The sched node is only valid within the same block, we cannot225* really say anything about src's from other blocks226*/227if (instr->block != ctx->block)228return true;229230struct ir3_sched_node *n = instr->data;231return n->sfu_index >= ctx->first_outstanding_sfu_index;232}233234static unsigned235cycle_count(struct ir3_instruction *instr)236{237if (instr->opc == OPC_META_COLLECT) {238/* Assume that only immed/const sources produce moves */239unsigned n = 0;240foreach_src (src, instr) {241if (src->flags & (IR3_REG_IMMED | IR3_REG_CONST))242n++;243}244return n;245} else if (is_meta(instr)) {246return 0;247} else {248return 1;249}250}251252static void253schedule(struct ir3_sched_ctx *ctx, struct ir3_instruction *instr)254{255debug_assert(ctx->block == instr->block);256257/* remove from depth list:258*/259list_delinit(&instr->node);260261if (writes_addr0(instr)) {262debug_assert(ctx->addr0 == NULL);263ctx->addr0 = instr;264}265266if (writes_addr1(instr)) {267debug_assert(ctx->addr1 == NULL);268ctx->addr1 = instr;269}270271if (writes_pred(instr)) {272debug_assert(ctx->pred == NULL);273ctx->pred = instr;274}275276instr->flags |= IR3_INSTR_MARK;277278di(instr, "schedule");279280list_addtail(&instr->node, &instr->block->instr_list);281ctx->scheduled = instr;282283if (is_kill_or_demote(instr)) {284assert(ctx->remaining_kills > 0);285ctx->remaining_kills--;286}287288struct ir3_sched_node *n = instr->data;289290/* If this instruction is a meta:collect src, mark the remaining291* collect srcs as partially live.292*/293if (n->collect) {294foreach_ssa_src (src, n->collect) {295if (src->block != instr->block)296continue;297struct ir3_sched_node *sn = src->data;298sn->partially_live = true;299}300}301302dag_prune_head(ctx->dag, &n->dag);303304unsigned cycles = cycle_count(instr);305306if (is_sfu(instr)) {307ctx->sfu_delay = 8;308n->sfu_index = ctx->sfu_index++;309} else if (!is_meta(instr) &&310sched_check_src_cond(instr, is_outstanding_sfu, ctx)) {311ctx->sfu_delay = 0;312ctx->first_outstanding_sfu_index = ctx->sfu_index;313} else if (ctx->sfu_delay > 0) {314ctx->sfu_delay -= MIN2(cycles, ctx->sfu_delay);315}316317if (is_tex_or_prefetch(instr)) {318/* NOTE that this isn't an attempt to hide texture fetch latency,319* but an attempt to hide the cost of switching to another warp.320* If we can, we'd like to try to schedule another texture fetch321* before scheduling something that would sync.322*/323ctx->tex_delay = 10;324assert(ctx->remaining_tex > 0);325ctx->remaining_tex--;326n->tex_index = ctx->tex_index++;327} else if (!is_meta(instr) &&328sched_check_src_cond(instr, is_outstanding_tex_or_prefetch,329ctx)) {330ctx->tex_delay = 0;331ctx->first_outstanding_tex_index = ctx->tex_index;332} else if (ctx->tex_delay > 0) {333ctx->tex_delay -= MIN2(cycles, ctx->tex_delay);334}335}336337struct ir3_sched_notes {338/* there is at least one kill which could be scheduled, except339* for unscheduled bary.f's:340*/341bool blocked_kill;342/* there is at least one instruction that could be scheduled,343* except for conflicting address/predicate register usage:344*/345bool addr0_conflict, addr1_conflict, pred_conflict;346};347348/* could an instruction be scheduled if specified ssa src was scheduled? */349static bool350could_sched(struct ir3_instruction *instr, struct ir3_instruction *src)351{352foreach_ssa_src (other_src, instr) {353/* if dependency not scheduled, we aren't ready yet: */354if ((src != other_src) && !is_scheduled(other_src)) {355return false;356}357}358return true;359}360361/* Check if instruction is ok to schedule. Make sure it is not blocked362* by use of addr/predicate register, etc.363*/364static bool365check_instr(struct ir3_sched_ctx *ctx, struct ir3_sched_notes *notes,366struct ir3_instruction *instr)367{368debug_assert(!is_scheduled(instr));369370if (instr == ctx->split) {371/* Don't schedule instructions created by splitting a a0.x/a1.x/p0.x372* write until another "normal" instruction has been scheduled.373*/374return false;375}376377if (ctx->remaining_kills && (is_tex(instr) || is_mem(instr))) {378/* avoid texture/memory access if we have unscheduled kills379* that could make the expensive operation unnecessary. By380* definition, if there are remaining kills, and this instr381* is not a dependency of a kill, there are other instructions382* that we can choose from.383*/384struct ir3_sched_node *n = instr->data;385if (!n->kill_path)386return false;387}388389/* For instructions that write address register we need to390* make sure there is at least one instruction that uses the391* addr value which is otherwise ready.392*393* NOTE if any instructions use pred register and have other394* src args, we would need to do the same for writes_pred()..395*/396if (writes_addr0(instr)) {397struct ir3 *ir = instr->block->shader;398bool ready = false;399for (unsigned i = 0; (i < ir->a0_users_count) && !ready; i++) {400struct ir3_instruction *indirect = ir->a0_users[i];401if (!indirect)402continue;403if (indirect->address->def != instr->dsts[0])404continue;405ready = could_sched(indirect, instr);406}407408/* nothing could be scheduled, so keep looking: */409if (!ready)410return false;411}412413if (writes_addr1(instr)) {414struct ir3 *ir = instr->block->shader;415bool ready = false;416for (unsigned i = 0; (i < ir->a1_users_count) && !ready; i++) {417struct ir3_instruction *indirect = ir->a1_users[i];418if (!indirect)419continue;420if (indirect->address->def != instr->dsts[0])421continue;422ready = could_sched(indirect, instr);423}424425/* nothing could be scheduled, so keep looking: */426if (!ready)427return false;428}429430/* if this is a write to address/predicate register, and that431* register is currently in use, we need to defer until it is432* free:433*/434if (writes_addr0(instr) && ctx->addr0) {435debug_assert(ctx->addr0 != instr);436notes->addr0_conflict = true;437return false;438}439440if (writes_addr1(instr) && ctx->addr1) {441debug_assert(ctx->addr1 != instr);442notes->addr1_conflict = true;443return false;444}445446if (writes_pred(instr) && ctx->pred) {447debug_assert(ctx->pred != instr);448notes->pred_conflict = true;449return false;450}451452/* if the instruction is a kill, we need to ensure *every*453* bary.f is scheduled. The hw seems unhappy if the thread454* gets killed before the end-input (ei) flag is hit.455*456* We could do this by adding each bary.f instruction as457* virtual ssa src for the kill instruction. But we have458* fixed length instr->srcs[].459*460* TODO we could handle this by false-deps now, probably.461*/462if (is_kill_or_demote(instr)) {463struct ir3 *ir = instr->block->shader;464465for (unsigned i = 0; i < ir->baryfs_count; i++) {466struct ir3_instruction *baryf = ir->baryfs[i];467if (baryf->flags & IR3_INSTR_UNUSED)468continue;469if (!is_scheduled(baryf)) {470notes->blocked_kill = true;471return false;472}473}474}475476return true;477}478479/* Find the instr->ip of the closest use of an instruction, in480* pre-sched order. This isn't going to be the same as post-sched481* order, but it is a reasonable approximation to limit scheduling482* instructions *too* early. This is mostly to prevent bad behavior483* in cases where we have a large number of possible instructions484* to choose, to avoid creating too much parallelism (ie. blowing485* up register pressure)486*487* See488* dEQP-GLES31.functional.atomic_counter.layout.reverse_offset.inc_dec.8_counters_5_calls_1_thread489*/490static int491nearest_use(struct ir3_instruction *instr)492{493unsigned nearest = ~0;494foreach_ssa_use (use, instr)495if (!is_scheduled(use))496nearest = MIN2(nearest, use->ip);497498/* slight hack.. this heuristic tends to push bary.f's to later499* in the shader, closer to their uses. But we actually would500* prefer to get these scheduled earlier, to unlock varying501* storage for more VS jobs:502*/503if (is_input(instr))504nearest /= 2;505506return nearest;507}508509static bool510is_only_nonscheduled_use(struct ir3_instruction *instr,511struct ir3_instruction *use)512{513foreach_ssa_use (other_use, instr) {514if (other_use != use && !is_scheduled(other_use))515return false;516}517518return true;519}520521/* find net change to live values if instruction were scheduled: */522static int523live_effect(struct ir3_instruction *instr)524{525struct ir3_sched_node *n = instr->data;526int new_live =527(n->partially_live || !instr->uses || instr->uses->entries == 0)528? 0529: dest_regs(instr);530int freed_live = 0;531532/* if we schedule something that causes a vecN to be live,533* then count all it's other components too:534*/535if (n->collect)536new_live *= n->collect->srcs_count;537538foreach_ssa_src_n (src, n, instr) {539if (__is_false_dep(instr, n))540continue;541542if (instr->block != src->block)543continue;544545if (is_only_nonscheduled_use(src, instr))546freed_live += dest_regs(src);547}548549return new_live - freed_live;550}551552/* Determine if this is an instruction that we'd prefer not to schedule553* yet, in order to avoid an (ss)/(sy) sync. This is limited by the554* sfu_delay/tex_delay counters, ie. the more cycles it has been since555* the last SFU/tex, the less costly a sync would be, and the number of556* outstanding SFU/tex instructions to prevent a blowup in register pressure.557*/558static bool559should_defer(struct ir3_sched_ctx *ctx, struct ir3_instruction *instr)560{561if (ctx->sfu_delay) {562if (sched_check_src_cond(instr, is_outstanding_sfu, ctx))563return true;564}565566/* We mostly just want to try to schedule another texture fetch567* before scheduling something that would (sy) sync, so we can568* limit this rule to cases where there are remaining texture569* fetches570*/571if (ctx->tex_delay && ctx->remaining_tex) {572if (sched_check_src_cond(instr, is_outstanding_tex_or_prefetch, ctx))573return true;574}575576/* Avoid scheduling too many outstanding texture or sfu instructions at577* once by deferring further tex/SFU instructions. This both prevents578* stalls when the queue of texture/sfu instructions becomes too large,579* and prevents unacceptably large increases in register pressure from too580* many outstanding texture instructions.581*/582if (ctx->tex_index - ctx->first_outstanding_tex_index >= 8 && is_tex(instr))583return true;584585if (ctx->sfu_index - ctx->first_outstanding_sfu_index >= 8 && is_sfu(instr))586return true;587588return false;589}590591static struct ir3_sched_node *choose_instr_inc(struct ir3_sched_ctx *ctx,592struct ir3_sched_notes *notes,593bool defer, bool avoid_output);594595/**596* Chooses an instruction to schedule using the Goodman/Hsu (1988) CSR (Code597* Scheduling for Register pressure) heuristic.598*599* Only handles the case of choosing instructions that reduce register pressure600* or are even.601*/602static struct ir3_sched_node *603choose_instr_dec(struct ir3_sched_ctx *ctx, struct ir3_sched_notes *notes,604bool defer)605{606const char *mode = defer ? "-d" : "";607struct ir3_sched_node *chosen = NULL;608609/* Find a ready inst with regs freed and pick the one with max cost. */610foreach_sched_node (n, &ctx->dag->heads) {611if (defer && should_defer(ctx, n->instr))612continue;613614/* Note: mergedregs is only used post-RA, just set it to false */615unsigned d = ir3_delay_calc_prera(ctx->block, n->instr);616617if (d > 0)618continue;619620if (live_effect(n->instr) > -1)621continue;622623if (!check_instr(ctx, notes, n->instr))624continue;625626if (!chosen || chosen->max_delay < n->max_delay) {627chosen = n;628}629}630631if (chosen) {632di(chosen->instr, "dec%s: chose (freed+ready)", mode);633return chosen;634}635636/* Find a leader with regs freed and pick the one with max cost. */637foreach_sched_node (n, &ctx->dag->heads) {638if (defer && should_defer(ctx, n->instr))639continue;640641if (live_effect(n->instr) > -1)642continue;643644if (!check_instr(ctx, notes, n->instr))645continue;646647if (!chosen || chosen->max_delay < n->max_delay) {648chosen = n;649}650}651652if (chosen) {653di(chosen->instr, "dec%s: chose (freed)", mode);654return chosen;655}656657/* Contra the paper, pick a leader with no effect on used regs. This may658* open up new opportunities, as otherwise a single-operand instr consuming659* a value will tend to block finding freeing that value. This had a660* massive effect on reducing spilling on V3D.661*662* XXX: Should this prioritize ready?663*/664foreach_sched_node (n, &ctx->dag->heads) {665if (defer && should_defer(ctx, n->instr))666continue;667668unsigned d = ir3_delay_calc_prera(ctx->block, n->instr);669670if (d > 0)671continue;672673if (live_effect(n->instr) > 0)674continue;675676if (!check_instr(ctx, notes, n->instr))677continue;678679if (!chosen || chosen->max_delay < n->max_delay)680chosen = n;681}682683if (chosen) {684di(chosen->instr, "dec%s: chose (neutral+ready)", mode);685return chosen;686}687688foreach_sched_node (n, &ctx->dag->heads) {689if (defer && should_defer(ctx, n->instr))690continue;691692if (live_effect(n->instr) > 0)693continue;694695if (!check_instr(ctx, notes, n->instr))696continue;697698if (!chosen || chosen->max_delay < n->max_delay)699chosen = n;700}701702if (chosen) {703di(chosen->instr, "dec%s: chose (neutral)", mode);704return chosen;705}706707return choose_instr_inc(ctx, notes, defer, true);708}709710/**711* When we can't choose an instruction that reduces register pressure or712* is neutral, we end up here to try and pick the least bad option.713*/714static struct ir3_sched_node *715choose_instr_inc(struct ir3_sched_ctx *ctx, struct ir3_sched_notes *notes,716bool defer, bool avoid_output)717{718const char *mode = defer ? "-d" : "";719struct ir3_sched_node *chosen = NULL;720721/*722* From hear on out, we are picking something that increases723* register pressure. So try to pick something which will724* be consumed soon:725*/726unsigned chosen_distance = 0;727728/* Pick the max delay of the remaining ready set. */729foreach_sched_node (n, &ctx->dag->heads) {730if (avoid_output && n->output)731continue;732733if (defer && should_defer(ctx, n->instr))734continue;735736unsigned d = ir3_delay_calc_prera(ctx->block, n->instr);737738if (d > 0)739continue;740741if (!check_instr(ctx, notes, n->instr))742continue;743744unsigned distance = nearest_use(n->instr);745746if (!chosen || distance < chosen_distance) {747chosen = n;748chosen_distance = distance;749}750}751752if (chosen) {753di(chosen->instr, "inc%s: chose (distance+ready)", mode);754return chosen;755}756757/* Pick the max delay of the remaining leaders. */758foreach_sched_node (n, &ctx->dag->heads) {759if (avoid_output && n->output)760continue;761762if (defer && should_defer(ctx, n->instr))763continue;764765if (!check_instr(ctx, notes, n->instr))766continue;767768unsigned distance = nearest_use(n->instr);769770if (!chosen || distance < chosen_distance) {771chosen = n;772chosen_distance = distance;773}774}775776if (chosen) {777di(chosen->instr, "inc%s: chose (distance)", mode);778return chosen;779}780781return NULL;782}783784/* Handles instruction selections for instructions we want to prioritize785* even if csp/csr would not pick them.786*/787static struct ir3_sched_node *788choose_instr_prio(struct ir3_sched_ctx *ctx, struct ir3_sched_notes *notes)789{790struct ir3_sched_node *chosen = NULL;791792foreach_sched_node (n, &ctx->dag->heads) {793/*794* - phi nodes and inputs must be scheduled first795* - split should be scheduled first, so that the vector value is796* killed as soon as possible. RA cannot split up the vector and797* reuse components that have been killed until it's been killed.798* - collect, on the other hand, should be treated as a "normal"799* instruction, and may add to register pressure if its sources are800* part of another vector or immediates.801*/802if (!is_meta(n->instr) || n->instr->opc == OPC_META_COLLECT)803continue;804805if (!chosen || (chosen->max_delay < n->max_delay))806chosen = n;807}808809if (chosen) {810di(chosen->instr, "prio: chose (meta)");811return chosen;812}813814return NULL;815}816817static void818dump_state(struct ir3_sched_ctx *ctx)819{820if (!SCHED_DEBUG)821return;822823foreach_sched_node (n, &ctx->dag->heads) {824di(n->instr, "maxdel=%3d le=%d del=%u ", n->max_delay,825live_effect(n->instr), ir3_delay_calc_prera(ctx->block, n->instr));826827util_dynarray_foreach (&n->dag.edges, struct dag_edge, edge) {828struct ir3_sched_node *child = (struct ir3_sched_node *)edge->child;829830di(child->instr, " -> (%d parents) ", child->dag.parent_count);831}832}833}834835/* find instruction to schedule: */836static struct ir3_instruction *837choose_instr(struct ir3_sched_ctx *ctx, struct ir3_sched_notes *notes)838{839struct ir3_sched_node *chosen;840841dump_state(ctx);842843chosen = choose_instr_prio(ctx, notes);844if (chosen)845return chosen->instr;846847chosen = choose_instr_dec(ctx, notes, true);848if (chosen)849return chosen->instr;850851chosen = choose_instr_dec(ctx, notes, false);852if (chosen)853return chosen->instr;854855chosen = choose_instr_inc(ctx, notes, false, false);856if (chosen)857return chosen->instr;858859return NULL;860}861862static struct ir3_instruction *863split_instr(struct ir3_sched_ctx *ctx, struct ir3_instruction *orig_instr)864{865struct ir3_instruction *new_instr = ir3_instr_clone(orig_instr);866di(new_instr, "split instruction");867sched_node_init(ctx, new_instr);868return new_instr;869}870871/* "spill" the address registers by remapping any unscheduled872* instructions which depend on the current address register873* to a clone of the instruction which wrote the address reg.874*/875static struct ir3_instruction *876split_addr(struct ir3_sched_ctx *ctx, struct ir3_instruction **addr,877struct ir3_instruction **users, unsigned users_count)878{879struct ir3_instruction *new_addr = NULL;880unsigned i;881882debug_assert(*addr);883884for (i = 0; i < users_count; i++) {885struct ir3_instruction *indirect = users[i];886887if (!indirect)888continue;889890/* skip instructions already scheduled: */891if (is_scheduled(indirect))892continue;893894/* remap remaining instructions using current addr895* to new addr:896*/897if (indirect->address->def == (*addr)->dsts[0]) {898if (!new_addr) {899new_addr = split_instr(ctx, *addr);900/* original addr is scheduled, but new one isn't: */901new_addr->flags &= ~IR3_INSTR_MARK;902}903indirect->address->def = new_addr->dsts[0];904/* don't need to remove old dag edge since old addr is905* already scheduled:906*/907sched_node_add_dep(indirect, new_addr, 0);908di(indirect, "new address");909}910}911912/* all remaining indirects remapped to new addr: */913*addr = NULL;914915return new_addr;916}917918/* "spill" the predicate register by remapping any unscheduled919* instructions which depend on the current predicate register920* to a clone of the instruction which wrote the address reg.921*/922static struct ir3_instruction *923split_pred(struct ir3_sched_ctx *ctx)924{925struct ir3 *ir;926struct ir3_instruction *new_pred = NULL;927unsigned i;928929debug_assert(ctx->pred);930931ir = ctx->pred->block->shader;932933for (i = 0; i < ir->predicates_count; i++) {934struct ir3_instruction *predicated = ir->predicates[i];935936if (!predicated)937continue;938939/* skip instructions already scheduled: */940if (is_scheduled(predicated))941continue;942943/* remap remaining instructions using current pred944* to new pred:945*946* TODO is there ever a case when pred isn't first947* (and only) src?948*/949if (ssa(predicated->srcs[0]) == ctx->pred) {950if (!new_pred) {951new_pred = split_instr(ctx, ctx->pred);952/* original pred is scheduled, but new one isn't: */953new_pred->flags &= ~IR3_INSTR_MARK;954}955predicated->srcs[0]->instr = new_pred;956/* don't need to remove old dag edge since old pred is957* already scheduled:958*/959sched_node_add_dep(predicated, new_pred, 0);960di(predicated, "new predicate");961}962}963964if (ctx->block->condition == ctx->pred) {965if (!new_pred) {966new_pred = split_instr(ctx, ctx->pred);967/* original pred is scheduled, but new one isn't: */968new_pred->flags &= ~IR3_INSTR_MARK;969}970ctx->block->condition = new_pred;971d("new branch condition");972}973974/* all remaining predicated remapped to new pred: */975ctx->pred = NULL;976977return new_pred;978}979980static void981sched_node_init(struct ir3_sched_ctx *ctx, struct ir3_instruction *instr)982{983struct ir3_sched_node *n = rzalloc(ctx->dag, struct ir3_sched_node);984985dag_init_node(ctx->dag, &n->dag);986987n->instr = instr;988instr->data = n;989}990991static void992sched_node_add_dep(struct ir3_instruction *instr, struct ir3_instruction *src,993int i)994{995/* don't consider dependencies in other blocks: */996if (src->block != instr->block)997return;998999/* we could have false-dep's that end up unused: */1000if (src->flags & IR3_INSTR_UNUSED) {1001debug_assert(__is_false_dep(instr, i));1002return;1003}10041005struct ir3_sched_node *n = instr->data;1006struct ir3_sched_node *sn = src->data;10071008/* If src is consumed by a collect, track that to realize that once1009* any of the collect srcs are live, we should hurry up and schedule1010* the rest.1011*/1012if (instr->opc == OPC_META_COLLECT)1013sn->collect = instr;10141015dag_add_edge(&sn->dag, &n->dag, NULL);10161017unsigned d = ir3_delayslots(src, instr, i, true);10181019n->delay = MAX2(n->delay, d);1020}10211022static void1023mark_kill_path(struct ir3_instruction *instr)1024{1025struct ir3_sched_node *n = instr->data;10261027if (n->kill_path) {1028return;1029}10301031n->kill_path = true;10321033foreach_ssa_src (src, instr) {1034if (src->block != instr->block)1035continue;1036mark_kill_path(src);1037}1038}10391040/* Is it an output? */1041static bool1042is_output_collect(struct ir3_instruction *instr)1043{1044if (instr->opc != OPC_META_COLLECT)1045return false;10461047foreach_ssa_use (use, instr) {1048if (use->opc != OPC_END && use->opc != OPC_CHMASK)1049return false;1050}10511052return true;1053}10541055/* Is it's only use as output? */1056static bool1057is_output_only(struct ir3_instruction *instr)1058{1059if (!writes_gpr(instr))1060return false;10611062if (!(instr->dsts[0]->flags & IR3_REG_SSA))1063return false;10641065foreach_ssa_use (use, instr)1066if (!is_output_collect(use))1067return false;10681069return true;1070}10711072static void1073sched_node_add_deps(struct ir3_instruction *instr)1074{1075/* There's nothing to do for phi nodes, since they always go first. And1076* phi nodes can reference sources later in the same block, so handling1077* sources is not only unnecessary but could cause problems.1078*/1079if (instr->opc == OPC_META_PHI)1080return;10811082/* Since foreach_ssa_src() already handles false-dep's we can construct1083* the DAG easily in a single pass.1084*/1085foreach_ssa_src_n (src, i, instr) {1086sched_node_add_dep(instr, src, i);1087}10881089/* NOTE that all inputs must be scheduled before a kill, so1090* mark these to be prioritized as well:1091*/1092if (is_kill_or_demote(instr) || is_input(instr)) {1093mark_kill_path(instr);1094}10951096if (is_output_only(instr)) {1097struct ir3_sched_node *n = instr->data;1098n->output = true;1099}1100}11011102static void1103sched_dag_max_delay_cb(struct dag_node *node, void *state)1104{1105struct ir3_sched_node *n = (struct ir3_sched_node *)node;1106uint32_t max_delay = 0;11071108util_dynarray_foreach (&n->dag.edges, struct dag_edge, edge) {1109struct ir3_sched_node *child = (struct ir3_sched_node *)edge->child;1110max_delay = MAX2(child->max_delay, max_delay);1111}11121113n->max_delay = MAX2(n->max_delay, max_delay + n->delay);1114}11151116static void1117sched_dag_init(struct ir3_sched_ctx *ctx)1118{1119ctx->dag = dag_create(ctx);11201121foreach_instr (instr, &ctx->unscheduled_list) {1122sched_node_init(ctx, instr);1123sched_node_add_deps(instr);1124}11251126dag_traverse_bottom_up(ctx->dag, sched_dag_max_delay_cb, NULL);1127}11281129static void1130sched_dag_destroy(struct ir3_sched_ctx *ctx)1131{1132ralloc_free(ctx->dag);1133ctx->dag = NULL;1134}11351136static void1137sched_block(struct ir3_sched_ctx *ctx, struct ir3_block *block)1138{1139ctx->block = block;11401141/* addr/pred writes are per-block: */1142ctx->addr0 = NULL;1143ctx->addr1 = NULL;1144ctx->pred = NULL;1145ctx->tex_delay = 0;1146ctx->sfu_delay = 0;1147ctx->tex_index = ctx->first_outstanding_tex_index = 0;1148ctx->sfu_index = ctx->first_outstanding_sfu_index = 0;11491150/* move all instructions to the unscheduled list, and1151* empty the block's instruction list (to which we will1152* be inserting).1153*/1154list_replace(&block->instr_list, &ctx->unscheduled_list);1155list_inithead(&block->instr_list);11561157sched_dag_init(ctx);11581159ctx->remaining_kills = 0;1160ctx->remaining_tex = 0;1161foreach_instr_safe (instr, &ctx->unscheduled_list) {1162if (is_kill_or_demote(instr))1163ctx->remaining_kills++;1164if (is_tex_or_prefetch(instr))1165ctx->remaining_tex++;1166}11671168/* First schedule all meta:input and meta:phi instructions, followed by1169* tex-prefetch. We want all of the instructions that load values into1170* registers before the shader starts to go before any other instructions.1171* But in particular we want inputs to come before prefetches. This is1172* because a FS's bary_ij input may not actually be live in the shader,1173* but it should not be scheduled on top of any other input (but can be1174* overwritten by a tex prefetch)1175*1176* Note: Because the first block cannot have predecessors, meta:input and1177* meta:phi cannot exist in the same block.1178*/1179foreach_instr_safe (instr, &ctx->unscheduled_list)1180if (instr->opc == OPC_META_INPUT || instr->opc == OPC_META_PHI)1181schedule(ctx, instr);11821183foreach_instr_safe (instr, &ctx->unscheduled_list)1184if (instr->opc == OPC_META_TEX_PREFETCH)1185schedule(ctx, instr);11861187while (!list_is_empty(&ctx->unscheduled_list)) {1188struct ir3_sched_notes notes = {0};1189struct ir3_instruction *instr;11901191instr = choose_instr(ctx, ¬es);1192if (instr) {1193unsigned delay = ir3_delay_calc_prera(ctx->block, instr);1194d("delay=%u", delay);11951196/* and if we run out of instructions that can be scheduled,1197* then it is time for nop's:1198*/1199debug_assert(delay <= 6);1200while (delay > 0) {1201ir3_NOP(block);1202delay--;1203}12041205schedule(ctx, instr);12061207/* Since we've scheduled a "real" instruction, we can now1208* schedule any split instruction created by the scheduler again.1209*/1210ctx->split = NULL;1211} else {1212struct ir3_instruction *new_instr = NULL;1213struct ir3 *ir = block->shader;12141215/* nothing available to schedule.. if we are blocked on1216* address/predicate register conflict, then break the1217* deadlock by cloning the instruction that wrote that1218* reg:1219*/1220if (notes.addr0_conflict) {1221new_instr =1222split_addr(ctx, &ctx->addr0, ir->a0_users, ir->a0_users_count);1223} else if (notes.addr1_conflict) {1224new_instr =1225split_addr(ctx, &ctx->addr1, ir->a1_users, ir->a1_users_count);1226} else if (notes.pred_conflict) {1227new_instr = split_pred(ctx);1228} else {1229d("unscheduled_list:");1230foreach_instr (instr, &ctx->unscheduled_list)1231di(instr, "unscheduled: ");1232debug_assert(0);1233ctx->error = true;1234return;1235}12361237if (new_instr) {1238list_delinit(&new_instr->node);1239list_addtail(&new_instr->node, &ctx->unscheduled_list);1240}12411242/* If we produced a new instruction, do not schedule it next to1243* guarantee progress.1244*/1245ctx->split = new_instr;1246}1247}12481249sched_dag_destroy(ctx);1250}12511252int1253ir3_sched(struct ir3 *ir)1254{1255struct ir3_sched_ctx *ctx = rzalloc(NULL, struct ir3_sched_ctx);12561257foreach_block (block, &ir->block_list) {1258foreach_instr (instr, &block->instr_list) {1259instr->data = NULL;1260}1261}12621263ir3_count_instructions(ir);1264ir3_clear_mark(ir);1265ir3_find_ssa_uses(ir, ctx, false);12661267foreach_block (block, &ir->block_list) {1268sched_block(ctx, block);1269}12701271int ret = ctx->error ? -1 : 0;12721273ralloc_free(ctx);12741275return ret;1276}12771278static unsigned1279get_array_id(struct ir3_instruction *instr)1280{1281/* The expectation is that there is only a single array1282* src or dst, ir3_cp should enforce this.1283*/12841285foreach_dst (dst, instr)1286if (dst->flags & IR3_REG_ARRAY)1287return dst->array.id;1288foreach_src (src, instr)1289if (src->flags & IR3_REG_ARRAY)1290return src->array.id;12911292unreachable("this was unexpected");1293}12941295/* does instruction 'prior' need to be scheduled before 'instr'? */1296static bool1297depends_on(struct ir3_instruction *instr, struct ir3_instruction *prior)1298{1299/* TODO for dependencies that are related to a specific object, ie1300* a specific SSBO/image/array, we could relax this constraint to1301* make accesses to unrelated objects not depend on each other (at1302* least as long as not declared coherent)1303*/1304if (((instr->barrier_class & IR3_BARRIER_EVERYTHING) &&1305prior->barrier_class) ||1306((prior->barrier_class & IR3_BARRIER_EVERYTHING) &&1307instr->barrier_class))1308return true;13091310if (instr->barrier_class & prior->barrier_conflict) {1311if (!(instr->barrier_class &1312~(IR3_BARRIER_ARRAY_R | IR3_BARRIER_ARRAY_W))) {1313/* if only array barrier, then we can further limit false-deps1314* by considering the array-id, ie reads/writes to different1315* arrays do not depend on each other (no aliasing)1316*/1317if (get_array_id(instr) != get_array_id(prior)) {1318return false;1319}1320}13211322return true;1323}13241325return false;1326}13271328static void1329add_barrier_deps(struct ir3_block *block, struct ir3_instruction *instr)1330{1331struct list_head *prev = instr->node.prev;1332struct list_head *next = instr->node.next;13331334/* add dependencies on previous instructions that must be scheduled1335* prior to the current instruction1336*/1337while (prev != &block->instr_list) {1338struct ir3_instruction *pi =1339LIST_ENTRY(struct ir3_instruction, prev, node);13401341prev = prev->prev;13421343if (is_meta(pi))1344continue;13451346if (instr->barrier_class == pi->barrier_class) {1347ir3_instr_add_dep(instr, pi);1348break;1349}13501351if (depends_on(instr, pi))1352ir3_instr_add_dep(instr, pi);1353}13541355/* add dependencies on this instruction to following instructions1356* that must be scheduled after the current instruction:1357*/1358while (next != &block->instr_list) {1359struct ir3_instruction *ni =1360LIST_ENTRY(struct ir3_instruction, next, node);13611362next = next->next;13631364if (is_meta(ni))1365continue;13661367if (instr->barrier_class == ni->barrier_class) {1368ir3_instr_add_dep(ni, instr);1369break;1370}13711372if (depends_on(ni, instr))1373ir3_instr_add_dep(ni, instr);1374}1375}13761377/* before scheduling a block, we need to add any necessary false-dependencies1378* to ensure that:1379*1380* (1) barriers are scheduled in the right order wrt instructions related1381* to the barrier1382*1383* (2) reads that come before a write actually get scheduled before the1384* write1385*/1386bool1387ir3_sched_add_deps(struct ir3 *ir)1388{1389bool progress = false;13901391foreach_block (block, &ir->block_list) {1392foreach_instr (instr, &block->instr_list) {1393if (instr->barrier_class) {1394add_barrier_deps(block, instr);1395progress = true;1396}1397}1398}13991400return progress;1401}140214031404