[llvm] CodeGen: replace assertions with explicit errors for landinpad lowering (PR #71287)

via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 5 01:04:53 PDT 2023


https://github.com/AlexDenisov updated https://github.com/llvm/llvm-project/pull/71287

>From 1f18b740f57583e327bfc9c516d5a991ce484add Mon Sep 17 00:00:00 2001
From: AlexDenisov <alex at lowlevelbits.org>
Date: Sat, 4 Nov 2023 16:46:18 +0100
Subject: [PATCH] CodeGen: replace assertions with explicit errors for
 landinpad lowering

Report explicit error messages instead of silently crashing in release mode.

These checks can be moved to Verifier, but it breaks compatibility tests.
---
 llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp   | 10 +++++++---
 .../SelectionDAG/SelectionDAGBuilder.cpp       |  4 +++-
 .../CodeGen/Generic/landingpad_wrong_type.ll   | 18 ++++++++++++++++++
 3 files changed, 28 insertions(+), 4 deletions(-)
 create mode 100644 llvm/test/CodeGen/Generic/landingpad_wrong_type.ll

diff --git a/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp b/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
index d8f9e30b2599779..a9129bb29c55fae 100644
--- a/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
@@ -2795,9 +2795,13 @@ bool IRTranslator::translateLandingPad(const User &U,
   MIRBuilder.buildUndef(Undef);
 
   SmallVector<LLT, 2> Tys;
-  for (Type *Ty : cast<StructType>(LP.getType())->elements())
-    Tys.push_back(getLLTForType(*Ty, *DL));
-  assert(Tys.size() == 2 && "Only two-valued landingpads are supported");
+  if (LP.getType()->isStructTy()) {
+    for (Type *Ty : cast<StructType>(LP.getType())->elements())
+      Tys.push_back(getLLTForType(*Ty, *DL));
+  }
+  if (Tys.size() != 2) {
+    report_fatal_error("Only two-valued landingpads are supported");
+  }
 
   // Mark exception register as live in.
   Register ExceptionReg = TLI.getExceptionPointerRegister(PersonalityFn);
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
index 48096dc1687fcec..1c25ae420a482a1 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
@@ -3166,7 +3166,9 @@ void SelectionDAGBuilder::visitLandingPad(const LandingPadInst &LP) {
   SmallVector<EVT, 2> ValueVTs;
   SDLoc dl = getCurSDLoc();
   ComputeValueVTs(TLI, DAG.getDataLayout(), LP.getType(), ValueVTs);
-  assert(ValueVTs.size() == 2 && "Only two-valued landingpads are supported");
+  if (ValueVTs.size() != 2) {
+    report_fatal_error("Only two-valued landingpads are supported");
+  }
 
   // Get the two live-in registers as SDValues. The physregs have already been
   // copied into virtual registers.
diff --git a/llvm/test/CodeGen/Generic/landingpad_wrong_type.ll b/llvm/test/CodeGen/Generic/landingpad_wrong_type.ll
new file mode 100644
index 000000000000000..deaa36e43575653
--- /dev/null
+++ b/llvm/test/CodeGen/Generic/landingpad_wrong_type.ll
@@ -0,0 +1,18 @@
+; UNSUPPORTED: system-windows
+; RUN: not --crash llc %s 2>&1 | FileCheck %s
+; RUN: not --crash llc --global-isel %s 2>&1 | FileCheck %s
+
+declare i32 @pers(...)
+declare void @f()
+
+define void @main() personality ptr @pers {
+  invoke void @f() to label %normal unwind label %lp
+
+normal:
+  ret void
+
+lp:
+  landingpad {ptr} cleanup
+  ret void
+}
+; CHECK:LLVM ERROR: Only two-valued landingpads are supported



More information about the llvm-commits mailing list