[clang] [llvm] [BPF] Add support for may_goto insn (PR #85358)

2024-03-15 Thread via cfe-commits

https://github.com/yonghong-song closed 
https://github.com/llvm/llvm-project/pull/85358
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [BPF] Add support for may_goto insn (PR #85358)

2024-03-15 Thread via cfe-commits

https://github.com/eddyz87 approved this pull request.


https://github.com/llvm/llvm-project/pull/85358
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [BPF] Add support for may_goto insn (PR #85358)

2024-03-14 Thread via cfe-commits

https://github.com/4ast approved this pull request.

lgtm

https://github.com/llvm/llvm-project/pull/85358
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [BPF] Add support for may_goto insn (PR #85358)

2024-03-14 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: None (yonghong-song)


Changes

Alexei added may_goto insn in [1]. The asm syntax for may_goto looks like
  may_goto 

[clang] [llvm] [BPF] Add support for may_goto insn (PR #85358)

2024-03-14 Thread via cfe-commits

yonghong-song wrote:

cc @anakryiko @jemarch

https://github.com/llvm/llvm-project/pull/85358
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [BPF] Add support for may_goto insn (PR #85358)

2024-03-14 Thread via cfe-commits

https://github.com/yonghong-song created 
https://github.com/llvm/llvm-project/pull/85358

Alexei added may_goto insn in [1]. The asm syntax for may_goto looks like
  may_goto 

The instruction represents a conditional branch but the condition is implicit. 
Later in bpf kernel verifier, the 'may_goto ' insn will be rewritten 
with an explicit condition. The encoding of 'may_goto' insn is enforced in [2] 
and is also implemented in this patch.

In [3], 'may_goto' insn is encoded with raw bytes. I made the following change
```
  --- a/tools/testing/selftests/bpf/bpf_experimental.h
  +++ b/tools/testing/selftests/bpf/bpf_experimental.h
  @@ -328,10 +328,7 @@ l_true:  
  \

   #define cond_break \
  ({ __label__ l_break, l_continue;   \
  -asm volatile goto("1:.byte 0xe5;   \
  - .byte 0;  \
  - .long ((%l[l_break] - 1b - 8) / 8) & 0x;  \
  - .short 0" \
  +asm volatile goto("may_goto %l[l_break]"   \
 l_break);\
  goto l_continue;\
  l_break: break;
```
and ran the selftest with the latest llvm with this patch. All tests are passed.

  [1] 
https://lore.kernel.org/bpf/20240306031929.42666-1-alexei.starovoi...@gmail.com/
  [2] 
https://lore.kernel.org/bpf/20240306031929.42666-2-alexei.starovoi...@gmail.com/
  [3] 
https://lore.kernel.org/bpf/20240306031929.42666-4-alexei.starovoi...@gmail.com/

>From 98e0602539ecc28055e55f2f9ce1c3f7f9e8ddb2 Mon Sep 17 00:00:00 2001
From: Yonghong Song 
Date: Thu, 14 Mar 2024 19:53:35 -0700
Subject: [PATCH] [BPF] Add support for may_goto insn

Alexei added may_goto insn in [1]. The asm syntax for may_goto looks like
  may_goto 

The instruction represents a conditional branch but the condition is implicit.
Later in bpf kernel verifier, the 'may_goto ' insn will be rewritten
with an explicit condition. The encoding of 'may_goto' insn is enforced
in [2] and is also implemented in this patch.

In [3], 'may_goto' insn is encoded with raw bytes. I made the following change
  --- a/tools/testing/selftests/bpf/bpf_experimental.h
  +++ b/tools/testing/selftests/bpf/bpf_experimental.h
  @@ -328,10 +328,7 @@ l_true:  
  \

   #define cond_break \
  ({ __label__ l_break, l_continue;   \
  -asm volatile goto("1:.byte 0xe5;   \
  - .byte 0;  \
  - .long ((%l[l_break] - 1b - 8) / 8) & 0x;  \
  - .short 0" \
  +asm volatile goto("may_goto %l[l_break]"   \
 l_break);\
  goto l_continue;\
  l_break: break;
and ran the selftest with the latest llvm with this patch. All tests are passed.

  [1] 
https://lore.kernel.org/bpf/20240306031929.42666-1-alexei.starovoi...@gmail.com/
  [2] 
https://lore.kernel.org/bpf/20240306031929.42666-2-alexei.starovoi...@gmail.com/
  [3] 
https://lore.kernel.org/bpf/20240306031929.42666-4-alexei.starovoi...@gmail.com/

Signed-off-by: Yonghong Song 
---
 clang/lib/Basic/Targets/BPF.cpp |  1 +
 llvm/lib/Target/BPF/AsmParser/BPFAsmParser.cpp  |  1 +
 llvm/lib/Target/BPF/BPFInstrFormats.td  |  1 +
 llvm/lib/Target/BPF/BPFInstrInfo.td | 13 +
 .../lib/Target/BPF/MCTargetDesc/BPFMCTargetDesc.cpp |  5 -
 llvm/test/MC/BPF/insn-unit.s|  2 ++
 6 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Basic/Targets/BPF.cpp b/clang/lib/Basic/Targets/BPF.cpp
index a90398a362d10a..b5ba11a3bdca9d 100644
--- a/clang/lib/Basic/Targets/BPF.cpp
+++ b/clang/lib/Basic/Targets/BPF.cpp
@@ -45,6 +45,7 @@ void BPFTargetInfo::getTargetDefines(const LangOptions &Opts,
 
   std::string CpuVerNumStr = CPU.substr(1);
   Builder.defineMacro("__BPF_CPU_VERSION__", CpuVerNumStr);
+  Builder.defineMacro("__BPF_FEATURE_MAY_GOTO");
 
   int CpuVerNum = std::stoi(CpuVerNumStr);
   if (CpuVerNum >= 2)
diff --git a/llvm/lib/Target/BPF/AsmParser/BPFAsmParser.cpp 
b/llvm/lib/Target/BPF/AsmParser/BPFAsmParser.cpp
index 3145bc3d19f5dc..1688355f427cc7 100644
--- a/llvm/lib/Target/BPF/AsmParser/BPFAsmParser.cpp
+++ b/llvm/lib/Target/BPF/AsmParser/BPFAsmParser.cpp
@@ -232,6 +232,7 @@ struct BPFOperand : public MCParsedAsmOperand {
 .Case("callx", true)
 .Case("goto", true)
 .Case("gotol", true)
+.Case("may_goto", true)
 .Case("*", true)
 .Case("exit", true)
 .Case("lock", tru