Mesa (main): ir3: Add foreach_dst/foreach_dst_n

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Tue Jun 29 08:25:32 UTC 2021


Module: Mesa
Branch: main
Commit: ea325226d6206545d93379f6cbf0079a472d1707
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=ea325226d6206545d93379f6cbf0079a472d1707

Author: Connor Abbott <cwabbott0 at gmail.com>
Date:   Mon Jun 21 17:46:33 2021 +0200

ir3: Add foreach_dst/foreach_dst_n

And cleanup a few places I know of that are open-coding it

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/11565>

---

 src/freedreno/.clang-format          |  2 ++
 src/freedreno/ir3/ir3.c              |  7 ++-----
 src/freedreno/ir3/ir3.h              | 11 +++++++++++
 src/freedreno/ir3/ir3_array_to_ssa.c | 20 ++++++++------------
 src/freedreno/ir3/ir3_print.c        |  6 ++----
 src/freedreno/ir3/ir3_sched.c        | 12 ++++++------
 src/freedreno/ir3/ir3_validate.c     |  8 ++++----
 7 files changed, 35 insertions(+), 31 deletions(-)

diff --git a/src/freedreno/.clang-format b/src/freedreno/.clang-format
index 461df1c8675..4a5a4ba253f 100644
--- a/src/freedreno/.clang-format
+++ b/src/freedreno/.clang-format
@@ -67,6 +67,8 @@ ForEachMacros:
   - foreach_sched_node
   - foreach_src
   - foreach_src_n
+  - foreach_dst
+  - foreach_dst_n
   - foreach_ssa_use
   - foreach_ssa_srcp_n
   - foreach_ssa_srcp
diff --git a/src/freedreno/ir3/ir3.c b/src/freedreno/ir3/ir3.c
index f401745216e..4680be6c652 100644
--- a/src/freedreno/ir3/ir3.c
+++ b/src/freedreno/ir3/ir3.c
@@ -426,7 +426,6 @@ struct ir3_instruction * ir3_instr_clone(struct ir3_instruction *instr)
 	struct ir3_instruction *new_instr = instr_create(instr->block, instr->opc,
 			instr->dsts_count, instr->srcs_count);
 	struct ir3_register **dsts, **srcs;
-	unsigned i;
 
 	dsts = new_instr->dsts;
 	srcs = new_instr->srcs;
@@ -439,15 +438,13 @@ struct ir3_instruction * ir3_instr_clone(struct ir3_instruction *instr)
 	/* clone registers: */
 	new_instr->dsts_count = 0;
 	new_instr->srcs_count = 0;
-	for (i = 0; i < instr->dsts_count; i++) {
-		struct ir3_register *reg = instr->dsts[i];
+	foreach_dst (reg, instr) {
 		struct ir3_register *new_reg = ir3_dst_create(new_instr, reg->num, reg->flags);
 		*new_reg = *reg;
 		if (new_reg->instr)
 			new_reg->instr = new_instr;
 	}
-	for (i = 0; i < instr->srcs_count; i++) {
-		struct ir3_register *reg = instr->srcs[i];
+	foreach_src (reg, instr) {
 		struct ir3_register *new_reg = ir3_src_create(new_instr, reg->num, reg->flags);
 		*new_reg = *reg;
 	}
diff --git a/src/freedreno/ir3/ir3.h b/src/freedreno/ir3/ir3.h
index 41452856e87..5bbdc4cc748 100644
--- a/src/freedreno/ir3/ir3.h
+++ b/src/freedreno/ir3/ir3.h
@@ -1363,6 +1363,17 @@ ir3_try_swap_signedness(opc_t opc, bool *can_swap)
 #define foreach_src(__srcreg, __instr) \
 	foreach_src_n(__srcreg, __i, __instr)
 
+/* iterator for an instructions's destinations (reg), also returns dst #: */
+#define foreach_dst_n(__dstreg, __n, __instr) \
+       if ((__instr)->dsts_count) \
+               for (struct ir3_register *__dstreg = (void *)~0; __dstreg; __dstreg = NULL) \
+                       for (unsigned __cnt = (__instr)->dsts_count, __n = 0; __n < __cnt; __n++) \
+                               if ((__dstreg = (__instr)->dsts[__n]))
+
+/* iterator for an instructions's destinations (reg): */
+#define foreach_dst(__dstreg, __instr) \
+	foreach_dst_n(__dstreg, __i, __instr)
+
 static inline unsigned __ssa_src_cnt(struct ir3_instruction *instr)
 {
 	return instr->srcs_count + instr->deps_count;
diff --git a/src/freedreno/ir3/ir3_array_to_ssa.c b/src/freedreno/ir3/ir3_array_to_ssa.c
index 499002a74f7..6fdf0ee81e9 100644
--- a/src/freedreno/ir3/ir3_array_to_ssa.c
+++ b/src/freedreno/ir3/ir3_array_to_ssa.c
@@ -221,11 +221,11 @@ ir3_array_to_ssa(struct ir3 *ir)
 
 	foreach_block (block, &ir->block_list) {
 		foreach_instr (instr, &block->instr_list) {
-			for (unsigned i = 0; i < instr->dsts_count; i++) {
-				if (instr->dsts[i]->flags & IR3_REG_ARRAY) {
+			foreach_dst (dst, instr) {
+				if (dst->flags & IR3_REG_ARRAY) {
 					struct array_state *state =
-						get_state(&ctx, block, instr->dsts[i]->array.id);
-					state->live_out_definition = instr->dsts[i];
+						get_state(&ctx, block, dst->array.id);
+					state->live_out_definition = dst;
 				}
 			}
 		}
@@ -236,8 +236,7 @@ ir3_array_to_ssa(struct ir3 *ir)
 			if (instr->opc == OPC_META_PHI)
 				continue;
 
-			for (unsigned i = 0; i < instr->dsts_count; i++) {
-				struct ir3_register *reg = instr->dsts[i];
+			foreach_dst (reg, instr) {
 				if ((reg->flags & IR3_REG_ARRAY) && !reg->tied) {
 					struct ir3_array *arr = ir3_lookup_array(ir, reg->array.id);
 
@@ -245,8 +244,7 @@ ir3_array_to_ssa(struct ir3 *ir)
 					read_value_beginning(&ctx, block, arr);
 				}
 			}
-			for (unsigned i = 0; i < instr->srcs_count; i++) {
-				struct ir3_register *reg = instr->srcs[i];
+			foreach_src (reg, instr) {
 				if ((reg->flags & IR3_REG_ARRAY) && !reg->def) {
 					struct ir3_array *arr = ir3_lookup_array(ir, reg->array.id);
 
@@ -279,8 +277,7 @@ ir3_array_to_ssa(struct ir3 *ir)
 					instr->srcs[i] = lookup_value(instr->srcs[i]);
 				}
 			} else {
-				for (unsigned i = 0; i < instr->dsts_count; i++) {
-					struct ir3_register *reg = instr->dsts[i];
+				foreach_dst (reg, instr) {
 					if ((reg->flags & IR3_REG_ARRAY)) {
 						if (!reg->tied) {
 							struct ir3_register *def =
@@ -291,8 +288,7 @@ ir3_array_to_ssa(struct ir3 *ir)
 						reg->flags |= IR3_REG_SSA;
 					}
 				}
-				for (unsigned i = 0; i < instr->srcs_count; i++) {
-					struct ir3_register *reg = instr->srcs[i];
+				foreach_src (reg, instr) {
 					if ((reg->flags & IR3_REG_ARRAY)) {
 						/* It is assumed that before calling
 						 * ir3_array_to_ssa(), reg->def was set to the
diff --git a/src/freedreno/ir3/ir3_print.c b/src/freedreno/ir3/ir3_print.c
index db2190a565d..14fc4ef9a9f 100644
--- a/src/freedreno/ir3/ir3_print.c
+++ b/src/freedreno/ir3/ir3_print.c
@@ -275,8 +275,7 @@ print_instr(struct log_stream *stream, struct ir3_instruction *instr, int lvl)
 
 	if (!is_flow(instr) || instr->opc == OPC_END || instr->opc == OPC_CHMASK) {
 		bool first = true;
-		for (unsigned i = 0; i < instr->dsts_count; i++) {
-			struct ir3_register *reg = instr->dsts[i];
+		foreach_dst (reg, instr) {
 			if (dest_regs(instr) == 0)
 				continue;
 			if (!first)
@@ -284,8 +283,7 @@ print_instr(struct log_stream *stream, struct ir3_instruction *instr, int lvl)
 			print_reg_name(stream, instr, reg, true);
 			first = false;
 		}
-		for (unsigned i = 0; i < instr->srcs_count; i++) {
-			struct ir3_register *reg = instr->srcs[i];
+		foreach_src (reg, instr) {
 			if (!first)
 				mesa_log_stream_printf(stream, ", ");
 			print_reg_name(stream, instr, reg, false);
diff --git a/src/freedreno/ir3/ir3_sched.c b/src/freedreno/ir3/ir3_sched.c
index 69281d37692..08ce91ca064 100644
--- a/src/freedreno/ir3/ir3_sched.c
+++ b/src/freedreno/ir3/ir3_sched.c
@@ -1235,12 +1235,12 @@ get_array_id(struct ir3_instruction *instr)
 	 * src or dst, ir3_cp should enforce this.
 	 */
 
-	for (unsigned i = 0; i < instr->dsts_count; i++)
-		if (instr->dsts[i]->flags & IR3_REG_ARRAY)
-			return instr->dsts[i]->array.id;
-	for (unsigned i = 0; i < instr->srcs_count; i++)
-		if (instr->srcs[i]->flags & IR3_REG_ARRAY)
-			return instr->srcs[i]->array.id;
+	foreach_dst (dst, instr)
+		if (dst->flags & IR3_REG_ARRAY)
+			return dst->array.id;
+	foreach_src (src, instr)
+		if (src->flags & IR3_REG_ARRAY)
+			return src->array.id;
 
 	unreachable("this was unexpected");
 }
diff --git a/src/freedreno/ir3/ir3_validate.c b/src/freedreno/ir3/ir3_validate.c
index 38e3ecaea3b..d091031e46e 100644
--- a/src/freedreno/ir3/ir3_validate.c
+++ b/src/freedreno/ir3/ir3_validate.c
@@ -75,8 +75,8 @@ validate_src(struct ir3_validate_ctx *ctx, struct ir3_instruction *instr,
 	if (reg->tied) {
 		validate_assert(ctx, reg->tied->tied == reg);
 		bool found = false;
-		for (unsigned i = 0; i < instr->dsts_count; i++) {
-			if (instr->dsts[i] == reg->tied) {
+		foreach_dst (dst, instr) {
+			if (dst == reg->tied) {
 				found = true;
 				break;
 			}
@@ -124,8 +124,8 @@ validate_dst(struct ir3_validate_ctx *ctx, struct ir3_instruction *instr,
 			validate_assert(ctx, reg->tied->size == reg->size);
 		}
 		bool found = false;
-		for (unsigned i = 0; i < instr->srcs_count; i++) {
-			if (instr->srcs[i] == reg->tied) {
+		foreach_src (src, instr) {
+			if (src == reg->tied) {
 				found = true;
 				break;
 			}



More information about the mesa-commit mailing list