forked from llvm-mirror/llvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bpf: generate better lowering code for certain select/setcc instructions
Currently, for code like below, === inner_map = bpf_map_lookup_elem(outer_map, &port_key); if (!inner_map) { inner_map = &fallback_map; } === the compiler generates (pseudo) code like the below: === I1: r1 = bpf_map_lookup_elem(outer_map, &port_key); I2: r2 = 0 I3: if (r1 == r2) I4: r6 = &fallback_map I5: ... === During kernel verification process, After I1, r1 holds a state map_ptr_or_null. If I3 condition is not taken (path [I1, I2, I3, I5]), supposedly r1 should become map_ptr. Unfortunately, kernel does not recognize this pattern and r1 remains map_ptr_or_null at insn I5. This will cause verificaiton failure later on. Kernel, however, is able to recognize pattern "if (r1 == 0)" properly and give a map_ptr state to r1 in the above case. LLVM here generates suboptimal code which causes kernel verification failure. This patch fixes the issue by changing BPF insn pattern matching and lowering to generate proper codes if the righthand parameter of the above condition is a constant. A test case is also added. Signed-off-by: Yonghong Song <[email protected]> git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@308080 91177308-0d34-0410-b5e6-96231b3b80d8
- Loading branch information
1 parent
cf17bf0
commit 7c423e0
Showing
4 changed files
with
54 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
; RUN: llc < %s -march=bpf -verify-machineinstrs | FileCheck %s | ||
; | ||
; Source file: | ||
; int b, c; | ||
; int test() { | ||
; int a = b; | ||
; if (a) | ||
; a = c; | ||
; return a; | ||
; } | ||
@b = common local_unnamed_addr global i32 0, align 4 | ||
@c = common local_unnamed_addr global i32 0, align 4 | ||
|
||
; Function Attrs: norecurse nounwind readonly | ||
define i32 @test() local_unnamed_addr #0 { | ||
entry: | ||
%0 = load i32, i32* @b, align 4 | ||
%tobool = icmp eq i32 %0, 0 | ||
%1 = load i32, i32* @c, align 4 | ||
%. = select i1 %tobool, i32 0, i32 %1 | ||
; CHECK: r1 = <MCOperand Expr:(b)>ll | ||
; CHECK: r1 = *(u32 *)(r1 + 0) | ||
; CHECK: if r1 == 0 goto | ||
ret i32 %. | ||
} | ||
|
||
attributes #0 = { norecurse nounwind readonly } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters