Skip to content

Commit 8e0b8da

Browse files
keithwshravanrn
andauthored
wasm2c: include simd128.h and wasm-rt-exceptions.h where necessary (#2236)
Co-authored-by: Shravan Narayan <shravanrn@gmail.com>
1 parent 6269214 commit 8e0b8da

File tree

15 files changed

+409
-1218
lines changed

15 files changed

+409
-1218
lines changed

CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,20 +260,23 @@ add_custom_command(
260260
COMMAND ${CMAKE_COMMAND} -D out="${WABT_SOURCE_DIR}/src/prebuilt/wasm2c_header_bottom.cc" -D in="${WABT_SOURCE_DIR}/src/template/wasm2c.bottom.h" -D symbol="s_header_bottom" -P ${TEMPLATE_CMAKE}
261261
COMMAND ${CMAKE_COMMAND} -D out="${WABT_SOURCE_DIR}/src/prebuilt/wasm2c_source_includes.cc" -D in="${WABT_SOURCE_DIR}/src/template/wasm2c.includes.c" -D symbol="s_source_includes" -P ${TEMPLATE_CMAKE}
262262
COMMAND ${CMAKE_COMMAND} -D out="${WABT_SOURCE_DIR}/src/prebuilt/wasm2c_source_declarations.cc" -D in="${WABT_SOURCE_DIR}/src/template/wasm2c.declarations.c" -D symbol="s_source_declarations" -P ${TEMPLATE_CMAKE}
263+
COMMAND ${CMAKE_COMMAND} -D out="${WABT_SOURCE_DIR}/src/prebuilt/wasm2c_simd_source_declarations.cc" -D in="${WABT_SOURCE_DIR}/src/template/wasm2c_simd.declarations.c" -D symbol="s_simd_source_declarations" -P ${TEMPLATE_CMAKE}
263264
COMMAND ${CMAKE_COMMAND} -E touch gen-wasm2c-prebuilt
264265

265266
DEPENDS ${WABT_SOURCE_DIR}/src/template/wasm2c.top.h
266267
${WABT_SOURCE_DIR}/src/template/wasm2c.bottom.h
267268
${WABT_SOURCE_DIR}/src/template/wasm2c.includes.c
268269
${WABT_SOURCE_DIR}/src/template/wasm2c.declarations.c
270+
${WABT_SOURCE_DIR}/src/template/wasm2c_simd.declarations.c
269271
)
270272

271273
add_custom_target(gen-wasm2c-prebuilt-target DEPENDS gen-wasm2c-prebuilt)
272274

273275
set(CWRITER_TEMPLATE_SRC ${WABT_SOURCE_DIR}/src/prebuilt/wasm2c_header_top.cc
274276
${WABT_SOURCE_DIR}/src/prebuilt/wasm2c_header_bottom.cc
275277
${WABT_SOURCE_DIR}/src/prebuilt/wasm2c_source_includes.cc
276-
${WABT_SOURCE_DIR}/src/prebuilt/wasm2c_source_declarations.cc)
278+
${WABT_SOURCE_DIR}/src/prebuilt/wasm2c_source_declarations.cc
279+
${WABT_SOURCE_DIR}/src/prebuilt/wasm2c_simd_source_declarations.cc)
277280

278281
add_custom_target(everything)
279282

src/c-writer.cc

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ extern const char* s_header_top;
4242
extern const char* s_header_bottom;
4343
extern const char* s_source_includes;
4444
extern const char* s_source_declarations;
45+
extern const char* s_simd_source_declarations;
4546

4647
namespace wabt {
4748

@@ -368,7 +369,9 @@ class CWriter {
368369
const std::string&);
369370
void WriteCallIndirectFuncDeclaration(const FuncDeclaration&,
370371
const std::string&);
371-
void WriteFeatureMacros();
372+
void ComputeSimdScope();
373+
void WriteHeaderIncludes();
374+
void WriteV128Decl();
372375
void WriteModuleInstance();
373376
void WriteGlobals();
374377
void WriteGlobal(const Global&, const std::string&);
@@ -484,6 +487,8 @@ class CWriter {
484487
size_t,
485488
size_t)>
486489
name_to_output_file_index_;
490+
491+
bool simd_used_in_header_;
487492
};
488493

489494
// TODO: if WABT begins supporting debug names for labels,
@@ -1411,7 +1416,14 @@ std::string CWriter::GenerateHeaderGuard() const {
14111416
void CWriter::WriteSourceTop() {
14121417
Write(s_source_includes);
14131418
Write(Newline(), "#include \"", header_name_, "\"", Newline());
1414-
Write(s_source_declarations);
1419+
Write(s_source_declarations, Newline());
1420+
1421+
if (module_->features_used.simd) {
1422+
if (!simd_used_in_header_) {
1423+
WriteV128Decl();
1424+
}
1425+
Write(s_simd_source_declarations);
1426+
}
14151427
}
14161428

14171429
void CWriter::WriteMultiCTop() {
@@ -1790,13 +1802,50 @@ void CWriter::WriteCallIndirectFuncDeclaration(const FuncDeclaration& decl,
17901802
Write(")");
17911803
}
17921804

1793-
void CWriter::WriteFeatureMacros() {
1805+
static bool func_uses_simd(const FuncSignature& sig) {
1806+
return std::any_of(sig.param_types.begin(), sig.param_types.end(),
1807+
[](auto x) { return x == Type::V128; }) ||
1808+
std::any_of(sig.result_types.begin(), sig.result_types.end(),
1809+
[](auto x) { return x == Type::V128; });
1810+
}
1811+
1812+
void CWriter::ComputeSimdScope() {
1813+
simd_used_in_header_ =
1814+
module_->features_used.simd &&
1815+
(std::any_of(module_->globals.begin(), module_->globals.end(),
1816+
[](const auto& x) { return x->type == Type::V128; }) ||
1817+
std::any_of(module_->imports.begin(), module_->imports.end(),
1818+
[](const auto& x) {
1819+
return x->kind() == ExternalKind::Func &&
1820+
func_uses_simd(cast<FuncImport>(x)->func.decl.sig);
1821+
}) ||
1822+
std::any_of(module_->exports.begin(), module_->exports.end(),
1823+
[&](const auto& x) {
1824+
return x->kind == ExternalKind::Func &&
1825+
func_uses_simd(module_->GetFunc(x->var)->decl.sig);
1826+
}));
1827+
}
1828+
1829+
void CWriter::WriteHeaderIncludes() {
1830+
Write("#include \"wasm-rt.h\"", Newline());
1831+
17941832
if (module_->features_used.exceptions) {
1795-
Write("#define WASM_RT_ENABLE_EXCEPTION_HANDLING", Newline(), Newline());
1833+
Write("#include \"wasm-rt-exceptions.h\"", Newline(), Newline());
17961834
}
1797-
if (module_->features_used.simd) {
1798-
Write("#define WASM_RT_ENABLE_SIMD", Newline(), Newline());
1835+
1836+
if (simd_used_in_header_) {
1837+
WriteV128Decl();
17991838
}
1839+
1840+
Write(Newline());
1841+
}
1842+
1843+
void CWriter::WriteV128Decl() {
1844+
Write("#include <simde/wasm/simd128.h>", Newline(), Newline());
1845+
Write("#ifndef WASM_RT_SIMD_TYPE_DEFINED", Newline(),
1846+
"#define WASM_RT_SIMD_TYPE_DEFINED", Newline(),
1847+
"typedef simde_v128_t v128;", Newline(), "#endif", Newline(),
1848+
Newline());
18001849
}
18011850

18021851
void CWriter::WriteModuleInstance() {
@@ -5136,7 +5185,8 @@ void CWriter::WriteCHeader() {
51365185
Write("#ifndef ", guard, Newline());
51375186
Write("#define ", guard, Newline());
51385187
Write(Newline());
5139-
WriteFeatureMacros();
5188+
ComputeSimdScope();
5189+
WriteHeaderIncludes();
51405190
Write(s_header_top);
51415191
Write(Newline());
51425192
WriteModuleInstance();

src/prebuilt/wasm2c_header_top.cc

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,7 @@
11
const char* s_header_top = R"w2c_template(#include <stdint.h>
22
)w2c_template"
33
R"w2c_template(
4-
#include "wasm-rt.h"
5-
)w2c_template"
6-
R"w2c_template(
7-
#if defined(WASM_RT_ENABLE_EXCEPTION_HANDLING)
8-
)w2c_template"
9-
R"w2c_template(#include "wasm-rt-exceptions.h"
10-
)w2c_template"
11-
R"w2c_template(#endif
12-
)w2c_template"
13-
R"w2c_template(
14-
#if defined(WASM_RT_ENABLE_SIMD)
15-
)w2c_template"
16-
R"w2c_template(#include "simde/wasm/simd128.h"
17-
)w2c_template"
18-
R"w2c_template(#endif
19-
)w2c_template"
20-
R"w2c_template(
21-
/* TODO(binji): only use stdint.h types in header */
22-
)w2c_template"
23-
R"w2c_template(#ifndef WASM_RT_CORE_TYPES_DEFINED
4+
#ifndef WASM_RT_CORE_TYPES_DEFINED
245
)w2c_template"
256
R"w2c_template(#define WASM_RT_CORE_TYPES_DEFINED
267
)w2c_template"
@@ -44,17 +25,9 @@ R"w2c_template(typedef float f32;
4425
)w2c_template"
4526
R"w2c_template(typedef double f64;
4627
)w2c_template"
47-
R"w2c_template(
48-
#if defined(WASM_RT_ENABLE_SIMD)
49-
)w2c_template"
50-
R"w2c_template(typedef simde_v128_t v128;
51-
)w2c_template"
5228
R"w2c_template(#endif
5329
)w2c_template"
5430
R"w2c_template(
55-
#endif
56-
)w2c_template"
57-
R"w2c_template(
5831
#ifdef __cplusplus
5932
)w2c_template"
6033
R"w2c_template(extern "C" {

0 commit comments

Comments
 (0)