BuiltinGCs.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 compatibile 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() {}
  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. // These options are all gc.root specific, we specify them so that the
  63. // gc.root lowering code doesn't run.
  64. NeededSafePoints = false;
  65. UsesMetadata = false;
  66. }
  67. Optional<bool> isGCManagedPointer(const Type *Ty) const override {
  68. // Method is only valid on pointer typed values.
  69. const PointerType *PT = cast<PointerType>(Ty);
  70. // For the sake of this example GC, we arbitrarily pick addrspace(1) as our
  71. // GC managed heap. We know that a pointer into this heap needs to be
  72. // updated and that no other pointer does. Note that addrspace(1) is used
  73. // only as an example, it has no special meaning, and is not reserved for
  74. // GC usage.
  75. return (1 == PT->getAddressSpace());
  76. }
  77. };
  78. /// A GCStrategy for the CoreCLR Runtime. The strategy is similar to
  79. /// Statepoint-example GC, but differs from it in certain aspects, such as:
  80. /// 1) Base-pointers need not be explicitly tracked and reported for
  81. /// interior pointers
  82. /// 2) Uses a different format for encoding stack-maps
  83. /// 3) Location of Safe-point polls: polls are only needed before loop-back
  84. /// edges and before tail-calls (not needed at function-entry)
  85. ///
  86. /// The above differences in behavior are to be implemented in upcoming
  87. /// checkins.
  88. class CoreCLRGC : public GCStrategy {
  89. public:
  90. CoreCLRGC() {
  91. UseStatepoints = true;
  92. // These options are all gc.root specific, we specify them so that the
  93. // gc.root lowering code doesn't run.
  94. NeededSafePoints = false;
  95. UsesMetadata = false;
  96. }
  97. Optional<bool> isGCManagedPointer(const Type *Ty) const override {
  98. // Method is only valid on pointer typed values.
  99. const PointerType *PT = cast<PointerType>(Ty);
  100. // We pick addrspace(1) as our GC managed heap.
  101. return (1 == PT->getAddressSpace());
  102. }
  103. };
  104. } // end anonymous namespace
  105. // Register all the above so that they can be found at runtime. Note that
  106. // these static initializers are important since the registration list is
  107. // constructed from their storage.
  108. static GCRegistry::Add<ErlangGC> A("erlang",
  109. "erlang-compatible garbage collector");
  110. static GCRegistry::Add<OcamlGC> B("ocaml", "ocaml 3.10-compatible GC");
  111. static GCRegistry::Add<ShadowStackGC>
  112. C("shadow-stack", "Very portable GC for uncooperative code generators");
  113. static GCRegistry::Add<StatepointGC> D("statepoint-example",
  114. "an example strategy for statepoint");
  115. static GCRegistry::Add<CoreCLRGC> E("coreclr", "CoreCLR-compatible GC");
  116. // Provide hook to ensure the containing library is fully loaded.
  117. void llvm::linkAllBuiltinGCs() {}