LLVM 22.0.0git
GCStrategy.cpp
Go to the documentation of this file.
1//===- GCStrategy.cpp - Garbage Collector Description ---------------------===//
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// This file implements the policy object GCStrategy which describes the
10// behavior of a given garbage collector.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/IR/GCStrategy.h"
15#include "llvm/ADT/Twine.h"
16#include "llvm/IR/BuiltinGCs.h"
17
18using namespace llvm;
19
21
22GCStrategy::GCStrategy() = default;
23
24std::unique_ptr<GCStrategy> llvm::getGCStrategy(const StringRef Name) {
25 for (auto &S : GCRegistry::entries())
26 if (S.getName() == Name)
27 return S.instantiate();
28
29 // We need to link all the builtin GCs when LLVM is used as a static library.
30 // The linker will quite happily remove the static constructors that register
31 // the builtin GCs if we don't use a function from that object. This function
32 // does nothing but we need to make sure it is (or at least could be, even
33 // with all optimisations enabled) called *somewhere*, and this is a good
34 // place to do that: if the GC strategies are being used then this function
35 // obviously can't be removed by the linker, and here it won't affect
36 // performance, since there's about to be a fatal error anyway.
38
40 // In normal operation, the registry should not be empty. There should
41 // be the builtin GCs if nothing else. The most likely scenario here is
42 // that we got here without running the initializers used by the Registry
43 // itself and it's registration mechanism.
45 "unsupported GC: " + Name +
46 " (did you remember to link and initialize the library?)");
47 } else
48 report_fatal_error(Twine("unsupported GC: ") + Name);
49}
#define LLVM_INSTANTIATE_REGISTRY(REGISTRY_CLASS)
Definition Registry.h:153
LLVM_ABI GCStrategy()
static iterator end()
Definition Registry.h:111
static iterator_range< iterator > entries()
Definition Registry.h:113
static iterator begin()
Definition Registry.h:110
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
This is an optimization pass for GlobalISel generic memory operations.
Registry< GCStrategy > GCRegistry
Subclasses of GCStrategy are made available for use during compilation by adding them to the global G...
Definition GCStrategy.h:144
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
LLVM_ABI void linkAllBuiltinGCs()
FIXME: Collector instances are not useful on their own.
LLVM_ABI std::unique_ptr< GCStrategy > getGCStrategy(const StringRef Name)
Lookup the GCStrategy object associated with the given gc name.