[emdawnwebgpu] Change link test to make function calls to test code size
Real function calls (even with bogus arguments) force all of the code to
be included, and also be able to invoke LTO inlining.
A volatile hack is used to prevent any of the arguments to the function
from being constexpr so they can't be constant-folded.
Bug: 414858419
Change-Id: Ic8e8245567990a55d15eaf504e75345c7ffcb4a3
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/252316
Reviewed-by: Loko Kung <lokokung@google.com>
Commit-Queue: Kai Ninomiya <kainino@chromium.org>
diff --git a/generator/templates/emdawnwebgpu/LinkTest.cpp b/generator/templates/emdawnwebgpu/LinkTest.cpp
index ec6af42..c90fcd1 100644
--- a/generator/templates/emdawnwebgpu/LinkTest.cpp
+++ b/generator/templates/emdawnwebgpu/LinkTest.cpp
@@ -1,14 +1,45 @@
-#include <cstdio>
+#include <cstring>
#include <webgpu/webgpu.h>
+namespace {
+
+// Make a runtime-constructed value of T in a way that can't be optimized away.
+template<typename T>
+T val() {
+ T value{};
+ static volatile bool x;
+ if (x) {
+ char nonzero = 123;
+ memcpy(&value, &nonzero, 1);
+ }
+ return value;
+}
+
+}
+
+{% macro render_dummy_args(this, args) %}
+ {%- if this %}
+ val<{{ as_cType(this.name) }}>()
+ {{- ", " if args }}
+ {%- endif %}
+ {%- for arg in args -%}
+ val<{{ decorate(as_cType(arg.type.name), arg) }}>()
+ {{- ", " if not loop.last }}
+ {%- endfor %}
+{%- endmacro -%}
+
int main() {
{% for function in by_category["function"] %}
- printf("%p\n", {{as_cMethod(None, function.name)}});
+ {{as_cMethod(None, function.name)}}(
+ {{- render_dummy_args(None, function.arguments) -}}
+ );
{% endfor %}
{% for type in by_category["object"] if len(c_methods(type)) > 0 %}
{% for method in c_methods(type) %}
- printf("%p\n", {{as_cMethod(type.name, method.name)}});
+ {{as_cMethod(type.name, method.name)}}(
+ {{- render_dummy_args(type, method.arguments) -}}
+ );
{% endfor %}
{% endfor %}
}
diff --git a/src/emdawnwebgpu/CMakeLists.txt b/src/emdawnwebgpu/CMakeLists.txt
index 7eb79ac..51ae79d 100644
--- a/src/emdawnwebgpu/CMakeLists.txt
+++ b/src/emdawnwebgpu/CMakeLists.txt
@@ -316,36 +316,40 @@
gmock_main_wasmsafe
)
+ # Build with Closure so we can look at code size, and to test that Closure minification
+ # works, and to let Closure statically analyze our JS.
+ add_library(enable_closure INTERFACE)
+ target_link_options(enable_closure
+ INTERFACE
+ "$<$<CONFIG:Release,MinSizeRel>:--closure=1>"
+ )
+
+ add_library(emdawnwebgpu_tests)
+ target_link_libraries(
+ emdawnwebgpu_tests
+ PUBLIC
+ enable_closure
+ ${emdawnwebgpu_test_deps}
+ )
+
add_executable(emdawnwebgpu_tests_asyncify ${emdawnwebgpu_test_sources})
set_target_properties(emdawnwebgpu_tests_asyncify PROPERTIES
SUFFIX ".html")
- target_link_libraries(
- emdawnwebgpu_tests_asyncify
- PUBLIC
- ${emdawnwebgpu_test_deps}
- )
+ target_link_libraries(emdawnwebgpu_tests_asyncify PUBLIC emdawnwebgpu_tests)
target_link_options(emdawnwebgpu_tests_asyncify
PUBLIC
# We need Asyncify or JSPI for Future tests.
"-sASYNCIFY=1"
- # Test that Closure minification and externs work.
- "$<$<CONFIG:Release>:--closure=1>"
)
add_executable(emdawnwebgpu_tests_jspi ${emdawnwebgpu_test_sources})
set_target_properties(emdawnwebgpu_tests_jspi PROPERTIES
SUFFIX ".html")
- target_link_libraries(
- emdawnwebgpu_tests_jspi
- PUBLIC
- ${emdawnwebgpu_test_deps}
- )
+ target_link_libraries(emdawnwebgpu_tests_jspi PUBLIC emdawnwebgpu_tests)
target_link_options(emdawnwebgpu_tests_jspi
PUBLIC
# We need Asyncify or JSPI for Future tests.
"-sJSPI=1"
- # Test that Closure minification and externs work.
- "$<$<CONFIG:Release>:--closure=1>"
)
DawnJSONGenerator(
@@ -356,19 +360,16 @@
add_executable(emdawnwebgpu_link_test
${EMDAWNWEBGPU_LINK_TEST_CPP_SOURCES}
)
- # The test is just that this links, not that it runs (it doesn't do
- # anything interesting), so just build to .js. Since it has a main()
- # function, this is the same as building to .html, but skipping the
- # .html file. It _is_ runnable if invoked via <script> or `node`.
+ # The test is just that this links, not that it runs (it will just
+ # crash), so just build to .js. Since it has a main() function, this is
+ # the same as building to .html, but skipping the .html file.
+ # This makes real (bogus) API calls to serve as a basic code size test.
set_target_properties(emdawnwebgpu_link_test PROPERTIES
SUFFIX ".js")
- target_link_options(emdawnwebgpu_link_test
- PRIVATE
- --closure=1
- )
target_link_libraries(
emdawnwebgpu_link_test
PUBLIC
+ enable_closure
emdawnwebgpu_c
)
endif()