Index: llvm/lib/Target/AMDGPU/AMDGPULowerModuleLDSPass.cpp =================================================================== --- llvm/lib/Target/AMDGPU/AMDGPULowerModuleLDSPass.cpp +++ llvm/lib/Target/AMDGPU/AMDGPULowerModuleLDSPass.cpp @@ -163,6 +163,9 @@ 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 @@ if (!F) { IRBuilder<> Builder(Ctx); SmallPtrSet 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: llvm/test/CodeGen/AMDGPU/lower-module-lds.ll =================================================================== --- llvm/test/CodeGen/AMDGPU/lower-module-lds.ll +++ llvm/test/CodeGen/AMDGPU/lower-module-lds.ll @@ -54,3 +54,7 @@ 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()