GCStrategy.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. #include "llvm/IR/GCStrategy.h"
  14. #include "llvm/ADT/Twine.h"
  15. #include "llvm/IR/BuiltinGCs.h"
  16. using namespace llvm;
  17. LLVM_INSTANTIATE_REGISTRY(GCRegistry)
  18. GCStrategy::GCStrategy() = default;
  19. std::unique_ptr<GCStrategy> llvm::getGCStrategy(const StringRef Name) {
  20. for (auto &S : GCRegistry::entries())
  21. if (S.getName() == Name)
  22. return S.instantiate();
  23. // We need to link all the builtin GCs when LLVM is used as a static library.
  24. // The linker will quite happily remove the static constructors that register
  25. // the builtin GCs if we don't use a function from that object. This function
  26. // does nothing but we need to make sure it is (or at least could be, even
  27. // with all optimisations enabled) called *somewhere*, and this is a good
  28. // place to do that: if the GC strategies are being used then this function
  29. // obviously can't be removed by the linker, and here it won't affect
  30. // performance, since there's about to be a fatal error anyway.
  31. llvm::linkAllBuiltinGCs();
  32. if (GCRegistry::begin() == GCRegistry::end()) {
  33. // In normal operation, the registry should not be empty. There should
  34. // be the builtin GCs if nothing else. The most likely scenario here is
  35. // that we got here without running the initializers used by the Registry
  36. // itself and it's registration mechanism.
  37. const std::string error =
  38. std::string("unsupported GC: ") + Name.str() +
  39. " (did you remember to link and initialize the library?)";
  40. report_fatal_error(Twine(error));
  41. } else
  42. report_fatal_error(Twine(std::string("unsupported GC: ") + Name.str()));
  43. }