[llvm] [BPF] report `Invalid usage of the XADD return value"` elegantly (PR #92742)

Yingchi Long via llvm-commits llvm-commits at lists.llvm.org
Mon May 20 04:29:46 PDT 2024


https://github.com/inclyc created https://github.com/llvm/llvm-project/pull/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.

>From 49ce143aafd9b009016d357422504c59034736a2 Mon Sep 17 00:00:00 2001
From: Yingchi Long <i at lyc.dev>
Date: Mon, 20 May 2024 19:22:48 +0800
Subject: [PATCH] [BPF] report `Invalid usage of the XADD return value"`
 elegantly

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 89ac485b1675e..a968950f5bfcb 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 4901d9380ac4d..5aeeb9baf7b89 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
 }



More information about the llvm-commits mailing list