[PATCH] D110337: [AMDGPU] Use "amdgpu-hostcalls" attribute to detect if a kernel function uses any host calls.
Konstantin Pyzhov via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 23 08:30:55 PDT 2021
kpyzhov created this revision.
kpyzhov added reviewers: scott.linder, sameerds.
kpyzhov added a project: AMDGPU.
Herald added subscribers: foad, kerbowa, hiraditya, t-tye, tpr, dstuttard, yaxunl, nhaehnle, jvesely, kzhuravl, arsenm.
kpyzhov requested review of this revision.
Herald added subscribers: llvm-commits, wdng.
Herald added a project: LLVM.
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 new function attribute: "amdgpu-hostcalls" that can be used to detect whether GPU functions use any host calls.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D110337
Files:
llvm/lib/Target/AMDGPU/AMDGPUHSAMetadataStreamer.cpp
llvm/lib/Transforms/Utils/AMDGPUEmitPrintf.cpp
llvm/test/CodeGen/AMDGPU/hsa-metadata-hostcall-present-v3.ll
Index: llvm/test/CodeGen/AMDGPU/hsa-metadata-hostcall-present-v3.ll
===================================================================
--- llvm/test/CodeGen/AMDGPU/hsa-metadata-hostcall-present-v3.ll
+++ llvm/test/CodeGen/AMDGPU/hsa-metadata-hostcall-present-v3.ll
@@ -41,7 +41,7 @@
; CHECK-NEXT: - 1
; CHECK-NEXT: - 0
-attributes #0 = { "amdgpu-implicitarg-num-bytes"="48" }
+attributes #0 = { "amdgpu-implicitarg-num-bytes"="48" "amdgpu-hostcalls"}
!1 = !{i32 0}
!2 = !{!"none"}
Index: llvm/lib/Transforms/Utils/AMDGPUEmitPrintf.cpp
===================================================================
--- llvm/lib/Transforms/Utils/AMDGPUEmitPrintf.cpp
+++ llvm/lib/Transforms/Utils/AMDGPUEmitPrintf.cpp
@@ -63,7 +63,11 @@
auto Int64Ty = Builder.getInt64Ty();
auto M = Builder.GetInsertBlock()->getModule();
auto Fn = M->getOrInsertFunction("__ockl_printf_begin", Int64Ty, Int64Ty);
- return Builder.CreateCall(Fn, Version);
+ auto Call = Builder.CreateCall(Fn, Version);
+ auto Bb = Call->getParent();
+ assert(Bb && Bb->getParent());
+ Bb->getParent()->addFnAttr("amdgpu-hostcalls");
+ return Call;
}
static Value *callAppendArgs(IRBuilder<> &Builder, Value *Desc, int NumArgs,
Index: llvm/lib/Target/AMDGPU/AMDGPUHSAMetadataStreamer.cpp
===================================================================
--- llvm/lib/Target/AMDGPU/AMDGPUHSAMetadataStreamer.cpp
+++ llvm/lib/Target/AMDGPU/AMDGPUHSAMetadataStreamer.cpp
@@ -798,7 +798,8 @@
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,18 @@
auto Int8PtrTy =
Type::getInt8PtrTy(Func.getContext(), AMDGPUAS::GLOBAL_ADDRESS);
- // Emit "printf buffer" argument if printf is used, otherwise emit dummy
- // "none" argument.
+ // Emit "hidden_printf_buffer" argument if printf is used,
+ // "hidden_hostcall_buffer" if hostcalls are used,
+ // 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 ([&](){ for (auto &F: *M) if(F.hasFnAttribute("amdgpu-hostcalls")) {
+ return true; break; } return false;}() ) {
// 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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D110337.374559.patch
Type: text/x-patch
Size: 2973 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210923/d41f40ee/attachment.bin>
More information about the llvm-commits
mailing list