Skip to content

Commit 3464b06

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 5c27511 commit 3464b06

File tree

6 files changed

+182
-53
lines changed

6 files changed

+182
-53
lines changed

llvm/include/llvm/IR/CallingConv.h

+18
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,24 @@ namespace CallingConv {
290290

291291
} // end namespace CallingConv
292292

293+
/// \return true if the calling convention allows the function to be called
294+
/// directly or indirectly via a call-like instruction.
295+
constexpr bool isCallableCC(CallingConv::ID CC) {
296+
switch (CC) {
297+
case CallingConv::AMDGPU_KERNEL:
298+
case CallingConv::AMDGPU_VS:
299+
case CallingConv::AMDGPU_GS:
300+
case CallingConv::AMDGPU_PS:
301+
case CallingConv::AMDGPU_CS:
302+
case CallingConv::AMDGPU_ES:
303+
case CallingConv::AMDGPU_HS:
304+
case CallingConv::AMDGPU_LS:
305+
return false;
306+
default:
307+
return true;
308+
}
309+
}
310+
293311
} // end namespace llvm
294312

295313
#endif // LLVM_IR_CALLINGCONV_H

llvm/lib/IR/Verifier.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -3769,6 +3769,9 @@ void Verifier::visitCallBase(CallBase &Call) {
37693769
if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID())
37703770
visitIntrinsicCall(ID, Call);
37713771

3772+
// Verify if the calling convention of the callee is callable.
3773+
Check(isCallableCC(Call.getCallingConv()), "Callee is not callable");
3774+
37723775
// Verify that a callsite has at most one "deopt", at most one "funclet", at
37733776
// most one "gc-transition", at most one "cfguardtarget", at most one
37743777
// "preallocated" operand bundle, and at most one "ptrauth" operand bundle.

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

-15
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

-20
This file was deleted.

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

-18
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
; RUN: not llvm-as %s -o /dev/null 2>&1 | FileCheck %s
2+
3+
define amdgpu_kernel void @callee_kernel(ptr addrspace(1) %out) {
4+
entry:
5+
store volatile i32 0, ptr addrspace(1) %out
6+
ret void
7+
}
8+
9+
define amdgpu_vs void @callee_vs(ptr addrspace(1) inreg %out) {
10+
entry:
11+
store volatile i32 0, ptr addrspace(1) %out
12+
ret void
13+
}
14+
15+
define amdgpu_gs void @callee_gs(ptr addrspace(1) %out) {
16+
entry:
17+
store volatile i32 0, ptr addrspace(1) %out
18+
ret void
19+
}
20+
21+
define amdgpu_ps void @callee_ps(ptr addrspace(1) %out) {
22+
entry:
23+
store volatile i32 0, ptr addrspace(1) %out
24+
ret void
25+
}
26+
27+
define amdgpu_cs void @callee_cs(ptr addrspace(1) %out) {
28+
entry:
29+
store volatile i32 0, ptr addrspace(1) %out
30+
ret void
31+
}
32+
33+
define amdgpu_es void @callee_es(ptr addrspace(1) %out) {
34+
entry:
35+
store volatile i32 0, ptr addrspace(1) %out
36+
ret void
37+
}
38+
39+
define amdgpu_hs void @callee_hs(ptr addrspace(1) %out) {
40+
entry:
41+
store volatile i32 0, ptr addrspace(1) %out
42+
ret void
43+
}
44+
45+
define amdgpu_ls void @callee_ls(ptr addrspace(1) %out) {
46+
entry:
47+
store volatile i32 0, ptr addrspace(1) %out
48+
ret void
49+
}
50+
51+
; CHECK: Callee is not callable
52+
define amdgpu_kernel void @caller_kernel(ptr addrspace(1) %out) {
53+
entry:
54+
call amdgpu_kernel void @callee_kernel(ptr addrspace(1) %out)
55+
ret void
56+
}
57+
58+
; CHECK: Callee is not callable
59+
define amdgpu_vs void @caller_vs(ptr addrspace(1) inreg %out) {
60+
entry:
61+
call amdgpu_vs void @callee_vs(ptr addrspace(1) %out)
62+
ret void
63+
}
64+
65+
; CHECK: Callee is not callable
66+
define amdgpu_gs void @caller_gs(ptr addrspace(1) %out) {
67+
entry:
68+
call amdgpu_gs void @callee_gs(ptr addrspace(1) %out)
69+
ret void
70+
}
71+
72+
; CHECK: Callee is not callable
73+
define amdgpu_ps void @caller_ps(ptr addrspace(1) %out) {
74+
entry:
75+
call amdgpu_ps void @callee_ps(ptr addrspace(1) %out)
76+
ret void
77+
}
78+
79+
; CHECK: Callee is not callable
80+
define amdgpu_cs void @caller_cs(ptr addrspace(1) %out) {
81+
entry:
82+
call amdgpu_cs void @callee_cs(ptr addrspace(1) %out)
83+
ret void
84+
}
85+
86+
; CHECK: Callee is not callable
87+
define amdgpu_es void @caller_es(ptr addrspace(1) %out) {
88+
entry:
89+
call amdgpu_es void @callee_es(ptr addrspace(1) %out)
90+
ret void
91+
}
92+
93+
; CHECK: Callee is not callable
94+
define amdgpu_hs void @caller_hs(ptr addrspace(1) %out) {
95+
entry:
96+
call amdgpu_hs void @callee_hs(ptr addrspace(1) %out)
97+
ret void
98+
}
99+
100+
; CHECK: Callee is not callable
101+
define amdgpu_ls void @caller_ls(ptr addrspace(1) %out) {
102+
entry:
103+
call amdgpu_ls void @callee_ls(ptr addrspace(1) %out)
104+
ret void
105+
}
106+
107+
; CHECK: Callee is not callable
108+
define void @indirect_caller_kernel(ptr %func) {
109+
entry:
110+
call amdgpu_kernel void %func()
111+
ret void
112+
}
113+
114+
; CHECK: Callee is not callable
115+
define void @indirect_caller_vs(ptr %func) {
116+
entry:
117+
call amdgpu_vs void %func()
118+
ret void
119+
}
120+
121+
; CHECK: Callee is not callable
122+
define void @indirect_caller_gs(ptr %func) {
123+
entry:
124+
call amdgpu_gs void %func()
125+
ret void
126+
}
127+
128+
; CHECK: Callee is not callable
129+
define void @indirect_caller_ps(ptr %func) {
130+
entry:
131+
call amdgpu_ps void %func()
132+
ret void
133+
}
134+
135+
; CHECK: Callee is not callable
136+
define void @indirect_caller_cs(ptr %func) {
137+
entry:
138+
call amdgpu_cs void %func()
139+
ret void
140+
}
141+
142+
; CHECK: Callee is not callable
143+
define void @indirect_caller_es(ptr %func) {
144+
entry:
145+
call amdgpu_es void %func()
146+
ret void
147+
}
148+
149+
; CHECK: Callee is not callable
150+
define void @indirect_caller_hs(ptr %func) {
151+
entry:
152+
call amdgpu_hs void %func()
153+
ret void
154+
}
155+
156+
; CHECK: Callee is not callable
157+
define void @indirect_caller_ls(ptr %func) {
158+
entry:
159+
call amdgpu_ls void %func()
160+
ret void
161+
}

0 commit comments

Comments
 (0)