Skip to content

Commit

Permalink
[BPF] report Invalid usage of the XADD return value" elegantly (llv…
Browse files Browse the repository at this point in the history
…m#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.
  • Loading branch information
inclyc committed May 20, 2024
1 parent fd87d76 commit c648663
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
9 changes: 4 additions & 5 deletions llvm/lib/Target/BPF/BPFMIChecking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/Support/Debug.h"

using namespace llvm;
Expand Down Expand Up @@ -164,11 +165,9 @@ bool BPFMIPreEmitChecking::processAtomicInsts() {
if (hasLiveDefs(MI, TRI)) {
DebugLoc Empty;
const DebugLoc &DL = MI.getDebugLoc();
if (DL != Empty)
report_fatal_error(Twine("line ") + std::to_string(DL.getLine()) +
": Invalid usage of the XADD return value", false);
else
report_fatal_error("Invalid usage of the XADD return value", false);
const Function &F = MF->getFunction();
F.getContext().diagnose(DiagnosticInfoUnsupported{
F, "Invalid usage of the XADD return value", DL});
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/BPF/xadd.ll
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ entry:
call void @llvm.dbg.value(metadata ptr %ptr, metadata !13, metadata !DIExpression()), !dbg !15
%0 = atomicrmw add ptr %ptr, i32 4 seq_cst, !dbg !16
%1 = atomicrmw add ptr %ptr, i32 6 seq_cst, !dbg !17
; CHECK: line 4: Invalid usage of the XADD return value
; CHECK: in function test i32 (ptr): Invalid usage of the XADD return value
call void @llvm.dbg.value(metadata i32 %1, metadata !14, metadata !DIExpression()), !dbg !18
ret i32 %1, !dbg !19
}
Expand Down

0 comments on commit c648663

Please sign in to comment.