[llvm] [DirectX] Fix assertion in PointerTypeAnalysis with empty global_ctors (PR #179034)

via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 4 04:53:43 PST 2026


https://github.com/mugiwaraluffy56 updated https://github.com/llvm/llvm-project/pull/179034

>From 8c0c15d62e4be7ed566f79d49fbdc299125f5717 Mon Sep 17 00:00:00 2001
From: mugiwaraluffy56 <myakampuneeth at gmail.com>
Date: Sat, 31 Jan 2026 19:01:28 +0530
Subject: [PATCH] [DirectX] Fix assertion in PointerTypeAnalysis with empty
 global_ctors

When llvm.global_ctors has no elements, its initializer is a
zeroinitializer (ConstantAggregateZero) rather than a ConstantArray.
The previous code used cast<ConstantArray> which asserts on
incompatible types.

Use dyn_cast and return early if the initializer is not a
ConstantArray to handle this case gracefully.

Fixes #178993.
---
 .../DirectXIRPasses/PointerTypeAnalysis.cpp      |  8 +++++++-
 llvm/test/CodeGen/DirectX/empty-global-ctors.ll  | 16 ++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/CodeGen/DirectX/empty-global-ctors.ll

diff --git a/llvm/lib/Target/DirectX/DirectXIRPasses/PointerTypeAnalysis.cpp b/llvm/lib/Target/DirectX/DirectXIRPasses/PointerTypeAnalysis.cpp
index c2e139edc6bd1..4309e29e1440d 100644
--- a/llvm/lib/Target/DirectX/DirectXIRPasses/PointerTypeAnalysis.cpp
+++ b/llvm/lib/Target/DirectX/DirectXIRPasses/PointerTypeAnalysis.cpp
@@ -193,7 +193,13 @@ static Type *classifyConstantWithOpaquePtr(const Constant *C,
 
 static void classifyGlobalCtorPointerType(const GlobalVariable &GV,
                                           PointerTypeMap &Map) {
-  const auto *CA = cast<ConstantArray>(GV.getInitializer());
+  const auto *CA = dyn_cast<ConstantArray>(GV.getInitializer());
+  if (!CA) {
+    // An empty global_ctors will be a zeroinitializer, so just skip it.
+    assert(isa<ConstantAggregateZero>(GV.getInitializer()) &&
+           "global_ctors should be a ConstantArray or ConstantAggregateZero");
+    return;
+  }
   // Type for global ctor should be array of { i32, void ()*, i8* }.
   Type *CtorArrayTy = classifyConstantWithOpaquePtr(CA, Map);
 
diff --git a/llvm/test/CodeGen/DirectX/empty-global-ctors.ll b/llvm/test/CodeGen/DirectX/empty-global-ctors.ll
new file mode 100644
index 0000000000000..4c1590a0a0953
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/empty-global-ctors.ll
@@ -0,0 +1,16 @@
+; RUN: opt -S -dxil-prepare < %s | FileCheck %s
+
+; Test that dxil-prepare handles llvm.global_ctors with zeroinitializer
+; (which is not a ConstantArray) without crashing.
+; Fixes https://github.com/llvm/llvm-project/issues/178993
+
+target triple = "dxil-unknown-shadermodel6.7-library"
+
+; An empty global_ctors array uses zeroinitializer, not ConstantArray
+ at llvm.global_ctors = appending global [0 x { i32, ptr, ptr }] zeroinitializer
+
+; CHECK: define void @main()
+define void @main() {
+entry:
+  ret void
+}



More information about the llvm-commits mailing list