-
Notifications
You must be signed in to change notification settings - Fork 79
/
config.go
268 lines (235 loc) · 9.38 KB
/
config.go
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package wasmtime
// #include <wasm.h>
// #include <wasmtime.h>
// #include <stdlib.h>
import "C"
import (
"runtime"
"unsafe"
)
// Strategy is the compilation strategies for wasmtime
type Strategy C.wasmtime_strategy_t
const (
// StrategyAuto will let wasmtime automatically pick an appropriate compilation strategy
StrategyAuto Strategy = C.WASMTIME_STRATEGY_AUTO
// StrategyCranelift will force wasmtime to use the Cranelift backend
StrategyCranelift Strategy = C.WASMTIME_STRATEGY_CRANELIFT
)
// OptLevel decides what degree of optimization wasmtime will perform on generated machine code
type OptLevel C.wasmtime_opt_level_t
const (
// OptLevelNone will perform no optimizations
OptLevelNone OptLevel = C.WASMTIME_OPT_LEVEL_NONE
// OptLevelSpeed will optimize machine code to be as fast as possible
OptLevelSpeed OptLevel = C.WASMTIME_OPT_LEVEL_SPEED
// OptLevelSpeedAndSize will optimize machine code for speed, but also optimize
// to be small, sometimes at the cost of speed.
OptLevelSpeedAndSize OptLevel = C.WASMTIME_OPT_LEVEL_SPEED_AND_SIZE
)
// ProfilingStrategy decides what sort of profiling to enable, if any.
type ProfilingStrategy C.wasmtime_profiling_strategy_t
const (
// ProfilingStrategyNone means no profiler will be used
ProfilingStrategyNone ProfilingStrategy = C.WASMTIME_PROFILING_STRATEGY_NONE
// ProfilingStrategyJitdump will use the "jitdump" linux support
ProfilingStrategyJitdump ProfilingStrategy = C.WASMTIME_PROFILING_STRATEGY_JITDUMP
)
// Config holds options used to create an Engine and customize its behavior.
type Config struct {
_ptr *C.wasm_config_t
}
// NewConfig creates a new `Config` with all default options configured.
func NewConfig() *Config {
config := &Config{_ptr: C.wasm_config_new()}
runtime.SetFinalizer(config, func(config *Config) {
config.Close()
})
return config
}
// SetDebugInfo configures whether dwarf debug information for JIT code is enabled
func (cfg *Config) SetDebugInfo(enabled bool) {
C.wasmtime_config_debug_info_set(cfg.ptr(), C.bool(enabled))
runtime.KeepAlive(cfg)
}
// SetMaxWasmStack configures the maximum stack size, in bytes, that JIT code can use.
// The amount of stack space that wasm takes is always relative to the first invocation of wasm on the stack.
// Recursive calls with host frames in the middle will all need to fit within this setting.
// Note that this setting is not interpreted with 100% precision.
func (cfg *Config) SetMaxWasmStack(size int) {
C.wasmtime_config_max_wasm_stack_set(cfg.ptr(), C.size_t(size))
runtime.KeepAlive(cfg)
}
// SetWasmThreads configures whether the wasm threads proposal is enabled
func (cfg *Config) SetWasmThreads(enabled bool) {
C.wasmtime_config_wasm_threads_set(cfg.ptr(), C.bool(enabled))
runtime.KeepAlive(cfg)
}
// SetWasmReferenceTypes configures whether the wasm reference types proposal is enabled
func (cfg *Config) SetWasmReferenceTypes(enabled bool) {
C.wasmtime_config_wasm_reference_types_set(cfg.ptr(), C.bool(enabled))
runtime.KeepAlive(cfg)
}
// SetWasmSIMD configures whether the wasm SIMD proposal is enabled
func (cfg *Config) SetWasmSIMD(enabled bool) {
C.wasmtime_config_wasm_simd_set(cfg.ptr(), C.bool(enabled))
runtime.KeepAlive(cfg)
}
// SetWasmRelaxedSIMD configures whether the wasm relaxed SIMD proposal is enabled
func (cfg *Config) SetWasmRelaxedSIMD(enabled bool) {
C.wasmtime_config_wasm_relaxed_simd_set(cfg.ptr(), C.bool(enabled))
runtime.KeepAlive(cfg)
}
// SetWasmRelaxedSIMDDeterministic configures whether the wasm relaxed SIMD proposal is in deterministic mode
func (cfg *Config) SetWasmRelaxedSIMDDeterministic(enabled bool) {
C.wasmtime_config_wasm_relaxed_simd_deterministic_set(cfg.ptr(), C.bool(enabled))
runtime.KeepAlive(cfg)
}
// SetWasmBulkMemory configures whether the wasm bulk memory proposal is enabled
func (cfg *Config) SetWasmBulkMemory(enabled bool) {
C.wasmtime_config_wasm_bulk_memory_set(cfg.ptr(), C.bool(enabled))
runtime.KeepAlive(cfg)
}
// SetWasmMultiValue configures whether the wasm multi value proposal is enabled
func (cfg *Config) SetWasmMultiValue(enabled bool) {
C.wasmtime_config_wasm_multi_value_set(cfg.ptr(), C.bool(enabled))
runtime.KeepAlive(cfg)
}
// SetWasmMultiMemory configures whether the wasm multi memory proposal is enabled
func (cfg *Config) SetWasmMultiMemory(enabled bool) {
C.wasmtime_config_wasm_multi_memory_set(cfg.ptr(), C.bool(enabled))
runtime.KeepAlive(cfg)
}
// SetWasmMemory64 configures whether the wasm memory64 proposal is enabled
func (cfg *Config) SetWasmMemory64(enabled bool) {
C.wasmtime_config_wasm_memory64_set(cfg.ptr(), C.bool(enabled))
runtime.KeepAlive(cfg)
}
// SetConsumFuel configures whether fuel is enabled
func (cfg *Config) SetConsumeFuel(enabled bool) {
C.wasmtime_config_consume_fuel_set(cfg.ptr(), C.bool(enabled))
runtime.KeepAlive(cfg)
}
// SetStrategy configures what compilation strategy is used to compile wasm code
func (cfg *Config) SetStrategy(strat Strategy) {
C.wasmtime_config_strategy_set(cfg.ptr(), C.wasmtime_strategy_t(strat))
runtime.KeepAlive(cfg)
}
// SetCraneliftDebugVerifier configures whether the cranelift debug verifier will be active when
// cranelift is used to compile wasm code.
func (cfg *Config) SetCraneliftDebugVerifier(enabled bool) {
C.wasmtime_config_cranelift_debug_verifier_set(cfg.ptr(), C.bool(enabled))
runtime.KeepAlive(cfg)
}
// SetCraneliftOptLevel configures the cranelift optimization level for generated code
func (cfg *Config) SetCraneliftOptLevel(level OptLevel) {
C.wasmtime_config_cranelift_opt_level_set(cfg.ptr(), C.wasmtime_opt_level_t(level))
runtime.KeepAlive(cfg)
}
// SetProfiler configures what profiler strategy to use for generated code
func (cfg *Config) SetProfiler(profiler ProfilingStrategy) {
C.wasmtime_config_profiler_set(cfg.ptr(), C.wasmtime_profiling_strategy_t(profiler))
runtime.KeepAlive(cfg)
}
// CacheConfigLoadDefault enables compiled code caching for this `Config` using the default settings
// configuration can be found.
//
// For more information about caching see
// https://bytecodealliance.github.io/wasmtime/cli-cache.html
func (cfg *Config) CacheConfigLoadDefault() error {
err := C.wasmtime_config_cache_config_load(cfg.ptr(), nil)
runtime.KeepAlive(cfg)
if err != nil {
return mkError(err)
}
return nil
}
// CacheConfigLoad enables compiled code caching for this `Config` using the settings specified
// in the configuration file `path`.
//
// For more information about caching and configuration options see
// https://bytecodealliance.github.io/wasmtime/cli-cache.html
func (cfg *Config) CacheConfigLoad(path string) error {
cstr := C.CString(path)
err := C.wasmtime_config_cache_config_load(cfg.ptr(), cstr)
C.free(unsafe.Pointer(cstr))
runtime.KeepAlive(cfg)
if err != nil {
return mkError(err)
}
return nil
}
// SetEpochInterruption enables epoch-based instrumentation of generated code to
// interrupt WebAssembly execution when the current engine epoch exceeds a
// defined threshold.
func (cfg *Config) SetEpochInterruption(enable bool) {
C.wasmtime_config_epoch_interruption_set(cfg.ptr(), C.bool(enable))
runtime.KeepAlive(cfg)
}
// SetTarget configures the target triple that this configuration will produce
// machine code for.
//
// This option defaults to the native host. Calling this method will
// additionally disable inference of the native features of the host (e.g.
// detection of SSE4.2 on x86_64 hosts). Native features can be reenabled with
// the `cranelift_flag_{set,enable}` properties.
//
// For more information see the Rust documentation at
// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.config
func (cfg *Config) SetTarget(target string) error {
cstr := C.CString(target)
err := C.wasmtime_config_target_set(cfg.ptr(), cstr)
C.free(unsafe.Pointer(cstr))
runtime.KeepAlive(cfg)
if err != nil {
return mkError(err)
}
return nil
}
// EnableCraneliftFlag enables a target-specific flag in Cranelift.
//
// This can be used, for example, to enable SSE4.2 on x86_64 hosts. Settings can
// be explored with `wasmtime settings` on the CLI.
//
// For more information see the Rust documentation at
// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.cranelift_flag_enable
func (cfg *Config) EnableCraneliftFlag(flag string) {
cstr := C.CString(flag)
C.wasmtime_config_cranelift_flag_enable(cfg.ptr(), cstr)
C.free(unsafe.Pointer(cstr))
runtime.KeepAlive(cfg)
}
// SetCraneliftFlag sets a target-specific flag in Cranelift to the specified value.
//
// This can be used, for example, to enable SSE4.2 on x86_64 hosts. Settings can
// be explored with `wasmtime settings` on the CLI.
//
// For more information see the Rust documentation at
// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.cranelift_flag_set
func (cfg *Config) SetCraneliftFlag(name string, value string) {
cstrName := C.CString(name)
cstrValue := C.CString(value)
C.wasmtime_config_cranelift_flag_set(cfg.ptr(), cstrName, cstrValue)
C.free(unsafe.Pointer(cstrName))
C.free(unsafe.Pointer(cstrValue))
runtime.KeepAlive(cfg)
}
// See comments in `ffi.go` for what's going on here
func (cfg *Config) ptr() *C.wasm_config_t {
ret := cfg._ptr
if ret == nil {
panic("Config has already been used up")
}
maybeGC()
return ret
}
// Close will deallocate this config's state explicitly.
//
// For more information see the documentation for engine.Close()
func (cfg *Config) Close() {
if cfg._ptr == nil {
return
}
runtime.SetFinalizer(cfg, nil)
C.wasm_config_delete(cfg._ptr)
cfg._ptr = nil
}