Path: blob/21.2-virgl/src/intel/compiler/brw_fs_cmod_propagation.cpp
4550 views
/*1* Copyright © 2014 Intel Corporation2*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, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include "brw_fs.h"24#include "brw_cfg.h"25#include "brw_eu.h"2627/** @file brw_fs_cmod_propagation.cpp28*29* Implements a pass that propagates the conditional modifier from a CMP x 0.030* instruction into the instruction that generated x. For instance, in this31* sequence32*33* add(8) g70<1>F g69<8,8,1>F 4096F34* cmp.ge.f0(8) null g70<8,8,1>F 0F35*36* we can do the comparison as part of the ADD instruction directly:37*38* add.ge.f0(8) g70<1>F g69<8,8,1>F 4096F39*40* If there had been a use of the flag register and another CMP using g7041*42* add.ge.f0(8) g70<1>F g69<8,8,1>F 4096F43* (+f0) sel(8) g71<F> g72<8,8,1>F g73<8,8,1>F44* cmp.ge.f0(8) null g70<8,8,1>F 0F45*46* we can recognize that the CMP is generating the flag value that already47* exists and therefore remove the instruction.48*/4950using namespace brw;5152static bool53cmod_propagate_cmp_to_add(const intel_device_info *devinfo, bblock_t *block,54fs_inst *inst)55{56bool read_flag = false;57const unsigned flags_written = inst->flags_written(devinfo);5859foreach_inst_in_block_reverse_starting_from(fs_inst, scan_inst, inst) {60if (scan_inst->opcode == BRW_OPCODE_ADD &&61!scan_inst->is_partial_write() &&62scan_inst->exec_size == inst->exec_size) {63bool negate;6465/* A CMP is basically a subtraction. The result of the66* subtraction must be the same as the result of the addition.67* This means that one of the operands must be negated. So (a +68* b) vs (a == -b) or (a + -b) vs (a == b).69*/70if ((inst->src[0].equals(scan_inst->src[0]) &&71inst->src[1].negative_equals(scan_inst->src[1])) ||72(inst->src[0].equals(scan_inst->src[1]) &&73inst->src[1].negative_equals(scan_inst->src[0]))) {74negate = false;75} else if ((inst->src[0].negative_equals(scan_inst->src[0]) &&76inst->src[1].equals(scan_inst->src[1])) ||77(inst->src[0].negative_equals(scan_inst->src[1]) &&78inst->src[1].equals(scan_inst->src[0]))) {79negate = true;80} else {81goto not_match;82}8384/* If the scan instruction writes a different flag register than the85* instruction we're trying to propagate from, bail.86*87* FINISHME: The second part of the condition may be too strong.88* Perhaps (scan_inst->flags_written() & flags_written) !=89* flags_written?90*/91if (scan_inst->flags_written(devinfo) != 0 &&92scan_inst->flags_written(devinfo) != flags_written)93goto not_match;9495/* From the Kaby Lake PRM Vol. 7 "Assigning Conditional Flags":96*97* * Note that the [post condition signal] bits generated at98* the output of a compute are before the .sat.99*100* Paragraph about post_zero does not mention saturation, but101* testing it on actual GPUs shows that conditional modifiers102* are applied after saturation.103*104* * post_zero bit: This bit reflects whether the final105* result is zero after all the clamping, normalizing,106* or format conversion logic.107*108* For signed types we don't care about saturation: it won't109* change the result of conditional modifier.110*111* For floating and unsigned types there two special cases,112* when we can remove inst even if scan_inst is saturated: G113* and LE. Since conditional modifiers are just comparations114* against zero, saturating positive values to the upper115* limit never changes the result of comparation.116*117* For negative values:118* (sat(x) > 0) == (x > 0) --- false119* (sat(x) <= 0) == (x <= 0) --- true120*/121const enum brw_conditional_mod cond =122negate ? brw_swap_cmod(inst->conditional_mod)123: inst->conditional_mod;124125if (scan_inst->saturate &&126(brw_reg_type_is_floating_point(scan_inst->dst.type) ||127type_is_unsigned_int(scan_inst->dst.type)) &&128(cond != BRW_CONDITIONAL_G &&129cond != BRW_CONDITIONAL_LE))130goto not_match;131132/* Otherwise, try propagating the conditional. */133if (scan_inst->can_do_cmod() &&134((!read_flag && scan_inst->conditional_mod == BRW_CONDITIONAL_NONE) ||135scan_inst->conditional_mod == cond)) {136scan_inst->conditional_mod = cond;137inst->remove(block, true);138return true;139}140break;141}142143not_match:144if ((scan_inst->flags_written(devinfo) & flags_written) != 0)145break;146147read_flag = read_flag ||148(scan_inst->flags_read(devinfo) & flags_written) != 0;149}150151return false;152}153154/**155* Propagate conditional modifiers from NOT instructions156*157* Attempt to convert sequences like158*159* or(8) g78<8,8,1> g76<8,8,1>UD g77<8,8,1>UD160* ...161* not.nz.f0(8) null g78<8,8,1>UD162*163* into164*165* or.z.f0(8) g78<8,8,1> g76<8,8,1>UD g77<8,8,1>UD166*/167static bool168cmod_propagate_not(const intel_device_info *devinfo, bblock_t *block,169fs_inst *inst)170{171const enum brw_conditional_mod cond = brw_negate_cmod(inst->conditional_mod);172bool read_flag = false;173const unsigned flags_written = inst->flags_written(devinfo);174175if (cond != BRW_CONDITIONAL_Z && cond != BRW_CONDITIONAL_NZ)176return false;177178foreach_inst_in_block_reverse_starting_from(fs_inst, scan_inst, inst) {179if (regions_overlap(scan_inst->dst, scan_inst->size_written,180inst->src[0], inst->size_read(0))) {181if (scan_inst->opcode != BRW_OPCODE_OR &&182scan_inst->opcode != BRW_OPCODE_AND)183break;184185if (scan_inst->is_partial_write() ||186scan_inst->dst.offset != inst->src[0].offset ||187scan_inst->exec_size != inst->exec_size)188break;189190/* If the scan instruction writes a different flag register than the191* instruction we're trying to propagate from, bail.192*193* FINISHME: The second part of the condition may be too strong.194* Perhaps (scan_inst->flags_written() & flags_written) !=195* flags_written?196*/197if (scan_inst->flags_written(devinfo) != 0 &&198scan_inst->flags_written(devinfo) != flags_written)199break;200201if (scan_inst->can_do_cmod() &&202((!read_flag && scan_inst->conditional_mod == BRW_CONDITIONAL_NONE) ||203scan_inst->conditional_mod == cond)) {204scan_inst->conditional_mod = cond;205inst->remove(block, true);206return true;207}208break;209}210211if ((scan_inst->flags_written(devinfo) & flags_written) != 0)212break;213214read_flag = read_flag ||215(scan_inst->flags_read(devinfo) & flags_written) != 0;216}217218return false;219}220221static bool222opt_cmod_propagation_local(const intel_device_info *devinfo, bblock_t *block)223{224bool progress = false;225int ip = block->end_ip + 1;226227foreach_inst_in_block_reverse_safe(fs_inst, inst, block) {228ip--;229230if ((inst->opcode != BRW_OPCODE_AND &&231inst->opcode != BRW_OPCODE_CMP &&232inst->opcode != BRW_OPCODE_MOV &&233inst->opcode != BRW_OPCODE_NOT) ||234inst->predicate != BRW_PREDICATE_NONE ||235!inst->dst.is_null() ||236(inst->src[0].file != VGRF && inst->src[0].file != ATTR &&237inst->src[0].file != UNIFORM))238continue;239240/* An ABS source modifier can only be handled when processing a compare241* with a value other than zero.242*/243if (inst->src[0].abs &&244(inst->opcode != BRW_OPCODE_CMP || inst->src[1].is_zero()))245continue;246247/* Only an AND.NZ can be propagated. Many AND.Z instructions are248* generated (for ir_unop_not in fs_visitor::emit_bool_to_cond_code).249* Propagating those would require inverting the condition on the CMP.250* This changes both the flag value and the register destination of the251* CMP. That result may be used elsewhere, so we can't change its value252* on a whim.253*/254if (inst->opcode == BRW_OPCODE_AND &&255!(inst->src[1].is_one() &&256inst->conditional_mod == BRW_CONDITIONAL_NZ &&257!inst->src[0].negate))258continue;259260if (inst->opcode == BRW_OPCODE_MOV &&261inst->conditional_mod != BRW_CONDITIONAL_NZ)262continue;263264/* A CMP with a second source of zero can match with anything. A CMP265* with a second source that is not zero can only match with an ADD266* instruction.267*268* Only apply this optimization to float-point sources. It can fail for269* integers. For inputs a = 0x80000000, b = 4, int(0x80000000) < 4, but270* int(0x80000000) - 4 overflows and results in 0x7ffffffc. that's not271* less than zero, so the flags get set differently than for (a < b).272*/273if (inst->opcode == BRW_OPCODE_CMP && !inst->src[1].is_zero()) {274if (brw_reg_type_is_floating_point(inst->src[0].type) &&275cmod_propagate_cmp_to_add(devinfo, block, inst))276progress = true;277278continue;279}280281if (inst->opcode == BRW_OPCODE_NOT) {282progress = cmod_propagate_not(devinfo, block, inst) || progress;283continue;284}285286bool read_flag = false;287const unsigned flags_written = inst->flags_written(devinfo);288foreach_inst_in_block_reverse_starting_from(fs_inst, scan_inst, inst) {289if (regions_overlap(scan_inst->dst, scan_inst->size_written,290inst->src[0], inst->size_read(0))) {291/* If the scan instruction writes a different flag register than292* the instruction we're trying to propagate from, bail.293*294* FINISHME: The second part of the condition may be too strong.295* Perhaps (scan_inst->flags_written() & flags_written) !=296* flags_written?297*/298if (scan_inst->flags_written(devinfo) != 0 &&299scan_inst->flags_written(devinfo) != flags_written)300break;301302if (scan_inst->is_partial_write() ||303scan_inst->dst.offset != inst->src[0].offset ||304scan_inst->exec_size != inst->exec_size)305break;306307/* CMP's result is the same regardless of dest type. */308if (inst->conditional_mod == BRW_CONDITIONAL_NZ &&309scan_inst->opcode == BRW_OPCODE_CMP &&310brw_reg_type_is_integer(inst->dst.type)) {311inst->remove(block, true);312progress = true;313break;314}315316/* If the AND wasn't handled by the previous case, it isn't safe317* to remove it.318*/319if (inst->opcode == BRW_OPCODE_AND)320break;321322/* Not safe to use inequality operators if the types are different323*/324if (scan_inst->dst.type != inst->src[0].type &&325inst->conditional_mod != BRW_CONDITIONAL_Z &&326inst->conditional_mod != BRW_CONDITIONAL_NZ)327break;328329/* Comparisons operate differently for ints and floats */330if (scan_inst->dst.type != inst->dst.type) {331/* Comparison result may be altered if the bit-size changes332* since that affects range, denorms, etc333*/334if (type_sz(scan_inst->dst.type) != type_sz(inst->dst.type))335break;336337/* We should propagate from a MOV to another instruction in a338* sequence like:339*340* and(16) g31<1>UD g20<8,8,1>UD g22<8,8,1>UD341* mov.nz.f0(16) null<1>F g31<8,8,1>D342*/343if (inst->opcode == BRW_OPCODE_MOV) {344if ((inst->src[0].type != BRW_REGISTER_TYPE_D &&345inst->src[0].type != BRW_REGISTER_TYPE_UD) ||346(scan_inst->dst.type != BRW_REGISTER_TYPE_D &&347scan_inst->dst.type != BRW_REGISTER_TYPE_UD)) {348break;349}350} else if (brw_reg_type_is_floating_point(scan_inst->dst.type) !=351brw_reg_type_is_floating_point(inst->dst.type)) {352break;353}354}355356/* Knowing following:357* - CMP writes to flag register the result of358* applying cmod to the `src0 - src1`.359* After that it stores the same value to dst.360* Other instructions first store their result to361* dst, and then store cmod(dst) to the flag362* register.363* - inst is either CMP or MOV364* - inst->dst is null365* - inst->src[0] overlaps with scan_inst->dst366* - inst->src[1] is zero367* - scan_inst wrote to a flag register368*369* There can be three possible paths:370*371* - scan_inst is CMP:372*373* Considering that src0 is either 0x0 (false),374* or 0xffffffff (true), and src1 is 0x0:375*376* - If inst's cmod is NZ, we can always remove377* scan_inst: NZ is invariant for false and true. This378* holds even if src0 is NaN: .nz is the only cmod,379* that returns true for NaN.380*381* - .g is invariant if src0 has a UD type382*383* - .l is invariant if src0 has a D type384*385* - scan_inst and inst have the same cmod:386*387* If scan_inst is anything than CMP, it already388* wrote the appropriate value to the flag register.389*390* - else:391*392* We can change cmod of scan_inst to that of inst,393* and remove inst. It is valid as long as we make394* sure that no instruction uses the flag register395* between scan_inst and inst.396*/397if (!inst->src[0].negate &&398scan_inst->flags_written(devinfo)) {399if (scan_inst->opcode == BRW_OPCODE_CMP) {400if ((inst->conditional_mod == BRW_CONDITIONAL_NZ) ||401(inst->conditional_mod == BRW_CONDITIONAL_G &&402inst->src[0].type == BRW_REGISTER_TYPE_UD) ||403(inst->conditional_mod == BRW_CONDITIONAL_L &&404inst->src[0].type == BRW_REGISTER_TYPE_D)) {405inst->remove(block, true);406progress = true;407break;408}409} else if (scan_inst->conditional_mod == inst->conditional_mod) {410/* On Gfx4 and Gfx5 sel.cond will dirty the flags, but the411* flags value is not based on the result stored in the412* destination. On all other platforms sel.cond will not413* write the flags, so execution will not get to this point.414*/415if (scan_inst->opcode == BRW_OPCODE_SEL) {416assert(devinfo->ver <= 5);417} else {418inst->remove(block, true);419progress = true;420}421422break;423} else if (!read_flag) {424scan_inst->conditional_mod = inst->conditional_mod;425inst->remove(block, true);426progress = true;427break;428}429}430431/* The conditional mod of the CMP/CMPN instructions behaves432* specially because the flag output is not calculated from the433* result of the instruction, but the other way around, which434* means that even if the condmod to propagate and the condmod435* from the CMP instruction are the same they will in general give436* different results because they are evaluated based on different437* inputs.438*/439if (scan_inst->opcode == BRW_OPCODE_CMP ||440scan_inst->opcode == BRW_OPCODE_CMPN)441break;442443/* From the Sky Lake PRM, Vol 2a, "Multiply":444*445* "When multiplying integer data types, if one of the sources446* is a DW, the resulting full precision data is stored in447* the accumulator. However, if the destination data type is448* either W or DW, the low bits of the result are written to449* the destination register and the remaining high bits are450* discarded. This results in undefined Overflow and Sign451* flags. Therefore, conditional modifiers and saturation452* (.sat) cannot be used in this case."453*454* We just disallow cmod propagation on all integer multiplies.455*/456if (!brw_reg_type_is_floating_point(scan_inst->dst.type) &&457scan_inst->opcode == BRW_OPCODE_MUL)458break;459460enum brw_conditional_mod cond =461inst->src[0].negate ? brw_swap_cmod(inst->conditional_mod)462: inst->conditional_mod;463464/* From the Sky Lake PRM Vol. 7 "Assigning Conditional Mods":465*466* * Note that the [post condition signal] bits generated at467* the output of a compute are before the .sat.468*469* This limits the cases where we can propagate the conditional470* modifier. If scan_inst has a saturate modifier, then we can471* only propagate from inst if inst is 'scan_inst <= 0',472* 'scan_inst == 0', 'scan_inst != 0', or 'scan_inst > 0'. If473* inst is 'scan_inst == 0', the conditional modifier must be474* replace with LE. Likewise, if inst is 'scan_inst != 0', the475* conditional modifier must be replace with G.476*477* The only other cases are 'scan_inst < 0' (which is a478* contradiction) and 'scan_inst >= 0' (which is a tautology).479*/480if (scan_inst->saturate) {481if (scan_inst->dst.type != BRW_REGISTER_TYPE_F)482break;483484if (cond != BRW_CONDITIONAL_Z &&485cond != BRW_CONDITIONAL_NZ &&486cond != BRW_CONDITIONAL_LE &&487cond != BRW_CONDITIONAL_G)488break;489490if (inst->opcode != BRW_OPCODE_MOV &&491inst->opcode != BRW_OPCODE_CMP)492break;493494/* inst->src[1].is_zero() was tested before, but be safe495* against possible future changes in this code.496*/497assert(inst->opcode != BRW_OPCODE_CMP || inst->src[1].is_zero());498499if (cond == BRW_CONDITIONAL_Z)500cond = BRW_CONDITIONAL_LE;501else if (cond == BRW_CONDITIONAL_NZ)502cond = BRW_CONDITIONAL_G;503}504505/* Otherwise, try propagating the conditional. */506if (scan_inst->can_do_cmod() &&507((!read_flag && scan_inst->conditional_mod == BRW_CONDITIONAL_NONE) ||508scan_inst->conditional_mod == cond)) {509scan_inst->conditional_mod = cond;510scan_inst->flag_subreg = inst->flag_subreg;511inst->remove(block, true);512progress = true;513}514break;515}516517if ((scan_inst->flags_written(devinfo) & flags_written) != 0)518break;519520read_flag = read_flag ||521(scan_inst->flags_read(devinfo) & flags_written) != 0;522}523}524525/* There is progress if and only if instructions were removed. */526assert(progress == (block->end_ip_delta != 0));527528return progress;529}530531bool532fs_visitor::opt_cmod_propagation()533{534bool progress = false;535536foreach_block_reverse(block, cfg) {537progress = opt_cmod_propagation_local(devinfo, block) || progress;538}539540if (progress) {541cfg->adjust_block_ips();542543invalidate_analysis(DEPENDENCY_INSTRUCTIONS);544}545546return progress;547}548549550