LLVM 20.0.0git
GCStrategy.h
Go to the documentation of this file.
1//===- llvm/CodeGen/GCStrategy.h - Garbage collection -----------*- 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// GCStrategy coordinates code generation algorithms and implements some itself
10// in order to generate code compatible with a target code generator as
11// specified in a function's 'gc' attribute. Algorithms are enabled by setting
12// flags in a subclass's constructor, and some virtual methods can be
13// overridden.
14//
15// GCStrategy is relevant for implementations using either gc.root or
16// gc.statepoint based lowering strategies, but is currently focused mostly on
17// options for gc.root. This will change over time.
18//
19// When requested by a subclass of GCStrategy, the gc.root implementation will
20// populate GCModuleInfo and GCFunctionInfo with that about each Function in
21// the Module that opts in to garbage collection. Specifically:
22//
23// - Safe points
24// Garbage collection is generally only possible at certain points in code.
25// GCStrategy can request that the collector insert such points:
26//
27// - At and after any call to a subroutine
28// - Before returning from the current function
29// - Before backwards branches (loops)
30//
31// - Roots
32// When a reference to a GC-allocated object exists on the stack, it must be
33// stored in an alloca registered with llvm.gcoot.
34//
35// This information can used to emit the metadata tables which are required by
36// the target garbage collector runtime.
37//
38// When used with gc.statepoint, information about safepoint and roots can be
39// found in the binary StackMap section after code generation. Safepoint
40// placement is currently the responsibility of the frontend, though late
41// insertion support is planned.
42//
43// The read and write barrier support can be used with either implementation.
44//
45//===----------------------------------------------------------------------===//
46
47#ifndef LLVM_IR_GCSTRATEGY_H
48#define LLVM_IR_GCSTRATEGY_H
49
51#include <optional>
52#include <string>
53
54namespace llvm {
55
56class Type;
57
58/// GCStrategy describes a garbage collector algorithm's code generation
59/// requirements, and provides overridable hooks for those needs which cannot
60/// be abstractly described. GCStrategy objects must be looked up through
61/// the Function. The objects themselves are owned by the Context and must
62/// be immutable.
64private:
65 friend class GCModuleInfo;
66
67 std::string Name;
68
69protected:
70 bool UseStatepoints = false; /// Uses gc.statepoints as opposed to gc.roots,
71 /// if set, NeededSafePoints and UsesMetadata
72 /// should be left at their default values.
73
74 bool UseRS4GC = false; /// If UseStatepoints is set, this determines whether
75 /// the RewriteStatepointsForGC pass should rewrite
76 /// this function's calls.
77 /// This should only be set if UseStatepoints is set.
78
79 bool NeededSafePoints = false; ///< if set, calls are inferred to be safepoints
80 bool UsesMetadata = false; ///< If set, backend must emit metadata tables.
81
82public:
84 virtual ~GCStrategy() = default;
85
86 /// Return the name of the GC strategy. This is the value of the collector
87 /// name string specified on functions which use this strategy.
88 const std::string &getName() const { return Name; }
89
90 /// Returns true if this strategy is expecting the use of gc.statepoints,
91 /// and false otherwise.
92 bool useStatepoints() const { return UseStatepoints; }
93
94 /** @name Statepoint Specific Properties */
95 ///@{
96
97 /// If the type specified can be reliably distinguished, returns true for
98 /// pointers to GC managed locations and false for pointers to non-GC
99 /// managed locations. Note a GCStrategy can always return 'std::nullopt'
100 /// (i.e. an empty optional indicating it can't reliably distinguish.
101 virtual std::optional<bool> isGCManagedPointer(const Type *Ty) const {
102 return std::nullopt;
103 }
104
105 /// Returns true if the RewriteStatepointsForGC pass should run on functions
106 /// using this GC.
107 bool useRS4GC() const {
109 "GC strategy has useRS4GC but not useStatepoints set");
110 return UseRS4GC;
111 }
112
113 ///@}
114
115 /// If set, appropriate metadata tables must be emitted by the back-end
116 /// (assembler, JIT, or otherwise). The default stackmap information can be
117 /// found in the StackMap section as described in the documentation.
118 bool usesMetadata() const { return UsesMetadata; }
119
120 /** @name GCRoot Specific Properties
121 * These properties and overrides only apply to collector strategies using
122 * GCRoot.
123 */
124 ///@{
125
126 /// True if safe points need to be inferred on call sites
127 bool needsSafePoints() const { return NeededSafePoints; }
128
129 ///@}
130};
131
132/// Subclasses of GCStrategy are made available for use during compilation by
133/// adding them to the global GCRegistry. This can done either within the
134/// LLVM source tree or via a loadable plugin. An example registeration
135/// would be:
136/// static GCRegistry::Add<CustomGC> X("custom-name",
137/// "my custom supper fancy gc strategy");
138///
139/// Note that to use a custom GCMetadataPrinter, you must also
140/// register your GCMetadataPrinter subclass with the
141/// GCMetadataPrinterRegistery as well.
143
144/// Lookup the GCStrategy object associated with the given gc name.
145std::unique_ptr<GCStrategy> getGCStrategy(const StringRef Name);
146
147} // end namespace llvm
148
149#endif // LLVM_IR_GCSTRATEGY_H
RelocType Type
Definition: COFFYAML.cpp:391
std::string Name
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
An analysis pass which caches information about the entire Module.
Definition: GCMetadata.h:203
GCStrategy describes a garbage collector algorithm's code generation requirements,...
Definition: GCStrategy.h:63
bool UseRS4GC
Uses gc.statepoints as opposed to gc.roots, if set, NeededSafePoints and UsesMetadata should be left ...
Definition: GCStrategy.h:74
bool UsesMetadata
If set, backend must emit metadata tables.
Definition: GCStrategy.h:80
virtual std::optional< bool > isGCManagedPointer(const Type *Ty) const
If the type specified can be reliably distinguished, returns true for pointers to GC managed location...
Definition: GCStrategy.h:101
bool usesMetadata() const
If set, appropriate metadata tables must be emitted by the back-end (assembler, JIT,...
Definition: GCStrategy.h:118
bool useStatepoints() const
Returns true if this strategy is expecting the use of gc.statepoints, and false otherwise.
Definition: GCStrategy.h:92
bool needsSafePoints() const
True if safe points need to be inferred on call sites.
Definition: GCStrategy.h:127
bool useRS4GC() const
Returns true if the RewriteStatepointsForGC pass should run on functions using this GC.
Definition: GCStrategy.h:107
bool NeededSafePoints
If UseStatepoints is set, this determines whether the RewriteStatepointsForGC pass should rewrite thi...
Definition: GCStrategy.h:79
const std::string & getName() const
Return the name of the GC strategy.
Definition: GCStrategy.h:88
virtual ~GCStrategy()=default
A global registry used in conjunction with static constructors to make pluggable components (like tar...
Definition: Registry.h:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::unique_ptr< GCStrategy > getGCStrategy(const StringRef Name)
Lookup the GCStrategy object associated with the given gc name.
Definition: GCStrategy.cpp:24