* [PATCH] LoongArch: BPF: Optimize doubleword loads with ld.d and ldptr.d
@ 2026-11-18 8:12 Tiezhu Yang
2026-11-18 8:22 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Tiezhu Yang @ 2026-11-18 8:12 UTC (permalink / raw)
To: Huacai Chen, Hengqi Chen; +Cc: loongarch, bpf, linux-kernel
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 <yangtiezhu@loongson.cn>
---
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
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] LoongArch: BPF: Optimize doubleword loads with ld.d and ldptr.d
2026-11-18 8:12 [PATCH] LoongArch: BPF: Optimize doubleword loads with ld.d and ldptr.d Tiezhu Yang
@ 2026-11-18 8:22 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-18 8:22 UTC (permalink / raw)
To: Tiezhu Yang; +Cc: bpf
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] loongarch: bpf: call to undeclared is_aligned_signed_imm16() in
build_insn()
--
commit 6d9b52d1b981a940a37fd81d368c20f9bfdc04fb
Author: Tiezhu Yang <yangtiezhu@loongson.cn>
LoongArch: BPF: Optimize doubleword loads with ld.d and ldptr.d
This commit optimizes 64-bit BPF doubleword loads in the LoongArch JIT by
emitting ld.d or ldptr.d when the offset fits in a signed 12-bit or aligned
signed 16-bit immediate.
> diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c
> index 15af1abb5cb69..15f40b570b9af 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);
[Severity: High]
Does this break the build when CONFIG_BPF_JIT is enabled because
is_aligned_signed_imm16() is not defined in arch/loongarch/net/bpf_jit.h?
The prerequisite commit "LoongArch: BPF: Fix the offset of stptr.w and
stptr.d stores" noted below the --- separator is not applied in this tree.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118081201.10498-1-yangtiezhu@loongson.cn?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-18 8:22 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18 8:12 [PATCH] LoongArch: BPF: Optimize doubleword loads with ld.d and ldptr.d Tiezhu Yang
2026-11-18 8:22 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox