Skip to contentSkip to conversation

fix SSA conversion of mismatched memops

PATCH New Luc Van Oostenryck 1 participant 5 messages Mar 9, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover fix SSA conversion of mismatched memops

The SSA conversion works under the assumption that all the memory operations on a given symbol always refer to the same object (same size, offset and type/kind of object) but doesn't check this well enough. This series now does.

Luc Van Oostenryck (4):
ssa: add some testcases for mismatched memops
ssa: the sparse set is not needed
ssa: avoid SSA conversion of packed bitfields
ssa: fix conversion with mismatched size or offset

 Makefile                             |   1 -
 linearize.h                          |   2 +-
 ssa.c                                | 111 +++++++++++++++++++++------
 sset.c                               |  28 -------
 sset.h                               |  56 --------------
 validation/mem2reg/not-same-memop0.c |  48 ++++++++++++
 validation/mem2reg/packed-bitfield.c |  35 +++++++++
 7 files changed, 170 insertions(+), 111 deletions(-)
 delete mode 100644 sset.c
 delete mode 100644 sset.h
 create mode 100644 validation/mem2reg/not-same-memop0.c
 create mode 100644 validation/mem2reg/packed-bitfield.c
Signature · 1 line
2.30.0
#1 lore raw

PATCH 1/4 ssa: add some testcases for mismatched memops

The SSA conversion is incorrect when the size or offset of the memory operations doesn't match. It shouldn't be done at all.

So, add a few testcases for this.

---
 validation/mem2reg/not-same-memop0.c | 49 ++++++++++++++++++++++++++++
 validation/mem2reg/packed-bitfield.c | 36 ++++++++++++++++++++
 2 files changed, 85 insertions(+)
 create mode 100644 validation/mem2reg/not-same-memop0.c
 create mode 100644 validation/mem2reg/packed-bitfield.c
validation/mem2reg/not-same-memop0.c new file +49 -0
new file mode 100644
index 000000000000..7a84ddce4efc
@@ -0,0 +1,49 @@
+struct s {
+	int:16;
+	short f:6;
+};
+
+static short local(struct s s)
+{
+	return s.f;
+}
+
+static void foo(struct s s)
+{
+	while (s.f) ;
+}
+
+/*
+ * check-name: not-same-memop0
+ * check-command: test-linearize -Wno-decl -fdump-ir=mem2reg $file
+ * check-known-to-fail
+ *
+ * check-output-start
+local:
+.L0:
+	<entry-point>
+	store.32    %arg1 -> 0[s]
+	load.16     %r1 <- 2[s]
+	trunc.6     %r2 <- (16) %r1
+	sext.16     %r3 <- (6) %r2
+	ret.16      %r3
+
+
+foo:
+.L2:
+	<entry-point>
+	store.32    %arg1 -> 0[s]
+	br          .L6
+
+.L6:
+	load.16     %r5 <- 2[s]
+	trunc.6     %r6 <- (16) %r5
+	setne.1     %r7 <- %r6, $0
+	cbr         %r7, .L6, .L5
+
+.L5:
+	ret
+
+
+ * check-output-end
+ */
validation/mem2reg/packed-bitfield.c new file +36 -0
new file mode 100644
index 000000000000..4eaf0befeaf5
@@ -0,0 +1,36 @@
+struct s {
+	int:16;
+	int f:16;
+} __attribute__((__packed__));
+
+static void foo(struct s s)
+{
+	while (s.f)
+		;
+}
+
+/*
+ * check-name: packed-bitfield
+ * check-command: test-linearize -fmem2reg $file
+ * check-known-to-fail
+ *
+ * check-output-contains: store.32
+ * check-output-contains: load.16
+ *
+ * check-output-start
+foo:
+.L0:
+	<entry-point>
+	store.32    %arg1 -> 0[s]
+	br          .L4
+
+.L4:
+	load.16     %r1 <- 2[s]
+	cbr         %r1, .L4, .L3
+
+.L3:
+	ret
+
+
+ * check-output-end
+ */
Signature · 1 line
2.30.0
#2 lore raw

PATCH 2/4 ssa: the sparse set is not needed

In reply to Luc Van Oostenryck

The implementation of a 'sparse set without initialization' was somehow needed during the initial design but it's not needed anymore.

So, remove the implementation and replace its use by the usual bb->generation mechanism.

---
 Makefile |  1 -
 ssa.c    | 11 ++++-------
 sset.c   | 28 ----------------------------
 sset.h   | 56 --------------------------------------------------------
 4 files changed, 4 insertions(+), 92 deletions(-)
 delete mode 100644 sset.c
 delete mode 100644 sset.h
Makefile +0 -1
index f9883da101c7..6ad14375b3cd 100644
@@ -63,7 +63,6 @@ LIB_OBJS += show-parse.o
 LIB_OBJS += simplify.o
 LIB_OBJS += sort.o
 LIB_OBJS += ssa.o
-LIB_OBJS += sset.o
 LIB_OBJS += stats.o
 LIB_OBJS += storage.o
 LIB_OBJS += symbol.o
ssa.c +4 -7
index b9044207db16..3f4fa1a831df 100644
@@ -7,7 +7,6 @@
 #include <assert.h>
 #include "ssa.h"
 #include "lib.h"
-#include "sset.h"
 #include "dominate.h"
 #include "flowgraph.h"
 #include "linearize.h"
@@ -162,13 +161,12 @@ static bool rewrite_single_store(struct instruction *store)
 	return true;
 }
 
-static struct sset *processed;
-
 // we would like to know:
 // is there one or more stores?
 // are all loads & stores local/done in a single block?
 static void ssa_convert_one_var(struct entrypoint *ep, struct symbol *var)
 {
+	unsigned long generation = ++bb_generation;
 	struct basic_block_list *alpha = NULL;
 	struct basic_block_list *idf = NULL;
 	struct basic_block *samebb = NULL;
@@ -199,7 +197,6 @@ static void ssa_convert_one_var(struct entrypoint *ep, struct symbol *var)
 		return;
 
 	// 1) insert in the worklist all BBs that may modify var
-	sset_reset(processed);
 	FOR_EACH_PTR(addr->users, pu) {
 		struct instruction *insn = pu->insn;
 		struct basic_block *bb = insn->bb;
@@ -208,8 +205,10 @@ static void ssa_convert_one_var(struct entrypoint *ep, struct symbol *var)
 		case OP_STORE:
 			nbr_stores++;
 			store = insn;
-			if (!sset_testset(processed, bb->nr))
+			if (bb->generation != generation) {
+				bb->generation = generation;
 				add_bb(&alpha, bb);
+			}
 			/* fall through */
 		case OP_LOAD:
 			if (local) {
@@ -390,8 +389,6 @@ void ssa_convert(struct entrypoint *ep)
 		bb->phi_map = NULL;
 	} END_FOR_EACH_PTR(bb);
 
-	processed = sset_init(first, last);
-
 	// try to promote memory accesses to pseudos
 	FOR_EACH_PTR(ep->accesses, pseudo) {
 		ssa_convert_one_var(ep, pseudo->sym);
sset.c deleted +0 -28
deleted file mode 100644
index e9681e00ddd4..000000000000
@@ -1,28 +0,0 @@
-// SPDX-License-Identifier: MIT
-//
-// sset.c - an all O(1) implementation of sparse sets as presented in:
-//	"An Efficient Representation for Sparse Sets"
-//	by Preston Briggs and Linda Torczon
-//
-// Copyright (C) 2017 - Luc Van Oostenryck
-
-#include "sset.h"
-#include "lib.h"
-#include <stdlib.h>
-
-
-struct sset *sset_init(unsigned int first, unsigned int last)
-{
-	unsigned int size = last - first + 1;
-	struct sset *s = malloc(sizeof(*s) + size * 2 * sizeof(s->sets[0]));
-
-	s->size = size;
-	s->off = first;
-	s->nbr = 0;
-	return s;
-}
-
-void sset_free(struct sset *s)
-{
-	free(s);
-}
sset.h deleted +0 -56

56 lines not on this page · raw message

Signature · 1 line
2.30.0
#3 lore raw

PATCH 3/4 ssa: avoid SSA conversion of packed bitfields

In reply to Luc Van Oostenryck

Packed bitfields are incompatible with the SSA conversion which works on the assumption that memory operations are done on the whole symbol.

So, directly exclude packed bitfields from the SSA conversion.

---
 ssa.c                                | 3 +++
 validation/mem2reg/packed-bitfield.c | 1 -
 2 files changed, 3 insertions(+), 1 deletion(-)
ssa.c +3 -0
index 3f4fa1a831df..26d46baaa16c 100644
@@ -32,6 +32,9 @@ static inline bool is_promotable(struct symbol *type)
 	case SYM_STRUCT:
 		// we allow a single scalar field
 		// but a run of bitfields count for 1
+		// (and packed bifields are excluded).
+		if (type->packed)
+			return 0;
 		nbr = 0;
 		bf_seen = 0;
 		FOR_EACH_PTR(type->symbol_list, member) {
validation/mem2reg/packed-bitfield.c +0 -1
index 4eaf0befeaf5..f3ee259a62b8 100644
@@ -12,7 +12,6 @@ static void foo(struct s s)
 /*
  * check-name: packed-bitfield
  * check-command: test-linearize -fmem2reg $file
- * check-known-to-fail
  *
  * check-output-contains: store.32
  * check-output-contains: load.16
Signature · 1 line
2.30.0
#4 lore raw

PATCH 4/4 ssa: fix conversion with mismatched size or offset

In reply to Luc Van Oostenryck

The SSA conversion works under the assumption that all the memory operations on a given symbol always refer to the same object.

So, exclude the conversion of variables where:
* memory operations do not always match in size or offset
* there is an implicit integer/float conversion.

---
 linearize.h                          |  2 +-
 ssa.c                                | 97 ++++++++++++++++++++++------
 validation/mem2reg/not-same-memop0.c |  1 -
 3 files changed, 80 insertions(+), 20 deletions(-)
linearize.h +1 -1
index 7909b01f29c5..86ae119c82e6 100644
@@ -18,7 +18,7 @@ struct pseudo_user {
 
 DECLARE_ALLOCATOR(pseudo_user);
 DECLARE_PTR_LIST(pseudo_user_list, struct pseudo_user);
-DECLARE_PTRMAP(phi_map, struct symbol *, pseudo_t);
+DECLARE_PTRMAP(phi_map, struct symbol *, struct instruction *);
 
 
 enum pseudo_type {
ssa.c +79 -18

179 lines not on this page · raw message

validation/mem2reg/not-same-memop0.c +0 -1
index 7a84ddce4efc..4de981956b74 100644
@@ -16,7 +16,6 @@ static void foo(struct s s)
 /*
  * check-name: not-same-memop0
  * check-command: test-linearize -Wno-decl -fdump-ir=mem2reg $file
- * check-known-to-fail
  *
  * check-output-start
 local:
Signature · 1 line
2.30.0