blob: 7bd8533a0ccf799276e6a49341e1095f8577327b [file] [log] [blame]
Yaxun Liude4b88d2017-10-10 19:39:48 +00001//===- AMDGPUOpenCLEnqueuedBlockLowering.cpp - Lower enqueued block -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000011// This post-linking pass replaces the function pointer of enqueued
Yaxun Liude4b88d2017-10-10 19:39:48 +000012// block kernel with a global variable (runtime handle) and adds
13// "runtime-handle" attribute to the enqueued block kernel.
14//
15// In LLVM CodeGen the runtime-handle metadata will be translated to
16// RuntimeHandle metadata in code object. Runtime allocates a global buffer
17// for each kernel with RuntimeHandel metadata and saves the kernel address
18// required for the AQL packet into the buffer. __enqueue_kernel function
19// in device library knows that the invoke function pointer in the block
20// literal is actually runtime handle and loads the kernel address from it
21// and put it into AQL packet for dispatching.
22//
23// This cannot be done in FE since FE cannot create a unique global variable
24// with external linkage across LLVM modules. The global variable with internal
25// linkage does not work since optimization passes will try to replace loads
26// of the global variable with its initialization value.
27//
Yaxun Liuc928f2a2017-10-30 14:30:28 +000028// It also identifies the kernels directly or indirectly enqueues kernels
29// and adds "calls-enqueue-kernel" function attribute to them, which will
30// be used to determine whether to emit runtime metadata for the kernel
31// enqueue related hidden kernel arguments.
32//
Yaxun Liude4b88d2017-10-10 19:39:48 +000033//===----------------------------------------------------------------------===//
34
35#include "AMDGPU.h"
Yaxun Liuc928f2a2017-10-30 14:30:28 +000036#include "llvm/ADT/DenseSet.h"
Yaxun Liude4b88d2017-10-10 19:39:48 +000037#include "llvm/ADT/StringRef.h"
38#include "llvm/IR/Constants.h"
Yaxun Liufb17bf62018-06-13 17:31:51 +000039#include "llvm/IR/DerivedTypes.h"
Yaxun Liuc928f2a2017-10-30 14:30:28 +000040#include "llvm/IR/Instructions.h"
Yaxun Liua99e7d82018-03-12 16:34:06 +000041#include "llvm/IR/Mangler.h"
Yaxun Liude4b88d2017-10-10 19:39:48 +000042#include "llvm/IR/Module.h"
Yaxun Liuc928f2a2017-10-30 14:30:28 +000043#include "llvm/IR/User.h"
Yaxun Liude4b88d2017-10-10 19:39:48 +000044#include "llvm/Pass.h"
45#include "llvm/Support/Debug.h"
46#include "llvm/Support/raw_ostream.h"
47
48#define DEBUG_TYPE "amdgpu-lower-enqueued-block"
49
50using namespace llvm;
51
52namespace {
53
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000054/// Lower enqueued blocks.
Yaxun Liude4b88d2017-10-10 19:39:48 +000055class AMDGPUOpenCLEnqueuedBlockLowering : public ModulePass {
56public:
57 static char ID;
58
59 explicit AMDGPUOpenCLEnqueuedBlockLowering() : ModulePass(ID) {}
60
61private:
62 bool runOnModule(Module &M) override;
63};
64
65} // end anonymous namespace
66
67char AMDGPUOpenCLEnqueuedBlockLowering::ID = 0;
68
69char &llvm::AMDGPUOpenCLEnqueuedBlockLoweringID =
70 AMDGPUOpenCLEnqueuedBlockLowering::ID;
71
72INITIALIZE_PASS(AMDGPUOpenCLEnqueuedBlockLowering, DEBUG_TYPE,
73 "Lower OpenCL enqueued blocks", false, false)
74
75ModulePass* llvm::createAMDGPUOpenCLEnqueuedBlockLoweringPass() {
76 return new AMDGPUOpenCLEnqueuedBlockLowering();
77}
78
Yaxun Liuc928f2a2017-10-30 14:30:28 +000079/// Collect direct or indrect callers of \p F and save them
80/// to \p Callers.
81static void collectCallers(Function *F, DenseSet<Function *> &Callers) {
82 for (auto U : F->users()) {
83 if (auto *CI = dyn_cast<CallInst>(&*U)) {
84 auto *Caller = CI->getParent()->getParent();
Yaxun Liu9381ae92018-04-11 14:46:15 +000085 if (Callers.insert(Caller).second)
86 collectCallers(Caller, Callers);
Yaxun Liuc928f2a2017-10-30 14:30:28 +000087 }
88 }
89}
90
Yaxun Liu9381ae92018-04-11 14:46:15 +000091/// If \p U is instruction or constant, collect functions which directly or
92/// indirectly use it.
93static void collectFunctionUsers(User *U, DenseSet<Function *> &Funcs) {
94 if (auto *I = dyn_cast<Instruction>(U)) {
95 auto *F = I->getParent()->getParent();
96 if (Funcs.insert(F).second)
97 collectCallers(F, Funcs);
98 return;
99 }
100 if (!isa<Constant>(U))
101 return;
102 for (auto UU : U->users())
103 collectFunctionUsers(&*UU, Funcs);
104}
105
Yaxun Liude4b88d2017-10-10 19:39:48 +0000106bool AMDGPUOpenCLEnqueuedBlockLowering::runOnModule(Module &M) {
Yaxun Liuc928f2a2017-10-30 14:30:28 +0000107 DenseSet<Function *> Callers;
Yaxun Liude4b88d2017-10-10 19:39:48 +0000108 auto &C = M.getContext();
Yaxun Liude4b88d2017-10-10 19:39:48 +0000109 bool Changed = false;
110 for (auto &F : M.functions()) {
111 if (F.hasFnAttribute("enqueued-block")) {
Yaxun Liua99e7d82018-03-12 16:34:06 +0000112 if (!F.hasName()) {
113 SmallString<64> Name;
114 Mangler::getNameWithPrefix(Name, "__amdgpu_enqueued_kernel",
115 M.getDataLayout());
116 F.setName(Name);
117 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000118 LLVM_DEBUG(dbgs() << "found enqueued kernel: " << F.getName() << '\n');
Yaxun Liua99e7d82018-03-12 16:34:06 +0000119 auto RuntimeHandle = (F.getName() + ".runtime_handle").str();
Yaxun Liufb17bf62018-06-13 17:31:51 +0000120 auto T = ArrayType::get(Type::getInt64Ty(C), 2);
Yaxun Liua99e7d82018-03-12 16:34:06 +0000121 auto *GV = new GlobalVariable(
Yaxun Liu9381ae92018-04-11 14:46:15 +0000122 M, T,
123 /*IsConstant=*/false, GlobalValue::ExternalLinkage,
124 /*Initializer=*/Constant::getNullValue(T), RuntimeHandle,
125 /*InsertBefore=*/nullptr, GlobalValue::NotThreadLocal,
126 AMDGPUAS::GLOBAL_ADDRESS,
127 /*IsExternallyInitialized=*/false);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000128 LLVM_DEBUG(dbgs() << "runtime handle created: " << *GV << '\n');
Yaxun Liua99e7d82018-03-12 16:34:06 +0000129
Yaxun Liu46439e82018-03-06 16:04:39 +0000130 for (auto U : F.users()) {
Yaxun Liu9381ae92018-04-11 14:46:15 +0000131 auto *UU = &*U;
132 if (!isa<ConstantExpr>(UU))
Yaxun Liu46439e82018-03-06 16:04:39 +0000133 continue;
Yaxun Liu9381ae92018-04-11 14:46:15 +0000134 collectFunctionUsers(UU, Callers);
135 auto *BitCast = cast<ConstantExpr>(UU);
Yaxun Liu46439e82018-03-06 16:04:39 +0000136 auto *NewPtr = ConstantExpr::getPointerCast(GV, BitCast->getType());
137 BitCast->replaceAllUsesWith(NewPtr);
138 F.addFnAttr("runtime-handle", RuntimeHandle);
139 F.setLinkage(GlobalValue::ExternalLinkage);
Yaxun Liu46439e82018-03-06 16:04:39 +0000140 Changed = true;
Yaxun Liuc928f2a2017-10-30 14:30:28 +0000141 }
Yaxun Liude4b88d2017-10-10 19:39:48 +0000142 }
143 }
Yaxun Liuc928f2a2017-10-30 14:30:28 +0000144
145 for (auto F : Callers) {
146 if (F->getCallingConv() != CallingConv::AMDGPU_KERNEL)
147 continue;
148 F->addFnAttr("calls-enqueue-kernel");
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000149 LLVM_DEBUG(dbgs() << "mark enqueue_kernel caller:" << F->getName() << '\n');
Yaxun Liuc928f2a2017-10-30 14:30:28 +0000150 }
Yaxun Liude4b88d2017-10-10 19:39:48 +0000151 return Changed;
152}