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

via llvm-commits llvm-commits at lists.llvm.org
Sat Nov 4 14:30:28 PDT 2023


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

>From 3f3eb6f781d4d83c3c6785c2bcb63a2911d85f07 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    | 17 +++++++++++++++++
 3 files changed, 27 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..56fe2cf6ac936e4
--- /dev/null
+++ b/llvm/test/CodeGen/Generic/landingpad_wrong_type.ll
@@ -0,0 +1,17 @@
+; 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