Blame SOURCES/rhbz1643997.0021-bpf-behind-the-scenes-useful-DEBUG_CODEGEN-diagnosti.patch

132810
From 31ce3fd0cd21e1a808c417f813b3b5285aeb938a Mon Sep 17 00:00:00 2001
132810
From: Serhei Makarov <smakarov@redhat.com>
132810
Date: Tue, 6 Nov 2018 12:32:38 -0500
132810
Subject: [PATCH 21/32] bpf behind-the-scenes :: useful DEBUG_CODEGEN
132810
 diagnostic
132810
132810
---
132810
 bpf-base.cxx      | 10 ++++++++++
132810
 bpf-internal.h    | 21 +++++++++++++++++++++
132810
 bpf-opt.cxx       | 28 +++++++++++++++++-----------
132810
 bpf-translate.cxx | 26 ++++++++++++++++++++++++--
132810
 4 files changed, 72 insertions(+), 13 deletions(-)
132810
132810
diff --git a/bpf-base.cxx b/bpf-base.cxx
132810
index 24b610cf3..5d132bcd1 100644
132810
--- a/bpf-base.cxx
132810
+++ b/bpf-base.cxx
132810
@@ -368,6 +368,10 @@ opcode_name(opcode op)
132810
 std::ostream &
132810
 insn::print(std::ostream &o) const
132810
 {
132810
+#ifdef DEBUG_CODEGEN
132810
+  if (note != "")
132810
+    o << "{" << note << "} ";
132810
+#endif
132810
   const char *opn = opcode_name (code);
132810
 
132810
   switch (code)
132810
@@ -541,6 +545,12 @@ insn *
132810
 insn_inserter::new_insn()
132810
 {
132810
   insn *n = new insn;
132810
+#ifdef DEBUG_CODEGEN
132810
+  if (!notes.empty())
132810
+    n->note = notes.top();
132810
+  else
132810
+    n->note = "";
132810
+#endif
132810
   insert(n);
132810
   return n;
132810
 }
132810
diff --git a/bpf-internal.h b/bpf-internal.h
132810
index 82cba2c79..75fefb769 100644
132810
--- a/bpf-internal.h
132810
+++ b/bpf-internal.h
132810
@@ -48,6 +48,8 @@ namespace bpf {
132810
 // TODO: BPF_MAX{STRING,FORMAT}LEN,BPF_MAXMAPENTRIES,BPF_MAXSPRINTFLEN should be user-configurable.
132810
 // XXX: BPF_MAXMAPENTRIES may depend on kernel version. May need to experiment with rlimit in instantiate_maps().
132810
 
132810
+// #define DEBUG_CODEGEN
132810
+
132810
 typedef unsigned short regno;
132810
 static const regno max_regno = BPF_MAXINSNS;
132810
 static const regno noreg = -1;
132810
@@ -135,6 +137,9 @@ struct insn
132810
   value *src0;			// The destination input, pre-allocation
132810
   value *src1;			// The usual source register operand
132810
   insn *prev, *next;		// Linked list of insns in the block
132810
+#ifdef DEBUG_CODEGEN
132810
+  std::string note;             // For additional diagnostics.
132810
+#endif
132810
 
132810
   insn();
132810
 
132810
@@ -198,8 +203,18 @@ private:
132810
 public:
132810
   block *b;
132810
   insn *i;
132810
+#ifdef DEBUG_CODEGEN
132810
+  std::stack<std::string> notes;
132810
+#endif
132810
 
132810
   insn_inserter(block *bb, insn *ii) : b(bb), i(ii) { }
132810
+  insn_inserter(block *bb, insn *ii, const std::string& note) : b(bb), i(ii) {
132810
+#ifdef DEBUG_CODEGEN
132810
+    notes.push(note);
132810
+#else
132810
+    (void)note; // unused
132810
+#endif
132810
+  }
132810
   virtual ~insn_inserter() { }
132810
   virtual void insert(insn *i) = 0;
132810
 
132810
@@ -214,6 +229,8 @@ struct insn_before_inserter : public insn_inserter
132810
 {
132810
   insn_before_inserter() : insn_inserter(NULL, NULL) { }
132810
   insn_before_inserter(block *b, insn *i) : insn_inserter(b,i) { }
132810
+  insn_before_inserter(block *b, insn *i, const std::string& note)
132810
+    : insn_inserter(b,i,note) { }
132810
   virtual void insert(insn *i);
132810
 };
132810
 
132810
@@ -221,6 +238,8 @@ struct insn_after_inserter : public insn_inserter
132810
 {
132810
   insn_after_inserter() : insn_inserter(NULL, NULL) { }
132810
   insn_after_inserter(block *b, insn *i) : insn_inserter(b,i) { }
132810
+  insn_after_inserter(block *b, insn *i, const std::string& note)
132810
+    : insn_inserter(b,i,note) { }
132810
   virtual void insert(insn *i);
132810
 };
132810
 
132810
@@ -228,6 +247,8 @@ struct insn_append_inserter : public insn_after_inserter
132810
 {
132810
   insn_append_inserter() : insn_after_inserter(NULL, NULL) { }
132810
   insn_append_inserter(block *b) : insn_after_inserter(b, NULL) { }
132810
+  insn_append_inserter(block *b, const std::string& note)
132810
+    : insn_after_inserter(b, NULL, note) { }
132810
 };
132810
 
132810
 struct program
132810
diff --git a/bpf-opt.cxx b/bpf-opt.cxx
132810
index c2e30a690..904b33b46 100644
132810
--- a/bpf-opt.cxx
132810
+++ b/bpf-opt.cxx
132810
@@ -59,7 +59,7 @@ lower_str_values(program &p)
132810
           value *s0 = j->src0;
132810
           if (s0 && s0->is_str())
132810
             {
132810
-              insn_before_inserter ins(b, j);
132810
+              insn_before_inserter ins(b, j, "str");
132810
               std::string str0 = s0->str();
132810
               value *new_s0 = alloc_literal_str(p, ins, str0);
132810
               j->src0 = new_s0;
132810
@@ -68,7 +68,7 @@ lower_str_values(program &p)
132810
           value *s1 = j->src1;
132810
           if (s1 && s1->is_str())
132810
             {
132810
-              insn_before_inserter ins(b, j);
132810
+              insn_before_inserter ins(b, j, "str");
132810
               std::string str1 = s1->str();
132810
               value *new_s1 = alloc_literal_str(p, ins, str1);
132810
               j->src1 = new_s1;
132810
@@ -97,7 +97,7 @@ fixup_operands(program &p)
132810
 	  if (s1 && s1->is_imm() && s1->imm() != (int32_t)s1->imm())
132810
 	    {
132810
 	      value *n = p.new_reg();
132810
-	      insn_before_inserter ins(b, j);
132810
+	      insn_before_inserter ins(b, j, "opt");
132810
 	      p.mk_mov(ins, n, s1);
132810
 	      j->src1 = s1 = n;
132810
 	    }
132810
@@ -121,13 +121,13 @@ fixup_operands(program &p)
132810
 			  // Special care for x = y - x
132810
 			  value *n = p.new_reg();
132810
 			  {
132810
-			    insn_before_inserter ins(b, j);
132810
+			    insn_before_inserter ins(b, j, "opt");
132810
 			    p.mk_mov(ins, n, s0);
132810
 			  }
132810
 			  j->src0 = n;
132810
 			  j->dest = n;
132810
 			  {
132810
-			    insn_after_inserter ins(b, j);
132810
+			    insn_after_inserter ins(b, j, "opt");
132810
 			    p.mk_mov(ins, d, n);
132810
 			  }
132810
 			}
132810
@@ -135,7 +135,7 @@ fixup_operands(program &p)
132810
 		  else
132810
 		    {
132810
 		      // Transform { x = y - z } to { x = y; x -= z; }
132810
-		      insn_before_inserter ins(b, j);
132810
+		      insn_before_inserter ins(b, j, "opt");
132810
 		      p.mk_mov(ins, d, s0);
132810
 		      j->src0 = d;
132810
 		    }
132810
@@ -144,7 +144,7 @@ fixup_operands(program &p)
132810
 		{
132810
 		  // Comparisons can't have src0 constant.
132810
 		  value *n = p.new_reg();
132810
-		  insn_before_inserter ins(b, j);
132810
+		  insn_before_inserter ins(b, j, "opt");
132810
 		  p.mk_mov(ins, n, s0);
132810
 		  j->src0 = n;
132810
 		}
132810
@@ -293,7 +293,7 @@ reorder_blocks(program &p)
132810
 	      if (t)
132810
 		{
132810
 		  block *n = p.new_block ();
132810
-		  insn_append_inserter ins(n);
132810
+		  insn_append_inserter ins(n, "opt");
132810
 		  p.mk_jmp (ins, o);
132810
 		  ordered.push_back (n);
132810
 		  f->redirect_next (n);
132810
@@ -301,7 +301,7 @@ reorder_blocks(program &p)
132810
 	      else
132810
 		{
132810
 		  delete f;
132810
-		  insn_after_inserter ins(b, b->last);
132810
+		  insn_after_inserter ins(b, b->last, "opt");
132810
 		  p.mk_jmp (ins, o);
132810
 		}
132810
 	    }
132810
@@ -780,7 +780,7 @@ spill(unsigned reg, unsigned num_spills, program &p)
132810
           // If reg is a source, insert a load before j
132810
           if ((src0 && src0->reg_val == reg) || (src1 && src1->reg_val == reg))
132810
             {
132810
-              insn_before_inserter ins(b, j);
132810
+              insn_before_inserter ins(b, j, "regalloc");
132810
               new_tmp = p.new_reg();
132810
 
132810
               p.mk_ld (ins, BPF_DW, new_tmp, frame, -off);
132810
@@ -795,7 +795,7 @@ spill(unsigned reg, unsigned num_spills, program &p)
132810
           // If reg is the destination, insert a store after j
132810
           if (dest && dest->reg_val == reg)
132810
             {
132810
-              insn_after_inserter ins(b, j);
132810
+              insn_after_inserter ins(b, j, "regalloc");
132810
               new_tmp = new_tmp ?: p.new_reg();
132810
 
132810
               p.mk_st (ins, BPF_DW, frame, -off, new_tmp);
132810
@@ -935,6 +935,9 @@ post_alloc_cleanup (program &p)
132810
 void
132810
 program::generate()
132810
 {
132810
+#ifdef DEBUG_CODEGEN
132810
+  std::cerr << "DEBUG BEFORE OPT " << *this << std::endl;
132810
+#endif
132810
   lower_str_values(*this);
132810
   fixup_operands(*this);
132810
   thread_jumps(*this);
132810
@@ -942,6 +945,9 @@ program::generate()
132810
   reorder_blocks(*this);
132810
   reg_alloc(*this);
132810
   post_alloc_cleanup(*this);
132810
+#ifdef DEBUG_CODEGEN
132810
+  std::cerr << "DEBUG AFTER OPT " << *this << std::endl;
132810
+#endif
132810
 }
132810
 
132810
 } // namespace bpf
132810
diff --git a/bpf-translate.cxx b/bpf-translate.cxx
132810
index d46dae44a..57a4cb107 100644
132810
--- a/bpf-translate.cxx
132810
+++ b/bpf-translate.cxx
132810
@@ -310,7 +310,7 @@ bpf_unparser::get_exit_block()
132810
     return exit_block;
132810
 
132810
   block *b = this_prog.new_block();
132810
-  insn_append_inserter ins(b);
132810
+  insn_append_inserter ins(b, "exit_block");
132810
 
132810
   this_prog.mk_exit(ins);
132810
 
132810
@@ -325,7 +325,7 @@ bpf_unparser::get_ret0_block()
132810
     return ret0_block;
132810
 
132810
   block *b = this_prog.new_block();
132810
-  insn_append_inserter ins(b);
132810
+  insn_append_inserter ins(b, "ret0_block");
132810
 
132810
   this_prog.mk_mov(ins, this_prog.lookup_reg(BPF_REG_0), this_prog.new_imm(0));
132810
   b->fallthru = new edge(b, get_exit_block());
132810
@@ -1166,6 +1166,9 @@ bpf_unparser::emit_asm_opcode (const asm_stmt &stmt,
132810
 void
132810
 bpf_unparser::visit_embeddedcode (embeddedcode *s)
132810
 {
132810
+#ifdef DEBUG_CODEGEN
132810
+  this_ins.notes.push("asm");
132810
+#endif
132810
   std::vector<asm_stmt> statements;
132810
   asm_stmt stmt;
132810
 
132810
@@ -1429,6 +1432,10 @@ bpf_unparser::visit_embeddedcode (embeddedcode *s)
132810
        it != adjusted_toks.end(); it++)
132810
     delete *it;
132810
   adjusted_toks.clear();
132810
+
132810
+#ifdef DEBUG_CODEGEN
132810
+  this_ins.notes.pop(); // asm
132810
+#endif
132810
 }
132810
 
132810
 void
132810
@@ -2507,6 +2514,10 @@ value *
132810
 emit_simple_literal_str(program &this_prog, insn_inserter &this_ins,
132810
                  value *dest, int ofs, std::string &src, bool zero_pad)
132810
 {
132810
+#ifdef DEBUG_CODEGEN
132810
+  this_ins.notes.push("str");
132810
+#endif
132810
+
132810
   size_t str_bytes = src.size() + 1;
132810
   size_t str_words = (str_bytes + 3) / 4;
132810
 
132810
@@ -2546,6 +2557,10 @@ emit_simple_literal_str(program &this_prog, insn_inserter &this_ins,
132810
   value *out = this_prog.new_reg();
132810
   this_prog.mk_binary(this_ins, BPF_ADD, out,
132810
                       dest, this_prog.new_imm(ofs));
132810
+
132810
+#ifdef DEBUG_CODEGEN
132810
+  this_ins.notes.pop(); // str
132810
+#endif
132810
   return out;
132810
 }
132810
 
132810
@@ -2567,6 +2582,10 @@ bpf_unparser::emit_string_copy(value *dest, int ofs, value *src, bool zero_pad)
132810
                                      dest, ofs, str, zero_pad);
132810
     }
132810
 
132810
+#ifdef DEBUG_CODEGEN
132810
+  this_ins.notes.push("strcpy");
132810
+#endif
132810
+
132810
   size_t str_bytes = BPF_MAXSTRINGLEN;
132810
   size_t str_words = (str_bytes + 3) / 4;
132810
 
132810
@@ -2674,6 +2693,9 @@ bpf_unparser::emit_string_copy(value *dest, int ofs, value *src, bool zero_pad)
132810
   value *out = this_prog.new_reg();
132810
   this_prog.mk_binary(this_ins, BPF_ADD, out,
132810
                       dest, this_prog.new_imm(ofs));
132810
+#ifdef DEBUG_CODEGEN
132810
+  this_ins.notes.pop(); // strcpy
132810
+#endif
132810
   return out;
132810
 }
132810
 
132810
-- 
132810
2.14.5
132810