diff --git a/library/alloc/Cargo.toml b/library/alloc/Cargo.toml
index e8afed6b35a83..2e7fcb9dbd35f 100644
--- a/library/alloc/Cargo.toml
+++ b/library/alloc/Cargo.toml
@@ -37,4 +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 = ["core/optimize_for_size"]
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 = []
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
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"]
diff --git a/src/ci/docker/scripts/x86_64-gnu-llvm.sh b/src/ci/docker/scripts/x86_64-gnu-llvm.sh
index 2eb751ca3766d..876b300d35c6b 100755
--- a/src/ci/docker/scripts/x86_64-gnu-llvm.sh
+++ b/src/ci/docker/scripts/x86_64-gnu-llvm.sh
@@ -23,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