From c6486633d2656faecea8d7eb426bb22c52ddfd14 Mon Sep 17 00:00:00 2001 From: Yingchi Long Date: Tue, 21 May 2024 01:57:56 +0800 Subject: [PATCH] [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. --- llvm/lib/Target/BPF/BPFMIChecking.cpp | 9 ++++----- llvm/test/CodeGen/BPF/xadd.ll | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/llvm/lib/Target/BPF/BPFMIChecking.cpp b/llvm/lib/Target/BPF/BPFMIChecking.cpp index 89ac485b1675eb..a968950f5bfcb8 100644 --- a/llvm/lib/Target/BPF/BPFMIChecking.cpp +++ b/llvm/lib/Target/BPF/BPFMIChecking.cpp @@ -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; @@ -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}); } } } diff --git a/llvm/test/CodeGen/BPF/xadd.ll b/llvm/test/CodeGen/BPF/xadd.ll index 4901d9380ac4d6..5aeeb9baf7b892 100644 --- a/llvm/test/CodeGen/BPF/xadd.ll +++ b/llvm/test/CodeGen/BPF/xadd.ll @@ -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 }