seed/update 5.7 KiB raw
1
#!/bin/sh
2
#
3
# Find the compiler fixed point and update the seed binary.
4
#
5
# Iterates self-compilation stages starting from the current seed until a fixed
6
# point is reached, then copies the result back to the seed.
7
#
8
# Usage: seed/update [--seed <path>] [--from-s0]
9
#
10
# Options:
11
#   --seed <path>            Use <path> as the seed binary instead of seed/radiance.rv64
12
#   --from-s0                Run the seed as a native binary (stage 0) instead of
13
#                            through the emulator
14
#
15
# Environment variables:
16
#   EMULATOR_MEMORY_SIZE     Emulator memory in KB (default: 393216)
17
#   EMULATOR_DATA_SIZE       Emulator data segment in KB (default: 375040)
18
#
19
set -eu
20
21
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
22
ROOT_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
23
24
cd "${ROOT_DIR}"
25
26
# ---------------------------------------------------------------------------
27
# Helpers
28
# ---------------------------------------------------------------------------
29
30
step() {
31
  printf "\n==> %s\n" "$*"
32
}
33
34
fail() {
35
  printf "!! %s\n" "$*" >&2
36
  exit 1
37
}
38
39
# ---------------------------------------------------------------------------
40
# Emulator settings
41
# ---------------------------------------------------------------------------
42
43
MEMORY_SIZE="${EMULATOR_MEMORY_SIZE:-393216}"
44
DATA_SIZE="${EMULATOR_DATA_SIZE:-375040}"
45
EMULATOR="${RAD_EMULATOR:-emulator} -debug -no-validate \
46
    -memory-size=${MEMORY_SIZE} -data-size=${DATA_SIZE} \
47
    -run"
48
49
# ---------------------------------------------------------------------------
50
# Compile one self-hosted stage.
51
#   stage <input> <output>
52
# ---------------------------------------------------------------------------
53
54
stage() {
55
  time -p $EMULATOR "$1" $OPTS -o "$2"
56
}
57
58
# ---------------------------------------------------------------------------
59
# Compile one stage using a native (s0) binary.
60
#   stageS0 <input> <output>
61
# ---------------------------------------------------------------------------
62
63
stageS0() {
64
  time -p "$1" compiler/radiance/radiance.rad $OPTS -o "$2"
65
}
66
67
# ---------------------------------------------------------------------------
68
# Compare two stage binaries. Returns 0 if identical, 1 if they differ.
69
# ---------------------------------------------------------------------------
70
71
compareStages() {
72
  if cmp -s "$1" "$2"; then
73
    printf " IDENTICAL\n  sha256: %s\n" "$(sha256sum "$2" | cut -d' ' -f1)"
74
    return 0
75
  else
76
    printf " DIFFERENT\n"
77
    return 1
78
  fi
79
}
80
81
# ---------------------------------------------------------------------------
82
# Copy the fixed-point binary to the seed.
83
# ---------------------------------------------------------------------------
84
85
updateSeed() {
86
  [ "$1" != "$ROOT_DIR/seed/radiance.rv64" ] && cp "$1" "$ROOT_DIR/seed/radiance.rv64"
87
88
  printf '%s\n' "$SOURCE_TREE" > "$ROOT_DIR/seed/radiance.rv64.git"
89
90
  printf "\n"
91
  printf "Fixed point reached at %s\n" "$2"
92
  printf "Seed updated (tree $SOURCE_TREE)\n"
93
}
94
95
# ---------------------------------------------------------------------------
96
# Parse arguments
97
# ---------------------------------------------------------------------------
98
99
SEED="$ROOT_DIR/seed/radiance.rv64"
100
FROM_S0=0
101
102
while [ $# -gt 0 ]; do
103
  case "$1" in
104
    --seed)
105
      [ $# -ge 2 ] || fail "--seed requires an argument"
106
      SEED="$(realpath "$2")"
107
      shift 2
108
      ;;
109
    --from-s0)
110
      FROM_S0=1
111
      shift
112
      ;;
113
    *)
114
      fail "unknown option: $1"
115
      ;;
116
  esac
117
done
118
119
command -v "${RAD_EMULATOR:-emulator}" >/dev/null 2>&1 \
120
  || fail "emulator not found: set RAD_EMULATOR or add 'emulator' to PATH"
121
[ -f "${SEED}" ] || fail "seed binary not found: ${SEED}"
122
123
# ---------------------------------------------------------------------------
124
# Compiler source snapshot
125
# ---------------------------------------------------------------------------
126
127
# Compile the complete tree of the current source commit.
128
git diff --quiet HEAD -- . ':!seed/radiance.rv64' \
129
    ':!seed/radiance.rv64.git' ||
130
    fail "commit source changes before updating the seed"
131
SOURCE_TREE="$(git rev-parse 'HEAD^{tree}')"
132
BUILD_DIR="$(mktemp -d)"
133
trap 'rm -rf "$BUILD_DIR"' EXIT
134
trap 'exit 1' HUP INT TERM
135
git archive "$SOURCE_TREE" > "$BUILD_DIR/source.tar"
136
mkdir "$BUILD_DIR/source"
137
tar -xf "$BUILD_DIR/source.tar" -C "$BUILD_DIR/source"
138
cd "$BUILD_DIR/source"
139
140
# ---------------------------------------------------------------------------
141
# Command line flags for Radiance compiler
142
# ---------------------------------------------------------------------------
143
144
OPTS="$(make -s compiler-options)"
145
146
# ---------------------------------------------------------------------------
147
# Stage 1: seed compiles source
148
# ---------------------------------------------------------------------------
149
150
S1="$ROOT_DIR/seed/radiance.rv64.s1"
151
S2="$ROOT_DIR/seed/radiance.rv64.s2"
152
S3="$ROOT_DIR/seed/radiance.rv64.s3"
153
S4="$ROOT_DIR/seed/radiance.rv64.s4"
154
155
step "Stage 1: S0 builds S1"
156
if [ "${FROM_S0}" -eq 1 ]; then
157
  stageS0 "${SEED}" "${S1}"
158
else
159
  stage "${SEED}" "${S1}"
160
fi
161
162
printf "\nComparing S0 and S1 ..."
163
if compareStages "${SEED}" "${S1}"; then
164
  updateSeed "${SEED}" "S0"
165
  exit 0
166
fi
167
168
# ---------------------------------------------------------------------------
169
# Stage 2: S1 compiles source
170
# ---------------------------------------------------------------------------
171
172
step "Stage 2: S1 builds S2"
173
stage "${S1}" "${S2}"
174
175
printf "\nComparing S1 and S2 ..."
176
if compareStages "${S1}" "${S2}"; then
177
  updateSeed "${S1}" "S1"
178
  exit 0
179
fi
180
181
# ---------------------------------------------------------------------------
182
# Stage 3: S2 compiles source
183
# ---------------------------------------------------------------------------
184
185
step "Stage 3: S2 builds S3"
186
stage "${S2}" "${S3}"
187
188
printf "\nComparing S2 and S3 ..."
189
if compareStages "${S2}" "${S3}"; then
190
  updateSeed "${S2}" "S2"
191
  exit 0
192
fi
193
194
fail "No fixed point reached after 3 stages"