1 /* 2 * Copyright 2014 Broadcom 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 */ 23 24 /** 25 * @file vc4_opt_small_immediates.c 26 * 27 * Turns references to small constant uniform values into small immediates 28 * fields. 29 */ 30 31 #include "vc4_qir.h" 32 #include "vc4_qpu.h" 33 34 static bool debug; 35 36 bool 37 qir_opt_small_immediates(struct vc4_compile *c) 38 { 39 bool progress = false; 40 41 qir_for_each_inst_inorder(inst, c) { 42 /* The small immediate value sits in the raddr B field, so we 43 * can't have 2 small immediates in one instruction (unless 44 * they're the same value, but that should be optimized away 45 * elsewhere). 46 */ 47 bool uses_small_imm = false; 48 for (int i = 0; i < qir_get_nsrc(inst); i++) { 49 if (inst->src[i].file == QFILE_SMALL_IMM) 50 uses_small_imm = true; 51 } 52 if (uses_small_imm) 53 continue; 54 55 /* Don't propagate small immediates into the top-end bounds 56 * checking for indirect UBO loads. The kernel doesn't parse 57 * small immediates and rejects the shader in this case. UBO 58 * loads are much more expensive than the uniform load, and 59 * indirect UBO regions are usually much larger than a small 60 * immediate, so it's not worth updating the kernel to allow 61 * optimizing it. 62 */ 63 if (inst->op == QOP_MIN_NOIMM) 64 continue; 65 66 for (int i = 0; i < qir_get_nsrc(inst); i++) { 67 struct qreg src = qir_follow_movs(c, inst->src[i]); 68 69 if (src.file != QFILE_UNIF || 70 src.pack || 71 c->uniform_contents[src.index] != 72 QUNIFORM_CONSTANT) { 73 continue; 74 } 75 76 if (qir_is_tex(inst) && 77 i == qir_get_tex_uniform_src(inst)) { 78 /* No turning the implicit uniform read into 79 * an immediate. 80 */ 81 continue; 82 } 83 84 uint32_t imm = c->uniform_data[src.index]; 85 uint32_t small_imm = qpu_encode_small_immediate(imm); 86 if (small_imm == ~0) 87 continue; 88 89 if (debug) { 90 fprintf(stderr, "opt_small_immediate() from: "); 91 qir_dump_inst(c, inst); 92 fprintf(stderr, "\n"); 93 } 94 inst->src[i].file = QFILE_SMALL_IMM; 95 inst->src[i].index = imm; 96 if (debug) { 97 fprintf(stderr, "to: "); 98 qir_dump_inst(c, inst); 99 fprintf(stderr, "\n"); 100 } 101 progress = true; 102 break; 103 } 104 } 105 106 return progress; 107 } 108