blob: bb65636f15afb30138b5854f513581ad2a3d0d8f [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
11// \brief This post-linking pass replaces the function pointer of enqueued
12// 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 Liuc928f2a2017-10-30 14:30:28 +000039#include "llvm/IR/Instructions.h"
Yaxun Liude4b88d2017-10-10 19:39:48 +000040#include "llvm/IR/Module.h"
Yaxun Liuc928f2a2017-10-30 14:30:28 +000041#include "llvm/IR/User.h"
Yaxun Liude4b88d2017-10-10 19:39:48 +000042#include "llvm/Pass.h"
43#include "llvm/Support/Debug.h"
44#include "llvm/Support/raw_ostream.h"
45
46#define DEBUG_TYPE "amdgpu-lower-enqueued-block"
47
48using namespace llvm;
49
50namespace {
51
52/// \brief Lower enqueued blocks.
53class AMDGPUOpenCLEnqueuedBlockLowering : public ModulePass {
54public:
55 static char ID;
56
57 explicit AMDGPUOpenCLEnqueuedBlockLowering() : ModulePass(ID) {}
58
59private:
60 bool runOnModule(Module &M) override;
61};
62
63} // end anonymous namespace
64
65char AMDGPUOpenCLEnqueuedBlockLowering::ID = 0;
66
67char &llvm::AMDGPUOpenCLEnqueuedBlockLoweringID =
68 AMDGPUOpenCLEnqueuedBlockLowering::ID;
69
70INITIALIZE_PASS(AMDGPUOpenCLEnqueuedBlockLowering, DEBUG_TYPE,
71 "Lower OpenCL enqueued blocks", false, false)
72
73ModulePass* llvm::createAMDGPUOpenCLEnqueuedBlockLoweringPass() {
74 return new AMDGPUOpenCLEnqueuedBlockLowering();
75}
76
Yaxun Liuc928f2a2017-10-30 14:30:28 +000077/// Collect direct or indrect callers of \p F and save them
78/// to \p Callers.
79static void collectCallers(Function *F, DenseSet<Function *> &Callers) {
80 for (auto U : F->users()) {
81 if (auto *CI = dyn_cast<CallInst>(&*U)) {
82 auto *Caller = CI->getParent()->getParent();
83 if (Callers.count(Caller))
84 continue;
85 Callers.insert(Caller);
86 collectCallers(Caller, Callers);
87 }
88 }
89}
90
Yaxun Liude4b88d2017-10-10 19:39:48 +000091bool AMDGPUOpenCLEnqueuedBlockLowering::runOnModule(Module &M) {
Yaxun Liuc928f2a2017-10-30 14:30:28 +000092 DenseSet<Function *> Callers;
Yaxun Liude4b88d2017-10-10 19:39:48 +000093 auto &C = M.getContext();
Yaxun Liude4b88d2017-10-10 19:39:48 +000094 bool Changed = false;
95 for (auto &F : M.functions()) {
96 if (F.hasFnAttribute("enqueued-block")) {
97 if (!F.hasOneUse() || !F.user_begin()->hasOneUse() ||
98 !isa<ConstantExpr>(*F.user_begin()) ||
99 !isa<ConstantExpr>(*F.user_begin()->user_begin())) {
100 continue;
101 }
102 auto *BitCast = cast<ConstantExpr>(*F.user_begin());
103 auto *AddrCast = cast<ConstantExpr>(*BitCast->user_begin());
104 auto RuntimeHandle = (F.getName() + "_runtime_handle").str();
105 auto *GV = new GlobalVariable(
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +0000106 M, Type::getInt8Ty(C)->getPointerTo(AMDGPUAS::GLOBAL_ADDRESS),
Yaxun Liude4b88d2017-10-10 19:39:48 +0000107 /*IsConstant=*/true, GlobalValue::ExternalLinkage,
108 /*Initializer=*/nullptr, RuntimeHandle, /*InsertBefore=*/nullptr,
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +0000109 GlobalValue::NotThreadLocal, AMDGPUAS::GLOBAL_ADDRESS,
Yaxun Liude4b88d2017-10-10 19:39:48 +0000110 /*IsExternallyInitialized=*/true);
111 DEBUG(dbgs() << "runtime handle created: " << *GV << '\n');
112 auto *NewPtr = ConstantExpr::getPointerCast(GV, AddrCast->getType());
113 AddrCast->replaceAllUsesWith(NewPtr);
114 F.addFnAttr("runtime-handle", RuntimeHandle);
115 F.setLinkage(GlobalValue::ExternalLinkage);
Yaxun Liuc928f2a2017-10-30 14:30:28 +0000116
117 // Collect direct or indirect callers of enqueue_kernel.
118 for (auto U : NewPtr->users()) {
119 if (auto *I = dyn_cast<Instruction>(&*U)) {
120 auto *F = I->getParent()->getParent();
121 Callers.insert(F);
122 collectCallers(F, Callers);
123 }
124 }
Yaxun Liude4b88d2017-10-10 19:39:48 +0000125 Changed = true;
126 }
127 }
Yaxun Liuc928f2a2017-10-30 14:30:28 +0000128
129 for (auto F : Callers) {
130 if (F->getCallingConv() != CallingConv::AMDGPU_KERNEL)
131 continue;
132 F->addFnAttr("calls-enqueue-kernel");
133 }
Yaxun Liude4b88d2017-10-10 19:39:48 +0000134 return Changed;
135}