Skip to content

Commit e9fb6c4

Browse files
authored
-frandom-seed: reduce value length (#8)
The current default `-frandom-seed="$*"` can run into "Argument list too long" errors (E2BIG). This happened in pytorch with 1300 object files on the command line. Fix that by filtering: 1. flags 2. object files 3. static/shared libraries 4. already parsed, separated paths from `-I <path>`, `-L <path>`, and others. What remains is hopefully a short string that contains at a minimum all source files. Signed-off-by: Harmen Stoppels <me@harmenstoppels.nl>
1 parent e47dd22 commit e9fb6c4

2 files changed

Lines changed: 72 additions & 14 deletions

File tree

cc.sh

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -366,16 +366,14 @@ if [ -z "$mode" ] || [ "$mode" = ld ]; then
366366
done
367367
fi
368368

369-
# Finish setting up the mode, and check whether -frandom-seed needs to be set.
370-
eval "_set_frandom_seed=\${SPACK_${comp}_HAS_FRANDOM_SEED:-}"
369+
# Finish setting up the mode.
371370
if [ -z "$mode" ]; then
372371
mode=ccld
373372
for arg in "$@"; do
374373
case $arg in
375-
-E) mode=cpp ;;
376-
-S) mode=as ;;
377-
-c) mode=cc ;;
378-
-frandom-seed=*) _set_frandom_seed= ;;
374+
-E) mode=cpp; break ;;
375+
-S) mode=as; break ;;
376+
-c) mode=cc; break ;;
379377
esac
380378
done
381379
fi
@@ -953,14 +951,27 @@ extend args_list libs_list "-l"
953951
full_command_list="$command"
954952
extend full_command_list args_list
955953

956-
if [ -n "$_set_frandom_seed" ]; then
957-
case "$mode" in
958-
cc|ccld)
959-
# Make GCC deterministic by setting the random seed to command line arguments
960-
append full_command_list "-frandom-seed=$input_command"
961-
;;
962-
esac
963-
fi
954+
case "$mode" in
955+
cc|ccld)
956+
eval "_frandom_seed_input=\${SPACK_${comp}_HAS_FRANDOM_SEED:-}"
957+
if [ -n "$_frandom_seed_input" ]; then
958+
_frandom_seed_input=""
959+
setsep other_args_list
960+
[ "$sep" != " " ] && IFS="$sep"
961+
for arg in $other_args_list; do
962+
case "$arg" in
963+
-frandom-seed=*) _frandom_seed_input=; break ;;
964+
-*|*.o|*.so|*.dylib|*.a) ;;
965+
*) _frandom_seed_input="${_frandom_seed_input}${arg}" ;;
966+
esac
967+
done
968+
unset IFS
969+
if [ -n "$_frandom_seed_input" ]; then
970+
append full_command_list "-frandom-seed=$_frandom_seed_input"
971+
fi
972+
fi
973+
;;
974+
esac
964975

965976
# prepend the ccache binary if we're using ccache
966977
if [ -n "$SPACK_CCACHE_BINARY" ]; then

test/run.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,51 @@ expected: $_expected_LR"
10111011
fi
10121012
}
10131013

1014+
# ---------------------------------------------------------------------------
1015+
# -frandom-seed
1016+
# ---------------------------------------------------------------------------
1017+
1018+
test_frandom_seed_not_added_without_env() {
1019+
wrapper_environment
1020+
_out=$(dump_args cc '-c
1021+
hello.c
1022+
-O2')
1023+
if printf '%s\n' "$_out" | grep -F -- '-frandom-seed=' >/dev/null; then
1024+
fail "frandom_seed_absent: -frandom-seed should not appear without SPACK_CC_HAS_FRANDOM_SEED"
1025+
fi
1026+
}
1027+
1028+
test_frandom_seed_filters_args() {
1029+
wrapper_environment
1030+
SPACK_CC_HAS_FRANDOM_SEED=1; export SPACK_CC_HAS_FRANDOM_SEED
1031+
1032+
# cc mode: -frandom-seed should contain only source files, concatenated.
1033+
# Includes space-separated path flags to verify their values do not leak.
1034+
_out=$(dump_args cc '-c
1035+
-O2
1036+
-I/some/include
1037+
-isystem
1038+
/some/sys
1039+
-L
1040+
/some/lib
1041+
hello.c
1042+
world.c
1043+
foo.o
1044+
bar.a
1045+
baz.so
1046+
quux.dylib')
1047+
expect_contains frandom_seed_value "$_out" '-frandom-seed=hello.cworld.c'
1048+
1049+
# User-supplied -frandom-seed suppresses auto-generated one
1050+
_out=$(dump_args cc '-c
1051+
-frandom-seed=custom
1052+
hello.c')
1053+
if printf '%s\n' "$_out" | grep -cF -- '-frandom-seed=' | grep -qv '^1$'; then
1054+
fail "frandom_seed_user_override: expected exactly one -frandom-seed"
1055+
fi
1056+
expect_contains frandom_seed_user_passthrough "$_out" '-frandom-seed=custom'
1057+
}
1058+
10141059
# ---------------------------------------------------------------------------
10151060
# Runner
10161061
# ---------------------------------------------------------------------------
@@ -1029,6 +1074,8 @@ test_disable_new_dtags
10291074
test_filter_enable_new_dtags
10301075
test_linker_strips_loopopt
10311076
test_spack_managed_dirs_are_prioritized
1077+
test_frandom_seed_not_added_without_env
1078+
test_frandom_seed_filters_args
10321079
'
10331080

10341081
if [ $# -gt 0 ]; then

0 commit comments

Comments
 (0)