[PATCH] D156497: [BPF] report fatal error rather than miscompile

Tamir Duberstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 27 17:15:33 PDT 2023


tamird created this revision.
tamird added reviewers: ast, yonghong-song, eddyz87.
Herald added a subscriber: hiraditya.
Herald added a project: All.
tamird requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This commit causes BPF codegen to abort if any unsupported usages were
encountered. Prior to this commit the decision to abort was left in the
hands of the embedder, whose diagnostic handler could choose to proceed
to use miscompiled code. See 20a8d8e (D20571 <https://reviews.llvm.org/D20571>) and 353a5e0 (D20726 <https://reviews.llvm.org/D20726>) for
instances of deliberate miscompilation that were introduced in service
of more complete diagnostics.

Note that this causes codegen to stop after the first faulty function
rather than emitting diagnostics for all functions in a compile unit.

It's not clear to me that we want to land this; sending the diff to
start the discussion.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156497

Files:
  llvm/lib/Target/BPF/BPFISelLowering.cpp
  llvm/lib/Target/BPF/BPFISelLowering.h


Index: llvm/lib/Target/BPF/BPFISelLowering.h
===================================================================
--- llvm/lib/Target/BPF/BPFISelLowering.h
+++ llvm/lib/Target/BPF/BPFISelLowering.h
@@ -66,6 +66,8 @@
 
   MVT getScalarShiftAmountTy(const DataLayout &, EVT) const override;
 
+  void finalizeLowering(MachineFunction &MF) const override;
+
 private:
   // Control Instruction Selection Features
   bool HasAlu32;
@@ -73,6 +75,11 @@
   bool HasJmpExt;
   bool HasMovsx;
 
+  mutable bool HasError = false;
+
+  void fail(const SDLoc &DL, SelectionDAG &DAG, const Twine &Msg,
+            SDValue Val) const;
+
   SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) const;
   SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const;
   SDValue LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const;
Index: llvm/lib/Target/BPF/BPFISelLowering.cpp
===================================================================
--- llvm/lib/Target/BPF/BPFISelLowering.cpp
+++ llvm/lib/Target/BPF/BPFISelLowering.cpp
@@ -35,22 +35,18 @@
   cl::Hidden, cl::init(false),
   cl::desc("Expand memcpy into load/store pairs in order"));
 
-static void fail(const SDLoc &DL, SelectionDAG &DAG, const Twine &Msg) {
-  MachineFunction &MF = DAG.getMachineFunction();
-  DAG.getContext()->diagnose(
-      DiagnosticInfoUnsupported(MF.getFunction(), Msg, DL.getDebugLoc()));
-}
-
-static void fail(const SDLoc &DL, SelectionDAG &DAG, const char *Msg,
-                 SDValue Val) {
-  MachineFunction &MF = DAG.getMachineFunction();
+void BPFTargetLowering::fail(const SDLoc &DL, SelectionDAG &DAG,
+                             const Twine &Msg, SDValue Val = {}) const {
+  HasError = true;
   std::string Str;
-  raw_string_ostream OS(Str);
-  OS << Msg;
-  Val->print(OS);
-  OS.flush();
-  DAG.getContext()->diagnose(
-      DiagnosticInfoUnsupported(MF.getFunction(), Str, DL.getDebugLoc()));
+  if (Val) {
+    raw_string_ostream OS(Str);
+    Val->print(OS);
+    OS << ' ';
+  }
+  MachineFunction &MF = DAG.getMachineFunction();
+  DAG.getContext()->diagnose(DiagnosticInfoUnsupported(
+      MF.getFunction(), Msg.concat(Str), DL.getDebugLoc()));
 }
 
 BPFTargetLowering::BPFTargetLowering(const TargetMachine &TM,
@@ -871,6 +867,12 @@
   return (getHasAlu32() && VT == MVT::i32) ? MVT::i32 : MVT::i64;
 }
 
+void BPFTargetLowering::finalizeLowering(MachineFunction &MF) const {
+  if (HasError) {
+    report_fatal_error("failed to generate BPF instructions, see diagnostics");
+  }
+}
+
 bool BPFTargetLowering::isLegalAddressingMode(const DataLayout &DL,
                                               const AddrMode &AM, Type *Ty,
                                               unsigned AS,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156497.544981.patch
Type: text/x-patch
Size: 2699 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230728/93de3f1f/attachment.bin>


More information about the llvm-commits mailing list