[Git][ghc/ghc][wip/9.6.6-backports] 2 commits: GHCi interpreter: Tag constructor closures when possible.

Zubin (@wz1000) gitlab at gitlab.haskell.org
Sun Jun 30 22:30:35 UTC 2024



Zubin pushed to branch wip/9.6.6-backports at Glasgow Haskell Compiler / GHC


Commits:
51b77722 by Andreas Klebinger at 2024-07-01T04:00:07+05:30
GHCi interpreter: Tag constructor closures when possible.

When evaluating PUSH_G try to tag the reference we are pushing if it's a
constructor. This is potentially helpful for performance and required to
fix #24870.

(cherry picked from commit 1bfa91115b8320ed99a5e946147528e21ca4f3e1)

- - - - -
3a18c0fa by Zubin Duggal at 2024-07-01T04:00:07+05:30
Prepare release 9.6.6

- - - - -


9 changed files:

- compiler/GHC/ByteCode/Instr.hs
- configure.ac
- docs/users_guide/9.6.5-notes.rst
- + docs/users_guide/9.6.6-notes.rst
- docs/users_guide/release-notes.rst
- rts/Interpreter.c
- + testsuite/tests/th/should_compile/T24870/Def.hs
- + testsuite/tests/th/should_compile/T24870/Use.hs
- + testsuite/tests/th/should_compile/T24870/all.T


Changes:

=====================================
compiler/GHC/ByteCode/Instr.hs
=====================================
@@ -82,7 +82,7 @@ data BCInstr
    | PUSH16_W !Word16
    | PUSH32_W !Word16
 
-   -- Push a ptr  (these all map to PUSH_G really)
+   -- Push a (heap) ptr  (these all map to PUSH_G really)
    | PUSH_G       Name
    | PUSH_PRIMOP  PrimOp
    | PUSH_BCO     (ProtoBCO Name)


=====================================
configure.ac
=====================================
@@ -13,7 +13,7 @@ dnl
 # see what flags are available. (Better yet, read the documentation!)
 #
 
-AC_INIT([The Glorious Glasgow Haskell Compilation System], [9.6.5], [glasgow-haskell-bugs at haskell.org], [ghc-AC_PACKAGE_VERSION])
+AC_INIT([The Glorious Glasgow Haskell Compilation System], [9.6.6], [glasgow-haskell-bugs at haskell.org], [ghc-AC_PACKAGE_VERSION])
     # Version on master must be X.Y (not X.Y.Z) for ProjectVersionMunged variable
     # to be useful (cf #19058). However, the version must have three components
     # (X.Y.Z) on stable branches (e.g. ghc-9.2) to ensure that pre-releases are


=====================================
docs/users_guide/9.6.5-notes.rst
=====================================
@@ -63,49 +63,3 @@ Core libraries
 - Bump ``Cabal`` to 3.10.3.0
 - Bump ``process`` to 1.6.19.0
 - Bump ``libffi-tarballs`` to 3.4.6
-
-Included libraries
-------------------
-
-The package database provided with this distribution also contains a number of
-packages other than GHC itself. See the changelogs provided with these packages
-for further change information.
-
-.. ghc-package-list::
-
-    libraries/array/array.cabal:             Dependency of ``ghc`` library
-    libraries/base/base.cabal:               Core library
-    libraries/binary/binary.cabal:           Dependency of ``ghc`` library
-    libraries/bytestring/bytestring.cabal:   Dependency of ``ghc`` library
-    libraries/Cabal/Cabal/Cabal.cabal:       Dependency of ``ghc-pkg`` utility
-    libraries/Cabal/Cabal-syntax/Cabal-syntax.cabal:  Dependency of ``ghc-pkg`` utility
-    libraries/containers/containers/containers.cabal: Dependency of ``ghc`` library
-    libraries/deepseq/deepseq.cabal:         Dependency of ``ghc`` library
-    libraries/directory/directory.cabal:     Dependency of ``ghc`` library
-    libraries/exceptions/exceptions.cabal:   Dependency of ``ghc`` and ``haskeline`` library
-    libraries/filepath/filepath.cabal:       Dependency of ``ghc`` library
-    compiler/ghc.cabal:                      The compiler itself
-    libraries/ghci/ghci.cabal:               The REPL interface
-    libraries/ghc-boot/ghc-boot.cabal:       Internal compiler library
-    libraries/ghc-boot-th/ghc-boot-th.cabal: Internal compiler library
-    libraries/ghc-compact/ghc-compact.cabal: Core library
-    libraries/ghc-heap/ghc-heap.cabal:       GHC heap-walking library
-    libraries/ghc-prim/ghc-prim.cabal:       Core library
-    libraries/haskeline/haskeline.cabal:     Dependency of ``ghci`` executable
-    libraries/hpc/hpc.cabal:                 Dependency of ``hpc`` executable
-    libraries/integer-gmp/integer-gmp.cabal: Core library
-    libraries/libiserv/libiserv.cabal:       Internal compiler library
-    libraries/mtl/mtl.cabal:                 Dependency of ``Cabal`` library
-    libraries/parsec/parsec.cabal:           Dependency of ``Cabal`` library
-    libraries/pretty/pretty.cabal:           Dependency of ``ghc`` library
-    libraries/process/process.cabal:         Dependency of ``ghc`` library
-    libraries/stm/stm.cabal:                 Dependency of ``haskeline`` library
-    libraries/template-haskell/template-haskell.cabal: Core library
-    libraries/terminfo/terminfo.cabal:       Dependency of ``haskeline`` library
-    libraries/text/text.cabal:               Dependency of ``Cabal`` library
-    libraries/time/time.cabal:               Dependency of ``ghc`` library
-    libraries/transformers/transformers.cabal: Dependency of ``ghc`` library
-    libraries/unix/unix.cabal:               Dependency of ``ghc`` library
-    libraries/Win32/Win32.cabal:             Dependency of ``ghc`` library
-    libraries/xhtml/xhtml.cabal:             Dependency of ``haddock`` executable
-


=====================================
docs/users_guide/9.6.6-notes.rst
=====================================
@@ -0,0 +1,107 @@
+.. _release-9.6.6:
+
+Version 9.6.6
+==============
+
+The significant changes to the various parts of the compiler are listed below.
+See the `migration guide
+<https://gitlab.haskell.org/ghc/ghc/-/wikis/migration/9.6>`_ on the GHC Wiki
+for specific guidance on migrating programs to this release.
+
+The :ghc-flag:`LLVM backend <-fllvm>` of this release is to be used with LLVM
+11, 12, 13, 14 or 15.
+
+Significant Changes
+~~~~~~~~~~~~~~~~~~~~
+
+Issues fixed in this release include:
+
+Compiler
+--------
+
+- Fix a bug in the NCG that could lead to incorrect runtime results due to
+  erroneously removing a jump instruction (:ghc-ticket:`24507`).
+- Fix a bug leading to runtime exponential in the size of the package dependency
+  graph when checking if a multiple home unit session satisfied the closure
+  property.
+- Fix a bug arising from interactions between GHC and cabal in how they
+  call the linker to merge objects, resulting in linker errors in
+  certain situations (:ghc-ticket:`22210`).
+- Improve the algorithm used to detect missing home modules for the purpose of
+  emitting warnings, making it no longer quadratic (:ghc-ticket:`24778`).
+- Ensure ``-ddump-splices`` doesn't omit required parentheses when printing
+  pattern signatures (:ghc-ticket:`24837`).
+- Fix a possible alignment confusion in the code generator
+  (:ghc-ticket:`24930`).
+- Fix sign hints for the PowerPC NCG in C foreign calls (:ghc-ticket:`23034`).
+- Fix a simplifier bug that could lead to compiler panics in certain sitations
+  due to incorrect eta expansion (:ghc-ticket:`24718`).
+- Fix a segfault in the bytecode interpreter due to incorrect constructor tagging
+  (:ghc-ticket:`24870`).
+
+Runtime system
+--------------
+
+- Fixes for various warnings emitted by ``UndefinedBehaviorSanitizer``. 
+- Fix an argument ordering warning for ``stgCallocBytes`` (:ghc-ticket:`24828`).
+- Fix a GC bug that manifests in certain situations when byte code objects end up
+  on the mutable list (:ghc-ticket:`23375`).
+
+Build system and packaging
+--------------------------
+
+- Allow hadrian to be bootstrapped using GHC 9.6 and Cabal 3.10.3 (:ghc-ticket:`24694`).
+- The ``no_dynamic_libs`` flavour transformer now doesn't need users to also explicitly
+  disable building a dynamic GHC.
+- 
+
+Core libraries
+--------------
+
+- Bump ``directory`` to 1.3.8.5
+
+Included libraries
+------------------
+
+The package database provided with this distribution also contains a number of
+packages other than GHC itself. See the changelogs provided with these packages
+for further change information.
+
+.. ghc-package-list::
+
+    libraries/array/array.cabal:             Dependency of ``ghc`` library
+    libraries/base/base.cabal:               Core library
+    libraries/binary/binary.cabal:           Dependency of ``ghc`` library
+    libraries/bytestring/bytestring.cabal:   Dependency of ``ghc`` library
+    libraries/Cabal/Cabal/Cabal.cabal:       Dependency of ``ghc-pkg`` utility
+    libraries/Cabal/Cabal-syntax/Cabal-syntax.cabal:  Dependency of ``ghc-pkg`` utility
+    libraries/containers/containers/containers.cabal: Dependency of ``ghc`` library
+    libraries/deepseq/deepseq.cabal:         Dependency of ``ghc`` library
+    libraries/directory/directory.cabal:     Dependency of ``ghc`` library
+    libraries/exceptions/exceptions.cabal:   Dependency of ``ghc`` and ``haskeline`` library
+    libraries/filepath/filepath.cabal:       Dependency of ``ghc`` library
+    compiler/ghc.cabal:                      The compiler itself
+    libraries/ghci/ghci.cabal:               The REPL interface
+    libraries/ghc-boot/ghc-boot.cabal:       Internal compiler library
+    libraries/ghc-boot-th/ghc-boot-th.cabal: Internal compiler library
+    libraries/ghc-compact/ghc-compact.cabal: Core library
+    libraries/ghc-heap/ghc-heap.cabal:       GHC heap-walking library
+    libraries/ghc-prim/ghc-prim.cabal:       Core library
+    libraries/haskeline/haskeline.cabal:     Dependency of ``ghci`` executable
+    libraries/hpc/hpc.cabal:                 Dependency of ``hpc`` executable
+    libraries/integer-gmp/integer-gmp.cabal: Core library
+    libraries/libiserv/libiserv.cabal:       Internal compiler library
+    libraries/mtl/mtl.cabal:                 Dependency of ``Cabal`` library
+    libraries/parsec/parsec.cabal:           Dependency of ``Cabal`` library
+    libraries/pretty/pretty.cabal:           Dependency of ``ghc`` library
+    libraries/process/process.cabal:         Dependency of ``ghc`` library
+    libraries/stm/stm.cabal:                 Dependency of ``haskeline`` library
+    libraries/template-haskell/template-haskell.cabal: Core library
+    libraries/terminfo/terminfo.cabal:       Dependency of ``haskeline`` library
+    libraries/text/text.cabal:               Dependency of ``Cabal`` library
+    libraries/time/time.cabal:               Dependency of ``ghc`` library
+    libraries/transformers/transformers.cabal: Dependency of ``ghc`` library
+    libraries/unix/unix.cabal:               Dependency of ``ghc`` library
+    libraries/Win32/Win32.cabal:             Dependency of ``ghc`` library
+    libraries/xhtml/xhtml.cabal:             Dependency of ``haddock`` executable
+


=====================================
docs/users_guide/release-notes.rst
=====================================
@@ -9,3 +9,4 @@ Release notes
    9.6.3-notes
    9.6.4-notes
    9.6.5-notes
+   9.6.6-notes


=====================================
rts/Interpreter.c
=====================================
@@ -4,6 +4,30 @@
  * Copyright (c) The GHC Team, 1994-2002.
  * ---------------------------------------------------------------------------*/
 
+/*
+Note [CBV Functions and the interpreter]
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+When the byte code interpreter loads a reference to a value it often
+ends up as a non-tagged pointers *especially* if we already know a value
+is a certain constructor and therefore don't perform an eval on the reference.
+This causes friction with CBV functions which assume
+their value arguments are properly tagged by the caller.
+
+In order to ensure CBV functions still get passed tagged functions we have
+three options:
+a)  Special case the interpreter behaviour into the tag inference analysis.
+    If we assume the interpreter can't properly tag value references the STG passes
+    would then wrap such calls in appropriate evals which are executed at runtime.
+    This would ensure tags by doing additional evals at runtime.
+b)  When the interpreter pushes references for known constructors instead of
+    pushing the objects address add the tag to the value pushed. This is what
+    the NCG backends do.
+c)  When the interpreter pushes a reference inspect the closure of the object
+    and apply the appropriate tag at runtime.
+
+For now we use approach c). Mostly because it's easiest to implement. We also don't
+tag functions as tag inference currently doesn't rely on those being properly tagged.
+*/
 
 #include "rts/PosixSource.h"
 #include "Rts.h"
@@ -1295,8 +1319,43 @@ run_BCO:
         }
 
         case bci_PUSH_G: {
-            int o1 = BCO_GET_LARGE_ARG;
-            SpW(-1) = BCO_PTR(o1);
+            W_ o1 = BCO_GET_LARGE_ARG;
+            StgClosure *tagged_obj = (StgClosure*) BCO_PTR(o1);
+
+            tag_push_g:
+            ASSERT(LOOKS_LIKE_CLOSURE_PTR((StgClosure*) tagged_obj));
+            // Here we make sure references we push are tagged.
+            // See Note [CBV Functions and the interpreter] in Info.hs
+
+            //Safe some memory reads if we already have a tag.
+            if(GET_CLOSURE_TAG(tagged_obj) == 0) {
+                StgClosure *obj = UNTAG_CLOSURE(tagged_obj);
+                switch ( get_itbl(obj)->type ) {
+                    case IND:
+                    case IND_STATIC:
+                    {
+                        tagged_obj = ACQUIRE_LOAD(&((StgInd*)obj)->indirectee);
+                        goto tag_push_g;
+                    }
+                    case CONSTR:
+                    case CONSTR_1_0:
+                    case CONSTR_0_1:
+                    case CONSTR_2_0:
+                    case CONSTR_1_1:
+                    case CONSTR_0_2:
+                    case CONSTR_NOCAF:
+                        // The value is already evaluated, so we can just return it. However,
+                        // before we do, we MUST ensure that the pointer is tagged, because we
+                        // might return to a native `case` expression, which assumes the returned
+                        // pointer is tagged so it can use the tag to select an alternative.
+                        tagged_obj = tagConstr(obj);
+                        break;
+                    default:
+                        break;
+                }
+            }
+
+            SpW(-1) = (W_) tagged_obj;
             Sp_subW(1);
             goto nextInsn;
         }


=====================================
testsuite/tests/th/should_compile/T24870/Def.hs
=====================================
@@ -0,0 +1,9 @@
+{-# LANGUAGE TemplateHaskell #-}
+
+module SDef where
+
+{-# NOINLINE aValue #-}
+aValue = True
+
+{-# NOINLINE aStrictFunction #-}
+aStrictFunction !x = [| x |]


=====================================
testsuite/tests/th/should_compile/T24870/Use.hs
=====================================
@@ -0,0 +1,9 @@
+{-# LANGUAGE TemplateHaskell #-}
+
+module SUse where
+
+import qualified Language.Haskell.TH.Syntax as TH
+import SDef
+import GHC.Exts
+
+bar = $( inline aStrictFunction aValue )


=====================================
testsuite/tests/th/should_compile/T24870/all.T
=====================================
@@ -0,0 +1,6 @@
+# The interpreter must uphold tagging invariants, and failed to do so in #24870
+# We test this here by having the interpreter calls a strict worker function
+# with a reference to a value it constructed.
+# See also Note [CBV Functions and the interpreter]
+test('T24870', [extra_files(['Def.hs', 'Use.hs']), req_th],
+	      multimod_compile, ['Def Use', '-dtag-inference-checks -v0'])



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/28ddc41b48e0947f2b31d1217b522df91b3756ac...3a18c0fa2edcd61b0c3b470661791b09501c4c2b

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/28ddc41b48e0947f2b31d1217b522df91b3756ac...3a18c0fa2edcd61b0c3b470661791b09501c4c2b
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20240630/1ea4c90e/attachment-0001.html>


More information about the ghc-commits mailing list