From f131e210ba7f521abc7f71e86b2916da5ac6f3a8 Mon Sep 17 00:00:00 2001
From: Alyssa Rosenzweig <alyssa@collabora.com>
Date: Thu, 22 Apr 2021 15:31:16 -0400
Subject: [PATCH] panfrost: Inline blend constants

If we're going to key them in NIR, we might as well get the benefit of
constant folding them too.

Signed-off-by: Alyssa Rosenzweig <alyssa@collabora.com>
---
 src/panfrost/bifrost/bifrost_compile.c | 20 ---------------
 src/panfrost/lib/pan_blend.c           | 34 ++++++++++++++++++++++----
 src/panfrost/midgard/midgard_compile.c | 12 ---------
 src/panfrost/util/pan_ir.h             |  1 -
 4 files changed, 29 insertions(+), 38 deletions(-)

diff --git a/src/panfrost/bifrost/bifrost_compile.c b/src/panfrost/bifrost/bifrost_compile.c
index 57db2e4d97e..63d38d1a71d 100644
--- a/src/panfrost/bifrost/bifrost_compile.c
+++ b/src/panfrost/bifrost/bifrost_compile.c
@@ -1173,26 +1173,6 @@ bi_emit_intrinsic(bi_builder *b, nir_intrinsic_instr *instr)
                                 nir_dest_num_components(instr->dest), 0);
                 break;
 
-        case nir_intrinsic_load_blend_const_color_r_float:
-                bi_mov_i32_to(b, dst,
-                                bi_imm_f32(b->shader->inputs->blend.constants[0]));
-                break;
-
-        case nir_intrinsic_load_blend_const_color_g_float:
-                bi_mov_i32_to(b, dst,
-                                bi_imm_f32(b->shader->inputs->blend.constants[1]));
-                break;
-
-        case nir_intrinsic_load_blend_const_color_b_float:
-                bi_mov_i32_to(b, dst,
-                                bi_imm_f32(b->shader->inputs->blend.constants[2]));
-                break;
-
-        case nir_intrinsic_load_blend_const_color_a_float:
-                bi_mov_i32_to(b, dst,
-                                bi_imm_f32(b->shader->inputs->blend.constants[3]));
-                break;
-
 	case nir_intrinsic_load_sample_positions_pan:
                 bi_mov_i32_to(b, bi_word(dst, 0),
                                 bi_fau(BIR_FAU_SAMPLE_POS_ARRAY, false));
diff --git a/src/panfrost/lib/pan_blend.c b/src/panfrost/lib/pan_blend.c
index 487d2a201b1..101e36970aa 100644
--- a/src/panfrost/lib/pan_blend.c
+++ b/src/panfrost/lib/pan_blend.c
@@ -449,6 +449,31 @@ get_equation_str(const struct pan_blend_rt_state *rt_state,
          }
 }
 
+static bool
+pan_inline_blend_constants(nir_builder *b, nir_instr *instr, void *data)
+{
+        if (instr->type != nir_instr_type_intrinsic)
+                return false;
+
+        nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
+        if (intr->intrinsic != nir_intrinsic_load_blend_const_color_rgba)
+                return false;
+
+        float *floats = data;
+        const nir_const_value constants[4] = {
+                { .f32 = floats[0] },
+                { .f32 = floats[1] },
+                { .f32 = floats[2] },
+                { .f32 = floats[3] }
+        };
+
+        b->cursor = nir_after_instr(instr);
+        nir_ssa_def *constant = nir_build_imm(b, 4, 32, constants);
+        nir_ssa_def_rewrite_uses(&intr->dest.ssa, constant);
+        nir_instr_remove(instr);
+        return true;
+}
+
 nir_shader *
 pan_blend_create_shader(const struct panfrost_device *dev,
                         const struct pan_blend_state *state,
@@ -481,8 +506,7 @@ pan_blend_create_shader(const struct panfrost_device *dev,
                 .logicop_func = state->logicop_func,
                 .colormask = rt_state->equation.color_mask,
                 .half = nir_type == nir_type_float16,
-                .format = rt_state->format,
-                .scalar = pan_is_bifrost(dev),
+                .format = rt_state->format
         };
 
         if (!rt_state->equation.blend_enable) {
@@ -542,6 +566,9 @@ pan_blend_create_shader(const struct panfrost_device *dev,
         options.src1 = s_src[1];
 
         NIR_PASS_V(b.shader, nir_lower_blend, options);
+        nir_shader_instructions_pass(b.shader, pan_inline_blend_constants,
+                        nir_metadata_block_index | nir_metadata_dominance,
+                        (void *) state->constants);
 
         return b.shader;
 }
@@ -665,9 +692,6 @@ pan_blend_get_shader_locked(const struct panfrost_device *dev,
                 .rt_formats = { key.format },
         };
 
-        if (key.has_constants)
-                memcpy(inputs.blend.constants, state->constants, sizeof(inputs.blend.constants));
-
         if (pan_is_bifrost(dev)) {
                 inputs.blend.bifrost_blend_desc =
                         pan_blend_get_bifrost_desc(dev, key.format, key.rt, 0);
diff --git a/src/panfrost/midgard/midgard_compile.c b/src/panfrost/midgard/midgard_compile.c
index 64e3cdb25a9..b5bd86c90bf 100644
--- a/src/panfrost/midgard/midgard_compile.c
+++ b/src/panfrost/midgard/midgard_compile.c
@@ -1836,18 +1836,6 @@ emit_intrinsic(compiler_context *ctx, nir_intrinsic_instr *instr)
                 break;
         }
 
-        case nir_intrinsic_load_blend_const_color_rgba: {
-                assert(ctx->inputs->is_blend);
-                reg = nir_dest_index(&instr->dest);
-
-                midgard_instruction ins = v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT), reg);
-                ins.has_constants = true;
-                memcpy(ins.constants.f32, ctx->inputs->blend.constants,
-                       sizeof(ctx->inputs->blend.constants));
-                emit_mir_instruction(ctx, ins);
-                break;
-        }
-
         case nir_intrinsic_store_output:
         case nir_intrinsic_store_combined_output_pan:
                 assert(nir_src_is_const(instr->src[1]) && "no indirect outputs");
diff --git a/src/panfrost/util/pan_ir.h b/src/panfrost/util/pan_ir.h
index 7b6f4fdfe3a..6460ca1c0c4 100644
--- a/src/panfrost/util/pan_ir.h
+++ b/src/panfrost/util/pan_ir.h
@@ -122,7 +122,6 @@ struct panfrost_compile_inputs {
         struct {
                 unsigned rt;
                 unsigned nr_samples;
-                float constants[4];
                 uint64_t bifrost_blend_desc;
         } blend;
         unsigned sysval_ubo;
-- 
2.30.2

