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

via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 31 05:32:42 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-directx

Author: puneeth_aditya_5656 (mugiwaraluffy56)

<details>
<summary>Changes</summary>

When `llvm.global_ctors` has no elements (e.g., when all resources are unused in a shader library), its initializer is a `zeroinitializer` (`ConstantAggregateZero`) rather than a `ConstantArray`. The previous code used `cast<ConstantArray>` which asserts on incompatible types:

> "cast<Ty>() argument of incompatible type!"

This patch uses `dyn_cast` and returns early if the initializer is not a `ConstantArray`, handling the edge case gracefully.

Fixes #<!-- -->178993.

---
Full diff: https://github.com/llvm/llvm-project/pull/179034.diff


2 Files Affected:

- (modified) llvm/lib/Target/DirectX/DirectXIRPasses/PointerTypeAnalysis.cpp (+3-1) 
- (added) llvm/test/CodeGen/DirectX/empty-global-ctors.ll (+16) 


``````````diff
diff --git a/llvm/lib/Target/DirectX/DirectXIRPasses/PointerTypeAnalysis.cpp b/llvm/lib/Target/DirectX/DirectXIRPasses/PointerTypeAnalysis.cpp
index c2e139edc6bd1..ee49fa816d9b7 100644
--- a/llvm/lib/Target/DirectX/DirectXIRPasses/PointerTypeAnalysis.cpp
+++ b/llvm/lib/Target/DirectX/DirectXIRPasses/PointerTypeAnalysis.cpp
@@ -193,7 +193,9 @@ 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)
+    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
+}

``````````

</details>


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


More information about the llvm-commits mailing list