Repository navigation
build: keep the NDK's default compile flags - #2136
Draft
edusperoni wants to merge 2 commits into
Draft
edusperoni wants to merge 2 commits into
edusperoni wants to merge 2 commits into
Conversation
CMakeLists.txt assigned CMAKE_CXX_FLAGS outright in both the release and Debug branches, discarding everything the NDK toolchain had put there: -DANDROID, -fdata-sections, -funwind-tables, -Wformat -Werror=format-security, the hardening defaults and, on armeabi-v7a, -march=armv7-a -mthumb. The runtime's v7 code was therefore built as A32 while V8's v7 archive is Thumb-2. Append to CMAKE_CXX_FLAGS instead so the toolchain defaults survive, and drop -fno-data-sections, which had no stated reason. The NDK's -fstack-protector-strong and -D_FORTIFY_SOURCE=2 are switched back off right after the defaults, so codegen stays as fast as before. -mstackrealign is no longer passed by us for any ABI: it only works around an x86 stack-alignment bug below API 24, and the NDK toolchain adds it itself for exactly that case. Optimized links also get -Wl,-O2 (string tail merging).
With the NDK defaults no longer overwritten, armeabi-v7a picks up the toolchain's -mthumb and the runtime's own code turns into Thumb-2. That shrank the stripped v7 library by 480 KB (-140 KB gzipped, runtime code -33%), but on a Samsung SM-G780G (API 33, optimized release, 32-bit process) it made the JNI bridge consistently slower than the A32 build we have shipped so far: static.noop() 175 -> 210 ns (+19.5%) static.add(int,int) +14.4% instance.getInt() +10.2% instance.echo(String) +10.9% field.get int +12.8% geomean, 63 rows +4-11% Pass -DANDROID_ARM_MODE=arm so the toolchain leaves -mthumb out (clang's armv7 default is A32) while every other NDK default still applies. V8's prebuilt archive is unaffected and stays Thumb-2.
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Current behavior.
test-app/runtime/CMakeLists.txtassignsCMAKE_CXX_FLAGSoutright in both the release branch (set(CMAKE_CXX_FLAGS "${COMMON_CMAKE_ARGUMENTS} -O3 ...")) and the Debug branch. That throws away every flag the NDK toolchain had put there. AGP'scompile_commands.jsonconfirms that every runtime compile unit is built without:-DANDROID,-fdata-sections,-funwind-tables-Wformat -Werror=format-security-fstack-protector-strong,-D_FORTIFY_SOURCE=2-march=armv7-a -mthumb.Because
-mthumbwas dropped too, the runtime's armeabi-v7a code has been A32 (ARM) all along, while the NDK default would be Thumb-2. V8's prebuilt v7 archive is Thumb-2.-fno-data-sections(added in 2017 with no stated reason) and-mstackrealign(added for an x86 API 19 crash) are also passed for every ABI.New behavior.
CMAKE_CXX_FLAGS(string(APPEND ...)), so the NDK defaults survive.-fno-data-sectionsis dropped.-fno-stack-protector -U_FORTIFY_SOURCEare appended after them. Their checks only cost code size (+65 KB.textmeasured on arm64) and time on the runtime's hot paths, so this keeps codegen as fast as today. None of the runtime's objects reference__stack_chk_failor any__*_chkfunction (llvm-nm -u, all four ABIs). The runtime has no C or assembly sources, so the C++ flags cover everything.-mstackrealignourselves. It only works around an x86 stack-alignment bug below API 24, and the NDK toolchain adds it for exactly that case (x86 and minSdk < 24). x86 still gets it today through the NDK (checked incompile_commands.json). arm64-v8a, armeabi-v7a and x86_64 lose it.-Wl,-O2, which turns on lld's string tail merging.-DANDROID_ARM_MODE=armis passed to CMake (test-app/runtime/build.gradle), so the toolchain leaves out the-mthumbit would otherwise add. Every other NDK default still applies. NDK r29 does not add-marmfor this mode; clang's armv7 default is A32. See ARM vs Thumb-2 below for why.-Wformat -Werror=format-securityare now on. The Debug variant (inspector and Node-API test addons included) and the optimized variant both build warning-free on all four ABIs, so no source changes were needed.Mapping symbols confirm that the armeabi-v7a runtime objects are all A32:
$a=4585,$t=0. With the Thumb default they were$t=4530,$a=0. arm64-v8a flags are unaffected by the ARM mode.ARM vs Thumb-2 on armeabi-v7a
Taking the NDK's Thumb-2 default would shrink the stripped v7 library by 480 KB (−140 KB gzipped; the runtime's own
.textdrops by a third). On device, though, it made the JNI bridge consistently slower than the A32 code we ship today. Measured on a Samsung SM-G780G (Android 13), optimized release APKs installed as a 32-bit process (--abi armeabi-v7a),debug.ns.jnibench, 2 interleaved blocks × 2 runs per build. Each value is the median of the 4 per-run medians:static.noop()static.add(int,int)instance.getInt()instance.echo(String)instance.getString()field.get intfield.get ObjectSo the runtime's v7 code stays A32. Codegen mode is the same as today.
Confirmation run with this branch's final build (
-DANDROID_ARM_MODE=arm). It used the same phone and harness, filtered to the 26static.*/instance.*/field.*rows, and ran the three builds interleaved in 2 blocks, so each build got 2 runs. Values are run 1 / run 2. Δ is the median of the per-block deltas.static.noop()static.add(int,int)instance.getInt()instance.echo(String)instance.getString()instance.takeObject(Object)field.get intfield.get ObjectThis branch's A32 build matches
main: the geomean is within ±1% in both blocks. The field rows are mixed.field.get intandfield.get Objectare 5–7% slower in both blocks, whilefield.set intis 4% faster andfield.get Stringis unchanged. With n=2, that is not resolved. Thumb-2 is slower on every row either way.The phone switches between two clock states between sessions, so absolute values only compare within one table. For example,
static.noop()onmainis 175 ns in the first table and 151 ns here.Size (optimized release,
-PoptimizedRelWithDebInfo,llvm-strip --strip-all, NDK r29).so.text.textOn x86_64, the stack-realigning prologues in the
.so(and $-N, %rsp) drop from 3,760 to 1,420; the rest come from V8. x86 runtime code is unchanged because the NDK still adds-mstackrealignthere at minSdk 21.Performance on arm64
Same phone, same harness, arm64 process: neutral. The stable rows stay within ±1–3% of
main.dlopen(libNativeScript.so, RTLD_NOW)of the stripped library, median of 20 interleaved fresh processes, is unchanged: arm64 58.24 → 58.28 ms. In a 32-bit process it was 61.16 → 61.13 ms, measured with the Thumb-2 variant of this branch.Does your commit message include the wording below to reference a specific issue in this repo?
No.
Related Pull Requests
main. Once both land, x86 loses-mstackrealignas well, because the NDK only adds it below API 24.Does your pull request have unit tests?
No new tests: this is a build-flag change. It was verified as follows:
./gradlew runtestsAndVerifyResults -Pabis=arm64-v8a) on an arm64 API 36 emulator: 1773 specs, 0 failures.compile_commands.json: the NDK defaults come first, followed by-fno-stack-protector -U_FORTIFY_SOURCE, then-O3.