[llvm] 30b29db - [amdgpu] Don't crash on empty global ctor/dtor

Jon Chesterfield via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 16 06:36:22 PST 2021


Author: Jon Chesterfield
Date: 2021-11-16T14:36:08Z
New Revision: 30b29db7c79ce70801800572b898b2e0df03bbb1

URL: https://github.com/llvm/llvm-project/commit/30b29db7c79ce70801800572b898b2e0df03bbb1
DIFF: https://github.com/llvm/llvm-project/commit/30b29db7c79ce70801800572b898b2e0df03bbb1.diff

LOG: [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

Added: 
    llvm/test/CodeGen/AMDGPU/lower-empty-ctor-dtor.ll

Modified: 
    llvm/lib/Target/AMDGPU/AMDGPUCtorDtorLowering.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/AMDGPU/AMDGPUCtorDtorLowering.cpp b/llvm/lib/Target/AMDGPU/AMDGPUCtorDtorLowering.cpp
index 95b848e26f46..04bf623bfa46 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUCtorDtorLowering.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUCtorDtorLowering.cpp
@@ -52,8 +52,8 @@ class AMDGPUCtorDtorLowering final : public ModulePass {
   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());

diff  --git a/llvm/test/CodeGen/AMDGPU/lower-empty-ctor-dtor.ll b/llvm/test/CodeGen/AMDGPU/lower-empty-ctor-dtor.ll
new file mode 100644
index 000000000000..0657ad78ff57
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/lower-empty-ctor-dtor.ll
@@ -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
+
+ at llvm.global_ctors = appending global [0 x { i32, void ()*, i8* }] zeroinitializer
+ at 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


        


More information about the llvm-commits mailing list