File: compile.cpp

package info (click to toggle)
ldc 1%3A1.24.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 53,728 kB
  • sloc: cpp: 55,939; ansic: 10,599; sh: 958; makefile: 801; asm: 507; objc: 122; exp: 30; python: 12
file content (487 lines) | stat: -rw-r--r-- 15,705 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//===-- compile.cpp -------------------------------------------------------===//
//
//                         LDC – the LLVM D compiler
//
// This file is distributed under the Boost Software License. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//
//
// Jit runtime - shared library part.
// Defines jit runtime entry point and main compilation routines.
//
//===----------------------------------------------------------------------===//

#include <cassert>
#include <map>
#include <memory>
#include <sstream>
#include <stdexcept>
#include <type_traits>
#include <unordered_map>

#include "bind.h"
#include "callback_ostream.h"
#include "context.h"
#include "jit_context.h"
#include "optimizer.h"
#include "options.h"
#include "utils.h"

#include "llvm/Bitcode/BitcodeReader.h"
#include "llvm/ExecutionEngine/JITSymbol.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/Mangler.h"
#include "llvm/Linker/Linker.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils/Cloning.h"

namespace {

#pragma pack(push, 1)

struct RtCompileFuncList {
  const char *name;
  void **func;
  void *originalFunc;
};

struct RtCompileSymList {
  const char *name;
  void *sym;
};

struct RtCompileVarList {
  const char *name;
  const void *init;
};

struct RtCompileModuleList {
  int32_t version;
  RtCompileModuleList *next;
  const char *irData;
  int32_t irDataSize;
  const RtCompileFuncList *funcList;
  int32_t funcListSize;
  const RtCompileSymList *symList;
  int32_t symListSize;
  const RtCompileVarList *varList;
  int32_t varListSize;
};

#pragma pack(pop)

template <typename T>
auto toArray(T *ptr, size_t size)
    -> llvm::ArrayRef<typename std::remove_cv<T>::type> {
  return llvm::ArrayRef<typename std::remove_cv<T>::type>(ptr, size);
}

template <typename F>
void enumModules(const RtCompileModuleList *modlist_head,
                 const Context &context, F &&fun) {
  auto current = modlist_head;
  while (current != nullptr) {
    interruptPoint(context, "check version");
    if (current->version != ApiVersion) {
      fatal(context, "Module was built with different jit api version");
    }
    fun(*current);
    current = current->next;
  }
}

std::string decorate(llvm::StringRef name, const llvm::DataLayout &datalayout) {
  assert(!name.empty());
  llvm::SmallVector<char, 64> ret;
  llvm::Mangler::getNameWithPrefix(ret, name, datalayout);
  assert(!ret.empty());
  return std::string(ret.data(), ret.size());
}

struct JitModuleInfo final {
private:
  struct Func final {
    llvm::StringRef name;
    void **thunkVar;
    void *originalFunc;
  };
  std::vector<Func> funcs;
  mutable std::unordered_map<const void *, const Func *> funcsMap;

  struct BindHandle final {
    std::string name;
    void *handle = nullptr;
  };
  std::vector<BindHandle> bindHandles;

public:
  JitModuleInfo(const Context &context,
                const RtCompileModuleList *modlist_head) {
    enumModules(modlist_head, context, [&](const RtCompileModuleList &current) {
      for (auto &&fun : toArray(current.funcList, static_cast<std::size_t>(
                                                      current.funcListSize))) {
        funcs.push_back({fun.name, fun.func, fun.originalFunc});
      }
    });
  }

  const std::vector<Func> &functions() const { return funcs; }

  const std::unordered_map<const void *, const Func *> &functionsMap() const {
    if (funcsMap.empty() && !funcs.empty()) {
      for (auto &&fun : funcs) {
        funcsMap.insert({fun.originalFunc, &fun});
      }
    }
    return funcsMap;
  }

  const Func *getFunc(const void *ptr) const {
    assert(ptr != nullptr);
    auto &funcMap = functionsMap();
    auto it = funcMap.find(ptr);
    if (funcMap.end() != it) {
      return it->second;
    }
    return nullptr;
  }

  const std::vector<BindHandle> &getBindHandles() const { return bindHandles; }

  void addBindHandle(llvm::StringRef name, void *handle) {
    assert(!name.empty());
    assert(handle != nullptr);
    BindHandle h;
    h.name = name.str();
    h.handle = handle;
    bindHandles.emplace_back(std::move(h));
  }
};

void *resolveSymbol(llvm::JITSymbol &symbol) {
  auto addr = symbol.getAddress();
  if (!addr) {
    consumeError(addr.takeError());
    return nullptr;
  } else {
    return reinterpret_cast<void *>(addr.get());
  }
}

void generateBind(const Context &context, DynamicCompilerContext &jitContext,
                  JitModuleInfo &moduleInfo, llvm::Module &module) {
  auto getIrFunc = [&](const void *ptr) -> llvm::Function * {
    assert(ptr != nullptr);
    auto funcDesc = moduleInfo.getFunc(ptr);
    if (funcDesc == nullptr) {
      return nullptr;
    }
    return module.getFunction(funcDesc->name);
  };

  std::unordered_map<const void *, llvm::Function *> bindFuncs;
  bindFuncs.reserve(jitContext.getBindInstances().size() * 2);

  auto genBind = [&](void *bindPtr, void *originalFunc, void *exampleFunc,
                     const llvm::ArrayRef<ParamSlice> &params) {
    assert(bindPtr != nullptr);
    assert(bindFuncs.end() == bindFuncs.find(bindPtr));
    auto funcToInline = getIrFunc(originalFunc);
    if (funcToInline != nullptr) {
      auto exampleIrFunc = getIrFunc(exampleFunc);
      assert(exampleIrFunc != nullptr);
      auto errhandler = [&](const std::string &str) { fatal(context, str); };
      auto overrideHandler = [&](llvm::Type &type, const void *data,
                                 size_t size) -> llvm::Constant * {
        if (type.isPointerTy()) {
          auto getBindFunc = [&]() {
            auto handle = *static_cast<void *const *>(data);
            return handle != nullptr && jitContext.hasBindFunction(handle)
                       ? handle
                       : nullptr;
          };

          auto ptype = llvm::cast<llvm::PointerType>(&type);
          auto elemType = ptype->getElementType();
          if (elemType->isFunctionTy()) {
            (void)size;
            assert(size == sizeof(void *));
            auto val = *reinterpret_cast<void *const *>(data);
            if (val != nullptr) {
              auto ret = getIrFunc(val);
              if (ret != nullptr && ret->getType() != &type) {
                return llvm::ConstantExpr::getBitCast(ret, &type);
              }
              return ret;
            }
          } else if (auto handle = getBindFunc()) {
            auto it = bindFuncs.find(handle);
            assert(bindFuncs.end() != it);
            auto bindIrFunc = it->second;
            auto funcPtrType = bindIrFunc->getType();
            auto globalVar1 = new llvm::GlobalVariable(
                module, funcPtrType, true, llvm::GlobalValue::PrivateLinkage,
                bindIrFunc, ".jit_bind_handle");
            return llvm::ConstantExpr::getBitCast(globalVar1, &type);
          }
        }
        return nullptr;
      };
      auto func =
          bindParamsToFunc(module, *funcToInline, *exampleIrFunc, params,
                           errhandler, BindOverride(overrideHandler));
      moduleInfo.addBindHandle(func->getName(), bindPtr);
      bindFuncs.insert({bindPtr, func});
    } else {
      fatal(context, "Bind: function body not available");
    }
  };
  for (auto &&bind : jitContext.getBindInstances()) {
    auto bindPtr = bind.first;
    auto &bindDesc = bind.second;
    assert(bindDesc.originalFunc != nullptr);
    genBind(bindPtr, bindDesc.originalFunc, bindDesc.exampleFunc,
            bindDesc.params);
  }
}

void applyBind(const Context &context, DynamicCompilerContext &jitContext,
               const JitModuleInfo &moduleInfo) {
  auto &layout = jitContext.getDataLayout();
  for (auto &elem : moduleInfo.getBindHandles()) {
    auto decorated = decorate(elem.name, layout);
    auto symbol = jitContext.findSymbol(decorated);
    auto addr = resolveSymbol(symbol);
    if (nullptr == addr) {
      std::string desc = std::string("Symbol not found in jitted code: \"") +
                         elem.name + "\" (\"" + decorated + "\")";
      fatal(context, desc);
    } else {
      auto handle = static_cast<void **>(elem.handle);
      *handle = addr;
    }
  }
}

DynamicCompilerContext &getJit(DynamicCompilerContext *context) {
  if (context != nullptr) {
    return *context;
  }
  static DynamicCompilerContext jit(/*mainContext*/ true);
  return jit;
}

void setRtCompileVars(const Context &context, llvm::Module &module,
                      llvm::ArrayRef<RtCompileVarList> vals) {
  for (auto &&val : vals) {
    setRtCompileVar(context, module, val.name, val.init);
  }
}

void dumpModule(const Context &context, const llvm::Module &module,
                DumpStage stage) {
  if (nullptr != context.dumpHandler) {
    auto callback = [&](const char *str, size_t len) {
      context.dumpHandler(context.dumpHandlerData, stage, str, len);
    };

    CallbackOstream os(callback);
    module.print(os, nullptr, false, true);
  }
}

void setFunctionsTarget(llvm::Module &module, const llvm::TargetMachine &TM) {
  // Set function target cpu to host if it wasn't set explicitly
  for (auto &&func : module.functions()) {
    if (!func.hasFnAttribute("target-cpu")) {
      func.addFnAttr("target-cpu", TM.getTargetCPU());
    }

    if (!func.hasFnAttribute("target-features")) {
      auto featStr = TM.getTargetFeatureString();
      if (!featStr.empty()) {
        func.addFnAttr("target-features", featStr);
      }
    }
  }
}

struct JitFinaliser final {
  DynamicCompilerContext &jit;
  bool finalized = false;
  explicit JitFinaliser(DynamicCompilerContext &j) : jit(j) {}
  ~JitFinaliser() {
    if (!finalized) {
      jit.reset();
    }
  }

  void finalze() { finalized = true; }
};

void rtCompileProcessImplSoInternal(const RtCompileModuleList *modlist_head,
                                    const Context &context) {
  if (nullptr == modlist_head) {
    // No jit modules to compile
    return;
  }
  interruptPoint(context, "Init");
  DynamicCompilerContext &myJit = getJit(context.compilerContext);

  JitModuleInfo moduleInfo(context, modlist_head);
  std::unique_ptr<llvm::Module> finalModule;
  myJit.clearSymMap();
  auto &layout = myJit.getDataLayout();
  OptimizerSettings settings;
  settings.optLevel = context.optLevel;
  settings.sizeLevel = context.sizeLevel;
  enumModules(modlist_head, context, [&](const RtCompileModuleList &current) {
    interruptPoint(context, "load IR");
    auto buff = llvm::MemoryBuffer::getMemBuffer(
        llvm::StringRef(current.irData,
                        static_cast<std::size_t>(current.irDataSize)),
        "", false);
    interruptPoint(context, "parse IR");
    auto mod = llvm::parseBitcodeFile(*buff, myJit.getContext());
    if (!mod) {
      fatal(context, "Unable to parse IR: " + llvm::toString(mod.takeError()));
    } else {
      llvm::Module &module = **mod;
      const auto name = module.getName();
      interruptPoint(context, "Verify module", name.data());
      verifyModule(context, module);

      dumpModule(context, module, DumpStage::OriginalModule);
      setFunctionsTarget(module, myJit.getTargetMachine());

      module.setDataLayout(myJit.getTargetMachine().createDataLayout());

      interruptPoint(context, "setRtCompileVars", name.data());
      setRtCompileVars(context, module,
                       toArray(current.varList,
                               static_cast<std::size_t>(current.varListSize)));

      if (nullptr == finalModule) {
        finalModule = std::move(*mod);
      } else {
        if (llvm::Linker::linkModules(*finalModule, std::move(*mod))) {
          fatal(context, "Can't merge module");
        }
      }

      for (auto &&sym : toArray(current.symList, static_cast<std::size_t>(
                                                     current.symListSize))) {
        myJit.addSymbol(decorate(sym.name, layout), sym.sym);
      }
    }
  });

  assert(nullptr != finalModule);

  interruptPoint(context, "Generate bind functions");
  generateBind(context, myJit, moduleInfo, *finalModule);
  dumpModule(context, *finalModule, DumpStage::MergedModule);
  interruptPoint(context, "Optimize final module");
  optimizeModule(context, myJit.getTargetMachine(), settings, *finalModule);

  interruptPoint(context, "Verify final module");
  verifyModule(context, *finalModule);

  dumpModule(context, *finalModule, DumpStage::OptimizedModule);

  interruptPoint(context, "Codegen final module");
  if (nullptr != context.dumpHandler) {
    auto callback = [&](const char *str, size_t len) {
      context.dumpHandler(context.dumpHandlerData, DumpStage::FinalAsm, str,
                          len);
    };

    CallbackOstream os(callback);
    if (auto err = myJit.addModule(std::move(finalModule), &os)) {
      fatal(context, "Can't codegen module: " + llvm::toString(std::move(err)));
    }
  } else {
    if (auto err = myJit.addModule(std::move(finalModule), nullptr)) {
      fatal(context, "Can't codegen module: " + llvm::toString(std::move(err)));
    }
  }

  JitFinaliser jitFinalizer(myJit);
  if (myJit.isMainContext()) {
    interruptPoint(context, "Resolve functions");
    for (auto &&fun : moduleInfo.functions()) {
      if (fun.thunkVar == nullptr) {
        continue;
      }
      auto decorated = decorate(fun.name, layout);
      auto symbol = myJit.findSymbol(decorated);
      auto addr = resolveSymbol(symbol);
      if (nullptr == addr) {
        std::string desc = std::string("Symbol not found in jitted code: \"") +
                           fun.name.data() + "\" (\"" + decorated + "\")";
        fatal(context, desc);
      } else {
        *fun.thunkVar = addr;
      }

      if (nullptr != context.interruptPointHandler) {
        std::stringstream ss;
        ss << fun.name.data() << " to " << addr;
        auto str = ss.str();
        interruptPoint(context, "Resolved", str.c_str());
      }
    }
  }
  interruptPoint(context, "Update bind handles");
  applyBind(context, myJit, moduleInfo);
  jitFinalizer.finalze();
}

} // anon namespace

extern "C" {
EXTERNAL void JIT_API_ENTRYPOINT(const void *modlist_head,
                                 const Context *context, size_t contextSize) {
  assert(nullptr != context);
  assert(sizeof(*context) == contextSize);
  rtCompileProcessImplSoInternal(
      static_cast<const RtCompileModuleList *>(modlist_head), *context);
}

EXTERNAL void JIT_REG_BIND_PAYLOAD(class DynamicCompilerContext *context,
                                   void *handle, void *originalFunc,
                                   void *exampleFunc, const ParamSlice *params,
                                   size_t paramsSize) {
  assert(handle != nullptr);
  assert(originalFunc != nullptr);
  assert(exampleFunc != nullptr);
  DynamicCompilerContext &myJit = getJit(context);
  myJit.registerBind(handle, originalFunc, exampleFunc,
                     toArray(params, paramsSize));
}

EXTERNAL void JIT_UNREG_BIND_PAYLOAD(class DynamicCompilerContext *context,
                                     void *handle) {
  assert(handle != nullptr);
  DynamicCompilerContext &myJit = getJit(context);
  myJit.unregisterBind(handle);
}

EXTERNAL DynamicCompilerContext *JIT_CREATE_COMPILER_CONTEXT() {
  return new DynamicCompilerContext(false);
}

EXTERNAL void JIT_DESTROY_COMPILER_CONTEXT(DynamicCompilerContext *context) {
  assert(context != nullptr);
  delete context;
}

EXTERNAL bool JIT_SET_OPTS(const Slice<Slice<const char>> *args,
                           void (*errs)(void *, const char *, size_t),
                           void *errsContext) {
  assert(args != nullptr);
  return parseOptions(*args, errs, errsContext);
}
}