[llvm] r355049 - AMDGPU: Fix crashes in invalid call cases

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 27 16:28:44 PST 2019


Author: arsenm
Date: Wed Feb 27 16:28:44 2019
New Revision: 355049

URL: http://llvm.org/viewvc/llvm-project?rev=355049&view=rev
Log:
AMDGPU: Fix crashes in invalid call cases

We have to at least tolerate calls to kernels, possibly with a
mismatched calling convention on the callsite.

Added:
    llvm/trunk/test/CodeGen/AMDGPU/call-to-kernel-undefined.ll
    llvm/trunk/test/CodeGen/AMDGPU/call-to-kernel.ll
Modified:
    llvm/trunk/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
    llvm/trunk/lib/Target/AMDGPU/AMDGPUISelLowering.cpp
    llvm/trunk/test/CodeGen/AMDGPU/inline-calls.ll

Modified: llvm/trunk/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp?rev=355049&r1=355048&r2=355049&view=diff
==============================================================================
--- llvm/trunk/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp (original)
+++ llvm/trunk/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp Wed Feb 27 16:28:44 2019
@@ -773,9 +773,19 @@ AMDGPUAsmPrinter::SIFunctionResourceInfo
         } else {
           // We force CodeGen to run in SCC order, so the callee's register
           // usage etc. should be the cumulative usage of all callees.
+
           auto I = CallGraphResourceInfo.find(Callee);
-          assert(I != CallGraphResourceInfo.end() &&
-                 "callee should have been handled before caller");
+          if (I == CallGraphResourceInfo.end()) {
+            // Avoid crashing on undefined behavior with an illegal call to a
+            // kernel. If a callsite's calling convention doesn't match the
+            // function's, it's undefined behavior. If the callsite calling
+            // convention does match, that would have errored earlier.
+            // FIXME: The verifier shouldn't allow this.
+            if (AMDGPU::isEntryFunctionCC(Callee->getCallingConv()))
+              report_fatal_error("invalid call to entry function");
+
+            llvm_unreachable("callee should have been handled before caller");
+          }
 
           MaxSGPR = std::max(I->second.NumExplicitSGPR - 1, MaxSGPR);
           MaxVGPR = std::max(I->second.NumVGPR - 1, MaxVGPR);

Modified: llvm/trunk/lib/Target/AMDGPU/AMDGPUISelLowering.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/AMDGPU/AMDGPUISelLowering.cpp?rev=355049&r1=355048&r2=355049&view=diff
==============================================================================
--- llvm/trunk/lib/Target/AMDGPU/AMDGPUISelLowering.cpp (original)
+++ llvm/trunk/lib/Target/AMDGPU/AMDGPUISelLowering.cpp Wed Feb 27 16:28:44 2019
@@ -848,9 +848,6 @@ bool AMDGPUTargetLowering::isNarrowingPr
 CCAssignFn *AMDGPUCallLowering::CCAssignFnForCall(CallingConv::ID CC,
                                                   bool IsVarArg) {
   switch (CC) {
-  case CallingConv::AMDGPU_KERNEL:
-  case CallingConv::SPIR_KERNEL:
-    llvm_unreachable("kernels should not be handled here");
   case CallingConv::AMDGPU_VS:
   case CallingConv::AMDGPU_GS:
   case CallingConv::AMDGPU_PS:
@@ -863,8 +860,10 @@ CCAssignFn *AMDGPUCallLowering::CCAssign
   case CallingConv::Fast:
   case CallingConv::Cold:
     return CC_AMDGPU_Func;
+  case CallingConv::AMDGPU_KERNEL:
+  case CallingConv::SPIR_KERNEL:
   default:
-    report_fatal_error("Unsupported calling convention.");
+    report_fatal_error("Unsupported calling convention for call");
   }
 }
 

Added: llvm/trunk/test/CodeGen/AMDGPU/call-to-kernel-undefined.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/AMDGPU/call-to-kernel-undefined.ll?rev=355049&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/AMDGPU/call-to-kernel-undefined.ll (added)
+++ llvm/trunk/test/CodeGen/AMDGPU/call-to-kernel-undefined.ll Wed Feb 27 16:28:44 2019
@@ -0,0 +1,20 @@
+; RUN: not llc -march=amdgcn -mcpu=tahiti -verify-machineinstrs -o /dev/null %s 2>&1 | FileCheck %s
+
+; FIXME: It should be invalid IR to have a call to a kernel, but this
+; is currently relied on, but should be eliminated before codegen.
+define amdgpu_kernel void @callee_kernel(i32 addrspace(1)* %out) #0 {
+entry:
+  store volatile i32 0, i32 addrspace(1)* %out
+  ret void
+}
+
+; Make sure there's no crash when the callsite calling convention
+; doesn't match.
+; CHECK: LLVM ERROR: invalid call to entry function
+define amdgpu_kernel void @caller_kernel(i32 addrspace(1)* %out) #0 {
+entry:
+  call void @callee_kernel(i32 addrspace(1)* %out)
+  ret void
+}
+
+attributes #0 = { nounwind noinline }

Added: llvm/trunk/test/CodeGen/AMDGPU/call-to-kernel.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/AMDGPU/call-to-kernel.ll?rev=355049&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/AMDGPU/call-to-kernel.ll (added)
+++ llvm/trunk/test/CodeGen/AMDGPU/call-to-kernel.ll Wed Feb 27 16:28:44 2019
@@ -0,0 +1,18 @@
+; RUN: not llc -march=amdgcn -mcpu=tahiti -verify-machineinstrs -o /dev/null %s 2>&1 | FileCheck %s
+
+; FIXME: It should be invalid IR to have a call to a kernel, but this
+; is currently relied on, but should be eliminated before codegen.
+define amdgpu_kernel void @callee_kernel(i32 addrspace(1)* %out) #0 {
+entry:
+  store volatile i32 0, i32 addrspace(1)* %out
+  ret void
+}
+
+; CHECK: LLVM ERROR: Unsupported calling convention for call
+define amdgpu_kernel void @caller_kernel(i32 addrspace(1)* %out) #0 {
+entry:
+  call amdgpu_kernel void @callee_kernel(i32 addrspace(1)* %out)
+  ret void
+}
+
+attributes #0 = { nounwind noinline }

Modified: llvm/trunk/test/CodeGen/AMDGPU/inline-calls.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/AMDGPU/inline-calls.ll?rev=355049&r1=355048&r2=355049&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/AMDGPU/inline-calls.ll (original)
+++ llvm/trunk/test/CodeGen/AMDGPU/inline-calls.ll Wed Feb 27 16:28:44 2019
@@ -3,7 +3,7 @@
 ; RUN: llc -march=r600 -mcpu=redwood -verify-machineinstrs < %s | FileCheck %s
 
 ; CHECK-NOT: {{^}}func:
-define internal fastcc i32 @func(i32 %a) {
+define internal i32 @func(i32 %a) {
 entry:
   %tmp0 = add i32 %a, 1
   ret i32 %tmp0
@@ -18,14 +18,6 @@ entry:
   ret void
 }
 
-; CHECK: {{^}}kernel2:
-; CHECK-NOT: call
-define amdgpu_kernel void @kernel2(i32 addrspace(1)* %out) {
-entry:
-  call void @kernel(i32 addrspace(1)* %out)
-  ret void
-}
-
 ; CHECK-NOT: func_alias
 @func_alias = alias i32 (i32), i32 (i32)* @func
 
@@ -37,14 +29,3 @@ entry:
   store i32 %tmp0, i32 addrspace(1)* %out
   ret void
 }
-
-; CHECK-NOT: kernel_alias
- at kernel_alias = alias void (i32 addrspace(1)*), void (i32 addrspace(1)*)* @kernel
-
-; CHECK: {{^}}kernel4:
-; CHECK-NOT: call
-define amdgpu_kernel void @kernel4(i32 addrspace(1)* %out) {
-entry:
-  call void @kernel_alias(i32 addrspace(1)* %out)
-  ret void
-}




More information about the llvm-commits mailing list