Skip to contentSkip to conversation

output *memory* operands need their address as *input*

PATCH New Luc Van Oostenryck 1 participant 4 messages Feb 21, 2021 Sparse checker

lore
Share & tools
lore
#0 lore raw

cover output *memory* operands need their address as *input*

These patches fix a problem with the linearization of asm statements with output memory operands. These operands need to have their address generated before the asm instruction but was generated after it.

Note: the problem wasn't visible when the address was a symbol
because the OP_SYMADDR producing the address is simplified away at some later stage with the symbol directly handled by the memory operation.

Luc Van Oostenryck (3):
asm: add testcase for problem with output addresses
asm: factor out add_asm_rule() from add_asm_{in,out}put()
asm: output *memory* operands need their address as *input*

 linearize.c                  | 54 +++++++++++++++++++++++-------------
 validation/linear/asm-out0.c | 25 +++++++++++++++++
 2 files changed, 60 insertions(+), 19 deletions(-)
 create mode 100644 validation/linear/asm-out0.c

base-commit: 60c1f2706e30eacc29296e6cb5d9327c85a01340

Signature · 1 line
2.30.0
#1 lore raw

PATCH 1/3 asm: add testcase for problem with output addresses

The addresses needed by memory output operands are linearized (and placed) after the ASM instruction needing them.

So, add a test case for this.

---
 validation/linear/asm-out0.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
 create mode 100644 validation/linear/asm-out0.c
validation/linear/asm-out0.c new file +26 -0
new file mode 100644
index 000000000000..64d154ed5ad7
@@ -0,0 +1,26 @@
+static void asm_out0(void)
+{
+	int mem;
+	asm volatile ("[%1] <= 0" : "=m" (mem));
+}
+
+/*
+ * check-name: asm-out0
+ * check-command: test-linearize -fdump-ir $file
+ * check-known-to-fail
+ *
+ * check-output-start
+asm_out0:
+.L0:
+	<entry-point>
+	symaddr.64  %r1 <- mem
+	asm         "[%1] <= 0"
+		out: "=m" (%r1)
+	br          .L1
+
+.L1:
+	ret
+
+
+ * check-output-end
+ */
Signature · 1 line
2.30.0
#2 lore raw

PATCH 2/3 asm: factor out add_asm_rule() from add_asm_{in,out}put()

In reply to Luc Van Oostenryck

The functions add_asm_input() and add_asm_output() are very similar. So, factorize out the common part.

---
 linearize.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
linearize.c +12 -11
index 7a6f745fd4fc..6efa47492869 100644
@@ -2127,22 +2127,27 @@ static pseudo_t linearize_range(struct entrypoint *ep, struct statement *stmt)
 ALLOCATOR(asm_rules, "asm rules");
 ALLOCATOR(asm_constraint, "asm constraints");
 
-static void add_asm_input(struct entrypoint *ep, struct instruction *insn, struct asm_operand *op)
+static void add_asm_rule(struct instruction *insn, struct asm_constraint_list **list, struct asm_operand *op, pseudo_t pseudo)
 {
-	pseudo_t pseudo = linearize_expression(ep, op->expr);
 	struct asm_constraint *rule = __alloc_asm_constraint(0);
-
+	rule->is_memory = op->is_memory;
 	rule->ident = op->name;
 	rule->constraint = op->constraint ? op->constraint->string->data : "";
 	use_pseudo(insn, pseudo, &rule->pseudo);
-	add_ptr_list(&insn->asm_rules->inputs, rule);
+	add_ptr_list(list, rule);
+}
+
+static void add_asm_input(struct entrypoint *ep, struct instruction *insn, struct asm_operand *op)
+{
+	pseudo_t pseudo = linearize_expression(ep, op->expr);
+
+	add_asm_rule(insn, &insn->asm_rules->inputs, op, pseudo);
 }
 
 static void add_asm_output(struct entrypoint *ep, struct instruction *insn, struct asm_operand *op)
 {
 	struct access_data ad = { NULL, };
 	pseudo_t pseudo;
-	struct asm_constraint *rule;
 
 	if (op->is_memory) {
 		pseudo = linearize_expression(ep, op->expr);
@@ -2152,12 +2157,8 @@ static void add_asm_output(struct entrypoint *ep, struct instruction *insn, stru
 		pseudo = alloc_pseudo(insn);
 		linearize_store_gen(ep, pseudo, &ad);
 	}
-	rule = __alloc_asm_constraint(0);
-	rule->is_memory = op->is_memory;
-	rule->ident = op->name;
-	rule->constraint = op->constraint ? op->constraint->string->data : "";
-	use_pseudo(insn, pseudo, &rule->pseudo);
-	add_ptr_list(&insn->asm_rules->outputs, rule);
+
+	add_asm_rule(insn, &insn->asm_rules->outputs, op, pseudo);
 }
 
 static pseudo_t linearize_asm_statement(struct entrypoint *ep, struct statement *stmt)
Signature · 1 line
2.30.0
#3 lore raw

PATCH 3/3 asm: output *memory* operands need their address as *input*

In reply to Luc Van Oostenryck

The addresses needed by memory output operands are linearized (and placed) after the ASM instruction needing them.

So, split add_asm_output() in 2 parts: one generating only the addresses for memory operands and called before issuing the body, and another one doing the usual copy of (non-memory) output operands back into their corresponding variables.

---
 linearize.c                  | 31 +++++++++++++++++++++++--------
 validation/linear/asm-out0.c |  1 -
 2 files changed, 23 insertions(+), 9 deletions(-)
linearize.c +23 -8
index 6efa47492869..33d641b40de6 100644
@@ -2144,19 +2144,29 @@ static void add_asm_input(struct entrypoint *ep, struct instruction *insn, struc
 	add_asm_rule(insn, &insn->asm_rules->inputs, op, pseudo);
 }
 
+static void add_asm_output_address(struct entrypoint *ep, struct instruction *insn, struct asm_operand *op)
+{
+	pseudo_t pseudo;
+
+	if (!op->is_memory)
+		return;
+
+	pseudo = linearize_expression(ep, op->expr);
+	add_asm_rule(insn, &insn->asm_rules->outputs, op, pseudo);
+}
+
 static void add_asm_output(struct entrypoint *ep, struct instruction *insn, struct asm_operand *op)
 {
 	struct access_data ad = { NULL, };
 	pseudo_t pseudo;
 
-	if (op->is_memory) {
-		pseudo = linearize_expression(ep, op->expr);
-	} else {
-		if (!linearize_address_gen(ep, op->expr, &ad))
-			return;
-		pseudo = alloc_pseudo(insn);
-		linearize_store_gen(ep, pseudo, &ad);
-	}
+	if (op->is_memory)
+		return;
+
+	if (!linearize_address_gen(ep, op->expr, &ad))
+		return;
+	pseudo = alloc_pseudo(insn);
+	linearize_store_gen(ep, pseudo, &ad);
 
 	add_asm_rule(insn, &insn->asm_rules->outputs, op, pseudo);
 }
@@ -2184,6 +2194,11 @@ static pseudo_t linearize_asm_statement(struct entrypoint *ep, struct statement
 		add_asm_input(ep, insn, op);
 	} END_FOR_EACH_PTR(op);
 
+	/* ... and the addresses for memory outputs */
+	FOR_EACH_PTR(stmt->asm_outputs, op) {
+		add_asm_output_address(ep, insn, op);
+	} END_FOR_EACH_PTR(op);
+
 	add_one_insn(ep, insn);
 
 	/* Assign the outputs */
validation/linear/asm-out0.c +0 -1
index 64d154ed5ad7..a8e0be693d87 100644
@@ -7,7 +7,6 @@ static void asm_out0(void)
 /*
  * check-name: asm-out0
  * check-command: test-linearize -fdump-ir $file
- * check-known-to-fail
  *
  * check-output-start
 asm_out0:
Signature · 1 line
2.30.0