[llvm] [AMDGPU] Fix crash when a MachineFunction is not generated (PR #87727)

Shilei Tian via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 4 16:37:04 PDT 2024


https://github.com/shiltian created https://github.com/llvm/llvm-project/pull/87727

The `AMDGPUResourceUsageAnalysis` pass assumes the compile generates
MachineFunction for each function in a module, which is not always true. The
CodeGen pass skips an internal unreachable function. In this patch we simply
skip the function that is not generated because we don't need to count its
resource usage anyway.

Fix #65188.


>From 688a5eb42f63a230c11c94d53f9c927082e2111b Mon Sep 17 00:00:00 2001
From: Shilei Tian <i at tianshilei.me>
Date: Thu, 4 Apr 2024 19:32:09 -0400
Subject: [PATCH] [AMDGPU] Fix crash when a MachineFunction is not generated

The `AMDGPUResourceUsageAnalysis` pass assumes the compile generates
MachineFunction for each function in a module, which is not always true. The
CodeGen pass skips an internal unreachable function. In this patch we simply
skip the function that is not generated because we don't need to count its
resource usage anyway.

Fix #65188.
---
 llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp                | 5 +++++
 llvm/lib/Target/AMDGPU/AMDGPUResourceUsageAnalysis.cpp     | 5 ++++-
 llvm/test/CodeGen/AMDGPU/machine-function-not-generated.ll | 5 +++++
 3 files changed, 14 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/CodeGen/AMDGPU/machine-function-not-generated.ll

diff --git a/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp b/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
index 052b231d62a3eb..6052b5761d61c7 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
@@ -471,6 +471,11 @@ AMDGPUAsmPrinter::getAmdhsaKernelDescriptor(const MachineFunction &MF,
 }
 
 bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
+  // It is possible that an unreachable function is not emitted so we don't need
+  // to print it anyway.
+  if (MF.empty())
+    return false;
+
   // Init target streamer lazily on the first function so that previous passes
   // can set metadata.
   if (!IsTargetStreamerInitialized)
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUResourceUsageAnalysis.cpp b/llvm/lib/Target/AMDGPU/AMDGPUResourceUsageAnalysis.cpp
index 326d0fa58dd15f..ed3c1aa6009aa1 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUResourceUsageAnalysis.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUResourceUsageAnalysis.cpp
@@ -155,7 +155,10 @@ bool AMDGPUResourceUsageAnalysis::runOnModule(Module &M) {
 
     SIFunctionResourceInfo &Info = CI.first->second;
     MachineFunction *MF = MMI.getMachineFunction(*F);
-    assert(MF && "function must have been generated already");
+    // It's possible that the unreachable function is not generated at all. We
+    // don't need to count resource usage in that case anyway.
+    if (!MF)
+      continue;
     Info = analyzeResourceUsage(*MF, TM, AssumedStackSizeForDynamicSizeObjects,
                                 AssumedStackSizeForExternalCall);
     HasIndirectCall |= Info.HasIndirectCall;
diff --git a/llvm/test/CodeGen/AMDGPU/machine-function-not-generated.ll b/llvm/test/CodeGen/AMDGPU/machine-function-not-generated.ll
new file mode 100644
index 00000000000000..e67ca1a1ed2a66
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/machine-function-not-generated.ll
@@ -0,0 +1,5 @@
+; RUN: llc --mtriple=amdgcn-amd-amdhsa %s -o -
+
+define internal void @_omp_reduction_list_to_global_reduce_func() {
+  ret void
+}



More information about the llvm-commits mailing list