Skip to content

Commit dc71454

Browse files
committed
[AMDGPU][Verifier] Mark calls to entry functions as invalid in the IR verifier
For AMDGPU, calls to entry functions are invalid. Previously, due to certain limitations, this restriction was not enforced by the IR verifier. These limitations have now been resolved, enabling us to enforce this check. Adding target-dependent checks directly into the IR verifier is not ideal. However, a cleaner solution, such as a dedicated target-dependent IR verifier, is underway (e.g., #123609). Once that or similar code is merged, we can move this check accordingly.
1 parent 661f90a commit dc71454

File tree

4 files changed

+115
-44
lines changed

4 files changed

+115
-44
lines changed

llvm/lib/IR/Verifier.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3765,9 +3765,28 @@ void Verifier::visitCallBase(CallBase &Call) {
37653765
"Return type cannot be x86_amx for indirect call!");
37663766
}
37673767

3768-
if (Function *F = Call.getCalledFunction())
3768+
if (Function *F = Call.getCalledFunction()) {
37693769
if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID())
37703770
visitIntrinsicCall(ID, Call);
3771+
// TODO: Move this to target dependent IR verifier once we have it.
3772+
auto IsAMDGPUEntryFunctionCC = [](CallingConv::ID CC) -> bool {
3773+
switch (CC) {
3774+
case CallingConv::AMDGPU_KERNEL:
3775+
case CallingConv::AMDGPU_VS:
3776+
case CallingConv::AMDGPU_GS:
3777+
case CallingConv::AMDGPU_PS:
3778+
case CallingConv::AMDGPU_CS:
3779+
case CallingConv::AMDGPU_ES:
3780+
case CallingConv::AMDGPU_HS:
3781+
case CallingConv::AMDGPU_LS:
3782+
return true;
3783+
default:
3784+
return false;
3785+
}
3786+
};
3787+
Check(!IsAMDGPUEntryFunctionCC(F->getCallingConv()),
3788+
"Call to amdgpu entry function is not allowed");
3789+
}
37713790

37723791
// Verify that a callsite has at most one "deopt", at most one "funclet", at
37733792
// most one "gc-transition", at most one "cfguardtarget", at most one

llvm/test/CodeGen/AMDGPU/attributor-flatscratchinit.ll

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -849,21 +849,6 @@ define amdgpu_kernel void @calls_intrin_ascast_cc_kernel(ptr addrspace(3) %ptr)
849849
ret void
850850
}
851851

852-
define amdgpu_kernel void @call_calls_intrin_ascast_cc_kernel(ptr addrspace(3) %ptr) {
853-
; GFX9-LABEL: define amdgpu_kernel void @call_calls_intrin_ascast_cc_kernel(
854-
; GFX9-SAME: ptr addrspace(3) [[PTR:%.*]]) #[[ATTR1]] {
855-
; GFX9-NEXT: call void @calls_intrin_ascast_cc_kernel(ptr addrspace(3) [[PTR]])
856-
; GFX9-NEXT: ret void
857-
;
858-
; GFX10-LABEL: define amdgpu_kernel void @call_calls_intrin_ascast_cc_kernel(
859-
; GFX10-SAME: ptr addrspace(3) [[PTR:%.*]]) #[[ATTR1]] {
860-
; GFX10-NEXT: call void @calls_intrin_ascast_cc_kernel(ptr addrspace(3) [[PTR]])
861-
; GFX10-NEXT: ret void
862-
;
863-
call void @calls_intrin_ascast_cc_kernel(ptr addrspace(3) %ptr)
864-
ret void
865-
}
866-
867852
define amdgpu_kernel void @with_inline_asm() {
868853
; GFX9-LABEL: define amdgpu_kernel void @with_inline_asm(
869854
; GFX9-SAME: ) #[[ATTR3]] {

llvm/test/CodeGen/AMDGPU/call-to-kernel-undefined.ll

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 95 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,105 @@
1-
; RUN: not --crash llc -mtriple=amdgcn -mcpu=tahiti -verify-machineinstrs -o /dev/null %s 2>&1 | FileCheck %s
1+
; RUN: not llvm-as %s -o /dev/null 2>&1 | FileCheck %s
22

3-
; FIXME: It should be invalid IR to have a call to a kernel, but this
4-
; is currently relied on, but should be eliminated before codegen.
5-
define amdgpu_kernel void @callee_kernel(ptr addrspace(1) %out) #0 {
3+
; CHECK: Call to amdgpu entry function is not allowed
4+
define amdgpu_kernel void @callee_kernel(ptr addrspace(1) %out) {
65
entry:
76
store volatile i32 0, ptr addrspace(1) %out
87
ret void
98
}
109

11-
; CHECK: LLVM ERROR: Unsupported calling convention for call
12-
define amdgpu_kernel void @caller_kernel(ptr addrspace(1) %out) #0 {
10+
define amdgpu_kernel void @caller_kernel(ptr addrspace(1) %out) {
1311
entry:
14-
call amdgpu_kernel void @callee_kernel(ptr addrspace(1) %out)
12+
call void @callee_kernel(ptr addrspace(1) %out)
1513
ret void
1614
}
1715

18-
attributes #0 = { nounwind noinline }
16+
; CHECK: Call to amdgpu entry function is not allowed
17+
define amdgpu_vs void @callee_vs(ptr addrspace(1) inreg %out) {
18+
entry:
19+
store volatile i32 0, ptr addrspace(1) %out
20+
ret void
21+
}
22+
23+
define amdgpu_vs void @caller_vs(ptr addrspace(1) inreg %out) {
24+
entry:
25+
call void @callee_vs(ptr addrspace(1) %out)
26+
ret void
27+
}
28+
29+
; CHECK: Call to amdgpu entry function is not allowed
30+
define amdgpu_gs void @callee_gs(ptr addrspace(1) %out) {
31+
entry:
32+
store volatile i32 0, ptr addrspace(1) %out
33+
ret void
34+
}
35+
36+
define amdgpu_gs void @caller_gs(ptr addrspace(1) %out) {
37+
entry:
38+
call void @callee_gs(ptr addrspace(1) %out)
39+
ret void
40+
}
41+
42+
; CHECK: Call to amdgpu entry function is not allowed
43+
define amdgpu_ps void @callee_ps(ptr addrspace(1) %out) {
44+
entry:
45+
store volatile i32 0, ptr addrspace(1) %out
46+
ret void
47+
}
48+
49+
define amdgpu_ps void @caller_ps(ptr addrspace(1) %out) {
50+
entry:
51+
call void @callee_ps(ptr addrspace(1) %out)
52+
ret void
53+
}
54+
55+
; CHECK: Call to amdgpu entry function is not allowed
56+
define amdgpu_cs void @callee_cs(ptr addrspace(1) %out) {
57+
entry:
58+
store volatile i32 0, ptr addrspace(1) %out
59+
ret void
60+
}
61+
62+
define amdgpu_cs void @caller_cs(ptr addrspace(1) %out) {
63+
entry:
64+
call void @callee_cs(ptr addrspace(1) %out)
65+
ret void
66+
}
67+
68+
; CHECK: Call to amdgpu entry function is not allowed
69+
define amdgpu_es void @callee_es(ptr addrspace(1) %out) {
70+
entry:
71+
store volatile i32 0, ptr addrspace(1) %out
72+
ret void
73+
}
74+
75+
define amdgpu_es void @caller_es(ptr addrspace(1) %out) {
76+
entry:
77+
call void @callee_es(ptr addrspace(1) %out)
78+
ret void
79+
}
80+
81+
; CHECK: Call to amdgpu entry function is not allowed
82+
define amdgpu_hs void @callee_hs(ptr addrspace(1) %out) {
83+
entry:
84+
store volatile i32 0, ptr addrspace(1) %out
85+
ret void
86+
}
87+
88+
define amdgpu_hs void @caller_hs(ptr addrspace(1) %out) {
89+
entry:
90+
call void @callee_hs(ptr addrspace(1) %out)
91+
ret void
92+
}
93+
94+
; CHECK: Call to amdgpu entry function is not allowed
95+
define amdgpu_ls void @callee_ls(ptr addrspace(1) %out) {
96+
entry:
97+
store volatile i32 0, ptr addrspace(1) %out
98+
ret void
99+
}
100+
101+
define amdgpu_ls void @caller_ls(ptr addrspace(1) %out) {
102+
entry:
103+
call void @callee_ls(ptr addrspace(1) %out)
104+
ret void
105+
}

0 commit comments

Comments
 (0)