Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions llvm/lib/Target/BPF/BPFAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ bool BPFAsmPrinter::doFinalization(Module &M) {
}
}

for (GlobalObject &GO : M.global_objects()) {
if (!GO.hasExternalWeakLinkage())
continue;

if (!SawTrapCall && GO.getName() == BPF_TRAP) {
GO.eraseFromParent();
break;
}
}

return AsmPrinter::doFinalization(M);
}

Expand Down Expand Up @@ -160,6 +170,20 @@ bool BPFAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
}

void BPFAsmPrinter::emitInstruction(const MachineInstr *MI) {
if (MI->isCall()) {
for (const MachineOperand &Op : MI->operands()) {
if (Op.isGlobal()) {
if (const GlobalValue *GV = Op.getGlobal())
if (GV->getName() == BPF_TRAP)
SawTrapCall = true;
} else if (Op.isSymbol()) {
if (const MCSymbol *Sym = Op.getMCSymbol())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will always fire an assertion:

  MCSymbol *getMCSymbol() const {
    assert(isMCSymbol() && "Wrong MachineOperand accessor");
    return Contents.Sym;
  }

But isMCSymbol() is always false since isSymbol() is true instead.

What's the right fix?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used Op.isSymbol() as a possible target but running bpf selftest, the branch with Op.isSymbol() never to be true, at least when running linux kernel bpf selftests.

Let me double check whether this branch isSymbol() is really necessary or not.

if (Sym->getName() == BPF_TRAP)
SawTrapCall = true;
}
}
}

BPF_MC::verifyInstructionPredicates(MI->getOpcode(),
getSubtargetInfo().getFeatureBits());

Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/BPF/BPFAsmPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class BPFAsmPrinter : public AsmPrinter {
private:
BTFDebug *BTF;
TargetMachine &TM;
bool SawTrapCall = false;

const BPFTargetMachine &getBTM() const;
};
Expand Down
32 changes: 32 additions & 0 deletions llvm/test/CodeGen/BPF/bpf_trap.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
; RUN: llc < %s | FileCheck %s
;
target triple = "bpf"

define i32 @test(i8 %x) {
entry:
%0 = and i8 %x, 3
switch i8 %0, label %default.unreachable4 [
i8 0, label %return
i8 1, label %sw.bb1
i8 2, label %sw.bb2
i8 3, label %sw.bb3
]

sw.bb1: ; preds = %entry
br label %return

sw.bb2: ; preds = %entry
br label %return

sw.bb3: ; preds = %entry
br label %return

default.unreachable4: ; preds = %entry
unreachable

return: ; preds = %entry, %sw.bb3, %sw.bb2, %sw.bb1
%retval.0 = phi i32 [ 12, %sw.bb1 ], [ 43, %sw.bb2 ], [ 54, %sw.bb3 ], [ 32, %entry ]
ret i32 %retval.0
}

; CHECK-NOT: __bpf_trap