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> (krank:ignore-line),
18# as these are not required to bootstrap GHC and at worst will produce an
19# error message.
20#
21# It is called `subopt` to reflect the fact that it uses `opt(1)` as a
22# subprocess, and the fact that the GHC build system situation
23# requiring this hack is suboptimal.
24
25set -e
26
27expect() {
28 if [[ $1 != $2 ]]; then
29 printf >&2 'subopt: got %q; expected %q\n' "$1" "$2"
30 return 2
31 fi
32}
33
34if [[ $NIX_DEBUG -ge 1 ]]; then
35 printf >&2 'subopt: before:'
36 printf >&2 ' %q' "$@"
37 printf >&2 '\n'
38fi
39
40args=()
41
42while [[ $# -gt 0 ]]; do
43 case "$1" in
44 -enable-new-pm=0)
45 shift 1
46 ;;
47 -mem2reg)
48 expect "$2" -globalopt
49 expect "$3" -lower-expect
50 expect "$4" -enable-tbaa
51 expect "$5" -tbaa
52 args+=('-passes=function(require<tbaa>),function(mem2reg),globalopt,function(lower-expect)')
53 shift 5
54 ;;
55 -O1)
56 expect "$2" -globalopt
57 expect "$3" -enable-tbaa
58 expect "$4" -tbaa
59 args+=('-passes=default<O1>')
60 shift 4
61 ;;
62 -O2)
63 expect "$2" -enable-tbaa
64 expect "$3" -tbaa
65 args+=('-passes=default<O2>')
66 shift 3
67 ;;
68 *)
69 args+=("$1")
70 shift 1
71 ;;
72 esac
73done
74
75if [[ $NIX_DEBUG -ge 1 ]]; then
76 printf >&2 'subopt: after:'
77 printf >&2 ' %q' "${args[@]}"
78 printf >&2 '\n'
79fi
80
81exec @opt@ "${args[@]}"