From b234e7819f3eb4893eed5c40b2d0478a1c94f971 Mon Sep 17 00:00:00 2001
From: Olivier Benz <olivier.benz@b-data.ch>
Date: Thu, 23 May 2024 13:25:24 +0200
Subject: [PATCH] Rebuild GHC version 9.8.2

- Update base image to alpine:3.20
- Patch to force LLVM v16 backend
---
 VERSION_MATRIX.md      |  2 +-
 patches/9.8.2.patch    | 72 ++++++++++++++++++++++++++++++++++++++++++
 prior/9.8.2.Dockerfile | 11 +++++--
 3 files changed, 81 insertions(+), 4 deletions(-)
 create mode 100644 patches/9.8.2.patch

diff --git a/VERSION_MATRIX.md b/VERSION_MATRIX.md
index 9ebe0c2..9f6331f 100644
--- a/VERSION_MATRIX.md
+++ b/VERSION_MATRIX.md
@@ -4,7 +4,7 @@ Topmost entry = Tag `latest`
 
 | GHC        | Cabal    | Stack      | LLVM | Linux distro |
 |:-----------|:---------|:-----------|:---- |:-------------|
-| 9.8.2      | 3.10.3.0 | 2.15.7     | 14   | Alpine 3.19  |
+| 9.8.2      | 3.10.3.0 | 2.15.7     | 16   | Alpine 3.20  |
 | 9.8.1      | 3.10.2.1 | 2.15.1     | 14   | Alpine 3.19  |
 | 9.6.5      | 3.10.3.0 | 2.15.7     | 16   | Alpine 3.20  |
 | 9.6.4      | 3.10.3.0 | 2.15.5     | 14   | Alpine 3.19  |
diff --git a/patches/9.8.2.patch b/patches/9.8.2.patch
new file mode 100644
index 0000000..f940e0c
--- /dev/null
+++ b/patches/9.8.2.patch
@@ -0,0 +1,72 @@
+--- compiler/GHC/Driver/DynFlags.hs
++++ compiler/GHC/Driver/DynFlags.hs.patched
+@@ -1248,7 +1248,6 @@
+ optLevelFlags -- see Note [Documenting optimisation flags]
+   = [ ([0,1,2], Opt_DoLambdaEtaExpansion)
+     , ([0,1,2], Opt_DoEtaReduction)       -- See Note [Eta-reduction in -O0]
+-    , ([0,1,2], Opt_LlvmTBAA)
+     , ([0,1,2], Opt_ProfManualCcs )
+     , ([2], Opt_DictsStrict)
+
+--- compiler/GHC/Driver/Flags.hs
++++ compiler/GHC/Driver/Flags.hs.patched
+@@ -285,7 +285,6 @@
+    | Opt_RegsGraph                      -- do graph coloring register allocation
+    | Opt_RegsIterative                  -- do iterative coalescing graph coloring register allocation
+    | Opt_PedanticBottoms                -- Be picky about how we treat bottom
+-   | Opt_LlvmTBAA                       -- Use LLVM TBAA infrastructure for improving AA (hidden flag)
+    | Opt_LlvmFillUndefWithGarbage       -- Testing for undef bugs (hidden flag)
+    | Opt_IrrefutableTuples
+    | Opt_CmmSink
+@@ -525,7 +524,6 @@
+    , Opt_EnableRewriteRules
+    , Opt_RegsGraph
+    , Opt_RegsIterative
+-   , Opt_LlvmTBAA
+    , Opt_IrrefutableTuples
+    , Opt_CmmSink
+    , Opt_CmmElimCommonBlocks
+--- compiler/GHC/Driver/Pipeline/Execute.hs
++++ compiler/GHC/Driver/Pipeline/Execute.hs.patched
+@@ -968,8 +968,7 @@
+             -> DynFlags
+             -> [(String, String)]  -- ^ pairs of (opt, llc) arguments
+ llvmOptions llvm_config dflags =
+-       [("-enable-tbaa -tbaa",  "-enable-tbaa") | gopt Opt_LlvmTBAA dflags ]
+-    ++ [("-relocation-model=" ++ rmodel
++       [("-relocation-model=" ++ rmodel
+         ,"-relocation-model=" ++ rmodel) | not (null rmodel)]
+     ++ [("-stack-alignment=" ++ (show align)
+         ,"-stack-alignment=" ++ (show align)) | align > 0 ]
+--- compiler/GHC/Driver/Session.hs
++++ compiler/GHC/Driver/Session.hs.patched
+@@ -2400,7 +2400,6 @@
+   flagSpec "late-dmd-anal"                    Opt_LateDmdAnal,
+   flagSpec "late-specialise"                  Opt_LateSpecialise,
+   flagSpec "liberate-case"                    Opt_LiberateCase,
+-  flagHiddenSpec "llvm-tbaa"                  Opt_LlvmTBAA,
+   flagHiddenSpec "llvm-fill-undef-with-garbage" Opt_LlvmFillUndefWithGarbage,
+   flagSpec "loopification"                    Opt_Loopification,
+   flagSpec "block-layout-cfg"                 Opt_CfgBlocklayout,
+--- configure.ac
++++ configure.ac.patched
+@@ -544,7 +544,7 @@
+ # versions of LLVM simultaneously, but that stopped working around
+ # 3.5/3.6 release of LLVM.
+ LlvmMinVersion=11  # inclusive
+-LlvmMaxVersion=16 # not inclusive
++LlvmMaxVersion=17 # not inclusive
+ AC_SUBST([LlvmMinVersion])
+ AC_SUBST([LlvmMaxVersion])
+ sUPPORTED_LLVM_VERSION_MIN=$(echo \($LlvmMinVersion\) | sed 's/\./,/')
+--- llvm-passes
++++ llvm-passes.patched
+@@ -1,5 +1,5 @@
+ [
+-(0, "-enable-new-pm=0 -mem2reg -globalopt -lower-expect"),
+-(1, "-enable-new-pm=0 -O1 -globalopt"),
+-(2, "-enable-new-pm=0 -O2")
++(0, "-passes=function(require<tbaa>),function(mem2reg),globalopt,function(lower-expect)"),
++(1, "-passes=default<O1>"),
++(2, "-passes=default<O2>")
+ ]
diff --git a/prior/9.8.2.Dockerfile b/prior/9.8.2.Dockerfile
index 59e2bc9..a674d80 100644
--- a/prior/9.8.2.Dockerfile
+++ b/prior/9.8.2.Dockerfile
@@ -19,7 +19,7 @@ RUN apk upgrade --no-cache \
     gnupg \
     linux-headers \
     libffi-dev \
-    llvm14 \
+    llvm16 \
     ncurses-dev \
     perl \
     python3 \
@@ -32,6 +32,8 @@ ARG GHC_VERSION_BUILD
 
 ENV GHC_VERSION=${GHC_VERSION_BUILD}
 
+COPY patches/${GHC_VERSION}.patch /tmp/
+
 RUN cd /tmp \
   && curl -sSLO https://downloads.haskell.org/~ghc/"$GHC_VERSION"/ghc-"$GHC_VERSION"-src.tar.xz \
   && curl -sSLO https://downloads.haskell.org/~ghc/"$GHC_VERSION"/ghc-"$GHC_VERSION"-src.tar.xz.sig \
@@ -42,6 +44,9 @@ RUN cd /tmp \
   && gpg --verify "ghc-$GHC_VERSION-src.tar.xz.sig" "ghc-$GHC_VERSION-src.tar.xz" \
   && tar -xJf "ghc-$GHC_VERSION-src.tar.xz" \
   && cd "ghc-$GHC_VERSION" \
+  # Apply patches
+  && mv "/tmp/$GHC_VERSION.patch" . \
+  && patch -p0 <"$GHC_VERSION.patch" \
   && ./boot.source \
   && ./configure --disable-ld-override LD=ld.gold \
   ## Use the LLVM backend
@@ -67,7 +72,7 @@ RUN cabal update \
   ## See https://gitlab.haskell.org/ghc/ghc/-/wikis/commentary/libraries/version-history
   && cabal install "cabal-install-$CABAL_VERSION"
 
-FROM alpine:3.19 as ghc-base
+FROM alpine:3.20 as ghc-base
 
 LABEL org.opencontainers.image.licenses="MIT" \
       org.opencontainers.image.source="https://gitlab.b-data.ch/ghc/ghc-musl" \
@@ -97,7 +102,7 @@ RUN apk add --no-cache \
     libcurl \
     libffi \
     libffi-dev \
-    llvm14 \
+    llvm16 \
     ncurses-dev \
     ncurses-static \
     openssl-dev \
-- 
GitLab