Skip to content

Commit

Permalink
[AMDGPU] Use "hostcall" module flag instead of searching for ockl_hos…
Browse files Browse the repository at this point in the history
…tcall_internal() declaration.

The current way to detect hostcalls by looking for "ockl_hostcall_internal()" function in the module seems to be not reliable enough. The LTO may rename the "ockl_hostcall_internal()" function when an application is compiled with "-fgpu-rdc", and MetadataStreamer pass to fail to detect hostcalls, therefore it does not set the "hidden_hostcall_buffer" kernel argument.
This change adds a new module flag: hostcall that can be used to detect whether GPU functions use host calls for printf.

Differential revision: https://reviews.llvm.org/D110337

[AMDGPU] Correction to 095c48f.

Differential Revision: https://reviews.llvm.org/D110337

Change-Id: I5eb847884f4cb98687dcfdef85f78d2d2c380bcd
  • Loading branch information
kpyzhov authored and zhang2amd committed Oct 19, 2021
1 parent f54f00d commit e2489b0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
13 changes: 7 additions & 6 deletions llvm/lib/Target/AMDGPU/AMDGPUHSAMetadataStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,8 @@ void MetadataStreamerV3::emitHiddenKernelArgs(const Function &Func,
if (!HiddenArgNumBytes)
return;

auto &DL = Func.getParent()->getDataLayout();
const Module *M = Func.getParent();
auto &DL = M->getDataLayout();
auto Int64Ty = Type::getInt64Ty(Func.getContext());

if (HiddenArgNumBytes >= 8)
Expand All @@ -810,16 +811,16 @@ void MetadataStreamerV3::emitHiddenKernelArgs(const Function &Func,
auto Int8PtrTy =
Type::getInt8PtrTy(Func.getContext(), AMDGPUAS::GLOBAL_ADDRESS);

// Emit "printf buffer" argument if printf is used, otherwise emit dummy
// "none" argument.
// Emit "printf buffer" argument if printf is used, emit "hostcall buffer"
// if "hostcall" module flag is set, otherwise emit dummy "none" argument.
if (HiddenArgNumBytes >= 32) {
if (Func.getParent()->getNamedMetadata("llvm.printf.fmts"))
if (M->getNamedMetadata("llvm.printf.fmts"))
emitKernelArg(DL, Int8PtrTy, Align(8), "hidden_printf_buffer", Offset,
Args);
else if (Func.getParent()->getFunction("__ockl_hostcall_internal")) {
else if (M->getModuleFlag("amdgpu_hostcall")) {
// The printf runtime binding pass should have ensured that hostcall and
// printf are not used in the same module.
assert(!Func.getParent()->getNamedMetadata("llvm.printf.fmts"));
assert(!M->getNamedMetadata("llvm.printf.fmts"));
emitKernelArg(DL, Int8PtrTy, Align(8), "hidden_hostcall_buffer", Offset,
Args);
} else
Expand Down
3 changes: 3 additions & 0 deletions llvm/lib/Transforms/Utils/AMDGPUEmitPrintf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ static Value *callPrintfBegin(IRBuilder<> &Builder, Value *Version) {
auto Int64Ty = Builder.getInt64Ty();
auto M = Builder.GetInsertBlock()->getModule();
auto Fn = M->getOrInsertFunction("__ockl_printf_begin", Int64Ty, Int64Ty);
if (!M->getModuleFlag("amdgpu_hostcall")) {
M->addModuleFlag(llvm::Module::Override, "amdgpu_hostcall", 1);
}
return Builder.CreateCall(Fn, Version);
}

Expand Down
5 changes: 3 additions & 2 deletions llvm/test/CodeGen/AMDGPU/hsa-metadata-hostcall-present-v3.ll
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
; CHECK: .name: test_kernel
; CHECK: .symbol: test_kernel.kd

declare <2 x i64> @__ockl_hostcall_internal(i8*, i32, i64, i64, i64, i64, i64, i64, i64, i64)

define amdgpu_kernel void @test_kernel(i8 %a) #0
!kernel_arg_addr_space !1 !kernel_arg_access_qual !2 !kernel_arg_type !3
!kernel_arg_base_type !3 !kernel_arg_type_qual !4 {
Expand All @@ -51,4 +49,7 @@ attributes #0 = { "amdgpu-implicitarg-num-bytes"="48" }
!opencl.ocl.version = !{!90}
!90 = !{i32 2, i32 0}

!llvm.module.flags = !{!0}
!0 = !{i32 1, !"amdgpu_hostcall", i32 1}

; PARSER: AMDGPU HSA Metadata Parser Test: PASS

0 comments on commit e2489b0

Please sign in to comment.