Skip to content

Commit 5076af2

Browse files
authored
Add a codesize check of the freestanding/minimal stdlib to prevent large codesize regressions (#40653)
1 parent 2653b5b commit 5076af2

File tree

7 files changed

+102
-2
lines changed

7 files changed

+102
-2
lines changed

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ function(get_test_dependencies SDK result_var_name)
9494
llvm-profdata
9595
llvm-readelf
9696
llvm-readobj
97+
llvm-size
9798
llvm-strings
9899
not
99100
split-file)

test/lit.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ config.llvm_dwarfdump = inferSwiftBinary('llvm-dwarfdump')
344344
config.llvm_readelf = inferSwiftBinary('llvm-readelf')
345345
config.llvm_dis = inferSwiftBinary('llvm-dis')
346346
config.llvm_nm = inferSwiftBinary('llvm-nm')
347+
config.llvm_size = inferSwiftBinary('llvm-size')
347348
config.sourcekitd_test = inferSwiftBinary('sourcekitd-test')
348349
config.complete_test = inferSwiftBinary('complete-test')
349350
config.swift_api_digester = inferSwiftBinary('swift-api-digester')
@@ -516,6 +517,7 @@ config.substitutions.append( ('%llvm-dwarfdump', config.llvm_dwarfdump) )
516517
config.substitutions.append( ('%llvm-readelf', config.llvm_readelf) )
517518
config.substitutions.append( ('%llvm-dis', config.llvm_dis) )
518519
config.substitutions.append( ('%llvm-nm', config.llvm_nm) )
520+
config.substitutions.append( ('%llvm-size', config.llvm_size) )
519521
config.substitutions.append( ('%swift-demangle-yamldump', config.swift_demangle_yamldump) )
520522
config.substitutions.append( ('%swift-demangle', config.swift_demangle) )
521523
config.substitutions.append( ('%Benchmark_O', config.benchmark_o) )
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Check that we can LTO-optimize the freestanding/minimal stdlib against client usage of APIs, and that we don't produce unexpectedly large final binary.
2+
3+
// Important (!): This test is in test/stdlib/ to make sure it is actually run on the minimal/freestanding CI job, which
4+
// filters the set of tests to test/stdlib/ only (see build-preset.ini).
5+
6+
// REQUIRES: freestanding
7+
// REQUIRES: executable_test
8+
9+
// RUN: %empty-directory(%t)
10+
// RUN: %target-build-swift %s -o %t/a.out
11+
// RUN: %{python} %utils/check_freestanding_size.py --path %t/a.out --triple %module-target-triple --size-path %llvm-size
12+
13+
print("Hello world")
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Check that we can LTO-optimize the freestanding/minimal stdlib against client usage of APIs, and that we don't produce unexpectedly large final binary.
2+
3+
// Important (!): This test is in test/stdlib/ to make sure it is actually run on the minimal/freestanding CI job, which
4+
// filters the set of tests to test/stdlib/ only (see build-preset.ini).
5+
6+
// REQUIRES: freestanding
7+
// REQUIRES: executable_test
8+
9+
// RUN: %empty-directory(%t)
10+
// RUN: %target-build-swift %s -o %t/a.out
11+
// RUN: %{python} %utils/check_freestanding_size.py --path %t/a.out --triple %module-target-triple --size-path %llvm-size
12+
13+
let array = [1, 2, 3]
14+
let dict = ["abc": 42]
15+
let s = Set([1, 3, 5])
16+
print("Hello \(array) \(dict) \(s)")

utils/build-script-impl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,14 +1769,15 @@ for host in "${ALL_HOSTS[@]}"; do
17691769
# build of Swift depend on these for building and testing.
17701770
build_targets=(llvm-tblgen clang-resource-headers intrinsics_gen clang-tablegen-targets)
17711771
# If we are not performing a toolchain only build, then we
1772-
# also want to include FileCheck, not, llvm-nm for testing
1773-
# purposes.
1772+
# also want to include FileCheck, not, llvm-nm, and similar
1773+
# for testing purposes.
17741774
if [[ ! "${BUILD_TOOLCHAIN_ONLY}" ]] ; then
17751775
build_targets=(
17761776
"${build_targets[@]}"
17771777
FileCheck
17781778
not
17791779
llvm-nm
1780+
llvm-size
17801781
)
17811782
fi
17821783
fi

utils/check_freestanding_size.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env python
2+
#
3+
# This source file is part of the Swift.org open source project
4+
#
5+
# Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
# Licensed under Apache License v2.0 with Runtime Library Exception
7+
#
8+
# See https://swift.org/LICENSE.txt for license information
9+
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
11+
import argparse
12+
import subprocess
13+
import sys
14+
15+
parser = argparse.ArgumentParser()
16+
parser.add_argument("--path", help="path to a binary to check")
17+
parser.add_argument("--triple", help="target triple of the freestanding stdlib")
18+
parser.add_argument("--size-path", help="path to llvm-size binary to use")
19+
args = parser.parse_args()
20+
21+
22+
################################################################################
23+
#
24+
# EXPECTED/ALLOWED TEXT SEGMENT SIZE
25+
#
26+
# Before bumping the maximum text segment size please consult with:
27+
# @kubamracek, @compnerd
28+
#
29+
# The 'freestanding' build of the Swift runtime and standard library is
30+
# intended to be lean and allow dead-code elimination from the standard library
31+
# based on client code usage.
32+
#
33+
################################################################################
34+
35+
# As of 2022-01-05: TEXT segment is 640 KiB on x86_64 Darwin, stdlib without
36+
# asserts. Let's allow for some minor increases (~ 15%) and cap at 740 kiB:
37+
38+
max_text_size_x86_64_apple_macos = 740 * 1024
39+
40+
################################################################################
41+
42+
43+
if args.triple == "x86_64-apple-macos":
44+
max_text_size = max_text_size_x86_64_apple_macos
45+
actual_text_size = 0
46+
47+
nm = args.size_path
48+
lines = subprocess.check_output(
49+
[nm, "--format=darwin", args.path]) \
50+
.decode("utf-8").strip().splitlines()
51+
for line in lines:
52+
if line.startswith("Segment __TEXT: "):
53+
actual_text_size = int(line[len("Segment __TEXT: "):])
54+
55+
if actual_text_size == 0:
56+
print("cannot determine TEXT segment size")
57+
sys.exit(1)
58+
59+
else:
60+
print("triple {} not handled yet".format(args.triple))
61+
sys.exit(1)
62+
63+
print("max_text_size: %d" % max_text_size)
64+
print("actual_text_size: %d" % actual_text_size)
65+
fail = actual_text_size > max_text_size
66+
67+
sys.exit(1 if fail else 0)

0 commit comments

Comments
 (0)