Skip to content

Commit

Permalink
Expose target and Cranelift settings in C API (bytecodealliance#6934)
Browse files Browse the repository at this point in the history
This commit comes from discussion on bytecodealliance#6932 and adds target settings to
the C API to configure compilation artifacts.
  • Loading branch information
alexcrichton authored Aug 30, 2023
1 parent 878a243 commit 2bb65c9
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
42 changes: 39 additions & 3 deletions crates/c-api/include/wasmtime/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,13 @@ WASMTIME_CONFIG_PROP(void, cranelift_debug_verifier, bool)
* When Cranelift is used as a code generation backend this will configure
* it to replace NaNs with a single canonical value. This is useful for users
* requiring entirely deterministic WebAssembly computation.
*
*
* This is not required by the WebAssembly spec, so it is not enabled by default.
*
*
* The default value for this is `false`
*/
WASMTIME_CONFIG_PROP(void, cranelift_nan_canonicalization, bool)

/**
* \brief Configures Cranelift's optimization level for JIT code.
*
Expand Down Expand Up @@ -331,6 +331,42 @@ WASMTIME_CONFIG_PROP(void, native_unwind_info, bool)
*/
WASM_API_EXTERN wasmtime_error_t* wasmtime_config_cache_config_load(wasm_config_t*, const char*);

/**
* \brief 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
*/
WASMTIME_CONFIG_PROP(wasmtime_error_t*, target, const char*)

/**
* \brief 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
*/
WASM_API_EXTERN void wasmtime_config_cranelift_flag_enable(wasm_config_t*, const char*);

/**
* \brief 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
*/
WASM_API_EXTERN void wasmtime_config_cranelift_flag_set(wasm_config_t*, const char *key, const char *value);

#ifdef __cplusplus
} // extern "C"
#endif
Expand Down
29 changes: 29 additions & 0 deletions crates/c-api/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,32 @@ pub extern "C" fn wasmtime_config_dynamic_memory_reserved_for_growth_set(
pub extern "C" fn wasmtime_config_native_unwind_info_set(c: &mut wasm_config_t, enabled: bool) {
c.config.native_unwind_info(enabled);
}

#[no_mangle]
pub unsafe extern "C" fn wasmtime_config_target_set(
c: &mut wasm_config_t,
target: *const c_char,
) -> Option<Box<wasmtime_error_t>> {
let target = CStr::from_ptr(target).to_str().expect("not valid utf-8");
handle_result(c.config.target(target), |_cfg| {})
}

#[no_mangle]
pub unsafe extern "C" fn wasmtime_config_cranelift_flag_enable(
c: &mut wasm_config_t,
flag: *const c_char,
) {
let flag = CStr::from_ptr(flag).to_str().expect("not valid utf-8");
c.config.cranelift_flag_enable(flag);
}

#[no_mangle]
pub unsafe extern "C" fn wasmtime_config_cranelift_flag_set(
c: &mut wasm_config_t,
flag: *const c_char,
value: *const c_char,
) {
let flag = CStr::from_ptr(flag).to_str().expect("not valid utf-8");
let value = CStr::from_ptr(value).to_str().expect("not valid utf-8");
c.config.cranelift_flag_set(flag, value);
}

0 comments on commit 2bb65c9

Please sign in to comment.