From 095c48fdf3d27a4f346f8680d1d7e89449bb557b Mon Sep 17 00:00:00 2001 From: kpyzhov Date: Tue, 5 Oct 2021 09:56:04 -0400 Subject: [PATCH] [AMDGPU] Use "hostcall" module flag instead of searching for ockl_hostcall_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 --- llvm/lib/Target/AMDGPU/AMDGPUHSAMetadataStreamer.cpp | 13 +++++++------ llvm/lib/Transforms/Utils/AMDGPUEmitPrintf.cpp | 1 + .../test/CodeGen/AMDGPU/hsa-metadata-hostcall-present-v3.ll | 5 +++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/llvm/lib/Target/AMDGPU/AMDGPUHSAMetadataStreamer.cpp b/llvm/lib/Target/AMDGPU/AMDGPUHSAMetadataStreamer.cpp index a50093f..cc9d68c 100644 --- a/llvm/lib/Target/AMDGPU/AMDGPUHSAMetadataStreamer.cpp +++ b/llvm/lib/Target/AMDGPU/AMDGPUHSAMetadataStreamer.cpp @@ -798,7 +798,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) @@ -814,16 +815,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 diff --git a/llvm/lib/Transforms/Utils/AMDGPUEmitPrintf.cpp b/llvm/lib/Transforms/Utils/AMDGPUEmitPrintf.cpp index 8cd16ca..a0d6553 100644 --- a/llvm/lib/Transforms/Utils/AMDGPUEmitPrintf.cpp +++ b/llvm/lib/Transforms/Utils/AMDGPUEmitPrintf.cpp @@ -63,6 +63,7 @@ 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); + M->addModuleFlag(llvm::Module::Override, "amdgpu_hostcall", 1); return Builder.CreateCall(Fn, Version); } diff --git a/llvm/test/CodeGen/AMDGPU/hsa-metadata-hostcall-present-v3.ll b/llvm/test/CodeGen/AMDGPU/hsa-metadata-hostcall-present-v3.ll index 11123e0..15186b3 100644 --- a/llvm/test/CodeGen/AMDGPU/hsa-metadata-hostcall-present-v3.ll +++ b/llvm/test/CodeGen/AMDGPU/hsa-metadata-hostcall-present-v3.ll @@ -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 { @@ -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 -- 2.7.4