GCStrategy.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/CodeGen/GCStrategy.h - Garbage collection -----------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // GCStrategy coordinates code generation algorithms and implements some itself
  15. // in order to generate code compatible with a target code generator as
  16. // specified in a function's 'gc' attribute. Algorithms are enabled by setting
  17. // flags in a subclass's constructor, and some virtual methods can be
  18. // overridden.
  19. //
  20. // GCStrategy is relevant for implementations using either gc.root or
  21. // gc.statepoint based lowering strategies, but is currently focused mostly on
  22. // options for gc.root. This will change over time.
  23. //
  24. // When requested by a subclass of GCStrategy, the gc.root implementation will
  25. // populate GCModuleInfo and GCFunctionInfo with that about each Function in
  26. // the Module that opts in to garbage collection. Specifically:
  27. //
  28. // - Safe points
  29. // Garbage collection is generally only possible at certain points in code.
  30. // GCStrategy can request that the collector insert such points:
  31. //
  32. // - At and after any call to a subroutine
  33. // - Before returning from the current function
  34. // - Before backwards branches (loops)
  35. //
  36. // - Roots
  37. // When a reference to a GC-allocated object exists on the stack, it must be
  38. // stored in an alloca registered with llvm.gcoot.
  39. //
  40. // This information can used to emit the metadata tables which are required by
  41. // the target garbage collector runtime.
  42. //
  43. // When used with gc.statepoint, information about safepoint and roots can be
  44. // found in the binary StackMap section after code generation. Safepoint
  45. // placement is currently the responsibility of the frontend, though late
  46. // insertion support is planned.
  47. //
  48. // The read and write barrier support can be used with either implementation.
  49. //
  50. //===----------------------------------------------------------------------===//
  51. #ifndef LLVM_IR_GCSTRATEGY_H
  52. #define LLVM_IR_GCSTRATEGY_H
  53. #include "llvm/Support/Registry.h"
  54. #include <optional>
  55. #include <string>
  56. namespace llvm {
  57. class Type;
  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.
  63. class GCStrategy {
  64. private:
  65. friend class GCModuleInfo;
  66. std::string Name;
  67. protected:
  68. bool UseStatepoints = false; /// Uses gc.statepoints as opposed to gc.roots,
  69. /// if set, NeededSafePoints and UsesMetadata
  70. /// should be left at their default values.
  71. bool UseRS4GC = false; /// If UseStatepoints is set, this determines whether
  72. /// the RewriteStatepointsForGC pass should rewrite
  73. /// this function's calls.
  74. /// This should only be set if UseStatepoints is set.
  75. bool NeededSafePoints = false; ///< if set, calls are inferred to be safepoints
  76. bool UsesMetadata = false; ///< If set, backend must emit metadata tables.
  77. public:
  78. GCStrategy();
  79. virtual ~GCStrategy() = default;
  80. /// Return the name of the GC strategy. This is the value of the collector
  81. /// name string specified on functions which use this strategy.
  82. const std::string &getName() const { return Name; }
  83. /// Returns true if this strategy is expecting the use of gc.statepoints,
  84. /// and false otherwise.
  85. bool useStatepoints() const { return UseStatepoints; }
  86. /** @name Statepoint Specific Properties */
  87. ///@{
  88. /// If the type specified can be reliably distinguished, returns true for
  89. /// pointers to GC managed locations and false for pointers to non-GC
  90. /// managed locations. Note a GCStrategy can always return 'std::nullopt'
  91. /// (i.e. an empty optional indicating it can't reliably distinguish.
  92. virtual std::optional<bool> isGCManagedPointer(const Type *Ty) const {
  93. return std::nullopt;
  94. }
  95. /// Returns true if the RewriteStatepointsForGC pass should run on functions
  96. /// using this GC.
  97. bool useRS4GC() const {
  98. assert(useStatepoints() &&
  99. "GC strategy has useRS4GC but not useStatepoints set");
  100. return UseRS4GC;
  101. }
  102. ///@}
  103. /// If set, appropriate metadata tables must be emitted by the back-end
  104. /// (assembler, JIT, or otherwise). The default stackmap information can be
  105. /// found in the StackMap section as described in the documentation.
  106. bool usesMetadata() const { return UsesMetadata; }
  107. /** @name GCRoot Specific Properties
  108. * These properties and overrides only apply to collector strategies using
  109. * GCRoot.
  110. */
  111. ///@{
  112. /// True if safe points need to be inferred on call sites
  113. bool needsSafePoints() const { return NeededSafePoints; }
  114. ///@}
  115. };
  116. /// Subclasses of GCStrategy are made available for use during compilation by
  117. /// adding them to the global GCRegistry. This can done either within the
  118. /// LLVM source tree or via a loadable plugin. An example registeration
  119. /// would be:
  120. /// static GCRegistry::Add<CustomGC> X("custom-name",
  121. /// "my custom supper fancy gc strategy");
  122. ///
  123. /// Note that to use a custom GCMetadataPrinter, you must also
  124. /// register your GCMetadataPrinter subclass with the
  125. /// GCMetadataPrinterRegistery as well.
  126. using GCRegistry = Registry<GCStrategy>;
  127. /// Lookup the GCStrategy object associated with the given gc name.
  128. std::unique_ptr<GCStrategy> getGCStrategy(const StringRef Name);
  129. } // end namespace llvm
  130. #endif // LLVM_IR_GCSTRATEGY_H
  131. #ifdef __GNUC__
  132. #pragma GCC diagnostic pop
  133. #endif