Skip to content

Commit 09dfb71

Browse files
committed
sync
1 parent d454e22 commit 09dfb71

3 files changed

Lines changed: 31 additions & 12 deletions

File tree

include/linux/bpf_ir.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,10 +1185,14 @@ void msan(struct bpf_ir_env *env, struct ir_function *fun, void *param);
11851185
void bpf_ir_div_by_zero(struct bpf_ir_env *env, struct ir_function *fun,
11861186
void *param);
11871187

1188+
void bpf_ir_optimize_code_compaction(struct bpf_ir_env *env,
1189+
struct ir_function *fun, void *param);
1190+
11881191
extern const struct builtin_pass_cfg bpf_ir_kern_add_counter_pass;
11891192
extern const struct builtin_pass_cfg bpf_ir_kern_optimization_pass;
11901193
extern const struct builtin_pass_cfg bpf_ir_kern_msan;
11911194
extern const struct builtin_pass_cfg bpf_ir_kern_div_by_zero_pass;
1195+
extern const struct builtin_pass_cfg bpf_ir_kern_compaction_pass;
11921196

11931197
void translate_throw(struct bpf_ir_env *env, struct ir_function *fun,
11941198
void *param);

kernel/bpf/ir/bpf_ir.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ static const struct function_pass post_passes[] = {
228228
DEF_FUNC_PASS(add_counter, "add_counter", false),
229229
/* CG Preparation Passes */
230230
DEF_NON_OVERRIDE_FUNC_PASS(translate_throw, "translate_throw"),
231+
DEF_NON_OVERRIDE_FUNC_PASS(bpf_ir_optimize_code_compaction,
232+
"optimize_compaction"),
231233
DEF_NON_OVERRIDE_FUNC_PASS(bpf_ir_optimize_ir, "optimize_ir"),
232234
DEF_NON_OVERRIDE_FUNC_PASS(bpf_ir_cg_change_fun_arg, "change_fun_arg"),
233235
DEF_NON_OVERRIDE_FUNC_PASS(bpf_ir_cg_change_call_pre_cg, "change_call"),

kernel/bpf/ir/code_compaction.c

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ void bpf_ir_optimize_code_compaction(struct bpf_ir_env *env,
1515
{
1616
struct ir_basic_block *bb = *pos;
1717
list_for_each_entry(cur_insn, &bb->ir_insn_head, list_ptr) {
18-
if (!prev_insn) {
18+
if (prev_insn == NULL) {
1919
prev_insn = cur_insn;
20+
continue;
2021
}
2122
if (prev_insn->op == IR_INSN_LSH &&
2223
cur_insn->op == IR_INSN_RSH) {
@@ -26,35 +27,47 @@ void bpf_ir_optimize_code_compaction(struct bpf_ir_env *env,
2627
IR_VALUE_CONSTANT) {
2728
continue;
2829
}
30+
if (prev_insn->values[1].data.constant_d !=
31+
32 ||
32+
cur_insn->values[1].data.constant_d != 32) {
33+
continue;
34+
}
2935
if (prev_insn->values[0].type ==
3036
IR_VALUE_INSN &&
3137
cur_insn->values[0].type == IR_VALUE_INSN &&
32-
prev_insn->values[0].data.insn_d ==
38+
prev_insn ==
3339
cur_insn->values[0].data.insn_d) {
3440
// Same!
3541
bpf_ir_array_push(env, &opt_insns,
3642
&cur_insn);
37-
bpf_ir_array_push(env, &opt_insns,
38-
&prev_insn);
3943
}
4044
}
4145
prev_insn = cur_insn;
4246
}
4347
}
48+
PRINT_LOG_DEBUG(env, "Found %d compaction opt\n", opt_insns.num_elem);
4449
if (opt_insns.num_elem == 0) {
4550
return;
4651
}
4752
struct ir_insn **pos2;
4853
array_for(pos2, opt_insns)
4954
{
5055
struct ir_insn *insn = *pos2;
51-
if (insn->op == IR_INSN_RSH) {
52-
// Insert optimized code
53-
struct ir_insn *ni = bpf_ir_create_assign_insn(
54-
env, insn, insn->values[0], INSERT_BACK);
55-
ni->vr_type = IR_VR_TYPE_32;
56-
}
57-
bpf_ir_erase_insn(env, insn);
56+
struct ir_insn *lsh_insn = insn->values[0].data.insn_d;
57+
DBGASSERT(insn->op == IR_INSN_RSH);
58+
DBGASSERT(lsh_insn->op == IR_INSN_LSH);
59+
// Insert optimized code
60+
bpf_ir_change_value(env, insn, &insn->values[0],
61+
lsh_insn->values[0]);
62+
insn->op = IR_INSN_ASSIGN;
63+
insn->vr_type = IR_VR_TYPE_32;
64+
insn->alu_op = IR_ALU_32;
65+
insn->value_num = 1;
66+
insn->raw_pos.valid = false;
67+
bpf_ir_erase_insn(env, lsh_insn);
5868
}
5969
bpf_ir_array_free(&opt_insns);
60-
}
70+
}
71+
72+
const struct builtin_pass_cfg bpf_ir_kern_compaction_pass =
73+
DEF_BUILTIN_PASS_ENABLE_CFG("optimize_compaction", NULL, NULL);

0 commit comments

Comments
 (0)