16948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar//===------ OrcTestCommon.h - Utilities for Orc Unit Tests ------*- C++ -*-===//
26948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar//
36948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar//                     The LLVM Compiler Infrastructure
46948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar//
56948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar// This file is distributed under the University of Illinois Open Source
66948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar// License. See LICENSE.TXT for details.
76948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar//
86948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar//===----------------------------------------------------------------------===//
96948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar//
106948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar// Common utilities for the Orc unit tests.
116948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar//
126948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar//===----------------------------------------------------------------------===//
136948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar
146948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar
156948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar#ifndef LLVM_UNITTESTS_EXECUTIONENGINE_ORC_ORCTESTCOMMON_H
166948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar#define LLVM_UNITTESTS_EXECUTIONENGINE_ORC_ORCTESTCOMMON_H
176948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar
186948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar#include "llvm/IR/Function.h"
196948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar#include "llvm/IR/IRBuilder.h"
206948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar#include "llvm/IR/LLVMContext.h"
216948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar#include "llvm/IR/Module.h"
226948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar#include "llvm/IR/TypeBuilder.h"
23cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar#include "llvm/ExecutionEngine/ExecutionEngine.h"
24cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar#include "llvm/ExecutionEngine/Orc/JITSymbol.h"
25cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar#include "llvm/Support/TargetSelect.h"
266948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar#include <memory>
276948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar
286948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainarnamespace llvm {
296948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar
30cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar// Base class for Orc tests that will execute code.
31cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainarclass OrcExecutionTest {
32cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainarpublic:
336948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar
34cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  OrcExecutionTest() {
35cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    if (!NativeTargetInitialized) {
36cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar      InitializeNativeTarget();
37cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar      InitializeNativeTargetAsmParser();
38cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar      InitializeNativeTargetAsmPrinter();
39cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar      NativeTargetInitialized = true;
406948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar    }
416948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar
42cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    // Try to select a TargetMachine for the host.
43cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    TM.reset(EngineBuilder().selectTarget());
446948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar
45cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    if (TM) {
46cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar      // If we found a TargetMachine, check that it's one that Orc supports.
47cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar      const Triple& TT = TM->getTargetTriple();
48cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar      if (TT.getArch() != Triple::x86_64 || !TT.isOSDarwin())
49cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar        TM = nullptr;
50cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    }
516948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  };
526948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar
53cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainarprotected:
54cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  std::unique_ptr<TargetMachine> TM;
55cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainarprivate:
56cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  static bool NativeTargetInitialized;
57cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar};
586948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar
59cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainarclass ModuleBuilder {
60cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainarpublic:
61cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  ModuleBuilder(LLVMContext &Context, StringRef Triple,
62cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar                StringRef Name);
63cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
64cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  template <typename FuncType>
65cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  Function* createFunctionDecl(StringRef Name) {
66cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    return Function::Create(
67cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar             TypeBuilder<FuncType, false>::get(M->getContext()),
68cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar             GlobalValue::ExternalLinkage, Name, M.get());
69cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  }
70cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
71cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  Module* getModule() { return M.get(); }
72cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  const Module* getModule() const { return M.get(); }
73cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  std::unique_ptr<Module> takeModule() { return std::move(M); }
74cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
75cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainarprivate:
76cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  std::unique_ptr<Module> M;
77cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  IRBuilder<> Builder;
78cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar};
79cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
80cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar// Dummy struct type.
81cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainarstruct DummyStruct {
82cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  int X[256];
83cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar};
84cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
85cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar// TypeBuilder specialization for DummyStruct.
86cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainartemplate <bool XCompile>
87cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainarclass TypeBuilder<DummyStruct, XCompile> {
88cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainarpublic:
89cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  static StructType *get(LLVMContext &Context) {
90cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    return StructType::get(
91cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar      TypeBuilder<types::i<32>[256], XCompile>::get(Context), nullptr);
92cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  }
93cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar};
94cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
95cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainartemplate <typename HandleT,
96cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar          typename AddModuleSetFtor,
97cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar          typename RemoveModuleSetFtor,
98cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar          typename FindSymbolFtor,
99cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar          typename FindSymbolInFtor>
100cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainarclass MockBaseLayer {
101cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainarpublic:
102cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
103cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  typedef HandleT ModuleSetHandleT;
104cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
105cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  MockBaseLayer(AddModuleSetFtor &&AddModuleSet,
106cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar                RemoveModuleSetFtor &&RemoveModuleSet,
107cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar                FindSymbolFtor &&FindSymbol,
108cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar                FindSymbolInFtor &&FindSymbolIn)
109cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar      : AddModuleSet(AddModuleSet), RemoveModuleSet(RemoveModuleSet),
110cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar        FindSymbol(FindSymbol), FindSymbolIn(FindSymbolIn)
111cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  {}
112cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
113cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  template <typename ModuleSetT, typename MemoryManagerPtrT,
114cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar            typename SymbolResolverPtrT>
115cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  ModuleSetHandleT addModuleSet(ModuleSetT Ms, MemoryManagerPtrT MemMgr,
116cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar                                SymbolResolverPtrT Resolver) {
117cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    return AddModuleSet(std::move(Ms), std::move(MemMgr), std::move(Resolver));
118cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  }
119cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
120cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  void removeModuleSet(ModuleSetHandleT H) {
121cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    RemoveModuleSet(H);
122cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  }
123cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
124cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  orc::JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
125cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    return FindSymbol(Name, ExportedSymbolsOnly);
126cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  }
127cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
128cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  orc::JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name,
129cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar                         bool ExportedSymbolsOnly) {
130cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    return FindSymbolIn(H, Name, ExportedSymbolsOnly);
131cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  }
132cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
133cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainarprivate:
134cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  AddModuleSetFtor AddModuleSet;
135cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  RemoveModuleSetFtor RemoveModuleSet;
136cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  FindSymbolFtor FindSymbol;
137cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  FindSymbolInFtor FindSymbolIn;
138cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar};
139cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
140cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainartemplate <typename ModuleSetHandleT,
141cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar          typename AddModuleSetFtor,
142cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar          typename RemoveModuleSetFtor,
143cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar          typename FindSymbolFtor,
144cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar          typename FindSymbolInFtor>
145cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga NainarMockBaseLayer<ModuleSetHandleT, AddModuleSetFtor, RemoveModuleSetFtor,
146cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar              FindSymbolFtor, FindSymbolInFtor>
147cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga NainarcreateMockBaseLayer(AddModuleSetFtor &&AddModuleSet,
148cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar                    RemoveModuleSetFtor &&RemoveModuleSet,
149cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar                    FindSymbolFtor &&FindSymbol,
150cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar                    FindSymbolInFtor &&FindSymbolIn) {
151cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  return MockBaseLayer<ModuleSetHandleT, AddModuleSetFtor, RemoveModuleSetFtor,
152cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar                       FindSymbolFtor, FindSymbolInFtor>(
153cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar                         std::forward<AddModuleSetFtor>(AddModuleSet),
154cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar                         std::forward<RemoveModuleSetFtor>(RemoveModuleSet),
155cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar                         std::forward<FindSymbolFtor>(FindSymbol),
156cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar                         std::forward<FindSymbolInFtor>(FindSymbolIn));
157cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar}
158cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
159cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainartemplate <typename ReturnT>
160cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainarclass DoNothingAndReturn {
161cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainarpublic:
162cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  DoNothingAndReturn(ReturnT Val) : Val(Val) {}
163cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
164cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  template <typename... Args>
165cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  ReturnT operator()(Args...) const { return Val; }
166cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainarprivate:
167cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  ReturnT Val;
168cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar};
1696948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar
170cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainartemplate <>
171cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainarclass DoNothingAndReturn<void> {
172cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainarpublic:
173cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  template <typename... Args>
174cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  void operator()(Args...) const { }
175cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar};
1766948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar
1776948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar} // namespace llvm
1786948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar
1796948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar#endif
180