Coverage Report

Created: 2026-08-14 06:41

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 compileTryTableOp(const AST::Instruction &Instr) noexcept;
75
76
  void compileThrowOp(const uint32_t TagIndex) noexcept;
77
78
  void compileThrowRefOp() noexcept;
79
80
  void compileCallOp(const unsigned int FuncIndex) noexcept;
81
82
  void compileIndirectCallOp(const uint32_t TableIndex,
83
                             const uint32_t FuncTypeIndex) noexcept;
84
85
  void compileReturnCallOp(const unsigned int FuncIndex) noexcept;
86
87
  void compileReturnIndirectCallOp(const uint32_t TableIndex,
88
                                   const uint32_t FuncTypeIndex) noexcept;
89
90
  void compileCallRefOp(const unsigned int TypeIndex) noexcept;
91
92
  void compileReturnCallRefOp(const unsigned int TypeIndex) noexcept;
93
94
  void compileLoadOp(unsigned MemoryIndex, uint64_t Offset, unsigned Alignment,
95
                     LLVM::Type LoadTy) noexcept;
96
  void compileLoadOp(unsigned MemoryIndex, uint64_t Offset, unsigned Alignment,
97
                     LLVM::Type LoadTy, LLVM::Type ExtendTy,
98
                     bool Signed) noexcept;
99
  void compileVectorLoadOp(unsigned MemoryIndex, uint64_t Offset,
100
                           unsigned Alignment, LLVM::Type LoadTy) noexcept;
101
  void compileVectorLoadOp(unsigned MemoryIndex, uint64_t Offset,
102
                           unsigned Alignment, LLVM::Type LoadTy,
103
                           LLVM::Type ExtendTy, bool Signed) noexcept;
104
  void compileSplatLoadOp(unsigned MemoryIndex, uint64_t Offset,
105
                          unsigned Alignment, LLVM::Type LoadTy,
106
                          LLVM::Type VectorTy) noexcept;
107
  void compileLoadLaneOp(unsigned MemoryIndex, uint64_t Offset,
108
                         unsigned Alignment, unsigned Index, LLVM::Type LoadTy,
109
                         LLVM::Type VectorTy) noexcept;
110
  void compileStoreOp(uint32_t MemoryIndex, uint64_t Offset, uint32_t Alignment,
111
                      LLVM::Type LoadTy, bool Trunc = false,
112
                      bool BitCast = false) noexcept;
113
  void compileStoreLaneOp(uint32_t MemoryIndex, uint64_t Offset,
114
                          uint32_t Alignment, uint8_t Index, LLVM::Type LoadTy,
115
                          LLVM::Type VectorTy) noexcept;
116
  void compileSplatOp(LLVM::Type VectorTy) noexcept;
117
  void compileExtractLaneOp(LLVM::Type VectorTy, unsigned Index) noexcept;
118
  void compileExtractLaneOp(LLVM::Type VectorTy, unsigned Index,
119
                            LLVM::Type ExtendTy, bool Signed) noexcept;
120
  void compileReplaceLaneOp(LLVM::Type VectorTy, unsigned Index) noexcept;
121
  void compileVectorCompareOp(LLVM::Type VectorTy,
122
                              LLVMIntPredicate Predicate) noexcept;
123
  void compileVectorCompareOp(LLVM::Type VectorTy, LLVMRealPredicate Predicate,
124
                              LLVM::Type ResultTy) noexcept;
125
  template <typename Func>
126
29.7k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
29.7k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
29.7k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
29.7k
  }
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
126
2.80k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
2.80k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
2.80k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
2.80k
  }
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
126
685
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
685
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
685
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
685
  }
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
126
1.97k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
1.97k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
1.97k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
1.97k
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorOp<WasmEdge::FunctionCompiler::compileVectorDemote()::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorDemote()::$_0&&)
Line
Count
Source
126
744
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
744
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
744
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
744
  }
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
126
3.16k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
3.16k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
3.16k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
3.16k
  }
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
126
556
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
556
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
556
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
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
126
1.81k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
1.81k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
1.81k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
1.81k
  }
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
126
2.90k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
2.90k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
2.90k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
2.90k
  }
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
126
437
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
437
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
437
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
437
  }
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
126
792
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
792
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
792
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
792
  }
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
126
268
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
268
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
268
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
268
  }
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
126
2.04k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
2.04k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
2.04k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
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
126
3.58k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
3.58k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
3.58k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
3.58k
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorOp<WasmEdge::FunctionCompiler::compileVectorPopcnt()::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorPopcnt()::$_0&&)
Line
Count
Source
126
126
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
126
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
126
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
126
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorOp<WasmEdge::FunctionCompiler::compileVectorPromote()::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorPromote()::$_0&&)
Line
Count
Source
126
813
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
813
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
813
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
813
  }
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
126
940
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
940
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
940
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
940
  }
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
126
6.05k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
6.05k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
6.05k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
6.05k
  }
130
  void compileVectorAbs(LLVM::Type VectorTy) noexcept;
131
  void compileVectorNeg(LLVM::Type VectorTy) noexcept;
132
  void compileVectorPopcnt() noexcept;
133
  template <typename Func>
134
2.19k
  void compileVectorReduceIOp(LLVM::Type VectorTy, Func &&Op) noexcept {
135
2.19k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
136
2.19k
    Stack.back() = Builder.createZExt(Op(V), Context.Int32Ty);
137
2.19k
  }
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
134
897
  void compileVectorReduceIOp(LLVM::Type VectorTy, Func &&Op) noexcept {
135
897
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
136
897
    Stack.back() = Builder.createZExt(Op(V), Context.Int32Ty);
137
897
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorReduceIOp<WasmEdge::FunctionCompiler::compileVectorAnyTrue()::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorAnyTrue()::$_0&&)
Line
Count
Source
134
162
  void compileVectorReduceIOp(LLVM::Type VectorTy, Func &&Op) noexcept {
135
162
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
136
162
    Stack.back() = Builder.createZExt(Op(V), Context.Int32Ty);
137
162
  }
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
134
1.13k
  void compileVectorReduceIOp(LLVM::Type VectorTy, Func &&Op) noexcept {
135
1.13k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
136
1.13k
    Stack.back() = Builder.createZExt(Op(V), Context.Int32Ty);
137
1.13k
  }
138
  void compileVectorAnyTrue() noexcept;
139
  void compileVectorAllTrue(LLVM::Type VectorTy) noexcept;
140
  void compileVectorBitMask(LLVM::Type VectorTy) noexcept;
141
  template <typename Func>
142
4.30k
  void compileVectorShiftOp(LLVM::Type VectorTy, Func &&Op) noexcept {
143
4.30k
    const bool Trunc = VectorTy.getElementType().getIntegerBitWidth() < 32;
144
4.30k
    const uint32_t Mask = VectorTy.getElementType().getIntegerBitWidth() - 1;
145
4.30k
    auto N = Builder.createAnd(stackPop(), LLContext.getInt32(Mask));
146
4.30k
    auto RHS = Builder.createVectorSplat(
147
4.30k
        VectorTy.getVectorSize(),
148
4.30k
        Trunc ? Builder.createTrunc(N, VectorTy.getElementType())
149
4.30k
              : Builder.createZExtOrTrunc(N, VectorTy.getElementType()));
150
4.30k
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
151
4.30k
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
152
4.30k
  }
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
142
2.01k
  void compileVectorShiftOp(LLVM::Type VectorTy, Func &&Op) noexcept {
143
2.01k
    const bool Trunc = VectorTy.getElementType().getIntegerBitWidth() < 32;
144
2.01k
    const uint32_t Mask = VectorTy.getElementType().getIntegerBitWidth() - 1;
145
2.01k
    auto N = Builder.createAnd(stackPop(), LLContext.getInt32(Mask));
146
2.01k
    auto RHS = Builder.createVectorSplat(
147
2.01k
        VectorTy.getVectorSize(),
148
2.01k
        Trunc ? Builder.createTrunc(N, VectorTy.getElementType())
149
2.01k
              : Builder.createZExtOrTrunc(N, VectorTy.getElementType()));
150
2.01k
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
151
2.01k
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
152
2.01k
  }
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
142
911
  void compileVectorShiftOp(LLVM::Type VectorTy, Func &&Op) noexcept {
143
911
    const bool Trunc = VectorTy.getElementType().getIntegerBitWidth() < 32;
144
911
    const uint32_t Mask = VectorTy.getElementType().getIntegerBitWidth() - 1;
145
911
    auto N = Builder.createAnd(stackPop(), LLContext.getInt32(Mask));
146
911
    auto RHS = Builder.createVectorSplat(
147
911
        VectorTy.getVectorSize(),
148
911
        Trunc ? Builder.createTrunc(N, VectorTy.getElementType())
149
911
              : Builder.createZExtOrTrunc(N, VectorTy.getElementType()));
150
911
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
151
911
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
152
911
  }
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
142
1.37k
  void compileVectorShiftOp(LLVM::Type VectorTy, Func &&Op) noexcept {
143
1.37k
    const bool Trunc = VectorTy.getElementType().getIntegerBitWidth() < 32;
144
1.37k
    const uint32_t Mask = VectorTy.getElementType().getIntegerBitWidth() - 1;
145
1.37k
    auto N = Builder.createAnd(stackPop(), LLContext.getInt32(Mask));
146
1.37k
    auto RHS = Builder.createVectorSplat(
147
1.37k
        VectorTy.getVectorSize(),
148
1.37k
        Trunc ? Builder.createTrunc(N, VectorTy.getElementType())
149
1.37k
              : Builder.createZExtOrTrunc(N, VectorTy.getElementType()));
150
1.37k
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
151
1.37k
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
152
1.37k
  }
153
  void compileVectorShl(LLVM::Type VectorTy) noexcept;
154
  void compileVectorLShr(LLVM::Type VectorTy) noexcept;
155
  void compileVectorAShr(LLVM::Type VectorTy) noexcept;
156
  template <typename Func>
157
7.67k
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
7.67k
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
7.67k
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
7.67k
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
7.67k
  }
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
157
440
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
440
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
440
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
440
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
440
  }
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
157
967
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
967
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
967
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
967
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
967
  }
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
157
177
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
177
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
177
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
177
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
177
  }
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
157
197
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
197
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
197
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
197
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
197
  }
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
157
214
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
214
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
214
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
214
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
214
  }
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
157
340
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
340
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
340
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
340
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
340
  }
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
157
268
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
268
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
268
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
268
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
268
  }
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
157
322
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
322
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
322
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
322
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
322
  }
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
157
328
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
328
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
328
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
328
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
328
  }
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
157
463
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
463
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
463
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
463
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
463
  }
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
157
475
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
475
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
475
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
475
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
475
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorVectorOp<WasmEdge::FunctionCompiler::compileVectorVectorQ15MulSat()::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorVectorQ15MulSat()::$_0&&)
Line
Count
Source
157
160
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
160
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
160
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
160
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
160
  }
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
157
527
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
527
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
527
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
527
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
527
  }
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
157
318
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
318
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
318
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
318
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
318
  }
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
157
772
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
772
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
772
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
772
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
772
  }
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
157
402
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
402
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
402
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
402
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
402
  }
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
157
270
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
270
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
270
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
270
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
270
  }
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
157
764
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
764
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
764
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
764
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
764
  }
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
157
266
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
266
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
266
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
266
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
266
  }
162
  void compileVectorVectorAdd(LLVM::Type VectorTy) noexcept;
163
  void compileVectorVectorAddSat(LLVM::Type VectorTy, bool Signed) noexcept;
164
  void compileVectorVectorSub(LLVM::Type VectorTy) noexcept;
165
  void compileVectorVectorSubSat(LLVM::Type VectorTy, bool Signed) noexcept;
166
  void compileVectorVectorMul(LLVM::Type VectorTy) noexcept;
167
  void compileVectorSwizzle() noexcept;
168
169
  void compileVectorVectorQ15MulSat() noexcept;
170
  void compileVectorVectorSMin(LLVM::Type VectorTy) noexcept;
171
  void compileVectorVectorUMin(LLVM::Type VectorTy) noexcept;
172
  void compileVectorVectorSMax(LLVM::Type VectorTy) noexcept;
173
  void compileVectorVectorUMax(LLVM::Type VectorTy) noexcept;
174
  void compileVectorVectorUAvgr(LLVM::Type VectorTy) noexcept;
175
  void compileVectorNarrow(LLVM::Type FromTy, bool Signed) noexcept;
176
  void compileVectorExtend(LLVM::Type FromTy, bool Signed, bool Low) noexcept;
177
  void compileVectorExtMul(LLVM::Type FromTy, bool Signed, bool Low) noexcept;
178
  void compileVectorExtAddPairwise(LLVM::Type VectorTy, bool Signed) noexcept;
179
  void compileVectorFAbs(LLVM::Type VectorTy) noexcept;
180
  void compileVectorFNeg(LLVM::Type VectorTy) noexcept;
181
  void compileVectorFSqrt(LLVM::Type VectorTy) noexcept;
182
  void compileVectorFCeil(LLVM::Type VectorTy) noexcept;
183
  void compileVectorFFloor(LLVM::Type VectorTy) noexcept;
184
  void compileVectorFTrunc(LLVM::Type VectorTy) noexcept;
185
  void compileVectorFNearest(LLVM::Type VectorTy) noexcept;
186
  void compileVectorVectorFAdd(LLVM::Type VectorTy) noexcept;
187
  void compileVectorVectorFSub(LLVM::Type VectorTy) noexcept;
188
  void compileVectorVectorFMul(LLVM::Type VectorTy) noexcept;
189
  void compileVectorVectorFDiv(LLVM::Type VectorTy) noexcept;
190
  void compileVectorVectorFMin(LLVM::Type VectorTy) noexcept;
191
  void compileVectorVectorFMax(LLVM::Type VectorTy) noexcept;
192
  void compileVectorVectorFPMin(LLVM::Type VectorTy) noexcept;
193
  void compileVectorVectorFPMax(LLVM::Type VectorTy) noexcept;
194
  void compileVectorTruncSatS32(LLVM::Type VectorTy, bool PadZero) noexcept;
195
  void compileVectorTruncSatU32(LLVM::Type VectorTy, bool PadZero) noexcept;
196
  void compileVectorConvertS(LLVM::Type VectorTy, LLVM::Type FPVectorTy,
197
                             bool Low) noexcept;
198
  void compileVectorConvertU(LLVM::Type VectorTy, LLVM::Type FPVectorTy,
199
                             bool Low) noexcept;
200
  void compileVectorDemote() noexcept;
201
  void compileVectorPromote() noexcept;
202
203
  void compileVectorVectorMAdd(LLVM::Type VectorTy) noexcept;
204
205
  void compileVectorVectorNMAdd(LLVM::Type VectorTy) noexcept;
206
207
  void compileVectorRelaxedIntegerDotProduct() noexcept;
208
209
  void compileVectorRelaxedIntegerDotProductAdd() noexcept;
210
211
  void
212
  enterBlock(LLVM::BasicBlock JumpBlock, LLVM::BasicBlock NextBlock,
213
             LLVM::BasicBlock ElseBlock, std::vector<LLVM::Value> Args,
214
             std::pair<std::vector<ValType>, std::vector<ValType>> Type,
215
             std::vector<std::tuple<std::vector<LLVM::Value>, LLVM::BasicBlock>>
216
                 ReturnPHI = {}) noexcept;
217
218
  Control leaveBlock() noexcept;
219
220
  void checkStop() noexcept;
221
222
  void checkPendingException() noexcept;
223
224
  void setUnreachable() noexcept;
225
226
  bool isUnreachable() const noexcept;
227
228
  void
229
  buildPHI(Span<const ValType> RetType,
230
           Span<const std::tuple<std::vector<LLVM::Value>, LLVM::BasicBlock>>
231
               Incomings) noexcept;
232
233
  void setLableJumpPHI(unsigned int Index) noexcept;
234
235
  LLVM::BasicBlock getLabel(unsigned int Index) const noexcept;
236
237
  LLVM::BasicBlock getEHDispatchTarget() noexcept;
238
239
898k
  void stackPush(LLVM::Value Value) noexcept { Stack.push_back(Value); }
240
  LLVM::Value stackPop() noexcept;
241
242
  LLVM::Value switchEndian(LLVM::Value Value);
243
244
  LLVM::Compiler::CompileContext &Context;
245
  LLVM::Context LLContext;
246
  std::vector<std::pair<LLVM::Type, LLVM::Value>> Local;
247
  std::vector<LLVM::Value> Stack;
248
  LLVM::Value LocalInstrCount = nullptr;
249
  LLVM::Value LocalGas = nullptr;
250
  std::unordered_map<ErrCode::Value, LLVM::BasicBlock> TrapBB;
251
  bool IsUnreachable = false;
252
  bool Interruptible = false;
253
  struct Control {
254
    size_t StackSize;
255
    bool Unreachable;
256
    LLVM::BasicBlock JumpBlock;
257
    LLVM::BasicBlock NextBlock;
258
    LLVM::BasicBlock ElseBlock;
259
    LLVM::BasicBlock TryDispatchBB;
260
    std::vector<LLVM::Value> Args;
261
    std::pair<std::vector<ValType>, std::vector<ValType>> Type;
262
    std::vector<std::tuple<std::vector<LLVM::Value>, LLVM::BasicBlock>>
263
        ReturnPHI;
264
    Control(size_t S, bool U, LLVM::BasicBlock J, LLVM::BasicBlock N,
265
            LLVM::BasicBlock E, std::vector<LLVM::Value> A,
266
            std::pair<std::vector<ValType>, std::vector<ValType>> T,
267
            std::vector<std::tuple<std::vector<LLVM::Value>, LLVM::BasicBlock>>
268
                R) noexcept
269
22.4k
        : StackSize(S), Unreachable(U), JumpBlock(J), NextBlock(N),
270
22.4k
          ElseBlock(E), TryDispatchBB(), Args(std::move(A)), Type(std::move(T)),
271
22.4k
          ReturnPHI(std::move(R)) {}
272
    Control(const Control &) = default;
273
27.6k
    Control(Control &&) = default;
274
    Control &operator=(const Control &) = default;
275
1.36k
    Control &operator=(Control &&) = default;
276
  };
277
  bool IsLazyJIT;
278
  std::vector<Control> ControlStack;
279
  LLVM::FunctionCallee F;
280
  LLVM::Value ExecCtx;
281
  LLVM::BasicBlock UnwindBB;
282
  LLVM::Builder Builder;
283
};
284
285
} // namespace WasmEdge