Coverage Report

Created: 2026-07-16 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/WasmEdge/lib/llvm/compiler/function_compiler.h
Line
Count
Source
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: Copyright The WasmEdge Authors
3
#pragma once
4
5
#include "compiler/context.h"
6
7
#include <cstdint>
8
#include <tuple>
9
#include <unordered_map>
10
#include <utility>
11
#include <vector>
12
13
namespace WasmEdge {
14
15
class FunctionCompiler {
16
  struct Control;
17
18
public:
19
  FunctionCompiler(LLVM::Compiler::CompileContext &Context,
20
                   LLVM::FunctionCallee F, Span<const ValType> Locals,
21
                   bool Interruptible, bool InstructionCounting,
22
                   bool GasMeasuring, bool IsLazyJIT) noexcept;
23
24
  LLVM::BasicBlock getTrapBB(ErrCode::Value Error) noexcept;
25
26
  Expect<void>
27
  compile(const AST::CodeSegment &Code,
28
          std::pair<std::vector<ValType>, std::vector<ValType>> Type) noexcept;
29
30
  Expect<void> compile(AST::InstrView Instrs) noexcept;
31
  Expect<void> compileAtomicOp(const AST::Instruction &Instr) noexcept;
32
  Expect<void> compileVectorOp(const AST::Instruction &Instr) noexcept;
33
  Expect<void> compileMemoryOp(const AST::Instruction &Instr) noexcept;
34
  Expect<void> compileNumericOp(const AST::Instruction &Instr) noexcept;
35
  Expect<void> compileRefOp(const AST::Instruction &Instr) noexcept;
36
  void compileSignedTrunc(LLVM::Type IntType) noexcept;
37
  void compileSignedTruncSat(LLVM::Type IntType) noexcept;
38
  void compileUnsignedTrunc(LLVM::Type IntType) noexcept;
39
  void compileUnsignedTruncSat(LLVM::Type IntType) noexcept;
40
41
  void compileAtomicCheckOffsetAlignment(LLVM::Value Offset,
42
                                         LLVM::Type IntType) noexcept;
43
44
  void compileMemoryFence() noexcept;
45
  void compileAtomicNotify(unsigned MemoryIndex,
46
                           uint64_t MemoryOffset) noexcept;
47
  void compileAtomicWait(unsigned MemoryIndex, uint64_t MemoryOffset,
48
                         LLVM::Type TargetType, uint32_t BitWidth) noexcept;
49
  void compileAtomicLoad(unsigned MemoryIndex, uint64_t MemoryOffset,
50
                         unsigned Alignment, LLVM::Type IntType,
51
                         LLVM::Type TargetType, bool Signed = false) noexcept;
52
  void compileAtomicStore(unsigned MemoryIndex, uint64_t MemoryOffset,
53
                          unsigned Alignment, LLVM::Type, LLVM::Type TargetType,
54
                          bool Signed = false) noexcept;
55
56
  void compileAtomicRMWOp(unsigned MemoryIndex, uint64_t MemoryOffset,
57
                          [[maybe_unused]] unsigned Alignment,
58
                          LLVMAtomicRMWBinOp BinOp, LLVM::Type IntType,
59
                          LLVM::Type TargetType, bool Signed = false) noexcept;
60
  void compileAtomicCompareExchange(unsigned MemoryIndex, uint64_t MemoryOffset,
61
                                    [[maybe_unused]] unsigned Alignment,
62
                                    LLVM::Type IntType, LLVM::Type TargetType,
63
                                    bool Signed = false) noexcept;
64
65
  void compileReturn() noexcept;
66
67
  void updateInstrCount() noexcept;
68
69
  void updateGas() noexcept;
70
71
  void updateGasAtTrap() noexcept;
72
73
private:
74
  void compileCallOp(const unsigned int FuncIndex) noexcept;
75
76
  void compileIndirectCallOp(const uint32_t TableIndex,
77
                             const uint32_t FuncTypeIndex) noexcept;
78
79
  void compileReturnCallOp(const unsigned int FuncIndex) noexcept;
80
81
  void compileReturnIndirectCallOp(const uint32_t TableIndex,
82
                                   const uint32_t FuncTypeIndex) noexcept;
83
84
  void compileCallRefOp(const unsigned int TypeIndex) noexcept;
85
86
  void compileReturnCallRefOp(const unsigned int TypeIndex) noexcept;
87
88
  void compileLoadOp(unsigned MemoryIndex, uint64_t Offset, unsigned Alignment,
89
                     LLVM::Type LoadTy) noexcept;
90
  void compileLoadOp(unsigned MemoryIndex, uint64_t Offset, unsigned Alignment,
91
                     LLVM::Type LoadTy, LLVM::Type ExtendTy,
92
                     bool Signed) noexcept;
93
  void compileVectorLoadOp(unsigned MemoryIndex, uint64_t Offset,
94
                           unsigned Alignment, LLVM::Type LoadTy) noexcept;
95
  void compileVectorLoadOp(unsigned MemoryIndex, uint64_t Offset,
96
                           unsigned Alignment, LLVM::Type LoadTy,
97
                           LLVM::Type ExtendTy, bool Signed) noexcept;
98
  void compileSplatLoadOp(unsigned MemoryIndex, uint64_t Offset,
99
                          unsigned Alignment, LLVM::Type LoadTy,
100
                          LLVM::Type VectorTy) noexcept;
101
  void compileLoadLaneOp(unsigned MemoryIndex, uint64_t Offset,
102
                         unsigned Alignment, unsigned Index, LLVM::Type LoadTy,
103
                         LLVM::Type VectorTy) noexcept;
104
  void compileStoreOp(uint32_t MemoryIndex, uint64_t Offset, uint32_t Alignment,
105
                      LLVM::Type LoadTy, bool Trunc = false,
106
                      bool BitCast = false) noexcept;
107
  void compileStoreLaneOp(uint32_t MemoryIndex, uint64_t Offset,
108
                          uint32_t Alignment, uint8_t Index, LLVM::Type LoadTy,
109
                          LLVM::Type VectorTy) noexcept;
110
  void compileSplatOp(LLVM::Type VectorTy) noexcept;
111
  void compileExtractLaneOp(LLVM::Type VectorTy, unsigned Index) noexcept;
112
  void compileExtractLaneOp(LLVM::Type VectorTy, unsigned Index,
113
                            LLVM::Type ExtendTy, bool Signed) noexcept;
114
  void compileReplaceLaneOp(LLVM::Type VectorTy, unsigned Index) noexcept;
115
  void compileVectorCompareOp(LLVM::Type VectorTy,
116
                              LLVMIntPredicate Predicate) noexcept;
117
  void compileVectorCompareOp(LLVM::Type VectorTy, LLVMRealPredicate Predicate,
118
                              LLVM::Type ResultTy) noexcept;
119
  template <typename Func>
120
28.3k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
121
28.3k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
122
28.3k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
123
28.3k
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorOp<WasmEdge::FunctionCompiler::compileVectorAbs(WasmEdge::LLVM::Type)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorAbs(WasmEdge::LLVM::Type)::$_0&&)
Line
Count
Source
120
2.48k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
121
2.48k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
122
2.48k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
123
2.48k
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorOp<WasmEdge::FunctionCompiler::compileVectorConvertS(WasmEdge::LLVM::Type, WasmEdge::LLVM::Type, bool)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorConvertS(WasmEdge::LLVM::Type, WasmEdge::LLVM::Type, bool)::$_0&&)
Line
Count
Source
120
649
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
121
649
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
122
649
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
123
649
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorOp<WasmEdge::FunctionCompiler::compileVectorConvertU(WasmEdge::LLVM::Type, WasmEdge::LLVM::Type, bool)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorConvertU(WasmEdge::LLVM::Type, WasmEdge::LLVM::Type, bool)::$_0&&)
Line
Count
Source
120
1.90k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
121
1.90k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
122
1.90k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
123
1.90k
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorOp<WasmEdge::FunctionCompiler::compileVectorDemote()::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorDemote()::$_0&&)
Line
Count
Source
120
737
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
121
737
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
122
737
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
123
737
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorOp<WasmEdge::FunctionCompiler::compileVectorExtAddPairwise(WasmEdge::LLVM::Type, bool)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorExtAddPairwise(WasmEdge::LLVM::Type, bool)::$_0&&)
Line
Count
Source
120
2.85k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
121
2.85k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
122
2.85k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
123
2.85k
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorOp<WasmEdge::FunctionCompiler::compileVectorFAbs(WasmEdge::LLVM::Type)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorFAbs(WasmEdge::LLVM::Type)::$_0&&)
Line
Count
Source
120
556
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
121
556
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
122
556
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
123
556
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorOp<WasmEdge::FunctionCompiler::compileVectorFCeil(WasmEdge::LLVM::Type)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorFCeil(WasmEdge::LLVM::Type)::$_0&&)
Line
Count
Source
120
1.71k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
121
1.71k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
122
1.71k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
123
1.71k
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorOp<WasmEdge::FunctionCompiler::compileVectorFFloor(WasmEdge::LLVM::Type)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorFFloor(WasmEdge::LLVM::Type)::$_0&&)
Line
Count
Source
120
2.82k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
121
2.82k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
122
2.82k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
123
2.82k
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorOp<WasmEdge::FunctionCompiler::compileVectorFNearest(WasmEdge::LLVM::Type)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorFNearest(WasmEdge::LLVM::Type)::$_0&&)
Line
Count
Source
120
434
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
121
434
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
122
434
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
123
434
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorOp<WasmEdge::FunctionCompiler::compileVectorFNeg(WasmEdge::LLVM::Type)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorFNeg(WasmEdge::LLVM::Type)::$_0&&)
Line
Count
Source
120
782
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
121
782
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
122
782
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
123
782
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorOp<WasmEdge::FunctionCompiler::compileVectorFSqrt(WasmEdge::LLVM::Type)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorFSqrt(WasmEdge::LLVM::Type)::$_0&&)
Line
Count
Source
120
279
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
121
279
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
122
279
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
123
279
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorOp<WasmEdge::FunctionCompiler::compileVectorFTrunc(WasmEdge::LLVM::Type)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorFTrunc(WasmEdge::LLVM::Type)::$_0&&)
Line
Count
Source
120
2.04k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
121
2.04k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
122
2.04k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
123
2.04k
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorOp<WasmEdge::FunctionCompiler::compileVectorNeg(WasmEdge::LLVM::Type)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorNeg(WasmEdge::LLVM::Type)::$_0&&)
Line
Count
Source
120
3.19k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
121
3.19k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
122
3.19k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
123
3.19k
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorOp<WasmEdge::FunctionCompiler::compileVectorPopcnt()::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorPopcnt()::$_0&&)
Line
Count
Source
120
140
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
121
140
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
122
140
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
123
140
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorOp<WasmEdge::FunctionCompiler::compileVectorPromote()::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorPromote()::$_0&&)
Line
Count
Source
120
789
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
121
789
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
122
789
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
123
789
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorOp<WasmEdge::FunctionCompiler::compileVectorTruncSatS32(WasmEdge::LLVM::Type, bool)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorTruncSatS32(WasmEdge::LLVM::Type, bool)::$_0&&)
Line
Count
Source
120
955
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
121
955
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
122
955
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
123
955
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorOp<WasmEdge::FunctionCompiler::compileVectorTruncSatU32(WasmEdge::LLVM::Type, bool)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorTruncSatU32(WasmEdge::LLVM::Type, bool)::$_0&&)
Line
Count
Source
120
5.97k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
121
5.97k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
122
5.97k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
123
5.97k
  }
124
  void compileVectorAbs(LLVM::Type VectorTy) noexcept;
125
  void compileVectorNeg(LLVM::Type VectorTy) noexcept;
126
  void compileVectorPopcnt() noexcept;
127
  template <typename Func>
128
2.16k
  void compileVectorReduceIOp(LLVM::Type VectorTy, Func &&Op) noexcept {
129
2.16k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
130
2.16k
    Stack.back() = Builder.createZExt(Op(V), Context.Int32Ty);
131
2.16k
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorReduceIOp<WasmEdge::FunctionCompiler::compileVectorAllTrue(WasmEdge::LLVM::Type)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorAllTrue(WasmEdge::LLVM::Type)::$_0&&)
Line
Count
Source
128
901
  void compileVectorReduceIOp(LLVM::Type VectorTy, Func &&Op) noexcept {
129
901
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
130
901
    Stack.back() = Builder.createZExt(Op(V), Context.Int32Ty);
131
901
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorReduceIOp<WasmEdge::FunctionCompiler::compileVectorAnyTrue()::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorAnyTrue()::$_0&&)
Line
Count
Source
128
131
  void compileVectorReduceIOp(LLVM::Type VectorTy, Func &&Op) noexcept {
129
131
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
130
131
    Stack.back() = Builder.createZExt(Op(V), Context.Int32Ty);
131
131
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorReduceIOp<WasmEdge::FunctionCompiler::compileVectorBitMask(WasmEdge::LLVM::Type)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorBitMask(WasmEdge::LLVM::Type)::$_0&&)
Line
Count
Source
128
1.13k
  void compileVectorReduceIOp(LLVM::Type VectorTy, Func &&Op) noexcept {
129
1.13k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
130
1.13k
    Stack.back() = Builder.createZExt(Op(V), Context.Int32Ty);
131
1.13k
  }
132
  void compileVectorAnyTrue() noexcept;
133
  void compileVectorAllTrue(LLVM::Type VectorTy) noexcept;
134
  void compileVectorBitMask(LLVM::Type VectorTy) noexcept;
135
  template <typename Func>
136
4.13k
  void compileVectorShiftOp(LLVM::Type VectorTy, Func &&Op) noexcept {
137
4.13k
    const bool Trunc = VectorTy.getElementType().getIntegerBitWidth() < 32;
138
4.13k
    const uint32_t Mask = VectorTy.getElementType().getIntegerBitWidth() - 1;
139
4.13k
    auto N = Builder.createAnd(stackPop(), LLContext.getInt32(Mask));
140
4.13k
    auto RHS = Builder.createVectorSplat(
141
4.13k
        VectorTy.getVectorSize(),
142
4.13k
        Trunc ? Builder.createTrunc(N, VectorTy.getElementType())
143
4.13k
              : Builder.createZExtOrTrunc(N, VectorTy.getElementType()));
144
4.13k
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
145
4.13k
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
146
4.13k
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorShiftOp<WasmEdge::FunctionCompiler::compileVectorAShr(WasmEdge::LLVM::Type)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorAShr(WasmEdge::LLVM::Type)::$_0&&)
Line
Count
Source
136
1.99k
  void compileVectorShiftOp(LLVM::Type VectorTy, Func &&Op) noexcept {
137
1.99k
    const bool Trunc = VectorTy.getElementType().getIntegerBitWidth() < 32;
138
1.99k
    const uint32_t Mask = VectorTy.getElementType().getIntegerBitWidth() - 1;
139
1.99k
    auto N = Builder.createAnd(stackPop(), LLContext.getInt32(Mask));
140
1.99k
    auto RHS = Builder.createVectorSplat(
141
1.99k
        VectorTy.getVectorSize(),
142
1.99k
        Trunc ? Builder.createTrunc(N, VectorTy.getElementType())
143
1.99k
              : Builder.createZExtOrTrunc(N, VectorTy.getElementType()));
144
1.99k
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
145
1.99k
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
146
1.99k
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorShiftOp<WasmEdge::FunctionCompiler::compileVectorLShr(WasmEdge::LLVM::Type)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorLShr(WasmEdge::LLVM::Type)::$_0&&)
Line
Count
Source
136
720
  void compileVectorShiftOp(LLVM::Type VectorTy, Func &&Op) noexcept {
137
720
    const bool Trunc = VectorTy.getElementType().getIntegerBitWidth() < 32;
138
720
    const uint32_t Mask = VectorTy.getElementType().getIntegerBitWidth() - 1;
139
720
    auto N = Builder.createAnd(stackPop(), LLContext.getInt32(Mask));
140
720
    auto RHS = Builder.createVectorSplat(
141
720
        VectorTy.getVectorSize(),
142
720
        Trunc ? Builder.createTrunc(N, VectorTy.getElementType())
143
720
              : Builder.createZExtOrTrunc(N, VectorTy.getElementType()));
144
720
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
145
720
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
146
720
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorShiftOp<WasmEdge::FunctionCompiler::compileVectorShl(WasmEdge::LLVM::Type)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorShl(WasmEdge::LLVM::Type)::$_0&&)
Line
Count
Source
136
1.42k
  void compileVectorShiftOp(LLVM::Type VectorTy, Func &&Op) noexcept {
137
1.42k
    const bool Trunc = VectorTy.getElementType().getIntegerBitWidth() < 32;
138
1.42k
    const uint32_t Mask = VectorTy.getElementType().getIntegerBitWidth() - 1;
139
1.42k
    auto N = Builder.createAnd(stackPop(), LLContext.getInt32(Mask));
140
1.42k
    auto RHS = Builder.createVectorSplat(
141
1.42k
        VectorTy.getVectorSize(),
142
1.42k
        Trunc ? Builder.createTrunc(N, VectorTy.getElementType())
143
1.42k
              : Builder.createZExtOrTrunc(N, VectorTy.getElementType()));
144
1.42k
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
145
1.42k
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
146
1.42k
  }
147
  void compileVectorShl(LLVM::Type VectorTy) noexcept;
148
  void compileVectorLShr(LLVM::Type VectorTy) noexcept;
149
  void compileVectorAShr(LLVM::Type VectorTy) noexcept;
150
  template <typename Func>
151
7.39k
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
152
7.39k
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
153
7.39k
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
154
7.39k
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
155
7.39k
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorVectorOp<WasmEdge::FunctionCompiler::compileVectorVectorAdd(WasmEdge::LLVM::Type)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorVectorAdd(WasmEdge::LLVM::Type)::$_0&&)
Line
Count
Source
151
406
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
152
406
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
153
406
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
154
406
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
155
406
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorVectorOp<WasmEdge::FunctionCompiler::compileVectorVectorAddSat(WasmEdge::LLVM::Type, bool)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorVectorAddSat(WasmEdge::LLVM::Type, bool)::$_0&&)
Line
Count
Source
151
991
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
152
991
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
153
991
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
154
991
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
155
991
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorVectorOp<WasmEdge::FunctionCompiler::compileVectorVectorFAdd(WasmEdge::LLVM::Type)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorVectorFAdd(WasmEdge::LLVM::Type)::$_0&&)
Line
Count
Source
151
171
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
152
171
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
153
171
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
154
171
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
155
171
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorVectorOp<WasmEdge::FunctionCompiler::compileVectorVectorFDiv(WasmEdge::LLVM::Type)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorVectorFDiv(WasmEdge::LLVM::Type)::$_0&&)
Line
Count
Source
151
198
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
152
198
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
153
198
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
154
198
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
155
198
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorVectorOp<WasmEdge::FunctionCompiler::compileVectorVectorFMax(WasmEdge::LLVM::Type)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorVectorFMax(WasmEdge::LLVM::Type)::$_0&&)
Line
Count
Source
151
209
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
152
209
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
153
209
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
154
209
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
155
209
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorVectorOp<WasmEdge::FunctionCompiler::compileVectorVectorFMin(WasmEdge::LLVM::Type)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorVectorFMin(WasmEdge::LLVM::Type)::$_0&&)
Line
Count
Source
151
344
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
152
344
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
153
344
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
154
344
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
155
344
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorVectorOp<WasmEdge::FunctionCompiler::compileVectorVectorFMul(WasmEdge::LLVM::Type)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorVectorFMul(WasmEdge::LLVM::Type)::$_0&&)
Line
Count
Source
151
269
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
152
269
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
153
269
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
154
269
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
155
269
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorVectorOp<WasmEdge::FunctionCompiler::compileVectorVectorFPMax(WasmEdge::LLVM::Type)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorVectorFPMax(WasmEdge::LLVM::Type)::$_0&&)
Line
Count
Source
151
325
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
152
325
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
153
325
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
154
325
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
155
325
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorVectorOp<WasmEdge::FunctionCompiler::compileVectorVectorFPMin(WasmEdge::LLVM::Type)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorVectorFPMin(WasmEdge::LLVM::Type)::$_0&&)
Line
Count
Source
151
332
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
152
332
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
153
332
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
154
332
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
155
332
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorVectorOp<WasmEdge::FunctionCompiler::compileVectorVectorFSub(WasmEdge::LLVM::Type)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorVectorFSub(WasmEdge::LLVM::Type)::$_0&&)
Line
Count
Source
151
467
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
152
467
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
153
467
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
154
467
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
155
467
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorVectorOp<WasmEdge::FunctionCompiler::compileVectorVectorMul(WasmEdge::LLVM::Type)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorVectorMul(WasmEdge::LLVM::Type)::$_0&&)
Line
Count
Source
151
362
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
152
362
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
153
362
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
154
362
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
155
362
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorVectorOp<WasmEdge::FunctionCompiler::compileVectorVectorQ15MulSat()::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorVectorQ15MulSat()::$_0&&)
Line
Count
Source
151
162
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
152
162
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
153
162
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
154
162
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
155
162
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorVectorOp<WasmEdge::FunctionCompiler::compileVectorVectorSMax(WasmEdge::LLVM::Type)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorVectorSMax(WasmEdge::LLVM::Type)::$_0&&)
Line
Count
Source
151
497
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
152
497
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
153
497
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
154
497
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
155
497
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorVectorOp<WasmEdge::FunctionCompiler::compileVectorVectorSMin(WasmEdge::LLVM::Type)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorVectorSMin(WasmEdge::LLVM::Type)::$_0&&)
Line
Count
Source
151
321
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
152
321
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
153
321
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
154
321
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
155
321
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorVectorOp<WasmEdge::FunctionCompiler::compileVectorVectorSub(WasmEdge::LLVM::Type)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorVectorSub(WasmEdge::LLVM::Type)::$_0&&)
Line
Count
Source
151
705
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
152
705
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
153
705
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
154
705
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
155
705
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorVectorOp<WasmEdge::FunctionCompiler::compileVectorVectorSubSat(WasmEdge::LLVM::Type, bool)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorVectorSubSat(WasmEdge::LLVM::Type, bool)::$_0&&)
Line
Count
Source
151
392
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
152
392
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
153
392
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
154
392
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
155
392
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorVectorOp<WasmEdge::FunctionCompiler::compileVectorVectorUAvgr(WasmEdge::LLVM::Type)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorVectorUAvgr(WasmEdge::LLVM::Type)::$_0&&)
Line
Count
Source
151
261
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
152
261
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
153
261
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
154
261
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
155
261
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorVectorOp<WasmEdge::FunctionCompiler::compileVectorVectorUMax(WasmEdge::LLVM::Type)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorVectorUMax(WasmEdge::LLVM::Type)::$_0&&)
Line
Count
Source
151
706
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
152
706
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
153
706
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
154
706
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
155
706
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorVectorOp<WasmEdge::FunctionCompiler::compileVectorVectorUMin(WasmEdge::LLVM::Type)::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorVectorUMin(WasmEdge::LLVM::Type)::$_0&&)
Line
Count
Source
151
281
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
152
281
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
153
281
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
154
281
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
155
281
  }
156
  void compileVectorVectorAdd(LLVM::Type VectorTy) noexcept;
157
  void compileVectorVectorAddSat(LLVM::Type VectorTy, bool Signed) noexcept;
158
  void compileVectorVectorSub(LLVM::Type VectorTy) noexcept;
159
  void compileVectorVectorSubSat(LLVM::Type VectorTy, bool Signed) noexcept;
160
  void compileVectorVectorMul(LLVM::Type VectorTy) noexcept;
161
  void compileVectorSwizzle() noexcept;
162
163
  void compileVectorVectorQ15MulSat() noexcept;
164
  void compileVectorVectorSMin(LLVM::Type VectorTy) noexcept;
165
  void compileVectorVectorUMin(LLVM::Type VectorTy) noexcept;
166
  void compileVectorVectorSMax(LLVM::Type VectorTy) noexcept;
167
  void compileVectorVectorUMax(LLVM::Type VectorTy) noexcept;
168
  void compileVectorVectorUAvgr(LLVM::Type VectorTy) noexcept;
169
  void compileVectorNarrow(LLVM::Type FromTy, bool Signed) noexcept;
170
  void compileVectorExtend(LLVM::Type FromTy, bool Signed, bool Low) noexcept;
171
  void compileVectorExtMul(LLVM::Type FromTy, bool Signed, bool Low) noexcept;
172
  void compileVectorExtAddPairwise(LLVM::Type VectorTy, bool Signed) noexcept;
173
  void compileVectorFAbs(LLVM::Type VectorTy) noexcept;
174
  void compileVectorFNeg(LLVM::Type VectorTy) noexcept;
175
  void compileVectorFSqrt(LLVM::Type VectorTy) noexcept;
176
  void compileVectorFCeil(LLVM::Type VectorTy) noexcept;
177
  void compileVectorFFloor(LLVM::Type VectorTy) noexcept;
178
  void compileVectorFTrunc(LLVM::Type VectorTy) noexcept;
179
  void compileVectorFNearest(LLVM::Type VectorTy) noexcept;
180
  void compileVectorVectorFAdd(LLVM::Type VectorTy) noexcept;
181
  void compileVectorVectorFSub(LLVM::Type VectorTy) noexcept;
182
  void compileVectorVectorFMul(LLVM::Type VectorTy) noexcept;
183
  void compileVectorVectorFDiv(LLVM::Type VectorTy) noexcept;
184
  void compileVectorVectorFMin(LLVM::Type VectorTy) noexcept;
185
  void compileVectorVectorFMax(LLVM::Type VectorTy) noexcept;
186
  void compileVectorVectorFPMin(LLVM::Type VectorTy) noexcept;
187
  void compileVectorVectorFPMax(LLVM::Type VectorTy) noexcept;
188
  void compileVectorTruncSatS32(LLVM::Type VectorTy, bool PadZero) noexcept;
189
  void compileVectorTruncSatU32(LLVM::Type VectorTy, bool PadZero) noexcept;
190
  void compileVectorConvertS(LLVM::Type VectorTy, LLVM::Type FPVectorTy,
191
                             bool Low) noexcept;
192
  void compileVectorConvertU(LLVM::Type VectorTy, LLVM::Type FPVectorTy,
193
                             bool Low) noexcept;
194
  void compileVectorDemote() noexcept;
195
  void compileVectorPromote() noexcept;
196
197
  void compileVectorVectorMAdd(LLVM::Type VectorTy) noexcept;
198
199
  void compileVectorVectorNMAdd(LLVM::Type VectorTy) noexcept;
200
201
  void compileVectorRelaxedIntegerDotProduct() noexcept;
202
203
  void compileVectorRelaxedIntegerDotProductAdd() noexcept;
204
205
  void
206
  enterBlock(LLVM::BasicBlock JumpBlock, LLVM::BasicBlock NextBlock,
207
             LLVM::BasicBlock ElseBlock, std::vector<LLVM::Value> Args,
208
             std::pair<std::vector<ValType>, std::vector<ValType>> Type,
209
             std::vector<std::tuple<std::vector<LLVM::Value>, LLVM::BasicBlock>>
210
                 ReturnPHI = {}) noexcept;
211
212
  Control leaveBlock() noexcept;
213
214
  void checkStop() noexcept;
215
216
  void setUnreachable() noexcept;
217
218
  bool isUnreachable() const noexcept;
219
220
  void
221
  buildPHI(Span<const ValType> RetType,
222
           Span<const std::tuple<std::vector<LLVM::Value>, LLVM::BasicBlock>>
223
               Incomings) noexcept;
224
225
  void setLableJumpPHI(unsigned int Index) noexcept;
226
227
  LLVM::BasicBlock getLabel(unsigned int Index) const noexcept;
228
229
864k
  void stackPush(LLVM::Value Value) noexcept { Stack.push_back(Value); }
230
  LLVM::Value stackPop() noexcept;
231
232
  LLVM::Value switchEndian(LLVM::Value Value);
233
234
  LLVM::Compiler::CompileContext &Context;
235
  LLVM::Context LLContext;
236
  std::vector<std::pair<LLVM::Type, LLVM::Value>> Local;
237
  std::vector<LLVM::Value> Stack;
238
  LLVM::Value LocalInstrCount = nullptr;
239
  LLVM::Value LocalGas = nullptr;
240
  std::unordered_map<ErrCode::Value, LLVM::BasicBlock> TrapBB;
241
  bool IsUnreachable = false;
242
  bool Interruptible = false;
243
  struct Control {
244
    size_t StackSize;
245
    bool Unreachable;
246
    LLVM::BasicBlock JumpBlock;
247
    LLVM::BasicBlock NextBlock;
248
    LLVM::BasicBlock ElseBlock;
249
    std::vector<LLVM::Value> Args;
250
    std::pair<std::vector<ValType>, std::vector<ValType>> Type;
251
    std::vector<std::tuple<std::vector<LLVM::Value>, LLVM::BasicBlock>>
252
        ReturnPHI;
253
    Control(size_t S, bool U, LLVM::BasicBlock J, LLVM::BasicBlock N,
254
            LLVM::BasicBlock E, std::vector<LLVM::Value> A,
255
            std::pair<std::vector<ValType>, std::vector<ValType>> T,
256
            std::vector<std::tuple<std::vector<LLVM::Value>, LLVM::BasicBlock>>
257
                R) noexcept
258
21.3k
        : StackSize(S), Unreachable(U), JumpBlock(J), NextBlock(N),
259
21.3k
          ElseBlock(E), Args(std::move(A)), Type(std::move(T)),
260
21.3k
          ReturnPHI(std::move(R)) {}
261
    Control(const Control &) = default;
262
26.3k
    Control(Control &&) = default;
263
    Control &operator=(const Control &) = default;
264
1.27k
    Control &operator=(Control &&) = default;
265
  };
266
  bool IsLazyJIT;
267
  std::vector<Control> ControlStack;
268
  LLVM::FunctionCallee F;
269
  LLVM::Value ExecCtx;
270
  LLVM::Builder Builder;
271
};
272
273
} // namespace WasmEdge