[llvm] [PlaceSafepoints] Report usage error instead of asserting on invalid gc.safepoint_poll (PR #205520)

Gustas Rove via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 24 03:04:00 PDT 2026


https://github.com/TheMixas created https://github.com/llvm/llvm-project/pull/205520

When opt runs place-safepoints on valid IR that needs a safepoint poll but does not define a valid gc.safepoint_poll, InsertSafepointPoll asserted and crashed. Since this is reachable from user input, replace the precondition asserts with reportFatalUsageError so the tool reports a clean error and exits instead of crashing. These now report in all builds, not only assertions-enabled builds.

The missing-function path has a regression test; the wrong-type and empty-function paths are handled the same way.

Fixes #191975

>From cd676a07e840231b588653652f1d5b4cc761948e Mon Sep 17 00:00:00 2001
From: themixas <themixaslt at gmail.com>
Date: Wed, 24 Jun 2026 10:54:28 +0100
Subject: [PATCH] [PlaceSafepoints] Report usage error instead of asserting on
 invalid gc.safepoint_poll

When opt runs place-safepoints on valid IR that needs a safepoint poll but
does not define a valid gc.safepoint_poll, InsertSafepointPoll asserted and
crashed. Since this is reachable from user input, replace the precondition
asserts with reportFatalUsageError so the tool reports a clean error and
exits instead of crashing. These now report in all builds, not only
assertions-enabled builds.

Fixes #191975.
---
 llvm/lib/Transforms/Scalar/PlaceSafepoints.cpp      | 13 ++++++++-----
 .../PlaceSafepoints/missing-safepoint-poll.ll       |  7 +++++++
 2 files changed, 15 insertions(+), 5 deletions(-)
 create mode 100644 llvm/test/Transforms/PlaceSafepoints/missing-safepoint-poll.ll

diff --git a/llvm/lib/Transforms/Scalar/PlaceSafepoints.cpp b/llvm/lib/Transforms/Scalar/PlaceSafepoints.cpp
index 44600acf5b418..85ab765d7b737 100644
--- a/llvm/lib/Transforms/Scalar/PlaceSafepoints.cpp
+++ b/llvm/lib/Transforms/Scalar/PlaceSafepoints.cpp
@@ -629,11 +629,14 @@ InsertSafepointPoll(BasicBlock::iterator InsertBefore,
   // path call - where we need to insert a safepoint (parsepoint).
 
   auto *F = M->getFunction(GCSafepointPollName);
-  assert(F && "gc.safepoint_poll function is missing");
-  assert(F->getFunctionType() ==
-             FunctionType::get(Type::getVoidTy(M->getContext()), false) &&
-         "gc.safepoint_poll declared with wrong type");
-  assert(!F->empty() && "gc.safepoint_poll must be a non-empty function");
+  if (!F) {
+    reportFatalUsageError("gc.safepoint_poll function is missing");
+  } else if (F->getFunctionType() !=
+             FunctionType::get(Type::getVoidTy(M->getContext()), false)) {
+    reportFatalUsageError("gc.safepoint_poll declared with wrong type");
+  } else if (F->empty()) {
+    reportFatalUsageError("gc.safepoint_poll must be a non-empty function");
+  }
   CallInst *PollCall = CallInst::Create(F, "", InsertBefore);
 
   // Record some information about the call site we're replacing
diff --git a/llvm/test/Transforms/PlaceSafepoints/missing-safepoint-poll.ll b/llvm/test/Transforms/PlaceSafepoints/missing-safepoint-poll.ll
new file mode 100644
index 0000000000000..5a7d5d4cc5b58
--- /dev/null
+++ b/llvm/test/Transforms/PlaceSafepoints/missing-safepoint-poll.ll
@@ -0,0 +1,7 @@
+; RUN: not opt < %s -S -passes=place-safepoints 2>&1 | FileCheck %s
+; CHECK: LLVM ERROR: gc.safepoint_poll function is missing
+
+define void @test_libcall() gc "statepoint-example" {
+entry:
+  ret void
+}
\ No newline at end of file



More information about the llvm-commits mailing list