···11+#!@shell@
22+33+# This script wraps the LLVM `opt(1)` executable and maps the options
44+# passed by old versions of GHC to the equivalents passed by newer
55+# versions that support recent versions of LLVM.
66+#
77+# It achieves the same effect as the following GHC change externally:
88+# <https://gitlab.haskell.org/ghc/ghc/-/merge_requests/8999>.
99+#
1010+# This is used solely for bootstrapping newer GHCs from the GHC 9.0.2
1111+# binary on AArch64, as that is the only architecture supported by that
1212+# binary distribution that requires LLVM, and our later binary packages
1313+# all use the native code generator for all supported platforms.
1414+#
1515+# No attempt is made to support custom LLVM optimization flags, or the
1616+# undocumented flag to disable TBAA, or avoid
1717+# <https://gitlab.haskell.org/ghc/ghc/-/issues/23870>, as these are not
1818+# required to bootstrap GHC and at worst will produce an error message.
1919+#
2020+# It is called `subopt` to reflect the fact that it uses `opt(1)` as a
2121+# subprocess, and the fact that the GHC build system situation
2222+# requiring this hack is suboptimal.
2323+2424+set -e
2525+2626+expect() {
2727+ if [[ $1 != $2 ]]; then
2828+ printf >&2 'subopt: got %q; expected %q\n' "$1" "$2"
2929+ return 2
3030+ fi
3131+}
3232+3333+if [[ $NIX_DEBUG -ge 1 ]]; then
3434+ printf >&2 'subopt: before:'
3535+ printf >&2 ' %q' "$@"
3636+ printf >&2 '\n'
3737+fi
3838+3939+args=()
4040+4141+while [[ $# -gt 0 ]]; do
4242+ case "$1" in
4343+ -enable-new-pm=0)
4444+ shift 1
4545+ ;;
4646+ -mem2reg)
4747+ expect "$2" -globalopt
4848+ expect "$3" -lower-expect
4949+ expect "$4" -enable-tbaa
5050+ expect "$5" -tbaa
5151+ args+=('-passes=function(require<tbaa>),function(mem2reg),globalopt,function(lower-expect)')
5252+ shift 5
5353+ ;;
5454+ -O1)
5555+ expect "$2" -globalopt
5656+ expect "$3" -enable-tbaa
5757+ expect "$4" -tbaa
5858+ args+=('-passes=default<O1>')
5959+ shift 4
6060+ ;;
6161+ -O2)
6262+ expect "$2" -enable-tbaa
6363+ expect "$3" -tbaa
6464+ args+=('-passes=default<O2>')
6565+ shift 3
6666+ ;;
6767+ *)
6868+ args+=("$1")
6969+ shift 1
7070+ ;;
7171+ esac
7272+done
7373+7474+if [[ $NIX_DEBUG -ge 1 ]]; then
7575+ printf >&2 'subopt: after:'
7676+ printf >&2 ' %q' "${args[@]}"
7777+ printf >&2 '\n'
7878+fi
7979+8080+exec @opt@ "${args[@]}"