Skip to content

Commit 51cdb78

Browse files
committed
BPF: Fix a bug in peephole TRUNC elimination optimization
Andrei Matei reported a llvm11 core dump for his bpf program https://bugs.llvm.org/show_bug.cgi?id=48578 The core dump happens in LiveVariables analysis phase. #4 0x00007fce54356bb0 __restore_rt #5 0x00007fce4d51785e llvm::LiveVariables::HandleVirtRegUse(unsigned int, llvm::MachineBasicBlock*, llvm::MachineInstr&) #6 0x00007fce4d519abe llvm::LiveVariables::runOnInstr(llvm::MachineInstr&, llvm::SmallVectorImpl<unsigned int>&) #7 0x00007fce4d519ec6 llvm::LiveVariables::runOnBlock(llvm::MachineBasicBlock*, unsigned int) #8 0x00007fce4d51a4bf llvm::LiveVariables::runOnMachineFunction(llvm::MachineFunction&) The bug can be reproduced with llvm12 and latest trunk as well. Futher analysis shows that there is a bug in BPF peephole TRUNC elimination optimization, which tries to remove unnecessary TRUNC operations (a <<= 32; a >>= 32). Specifically, the compiler did wrong transformation for the following patterns: %1 = LDW ... %2 = SLL_ri %1, 32 %3 = SRL_ri %2, 32 ... %3 ... %4 = SRA_ri %2, 32 ... %4 ... The current transformation did not check how many uses of %2 and did transformation like %1 = LDW ... ... %1 ... %4 = SRL_ri %2, 32 ... %4 ... and pseudo register %2 is used by not defined and caused LiveVariables analysis core dump. To fix the issue, when traversing back from SRL_ri to SLL_ri, check to ensure SLL_ri has only one use. Otherwise, don't do transformation. Differential Revision: https://reviews.llvm.org/D97792
1 parent e77b5c4 commit 51cdb78

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

llvm/lib/Target/BPF/BPFMIPeephole.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,9 @@ bool BPFMIPeepholeTruncElim::eliminateTruncSeq(void) {
475475
if (MI.getOpcode() == BPF::SRL_ri &&
476476
MI.getOperand(2).getImm() == 32) {
477477
SrcReg = MI.getOperand(1).getReg();
478+
if (!MRI->hasOneNonDBGUse(SrcReg))
479+
continue;
480+
478481
MI2 = MRI->getVRegDef(SrcReg);
479482
DstReg = MI.getOperand(0).getReg();
480483

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
; RUN: llc < %s -march=bpf -verify-machineinstrs | FileCheck %s
2+
; Source Code:
3+
; struct loc_prog {
4+
; unsigned int ip;
5+
; int len;
6+
; };
7+
; int exec_prog(struct loc_prog *prog) {
8+
; if (prog->ip < prog->len) {
9+
; int x = prog->ip;
10+
; if (x < 3)
11+
; prog->ip += 2;
12+
; }
13+
; return 3;
14+
; }
15+
; Compilation flag:
16+
; clang -target bpf -O2 -S -emit-llvm t.c
17+
18+
%struct.loc_prog = type { i32, i32 }
19+
20+
; Function Attrs: nofree norecurse nounwind willreturn
21+
define dso_local i32 @exec_prog(%struct.loc_prog* nocapture %prog) local_unnamed_addr {
22+
entry:
23+
%ip = getelementptr inbounds %struct.loc_prog, %struct.loc_prog* %prog, i64 0, i32 0
24+
%0 = load i32, i32* %ip, align 4
25+
%len = getelementptr inbounds %struct.loc_prog, %struct.loc_prog* %prog, i64 0, i32 1
26+
%1 = load i32, i32* %len, align 4
27+
%cmp = icmp ult i32 %0, %1
28+
%cmp2 = icmp slt i32 %0, 3
29+
%or.cond = and i1 %cmp2, %cmp
30+
; CHECK: r{{[0-9]+}} <<= 32
31+
; CHECK: r{{[0-9]+}} s>>= 32
32+
br i1 %or.cond, label %if.then3, label %if.end5
33+
34+
if.then3: ; preds = %entry
35+
%add = add nsw i32 %0, 2
36+
store i32 %add, i32* %ip, align 4
37+
br label %if.end5
38+
39+
if.end5: ; preds = %if.then3, %entry
40+
ret i32 3
41+
}

0 commit comments

Comments
 (0)