[amdgpu] Don't crash on empty global ctor/dtor

Global ctor/dtor can be an empty array, which is a Constant not a
ConstantArray. The cast<ConstantArray> therefore asserts / crashes.

Reviewed By: arsenm

Differential Revision: https://reviews.llvm.org/D113800
This commit is contained in:
Jon Chesterfield 2021-11-16 14:36:06 +00:00
parent 8fce94f916
commit 30b29db7c7
2 changed files with 11 additions and 2 deletions

View File

@ -52,8 +52,8 @@ public:
bool createInitOrFiniKernel(Module &M, GlobalVariable *GV, bool IsCtor) {
if (!GV)
return false;
ConstantArray *GA = cast<ConstantArray>(GV->getInitializer());
if (GA->getNumOperands() == 0)
ConstantArray *GA = dyn_cast<ConstantArray>(GV->getInitializer());
if (!GA || GA->getNumOperands() == 0)
return false;
Function *InitOrFiniKernel = createInitOrFiniKernelFunction(M, IsCtor);
IRBuilder<> IRB(InitOrFiniKernel->getEntryBlock().getTerminator());

View File

@ -0,0 +1,9 @@
; RUN: opt -S -mtriple=amdgcn-- -amdgpu-lower-ctor-dtor < %s | FileCheck %s
; RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx700 -filetype=obj -o - < %s | llvm-readelf -s - 2>&1 | FileCheck %s
@llvm.global_ctors = appending global [0 x { i32, void ()*, i8* }] zeroinitializer
@llvm.global_dtors = appending global [0 x { i32, void ()*, i8* }] zeroinitializer
; No amdgpu_kernels emitted for empty global_ctors
; CHECK-NOT: amdgcn.device.init
; CHECK-NOT: amdgcn.device.fini