BuiltinGCs.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //===- BuiltinGCs.cpp - Boilerplate for our built in GC types -------------===//
  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 contains the boilerplate required to define our various built in
  10. // gc lowering strategies.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/IR/BuiltinGCs.h"
  14. #include "llvm/IR/GCStrategy.h"
  15. #include "llvm/IR/DerivedTypes.h"
  16. #include "llvm/Support/Casting.h"
  17. using namespace llvm;
  18. namespace {
  19. /// An example GC which attempts to be compatible with Erlang/OTP garbage
  20. /// collector.
  21. ///
  22. /// The frametable emitter is in ErlangGCPrinter.cpp.
  23. class ErlangGC : public GCStrategy {
  24. public:
  25. ErlangGC() {
  26. NeededSafePoints = true;
  27. UsesMetadata = true;
  28. }
  29. };
  30. /// An example GC which attempts to be compatible with Objective Caml 3.10.0
  31. ///
  32. /// The frametable emitter is in OcamlGCPrinter.cpp.
  33. class OcamlGC : public GCStrategy {
  34. public:
  35. OcamlGC() {
  36. NeededSafePoints = true;
  37. UsesMetadata = true;
  38. }
  39. };
  40. /// A GC strategy for uncooperative targets. This implements lowering for the
  41. /// llvm.gc* intrinsics for targets that do not natively support them (which
  42. /// includes the C backend). Note that the code generated is not quite as
  43. /// efficient as algorithms which generate stack maps to identify roots.
  44. ///
  45. /// In order to support this particular transformation, all stack roots are
  46. /// coallocated in the stack. This allows a fully target-independent stack map
  47. /// while introducing only minor runtime overhead.
  48. class ShadowStackGC : public GCStrategy {
  49. public:
  50. ShadowStackGC() = default;
  51. };
  52. /// A GCStrategy which serves as an example for the usage of a statepoint based
  53. /// lowering strategy. This GCStrategy is intended to suitable as a default
  54. /// implementation usable with any collector which can consume the standard
  55. /// stackmap format generated by statepoints, uses the default addrespace to
  56. /// distinguish between gc managed and non-gc managed pointers, and has
  57. /// reasonable relocation semantics.
  58. class StatepointGC : public GCStrategy {
  59. public:
  60. StatepointGC() {
  61. UseStatepoints = true;
  62. UseRS4GC = true;
  63. // These options are all gc.root specific, we specify them so that the
  64. // gc.root lowering code doesn't run.
  65. NeededSafePoints = false;
  66. UsesMetadata = false;
  67. }
  68. std::optional<bool> isGCManagedPointer(const Type *Ty) const override {
  69. // Method is only valid on pointer typed values.
  70. const PointerType *PT = cast<PointerType>(Ty);
  71. // For the sake of this example GC, we arbitrarily pick addrspace(1) as our
  72. // GC managed heap. We know that a pointer into this heap needs to be
  73. // updated and that no other pointer does. Note that addrspace(1) is used
  74. // only as an example, it has no special meaning, and is not reserved for
  75. // GC usage.
  76. return (1 == PT->getAddressSpace());
  77. }
  78. };
  79. /// A GCStrategy for the CoreCLR Runtime. The strategy is similar to
  80. /// Statepoint-example GC, but differs from it in certain aspects, such as:
  81. /// 1) Base-pointers need not be explicitly tracked and reported for
  82. /// interior pointers
  83. /// 2) Uses a different format for encoding stack-maps
  84. /// 3) Location of Safe-point polls: polls are only needed before loop-back
  85. /// edges and before tail-calls (not needed at function-entry)
  86. ///
  87. /// The above differences in behavior are to be implemented in upcoming
  88. /// checkins.
  89. class CoreCLRGC : public GCStrategy {
  90. public:
  91. CoreCLRGC() {
  92. UseStatepoints = true;
  93. UseRS4GC = true;
  94. // These options are all gc.root specific, we specify them so that the
  95. // gc.root lowering code doesn't run.
  96. NeededSafePoints = false;
  97. UsesMetadata = false;
  98. }
  99. std::optional<bool> isGCManagedPointer(const Type *Ty) const override {
  100. // Method is only valid on pointer typed values.
  101. const PointerType *PT = cast<PointerType>(Ty);
  102. // We pick addrspace(1) as our GC managed heap.
  103. return (1 == PT->getAddressSpace());
  104. }
  105. };
  106. } // end anonymous namespace
  107. // Register all the above so that they can be found at runtime. Note that
  108. // these static initializers are important since the registration list is
  109. // constructed from their storage.
  110. static GCRegistry::Add<ErlangGC> A("erlang",
  111. "erlang-compatible garbage collector");
  112. static GCRegistry::Add<OcamlGC> B("ocaml", "ocaml 3.10-compatible GC");
  113. static GCRegistry::Add<ShadowStackGC>
  114. C("shadow-stack", "Very portable GC for uncooperative code generators");
  115. static GCRegistry::Add<StatepointGC> D("statepoint-example",
  116. "an example strategy for statepoint");
  117. static GCRegistry::Add<CoreCLRGC> E("coreclr", "CoreCLR-compatible GC");
  118. // Provide hook to ensure the containing library is fully loaded.
  119. void llvm::linkAllBuiltinGCs() {}