From 1d1981b7b05f7883d23b4edcdc73868333bd318a Mon Sep 17 00:00:00 2001
From: Dion Dokter <diondokter@gmail.com>
Date: Sat, 11 May 2024 14:07:19 +0200
Subject: [PATCH 1/8] Add opt-for-size core lib feature flag

---
 library/core/Cargo.toml | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/library/core/Cargo.toml b/library/core/Cargo.toml
index a02fcf504168a..11d33971f2563 100644
--- a/library/core/Cargo.toml
+++ b/library/core/Cargo.toml
@@ -31,6 +31,8 @@ rand_xorshift = { version = "0.3.0", default-features = false }
 [features]
 # Make panics and failed asserts immediately abort without formatting any message
 panic_immediate_abort = []
+# Choose algorithms that are optimized for binary size instead of runtime performance
+optimize_for_size = []
 # Make `RefCell` store additional debugging information, which is printed out when
 # a borrow error occurs
 debug_refcell = []

From 579266a6be892d2b2e7410fe992308a9a8896193 Mon Sep 17 00:00:00 2001
From: Dion Dokter <diondokter@gmail.com>
Date: Sat, 11 May 2024 14:20:03 +0200
Subject: [PATCH 2/8] Add flag to std and alloc too

---
 library/alloc/Cargo.toml | 2 ++
 library/std/Cargo.toml   | 2 ++
 2 files changed, 4 insertions(+)

diff --git a/library/alloc/Cargo.toml b/library/alloc/Cargo.toml
index e8afed6b35a83..4548a27b420c0 100644
--- a/library/alloc/Cargo.toml
+++ b/library/alloc/Cargo.toml
@@ -38,3 +38,5 @@ compiler-builtins-mangled-names = ["compiler_builtins/mangled-names"]
 compiler-builtins-weak-intrinsics = ["compiler_builtins/weak-intrinsics"]
 # Make panics and failed asserts immediately abort without formatting any message
 panic_immediate_abort = []
+# Choose algorithms that are optimized for binary size instead of runtime performance
+optimize_for_size = []
diff --git a/library/std/Cargo.toml b/library/std/Cargo.toml
index 52729ba1f8456..81fd26692a517 100644
--- a/library/std/Cargo.toml
+++ b/library/std/Cargo.toml
@@ -81,6 +81,8 @@ system-llvm-libunwind = ["unwind/system-llvm-libunwind"]
 
 # Make panics and failed asserts immediately abort without formatting any message
 panic_immediate_abort = ["core/panic_immediate_abort", "alloc/panic_immediate_abort"]
+# Choose algorithms that are optimized for binary size instead of runtime performance
+optimize_for_size = ["core/optimize_for_size", "alloc/optimize_for_size"]
 
 # Enable std_detect default features for stdarch/crates/std_detect:
 # https://github.com/rust-lang/stdarch/blob/master/crates/std_detect/Cargo.toml

From 9ab0af820dddae34405252869597d3d853896ed0 Mon Sep 17 00:00:00 2001
From: Dion Dokter <diondokter@gmail.com>
Date: Sat, 11 May 2024 14:31:55 +0200
Subject: [PATCH 3/8] Add flag to sysroot

---
 library/sysroot/Cargo.toml | 1 +
 1 file changed, 1 insertion(+)

diff --git a/library/sysroot/Cargo.toml b/library/sysroot/Cargo.toml
index 6ff24a8db59c3..1ddacd92e6b94 100644
--- a/library/sysroot/Cargo.toml
+++ b/library/sysroot/Cargo.toml
@@ -22,6 +22,7 @@ llvm-libunwind = ["std/llvm-libunwind"]
 system-llvm-libunwind = ["std/system-llvm-libunwind"]
 panic-unwind = ["std/panic_unwind"]
 panic_immediate_abort = ["std/panic_immediate_abort"]
+optimize_for_size = ["std/optimize_for_size"]
 profiler = ["std/profiler"]
 std_detect_file_io = ["std/std_detect_file_io"]
 std_detect_dlsym_getauxval = ["std/std_detect_dlsym_getauxval"]

From b8b68983f392ece653ef1173a7f4d7cbdfe0a9ac Mon Sep 17 00:00:00 2001
From: Dion Dokter <diondokter@gmail.com>
Date: Mon, 13 May 2024 22:20:32 +0200
Subject: [PATCH 4/8] Forward alloc features to core

---
 library/alloc/Cargo.toml | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/library/alloc/Cargo.toml b/library/alloc/Cargo.toml
index 4548a27b420c0..2e7fcb9dbd35f 100644
--- a/library/alloc/Cargo.toml
+++ b/library/alloc/Cargo.toml
@@ -37,6 +37,6 @@ compiler-builtins-no-asm = ["compiler_builtins/no-asm"]
 compiler-builtins-mangled-names = ["compiler_builtins/mangled-names"]
 compiler-builtins-weak-intrinsics = ["compiler_builtins/weak-intrinsics"]
 # Make panics and failed asserts immediately abort without formatting any message
-panic_immediate_abort = []
+panic_immediate_abort = ["core/panic_immediate_abort"]
 # Choose algorithms that are optimized for binary size instead of runtime performance
-optimize_for_size = []
+optimize_for_size = ["core/optimize_for_size"]

From b964b0066833bc4deb8ebc3bae218a5298da82a9 Mon Sep 17 00:00:00 2001
From: Dion Dokter <dion@tweedegolf.com>
Date: Fri, 17 May 2024 11:47:18 +0200
Subject: [PATCH 5/8] Run tests with the flag enabled

---
 src/ci/docker/scripts/x86_64-gnu-llvm.sh | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/src/ci/docker/scripts/x86_64-gnu-llvm.sh b/src/ci/docker/scripts/x86_64-gnu-llvm.sh
index 2eb751ca3766d..1f4ba4986737d 100755
--- a/src/ci/docker/scripts/x86_64-gnu-llvm.sh
+++ b/src/ci/docker/scripts/x86_64-gnu-llvm.sh
@@ -6,10 +6,15 @@ set -ex
 if [[ -z "${PR_CI_JOB}" ]]; then
     # When running gcc backend tests, we need to install `libgccjit` and to not run llvm codegen
     # tests as it will fail them.
+    # The tests are run a second time with the size optimizations enabled.
     if [[ "${ENABLE_GCC_CODEGEN}" == "1" ]]; then
         ../x.py --stage 1 test --skip src/tools/tidy --skip tests/codegen
+        ../x.py --stage 1 test --skip src/tools/tidy --skip tests/codegen \
+            --rustc-args "--cfg feature=\"optimize_for_size\""
     else
         ../x.py --stage 1 test --skip src/tools/tidy
+        ../x.py --stage 1 test --skip src/tools/tidy --rustc-args \
+            "--cfg feature=\"optimize_for_size\""
     fi
 
     # Run the `mir-opt` tests again but this time for a 32-bit target.

From 5c301429bd5fe54351f2c88a6c9b4114abcb1880 Mon Sep 17 00:00:00 2001
From: Dion Dokter <dion@tweedegolf.com>
Date: Fri, 17 May 2024 12:02:21 +0200
Subject: [PATCH 6/8] Only test std, alloc and core

---
 src/ci/docker/scripts/x86_64-gnu-llvm.sh | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/ci/docker/scripts/x86_64-gnu-llvm.sh b/src/ci/docker/scripts/x86_64-gnu-llvm.sh
index 1f4ba4986737d..fe545e1d5f776 100755
--- a/src/ci/docker/scripts/x86_64-gnu-llvm.sh
+++ b/src/ci/docker/scripts/x86_64-gnu-llvm.sh
@@ -9,12 +9,12 @@ if [[ -z "${PR_CI_JOB}" ]]; then
     # The tests are run a second time with the size optimizations enabled.
     if [[ "${ENABLE_GCC_CODEGEN}" == "1" ]]; then
         ../x.py --stage 1 test --skip src/tools/tidy --skip tests/codegen
-        ../x.py --stage 1 test --skip src/tools/tidy --skip tests/codegen \
+        ../x.py --stage 1 test library/std library/alloc library/core \
             --rustc-args "--cfg feature=\"optimize_for_size\""
     else
         ../x.py --stage 1 test --skip src/tools/tidy
-        ../x.py --stage 1 test --skip src/tools/tidy --rustc-args \
-            "--cfg feature=\"optimize_for_size\""
+        ../x.py --stage 1 test library/std library/alloc library/core \
+            --rustc-args "--cfg feature=\"optimize_for_size\""
     fi
 
     # Run the `mir-opt` tests again but this time for a 32-bit target.

From 0d23a71ec2c79bc2da6b3fa8fa6386817e5856c5 Mon Sep 17 00:00:00 2001
From: Dion Dokter <diondokter@gmail.com>
Date: Mon, 20 May 2024 12:18:24 +0200
Subject: [PATCH 7/8] Deduplicate test command

---
 src/ci/docker/scripts/x86_64-gnu-llvm.sh | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/src/ci/docker/scripts/x86_64-gnu-llvm.sh b/src/ci/docker/scripts/x86_64-gnu-llvm.sh
index fe545e1d5f776..8f5f7fdbe47a6 100755
--- a/src/ci/docker/scripts/x86_64-gnu-llvm.sh
+++ b/src/ci/docker/scripts/x86_64-gnu-llvm.sh
@@ -6,17 +6,16 @@ set -ex
 if [[ -z "${PR_CI_JOB}" ]]; then
     # When running gcc backend tests, we need to install `libgccjit` and to not run llvm codegen
     # tests as it will fail them.
-    # The tests are run a second time with the size optimizations enabled.
     if [[ "${ENABLE_GCC_CODEGEN}" == "1" ]]; then
         ../x.py --stage 1 test --skip src/tools/tidy --skip tests/codegen
-        ../x.py --stage 1 test library/std library/alloc library/core \
-            --rustc-args "--cfg feature=\"optimize_for_size\""
     else
         ../x.py --stage 1 test --skip src/tools/tidy
-        ../x.py --stage 1 test library/std library/alloc library/core \
-            --rustc-args "--cfg feature=\"optimize_for_size\""
     fi
 
+    # The tests are run a second time with the size optimizations enabled.
+    ../x.py --stage 1 test library/std library/alloc library/core \
+        --rustc-args "--cfg feature=\"optimize_for_size\""
+
     # Run the `mir-opt` tests again but this time for a 32-bit target.
     # This enforces that tests using `// EMIT_MIR_FOR_EACH_BIT_WIDTH` have
     # both 32-bit and 64-bit outputs updated by the PR author, before

From bd71c71ea04b4a4f954e579c2a6d44113274846a Mon Sep 17 00:00:00 2001
From: Dion Dokter <diondokter@gmail.com>
Date: Mon, 20 May 2024 12:50:37 +0200
Subject: [PATCH 8/8] Move tests to more opportune point

---
 src/ci/docker/scripts/x86_64-gnu-llvm.sh | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/ci/docker/scripts/x86_64-gnu-llvm.sh b/src/ci/docker/scripts/x86_64-gnu-llvm.sh
index 8f5f7fdbe47a6..876b300d35c6b 100755
--- a/src/ci/docker/scripts/x86_64-gnu-llvm.sh
+++ b/src/ci/docker/scripts/x86_64-gnu-llvm.sh
@@ -12,10 +12,6 @@ if [[ -z "${PR_CI_JOB}" ]]; then
         ../x.py --stage 1 test --skip src/tools/tidy
     fi
 
-    # The tests are run a second time with the size optimizations enabled.
-    ../x.py --stage 1 test library/std library/alloc library/core \
-        --rustc-args "--cfg feature=\"optimize_for_size\""
-
     # Run the `mir-opt` tests again but this time for a 32-bit target.
     # This enforces that tests using `// EMIT_MIR_FOR_EACH_BIT_WIDTH` have
     # both 32-bit and 64-bit outputs updated by the PR author, before
@@ -27,6 +23,10 @@ if [[ -z "${PR_CI_JOB}" ]]; then
     # Run `ui-fulldeps` in `--stage=1`, which actually uses the stage0
     # compiler, and is sensitive to the addition of new flags.
     ../x.py --stage 1 test tests/ui-fulldeps
+
+    # The tests are run a second time with the size optimizations enabled.
+    ../x.py --stage 1 test library/std library/alloc library/core \
+        --rustc-args "--cfg feature=\"optimize_for_size\""
 fi
 
 # When running gcc backend tests, we need to install `libgccjit` and to not run llvm codegen