All of lore.kernel.org
 help / color / mirror / Atom feed
* [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

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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.