[DirectX] Fix assertion in PointerTypeAnalysis with empty global_ctors#179034
[DirectX] Fix assertion in PointerTypeAnalysis with empty global_ctors#179034mugiwaraluffy56 wants to merge 1 commit intollvm:mainfrom
Conversation
|
@llvm/pr-subscribers-backend-directx Author: puneeth_aditya_5656 (mugiwaraluffy56) ChangesWhen > "cast<Ty>() argument of incompatible type!" This patch uses Fixes #178993. Full diff: https://github.com/llvm/llvm-project/pull/179034.diff 2 Files Affected:
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
+@llvm.global_ctors = appending global [0 x { i32, ptr, ptr }] zeroinitializer
+
+; CHECK: define void @main()
+define void @main() {
+entry:
+ ret void
+}
|
|
@bogner @EugeneZelenko can you review this. |
| if (!CA) | ||
| return; |
There was a problem hiding this comment.
Should we assert that the initializer isa<ConstantAggregateZero> in the case where we return here? It doesn't make sense for global_ctors to be anything other than these two options, so an assert would document that assumption, and make it obvious that this code needs to change if that were to ever change for some reason.
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 llvm#178993.
9310edb to
8c0c15d
Compare
🐧 Linux x64 Test Results
Failed Tests(click on a test name to see its output) lldb-apilldb-api.tools/lldb-dap/stopped-events/TestDAP_stopped_events.pyIf these failures are unrelated to your changes (for example tests are broken or flaky at HEAD), please open an issue at https://github.com/llvm/llvm-project/issues and add the |
When
llvm.global_ctorshas no elements (e.g., when all resources are unused in a shader library), its initializer is azeroinitializer(ConstantAggregateZero) rather than aConstantArray. The previous code usedcast<ConstantArray>which asserts on incompatible types:This patch uses
dyn_castand returns early if the initializer is not aConstantArray, handling the edge case gracefully.Fixes #178993.