1 //===---- MaterializationUnit.h -- Materialization Black Box ----*- C++ -*-===// 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 // MaterializationUnit class and related types and operations. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_EXECUTIONENGINE_ORC_MATERIALIZATIONUNIT_H 14 #define LLVM_EXECUTIONENGINE_ORC_MATERIALIZATIONUNIT_H 15 16 #include "llvm/ADT/StringRef.h" 17 #include "llvm/ExecutionEngine/Orc/CoreContainers.h" 18 #include "llvm/ExecutionEngine/Orc/SymbolStringPool.h" 19 #include "llvm/Support/Compiler.h" 20 21 namespace llvm::orc { 22 23 class MaterializationResponsibility; 24 25 /// A MaterializationUnit represents a set of symbol definitions that can 26 /// be materialized as a group, or individually discarded (when 27 /// overriding definitions are encountered). 28 /// 29 /// MaterializationUnits are used when providing lazy definitions of symbols to 30 /// JITDylibs. The JITDylib will call materialize when the address of a symbol 31 /// is requested via the lookup method. The JITDylib will call discard if a 32 /// stronger definition is added or already present. 33 class LLVM_ABI MaterializationUnit { 34 friend class ExecutionSession; 35 friend class JITDylib; 36 37 public: 38 static char ID; 39 40 struct Interface { 41 Interface() = default; InterfaceInterface42 Interface(SymbolFlagsMap InitalSymbolFlags, SymbolStringPtr InitSymbol) 43 : SymbolFlags(std::move(InitalSymbolFlags)), 44 InitSymbol(std::move(InitSymbol)) { 45 assert((!this->InitSymbol || this->SymbolFlags.count(this->InitSymbol)) && 46 "If set, InitSymbol should appear in InitialSymbolFlags map"); 47 } 48 49 SymbolFlagsMap SymbolFlags; 50 SymbolStringPtr InitSymbol; 51 }; 52 MaterializationUnit(Interface I)53 MaterializationUnit(Interface I) 54 : SymbolFlags(std::move(I.SymbolFlags)), 55 InitSymbol(std::move(I.InitSymbol)) {} 56 virtual ~MaterializationUnit() = default; 57 58 /// Return the name of this materialization unit. Useful for debugging 59 /// output. 60 virtual StringRef getName() const = 0; 61 62 /// Return the set of symbols that this source provides. getSymbols()63 const SymbolFlagsMap &getSymbols() const { return SymbolFlags; } 64 65 /// Returns the initialization symbol for this MaterializationUnit (if any). getInitializerSymbol()66 const SymbolStringPtr &getInitializerSymbol() const { return InitSymbol; } 67 68 /// Implementations of this method should materialize all symbols 69 /// in the materialzation unit, except for those that have been 70 /// previously discarded. 71 virtual void 72 materialize(std::unique_ptr<MaterializationResponsibility> R) = 0; 73 74 /// Called by JITDylibs to notify MaterializationUnits that the given symbol 75 /// has been overridden. doDiscard(const JITDylib & JD,const SymbolStringPtr & Name)76 void doDiscard(const JITDylib &JD, const SymbolStringPtr &Name) { 77 SymbolFlags.erase(Name); 78 if (InitSymbol == Name) { 79 DEBUG_WITH_TYPE("orc", { 80 dbgs() << "In " << getName() << ": discarding init symbol \"" 81 << *Name << "\"\n"; 82 }); 83 InitSymbol = nullptr; 84 } 85 discard(JD, std::move(Name)); 86 } 87 88 protected: 89 SymbolFlagsMap SymbolFlags; 90 SymbolStringPtr InitSymbol; 91 92 private: 93 virtual void anchor(); 94 95 /// Implementations of this method should discard the given symbol 96 /// from the source (e.g. if the source is an LLVM IR Module and the 97 /// symbol is a function, delete the function body or mark it available 98 /// externally). 99 virtual void discard(const JITDylib &JD, const SymbolStringPtr &Name) = 0; 100 }; 101 102 } // namespace llvm::orc 103 104 #endif // LLVM_EXECUTIONENGINE_ORC_MATERIALIZATIONUNIT_H 105