Spicy
jit.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <iostream>
6 #include <map>
7 #include <memory>
8 #include <string>
9 #include <utility>
10 #include <vector>
11 
12 #include <hilti/rt/filesystem.h>
13 #include <hilti/rt/library.h>
14 
15 #include <hilti/base/util.h>
16 #include <hilti/compiler/context.h>
17 #include <hilti/compiler/detail/cxx/unit.h>
18 
19 namespace reproc {
20 class process;
21 }
22 
23 namespace hilti {
24 
25 namespace logging::debug {
26 inline const DebugStream Jit("jit");
27 } // namespace logging::debug
28 
29 namespace detail::jit {
30 class Cxx;
31 } // namespace detail::jit
32 
34 class CxxCode {
35 public:
41  CxxCode(const hilti::rt::filesystem::path& path) { load(path); }
42 
49  CxxCode(const std::string& id, std::istream& code) { load(id, code); }
50 
56  explicit CxxCode(const detail::cxx::Unit& u);
57 
64  bool save(const hilti::rt::filesystem::path& p) const;
65 
72  bool save(std::ostream& out) const;
73 
75  auto code() const { return _code; }
76 
78  auto isLoaded() const { return _code.has_value(); }
79 
85  const std::string& id() const { return _id; }
86 
87 protected:
94  bool load(const hilti::rt::filesystem::path& path);
95 
103  bool load(const std::string& id, std::istream& in);
104 
105 private:
106  std::string _id;
107  std::optional<std::string> _code;
108 };
109 
110 using hilti::rt::Library;
111 
118 class JIT {
119 public:
124  explicit JIT(std::shared_ptr<Context> context, bool dump_code = false);
125  ~JIT();
126 
127  JIT() = delete;
128  JIT(const JIT&) = delete;
129  JIT(JIT&&) noexcept = delete;
130  JIT& operator=(const JIT&) = delete;
131  JIT& operator=(JIT&&) noexcept = delete;
132 
139  void add(CxxCode d) { _codes.push_back(std::move(d)); }
140 
147  void add(const hilti::rt::filesystem::path& p) { _files.push_back(p); }
148 
153  bool hasInputs() { return _codes.size() || _files.size(); }
154 
161 
163  auto context() const { return _context; }
164 
166  auto options() const { return _context->options(); }
167 
168 private:
169  // Check if we have a working compiler.
170  hilti::Result<Nothing> _checkCompiler();
171 
172  // Prepare for compilation.
173  hilti::Result<Nothing> _initialize();
174 
175  // Compile C++ to object files.
176  hilti::Result<Nothing> _compile();
177 
178  // Link object files into shared library.
180 
181  // Clean up after compilation.
182  void _finish();
183 
184  using JobID = uint64_t;
185  Result<JobID> _spawnJob(hilti::rt::filesystem::path cmd, std::vector<std::string> args);
186  Result<Nothing> _waitForJobs();
187 
188  hilti::rt::filesystem::path _makeTmp(std::string base, std::string ext);
189 
190  std::shared_ptr<Context> _context; // global context for options
191  bool _dump_code; // save all C++ code for debugging
192 
193  std::vector<hilti::rt::filesystem::path> _files; // all added source files
194  std::vector<CxxCode> _codes; // all C++ code units to be compiled
195  std::vector<hilti::rt::filesystem::path> _objects;
196 
197  std::optional<hilti::rt::TemporaryDirectory> _tmpdir;
198 
199  struct Job {
200  std::unique_ptr<reproc::process> process;
201  std::string stdout_;
202  std::string stderr_;
203 
204  void collectOutputs(int events);
205  };
206 
207  JobID _job_counter = 0;
208  std::map<JobID, Job> _jobs;
209 
210  std::map<std::string, unsigned int> _tmp_counters;
211 };
212 
213 } // namespace hilti
auto context() const
Definition: jit.h:163
const std::string & id() const
Definition: jit.h:85
CxxCode(const hilti::rt::filesystem::path &path)
Definition: jit.h:41
Definition: jit.h:19
void add(CxxCode d)
Definition: jit.h:139
CxxCode(const std::string &id, std::istream &code)
Definition: jit.h:49
Definition: unit.h:68
Definition: jit.h:118
Definition: library.h:62
bool hasInputs()
Definition: jit.h:153
auto code() const
Definition: jit.h:75
void add(const hilti::rt::filesystem::path &p)
Definition: jit.h:147
Definition: jit.h:34
auto options() const
Definition: jit.h:166
Definition: result.h:67
auto isLoaded() const
Definition: jit.h:78