From 6482c8084dee03efd19afc7f4fa5f9c4d7010c49 Mon Sep 17 00:00:00 2001 From: Alyssa Rosenzweig Date: Fri, 22 Jul 2022 13:21:19 -0400 Subject: [PATCH] pan/bi: Rebuild some instructions when lowering When we add a source, we need to build a new instruction (or at the very least reallocate sources). This is less of a hack anyway. Signed-off-by: Alyssa Rosenzweig Part-of: --- src/panfrost/bifrost/bi_opt_mod_props.c | 48 ++++++++++++++++++++------------- src/panfrost/bifrost/bifrost_compile.c | 4 +-- src/panfrost/bifrost/compiler.h | 2 +- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/src/panfrost/bifrost/bi_opt_mod_props.c b/src/panfrost/bifrost/bi_opt_mod_props.c index fff9332..1586afe 100644 --- a/src/panfrost/bifrost/bi_opt_mod_props.c +++ b/src/panfrost/bifrost/bi_opt_mod_props.c @@ -430,32 +430,44 @@ bi_opt_mod_prop_backward(bi_context *ctx) free(multiple); } -/** Lower pseudo instructions that exist to simplify the optimizer */ - -void -bi_lower_opt_instruction(bi_instr *I) +/* + * Lower pseudo instructions that exist to simplify the optimizer. Returns the + * replacement instruction, or NULL if no replacement is needed. + */ +static bool +bi_lower_opt_instruction_helper(bi_builder *b, bi_instr *I) { + bi_instr *repl; + switch (I->op) { case BI_OPCODE_FABSNEG_F32: - case BI_OPCODE_FABSNEG_V2F16: case BI_OPCODE_FCLAMP_F32: - case BI_OPCODE_FCLAMP_V2F16: - I->op = (bi_opcode_props[I->op].size == BI_SIZE_32) ? - BI_OPCODE_FADD_F32 : BI_OPCODE_FADD_V2F16; + repl = bi_fadd_f32_to(b, I->dest[0], I->src[0], bi_negzero()); + repl->clamp = I->clamp; + return true; - I->round = BI_ROUND_NONE; - I->src[1] = bi_negzero(); - I->nr_srcs = 2; - break; + case BI_OPCODE_FABSNEG_V2F16: + case BI_OPCODE_FCLAMP_V2F16: + repl = bi_fadd_v2f16_to(b, I->dest[0], I->src[0], bi_negzero()); + repl->clamp = I->clamp; + return true; case BI_OPCODE_DISCARD_B32: - I->op = BI_OPCODE_DISCARD_F32; - I->src[1] = bi_imm_u32(0); - I->cmpf = BI_CMPF_NE; - I->nr_srcs = 2; - break; + bi_discard_f32(b, I->src[0], bi_zero(), BI_CMPF_NE); + return true; default: - break; + return false; + } +} + +void +bi_lower_opt_instructions(bi_context *ctx) +{ + bi_foreach_instr_global_safe(ctx, I) { + bi_builder b = bi_init_builder(ctx, bi_before_instr(I)); + + if (bi_lower_opt_instruction_helper(&b, I)) + bi_remove_instruction(I); } } diff --git a/src/panfrost/bifrost/bifrost_compile.c b/src/panfrost/bifrost/bifrost_compile.c index e05a892..3568abc 100644 --- a/src/panfrost/bifrost/bifrost_compile.c +++ b/src/panfrost/bifrost/bifrost_compile.c @@ -5063,9 +5063,7 @@ bi_compile_variant_nir(nir_shader *nir, bi_validate(ctx, "Optimization passes"); } - bi_foreach_instr_global(ctx, I) { - bi_lower_opt_instruction(I); - } + bi_lower_opt_instructions(ctx); if (ctx->arch >= 9) { va_optimize(ctx); diff --git a/src/panfrost/bifrost/compiler.h b/src/panfrost/bifrost/compiler.h index 6fbdfa5..beefe64 100644 --- a/src/panfrost/bifrost/compiler.h +++ b/src/panfrost/bifrost/compiler.h @@ -1132,7 +1132,7 @@ void bi_register_allocate(bi_context *ctx); void va_optimize(bi_context *ctx); void va_lower_split_64bit(bi_context *ctx); -void bi_lower_opt_instruction(bi_instr *I); +void bi_lower_opt_instructions(bi_context *ctx); void bi_pressure_schedule(bi_context *ctx); void bi_schedule(bi_context *ctx); -- 2.7.4