Skip to contentSkip to conversation

Add BPF AsmParser support in LLVM

RFC PATCH Superseded Jiong Wang 2 participants 9 messages Sep 7, 2017 – Sep 12, 2017 1 message not in the archive

lore
Share & tools
lore

Jiong Wang

author
#0 lore raw

RFC PATCH 1/1 Add BPF AsmParser support in LLVM

Hi,

As discussed at threads:

    https://www.spinics.net/lists/xdp-newbies/msg00320.html
    https://www.spinics.net/lists/xdp-newbies/msg00323.html

This patch adds the initial BPF AsmParser support in LLVM.

This support is following the "verifier instruction format" which is C-like.

Things need to mention are:

1. LLVM AsmParser doesn't expect the character "*" to be used as the start of a statement while BPF "verifier instruction format" is. So an new generic method "starIsStartOfStatement" is introduced.

2. As the MC assembler is sharing code infrastructure with disassembler, the current supported instruction format is *strictly* those registered in instruction information .td files. For example, the parser doesn't acccept "*(u8 *)(r0) = r7", instead, you need to write
"*(u8 *)(r0 + 0) = r7". The offset is mandatory, even when it is zero. The instruction information .td files may need to register customized ParserMethods to allow more flexible syntaxes.

The testcase bpf-insn-unit.s contains unit tests for supported syntaxes.

Comments? Does the current accepted syntaxes look OK?

---
 include/llvm/MC/MCParser/MCTargetAsmParser.h |   2 +
 lib/MC/MCParser/AsmParser.cpp                |   5 +
 lib/Target/BPF/AsmParser/BPFAsmParser.cpp    | 472 +++++++++++++++++++++++++++
 lib/Target/BPF/AsmParser/CMakeLists.txt      |   3 +
 lib/Target/BPF/AsmParser/LLVMBuild.txt       |  23 ++
 lib/Target/BPF/CMakeLists.txt                |   1 +
 lib/Target/BPF/LLVMBuild.txt                 |   2 +-
 test/MC/BPF/bpf-insn-unit.s                  | 112 +++++++
 test/MC/BPF/lit.local.cfg                    |   3 +
 9 files changed, 622 insertions(+), 1 deletion(-)
 create mode 100644 lib/Target/BPF/AsmParser/BPFAsmParser.cpp
 create mode 100644 lib/Target/BPF/AsmParser/CMakeLists.txt
 create mode 100644 lib/Target/BPF/AsmParser/LLVMBuild.txt
 create mode 100644 test/MC/BPF/bpf-insn-unit.s
 create mode 100644 test/MC/BPF/lit.local.cfg
include/llvm/MC/MCParser/MCTargetAsmParser.h +2 -0
index 6d76d51..e5d5a2a 100644
@@ -266,6 +266,8 @@ public:
   virtual bool equalIsAsmAssignment() { return true; };
   // Return whether this start of statement identifier is a label
   virtual bool isLabel(AsmToken &Token) { return true; };
+  // Return whether this parser accept star as start of statement
+  virtual bool starIsStartOfStatement() { return false; };
 
   virtual const MCExpr *applyModifierToExpr(const MCExpr *E,
                                             MCSymbolRefExpr::VariantKind,
lib/MC/MCParser/AsmParser.cpp +5 -0
index 03858d3..ee3cc8d 100644
@@ -1687,6 +1687,11 @@ bool AsmParser::parseStatement(ParseStatementInfo &Info,
     // Treat '}' as a valid identifier in this context.
     Lex();
     IDVal = "}";
+  } else if (Lexer.is(AsmToken::Star) &&
+             getTargetParser().starIsStartOfStatement()) {
+    // Accept '*' as a valid start of statement.
+    Lex();
+    IDVal = "*";
   } else if (parseIdentifier(IDVal)) {
     if (!TheCondState.Ignore) {
       Lex(); // always eat a token
lib/Target/BPF/AsmParser/BPFAsmParser.cpp new file +472 -0

472 lines not on this page · raw message

lib/Target/BPF/AsmParser/CMakeLists.txt new file +3 -0
new file mode 100644
index 0000000..22fb4f0
@@ -0,0 +1,3 @@
+add_llvm_library(LLVMBPFAsmParser
+  BPFAsmParser.cpp
+  )
lib/Target/BPF/AsmParser/LLVMBuild.txt new file +23 -0
new file mode 100644
index 0000000..2733da9
@@ -0,0 +1,23 @@
+;===- ./lib/Target/BPF/AsmParser/LLVMBuild.txt ---------------*- Conf -*--===;
+;
+;                     The LLVM Compiler Infrastructure
+;
+; This file is distributed under the University of Illinois Open Source
+; License. See LICENSE.TXT for details.
+;
+;===------------------------------------------------------------------------===;
+;
+; This is an LLVMBuild description file for the components in this subdirectory.
+;
+; For more information on the LLVMBuild system, please see:
+;
+;   http://llvm.org/docs/LLVMBuild.html
+;
+;===------------------------------------------------------------------------===;
+
+[component_0]
+type = Library
+name = BPFAsmParser
+parent = BPF
+required_libraries = MC MCParser BPFDesc BPFInfo Support
+add_to_library_groups = BPF
lib/Target/BPF/CMakeLists.txt +1 -0
index 4918653..1e4b685 100644
@@ -23,6 +23,7 @@ add_llvm_target(BPFCodeGen
   BPFTargetMachine.cpp
   )
 
+add_subdirectory(AsmParser)
 add_subdirectory(Disassembler)
 add_subdirectory(InstPrinter)
 add_subdirectory(TargetInfo)
lib/Target/BPF/LLVMBuild.txt +1 -1
index 171536d..3f9849b 100644
@@ -16,7 +16,7 @@
 ;===------------------------------------------------------------------------===;
 
 [common]
-subdirectories = InstPrinter Disassembler MCTargetDesc TargetInfo
+subdirectories = AsmParser InstPrinter Disassembler MCTargetDesc TargetInfo
 
 [component_0]
 type = TargetGroup
test/MC/BPF/bpf-insn-unit.s new file +112 -0

112 lines not on this page · raw message

test/MC/BPF/lit.local.cfg new file +3 -0
new file mode 100644
index 0000000..a089ab8
@@ -0,0 +1,3 @@
+if not 'BPF' in config.root.targets:
+    config.unsupported = True
+
Signature · 1 line
2.7.4
#1 lore raw

Hi, Jiong,

The patch looks great! I tested with a few bpf programs from kernel:samples/bpf/ directory.
Your test case with verifier like input looks good. I will have a few suggestions later.

I tested with compiler generated asm as well with both big endian and little endian.
. clang generate .ll
. llc generate .asm
. llvm-mc generate .o
. llvm-objdump disassembles.
It works fine except in one case:

For ld_imm64, we have:
class LD_IMM64<bits<4> Pseudo, string OpcodeStr>

    : InstBPF<(outs GPR:$dst), (ins u64imm:$imm),
              "$dst "#OpcodeStr#" ${imm}ll",
              [(set GPR:$dst, (i64 imm:$imm))]>

In reality, the map it tries to access will not be constant, so the current assembler prints like:
r1 = <MCOperand Expr:(tx_port)>ll

The assembler does not like this and this insn is ignored. If I change the above to
r1 = tx_port ll
as expressed in the test case, the code will be generated properly with relocation record.

I guess, we may need to add something like LD_VAR_ADDR so that the proper assembly code can be issued. Also we could drop "ll" with "r1 = tx_port"?

Back to your test case bpf-insn-unit.s, you do not need add bpf in the file name since it is already under BPF directory.

Quoted 2 lines

r0 = *(u16 *)skb[2] // BPF_LD | BPF_ABS | BPF_H
r0 = * (u32*)skb[4] // BPF_LD | BPF_ABS | BPF_W

could you add BPF_B test as well?

Quoted 2 lines
 r0 = *  (u16 *)skb[r1] // BPF_LD | BPF_IND | BPF_H
 r0 = *(u32 *)skb[r2]   // BPF_LD | BPF_IND | BPF_W

BPF_B test?

Quoted 2 lines

r1 = 8589934591 ll // BPF_LD | BPF_DW | BPF_IMM
r1 = dummy_map ll // Symbolic version

// ======== BPF_LDX Class ========

  r5 = *(u8 *)(r0 + 0)   // BPF_LDX | BPF_B
  r6 = *(u16 *)(r1 + 8)  // BPF_LDX | BPF_H
  r7 = *(u32 *)(r2 + 16) // BPF_LDX | BPF_W
  r8 = *(u64 *)(r3 - 30) // BPF_LDX | BPF_DW

// ======== TODO: BPF_ST Class =======

Quoted 1 line

There is no insn here, you can remove this line for now.

// ======== BPF_STX Class ========

  *(u8 *)(r0 + 0) = r7    // BPF_STX | BPF_B
  *(u16 *)(r1 + 8) = r8   // BPF_STX | BPF_H
  *(u32 *)(r2 + 16) = r9  // BPF_STX | BPF_W
  *(u64 *)(r3 - 30) = r10 // BPF_STX | BPF_DW

lock *(u32 *)(r2 + 16) += r9 // BPF_STX | BPF_W | BPF_XADD lock *(u64 *)(r3 - 30) += r10 // BPF_STX | BPF_DW | BPF_XADD

// ======== BPF_JMP Class ========
goto Llabel0 // BPF_JA

  if r0 == r1 goto Llabel0   // BPF_JEQ
  if r1 > r2 goto Llabel0    // BPF_JGT
  if r2 >= r3 goto Llabel0   // BPF_JGE

// TODO: BPF_JSET

Quoted 1 line

there is no BPF_JSET, you can remove this.

  if r3 != r4 goto Llabel0   // BPF_JNE
  if r4 s> r5 goto Llabel0   // BPF_JSGT
  if r5 s>= r6 goto Llabel0  // BPF_JSGE
  call 1                     // BPF_CALL
  exit                       // BPF_EXIT
  if r6 < r7 goto Llabel0    // BPF_JLT
  if r7 <= r8 goto Llabel0   // BPF_JLE
  if r8 s< r9 goto Llabel0   // BPF_JSLT

if r9 s<= r10 goto Llabel0 // BPF_JSLE

Quoted 1 line

Could you add test compare register vs. imm?

// ======== BPF_ALU64 Class ========

  r0 += r1    // BPF_ADD
  r1 -= r2    // BPF_SUB
  r2 *= r3    // BPF_MUL
  r3 /= r4    // BPF_DIV
Llabel0:
  r4 |= r5    // BPF_OR
  r5 &= r6    // BPF_AND
  r6 <<= r7   // BPF_LSH
  r7 >>= r8   // BPF_RSH
              // TODO: BPF_NEG
              // TODO: BPF_MOD
Quoted 1 line

There is no BPF_NEG/MOD yet, you can remove these two lines.

r8 ^= r9 // BPF_XOR
r9 = r10 // BPF_MOV
r10 s>>= r0 // BPF_ARSH

Quoted 1 line

Could you add arith operations with register and imm. operands?

  bswap16 r1  // BPF_END | BPF_TO_BE
  bswap32 r2  // BPF_END | BPF_TO_BE
  bswap64 r3  // BPF_END | BPF_TO_BE
              // TODO: BPF_END | BPF_TO_LE
Quoted 3 lines

There is no BPF_TO_LE yet, you can remove this line.

Could you interleave the CHECK results with insn? This will be

clearer to do manual comparison.

Quoted 40 lines · Jiong Wang via iovisor-dev

Hi,

As discussed at threads:

    https://www.spinics.net/lists/xdp-newbies/msg00320.html
    https://www.spinics.net/lists/xdp-newbies/msg00323.html

This patch adds the initial BPF AsmParser support in LLVM.

This support is following the "verifier instruction format" which is
C-like.

Things need to mention are:

1. LLVM AsmParser doesn't expect the character "*" to be used as the
start
of a statement while BPF "verifier instruction format" is. So an new generic method "starIsStartOfStatement" is introduced.

2. As the MC assembler is sharing code infrastructure with disassembler,
the
current supported instruction format is *strictly* those registered
in
instruction information .td files. For example, the parser doesn't acccept "*(u8 *)(r0) = r7", instead, you need to write "*(u8 *)(r0 + 0) = r7". The offset is mandatory, even when it is
zero.
The instruction information .td files may need to register customized ParserMethods to allow more flexible syntaxes.

The testcase bpf-insn-unit.s contains unit tests for supported syntaxes.

Comments? Does the current accepted syntaxes look OK?

_______________________________________________
iovisor-dev mailing list
[email protected]
https://lists.iovisor.org/mailman/listinfo/iovisor-dev

author
#2 lore raw

Hi Y Song,

Thanks for the review and test.

[snip]

Quoted 3 lines

I guess, we may need to add something like LD_VAR_ADDR so that the proper assembly code can be issued. Also we could drop "ll" with "r1 = tx_port"?

Personally, I prefer drop "ll". The "ll" suffix was there to tell people it is 64bit constant while the register "r1" is with 64-bit width so I feel it is enough. For 32-bit situation, we need new register set, some thing like "w1 = tx_port".

And to generate proper assembly, it looks to me the customized print method for "uimm64" needs the following code (copied from printOperand):

@@ -90,5 +90,8 @@ void BPFInstPrinter::printImm64Operand(const MCInst *MI, unsigned OpNo,
if (Op.isImm())

     O << (uint64_t)Op.getImm();

else
- O << Op;
+ {
+ assert(Op.isExpr() && "Expected an expression");
+ printExpr(Op.getExpr(), O);
+ }
}

Quoted 3 lines

Back to your test case bpf-insn-unit.s, you do not need add bpf in the file name since it is already under BPF directory.

Will change the name. and will fix all reviews below in the next update.

Quoted 75 lines
Quoted 2 lines

r0 = *(u16 *)skb[2] // BPF_LD | BPF_ABS | BPF_H
r0 = * (u32*)skb[4] // BPF_LD | BPF_ABS | BPF_W

could you add BPF_B test as well?

Quoted 2 lines
 r0 = *  (u16 *)skb[r1] // BPF_LD | BPF_IND | BPF_H
 r0 = *(u32 *)skb[r2]   // BPF_LD | BPF_IND | BPF_W

BPF_B test?

Quoted 2 lines

r1 = 8589934591 ll // BPF_LD | BPF_DW | BPF_IMM
r1 = dummy_map ll // Symbolic version

// ======== BPF_LDX Class ========

  r5 = *(u8 *)(r0 + 0)   // BPF_LDX | BPF_B
  r6 = *(u16 *)(r1 + 8)  // BPF_LDX | BPF_H
  r7 = *(u32 *)(r2 + 16) // BPF_LDX | BPF_W
  r8 = *(u64 *)(r3 - 30) // BPF_LDX | BPF_DW

// ======== TODO: BPF_ST Class =======

Quoted 1 line

There is no insn here, you can remove this line for now.

// ======== BPF_STX Class ========

  *(u8 *)(r0 + 0) = r7    // BPF_STX | BPF_B
  *(u16 *)(r1 + 8) = r8   // BPF_STX | BPF_H
  *(u32 *)(r2 + 16) = r9  // BPF_STX | BPF_W
  *(u64 *)(r3 - 30) = r10 // BPF_STX | BPF_DW

lock *(u32 *)(r2 + 16) += r9 // BPF_STX | BPF_W | BPF_XADD lock *(u64 *)(r3 - 30) += r10 // BPF_STX | BPF_DW | BPF_XADD

// ======== BPF_JMP Class ========
goto Llabel0 // BPF_JA

  if r0 == r1 goto Llabel0   // BPF_JEQ
  if r1 > r2 goto Llabel0    // BPF_JGT
  if r2 >= r3 goto Llabel0   // BPF_JGE

// TODO: BPF_JSET

Quoted 1 line

there is no BPF_JSET, you can remove this.

  if r3 != r4 goto Llabel0   // BPF_JNE
  if r4 s> r5 goto Llabel0   // BPF_JSGT
  if r5 s>= r6 goto Llabel0  // BPF_JSGE
  call 1                     // BPF_CALL
  exit                       // BPF_EXIT
  if r6 < r7 goto Llabel0    // BPF_JLT
  if r7 <= r8 goto Llabel0   // BPF_JLE
  if r8 s< r9 goto Llabel0   // BPF_JSLT

if r9 s<= r10 goto Llabel0 // BPF_JSLE

Quoted 1 line

Could you add test compare register vs. imm?

// ======== BPF_ALU64 Class ========

  r0 += r1    // BPF_ADD
  r1 -= r2    // BPF_SUB
  r2 *= r3    // BPF_MUL
  r3 /= r4    // BPF_DIV
Llabel0:
  r4 |= r5    // BPF_OR
  r5 &= r6    // BPF_AND
  r6 <<= r7   // BPF_LSH
  r7 >>= r8   // BPF_RSH
              // TODO: BPF_NEG
              // TODO: BPF_MOD
Quoted 1 line

There is no BPF_NEG/MOD yet, you can remove these two lines.

r8 ^= r9 // BPF_XOR
r9 = r10 // BPF_MOV
r10 s>>= r0 // BPF_ARSH

Quoted 1 line

Could you add arith operations with register and imm. operands?

  bswap16 r1  // BPF_END | BPF_TO_BE
  bswap32 r2  // BPF_END | BPF_TO_BE
  bswap64 r3  // BPF_END | BPF_TO_BE
              // TODO: BPF_END | BPF_TO_LE
Quoted 3 lines

There is no BPF_TO_LE yet, you can remove this line.

Could you interleave the CHECK results with insn? This will be

clearer to do manual comparison.

#3 lore raw
Quoted 7 lines · Jiong Wang

Hi Y Song,

Thanks for the review and test.

[snip]

Quoted 1 line
Quoted 8 lines
Quoted 3 lines

I guess, we may need to add something like LD_VAR_ADDR so that the proper assembly code can be issued. Also we could drop "ll" with "r1 = tx_port"?

Personally, I prefer drop "ll". The "ll" suffix was there to tell people it is 64bit constant while the register "r1" is with 64-bit width so I feel it is enough. For 32-bit situation, we need new register set, some thing like "w1 = tx_port".

I just push a patch (similar to your suggestion below but without assertion) which still has
"ll" suffix for the constant, but no "ll" suffix for symbols. The reason is that we use "ll"
in the asm printout to differentiate "r = 5"=>"mov r, 5" (32bit imm) and "r = 5ll" => "ld_imm64 r, 5ll" (64bit imm).

Also, for long long constant, C standard does not allow space between value and "ll" and
hence "567 ll" is not allowed to represent a number "567ll". Could you try to disallow
64bit immediate like "567 ll" as well in your patch?

Thanks,

Yonghong

Quoted 114 lines

And to generate proper assembly, it looks to me the customized print method for "uimm64" needs the following code (copied from printOperand):

(quoted diff) +4 -1
@@ -90,5 +90,8 @@ void BPFInstPrinter::printImm64Operand(const MCInst *MI, unsigned OpNo,
   if (Op.isImm())
     O << (uint64_t)Op.getImm();
   else
-    O << Op;
+    {
+      assert(Op.isExpr() && "Expected an expression");
+      printExpr(Op.getExpr(), O);
+    }
 }
Quoted 4 lines

Back to your test case bpf-insn-unit.s, you do not need add bpf in the file name since it is already under BPF directory.

Will change the name. and will fix all reviews below in the next update.

Quoted 90 lines
Quoted 2 lines

r0 = *(u16 *)skb[2] // BPF_LD | BPF_ABS | BPF_H
r0 = * (u32*)skb[4] // BPF_LD | BPF_ABS | BPF_W

could you add BPF_B test as well?

Quoted 2 lines
 r0 = *  (u16 *)skb[r1] // BPF_LD | BPF_IND | BPF_H
 r0 = *(u32 *)skb[r2]   // BPF_LD | BPF_IND | BPF_W

BPF_B test?

Quoted 2 lines

r1 = 8589934591 ll // BPF_LD | BPF_DW | BPF_IMM
r1 = dummy_map ll // Symbolic version

// ======== BPF_LDX Class ========

  r5 = *(u8 *)(r0 + 0)   // BPF_LDX | BPF_B
  r6 = *(u16 *)(r1 + 8)  // BPF_LDX | BPF_H
  r7 = *(u32 *)(r2 + 16) // BPF_LDX | BPF_W
  r8 = *(u64 *)(r3 - 30) // BPF_LDX | BPF_DW

// ======== TODO: BPF_ST Class =======

Quoted 2 lines

There is no insn here, you can remove this line for now.

// ======== BPF_STX Class ========

  *(u8 *)(r0 + 0) = r7    // BPF_STX | BPF_B
  *(u16 *)(r1 + 8) = r8   // BPF_STX | BPF_H
  *(u32 *)(r2 + 16) = r9  // BPF_STX | BPF_W
  *(u64 *)(r3 - 30) = r10 // BPF_STX | BPF_DW

lock *(u32 *)(r2 + 16) += r9 // BPF_STX | BPF_W | BPF_XADD lock *(u64 *)(r3 - 30) += r10 // BPF_STX | BPF_DW | BPF_XADD

// ======== BPF_JMP Class ========
goto Llabel0 // BPF_JA

  if r0 == r1 goto Llabel0   // BPF_JEQ
  if r1 > r2 goto Llabel0    // BPF_JGT
  if r2 >= r3 goto Llabel0   // BPF_JGE

// TODO: BPF_JSET

Quoted 2 lines

there is no BPF_JSET, you can remove this.

  if r3 != r4 goto Llabel0   // BPF_JNE
  if r4 s> r5 goto Llabel0   // BPF_JSGT
  if r5 s>= r6 goto Llabel0  // BPF_JSGE
  call 1                     // BPF_CALL
  exit                       // BPF_EXIT
  if r6 < r7 goto Llabel0    // BPF_JLT
  if r7 <= r8 goto Llabel0   // BPF_JLE
  if r8 s< r9 goto Llabel0   // BPF_JSLT

if r9 s<= r10 goto Llabel0 // BPF_JSLE

Quoted 1 line

Could you add test compare register vs. imm?

// ======== BPF_ALU64 Class ========

  r0 += r1    // BPF_ADD
  r1 -= r2    // BPF_SUB
  r2 *= r3    // BPF_MUL
  r3 /= r4    // BPF_DIV
Llabel0:
  r4 |= r5    // BPF_OR
  r5 &= r6    // BPF_AND
  r6 <<= r7   // BPF_LSH
  r7 >>= r8   // BPF_RSH
              // TODO: BPF_NEG
              // TODO: BPF_MOD
Quoted 2 lines

There is no BPF_NEG/MOD yet, you can remove these two lines.

r8 ^= r9 // BPF_XOR
r9 = r10 // BPF_MOV
r10 s>>= r0 // BPF_ARSH

Quoted 2 lines

Could you add arith operations with register and imm. operands?

  bswap16 r1  // BPF_END | BPF_TO_BE
  bswap32 r2  // BPF_END | BPF_TO_BE
  bswap64 r3  // BPF_END | BPF_TO_BE
              // TODO: BPF_END | BPF_TO_LE
Quoted 5 lines

There is no BPF_TO_LE yet, you can remove this line.

Could you interleave the CHECK results with insn? This will be

clearer to do manual comparison.

author
#4 lore raw

RFC PATCH v2 1/1 Add BPF AsmParser support in LLVM

Quoted 24 lines · Y Song
Quoted 7 lines · Jiong Wang

Hi Y Song,

Thanks for the review and test.

[snip]

Quoted 1 line
Quoted 8 lines
Quoted 3 lines

I guess, we may need to add something like LD_VAR_ADDR so that the proper assembly code can be issued. Also we could drop "ll" with "r1 = tx_port"?

Personally, I prefer drop "ll". The "ll" suffix was there to tell people it is 64bit constant while the register "r1" is with 64-bit width so I feel it is enough. For 32-bit situation, we need new register set, some thing like "w1 = tx_port".

I just push a patch (similar to your suggestion below but without assertion) which still has
"ll" suffix for the constant, but no "ll" suffix for symbols. The reason is that we use "ll"
in the asm printout to differentiate "r = 5"=>"mov r, 5" (32bit imm) and "r = 5ll" => "ld_imm64 r, 5ll" (64bit imm).

Quoted 5 lines

Also, for long long constant, C standard does not allow space between value and "ll" and
hence "567 ll" is not allowed to represent a number "567ll". Could you try to disallow
64bit immediate like "567 ll" as well in your patch?

Hi Y Song,

Thanks, patch updated, please review.

Changes since the initial version:

* Addressed all comments on instruction unit tests.
- Renamed test file to "insn-unit.s".
- Removed unnecessary comments.
- Added BPF_B tests for load and store.
- Added BPF_K test for jmp and alu.
(Noticed a seperate issue with unsigned compare then jump. Looks like BPF
backend is alway printing the immediate as signed, I guess we need to change the operand types in instruction patterns.)
- Adjusted constant load tests.
- Interleaved the expected results with input insns.

* Changed the disassembly output "ll" to "LL" in BPFInstPrinter.cpp. This is because I noticed MC parser only accept "LL" as long long suffix. It might be the reason documented here:

https://www.securecoding.cert.org/confluence/pages/viewpage.action?pageId=19759250

---
 include/llvm/MC/MCParser/MCTargetAsmParser.h  |   2 +
 lib/MC/MCParser/AsmParser.cpp                 |   5 +
 lib/Target/BPF/AsmParser/BPFAsmParser.cpp     | 471 ++++++++++++++++++++++++++
 lib/Target/BPF/AsmParser/CMakeLists.txt       |   3 +
 lib/Target/BPF/AsmParser/LLVMBuild.txt        |  23 ++
 lib/Target/BPF/CMakeLists.txt                 |   1 +
 lib/Target/BPF/InstPrinter/BPFInstPrinter.cpp |   2 +-
 lib/Target/BPF/LLVMBuild.txt                  |   2 +-
 test/MC/BPF/insn-unit.s                       | 164 +++++++++
 test/MC/BPF/lit.local.cfg                     |   3 +
 10 files changed, 674 insertions(+), 2 deletions(-)
 create mode 100644 lib/Target/BPF/AsmParser/BPFAsmParser.cpp
 create mode 100644 lib/Target/BPF/AsmParser/CMakeLists.txt
 create mode 100644 lib/Target/BPF/AsmParser/LLVMBuild.txt
 create mode 100644 test/MC/BPF/insn-unit.s
 create mode 100644 test/MC/BPF/lit.local.cfg
include/llvm/MC/MCParser/MCTargetAsmParser.h +2 -0
index 6d76d51..e5d5a2a 100644
@@ -266,6 +266,8 @@ public:
   virtual bool equalIsAsmAssignment() { return true; };
   // Return whether this start of statement identifier is a label
   virtual bool isLabel(AsmToken &Token) { return true; };
+  // Return whether this parser accept star as start of statement
+  virtual bool starIsStartOfStatement() { return false; };
 
   virtual const MCExpr *applyModifierToExpr(const MCExpr *E,
                                             MCSymbolRefExpr::VariantKind,
lib/MC/MCParser/AsmParser.cpp +5 -0
index 03858d3..ee3cc8d 100644
@@ -1687,6 +1687,11 @@ bool AsmParser::parseStatement(ParseStatementInfo &Info,
     // Treat '}' as a valid identifier in this context.
     Lex();
     IDVal = "}";
+  } else if (Lexer.is(AsmToken::Star) &&
+             getTargetParser().starIsStartOfStatement()) {
+    // Accept '*' as a valid start of statement.
+    Lex();
+    IDVal = "*";
   } else if (parseIdentifier(IDVal)) {
     if (!TheCondState.Ignore) {
       Lex(); // always eat a token
lib/Target/BPF/AsmParser/BPFAsmParser.cpp new file +471 -0

471 lines not on this page · raw message

lib/Target/BPF/AsmParser/CMakeLists.txt new file +3 -0
new file mode 100644
index 0000000..22fb4f0
@@ -0,0 +1,3 @@
+add_llvm_library(LLVMBPFAsmParser
+  BPFAsmParser.cpp
+  )
lib/Target/BPF/AsmParser/LLVMBuild.txt new file +23 -0
new file mode 100644
index 0000000..2733da9
@@ -0,0 +1,23 @@
+;===- ./lib/Target/BPF/AsmParser/LLVMBuild.txt ---------------*- Conf -*--===;
+;
+;                     The LLVM Compiler Infrastructure
+;
+; This file is distributed under the University of Illinois Open Source
+; License. See LICENSE.TXT for details.
+;
+;===------------------------------------------------------------------------===;
+;
+; This is an LLVMBuild description file for the components in this subdirectory.
+;
+; For more information on the LLVMBuild system, please see:
+;
+;   http://llvm.org/docs/LLVMBuild.html
+;
+;===------------------------------------------------------------------------===;
+
+[component_0]
+type = Library
+name = BPFAsmParser
+parent = BPF
+required_libraries = MC MCParser BPFDesc BPFInfo Support
+add_to_library_groups = BPF
lib/Target/BPF/CMakeLists.txt +1 -0
index 4918653..1e4b685 100644
@@ -23,6 +23,7 @@ add_llvm_target(BPFCodeGen
   BPFTargetMachine.cpp
   )
 
+add_subdirectory(AsmParser)
 add_subdirectory(Disassembler)
 add_subdirectory(InstPrinter)
 add_subdirectory(TargetInfo)
lib/Target/BPF/InstPrinter/BPFInstPrinter.cpp +1 -1
index bb5546e..11b3f5f 100644
@@ -88,7 +88,7 @@ void BPFInstPrinter::printImm64Operand(const MCInst *MI, unsigned OpNo,
                                        raw_ostream &O) {
   const MCOperand &Op = MI->getOperand(OpNo);
   if (Op.isImm())
-    O << (uint64_t)Op.getImm() << "ll";
+    O << (uint64_t)Op.getImm() << "LL";
   else if (Op.isExpr())
     printExpr(Op.getExpr(), O);
   else
lib/Target/BPF/LLVMBuild.txt +1 -1
index 171536d..3f9849b 100644
@@ -16,7 +16,7 @@
 ;===------------------------------------------------------------------------===;
 
 [common]
-subdirectories = InstPrinter Disassembler MCTargetDesc TargetInfo
+subdirectories = AsmParser InstPrinter Disassembler MCTargetDesc TargetInfo
 
 [component_0]
 type = TargetGroup
test/MC/BPF/insn-unit.s new file +164 -0

164 lines not on this page · raw message

test/MC/BPF/lit.local.cfg new file +3 -0
new file mode 100644
index 0000000..a089ab8
@@ -0,0 +1,3 @@
+if not 'BPF' in config.root.targets:
+    config.unsupported = True
+
Signature · 1 line
2.7.4
#5 lore raw

Hi, Jiong,

Thanks for the patch. Comments inlined.

Quoted 11 lines

Changes since the initial version:

* Addressed all comments on instruction unit tests.
- Renamed test file to "insn-unit.s".
- Removed unnecessary comments.
- Added BPF_B tests for load and store.
- Added BPF_K test for jmp and alu.
(Noticed a seperate issue with unsigned compare then jump. Looks like
BPF
backend is alway printing the immediate as signed, I guess we need to change the operand types in instruction patterns.)

Right. We may need to separate out signed comparison vs. others. Will do that later.

Quoted 6 lines

- Adjusted constant load tests.
- Interleaved the expected results with input insns.

* Changed the disassembly output "ll" to "LL" in BPFInstPrinter.cpp. This is because I noticed MC parser only accept "LL" as long long
suffix.

Sorry for the confusion caused by "ll". I tested your patch and found that both r = 5 and r = 5LL generated ld_imm64 insn. This is not what compiler will do. Further debugging I found that this is due to my change to remove "ll" from asmstring in the insn definition (.td file). Since the assembler is patten matching based on asmstring. "r = 5" matched to ld_imm64 as well.

I just fixed the issue and restored " ll" suffix for ld_imm64 asmstring. You can then restore your previous change to add "ll" as a valid identifier in the middle of asm statement.

Thanks.

author Reviewed-by Yonghong Song
#6 lore raw
Quoted 14 lines
Quoted 7 lines

- Adjusted constant load tests.
- Interleaved the expected results with input insns.

* Changed the disassembly output "ll" to "LL" in BPFInstPrinter.cpp. This is because I noticed MC parser only accept "LL" as long long
suffix.

Sorry for the confusion caused by "ll". I tested your patch and found that both r = 5 and r = 5LL generated ld_imm64 insn. This is not what compiler will do. Further debugging I found that this is due to my change to remove "ll" from asmstring in the insn definition (.td file). Since the assembler is patten matching based on asmstring. "r = 5" matched to ld_imm64 as well.

Quoted 3 lines

I just fixed the issue and restored " ll" suffix for ld_imm64 asmstring. You can then restore your previous change to add "ll" as a valid identifier in the middle of asm statement.

Hmm, I actually feel there is no need to offer an separate syntax for 64bit imm encoding, it might be better to offer a unified single syntax "r = signed_imm" to the user and the encoder (BPFMCCodeEmitter::encodeInstruction) guarantee to choose optimal encoding.

Encoder could choose BPF_ALU64 | BPF_MOV | BPF_K for both "r1 = -1" and "r1 = -1ll" while only resorting to BPF_LD | BPF_DW | BPF_IMM when the imm really doesn't fit into 32bit (!isInt<32>(imm_opnd)), for example, "r1 = 0x1ffffffffll".

Anyway, patch updated, changes are:

- added "ll" back.
- updated the test cases for BPF_MOV | BPF_K in ALU64 class and BPF_IMM | BPF_DW in LD class.
- Dropped the change from "ll" to "LL". If we decided to move to "LL" which is in consistent with LLVM generic parser then could clean this up as a separate patch.

Please review, thanks.

Regards,
Jiong

---
 include/llvm/MC/MCParser/MCTargetAsmParser.h |   2 +
 lib/MC/MCParser/AsmParser.cpp                |   5 +
 lib/Target/BPF/AsmParser/BPFAsmParser.cpp    | 472 +++++++++++++++++++++++++++
 lib/Target/BPF/AsmParser/CMakeLists.txt      |   3 +
 lib/Target/BPF/AsmParser/LLVMBuild.txt       |  23 ++
 lib/Target/BPF/CMakeLists.txt                |   1 +
 lib/Target/BPF/LLVMBuild.txt                 |   2 +-
 test/MC/BPF/insn-unit.s                      | 168 ++++++++++
 test/MC/BPF/lit.local.cfg                    |   3 +
 9 files changed, 678 insertions(+), 1 deletion(-)
 create mode 100644 lib/Target/BPF/AsmParser/BPFAsmParser.cpp
 create mode 100644 lib/Target/BPF/AsmParser/CMakeLists.txt
 create mode 100644 lib/Target/BPF/AsmParser/LLVMBuild.txt
 create mode 100644 test/MC/BPF/insn-unit.s
 create mode 100644 test/MC/BPF/lit.local.cfg
include/llvm/MC/MCParser/MCTargetAsmParser.h +2 -0
index 6d76d51..e5d5a2a 100644
@@ -266,6 +266,8 @@ public:
   virtual bool equalIsAsmAssignment() { return true; };
   // Return whether this start of statement identifier is a label
   virtual bool isLabel(AsmToken &Token) { return true; };
+  // Return whether this parser accept star as start of statement
+  virtual bool starIsStartOfStatement() { return false; };
 
   virtual const MCExpr *applyModifierToExpr(const MCExpr *E,
                                             MCSymbolRefExpr::VariantKind,
lib/MC/MCParser/AsmParser.cpp +5 -0
index 03858d3..ee3cc8d 100644
@@ -1687,6 +1687,11 @@ bool AsmParser::parseStatement(ParseStatementInfo &Info,
     // Treat '}' as a valid identifier in this context.
     Lex();
     IDVal = "}";
+  } else if (Lexer.is(AsmToken::Star) &&
+             getTargetParser().starIsStartOfStatement()) {
+    // Accept '*' as a valid start of statement.
+    Lex();
+    IDVal = "*";
   } else if (parseIdentifier(IDVal)) {
     if (!TheCondState.Ignore) {
       Lex(); // always eat a token
lib/Target/BPF/AsmParser/BPFAsmParser.cpp new file +472 -0

472 lines not on this page · raw message

lib/Target/BPF/AsmParser/CMakeLists.txt new file +3 -0
new file mode 100644
index 0000000..22fb4f0
@@ -0,0 +1,3 @@
+add_llvm_library(LLVMBPFAsmParser
+  BPFAsmParser.cpp
+  )
lib/Target/BPF/AsmParser/LLVMBuild.txt new file +23 -0
new file mode 100644
index 0000000..2733da9
@@ -0,0 +1,23 @@
+;===- ./lib/Target/BPF/AsmParser/LLVMBuild.txt ---------------*- Conf -*--===;
+;
+;                     The LLVM Compiler Infrastructure
+;
+; This file is distributed under the University of Illinois Open Source
+; License. See LICENSE.TXT for details.
+;
+;===------------------------------------------------------------------------===;
+;
+; This is an LLVMBuild description file for the components in this subdirectory.
+;
+; For more information on the LLVMBuild system, please see:
+;
+;   http://llvm.org/docs/LLVMBuild.html
+;
+;===------------------------------------------------------------------------===;
+
+[component_0]
+type = Library
+name = BPFAsmParser
+parent = BPF
+required_libraries = MC MCParser BPFDesc BPFInfo Support
+add_to_library_groups = BPF
lib/Target/BPF/CMakeLists.txt +1 -0
index 4918653..1e4b685 100644
@@ -23,6 +23,7 @@ add_llvm_target(BPFCodeGen
   BPFTargetMachine.cpp
   )
 
+add_subdirectory(AsmParser)
 add_subdirectory(Disassembler)
 add_subdirectory(InstPrinter)
 add_subdirectory(TargetInfo)
lib/Target/BPF/LLVMBuild.txt +1 -1
index 171536d..3f9849b 100644
@@ -16,7 +16,7 @@
 ;===------------------------------------------------------------------------===;
 
 [common]
-subdirectories = InstPrinter Disassembler MCTargetDesc TargetInfo
+subdirectories = AsmParser InstPrinter Disassembler MCTargetDesc TargetInfo
 
 [component_0]
 type = TargetGroup
test/MC/BPF/insn-unit.s new file +168 -0

168 lines not on this page · raw message

test/MC/BPF/lit.local.cfg new file +3 -0
new file mode 100644
index 0000000..a089ab8
@@ -0,0 +1,3 @@
+if not 'BPF' in config.root.targets:
+    config.unsupported = True
+
Signature · 1 line
2.7.4

A message that is not in the archive (sent off-list or not yet received): CAH3MdRWOb6GnRcVLzFsKc215=anv+RGEF6FAJbvbkutbi3wjYg@mail.gmail.com

#8 lore raw

Sorry. Resend with "plain text format" to satisfy xdp-newbies mailing list requirement.

Quoted 69 lines · Y Song

Thanks, Jiong,

The patch looks good. I have applied to llvm trunk.
https://reviews.llvm.org/rL313055
A little bit more comments below.

Quoted 33 lines · Jiong Wang
Quoted 19 lines
Quoted 8 lines

- Adjusted constant load tests.
- Interleaved the expected results with input insns.

* Changed the disassembly output "ll" to "LL" in BPFInstPrinter.cpp. This is because I noticed MC parser only accept "LL" as long long
suffix.

Sorry for the confusion caused by "ll". I tested your patch and found that
both r = 5 and r = 5LL generated ld_imm64 insn. This is not what compiler will do. Further debugging I found that this is due to my change to remove
"ll" from asmstring in the insn definition (.td file). Since the assembler is
patten matching based on asmstring. "r = 5" matched to ld_imm64 as well.

Quoted 4 lines

I just fixed the issue and restored " ll" suffix for ld_imm64 asmstring. You can then restore your previous change to add "ll" as a valid identifier
in the middle of asm statement.

Hmm, I actually feel there is no need to offer an separate syntax for 64bit imm
encoding, it might be better to offer a unified single syntax "r = signed_imm"
to the user and the encoder (BPFMCCodeEmitter::encodeInstruction) guarantee to
choose optimal encoding.

Quoted 6 lines

Encoder could choose BPF_ALU64 | BPF_MOV | BPF_K for both "r1 = -1" and "r1 = -1ll" while only resorting to BPF_LD | BPF_DW | BPF_IMM when the imm really
doesn't fit into 32bit (!isInt<32>(imm_opnd)), for example, "r1 = 0x1ffffffffll".

Right now, this optimization actually
has been done in compiler side. So depending on the value, the compiler will use
"move r1, <32_bit_value>" or "ld_imm64 r1, <greater_than_32_bit_value>". In this case, it make sense to have different insn formats for two different kinds of
insns.

What you described is to move the optimization down to MC Emit Code stage, based on
the value, it can choose to use "move" or "ld_imm64" insn. So optimization will be available
for .c => .o and for .s => .o. (The current scheme, optimization not available from .s => .o).

Yes, it is possible. But typically, there is one-to-one mapping between asm insn to insn encoding.
If later on, we found the complain about this, we can revisit this issue.

Thanks.
Yonghong

author
#9 lore raw
Quoted 4 lines · Y Song
Quoted 4 lines

Thanks, Jiong,

The patch looks good. I have applied to llvm trunk.
https://reviews.llvm.org/rL313055

Thanks, Yonghong.

Quoted 30 lines
Quoted 30 lines
Quoted 9 lines

Hmm, I actually feel there is no need to offer an separate syntax for 64bit imm
encoding, it might be better to offer a unified single syntax "r = signed_imm"
to the user and the encoder (BPFMCCodeEmitter::encodeInstruction) guarantee to
choose optimal encoding.

Quoted 6 lines

Encoder could choose BPF_ALU64 | BPF_MOV | BPF_K for both "r1 = -1" and "r1 = -1ll" while only resorting to BPF_LD | BPF_DW | BPF_IMM when the imm really
doesn't fit into 32bit (!isInt<32>(imm_opnd)), for example, "r1 = 0x1ffffffffll".

Right now, this optimization actually
has been done in compiler side. So depending on the value, the compiler will use
"move r1, <32_bit_value>" or "ld_imm64 r1, <greater_than_32_bit_value>". In this case, it make sense to have different insn formats for two different kinds of
insns.

What you described is to move the optimization down to MC Emit Code stage, based on
the value, it can choose to use "move" or "ld_imm64" insn. So optimization will be available
for .c => .o and for .s => .o. (The current scheme, optimization not available from .s => .o).

Quoted 3 lines
Quoted 3 lines

Yes, it is possible. But typically, there is one-to-one mapping between asm insn to insn encoding.
If later on, we found the complain about this, we can revisit this issue.

OK.

Regards,
Jiong