Skip to content

Commit 1c19bae

Browse files
author
Yonghong Song
committed
Report an error by default instead of a warning
Report an error by default. Add a new flag -bpf-disable-check-unreachable-ir to disable checking.
1 parent fcbcd8e commit 1c19bae

File tree

6 files changed

+41
-31
lines changed

6 files changed

+41
-31
lines changed

llvm/lib/Target/BPF/BPF.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class BPFTargetMachine;
2222
class InstructionSelector;
2323
class PassRegistry;
2424

25-
ModulePass *createBPFCheckUndefIR();
25+
ModulePass *createBPFCheckUnreachableIR();
2626
ModulePass *createBPFCheckAndAdjustIR();
2727

2828
FunctionPass *createBPFISelDag(BPFTargetMachine &TM);
@@ -35,7 +35,7 @@ InstructionSelector *createBPFInstructionSelector(const BPFTargetMachine &,
3535
const BPFSubtarget &,
3636
const BPFRegisterBankInfo &);
3737

38-
void initializeBPFCheckUndefIRPass(PassRegistry &);
38+
void initializeBPFCheckUnreachableIRPass(PassRegistry &);
3939
void initializeBPFCheckAndAdjustIRPass(PassRegistry&);
4040
void initializeBPFDAGToDAGISelLegacyPass(PassRegistry &);
4141
void initializeBPFMIPeepholePass(PassRegistry &);

llvm/lib/Target/BPF/BPFCheckUndefIR.cpp renamed to llvm/lib/Target/BPF/BPFCheckUnreachableIR.cpp

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
//===---------------- BPFAdjustOpt.cpp - Adjust Optimization --------------===//
1+
//===--------- BPFCheckUnreachableIR.cpp - Issue Unreachable Error --------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// Check 'unreachable' IRs and issue proper warnings.
9+
// Check 'unreachable' IRs and issue proper errors.
1010
//
1111
//===----------------------------------------------------------------------===//
1212

@@ -19,34 +19,41 @@
1919
#include "llvm/IR/Value.h"
2020
#include "llvm/Pass.h"
2121

22-
#define DEBUG_TYPE "bpf-check-undef-ir"
22+
#define DEBUG_TYPE "bpf-check-unreachable-ir"
2323

2424
using namespace llvm;
2525

26+
static cl::opt<bool>
27+
DisableCheckUnreachableIR("bpf-disable-check-unreachable-ir", cl::Hidden,
28+
cl::desc("BPF: Disable Checking Unreachable IR"),
29+
cl::init(false));
30+
2631
namespace {
2732

28-
class BPFCheckUndefIR final : public ModulePass {
33+
class BPFCheckUnreachableIR final : public ModulePass {
2934
bool runOnModule(Module &F) override;
3035

3136
public:
3237
static char ID;
33-
BPFCheckUndefIR() : ModulePass(ID) {}
38+
BPFCheckUnreachableIR() : ModulePass(ID) {}
3439

3540
private:
36-
void BPFCheckUndefIRImpl(Function &F);
41+
void BPFCheckUnreachableIRImpl(Function &F);
3742
void BPFCheckInst(Function &F, BasicBlock &BB, Instruction &I);
3843
void HandleUnreachableInsn(Function &F, BasicBlock &BB, Instruction &I);
3944
};
4045
} // End anonymous namespace
4146

42-
char BPFCheckUndefIR::ID = 0;
43-
INITIALIZE_PASS(BPFCheckUndefIR, DEBUG_TYPE, "BPF Check Undef IRs", false,
44-
false)
47+
char BPFCheckUnreachableIR::ID = 0;
48+
INITIALIZE_PASS(BPFCheckUnreachableIR, DEBUG_TYPE, "BPF Check Unreachable IRs",
49+
false, false)
4550

46-
ModulePass *llvm::createBPFCheckUndefIR() { return new BPFCheckUndefIR(); }
51+
ModulePass *llvm::createBPFCheckUnreachableIR() {
52+
return new BPFCheckUnreachableIR();
53+
}
4754

48-
void BPFCheckUndefIR::HandleUnreachableInsn(Function &F, BasicBlock &BB,
49-
Instruction &I) {
55+
void BPFCheckUnreachableIR::HandleUnreachableInsn(Function &F, BasicBlock &BB,
56+
Instruction &I) {
5057
// LLVM may create a switch statement with default to a 'unreachable' basic
5158
// block. Do not warn for such cases.
5259
unsigned NumNoSwitches = 0, NumSwitches = 0;
@@ -73,17 +80,18 @@ void BPFCheckUndefIR::HandleUnreachableInsn(Function &F, BasicBlock &BB,
7380
F.getContext().diagnose(
7481
DiagnosticInfoGeneric(Twine("unreachable in func ")
7582
.concat(F.getName())
76-
.concat(", due to uninitialized variable?"),
77-
DS_Warning));
83+
.concat(", due to uninitialized variable?")
84+
.concat(" try -Wuninitialized?"),
85+
DS_Error));
7886
}
7987

80-
void BPFCheckUndefIR::BPFCheckInst(Function &F, BasicBlock &BB,
81-
Instruction &I) {
88+
void BPFCheckUnreachableIR::BPFCheckInst(Function &F, BasicBlock &BB,
89+
Instruction &I) {
8290
if (I.getOpcode() == Instruction::Unreachable)
8391
HandleUnreachableInsn(F, BB, I);
8492
}
8593

86-
void BPFCheckUndefIR::BPFCheckUndefIRImpl(Function &F) {
94+
void BPFCheckUnreachableIR::BPFCheckUnreachableIRImpl(Function &F) {
8795
// A 'unreachable' will be added to the end of naked function.
8896
// Let ignore these naked functions.
8997
if (F.hasFnAttribute(Attribute::Naked))
@@ -95,8 +103,10 @@ void BPFCheckUndefIR::BPFCheckUndefIRImpl(Function &F) {
95103
}
96104
}
97105

98-
bool BPFCheckUndefIR::runOnModule(Module &M) {
106+
bool BPFCheckUnreachableIR::runOnModule(Module &M) {
107+
if (DisableCheckUnreachableIR)
108+
return false;
99109
for (Function &F : M)
100-
BPFCheckUndefIRImpl(F);
110+
BPFCheckUnreachableIRImpl(F);
101111
return false;
102112
}

llvm/lib/Target/BPF/BPFTargetMachine.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeBPFTarget() {
4545

4646
PassRegistry &PR = *PassRegistry::getPassRegistry();
4747
initializeGlobalISel(PR);
48-
initializeBPFCheckUndefIRPass(PR);
48+
initializeBPFCheckUnreachableIRPass(PR);
4949
initializeBPFCheckAndAdjustIRPass(PR);
5050
initializeBPFMIPeepholePass(PR);
5151
initializeBPFDAGToDAGISelLegacyPass(PR);
@@ -144,7 +144,7 @@ void BPFTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) {
144144
}
145145

146146
void BPFPassConfig::addIRPasses() {
147-
addPass(createBPFCheckUndefIR());
147+
addPass(createBPFCheckUnreachableIR());
148148
addPass(createAtomicExpandLegacyPass());
149149
addPass(createBPFCheckAndAdjustIR());
150150

llvm/lib/Target/BPF/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ add_llvm_target(BPFCodeGen
2626
BPFAsmPrinter.cpp
2727
BPFASpaceCastSimplifyPass.cpp
2828
BPFCheckAndAdjustIR.cpp
29-
BPFCheckUndefIR.cpp
29+
BPFCheckUnreachableIR.cpp
3030
BPFFrameLowering.cpp
3131
BPFInstrInfo.cpp
3232
BPFIRPeephole.cpp

llvm/test/CodeGen/BPF/naked-fn-with-frame-pointer.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@ define dso_local void @normal() "frame-pointer"="all" {
3737
; CHECK-BE-NEXT: # %bb.0:
3838
; CHECK-BE-NEXT: call main
3939
call void @main()
40-
unreachable
40+
ret void
4141
}

llvm/test/CodeGen/BPF/store_imm.ll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ define void @byte(ptr %p0) {
1313
store volatile i8 1, ptr %p0, align 1
1414
store volatile i8 -1, ptr %p1, align 1
1515

16-
unreachable
16+
ret void
1717
}
1818

1919
define void @half(ptr, ptr %p0) {
@@ -26,7 +26,7 @@ define void @half(ptr, ptr %p0) {
2626
store volatile i16 1, ptr %p0, align 2
2727
store volatile i16 -1, ptr %p1, align 2
2828

29-
unreachable
29+
ret void
3030
}
3131

3232
define void @word(ptr, ptr, ptr %p0) {
@@ -47,7 +47,7 @@ define void @word(ptr, ptr, ptr %p0) {
4747
store volatile i32 4294967295, ptr %p3, align 4
4848
store volatile i32 4294967296, ptr %p3, align 4
4949

50-
unreachable
50+
ret void
5151
}
5252

5353
define void @dword(ptr, ptr, ptr, ptr %p0) {
@@ -69,7 +69,7 @@ define void @dword(ptr, ptr, ptr, ptr %p0) {
6969
store volatile i64 -2000000000, ptr %p2, align 8
7070
store volatile i64 4294967295, ptr %p3, align 8
7171

72-
unreachable
72+
ret void
7373
}
7474

7575
define void @unaligned(ptr %p0) {
@@ -88,7 +88,7 @@ define void @unaligned(ptr %p0) {
8888
store volatile i32 -2, ptr %p1, align 2
8989
store volatile i64 -2, ptr %p2, align 4
9090

91-
unreachable
91+
ret void
9292
}
9393

9494
define void @inline_asm(ptr %p0) {
@@ -100,5 +100,5 @@ define void @inline_asm(ptr %p0) {
100100
; CHECK-NEXT: #NO_APP
101101
call void asm "*(u32 *)(r0 + 42) = 7;", "~{r0},~{mem}"()
102102

103-
unreachable
103+
ret void
104104
}

0 commit comments

Comments
 (0)