LLVM 22.0.0git
AMDGPUExportKernelRuntimeHandles.cpp
Go to the documentation of this file.
1//===- AMDGPUExportKernelRuntimeHandles.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//
11// Give any globals used for OpenCL block enqueue runtime handles external
12// linkage so the runtime may access them. These should behave like internal
13// functions for purposes of linking, but need to have an external symbol in the
14// final object for the runtime to access them.
15//
16// TODO: This could be replaced with a new linkage type or global object
17// metadata that produces an external symbol in the final object, but allows
18// rename on IR linking. Alternatively if we can rely on
19// GlobalValue::getGlobalIdentifier we can just make these external symbols to
20// begin with.
21//
22//===----------------------------------------------------------------------===//
23
25#include "AMDGPU.h"
26#include "llvm/IR/Module.h"
27#include "llvm/Pass.h"
28
29#define DEBUG_TYPE "amdgpu-export-kernel-runtime-handles"
30
31using namespace llvm;
32
33namespace {
34
35/// Lower enqueued blocks.
36class AMDGPUExportKernelRuntimeHandlesLegacy : public ModulePass {
37public:
38 static char ID;
39
40 explicit AMDGPUExportKernelRuntimeHandlesLegacy() : ModulePass(ID) {}
41
42private:
43 bool runOnModule(Module &M) override;
44};
45
46} // end anonymous namespace
47
48char AMDGPUExportKernelRuntimeHandlesLegacy::ID = 0;
49
51 AMDGPUExportKernelRuntimeHandlesLegacy::ID;
52
53INITIALIZE_PASS(AMDGPUExportKernelRuntimeHandlesLegacy, DEBUG_TYPE,
54 "Externalize enqueued block runtime handles", false, false)
55
57 return new AMDGPUExportKernelRuntimeHandlesLegacy();
58}
59
61 bool Changed = false;
62
63 const StringLiteral HandleSectionName(".amdgpu.kernel.runtime.handle");
64
65 for (GlobalVariable &GV : M.globals()) {
66 if (GV.getSection() == HandleSectionName) {
67 GV.setLinkage(GlobalValue::ExternalLinkage);
68 GV.setDSOLocal(false);
69 Changed = true;
70 }
71 }
72
73 if (!Changed)
74 return false;
75
76 // FIXME: We shouldn't really need to export the kernel address. We can
77 // initialize the runtime handle with the kernel descriptor.
78 for (Function &F : M) {
79 if (F.getCallingConv() != CallingConv::AMDGPU_KERNEL)
80 continue;
81
82 const MDNode *Associated = F.getMetadata(LLVMContext::MD_associated);
83 if (!Associated)
84 continue;
85
86 auto *VM = cast<ValueAsMetadata>(Associated->getOperand(0));
87 auto *Handle = dyn_cast<GlobalObject>(VM->getValue());
88 if (Handle && Handle->getSection() == HandleSectionName) {
91 }
92 }
93
94 return Changed;
95}
96
97bool AMDGPUExportKernelRuntimeHandlesLegacy::runOnModule(Module &M) {
99}
100
105 return PreservedAnalyses::all();
106
109 return PA;
110}
static bool exportKernelRuntimeHandles(Module &M)
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition: MD5.cpp:55
ModuleAnalysisManager MAM
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:56
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM)
This templated class represents "all analyses that operate over <a particular IR unit>" (e....
Definition: Analysis.h:50
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:255
@ ProtectedVisibility
The GV is protected.
Definition: GlobalValue.h:70
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:53
Metadata node.
Definition: Metadata.h:1077
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1445
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:255
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:67
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition: Analysis.h:151
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition: StringRef.h:862
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
@ AMDGPU_KERNEL
Used for AMDGPU code object kernels.
Definition: CallingConv.h:200
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
char & AMDGPUExportKernelRuntimeHandlesLegacyID
ModulePass * createAMDGPUExportKernelRuntimeHandlesLegacyPass()