Path: blob/21.2-virgl/src/freedreno/ir3/ir3_cp.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 <math.h>27#include "util/half_float.h"28#include "util/u_math.h"2930#include "ir3.h"31#include "ir3_compiler.h"32#include "ir3_shader.h"3334#define swap(a, b) \35do { \36__typeof(a) __tmp = (a); \37(a) = (b); \38(b) = __tmp; \39} while (0)4041/*42* Copy Propagate:43*/4445struct ir3_cp_ctx {46struct ir3 *shader;47struct ir3_shader_variant *so;48bool progress;49};5051/* is it a type preserving mov, with ok flags?52*53* @instr: the mov to consider removing54* @dst_instr: the instruction consuming the mov (instr)55*56* TODO maybe drop allow_flags since this is only false when dst is57* NULL (ie. outputs)58*/59static bool60is_eligible_mov(struct ir3_instruction *instr,61struct ir3_instruction *dst_instr, bool allow_flags)62{63if (is_same_type_mov(instr)) {64struct ir3_register *dst = instr->dsts[0];65struct ir3_register *src = instr->srcs[0];66struct ir3_instruction *src_instr = ssa(src);6768/* only if mov src is SSA (not const/immed): */69if (!src_instr)70return false;7172/* no indirect: */73if (dst->flags & IR3_REG_RELATIV)74return false;75if (src->flags & IR3_REG_RELATIV)76return false;7778if (src->flags & IR3_REG_ARRAY)79return false;8081if (!allow_flags)82if (src->flags & (IR3_REG_FABS | IR3_REG_FNEG | IR3_REG_SABS |83IR3_REG_SNEG | IR3_REG_BNOT))84return false;8586return true;87}88return false;89}9091/* we can end up with extra cmps.s from frontend, which uses a92*93* cmps.s p0.x, cond, 094*95* as a way to mov into the predicate register. But frequently 'cond'96* is itself a cmps.s/cmps.f/cmps.u. So detect this special case.97*/98static bool99is_foldable_double_cmp(struct ir3_instruction *cmp)100{101struct ir3_instruction *cond = ssa(cmp->srcs[0]);102return (cmp->dsts[0]->num == regid(REG_P0, 0)) && cond &&103(cmp->srcs[1]->flags & IR3_REG_IMMED) &&104(cmp->srcs[1]->iim_val == 0) &&105(cmp->cat2.condition == IR3_COND_NE) &&106(!cond->address || cond->address->def->instr->block == cmp->block);107}108109/* propagate register flags from src to dst.. negates need special110* handling to cancel each other out.111*/112static void113combine_flags(unsigned *dstflags, struct ir3_instruction *src)114{115unsigned srcflags = src->srcs[0]->flags;116117/* if what we are combining into already has (abs) flags,118* we can drop (neg) from src:119*/120if (*dstflags & IR3_REG_FABS)121srcflags &= ~IR3_REG_FNEG;122if (*dstflags & IR3_REG_SABS)123srcflags &= ~IR3_REG_SNEG;124125if (srcflags & IR3_REG_FABS)126*dstflags |= IR3_REG_FABS;127if (srcflags & IR3_REG_SABS)128*dstflags |= IR3_REG_SABS;129if (srcflags & IR3_REG_FNEG)130*dstflags ^= IR3_REG_FNEG;131if (srcflags & IR3_REG_SNEG)132*dstflags ^= IR3_REG_SNEG;133if (srcflags & IR3_REG_BNOT)134*dstflags ^= IR3_REG_BNOT;135136*dstflags &= ~IR3_REG_SSA;137*dstflags |= srcflags & IR3_REG_SSA;138*dstflags |= srcflags & IR3_REG_CONST;139*dstflags |= srcflags & IR3_REG_IMMED;140*dstflags |= srcflags & IR3_REG_RELATIV;141*dstflags |= srcflags & IR3_REG_ARRAY;142*dstflags |= srcflags & IR3_REG_SHARED;143144/* if src of the src is boolean we can drop the (abs) since we know145* the source value is already a postitive integer. This cleans146* up the absnegs that get inserted when converting between nir and147* native boolean (see ir3_b2n/n2b)148*/149struct ir3_instruction *srcsrc = ssa(src->srcs[0]);150if (srcsrc && is_bool(srcsrc))151*dstflags &= ~IR3_REG_SABS;152}153154/* Tries lowering an immediate register argument to a const buffer access by155* adding to the list of immediates to be pushed to the const buffer when156* switching to this shader.157*/158static bool159lower_immed(struct ir3_cp_ctx *ctx, struct ir3_instruction *instr, unsigned n,160struct ir3_register *reg, unsigned new_flags)161{162if (!(new_flags & IR3_REG_IMMED))163return false;164165new_flags &= ~IR3_REG_IMMED;166new_flags |= IR3_REG_CONST;167168if (!ir3_valid_flags(instr, n, new_flags))169return false;170171reg = ir3_reg_clone(ctx->shader, reg);172173/* Half constant registers seems to handle only 32-bit values174* within floating-point opcodes. So convert back to 32-bit values.175*/176bool f_opcode =177(is_cat2_float(instr->opc) || is_cat3_float(instr->opc)) ? true : false;178if (f_opcode && (new_flags & IR3_REG_HALF))179reg->uim_val = fui(_mesa_half_to_float(reg->uim_val));180181/* in some cases, there are restrictions on (abs)/(neg) plus const..182* so just evaluate those and clear the flags:183*/184if (new_flags & IR3_REG_SABS) {185reg->iim_val = abs(reg->iim_val);186new_flags &= ~IR3_REG_SABS;187}188189if (new_flags & IR3_REG_FABS) {190reg->fim_val = fabs(reg->fim_val);191new_flags &= ~IR3_REG_FABS;192}193194if (new_flags & IR3_REG_SNEG) {195reg->iim_val = -reg->iim_val;196new_flags &= ~IR3_REG_SNEG;197}198199if (new_flags & IR3_REG_FNEG) {200reg->fim_val = -reg->fim_val;201new_flags &= ~IR3_REG_FNEG;202}203204/* Reallocate for 4 more elements whenever it's necessary. Note that ir3205* printing relies on having groups of 4 dwords, so we fill the unused206* slots with a dummy value.207*/208struct ir3_const_state *const_state = ir3_const_state(ctx->so);209if (const_state->immediates_count == const_state->immediates_size) {210const_state->immediates = rerzalloc(211const_state, const_state->immediates,212__typeof__(const_state->immediates[0]), const_state->immediates_size,213const_state->immediates_size + 4);214const_state->immediates_size += 4;215216for (int i = const_state->immediates_count;217i < const_state->immediates_size; i++)218const_state->immediates[i] = 0xd0d0d0d0;219}220221int i;222for (i = 0; i < const_state->immediates_count; i++) {223if (const_state->immediates[i] == reg->uim_val)224break;225}226227if (i == const_state->immediates_count) {228/* Add on a new immediate to be pushed, if we have space left in the229* constbuf.230*/231if (const_state->offsets.immediate + const_state->immediates_count / 4 >=232ir3_max_const(ctx->so))233return false;234235const_state->immediates[i] = reg->uim_val;236const_state->immediates_count++;237}238239reg->flags = new_flags;240reg->num = i + (4 * const_state->offsets.immediate);241242instr->srcs[n] = reg;243244return true;245}246247static void248unuse(struct ir3_instruction *instr)249{250debug_assert(instr->use_count > 0);251252if (--instr->use_count == 0) {253struct ir3_block *block = instr->block;254255instr->barrier_class = 0;256instr->barrier_conflict = 0;257258/* we don't want to remove anything in keeps (which could259* be things like array store's)260*/261for (unsigned i = 0; i < block->keeps_count; i++) {262debug_assert(block->keeps[i] != instr);263}264}265}266267/**268* Handles the special case of the 2nd src (n == 1) to "normal" mad269* instructions, which cannot reference a constant. See if it is270* possible to swap the 1st and 2nd sources.271*/272static bool273try_swap_mad_two_srcs(struct ir3_instruction *instr, unsigned new_flags)274{275if (!is_mad(instr->opc))276return false;277278/* NOTE: pre-swap first two src's before valid_flags(),279* which might try to dereference the n'th src:280*/281swap(instr->srcs[0], instr->srcs[1]);282283/* cat3 doesn't encode immediate, but we can lower immediate284* to const if that helps:285*/286if (new_flags & IR3_REG_IMMED) {287new_flags &= ~IR3_REG_IMMED;288new_flags |= IR3_REG_CONST;289}290291bool valid_swap =292/* can we propagate mov if we move 2nd src to first? */293ir3_valid_flags(instr, 0, new_flags) &&294/* and does first src fit in second slot? */295ir3_valid_flags(instr, 1, instr->srcs[1]->flags);296297if (!valid_swap) {298/* put things back the way they were: */299swap(instr->srcs[0], instr->srcs[1]);300} /* otherwise leave things swapped */301302return valid_swap;303}304305/**306* Handle cp for a given src register. This additionally handles307* the cases of collapsing immedate/const (which replace the src308* register with a non-ssa src) or collapsing mov's from relative309* src (which needs to also fixup the address src reference by the310* instruction).311*/312static bool313reg_cp(struct ir3_cp_ctx *ctx, struct ir3_instruction *instr,314struct ir3_register *reg, unsigned n)315{316struct ir3_instruction *src = ssa(reg);317318/* Values that are uniform inside a loop can become divergent outside319* it if the loop has a divergent trip count. This means that we can't320* propagate a copy of a shared to non-shared register if it would321* make the shared reg's live range extend outside of its loop. Users322* outside the loop would see the value for the thread(s) that last323* exited the loop, rather than for their own thread.324*/325if ((src->dsts[0]->flags & IR3_REG_SHARED) &&326src->block->loop_id != instr->block->loop_id)327return false;328329if (is_eligible_mov(src, instr, true)) {330/* simple case, no immed/const/relativ, only mov's w/ ssa src: */331struct ir3_register *src_reg = src->srcs[0];332unsigned new_flags = reg->flags;333334combine_flags(&new_flags, src);335336if (ir3_valid_flags(instr, n, new_flags)) {337if (new_flags & IR3_REG_ARRAY) {338debug_assert(!(reg->flags & IR3_REG_ARRAY));339reg->array = src_reg->array;340}341reg->flags = new_flags;342reg->def = src_reg->def;343344instr->barrier_class |= src->barrier_class;345instr->barrier_conflict |= src->barrier_conflict;346347unuse(src);348reg->def->instr->use_count++;349350return true;351}352} else if ((is_same_type_mov(src) || is_const_mov(src)) &&353/* cannot collapse const/immed/etc into control flow: */354opc_cat(instr->opc) != 0) {355/* immed/const/etc cases, which require some special handling: */356struct ir3_register *src_reg = src->srcs[0];357unsigned new_flags = reg->flags;358359if (src_reg->flags & IR3_REG_ARRAY)360return false;361362combine_flags(&new_flags, src);363364if (!ir3_valid_flags(instr, n, new_flags)) {365/* See if lowering an immediate to const would help. */366if (lower_immed(ctx, instr, n, src_reg, new_flags))367return true;368369/* special case for "normal" mad instructions, we can370* try swapping the first two args if that fits better.371*372* the "plain" MAD's (ie. the ones that don't shift first373* src prior to multiply) can swap their first two srcs if374* src[0] is !CONST and src[1] is CONST:375*/376if ((n == 1) && try_swap_mad_two_srcs(instr, new_flags)) {377return true;378} else {379return false;380}381}382383/* Here we handle the special case of mov from384* CONST and/or RELATIV. These need to be handled385* specially, because in the case of move from CONST386* there is no src ir3_instruction so we need to387* replace the ir3_register. And in the case of388* RELATIV we need to handle the address register389* dependency.390*/391if (src_reg->flags & IR3_REG_CONST) {392/* an instruction cannot reference two different393* address registers:394*/395if ((src_reg->flags & IR3_REG_RELATIV) &&396conflicts(instr->address, reg->def->instr->address))397return false;398399/* This seems to be a hw bug, or something where the timings400* just somehow don't work out. This restriction may only401* apply if the first src is also CONST.402*/403if ((opc_cat(instr->opc) == 3) && (n == 2) &&404(src_reg->flags & IR3_REG_RELATIV) && (src_reg->array.offset == 0))405return false;406407/* When narrowing constant from 32b to 16b, it seems408* to work only for float. So we should do this only with409* float opcodes.410*/411if (src->cat1.dst_type == TYPE_F16) {412/* TODO: should we have a way to tell phi/collect to use a413* float move so that this is legal?414*/415if (is_meta(instr))416return false;417if (instr->opc == OPC_MOV && !type_float(instr->cat1.src_type))418return false;419if (!is_cat2_float(instr->opc) && !is_cat3_float(instr->opc))420return false;421} else if (src->cat1.dst_type == TYPE_U16) {422if (is_meta(instr))423return true;424/* Since we set CONSTANT_DEMOTION_ENABLE, a float reference of425* what was a U16 value read from the constbuf would incorrectly426* do 32f->16f conversion, when we want to read a 16f value.427*/428if (is_cat2_float(instr->opc) || is_cat3_float(instr->opc))429return false;430}431432src_reg = ir3_reg_clone(instr->block->shader, src_reg);433src_reg->flags = new_flags;434instr->srcs[n] = src_reg;435436if (src_reg->flags & IR3_REG_RELATIV)437ir3_instr_set_address(instr, reg->def->instr->address->def->instr);438439return true;440}441442if (src_reg->flags & IR3_REG_IMMED) {443int32_t iim_val = src_reg->iim_val;444445debug_assert((opc_cat(instr->opc) == 1) ||446(opc_cat(instr->opc) == 2) ||447(opc_cat(instr->opc) == 6) ||448is_meta(instr) ||449(is_mad(instr->opc) && (n == 0)));450451if ((opc_cat(instr->opc) == 2) &&452!ir3_cat2_int(instr->opc)) {453iim_val = ir3_flut(src_reg);454if (iim_val < 0) {455/* Fall back to trying to load the immediate as a const: */456return lower_immed(ctx, instr, n, src_reg, new_flags);457}458}459460if (new_flags & IR3_REG_SABS)461iim_val = abs(iim_val);462463if (new_flags & IR3_REG_SNEG)464iim_val = -iim_val;465466if (new_flags & IR3_REG_BNOT)467iim_val = ~iim_val;468469/* other than category 1 (mov) we can only encode up to 10 bits: */470if (ir3_valid_flags(instr, n, new_flags) &&471((instr->opc == OPC_MOV) || is_meta(instr) ||472!((iim_val & ~0x3ff) && (-iim_val & ~0x3ff)))) {473new_flags &= ~(IR3_REG_SABS | IR3_REG_SNEG | IR3_REG_BNOT);474src_reg = ir3_reg_clone(instr->block->shader, src_reg);475src_reg->flags = new_flags;476src_reg->iim_val = iim_val;477instr->srcs[n] = src_reg;478479return true;480} else {481/* Fall back to trying to load the immediate as a const: */482return lower_immed(ctx, instr, n, src_reg, new_flags);483}484}485}486487return false;488}489490/* Handle special case of eliminating output mov, and similar cases where491* there isn't a normal "consuming" instruction. In this case we cannot492* collapse flags (ie. output mov from const, or w/ abs/neg flags, cannot493* be eliminated)494*/495static struct ir3_instruction *496eliminate_output_mov(struct ir3_cp_ctx *ctx, struct ir3_instruction *instr)497{498if (is_eligible_mov(instr, NULL, false)) {499struct ir3_register *reg = instr->srcs[0];500if (!(reg->flags & IR3_REG_ARRAY)) {501struct ir3_instruction *src_instr = ssa(reg);502debug_assert(src_instr);503ctx->progress = true;504return src_instr;505}506}507return instr;508}509510/**511* Find instruction src's which are mov's that can be collapsed, replacing512* the mov dst with the mov src513*/514static void515instr_cp(struct ir3_cp_ctx *ctx, struct ir3_instruction *instr)516{517if (instr->srcs_count == 0)518return;519520if (ir3_instr_check_mark(instr))521return;522523/* walk down the graph from each src: */524bool progress;525do {526progress = false;527foreach_src_n (reg, n, instr) {528struct ir3_instruction *src = ssa(reg);529530if (!src)531continue;532533instr_cp(ctx, src);534535/* TODO non-indirect access we could figure out which register536* we actually want and allow cp..537*/538if (reg->flags & IR3_REG_ARRAY)539continue;540541/* Don't CP absneg into meta instructions, that won't end well: */542if (is_meta(instr) && (src->opc != OPC_MOV))543continue;544545/* Don't CP mova and mova1 into their users */546if (writes_addr0(src) || writes_addr1(src))547continue;548549progress |= reg_cp(ctx, instr, reg, n);550ctx->progress |= progress;551}552} while (progress);553554/* After folding a mov's source we may wind up with a type-converting mov555* of an immediate. This happens e.g. with texture descriptors, since we556* narrow the descriptor (which may be a constant) to a half-reg in ir3.557* By converting the immediate in-place to the destination type, we can558* turn the mov into a same-type mov so that it can be further propagated.559*/560if (instr->opc == OPC_MOV && (instr->srcs[0]->flags & IR3_REG_IMMED) &&561instr->cat1.src_type != instr->cat1.dst_type &&562/* Only do uint types for now, until we generate other types of563* mov's during instruction selection.564*/565full_type(instr->cat1.src_type) == TYPE_U32 &&566full_type(instr->cat1.dst_type) == TYPE_U32) {567uint32_t uimm = instr->srcs[0]->uim_val;568if (instr->cat1.dst_type == TYPE_U16)569uimm &= 0xffff;570instr->srcs[0]->uim_val = uimm;571if (instr->dsts[0]->flags & IR3_REG_HALF)572instr->srcs[0]->flags |= IR3_REG_HALF;573else574instr->srcs[0]->flags &= ~IR3_REG_HALF;575instr->cat1.src_type = instr->cat1.dst_type;576ctx->progress = true;577}578579/* Re-write the instruction writing predicate register to get rid580* of the double cmps.581*/582if ((instr->opc == OPC_CMPS_S) && is_foldable_double_cmp(instr)) {583struct ir3_instruction *cond = ssa(instr->srcs[0]);584switch (cond->opc) {585case OPC_CMPS_S:586case OPC_CMPS_F:587case OPC_CMPS_U:588instr->opc = cond->opc;589instr->flags = cond->flags;590instr->cat2 = cond->cat2;591if (cond->address)592ir3_instr_set_address(instr, cond->address->def->instr);593instr->srcs[0] = ir3_reg_clone(ctx->shader, cond->srcs[0]);594instr->srcs[1] = ir3_reg_clone(ctx->shader, cond->srcs[1]);595instr->barrier_class |= cond->barrier_class;596instr->barrier_conflict |= cond->barrier_conflict;597unuse(cond);598ctx->progress = true;599break;600default:601break;602}603}604605/* Handle converting a sam.s2en (taking samp/tex idx params via register)606* into a normal sam (encoding immediate samp/tex idx) if they are607* immediate. This saves some instructions and regs in the common case608* where we know samp/tex at compile time. This needs to be done in the609* frontend for bindless tex, though, so don't replicate it here.610*/611if (is_tex(instr) && (instr->flags & IR3_INSTR_S2EN) &&612!(instr->flags & IR3_INSTR_B) &&613!(ir3_shader_debug & IR3_DBG_FORCES2EN)) {614/* The first src will be a collect, if both of it's615* two sources are mov from imm, then we can616*/617struct ir3_instruction *samp_tex = ssa(instr->srcs[0]);618619debug_assert(samp_tex->opc == OPC_META_COLLECT);620621struct ir3_register *samp = samp_tex->srcs[0];622struct ir3_register *tex = samp_tex->srcs[1];623624if ((samp->flags & IR3_REG_IMMED) && (tex->flags & IR3_REG_IMMED)) {625instr->flags &= ~IR3_INSTR_S2EN;626instr->cat5.samp = samp->iim_val;627instr->cat5.tex = tex->iim_val;628629/* shuffle around the regs to remove the first src: */630instr->srcs_count--;631for (unsigned i = 0; i < instr->srcs_count; i++) {632instr->srcs[i] = instr->srcs[i + 1];633}634635ctx->progress = true;636}637}638}639640bool641ir3_cp(struct ir3 *ir, struct ir3_shader_variant *so)642{643struct ir3_cp_ctx ctx = {644.shader = ir,645.so = so,646};647648/* This is a bit annoying, and probably wouldn't be necessary if we649* tracked a reverse link from producing instruction to consumer.650* But we need to know when we've eliminated the last consumer of651* a mov, so we need to do a pass to first count consumers of a652* mov.653*/654foreach_block (block, &ir->block_list) {655foreach_instr (instr, &block->instr_list) {656657/* by the way, we don't account for false-dep's, so the CP658* pass should always happen before false-dep's are inserted659*/660debug_assert(instr->deps_count == 0);661662foreach_ssa_src (src, instr) {663src->use_count++;664}665}666}667668ir3_clear_mark(ir);669670foreach_block (block, &ir->block_list) {671if (block->condition) {672instr_cp(&ctx, block->condition);673block->condition = eliminate_output_mov(&ctx, block->condition);674}675676for (unsigned i = 0; i < block->keeps_count; i++) {677instr_cp(&ctx, block->keeps[i]);678block->keeps[i] = eliminate_output_mov(&ctx, block->keeps[i]);679}680}681682return ctx.progress;683}684685686