Path: blob/main/contrib/llvm-project/llvm/lib/Target/AMDGPU/AMDGPUExportKernelRuntimeHandles.cpp
213799 views
//===- AMDGPUExportKernelRuntimeHandles.cpp - Lower enqueued block --------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// \file9//10// Give any globals used for OpenCL block enqueue runtime handles external11// linkage so the runtime may access them. These should behave like internal12// functions for purposes of linking, but need to have an external symbol in the13// final object for the runtime to access them.14//15// TODO: This could be replaced with a new linkage type or global object16// metadata that produces an external symbol in the final object, but allows17// rename on IR linking. Alternatively if we can rely on18// GlobalValue::getGlobalIdentifier we can just make these external symbols to19// begin with.20//21//===----------------------------------------------------------------------===//2223#include "AMDGPUExportKernelRuntimeHandles.h"24#include "AMDGPU.h"25#include "llvm/IR/Module.h"26#include "llvm/Pass.h"2728#define DEBUG_TYPE "amdgpu-export-kernel-runtime-handles"2930using namespace llvm;3132namespace {3334/// Lower enqueued blocks.35class AMDGPUExportKernelRuntimeHandlesLegacy : public ModulePass {36public:37static char ID;3839explicit AMDGPUExportKernelRuntimeHandlesLegacy() : ModulePass(ID) {}4041private:42bool runOnModule(Module &M) override;43};4445} // end anonymous namespace4647char AMDGPUExportKernelRuntimeHandlesLegacy::ID = 0;4849char &llvm::AMDGPUExportKernelRuntimeHandlesLegacyID =50AMDGPUExportKernelRuntimeHandlesLegacy::ID;5152INITIALIZE_PASS(AMDGPUExportKernelRuntimeHandlesLegacy, DEBUG_TYPE,53"Externalize enqueued block runtime handles", false, false)5455ModulePass *llvm::createAMDGPUExportKernelRuntimeHandlesLegacyPass() {56return new AMDGPUExportKernelRuntimeHandlesLegacy();57}5859static bool exportKernelRuntimeHandles(Module &M) {60bool Changed = false;6162const StringLiteral HandleSectionName(".amdgpu.kernel.runtime.handle");6364for (GlobalVariable &GV : M.globals()) {65if (GV.getSection() == HandleSectionName) {66GV.setLinkage(GlobalValue::ExternalLinkage);67GV.setDSOLocal(false);68Changed = true;69}70}7172if (!Changed)73return false;7475// FIXME: We shouldn't really need to export the kernel address. We can76// initialize the runtime handle with the kernel descriptor.77for (Function &F : M) {78if (F.getCallingConv() != CallingConv::AMDGPU_KERNEL)79continue;8081const MDNode *Associated = F.getMetadata(LLVMContext::MD_associated);82if (!Associated)83continue;8485auto *VM = cast<ValueAsMetadata>(Associated->getOperand(0));86auto *Handle = dyn_cast<GlobalObject>(VM->getValue());87if (Handle && Handle->getSection() == HandleSectionName) {88F.setLinkage(GlobalValue::ExternalLinkage);89F.setVisibility(GlobalValue::ProtectedVisibility);90}91}9293return Changed;94}9596bool AMDGPUExportKernelRuntimeHandlesLegacy::runOnModule(Module &M) {97return exportKernelRuntimeHandles(M);98}99100PreservedAnalyses101AMDGPUExportKernelRuntimeHandlesPass::run(Module &M,102ModuleAnalysisManager &MAM) {103if (!exportKernelRuntimeHandles(M))104return PreservedAnalyses::all();105106PreservedAnalyses PA;107PA.preserveSet<AllAnalysesOn<Function>>();108return PA;109}110111112