LLVM 23.0.0git
SPIRVPrepareGlobals.cpp
Go to the documentation of this file.
1//===-- SPIRVPrepareGlobals.cpp - Prepare IR SPIRV globals ------*- C++ -*-===//
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// The pass transforms IR globals that cannot be trivially mapped to SPIRV
10// into something that is trival to lower.
11//
12//===----------------------------------------------------------------------===//
13
14#include "SPIRV.h"
15#include "SPIRVUtils.h"
16
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/IR/Module.h"
19#include "llvm/Support/Debug.h"
20
21#define DEBUG_TYPE "spirv-prepare-globals"
22
23using namespace llvm;
24
25namespace {
26
27struct SPIRVPrepareGlobals : public ModulePass {
28 static char ID;
29 SPIRVPrepareGlobals() : ModulePass(ID) {}
30
31 StringRef getPassName() const override {
32 return "SPIRV prepare global variables";
33 }
34
35 bool runOnModule(Module &M) override;
36};
37
38// The backend does not support GlobalAlias. Replace aliases with their aliasees
39// when possible and remove them from the module.
40bool tryReplaceAliasWithAliasee(GlobalAlias &GA) {
41 // According to the lang ref, aliases cannot be replaced if either the alias
42 // or the aliasee are interposable. We only replace in the case that both
43 // are not interposable.
44 if (GA.isInterposable()) {
45 LLVM_DEBUG(dbgs() << "Skipping interposable alias: " << GA.getName()
46 << "\n");
47 return false;
48 }
49
50 auto *AO = dyn_cast<GlobalObject>(GA.getAliasee());
51 if (!AO) {
52 LLVM_DEBUG(dbgs() << "Skipping alias whose aliasee is not a GlobalObject: "
53 << GA.getName() << "\n");
54 return false;
55 }
56
57 if (AO->isInterposable()) {
58 LLVM_DEBUG(dbgs() << "Skipping interposable aliasee: " << AO->getName()
59 << "\n");
60 return false;
61 }
62
63 LLVM_DEBUG(dbgs() << "Replacing alias " << GA.getName()
64 << " with aliasee: " << AO->getName() << "\n");
65
66 GA.replaceAllUsesWith(AO);
67 if (GA.isDiscardableIfUnused()) {
68 GA.eraseFromParent();
69 }
70
71 return true;
72}
73
74bool SPIRVPrepareGlobals::runOnModule(Module &M) {
75 bool Changed = false;
76
77 for (GlobalAlias &GA : make_early_inc_range(M.aliases())) {
78 Changed |= tryReplaceAliasWithAliasee(GA);
79 }
80
81 return Changed;
82}
83char SPIRVPrepareGlobals::ID = 0;
84
85} // namespace
86
87INITIALIZE_PASS(SPIRVPrepareGlobals, "prepare-globals",
88 "SPIRV prepare global variables", false, false)
89
90namespace llvm {
92 return new SPIRVPrepareGlobals();
93}
94} // namespace llvm
Module.h This file contains the declarations for the Module class.
#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.
#define LLVM_DEBUG(...)
Definition Debug.h:114
LLVM_ABI void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition Globals.cpp:643
const Constant * getAliasee() const
Definition GlobalAlias.h:87
LLVM_ABI bool isInterposable() const
Return true if this global's definition can be substituted with an arbitrary definition at link time ...
Definition Globals.cpp:108
static bool isDiscardableIfUnused(LinkageTypes Linkage)
Whether the definition of this global may be discarded if it is not used in its compilation unit.
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
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:553
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
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.
Definition Types.h:26
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
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:632
ModulePass * createSPIRVPrepareGlobalsPass()
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207