···1+#!@shell@
2+3+# This script wraps the LLVM `opt(1)` executable and maps the options
4+# passed by old versions of GHC to the equivalents passed by newer
5+# versions that support recent versions of LLVM.
6+#
7+# It achieves the same effect as the following GHC change externally:
8+# <https://gitlab.haskell.org/ghc/ghc/-/merge_requests/8999>.
9+#
10+# This is used solely for bootstrapping newer GHCs from the GHC 9.0.2
11+# binary on AArch64, as that is the only architecture supported by that
12+# binary distribution that requires LLVM, and our later binary packages
13+# all use the native code generator for all supported platforms.
14+#
15+# No attempt is made to support custom LLVM optimization flags, or the
16+# undocumented flag to disable TBAA, or avoid
17+# <https://gitlab.haskell.org/ghc/ghc/-/issues/23870>, as these are not
18+# required to bootstrap GHC and at worst will produce an error message.
19+#
20+# It is called `subopt` to reflect the fact that it uses `opt(1)` as a
21+# subprocess, and the fact that the GHC build system situation
22+# requiring this hack is suboptimal.
23+24+set -e
25+26+expect() {
27+ if [[ $1 != $2 ]]; then
28+ printf >&2 'subopt: got %q; expected %q\n' "$1" "$2"
29+ return 2
30+ fi
31+}
32+33+if [[ $NIX_DEBUG -ge 1 ]]; then
34+ printf >&2 'subopt: before:'
35+ printf >&2 ' %q' "$@"
36+ printf >&2 '\n'
37+fi
38+39+args=()
40+41+while [[ $# -gt 0 ]]; do
42+ case "$1" in
43+ -enable-new-pm=0)
44+ shift 1
45+ ;;
46+ -mem2reg)
47+ expect "$2" -globalopt
48+ expect "$3" -lower-expect
49+ expect "$4" -enable-tbaa
50+ expect "$5" -tbaa
51+ args+=('-passes=function(require<tbaa>),function(mem2reg),globalopt,function(lower-expect)')
52+ shift 5
53+ ;;
54+ -O1)
55+ expect "$2" -globalopt
56+ expect "$3" -enable-tbaa
57+ expect "$4" -tbaa
58+ args+=('-passes=default<O1>')
59+ shift 4
60+ ;;
61+ -O2)
62+ expect "$2" -enable-tbaa
63+ expect "$3" -tbaa
64+ args+=('-passes=default<O2>')
65+ shift 3
66+ ;;
67+ *)
68+ args+=("$1")
69+ shift 1
70+ ;;
71+ esac
72+done
73+74+if [[ $NIX_DEBUG -ge 1 ]]; then
75+ printf >&2 'subopt: after:'
76+ printf >&2 ' %q' "${args[@]}"
77+ printf >&2 '\n'
78+fi
79+80+exec @opt@ "${args[@]}"