From 29e964ad1defeabef7f2d59221026f141c862a77 Mon Sep 17 00:00:00 2001 From: Scott Wolchok Date: Thu, 26 Jun 2025 13:00:24 -0700 Subject: [PATCH 1/8] Update [ghstack-poisoned] --- kernels/portable/cpu/util/normalization_ops_util.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/kernels/portable/cpu/util/normalization_ops_util.cpp b/kernels/portable/cpu/util/normalization_ops_util.cpp index 4adcf02b303..dedab427ae7 100644 --- a/kernels/portable/cpu/util/normalization_ops_util.cpp +++ b/kernels/portable/cpu/util/normalization_ops_util.cpp @@ -7,6 +7,7 @@ */ #include +#include #include #include @@ -92,6 +93,11 @@ bool check_layer_norm_args( ", ndim = %zu", in.dim(), ndim); + ET_CHECK_OR_RETURN_FALSE( + ndim <= kTensorDimensionLimit, + "Expected normalized shape to have at most %zu dimensions but it had %zu", + kTensorDimensionLimit, + ndim); size_t shift = in.dim() - ndim; for (const auto d : c10::irange(ndim)) { ET_CHECK_OR_RETURN_FALSE( @@ -103,7 +109,7 @@ bool check_layer_norm_args( d, normalized_shape[d]); } - executorch::aten::SizesType shape[ndim]; + std::array shape; for (const auto i : c10::irange(ndim)) { shape[i] = static_cast(normalized_shape[i]); } @@ -111,12 +117,12 @@ bool check_layer_norm_args( if (weight.has_value()) { ET_LOG_AND_RETURN_IF_FALSE(tensors_have_same_dtype(in, weight.value())); ET_LOG_AND_RETURN_IF_FALSE( - tensor_has_expected_size(weight.value(), {shape, ndim})); + tensor_has_expected_size(weight.value(), {shape.data(), ndim})); } if (bias.has_value()) { ET_LOG_AND_RETURN_IF_FALSE(tensors_have_same_dtype(in, bias.value())); ET_LOG_AND_RETURN_IF_FALSE( - tensor_has_expected_size(bias.value(), {shape, ndim})); + tensor_has_expected_size(bias.value(), {shape.data(), ndim})); } ET_LOG_AND_RETURN_IF_FALSE(tensors_have_same_dtype(in, out)); ET_LOG_AND_RETURN_IF_FALSE(tensors_have_same_dtype(in, mean_out)); From 1ae275b372315b031e206fbe979f13ab155009ef Mon Sep 17 00:00:00 2001 From: Scott Wolchok Date: Thu, 26 Jun 2025 13:00:28 -0700 Subject: [PATCH 2/8] Update [ghstack-poisoned] --- test/build_optimized_size_test.sh | 3 +++ test/build_size_test.sh | 2 ++ 2 files changed, 5 insertions(+) diff --git a/test/build_optimized_size_test.sh b/test/build_optimized_size_test.sh index d7b055559f0..3141a29e9f1 100644 --- a/test/build_optimized_size_test.sh +++ b/test/build_optimized_size_test.sh @@ -41,12 +41,15 @@ test_cmake_size_test() { echo 'ExecuTorch with no ops binary size, unstripped:' ls -al cmake-out/test/size_test + size cmake-out/test/size_test echo 'ExecuTorch with portable ops binary size, unstripped:' ls -al cmake-out/test/size_test_all_ops + size cmake-out/test/size_test_all_ops echo 'ExecuTorch with optimized ops binary size, unstripped:' ls -al cmake-out/test/size_test_all_optimized_ops + size cmake-out/test/size_test_all_optimized_ops } if [[ -z $PYTHON_EXECUTABLE ]]; then diff --git a/test/build_size_test.sh b/test/build_size_test.sh index d020ab58c95..baeef5a849e 100644 --- a/test/build_size_test.sh +++ b/test/build_size_test.sh @@ -46,9 +46,11 @@ test_cmake_size_test() { echo 'ExecuTorch with no ops binary size, unstripped:' ls -al cmake-out/test/size_test + size cmake-out/test/size_test echo 'ExecuTorch with portable ops binary size, unstripped:' ls -al cmake-out/test/size_test_all_ops + size cmake-out/test/size_test_all_ops } if [[ -z $PYTHON_EXECUTABLE ]]; then From e0f20e4010b7bde1dc4c110b87d658f00915083c Mon Sep 17 00:00:00 2001 From: Scott Wolchok Date: Thu, 26 Jun 2025 13:00:32 -0700 Subject: [PATCH 3/8] Update [ghstack-poisoned] --- test/build_size_test.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/build_size_test.sh b/test/build_size_test.sh index baeef5a849e..8b7c37fb020 100644 --- a/test/build_size_test.sh +++ b/test/build_size_test.sh @@ -26,23 +26,23 @@ cmake_install_executorch_lib() { CXXFLAGS="$COMMON_CXXFLAGS" retry cmake -DBUCK2="$BUCK2" \ -DCMAKE_CXX_STANDARD_REQUIRED=ON \ -DCMAKE_INSTALL_PREFIX=cmake-out \ - -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_BUILD_TYPE=RelWithDebInfo \ -DEXECUTORCH_BUILD_EXECUTOR_RUNNER=OFF \ -DEXECUTORCH_OPTIMIZE_SIZE=ON \ -DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \ ${EXTRA_BUILD_ARGS} \ -Bcmake-out . - cmake --build cmake-out -j9 --target install --config Release + cmake --build cmake-out -j9 --target install --config RelWithDebInfo } test_cmake_size_test() { - CXXFLAGS="$COMMON_CXXFLAGS" retry cmake -DCMAKE_BUILD_TYPE=Release \ + CXXFLAGS="$COMMON_CXXFLAGS" retry cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo \ -DCMAKE_INSTALL_PREFIX=cmake-out \ ${EXTRA_BUILD_ARGS} \ -Bcmake-out/test test echo "Build size test" - cmake --build cmake-out/test -j9 --config Release + cmake --build cmake-out/test -j9 --config RelWithDebInfo echo 'ExecuTorch with no ops binary size, unstripped:' ls -al cmake-out/test/size_test From 11f0f66a91628b6ec41b5aa689f575ed5287cc48 Mon Sep 17 00:00:00 2001 From: Scott Wolchok Date: Thu, 26 Jun 2025 14:04:18 -0700 Subject: [PATCH 4/8] Update [ghstack-poisoned] --- .../cpu/util/broadcast_indexes_range.h | 81 +++++++++++++++++++ .../test/broadcast_indexes_range_test.cpp | 18 ----- 2 files changed, 81 insertions(+), 18 deletions(-) diff --git a/kernels/portable/cpu/util/broadcast_indexes_range.h b/kernels/portable/cpu/util/broadcast_indexes_range.h index 7434748d505..ae9970df653 100644 --- a/kernels/portable/cpu/util/broadcast_indexes_range.h +++ b/kernels/portable/cpu/util/broadcast_indexes_range.h @@ -236,6 +236,87 @@ class BroadcastIndexesIterator { // shape would contain 1s. std::array effective_input_broadcast_strides_; }; + +// When there is only 1 input and no noncontiguous tensor support +// required, there is no actual broadcasting to do. +template <> +class BroadcastIndexesIterator<1, false> { + public: + using difference_type = ssize_t; + using value_type = std::array; + using reference = value_type; + using pointer = const value_type*; + using iterator_category = std::forward_iterator_tag; + + BroadcastIndexesIterator() = default; + + explicit BroadcastIndexesIterator( + [[maybe_unused]] const Tensor& output, + [[maybe_unused]] const Tensor& input) {} + + struct make_end_t { + explicit constexpr make_end_t() = default; + }; + + BroadcastIndexesIterator( + make_end_t, + const Tensor& output, + [[maybe_unused]] const Tensor& input) + : current_indexes_({output.numel(), output.numel()}) {} + + bool operator==(const BroadcastIndexesIterator& rhs) const { + return current_index() == rhs.current_index(); + } + + bool operator!=(const BroadcastIndexesIterator& rhs) const { + return current_index() != rhs.current_index(); + } + + reference operator*() const { + return current_indexes_; + } + + pointer operator->() const { + return ¤t_indexes_; + } + + BroadcastIndexesIterator& operator++() { + add_to_current_index(1); + return *this; + } + + BroadcastIndexesIterator operator++(int) { + auto it = *this; + operator++(); + return it; + } + + BroadcastIndexesIterator& operator+=(difference_type n) { + add_to_current_index(n); + return *this; + } + + BroadcastIndexesIterator operator+(difference_type n) { + auto it = *this; + it += n; + return it; + } + + difference_type operator-(const BroadcastIndexesIterator& rhs) const { + return difference_type(current_index() - rhs.current_index()); + } + + private: + ssize_t current_index() const { + return current_indexes_[0]; + } + + void add_to_current_index(ssize_t n) { + current_indexes_[0] += n; + current_indexes_[1] = current_indexes_[0]; + } + value_type current_indexes_ = {{0, 0}}; +}; } // namespace internal /** diff --git a/kernels/portable/cpu/util/test/broadcast_indexes_range_test.cpp b/kernels/portable/cpu/util/test/broadcast_indexes_range_test.cpp index 1023915ea66..42fd2484cf0 100644 --- a/kernels/portable/cpu/util/test/broadcast_indexes_range_test.cpp +++ b/kernels/portable/cpu/util/test/broadcast_indexes_range_test.cpp @@ -52,24 +52,6 @@ TEST(BroadcastIndexesRangeTest, OneDNotBroadcasted) { } } -// [1] -> [W] -TEST(BroadcastIndexesRangeTest, ScalarBroadcastToOneD) { - TensorFactory tf; - - Tensor out = tf.zeros({5}); - Tensor in = tf.zeros({1}); - - auto actual = range_to_vec(BroadcastIndexesRange<1>(out, in)); - decltype(actual) expected = { - {0, 0}, - {1, 0}, - {2, 0}, - {3, 0}, - {4, 0}, - }; - EXPECT_EQ(expected, actual); -} - template void test_operator_plus(const Range& range) { size_t idx = 0; From bd87af0a64ff3259dd2b886e9d3c632ab72e2c94 Mon Sep 17 00:00:00 2001 From: Scott Wolchok Date: Thu, 26 Jun 2025 14:52:21 -0700 Subject: [PATCH 5/8] Update [ghstack-poisoned] --- .github/workflows/pull.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/pull.yml b/.github/workflows/pull.yml index df254b7f409..7659b178ec7 100644 --- a/.github/workflows/pull.yml +++ b/.github/workflows/pull.yml @@ -366,6 +366,7 @@ jobs: # build module for executorch.extension.pybindings.portable_lib bash test/build_size_test.sh strip cmake-out/test/size_test + strip --strip-debug cmake-out/test/size_test output=$(ls -la cmake-out/test/size_test) arr=($output) size=${arr[4]} @@ -403,6 +404,7 @@ jobs: # build module for executorch.extension.pybindings.portable_lib bash test/build_size_test.sh strip cmake-out/test/size_test + strip --strip-debug cmake-out/test/size_test output=$(ls -la cmake-out/test/size_test) arr=($output) size=${arr[4]} From bf512fea3240526377e9e1107bb408a468bc5fc5 Mon Sep 17 00:00:00 2001 From: Scott Wolchok Date: Thu, 26 Jun 2025 17:28:17 -0700 Subject: [PATCH 6/8] Update [ghstack-poisoned] --- CMakeLists.txt | 2 +- backends/qualcomm/CMakeLists.txt | 4 ++-- examples/models/llama/CMakeLists.txt | 2 +- examples/models/llava/CMakeLists.txt | 2 +- examples/selective_build/CMakeLists.txt | 2 +- test/CMakeLists.txt | 6 +++--- tools/cmake/preset/default.cmake | 2 +- 7 files changed, 10 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3a3b1f7bfe0..5128a013076 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -694,7 +694,7 @@ if(EXECUTORCH_BUILD_EXECUTOR_RUNNER) endif() add_executable(executor_runner ${_executor_runner__srcs}) - if(CMAKE_BUILD_TYPE STREQUAL "Release") + if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") if(APPLE) target_link_options(executor_runner PRIVATE "LINKER:-dead_strip") else() diff --git a/backends/qualcomm/CMakeLists.txt b/backends/qualcomm/CMakeLists.txt index 203cdf2c314..9f1be61de2b 100644 --- a/backends/qualcomm/CMakeLists.txt +++ b/backends/qualcomm/CMakeLists.txt @@ -60,7 +60,7 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") add_link_options("-flto=auto") endif() -if(CMAKE_BUILD_TYPE STREQUAL "Release") +if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") # strip symbols add_link_options("-s") @@ -259,7 +259,7 @@ if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86_64") pybind11_strip(PyQnnWrapperAdaptor) endif() - if(CMAKE_BUILD_TYPE STREQUAL "Release") + if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") # need to allow exceptions in pybind set(_pybind_compile_options -Wno-deprecated-declarations -fPIC -frtti -fexceptions diff --git a/examples/models/llama/CMakeLists.txt b/examples/models/llama/CMakeLists.txt index 8c27de20845..3216469c13a 100644 --- a/examples/models/llama/CMakeLists.txt +++ b/examples/models/llama/CMakeLists.txt @@ -209,7 +209,7 @@ if(ANDROID) endif() add_executable(llama_main ${_srcs}) -if(CMAKE_BUILD_TYPE STREQUAL "Release") +if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") if(APPLE) target_link_options(llama_main PRIVATE "LINKER:-dead_strip") else() diff --git a/examples/models/llava/CMakeLists.txt b/examples/models/llava/CMakeLists.txt index 64be6111674..41270d0133f 100644 --- a/examples/models/llava/CMakeLists.txt +++ b/examples/models/llava/CMakeLists.txt @@ -197,7 +197,7 @@ list(APPEND _common_include_directories ${stb_SOURCE_DIR} ) add_executable(llava_main ${_srcs}) -if(CMAKE_BUILD_TYPE STREQUAL "Release") +if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") if(APPLE) target_link_options(llava_main PRIVATE "LINKER:-dead_strip,-s") else() diff --git a/examples/selective_build/CMakeLists.txt b/examples/selective_build/CMakeLists.txt index 39b212b16fb..24fd102fee9 100644 --- a/examples/selective_build/CMakeLists.txt +++ b/examples/selective_build/CMakeLists.txt @@ -151,7 +151,7 @@ list(TRANSFORM _executor_runner__srcs PREPEND "${EXECUTORCH_ROOT}/") # link to # add_executable(selective_build_test ${_executor_runner__srcs}) -if(CMAKE_BUILD_TYPE EQUAL "Release") +if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") target_link_options(selective_build_test PRIVATE "LINKER:--gc-sections") endif() target_link_libraries( diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 0e3a6703c54..967610f48f8 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -52,7 +52,7 @@ list(TRANSFORM _size_test__srcs PREPEND "${EXECUTORCH_ROOT}/") # when we cross compile to ios add_executable(size_test ${_size_test__srcs}) target_link_libraries(size_test executorch) -if(CMAKE_BUILD_TYPE EQUAL "Release") +if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") target_link_options(size_test PRIVATE "LINKER:--gc-sections") endif() @@ -64,7 +64,7 @@ target_link_options_shared_lib(portable_ops_lib) target_link_libraries( size_test_all_ops executorch portable_ops_lib portable_kernels ) -if(CMAKE_BUILD_TYPE EQUAL "Release") +if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") target_link_options(size_test_all_ops PRIVATE "LINKER:--gc-sections") endif() @@ -76,7 +76,7 @@ add_executable(size_test_all_optimized_ops ${_size_test__srcs}) target_link_options_shared_lib(optimized_native_cpu_ops_lib) target_link_libraries( size_test_all_optimized_ops executorch optimized_native_cpu_ops_lib) -if(CMAKE_BUILD_TYPE EQUAL "Release") +if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") target_link_options(size_test_all_optimized_ops PRIVATE "LINKER:--gc-sections") endif() endif() diff --git a/tools/cmake/preset/default.cmake b/tools/cmake/preset/default.cmake index 21f3cf53ae8..f2733f591eb 100644 --- a/tools/cmake/preset/default.cmake +++ b/tools/cmake/preset/default.cmake @@ -4,7 +4,7 @@ # This source code is licensed under the BSD-style license found in the # LICENSE file in the root directory of this source tree. -if(CMAKE_BUILD_TYPE STREQUAL "Release") +if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") set(_is_build_type_release ON) set(_is_build_type_debug OFF) else() From 8782fb2f44415d1ba7a7553168344f1da3b72c0a Mon Sep 17 00:00:00 2001 From: Scott Wolchok Date: Thu, 26 Jun 2025 23:28:30 -0700 Subject: [PATCH 7/8] suppress -Wmaybe-uninitialized in #12019 [ghstack-poisoned] --- test/build_size_test.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/build_size_test.sh b/test/build_size_test.sh index 8b7c37fb020..9dde5f676bf 100644 --- a/test/build_size_test.sh +++ b/test/build_size_test.sh @@ -15,7 +15,10 @@ EXTRA_BUILD_ARGS="${@:-}" # TODO(#8357): Remove -Wno-int-in-bool-context # TODO: Replace -ET_HAVE_PREAD=0 with a CMake option. # FileDataLoader used in the size_test breaks baremetal builds with pread when missing. -COMMON_CXXFLAGS="-fno-exceptions -fno-rtti -Wall -Werror -Wno-int-in-bool-context -DET_HAVE_PREAD=0" +# -Werror=maybe-uninitialized causes failures with GCC 9.5, which we +# -currently use in CI. There were known issues with this warning and +# -std::optional: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80635 +COMMON_CXXFLAGS="-fno-exceptions -fno-rtti -Wall -Werror -Wno-int-in-bool-context -Wno-maybe-uninitialized -DET_HAVE_PREAD=0" cmake_install_executorch_lib() { echo "Installing libexecutorch.a" From a3f0bf70fe39cf464edd02a10b04762f0db577e1 Mon Sep 17 00:00:00 2001 From: Scott Wolchok Date: Fri, 27 Jun 2025 17:21:15 -0700 Subject: [PATCH 8/8] Update [ghstack-poisoned] --- CMakeLists.txt | 6 +----- examples/models/llama/CMakeLists.txt | 7 +++---- examples/models/llava/CMakeLists.txt | 7 +++---- examples/selective_build/CMakeLists.txt | 18 +++++++++--------- test/CMakeLists.txt | 6 +++--- tools/cmake/Utils.cmake | 8 ++++++++ 6 files changed, 27 insertions(+), 25 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f01f1bebc86..8977e5c5aa9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -706,11 +706,7 @@ if(EXECUTORCH_BUILD_EXECUTOR_RUNNER) add_executable(executor_runner ${_executor_runner__srcs}) if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") - if(APPLE) - target_link_options(executor_runner PRIVATE "LINKER:-dead_strip") - else() - target_link_options(executor_runner PRIVATE "LINKER:--gc-sections") - endif() + target_link_options_gc_sections(executor_runner) endif() target_link_libraries(executor_runner ${_executor_runner_libs}) target_compile_options(executor_runner PUBLIC ${_common_compile_options}) diff --git a/examples/models/llama/CMakeLists.txt b/examples/models/llama/CMakeLists.txt index 20c946178d6..b850e4e6c0b 100644 --- a/examples/models/llama/CMakeLists.txt +++ b/examples/models/llama/CMakeLists.txt @@ -210,10 +210,9 @@ endif() add_executable(llama_main ${_srcs}) if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") - if(APPLE) - target_link_options(llama_main PRIVATE "LINKER:-dead_strip") - else() - target_link_options(llama_main PRIVATE "LINKER:--gc-sections,-s") + target_link_options_gc_sections(llama_main) + if(NOT APPLE) + target_link_options(llama_main PRIVATE "LINKER:-s") endif() endif() diff --git a/examples/models/llava/CMakeLists.txt b/examples/models/llava/CMakeLists.txt index be84cdb439b..b99d4c8106e 100644 --- a/examples/models/llava/CMakeLists.txt +++ b/examples/models/llava/CMakeLists.txt @@ -198,10 +198,9 @@ list(APPEND _common_include_directories ${stb_SOURCE_DIR} add_executable(llava_main ${_srcs}) if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") - if(APPLE) - target_link_options(llava_main PRIVATE "LINKER:-dead_strip,-s") - else() - target_link_options(llava_main PRIVATE "LINKER:--gc-sections,-s") + target_link_options_gc_sections(llama_main) + if(NOT APPLE) + target_link_options(llama_main PRIVATE "LINKER:-s") endif() endif() diff --git a/examples/selective_build/CMakeLists.txt b/examples/selective_build/CMakeLists.txt index 24fd102fee9..faa3cf568a6 100644 --- a/examples/selective_build/CMakeLists.txt +++ b/examples/selective_build/CMakeLists.txt @@ -123,10 +123,10 @@ gen_selected_ops( ) generate_bindings_for_kernels( - LIB_NAME - "select_build_lib" + LIB_NAME + "select_build_lib" FUNCTIONS_YAML - ${EXECUTORCH_ROOT}/kernels/portable/functions.yaml + ${EXECUTORCH_ROOT}/kernels/portable/functions.yaml CUSTOM_OPS_YAML "${_custom_ops_yaml}" DTYPE_SELECTIVE_BUILD @@ -134,11 +134,11 @@ generate_bindings_for_kernels( ) gen_operators_lib( - LIB_NAME - "select_build_lib" - KERNEL_LIBS - ${_kernel_lib} - DEPS + LIB_NAME + "select_build_lib" + KERNEL_LIBS + ${_kernel_lib} + DEPS executorch_core DTYPE_SELECTIVE_BUILD "${EXECUTORCH_DTYPE_SELECTIVE_BUILD}" @@ -152,7 +152,7 @@ list(TRANSFORM _executor_runner__srcs PREPEND "${EXECUTORCH_ROOT}/") # add_executable(selective_build_test ${_executor_runner__srcs}) if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") - target_link_options(selective_build_test PRIVATE "LINKER:--gc-sections") + target_link_options_gc_sections(selective_build_test) endif() target_link_libraries( selective_build_test PRIVATE executorch_core gflags select_build_lib diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 967610f48f8..3e8342a9741 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -53,7 +53,7 @@ list(TRANSFORM _size_test__srcs PREPEND "${EXECUTORCH_ROOT}/") add_executable(size_test ${_size_test__srcs}) target_link_libraries(size_test executorch) if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") - target_link_options(size_test PRIVATE "LINKER:--gc-sections") + target_link_options_gc_sections(size_test) endif() # @@ -65,7 +65,7 @@ target_link_libraries( size_test_all_ops executorch portable_ops_lib portable_kernels ) if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") - target_link_options(size_test_all_ops PRIVATE "LINKER:--gc-sections") + target_link_options_gc_sections(size_test_all_ops) endif() # @@ -77,6 +77,6 @@ target_link_options_shared_lib(optimized_native_cpu_ops_lib) target_link_libraries( size_test_all_optimized_ops executorch optimized_native_cpu_ops_lib) if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") - target_link_options(size_test_all_optimized_ops PRIVATE "LINKER:--gc-sections") + target_link_options_gc_sections(size_test_all_optimized_ops) endif() endif() diff --git a/tools/cmake/Utils.cmake b/tools/cmake/Utils.cmake index f0404f66bae..9fbab17728a 100644 --- a/tools/cmake/Utils.cmake +++ b/tools/cmake/Utils.cmake @@ -59,6 +59,14 @@ function(target_link_options_shared_lib target_name) endif() endfunction() +function(target_link_options_gc_sections target_name) + if(APPLE) + target_link_options(${target_name} PRIVATE "LINKER:-dead_strip") + else() + target_link_options(${target_name} PRIVATE "LINKER:--gc-sections") + endif() +endfunction() + # Extract source files based on toml config. This is useful to keep buck2 and # cmake aligned. Do not regenerate if file exists. function(extract_sources sources_file)