Skip to content

Commit c648663

Browse files
authored
[BPF] report Invalid usage of the XADD return value" elegantly (#92742)
Previously `report_fatal_error` is used for reporting something goes wrong in the backend, but this is confusing because `report_fatal_error` basically means there are something unexpected & crashed in the backend. So, turn this "crash" into an elegant error reporting. After this patch, clang can diagnose it: bpf-crash.c:4:30: error: Invalid usage of the XADD return value 4 | u32 next_event_id() { return __sync_fetch_and_add(&GLOBAL_EVENT_ID, 1); } | ^ 1 error generated.
1 parent fd87d76 commit c648663

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

llvm/lib/Target/BPF/BPFMIChecking.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "llvm/CodeGen/MachineFunctionPass.h"
2121
#include "llvm/CodeGen/MachineInstrBuilder.h"
2222
#include "llvm/CodeGen/MachineRegisterInfo.h"
23+
#include "llvm/IR/DiagnosticInfo.h"
2324
#include "llvm/Support/Debug.h"
2425

2526
using namespace llvm;
@@ -164,11 +165,9 @@ bool BPFMIPreEmitChecking::processAtomicInsts() {
164165
if (hasLiveDefs(MI, TRI)) {
165166
DebugLoc Empty;
166167
const DebugLoc &DL = MI.getDebugLoc();
167-
if (DL != Empty)
168-
report_fatal_error(Twine("line ") + std::to_string(DL.getLine()) +
169-
": Invalid usage of the XADD return value", false);
170-
else
171-
report_fatal_error("Invalid usage of the XADD return value", false);
168+
const Function &F = MF->getFunction();
169+
F.getContext().diagnose(DiagnosticInfoUnsupported{
170+
F, "Invalid usage of the XADD return value", DL});
172171
}
173172
}
174173
}

llvm/test/CodeGen/BPF/xadd.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ entry:
2222
call void @llvm.dbg.value(metadata ptr %ptr, metadata !13, metadata !DIExpression()), !dbg !15
2323
%0 = atomicrmw add ptr %ptr, i32 4 seq_cst, !dbg !16
2424
%1 = atomicrmw add ptr %ptr, i32 6 seq_cst, !dbg !17
25-
; CHECK: line 4: Invalid usage of the XADD return value
25+
; CHECK: in function test i32 (ptr): Invalid usage of the XADD return value
2626
call void @llvm.dbg.value(metadata i32 %1, metadata !14, metadata !DIExpression()), !dbg !18
2727
ret i32 %1, !dbg !19
2828
}

0 commit comments

Comments
 (0)