Skip to content

Commit d4216b5

Browse files
authored
[clang][CodeGen][AMDGPU] Enable AMDGPU printf for spirv64-amd-amdhsa (#97132)
This enables the AMDGPU specific implementation of `printf` when compiling for AMDGCN flavoured SPIR-V, the consequence being that the expansion into ROCDL calls & friends gets expanded before "lowering" to SPIR-V and gets carried through. The only relatively "novel" aspect is that the `callAppendStringN` is simplified to take the type of the passed in arguments, as opposed to querying them from the module. This is a neutral change since the arguments were passed directly to the call, without any attempt to cast them, hence the assumption that the actual types match the formal ones was already baked in.
1 parent 7102eae commit d4216b5

File tree

5 files changed

+222
-34
lines changed

5 files changed

+222
-34
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5888,12 +5888,16 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
58885888
case Builtin::BI__builtin_printf:
58895889
case Builtin::BIprintf:
58905890
if (getTarget().getTriple().isNVPTX() ||
5891-
getTarget().getTriple().isAMDGCN()) {
5891+
getTarget().getTriple().isAMDGCN() ||
5892+
(getTarget().getTriple().isSPIRV() &&
5893+
getTarget().getTriple().getVendor() == Triple::VendorType::AMD)) {
58925894
if (getLangOpts().OpenMPIsTargetDevice)
58935895
return EmitOpenMPDevicePrintfCallExpr(E);
58945896
if (getTarget().getTriple().isNVPTX())
58955897
return EmitNVPTXDevicePrintfCallExpr(E);
5896-
if (getTarget().getTriple().isAMDGCN() && getLangOpts().HIP)
5898+
if ((getTarget().getTriple().isAMDGCN() ||
5899+
getTarget().getTriple().isSPIRV()) &&
5900+
getLangOpts().HIP)
58975901
return EmitAMDGPUDevicePrintfCallExpr(E);
58985902
}
58995903

clang/lib/CodeGen/CGGPUBuiltin.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ RValue CodeGenFunction::EmitNVPTXDevicePrintfCallExpr(const CallExpr *E) {
179179
}
180180

181181
RValue CodeGenFunction::EmitAMDGPUDevicePrintfCallExpr(const CallExpr *E) {
182-
assert(getTarget().getTriple().getArch() == llvm::Triple::amdgcn);
182+
assert(getTarget().getTriple().isAMDGCN() ||
183+
(getTarget().getTriple().isSPIRV() &&
184+
getTarget().getTriple().getVendor() == llvm::Triple::AMD));
183185
assert(E->getBuiltinCallee() == Builtin::BIprintf ||
184186
E->getBuiltinCallee() == Builtin::BI__builtin_printf);
185187
assert(E->getNumArgs() >= 1); // printf always has at least one arg.

clang/test/CodeGenHIP/printf-builtin.hip

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
// REQUIRES: amdgpu-registered-target
22
// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -emit-llvm -disable-llvm-optzns -mprintf-kind=hostcall -fno-builtin-printf -fcuda-is-device \
33
// RUN: -o - %s | FileCheck --check-prefixes=CHECK,HOSTCALL %s
4+
// RUN: %clang_cc1 -triple spirv64-amd-amdhsa -emit-llvm -disable-llvm-optzns -mprintf-kind=hostcall -fno-builtin-printf -fcuda-is-device \
5+
// RUN: -o - %s | FileCheck --check-prefixes=CHECK-AMDGCNSPIRV,HOSTCALL-AMDGCNSPIRV %s
46
// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -emit-llvm -disable-llvm-optzns -mprintf-kind=buffered -fno-builtin-printf -fcuda-is-device \
57
// RUN: -o - %s | FileCheck --check-prefixes=CHECK,BUFFERED %s
8+
// RUN: %clang_cc1 -triple spirv64-amd-amdhsa -emit-llvm -disable-llvm-optzns -mprintf-kind=buffered -fno-builtin-printf -fcuda-is-device \
9+
// RUN: -o - %s | FileCheck --check-prefixes=CHECK-AMDGCNSPIRV,BUFFERED-AMDGCNSPIRV %s
610

711
#define __device__ __attribute__((device))
812

@@ -11,13 +15,17 @@ extern "C" __device__ int printf(const char *format, ...);
1115
// CHECK-LABEL: @_Z4foo1v()
1216
__device__ int foo1() {
1317
// HOSTCALL: call i64 @__ockl_printf_begin
18+
// HOSTCALL-AMDGCNSPIRV: call addrspace(4) i64 @__ockl_printf_begin
1419
// BUFFERED: call ptr addrspace(1) @__printf_alloc
20+
// BUFFERED-AMDGCNSPIRV: call addrspace(4) ptr addrspace(1) @__printf_alloc
1521
// CHECK-NOT: call i32 (ptr, ...) @printf
22+
// CHECK-AMDGCNSPIRV-NOT: call i32 (ptr, ...) @printf
1623
return __builtin_printf("Hello World\n");
1724
}
1825

1926
// CHECK-LABEL: @_Z4foo2v()
2027
__device__ int foo2() {
2128
// CHECK: call i32 (ptr, ...) @printf
29+
// CHECK-AMDGCNSPIRV: call spir_func addrspace(4) i32 (ptr addrspace(4), ...) @printf
2230
return printf("Hello World\n");
2331
}

0 commit comments

Comments
 (0)