Skip to content

Commit 8b92d0a

Browse files
committed
feat: update epass
1 parent ff359c0 commit 8b92d0a

5 files changed

Lines changed: 107 additions & 28 deletions

File tree

include/linux/bpf_ir.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,14 @@ struct builtin_pass_cfg {
5050
char name[30];
5151
};
5252

53+
struct bpf_ir_reg_opts {
54+
u8 max_reg_num; // Deafult: 11. 0:9 are normal, 10 is SP
55+
u8 min_callee_reg; // Deafult: 6. BPF_REG_6 to BPF_REG_9 are callee save
56+
};
57+
5358
struct bpf_ir_opts {
54-
u8 debug;
59+
bool debug;
60+
// struct bpf_ir_reg_opts reg_opts;
5561
enum {
5662
BPF_IR_PRINT_BPF,
5763
BPF_IR_PRINT_DETAIL,
@@ -270,6 +276,11 @@ enum ir_alu_op_type {
270276
IR_ALU_64,
271277
};
272278

279+
enum ir_builtin_constant {
280+
IR_BUILTIN_NONE, // Not a builtin constant
281+
IR_BUILTIN_BB_INSN_CNT, // The number of instructions in the basic block (computed during code generation)
282+
};
283+
273284
enum ir_value_type {
274285
IR_VALUE_CONSTANT,
275286
// A constant value in raw operations to be added during code generation
@@ -304,6 +315,7 @@ struct ir_value {
304315
} data;
305316
enum ir_value_type type;
306317
enum ir_alu_op_type const_type; // Used when type is a constant
318+
enum ir_builtin_constant builtin_const;
307319
struct ir_raw_pos raw_pos;
308320
};
309321

kernel/bpf/ir/add_counter_pass.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <linux/bpf_ir.h>
22

3-
#define MAX_RUN_INSN 1000
3+
#define MAX_RUN_INSN 10000
44

55
void add_counter(struct bpf_ir_env *env, struct ir_function *fun)
66
{
@@ -47,18 +47,19 @@ void add_counter(struct bpf_ir_env *env, struct ir_function *fun)
4747
// Skip Non-loop BBs
4848
continue;
4949
}
50-
size_t len = bpf_ir_bb_len(bb);
50+
// size_t len = bpf_ir_bb_len(bb);
5151
struct ir_insn *last = bpf_ir_get_last_insn(bb);
5252
if (!last) {
5353
// No insn in the bb
5454
continue;
5555
}
5656
struct ir_insn *load_insn = bpf_ir_create_load_insn(
5757
env, last, bpf_ir_value_insn(alloc_insn), INSERT_FRONT);
58+
struct ir_value cv = bpf_ir_value_const32(-1);
59+
cv.builtin_const = IR_BUILTIN_BB_INSN_CNT;
5860
struct ir_insn *added = bpf_ir_create_bin_insn(
59-
env, load_insn, bpf_ir_value_insn(load_insn),
60-
bpf_ir_value_const32(len), IR_INSN_ADD, IR_ALU_64,
61-
INSERT_BACK);
61+
env, load_insn, bpf_ir_value_insn(load_insn), cv,
62+
IR_INSN_ADD, IR_ALU_64, INSERT_BACK);
6263
struct ir_insn *store_back = bpf_ir_create_store_insn(
6364
env, added, alloc_insn, bpf_ir_value_insn(added),
6465
INSERT_BACK);

kernel/bpf/ir/ir_code_gen.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2667,6 +2667,49 @@ static void translate_cond_jmp(struct ir_insn *insn)
26672667
}
26682668
}
26692669

2670+
static u32 bb_insn_cnt(struct ir_basic_block *bb)
2671+
{
2672+
u32 cnt = 0;
2673+
struct ir_insn *insn, *tmp;
2674+
list_for_each_entry_safe(insn, tmp, &bb->ir_insn_head, list_ptr) {
2675+
if (insn->op == IR_INSN_ALLOC ||
2676+
insn->op == IR_INSN_ALLOCARRAY) {
2677+
continue;
2678+
} else {
2679+
cnt++;
2680+
}
2681+
}
2682+
return cnt;
2683+
}
2684+
2685+
static void replace_builtin_const(struct bpf_ir_env *env,
2686+
struct ir_function *fun)
2687+
{
2688+
struct ir_basic_block **pos;
2689+
array_for(pos, fun->reachable_bbs)
2690+
{
2691+
struct ir_basic_block *bb = *pos;
2692+
struct ir_insn *insn, *tmp;
2693+
list_for_each_entry_safe(insn, tmp, &bb->ir_insn_head,
2694+
list_ptr) {
2695+
struct array operands = bpf_ir_get_operands(env, insn);
2696+
struct ir_value **val;
2697+
array_for(val, operands)
2698+
{
2699+
struct ir_value *v = *val;
2700+
if (v->type == IR_VALUE_CONSTANT) {
2701+
if (v->builtin_const ==
2702+
IR_BUILTIN_BB_INSN_CNT) {
2703+
v->data.constant_d =
2704+
bb_insn_cnt(bb);
2705+
}
2706+
}
2707+
}
2708+
bpf_ir_array_free(&operands);
2709+
}
2710+
}
2711+
}
2712+
26702713
static void translate(struct bpf_ir_env *env, struct ir_function *fun)
26712714
{
26722715
struct ir_basic_block **pos;
@@ -2912,6 +2955,9 @@ void bpf_ir_code_gen(struct bpf_ir_env *env, struct ir_function *fun)
29122955
prog_check_cg(env, fun);
29132956
CHECK_ERR();
29142957

2958+
replace_builtin_const(env, fun);
2959+
CHECK_ERR();
2960+
29152961
// Step 13: Direct Translation
29162962
translate(env, fun);
29172963
CHECK_ERR();

kernel/bpf/ir/ir_helper.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,23 @@ void print_bb_ptr(struct bpf_ir_env *env, struct ir_basic_block *insn)
100100

101101
static void print_const(struct bpf_ir_env *env, struct ir_value v)
102102
{
103-
if (v.const_type == IR_ALU_64) {
104-
PRINT_LOG(env, "0x%llx", v.data.constant_d);
105-
PRINT_LOG(env, "(64)");
106-
} else if (v.const_type == IR_ALU_32) {
107-
PRINT_LOG(env, "0x%x", v.data.constant_d & 0xFFFFFFFF);
108-
PRINT_LOG(env, "(32)");
103+
if (v.builtin_const == IR_BUILTIN_NONE) {
104+
if (v.const_type == IR_ALU_64) {
105+
PRINT_LOG(env, "0x%llx", v.data.constant_d);
106+
PRINT_LOG(env, "(64)");
107+
} else if (v.const_type == IR_ALU_32) {
108+
PRINT_LOG(env, "0x%x", v.data.constant_d & 0xFFFFFFFF);
109+
PRINT_LOG(env, "(32)");
110+
} else {
111+
PRINT_LOG(env, "(unknown)");
112+
env->err = -1;
113+
return;
114+
}
109115
} else {
110-
PRINT_LOG(env, "(unknown)");
111-
env->err = -1;
112-
return;
116+
// Builtin constant
117+
if (v.builtin_const == IR_BUILTIN_BB_INSN_CNT) {
118+
PRINT_LOG(env, "__BB_INSN_CNT__");
119+
}
113120
}
114121
}
115122

kernel/bpf/ir/ir_value.c

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,46 @@ bool bpf_ir_value_equal(struct ir_value a, struct ir_value b)
1414
CRITICAL("Error");
1515
}
1616

17+
static struct ir_value value_base(void)
18+
{
19+
// Create a new value
20+
return (struct ir_value){ .type = IR_VALUE_UNDEF,
21+
.raw_pos = { .valid = false },
22+
.const_type = IR_ALU_UNKNOWN,
23+
.builtin_const = IR_BUILTIN_NONE };
24+
}
25+
1726
struct ir_value bpf_ir_value_insn(struct ir_insn *insn)
1827
{
19-
return (struct ir_value){ .type = IR_VALUE_INSN,
20-
.data.insn_d = insn,
21-
.raw_pos = { .valid = false } };
28+
struct ir_value v = value_base();
29+
v.type = IR_VALUE_INSN;
30+
v.data.insn_d = insn;
31+
return v;
2232
}
2333

2434
struct ir_value bpf_ir_value_undef(void)
2535
{
26-
return (struct ir_value){ .type = IR_VALUE_UNDEF,
27-
.raw_pos = { .valid = false } };
36+
struct ir_value v = value_base();
37+
v.type = IR_VALUE_UNDEF;
38+
return v;
2839
}
2940

3041
struct ir_value bpf_ir_value_const32(s32 val)
3142
{
32-
return (struct ir_value){ .type = IR_VALUE_CONSTANT,
33-
.data.constant_d = val,
34-
.const_type = IR_ALU_32,
35-
.raw_pos = { .valid = false } };
43+
struct ir_value v = value_base();
44+
v.type = IR_VALUE_CONSTANT;
45+
v.data.constant_d = val;
46+
v.const_type = IR_ALU_32;
47+
return v;
3648
}
3749

3850
struct ir_value bpf_ir_value_const64(s64 val)
3951
{
40-
return (struct ir_value){ .type = IR_VALUE_CONSTANT,
41-
.data.constant_d = val,
42-
.const_type = IR_ALU_64,
43-
.raw_pos = { .valid = false } };
52+
struct ir_value v = value_base();
53+
v.type = IR_VALUE_CONSTANT;
54+
v.data.constant_d = val;
55+
v.const_type = IR_ALU_64;
56+
return v;
4457
}
4558

4659
struct ir_address_value bpf_ir_addr_val(struct ir_value value, s16 offset)

0 commit comments

Comments
 (0)