pan/mdg: Optimize pipelining logic
[mesa.git] / src / panfrost / midgard / midgard_ra_pipeline.c
index 48f45cb9adb98145f2b501e081359d831ef82a6d..1037d0645d95eaf18fa0407b26468ca56bc709ac 100644 (file)
@@ -47,18 +47,30 @@ mir_pipeline_ins(
 {
         midgard_instruction *ins = bundle->instructions[i];
 
-        /* We could be pipelining a register, so we need to make sure that all
-         * of the components read in this bundle are written in this bundle,
-         * and that no components are written before this bundle */
+        /* Our goal is to create a pipeline register. Pipeline registers are
+         * created at the start of the bundle and are destroyed at the end. So
+         * we conservatively require:
+         *
+         *  1. Each component read in the second stage is written in the first stage.
+         *  2. The index is not live after the bundle.
+         *  3. We're not a special index (writeout, conditionals, ..)
+         *
+         * Rationale: #1 ensures that there is no need to go before the
+         * creation of the bundle, so the pipeline register can exist. #2 is
+         * since the pipeline register will be destroyed at the end. This
+         * ensures that nothing will try to read/write the pipeline register
+         * once it is not live, and that there's no need to go earlier. */
 
         unsigned node = ins->dest;
         unsigned read_mask = 0;
 
+        if (node >= SSA_FIXED_MINIMUM)
+                return false;
+
         /* Analyze the bundle for a per-byte read mask */
 
         for (unsigned j = 0; j < bundle->instruction_count; ++j) {
                 midgard_instruction *q = bundle->instructions[j];
-                read_mask |= mir_bytemask_of_read_components(q, node);
 
                 /* The fragment colour can't be pipelined (well, it is
                  * pipelined in r0, but this is a delicate dance with
@@ -66,11 +78,15 @@ mir_pipeline_ins(
 
                 if (q->compact_branch && q->writeout && mir_has_arg(q, node))
                         return false;
+
+                if (q->unit < UNIT_VADD) continue;
+                read_mask |= mir_bytemask_of_read_components(q, node);
         }
 
-        /* Now analyze for a write mask */
+        /* Now check what's written in the beginning stage  */
         for (unsigned j = 0; j < bundle->instruction_count; ++j) {
                 midgard_instruction *q = bundle->instructions[j];
+                if (q->unit >= UNIT_VADD) break;
                 if (q->dest != node) continue;
 
                 /* Remove the written mask from the read requirements */
@@ -81,12 +97,6 @@ mir_pipeline_ins(
         if (read_mask)
                 return false;
 
-        /* Now, check outside the bundle */
-        midgard_instruction *start = bundle->instructions[0];
-
-        if (mir_is_written_before(ctx, start, node))
-                return false;
-
         /* We want to know if we live after this bundle, so check if
          * we're live after the last instruction of the bundle */
 
@@ -97,8 +107,16 @@ mir_pipeline_ins(
                 return false;
 
         /* We're only live in this bundle -- pipeline! */
+        unsigned preg = SSA_FIXED_REGISTER(24 + pipeline_count);
+
+        for (unsigned j = 0; j < bundle->instruction_count; ++j) {
+                midgard_instruction *q = bundle->instructions[j];
 
-        mir_rewrite_index(ctx, node, SSA_FIXED_REGISTER(24 + pipeline_count));
+                if (q->unit >= UNIT_VADD)
+                        mir_rewrite_index_src_single(q, node, preg);
+                else
+                        mir_rewrite_index_dst_single(q, node, preg);
+        }
 
         return true;
 }