Coverage Report

Created: 2026-08-08 06:32

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
28.8k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
28.8k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
28.8k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
28.8k
  }
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.68k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
2.68k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
2.68k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
2.68k
  }
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
648
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
648
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
648
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
648
  }
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.85k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
1.85k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
1.85k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
1.85k
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorOp<WasmEdge::FunctionCompiler::compileVectorDemote()::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorDemote()::$_0&&)
Line
Count
Source
126
739
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
739
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
739
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
739
  }
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
2.99k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
2.99k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
2.99k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
2.99k
  }
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
557
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
557
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
557
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
557
  }
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.76k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
1.76k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
1.76k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
1.76k
  }
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.83k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
2.83k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
2.83k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
2.83k
  }
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
786
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
786
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
786
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
786
  }
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
273
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
273
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
273
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
273
  }
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.03k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
2.03k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
2.03k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
2.03k
  }
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.41k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
3.41k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
3.41k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
3.41k
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorOp<WasmEdge::FunctionCompiler::compileVectorPopcnt()::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorPopcnt()::$_0&&)
Line
Count
Source
126
140
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
140
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
140
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
140
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorOp<WasmEdge::FunctionCompiler::compileVectorPromote()::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorPromote()::$_0&&)
Line
Count
Source
126
797
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
797
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
797
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
797
  }
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
928
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
928
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
928
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
928
  }
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
5.96k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
127
5.96k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
128
5.96k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
129
5.96k
  }
130
  void compileVectorAbs(LLVM::Type VectorTy) noexcept;
131
  void compileVectorNeg(LLVM::Type VectorTy) noexcept;
132
  void compileVectorPopcnt() noexcept;
133
  template <typename Func>
134
2.16k
  void compileVectorReduceIOp(LLVM::Type VectorTy, Func &&Op) noexcept {
135
2.16k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
136
2.16k
    Stack.back() = Builder.createZExt(Op(V), Context.Int32Ty);
137
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
134
889
  void compileVectorReduceIOp(LLVM::Type VectorTy, Func &&Op) noexcept {
135
889
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
136
889
    Stack.back() = Builder.createZExt(Op(V), Context.Int32Ty);
137
889
  }
vectorInstr.cpp:void WasmEdge::FunctionCompiler::compileVectorReduceIOp<WasmEdge::FunctionCompiler::compileVectorAnyTrue()::$_0>(WasmEdge::LLVM::Type, WasmEdge::FunctionCompiler::compileVectorAnyTrue()::$_0&&)
Line
Count
Source
134
152
  void compileVectorReduceIOp(LLVM::Type VectorTy, Func &&Op) noexcept {
135
152
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
136
152
    Stack.back() = Builder.createZExt(Op(V), Context.Int32Ty);
137
152
  }
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.12k
  void compileVectorReduceIOp(LLVM::Type VectorTy, Func &&Op) noexcept {
135
1.12k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
136
1.12k
    Stack.back() = Builder.createZExt(Op(V), Context.Int32Ty);
137
1.12k
  }
138
  void compileVectorAnyTrue() noexcept;
139
  void compileVectorAllTrue(LLVM::Type VectorTy) noexcept;
140
  void compileVectorBitMask(LLVM::Type VectorTy) noexcept;
141
  template <typename Func>
142
4.06k
  void compileVectorShiftOp(LLVM::Type VectorTy, Func &&Op) noexcept {
143
4.06k
    const bool Trunc = VectorTy.getElementType().getIntegerBitWidth() < 32;
144
4.06k
    const uint32_t Mask = VectorTy.getElementType().getIntegerBitWidth() - 1;
145
4.06k
    auto N = Builder.createAnd(stackPop(), LLContext.getInt32(Mask));
146
4.06k
    auto RHS = Builder.createVectorSplat(
147
4.06k
        VectorTy.getVectorSize(),
148
4.06k
        Trunc ? Builder.createTrunc(N, VectorTy.getElementType())
149
4.06k
              : Builder.createZExtOrTrunc(N, VectorTy.getElementType()));
150
4.06k
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
151
4.06k
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
152
4.06k
  }
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
1.96k
  void compileVectorShiftOp(LLVM::Type VectorTy, Func &&Op) noexcept {
143
1.96k
    const bool Trunc = VectorTy.getElementType().getIntegerBitWidth() < 32;
144
1.96k
    const uint32_t Mask = VectorTy.getElementType().getIntegerBitWidth() - 1;
145
1.96k
    auto N = Builder.createAnd(stackPop(), LLContext.getInt32(Mask));
146
1.96k
    auto RHS = Builder.createVectorSplat(
147
1.96k
        VectorTy.getVectorSize(),
148
1.96k
        Trunc ? Builder.createTrunc(N, VectorTy.getElementType())
149
1.96k
              : Builder.createZExtOrTrunc(N, VectorTy.getElementType()));
150
1.96k
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
151
1.96k
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
152
1.96k
  }
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
745
  void compileVectorShiftOp(LLVM::Type VectorTy, Func &&Op) noexcept {
143
745
    const bool Trunc = VectorTy.getElementType().getIntegerBitWidth() < 32;
144
745
    const uint32_t Mask = VectorTy.getElementType().getIntegerBitWidth() - 1;
145
745
    auto N = Builder.createAnd(stackPop(), LLContext.getInt32(Mask));
146
745
    auto RHS = Builder.createVectorSplat(
147
745
        VectorTy.getVectorSize(),
148
745
        Trunc ? Builder.createTrunc(N, VectorTy.getElementType())
149
745
              : Builder.createZExtOrTrunc(N, VectorTy.getElementType()));
150
745
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
151
745
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
152
745
  }
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.35k
  void compileVectorShiftOp(LLVM::Type VectorTy, Func &&Op) noexcept {
143
1.35k
    const bool Trunc = VectorTy.getElementType().getIntegerBitWidth() < 32;
144
1.35k
    const uint32_t Mask = VectorTy.getElementType().getIntegerBitWidth() - 1;
145
1.35k
    auto N = Builder.createAnd(stackPop(), LLContext.getInt32(Mask));
146
1.35k
    auto RHS = Builder.createVectorSplat(
147
1.35k
        VectorTy.getVectorSize(),
148
1.35k
        Trunc ? Builder.createTrunc(N, VectorTy.getElementType())
149
1.35k
              : Builder.createZExtOrTrunc(N, VectorTy.getElementType()));
150
1.35k
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
151
1.35k
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
152
1.35k
  }
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.39k
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
7.39k
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
7.39k
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
7.39k
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
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
157
416
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
416
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
416
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
416
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
416
  }
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
867
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
867
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
867
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
867
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
867
  }
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
171
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
171
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
171
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
171
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
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
157
198
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
198
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
198
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
198
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
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
157
209
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
209
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
209
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
209
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
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
157
339
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
339
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
339
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
339
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
339
  }
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
323
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
323
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
323
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
323
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
323
  }
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
461
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
461
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
461
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
461
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
461
  }
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
432
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
432
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
432
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
432
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
432
  }
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
520
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
520
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
520
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
520
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
520
  }
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
323
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
323
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
323
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
323
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
323
  }
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
694
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
694
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
694
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
694
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
694
  }
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
396
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
396
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
396
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
396
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
396
  }
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
277
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
277
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
277
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
277
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
277
  }
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
740
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
740
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
740
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
740
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
740
  }
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
275
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
158
275
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
159
275
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
160
275
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
161
275
  }
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
21.6k
        : StackSize(S), Unreachable(U), JumpBlock(J), NextBlock(N),
270
21.6k
          ElseBlock(E), TryDispatchBB(), Args(std::move(A)), Type(std::move(T)),
271
21.6k
          ReturnPHI(std::move(R)) {}
272
    Control(const Control &) = default;
273
26.7k
    Control(Control &&) = default;
274
    Control &operator=(const Control &) = default;
275
1.33k
    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