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