Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:54

0001 //===- RedirectionManager.h - Redirection manager interface -----*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // Redirection manager interface that redirects a call to symbol to another.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_EXECUTIONENGINE_ORC_REDIRECTIONMANAGER_H
0014 #define LLVM_EXECUTIONENGINE_ORC_REDIRECTIONMANAGER_H
0015 
0016 #include "llvm/ExecutionEngine/Orc/Core.h"
0017 
0018 namespace llvm {
0019 namespace orc {
0020 
0021 /// Base class for performing redirection of call to symbol to another symbol in
0022 /// runtime.
0023 class RedirectionManager {
0024 public:
0025   virtual ~RedirectionManager() = default;
0026 
0027   /// Change the redirection destination of given symbols to new destination
0028   /// symbols.
0029   virtual Error redirect(JITDylib &JD, const SymbolMap &NewDests) = 0;
0030 
0031   /// Change the redirection destination of given symbol to new destination
0032   /// symbol.
0033   Error redirect(JITDylib &JD, SymbolStringPtr Symbol,
0034                  ExecutorSymbolDef NewDest) {
0035     return redirect(JD, {{std::move(Symbol), NewDest}});
0036   }
0037 
0038 private:
0039   virtual void anchor();
0040 };
0041 
0042 /// Base class for managing redirectable symbols in which a call
0043 /// gets redirected to another symbol in runtime.
0044 class RedirectableSymbolManager : public RedirectionManager {
0045 public:
0046   /// Create redirectable symbols with given symbol names and initial
0047   /// desitnation symbol addresses.
0048   Error createRedirectableSymbols(ResourceTrackerSP RT, SymbolMap InitialDests);
0049 
0050   /// Create a single redirectable symbol with given symbol name and initial
0051   /// desitnation symbol address.
0052   Error createRedirectableSymbol(ResourceTrackerSP RT, SymbolStringPtr Symbol,
0053                                  ExecutorSymbolDef InitialDest) {
0054     return createRedirectableSymbols(RT, {{std::move(Symbol), InitialDest}});
0055   }
0056 
0057   /// Emit redirectable symbol
0058   virtual void
0059   emitRedirectableSymbols(std::unique_ptr<MaterializationResponsibility> MR,
0060                           SymbolMap InitialDests) = 0;
0061 };
0062 
0063 /// RedirectableMaterializationUnit materializes redirectable symbol
0064 /// by invoking RedirectableSymbolManager::emitRedirectableSymbols
0065 class RedirectableMaterializationUnit : public MaterializationUnit {
0066 public:
0067   RedirectableMaterializationUnit(RedirectableSymbolManager &RM,
0068                                   SymbolMap InitialDests)
0069       : MaterializationUnit(convertToFlags(InitialDests)), RM(RM),
0070         InitialDests(std::move(InitialDests)) {}
0071 
0072   StringRef getName() const override {
0073     return "RedirectableSymbolMaterializationUnit";
0074   }
0075 
0076   void materialize(std::unique_ptr<MaterializationResponsibility> R) override {
0077     RM.emitRedirectableSymbols(std::move(R), std::move(InitialDests));
0078   }
0079 
0080   void discard(const JITDylib &JD, const SymbolStringPtr &Name) override {
0081     InitialDests.erase(Name);
0082   }
0083 
0084 private:
0085   static MaterializationUnit::Interface
0086   convertToFlags(const SymbolMap &InitialDests) {
0087     SymbolFlagsMap Flags;
0088     for (auto [K, V] : InitialDests)
0089       Flags[K] = V.getFlags();
0090     return MaterializationUnit::Interface(Flags, {});
0091   }
0092 
0093   RedirectableSymbolManager &RM;
0094   SymbolMap InitialDests;
0095 };
0096 
0097 } // namespace orc
0098 } // namespace llvm
0099 
0100 #endif