Skip to content

Commit be71146

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 5978bb2 commit be71146

File tree

7 files changed

+553
-84
lines changed

7 files changed

+553
-84
lines changed

llvm/include/llvm/IR/CallingConv.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,27 @@ 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_CS_Chain:
298+
case CallingConv::AMDGPU_CS_ChainPreserve:
299+
case CallingConv::AMDGPU_CS:
300+
case CallingConv::AMDGPU_ES:
301+
case CallingConv::AMDGPU_GS:
302+
case CallingConv::AMDGPU_HS:
303+
case CallingConv::AMDGPU_KERNEL:
304+
case CallingConv::AMDGPU_LS:
305+
case CallingConv::AMDGPU_PS:
306+
case CallingConv::AMDGPU_VS:
307+
case CallingConv::SPIR_KERNEL:
308+
return false;
309+
default:
310+
return true;
311+
}
312+
}
313+
293314
} // end namespace llvm
294315

295316
#endif // LLVM_IR_CALLINGCONV_H

llvm/lib/IR/Verifier.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3596,14 +3596,9 @@ void Verifier::visitCallBase(CallBase &Call) {
35963596
Check(Callee->getValueType() == FTy,
35973597
"Intrinsic called with incompatible signature", Call);
35983598

3599-
// Disallow calls to functions with the amdgpu_cs_chain[_preserve] calling
3600-
// convention.
3601-
auto CC = Call.getCallingConv();
3602-
Check(CC != CallingConv::AMDGPU_CS_Chain &&
3603-
CC != CallingConv::AMDGPU_CS_ChainPreserve,
3604-
"Direct calls to amdgpu_cs_chain/amdgpu_cs_chain_preserve functions "
3605-
"not allowed. Please use the @llvm.amdgpu.cs.chain intrinsic instead.",
3606-
Call);
3599+
// Verify if the calling convention of the callee is callable.
3600+
Check(isCallableCC(Call.getCallingConv()),
3601+
"calling convention does not permit calls", Call);
36073602

36083603
// Disallow passing/returning values with alignment higher than we can
36093604
// represent.

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.

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

Lines changed: 0 additions & 18 deletions
This file was deleted.

llvm/test/Verifier/amdgpu-cc.ll

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -217,26 +217,3 @@ define amdgpu_cs_chain_preserve void @preallocated_cc_amdgpu_cs_chain_preserve(p
217217
define amdgpu_cs_chain_preserve void @inalloca_cc_amdgpu_cs_chain_preserve(ptr inalloca(i32) %ptr) {
218218
ret void
219219
}
220-
221-
declare amdgpu_cs_chain void @amdgpu_cs_chain_call_target()
222-
declare amdgpu_cs_chain_preserve void @amdgpu_cs_chain_preserve_call_target()
223-
224-
define amdgpu_cs_chain void @cant_call_amdgpu_cs_chain_functions(ptr %f) {
225-
; CHECK: Direct calls to amdgpu_cs_chain/amdgpu_cs_chain_preserve functions not allowed. Please use the @llvm.amdgpu.cs.chain intrinsic instead.
226-
; CHECK-NEXT: call amdgpu_cs_chain
227-
call amdgpu_cs_chain void @amdgpu_cs_chain_call_target()
228-
229-
; CHECK: Direct calls to amdgpu_cs_chain/amdgpu_cs_chain_preserve functions not allowed. Please use the @llvm.amdgpu.cs.chain intrinsic instead.
230-
; CHECK-NEXT: call amdgpu_cs_chain_preserve
231-
call amdgpu_cs_chain_preserve void @amdgpu_cs_chain_preserve_call_target()
232-
233-
; CHECK: Direct calls to amdgpu_cs_chain/amdgpu_cs_chain_preserve functions not allowed. Please use the @llvm.amdgpu.cs.chain intrinsic instead.
234-
; CHECK-NEXT: call amdgpu_cs_chain
235-
call amdgpu_cs_chain void %f()
236-
237-
; CHECK: Direct calls to amdgpu_cs_chain/amdgpu_cs_chain_preserve functions not allowed. Please use the @llvm.amdgpu.cs.chain intrinsic instead.
238-
; CHECK-NEXT: call amdgpu_cs_chain
239-
call amdgpu_cs_chain_preserve void %f()
240-
241-
ret void
242-
}

0 commit comments

Comments
 (0)