LLVM 24.0.0git
SPIRVFinalizeShaderLinkage.cpp
Go to the documentation of this file.
1//===- SPIRVFinalizeShaderLinkage.cpp - Finalize shader linkage ----------===//
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// Shader-only analogue of DXILFinalizeLinkage: internalizes non-entry,
10// non-exported HLSL helper functions and erases the resulting dead ones.
11//
12//===----------------------------------------------------------------------===//
13
15#include "SPIRV.h"
16#include "SPIRVSubtarget.h"
17#include "SPIRVTargetMachine.h"
18#include "SPIRVUtils.h"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/IR/Function.h"
21#include "llvm/IR/GlobalValue.h"
22#include "llvm/IR/Module.h"
23#include "llvm/Pass.h"
24
25#define DEBUG_TYPE "spirv-finalize-shader-linkage"
26
27using namespace llvm;
28
29namespace {
30
31bool finalizeShaderLinkage(const SPIRVTargetMachine &TM, Module &M) {
32 // This pass only applies to shader targets because shaders don't have
33 // linkers.
34 if (!TM.getSubtargetImpl()->isShader())
35 return false;
36
37 bool Changed = false;
38
39 for (Function &F : M) {
40 if (F.isIntrinsic() || F.isDeclaration() || isEntryPoint(F))
41 continue;
42 if (F.hasExternalLinkage() && !F.hasHiddenVisibility())
43 continue;
44 if (!F.hasLocalLinkage()) {
46 Changed = true;
47 }
48 }
49
50 // Erase dead helpers, iterating to a fixpoint for helper-calls-helper chains.
51 bool LocalChange = true;
52 while (LocalChange) {
53 LocalChange = false;
55 if (F.isDefTriviallyDead()) {
56 F.eraseFromParent();
57 LocalChange = Changed = true;
58 }
59 }
60 return Changed;
61}
62
63class SPIRVFinalizeShaderLinkageLegacy : public ModulePass {
64public:
65 static char ID;
66 SPIRVFinalizeShaderLinkageLegacy(const SPIRVTargetMachine &TM)
67 : ModulePass(ID), TM(TM) {}
68 StringRef getPassName() const override {
69 return "SPIRV Finalize Shader Linkage";
70 }
71 bool runOnModule(Module &M) override { return finalizeShaderLinkage(TM, M); }
72
73private:
74 const SPIRVTargetMachine &TM;
75};
76
77} // namespace
78
84
85char SPIRVFinalizeShaderLinkageLegacy::ID = 0;
86
87INITIALIZE_PASS(SPIRVFinalizeShaderLinkageLegacy,
88 "spirv-finalize-shader-linkage",
89 "Finalize SPIR-V shader linkage", false, false)
90
93 return new SPIRVFinalizeShaderLinkageLegacy(TM);
94}
aarch64 promote const
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition MD5.cpp:54
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
This file contains some templates that are useful if you are working with the STL at all.
@ InternalLinkage
Rename collisions when linking (static functions).
Definition GlobalValue.h:60
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition Pass.h:255
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 none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
const SPIRVSubtarget * getSubtargetImpl() const
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Changed
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:633
ModulePass * createSPIRVFinalizeShaderLinkagePass(const SPIRVTargetMachine &TM)
bool isEntryPoint(const Function &F)
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39