AMDGPU: Fix crashing on kernel declarations when lowering LDS
authorMatt Arsenault <Matthew.Arsenault@amd.com>
Thu, 26 Aug 2021 21:41:33 +0000 (17:41 -0400)
committerMatt Arsenault <Matthew.Arsenault@amd.com>
Thu, 26 Aug 2021 23:01:10 +0000 (19:01 -0400)
This was trying to insert the used marker into a declaration.

llvm/lib/Target/AMDGPU/AMDGPULowerModuleLDSPass.cpp
llvm/test/CodeGen/AMDGPU/lower-module-lds.ll

index 26e2b5f..02bb3c4 100644 (file)
@@ -163,6 +163,9 @@ public:
     bool Changed = processUsedLDS(M);
 
     for (Function &F : M.functions()) {
+      if (F.isDeclaration())
+        continue;
+
       // Only lower compute kernels' LDS.
       if (!AMDGPU::isKernel(F.getCallingConv()))
         continue;
@@ -347,11 +350,13 @@ private:
     if (!F) {
       IRBuilder<> Builder(Ctx);
       SmallPtrSet<Function *, 32> Kernels;
-      for (auto &I : M.functions()) {
-        Function *Func = &I;
-        if (AMDGPU::isKernelCC(Func) && !Kernels.contains(Func)) {
-          markUsedByKernel(Builder, Func, SGV);
-          Kernels.insert(Func);
+      for (Function &Func : M.functions()) {
+        if (Func.isDeclaration())
+          continue;
+
+        if (AMDGPU::isKernelCC(&Func) && !Kernels.contains(&Func)) {
+          markUsedByKernel(Builder, &Func, SGV);
+          Kernels.insert(&Func);
         }
       }
     }
index e92d862..a620eb8 100644 (file)
@@ -54,3 +54,7 @@ define amdgpu_kernel void @kern_call() {
 define spir_kernel void @kern_empty() {
   ret void
 }
+
+; Make sure we don't crash trying to insert code into a kernel
+; declaration.
+declare amdgpu_kernel void @kernel_declaration()