Currently, 64-bit doubleword loads (BPF_DW) unconditionally load the offset into a temporary register with move_imm() and emit ldx.d. Optimize these loads by emitting ld.d for signed 12-bit offsets and ldptr.d for 4-byte aligned signed 16-bit offsets, or falling back to move_imm() + ldx.d when necessary. With this patch, only a single load instruction is emitted in most cases. Signed-off-by: Tiezhu Yang --- This is based on the patch: LoongArch: BPF: Fix the offset of stptr.w and stptr.d stores https://lore.kernel.org/loongarch/20261118071444.10258-1-dongtai.guo@linux.dev/T/#u Note that BPF_W is left unchanged because LoongArch provides ldptr.w (which sign-extends to 64 bits) but lacks an unsigned zero-extending ldptr.wu instruction required by standard 32-bit BPF loads. arch/loongarch/net/bpf_jit.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c index 906f34f420f1..d36891db1179 100644 --- a/arch/loongarch/net/bpf_jit.c +++ b/arch/loongarch/net/bpf_jit.c @@ -1352,8 +1352,14 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext } break; case BPF_DW: - move_imm(ctx, t1, off); - emit_insn(ctx, ldxd, dst, src, t1); + if (is_signed_imm12(off)) { + emit_insn(ctx, ldd, dst, src, off); + } else if (is_aligned_signed_imm16(off)) { + emit_insn(ctx, ldptrd, dst, src, off >> 2); + } else { + move_imm(ctx, t1, off); + emit_insn(ctx, ldxd, dst, src, t1); + } break; } -- 2.42.0