LLVM 19.0.0git
AMDGPUOpenCLEnqueuedBlockLowering.cpp
Go to the documentation of this file.
1//===- AMDGPUOpenCLEnqueuedBlockLowering.cpp - Lower enqueued block -------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// \file
10// This post-linking pass replaces the function pointer of enqueued
11// block kernel with a global variable (runtime handle) and adds
12// "runtime-handle" attribute to the enqueued block kernel.
13//
14// In LLVM CodeGen the runtime-handle metadata will be translated to
15// RuntimeHandle metadata in code object. Runtime allocates a global buffer
16// for each kernel with RuntimeHandle metadata and saves the kernel address
17// required for the AQL packet into the buffer. __enqueue_kernel function
18// in device library knows that the invoke function pointer in the block
19// literal is actually runtime handle and loads the kernel address from it
20// and put it into AQL packet for dispatching.
21//
22// This cannot be done in FE since FE cannot create a unique global variable
23// with external linkage across LLVM modules. The global variable with internal
24// linkage does not work since optimization passes will try to replace loads
25// of the global variable with its initialization value.
26//
27// It also identifies the kernels directly or indirectly enqueues kernels
28// and adds "calls-enqueue-kernel" function attribute to them, which will
29// be used to determine whether to emit runtime metadata for the kernel
30// enqueue related hidden kernel arguments.
31//
32//===----------------------------------------------------------------------===//
33
34#include "AMDGPU.h"
35#include "llvm/ADT/DenseSet.h"
37#include "llvm/IR/Constants.h"
39#include "llvm/IR/Mangler.h"
40#include "llvm/IR/Module.h"
41#include "llvm/Pass.h"
42#include "llvm/Support/Debug.h"
43
44#define DEBUG_TYPE "amdgpu-lower-enqueued-block"
45
46using namespace llvm;
47
48namespace {
49
50/// Lower enqueued blocks.
51class AMDGPUOpenCLEnqueuedBlockLowering : public ModulePass {
52public:
53 static char ID;
54
55 explicit AMDGPUOpenCLEnqueuedBlockLowering() : ModulePass(ID) {}
56
57private:
58 bool runOnModule(Module &M) override;
59};
60
61} // end anonymous namespace
62
63char AMDGPUOpenCLEnqueuedBlockLowering::ID = 0;
64
66 AMDGPUOpenCLEnqueuedBlockLowering::ID;
67
68INITIALIZE_PASS(AMDGPUOpenCLEnqueuedBlockLowering, DEBUG_TYPE,
69 "Lower OpenCL enqueued blocks", false, false)
70
72 return new AMDGPUOpenCLEnqueuedBlockLowering();
73}
74
75bool AMDGPUOpenCLEnqueuedBlockLowering::runOnModule(Module &M) {
77 auto &C = M.getContext();
78 bool Changed = false;
79
80 // ptr kernel_object, i32 private_segment_size, i32 group_segment_size
81 StructType *HandleTy = nullptr;
82
83 for (auto &F : M.functions()) {
84 if (F.hasFnAttribute("enqueued-block")) {
85 if (!F.hasName()) {
87 Mangler::getNameWithPrefix(Name, "__amdgpu_enqueued_kernel",
88 M.getDataLayout());
89 F.setName(Name);
90 }
91 LLVM_DEBUG(dbgs() << "found enqueued kernel: " << F.getName() << '\n');
92 auto RuntimeHandle = (F.getName() + ".runtime_handle").str();
93 if (!HandleTy) {
95 HandleTy =
96 StructType::create(C, {PointerType::getUnqual(C), Int32, Int32},
97 "block.runtime.handle.t");
98 }
99
100 auto *GV = new GlobalVariable(
101 M, HandleTy,
102 /*isConstant=*/true, GlobalValue::ExternalLinkage,
103 /*Initializer=*/Constant::getNullValue(HandleTy), RuntimeHandle,
104 /*InsertBefore=*/nullptr, GlobalValue::NotThreadLocal,
106 /*isExternallyInitialized=*/true);
107 LLVM_DEBUG(dbgs() << "runtime handle created: " << *GV << '\n');
108
109 F.replaceAllUsesWith(ConstantExpr::getAddrSpaceCast(GV, F.getType()));
110 F.addFnAttr("runtime-handle", RuntimeHandle);
112 Changed = true;
113 }
114 }
115
116 return Changed;
117}
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define LLVM_DEBUG(X)
Definition: Debug.h:101
This file defines the DenseSet and SmallDenseSet classes.
std::string Name
#define F(x, y, z)
Definition: MD5.cpp:55
Module.h This file contains the declarations for the Module class.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
This file defines the SmallString class.
static Constant * getAddrSpaceCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2152
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:370
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:52
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
Definition: Mangler.cpp:119
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:251
virtual bool runOnModule(Module &M)=0
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
Class to represent struct types.
Definition: DerivedTypes.h:216
static StructType * create(LLVMContext &Context, StringRef Name)
This creates an identified struct.
Definition: Type.cpp:513
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
static IntegerType * getInt32Ty(LLVMContext &C)
@ GLOBAL_ADDRESS
Address space for global memory (RAT0, VTX0).
constexpr char RuntimeHandle[]
Key for Kernel::Attr::Metadata::mRuntimeHandle.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
ModulePass * createAMDGPUOpenCLEnqueuedBlockLoweringPass()
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
char & AMDGPUOpenCLEnqueuedBlockLoweringID