1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
#include "WAVM/IR/Types.h"
#include <stdlib.h>
#include <string.h>
#include <new>
#include <utility>
#include "WAVM/Inline/Hash.h"
#include "WAVM/Inline/HashSet.h"
#include "WAVM/Platform/Diagnostics.h"
#include "WAVM/Platform/Mutex.h"
using namespace WAVM;
using namespace WAVM::IR;
struct TypeTupleHashPolicy
{
static bool areKeysEqual(TypeTuple left, TypeTuple right)
{
if(left.size() != right.size()) { return false; }
for(Uptr elemIndex = 0; elemIndex < left.size(); ++elemIndex)
{
if(left[elemIndex] != right[elemIndex]) { return false; }
}
return true;
}
static Uptr getKeyHash(TypeTuple typeTuple) { return typeTuple.getHash(); }
};
struct FunctionTypeHashPolicy
{
static bool areKeysEqual(FunctionType left, FunctionType right)
{
return left.params() == right.params() && left.results() == right.results();
}
static Uptr getKeyHash(FunctionType functionType) { return functionType.getHash(); }
};
IR::TypeTuple::Impl::Impl(Uptr inNumElems, const ValueType* inElems) : numElems(inNumElems)
{
if(numElems) { memcpy(elems, inElems, sizeof(ValueType) * numElems); }
hash = XXH<Uptr>(elems, numElems * sizeof(ValueType), 0);
}
IR::TypeTuple::Impl::Impl(const Impl& inCopy) : hash(inCopy.hash), numElems(inCopy.numElems)
{
if(numElems) { memcpy(elems, inCopy.elems, numElems * sizeof(ValueType)); }
}
IR::TypeTuple::TypeTuple(ValueType inElem) { impl = getUniqueImpl(1, &inElem); }
IR::TypeTuple::TypeTuple(const std::initializer_list<ValueType>& inElems)
{
impl = getUniqueImpl(inElems.size(), inElems.begin());
}
IR::TypeTuple::TypeTuple(const std::vector<ValueType>& inElems)
{
impl = getUniqueImpl(inElems.size(), inElems.data());
}
IR::TypeTuple::TypeTuple(const ValueType* inElems, Uptr numElems)
{
impl = getUniqueImpl(numElems, inElems);
}
struct GlobalUniqueTypeTuples
{
Platform::Mutex mutex;
HashSet<TypeTuple, TypeTupleHashPolicy> set;
std::vector<void*> impls;
~GlobalUniqueTypeTuples()
{
Platform::Mutex::Lock lock(mutex);
for(void* impl : impls) { free(impl); }
}
static GlobalUniqueTypeTuples& get()
{
static GlobalUniqueTypeTuples singleton;
return singleton;
}
private:
GlobalUniqueTypeTuples() = default;
};
const TypeTuple::Impl* IR::TypeTuple::getUniqueImpl(Uptr numElems, const ValueType* inElems)
{
if(numElems == 0)
{
static Impl emptyImpl(0, nullptr);
return &emptyImpl;
}
else
{
const Uptr numImplBytes = Impl::calcNumBytes(numElems);
Impl* localImpl = new(alloca(numImplBytes)) Impl(numElems, inElems);
GlobalUniqueTypeTuples& globalUniqueTypeTuples = GlobalUniqueTypeTuples::get();
Platform::Mutex::Lock lock(globalUniqueTypeTuples.mutex);
const TypeTuple* typeTuple = globalUniqueTypeTuples.set.get(TypeTuple(localImpl));
if(typeTuple) { return typeTuple->impl; }
else
{
Impl* globalImpl = new(malloc(numImplBytes)) Impl(*localImpl);
globalUniqueTypeTuples.set.addOrFail(TypeTuple(globalImpl));
globalUniqueTypeTuples.impls.push_back(globalImpl);
return globalImpl;
}
}
}
struct GlobalUniqueFunctionTypes
{
Platform::Mutex mutex;
HashSet<FunctionType, FunctionTypeHashPolicy> set;
std::vector<void*> impls;
~GlobalUniqueFunctionTypes()
{
Platform::Mutex::Lock lock(mutex);
for(void* impl : impls) { free(impl); }
}
static GlobalUniqueFunctionTypes& get()
{
static GlobalUniqueFunctionTypes singleton;
return singleton;
}
private:
GlobalUniqueFunctionTypes() = default;
};
IR::FunctionType::Impl::Impl(TypeTuple inResults,
TypeTuple inParams,
CallingConvention inCallingConvention)
: results(inResults), params(inParams), callingConvention(inCallingConvention)
{
hash = Hash<Uptr>()(results.getHash(), params.getHash());
hash = Hash<Uptr>()(hash, Uptr(callingConvention));
}
const FunctionType::Impl* IR::FunctionType::getUniqueImpl(TypeTuple results,
TypeTuple params,
CallingConvention callingConvention)
{
if(results.size() == 0 && params.size() == 0 && callingConvention == CallingConvention::wasm)
{
static Impl emptyImpl{TypeTuple(), TypeTuple(), CallingConvention::wasm};
return &emptyImpl;
}
else if(results.size() == 0 && params.size() == 0
&& callingConvention == CallingConvention::intrinsic)
{
static Impl emptyImpl{TypeTuple(), TypeTuple(), CallingConvention::intrinsic};
return &emptyImpl;
}
else
{
Impl localImpl(results, params, callingConvention);
GlobalUniqueFunctionTypes& globalUniqueFunctionTypes = GlobalUniqueFunctionTypes::get();
Platform::Mutex::Lock lock(globalUniqueFunctionTypes.mutex);
const FunctionType* functionType
= globalUniqueFunctionTypes.set.get(FunctionType(&localImpl));
if(functionType) { return functionType->impl; }
else
{
Impl* globalImpl = new(malloc(sizeof(Impl))) Impl(localImpl);
globalUniqueFunctionTypes.set.addOrFail(FunctionType(globalImpl));
globalUniqueFunctionTypes.impls.push_back(globalImpl);
return globalImpl;
}
}
}
|