Coverage Report

Created: 2025-07-01 06:18

/src/WasmEdge/lib/llvm/jit.cpp
Line
Count
Source (jump to first uncovered line)
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: 2019-2024 Second State INC
3
4
#include "llvm/jit.h"
5
#include "common/spdlog.h"
6
7
#include "data.h"
8
#include "llvm.h"
9
10
namespace LLVM = WasmEdge::LLVM;
11
using namespace std::literals;
12
13
namespace WasmEdge::LLVM {
14
15
JITLibrary::JITLibrary(OrcLLJIT JIT) noexcept
16
0
    : J(std::make_unique<OrcLLJIT>(std::move(JIT)).release()) {}
17
18
0
JITLibrary::~JITLibrary() noexcept {
19
0
  std::unique_ptr<OrcLLJIT> JIT(std::exchange(J, nullptr));
20
0
}
21
22
Symbol<const Executable::IntrinsicsTable *>
23
0
JITLibrary::getIntrinsics() noexcept {
24
0
  if (auto Symbol = J->lookup<const IntrinsicsTable *>("intrinsics")) {
25
0
    return createSymbol<const IntrinsicsTable *>(*Symbol);
26
0
  } else {
27
0
    spdlog::error("{}"sv, Symbol.error().message().string_view());
28
0
    return {};
29
0
  }
30
0
}
31
32
std::vector<Symbol<Executable::Wrapper>>
33
0
JITLibrary::getTypes(size_t Size) noexcept {
34
0
  std::vector<Symbol<Wrapper>> Result;
35
0
  Result.reserve(Size);
36
0
  for (size_t I = 0; I < Size; ++I) {
37
0
    const std::string Name = fmt::format("t{}"sv, I);
38
0
    if (auto Symbol = J->lookup<Wrapper>(Name.c_str())) {
39
0
      Result.push_back(createSymbol<Wrapper>(*Symbol));
40
0
    } else {
41
0
      spdlog::error("{}"sv, Symbol.error().message().string_view());
42
0
      Result.emplace_back();
43
0
    }
44
0
  }
45
46
0
  return Result;
47
0
}
48
49
std::vector<Symbol<void>> JITLibrary::getCodes(size_t Offset,
50
0
                                               size_t Size) noexcept {
51
0
  std::vector<Symbol<void>> Result;
52
0
  Result.reserve(Size);
53
0
  for (size_t I = 0; I < Size; ++I) {
54
0
    const std::string Name = fmt::format("f{}"sv, I + Offset);
55
0
    if (auto Symbol = J->lookup<void>(Name.c_str())) {
56
0
      Result.push_back(createSymbol<void>(*Symbol));
57
0
    } else {
58
0
      spdlog::error("{}"sv, Symbol.error().message().string_view());
59
0
      Result.emplace_back();
60
0
    }
61
0
  }
62
63
0
  return Result;
64
0
}
65
66
0
Expect<std::shared_ptr<Executable>> JIT::load(Data D) noexcept {
67
0
  OrcLLJIT J;
68
0
  if (auto Res = OrcLLJIT::create(); !Res) {
69
0
    spdlog::error("{}"sv, Res.error().message().string_view());
70
0
    return Unexpect(ErrCode::Value::HostFuncError);
71
0
  } else {
72
0
    J = std::move(*Res);
73
0
  }
74
75
0
  auto &LLModule = D.extract().LLModule;
76
77
0
  if (Conf.getCompilerConfigure().isDumpIR()) {
78
0
    if (auto ErrorMessage = LLModule.printModuleToFile("wasm-jit.ll")) {
79
0
      spdlog::error("printModuleToFile failed"sv);
80
0
    }
81
0
  }
82
83
0
  auto MainJD = J.getMainJITDylib();
84
0
  if (auto Err = J.addLLVMIRModule(
85
0
          MainJD,
86
0
          OrcThreadSafeModule(LLModule.release(), D.extract().TSContext))) {
87
0
    spdlog::error("{}"sv, Err.message().string_view());
88
0
    return Unexpect(ErrCode::Value::HostFuncError);
89
0
  }
90
91
0
  return std::make_shared<JITLibrary>(std::move(J));
92
0
}
93
} // namespace WasmEdge::LLVM