109109#include " llvm/IR/PatternMatch.h"
110110#include " llvm/IR/User.h"
111111#include " llvm/IR/Value.h"
112+ #include " llvm/SandboxIR/Module.h"
112113#include " llvm/SandboxIR/Tracker.h"
113114#include " llvm/SandboxIR/Type.h"
114115#include " llvm/SandboxIR/Use.h"
@@ -138,6 +139,7 @@ class ConstantPtrAuth;
138139class ConstantExpr ;
139140class Context ;
140141class Function ;
142+ class Module ;
141143class Instruction ;
142144class VAArgInst ;
143145class FreezeInst ;
@@ -347,7 +349,7 @@ class Value {
347349 friend class ConstantPtrAuth ; // For `Val`.
348350 friend class ConstantExpr ; // For `Val`.
349351 friend class Utils ; // For `Val`.
350-
352+ friend class Module ; // For `Val`.
351353 // Region needs to manipulate metadata in the underlying LLVM Value, we don't
352354 // expose metadata in sandboxir.
353355 friend class Region ;
@@ -1322,7 +1324,10 @@ class GlobalWithNodeAPI : public ParentT {
13221324 GlobalWithNodeAPI (Value::ClassID ID, LLVMParentT *C, Context &Ctx)
13231325 : ParentT(ID, C, Ctx) {}
13241326
1325- // TODO: Missing getParent(). Should be added once Module is available.
1327+ Module *getParent () const {
1328+ llvm::Module *LLVMM = cast<LLVMGlobalT>(this ->Val )->getParent ();
1329+ return this ->Ctx .getModule (LLVMM);
1330+ }
13261331
13271332 using iterator = mapped_iterator<
13281333 decltype (static_cast <LLVMGlobalT *>(nullptr )->getIterator ()), LLVMGVToGV>;
@@ -4556,6 +4561,9 @@ class Context {
45564561 DenseMap<llvm::Value *, std::unique_ptr<sandboxir::Value>>
45574562 LLVMValueToValueMap;
45584563
4564+ // / Maps an LLVM Module to the corresponding sandboxir::Module.
4565+ DenseMap<llvm::Module *, std::unique_ptr<Module>> LLVMModuleToModuleMap;
4566+
45594567 // / Type has a protected destructor to prohibit the user from managing the
45604568 // / lifetime of the Type objects. Context is friend of Type, and this custom
45614569 // / deleter can destroy Type.
@@ -4699,6 +4707,10 @@ class Context {
46994707 return getValue (const_cast <llvm::Value *>(V));
47004708 }
47014709
4710+ Module *getModule (llvm::Module *LLVMM) const ;
4711+
4712+ Module *getOrCreateModule (llvm::Module *LLVMM);
4713+
47024714 Type *getType (llvm::Type *LLVMTy) {
47034715 if (LLVMTy == nullptr )
47044716 return nullptr ;
@@ -4712,8 +4724,13 @@ class Context {
47124724 // / Create a sandboxir::Function for an existing LLVM IR \p F, including all
47134725 // / blocks and instructions.
47144726 // / This is the main API function for creating Sandbox IR.
4727+ // / Note: this will not fully populate its parent module. The only globals
4728+ // / that will be available are those used within the function.
47154729 Function *createFunction (llvm::Function *F);
47164730
4731+ // / Create a sandboxir::Module corresponding to \p LLVMM.
4732+ Module *createModule (llvm::Module *LLVMM);
4733+
47174734 // / \Returns the number of values registered with Context.
47184735 size_t getNumValues () const { return LLVMValueToValueMap.size (); }
47194736};
@@ -4739,6 +4756,10 @@ class Function : public GlobalWithNodeAPI<Function, llvm::Function,
47394756 return From->getSubclassID () == ClassID::Function;
47404757 }
47414758
4759+ Module *getParent () {
4760+ return Ctx.getModule (cast<llvm::Function>(Val)->getParent ());
4761+ }
4762+
47424763 Argument *getArg (unsigned Idx) const {
47434764 llvm::Argument *Arg = cast<llvm::Function>(Val)->getArg (Idx);
47444765 return cast<Argument>(Ctx.getValue (Arg));
0 commit comments