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
14#include "SPIRV.h"
15#include "SPIRVSubtarget.h"
16#include "SPIRVTargetMachine.h"
17#include "SPIRVUtils.h"
18#include "llvm/ADT/STLExtras.h"
19#include "llvm/IR/Function.h"
20#include "llvm/IR/GlobalValue.h"
21#include "llvm/IR/Module.h"
22#include "llvm/Pass.h"
23
24#define DEBUG_TYPE "spirv-finalize-shader-linkage"
25
26using namespace llvm;
27
28namespace {
29
30bool finalizeShaderLinkage(const SPIRVTargetMachine &TM, Module &M) {
31 // This pass only applies to shader targets because shaders don't have
32 // linkers.
33 if (!TM.getSubtargetImpl()->isShader())
34 return false;
35
36 bool Changed = false;
37
38 for (Function &F : M) {
39 if (F.isIntrinsic() || F.isDeclaration() || isEntryPoint(F))
40 continue;
41 if (F.hasExternalLinkage() && !F.hasHiddenVisibility())
42 continue;
43 if (!F.hasLocalLinkage()) {
45 Changed = true;
46 }
47 }
48
49 // Erase dead helpers, iterating to a fixpoint for helper-calls-helper chains.
50 bool LocalChange = true;
51 while (LocalChange) {
52 LocalChange = false;
54 if (F.isDefTriviallyDead()) {
55 F.eraseFromParent();
56 LocalChange = Changed = true;
57 }
58 }
59 return Changed;
60}
61
62class SPIRVFinalizeShaderLinkageLegacy : public ModulePass {
63public:
64 static char ID;
65 SPIRVFinalizeShaderLinkageLegacy(const SPIRVTargetMachine &TM)
66 : ModulePass(ID), TM(TM) {}
67 StringRef getPassName() const override {
68 return "SPIRV Finalize Shader Linkage";
69 }
70 bool runOnModule(Module &M) override { return finalizeShaderLinkage(TM, M); }
71
72private:
73 const SPIRVTargetMachine &TM;
74};
75
76} // namespace
77
83
84char SPIRVFinalizeShaderLinkageLegacy::ID = 0;
85
86INITIALIZE_PASS(SPIRVFinalizeShaderLinkageLegacy,
87 "spirv-finalize-shader-linkage",
88 "Finalize SPIR-V shader linkage", false, false)
89
92 return new SPIRVFinalizeShaderLinkageLegacy(TM);
93}
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:68
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
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