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

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


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-amdgpu

Author: Shilei Tian (shiltian)

<details>
<summary>Changes</summary>

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.


---
Full diff: https://github.com/llvm/llvm-project/pull/87727.diff


3 Files Affected:

- (modified) llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp (+5) 
- (modified) llvm/lib/Target/AMDGPU/AMDGPUResourceUsageAnalysis.cpp (+4-1) 
- (added) llvm/test/CodeGen/AMDGPU/machine-function-not-generated.ll (+5) 


``````````diff
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
+}

``````````

</details>


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


More information about the llvm-commits mailing list