Path: blob/21.2-virgl/src/panfrost/midgard/midgard_print.c
4564 views
/*1* Copyright (C) 2018-2019 Alyssa Rosenzweig <[email protected]>2* Copyright (C) 2019-2020 Collabora, Ltd.3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,20* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*/2324#include <math.h>2526#include "util/bitscan.h"27#include "util/half_float.h"28#include "compiler.h"29#include "helpers.h"30#include "midgard_ops.h"3132/* Pretty printer for Midgard IR, for use debugging compiler-internal33* passes like register allocation. The output superficially resembles34* Midgard assembly, with the exception that unit information and such is35* (normally) omitted, and generic indices are usually used instead of36* registers */3738static void39mir_print_index(int source)40{41if (source == ~0) {42printf("_");43return;44}4546if (source >= SSA_FIXED_MINIMUM) {47/* Specific register */48int reg = SSA_REG_FROM_FIXED(source);4950/* TODO: Moving threshold */51if (reg > 16 && reg < 24)52printf("u%d", 23 - reg);53else54printf("r%d", reg);55} else {56printf("%d", source);57}58}5960static const char components[16] = "xyzwefghijklmnop";6162static void63mir_print_mask(unsigned mask)64{65printf(".");6667for (unsigned i = 0; i < 16; ++i) {68if (mask & (1 << i))69putchar(components[i]);70}71}7273static void74mir_print_swizzle(unsigned *swizzle, nir_alu_type T)75{76unsigned comps = mir_components_for_type(T);7778printf(".");7980for (unsigned i = 0; i < comps; ++i) {81unsigned C = swizzle[i];82assert(C < comps);83putchar(components[C]);84}85}8687static const char *88mir_get_unit(unsigned unit)89{90switch (unit) {91case ALU_ENAB_VEC_MUL:92return "vmul";93case ALU_ENAB_SCAL_ADD:94return "sadd";95case ALU_ENAB_VEC_ADD:96return "vadd";97case ALU_ENAB_SCAL_MUL:98return "smul";99case ALU_ENAB_VEC_LUT:100return "lut";101case ALU_ENAB_BR_COMPACT:102return "br";103case ALU_ENAB_BRANCH:104return "brx";105default:106return "???";107}108}109110static void111mir_print_embedded_constant(midgard_instruction *ins, unsigned src_idx)112{113assert(src_idx <= 1);114115unsigned base_size = max_bitsize_for_alu(ins);116unsigned sz = nir_alu_type_get_type_size(ins->src_types[src_idx]);117bool half = (sz == (base_size >> 1));118unsigned mod = mir_pack_mod(ins, src_idx, false);119unsigned *swizzle = ins->swizzle[src_idx];120midgard_reg_mode reg_mode = reg_mode_for_bitsize(max_bitsize_for_alu(ins));121unsigned comp_mask = effective_writemask(ins->op, ins->mask);122unsigned num_comp = util_bitcount(comp_mask);123unsigned max_comp = mir_components_for_type(ins->dest_type);124bool first = true;125126printf("#");127128if (num_comp > 1)129printf("vec%d(", num_comp);130131for (unsigned comp = 0; comp < max_comp; comp++) {132if (!(comp_mask & (1 << comp)))133continue;134135if (first)136first = false;137else138printf(", ");139140mir_print_constant_component(stdout, &ins->constants,141swizzle[comp], reg_mode,142half, mod, ins->op);143}144145if (num_comp > 1)146printf(")");147}148149#define PRINT_SRC(ins, c) \150do { mir_print_index(ins->src[c]); \151if (ins->src[c] != ~0 && ins->src_types[c] != nir_type_invalid) { \152pan_print_alu_type(ins->src_types[c], stdout); \153mir_print_swizzle(ins->swizzle[c], ins->src_types[c]); \154} } while (0)155156void157mir_print_instruction(midgard_instruction *ins)158{159printf("\t");160161if (midgard_is_branch_unit(ins->unit)) {162const char *branch_target_names[] = {163"goto", "break", "continue", "discard"164};165166printf("%s.", mir_get_unit(ins->unit));167if (ins->branch.target_type == TARGET_DISCARD)168printf("discard.");169else if (ins->writeout)170printf("write.");171else if (ins->unit == ALU_ENAB_BR_COMPACT &&172!ins->branch.conditional)173printf("uncond.");174else175printf("cond.");176177if (!ins->branch.conditional)178printf("always");179else if (ins->branch.invert_conditional)180printf("false");181else182printf("true");183184if (ins->writeout) {185printf(" (c: ");186PRINT_SRC(ins, 0);187printf(", z: ");188PRINT_SRC(ins, 2);189printf(", s: ");190PRINT_SRC(ins, 3);191printf(")");192}193194if (ins->branch.target_type != TARGET_DISCARD)195printf(" %s -> block(%d)\n",196ins->branch.target_type < 4 ?197branch_target_names[ins->branch.target_type] : "??",198ins->branch.target_block);199200return;201}202203switch (ins->type) {204case TAG_ALU_4: {205midgard_alu_op op = ins->op;206const char *name = alu_opcode_props[op].name;207208if (ins->unit)209printf("%s.", mir_get_unit(ins->unit));210211printf("%s", name ? name : "??");212break;213}214215case TAG_LOAD_STORE_4: {216midgard_load_store_op op = ins->op;217const char *name = load_store_opcode_props[op].name;218219assert(name);220printf("%s", name);221break;222}223224case TAG_TEXTURE_4: {225printf("TEX");226227if (ins->helper_terminate)228printf(".terminate");229230if (ins->helper_execute)231printf(".execute");232233break;234}235236default:237assert(0);238}239240if (ins->compact_branch && ins->branch.invert_conditional)241printf(".not");242243printf(" ");244mir_print_index(ins->dest);245246if (ins->dest != ~0) {247pan_print_alu_type(ins->dest_type, stdout);248mir_print_mask(ins->mask);249}250251printf(", ");252253/* Only ALU can have an embedded constant, r26 as read on load/store is254* something else entirely */255bool is_alu = ins->type == TAG_ALU_4;256unsigned r_constant = SSA_FIXED_REGISTER(REGISTER_CONSTANT);257258if (ins->src[0] == r_constant && is_alu)259mir_print_embedded_constant(ins, 0);260else261PRINT_SRC(ins, 0);262263printf(", ");264265if (ins->has_inline_constant)266printf("#%d", ins->inline_constant);267else if (ins->src[1] == r_constant && is_alu)268mir_print_embedded_constant(ins, 1);269else270PRINT_SRC(ins, 1);271272for (unsigned c = 2; c <= 3; ++c) {273printf(", ");274PRINT_SRC(ins, c);275}276277if (ins->no_spill)278printf(" /* no spill */");279280printf("\n");281}282283/* Dumps MIR for a block or entire shader respective */284285void286mir_print_block(midgard_block *block)287{288printf("block%u: {\n", block->base.name);289290if (block->scheduled) {291mir_foreach_bundle_in_block(block, bundle) {292for (unsigned i = 0; i < bundle->instruction_count; ++i)293mir_print_instruction(bundle->instructions[i]);294295printf("\n");296}297} else {298mir_foreach_instr_in_block(block, ins) {299mir_print_instruction(ins);300}301}302303printf("}");304305if (block->base.successors[0]) {306printf(" -> ");307pan_foreach_successor((&block->base), succ)308printf(" block%u ", succ->name);309}310311printf(" from { ");312mir_foreach_predecessor(block, pred)313printf("block%u ", pred->base.name);314printf("}");315316printf("\n\n");317}318319void320mir_print_shader(compiler_context *ctx)321{322mir_foreach_block(ctx, block) {323mir_print_block((midgard_block *) block);324}325}326327328