[Git][ghc/ghc][wip/andreask/arm_mem_model] NCG: AArch64 - Add -fhuge-code-sections.

Andreas Klebinger (@AndreasK) gitlab at gitlab.haskell.org
Thu Apr 25 13:16:42 UTC 2024



Andreas Klebinger pushed to branch wip/andreask/arm_mem_model at Glasgow Haskell Compiler / GHC


Commits:
32c94235 by Andreas Klebinger at 2024-04-25T15:13:07+02:00
NCG: AArch64 - Add -fhuge-code-sections.

When enabled the arm backend will assume jumps to targets outside of the
current module are further than 128MB away.

This will allow for code to work if:
* The current module results in less than 128MB of code.
* The whole program is loaded within a 4GB memory region.

We enable this by default on mac where the lack of split sections can
sometimes cause us to go over this limit - see #24648.

This works around #24648 for now.

- - - - -


7 changed files:

- compiler/GHC/CmmToAsm/AArch64/CodeGen.hs
- compiler/GHC/CmmToAsm/Config.hs
- compiler/GHC/Driver/Config/CmmToAsm.hs
- compiler/GHC/Driver/DynFlags.hs
- compiler/GHC/Driver/Flags.hs
- compiler/GHC/Driver/Session.hs
- docs/users_guide/using-optimisation.rst


Changes:

=====================================
compiler/GHC/CmmToAsm/AArch64/CodeGen.hs
=====================================
@@ -23,7 +23,7 @@ import GHC.Cmm.DebugBlock
 import GHC.CmmToAsm.Monad
    ( NatM, getNewRegNat
    , getPicBaseMaybeNat, getPlatform, getConfig
-   , getDebugBlock, getFileId
+   , getDebugBlock, getFileId, getThisModuleNat
    )
 -- import GHC.CmmToAsm.Instr
 import GHC.CmmToAsm.PIC
@@ -1353,8 +1353,16 @@ assignReg_FltCode = assignReg_IntCode
 -- Jumps
 
 genJump :: CmmExpr{-the branch target-} -> NatM InstrBlock
-genJump expr@(CmmLit (CmmLabel lbl))
-  = return $ unitOL (annExpr expr (J (TLabel lbl)))
+genJump expr@(CmmLit (CmmLabel lbl)) = do
+  cur_mod <- getThisModuleNat
+  !huge_sections <- ncgEnableHugeTextSections <$> getConfig
+  let is_local = isLocalCLabel cur_mod lbl
+
+  if not huge_sections || is_local
+    then return $ unitOL (annExpr expr (J (TLabel lbl)))
+    else do
+      (target, _format, code) <- getSomeReg expr
+      return (code `appOL` unitOL (annExpr expr (J (TReg target))))
 
 genJump expr = do
     (target, _format, code) <- getSomeReg expr


=====================================
compiler/GHC/CmmToAsm/Config.hs
=====================================
@@ -44,6 +44,7 @@ data NCGConfig = NCGConfig
    , ncgDwarfSourceNotes      :: !Bool            -- ^ Enable GHC-specific source note DIEs
    , ncgCmmStaticPred         :: !Bool            -- ^ Enable static control-flow prediction
    , ncgEnableShortcutting    :: !Bool            -- ^ Enable shortcutting (don't jump to blocks only containing a jump)
+   , ncgEnableHugeTextSections:: !Bool            -- ^ Enable use of far-jumps by default.
    , ncgComputeUnwinding      :: !Bool            -- ^ Compute block unwinding tables
    , ncgEnableDeadCodeElimination :: !Bool        -- ^ Whether to enable the dead-code elimination
    }


=====================================
compiler/GHC/Driver/Config/CmmToAsm.hs
=====================================
@@ -68,6 +68,7 @@ initNCGConfig dflags this_mod = NCGConfig
    , ncgExposeInternalSymbols = gopt Opt_ExposeInternalSymbols dflags
    , ncgCmmStaticPred       = gopt Opt_CmmStaticPred dflags
    , ncgEnableShortcutting  = gopt Opt_AsmShortcutting dflags
+   , ncgEnableHugeTextSections = gopt Opt_HugeCodeSections dflags
    , ncgComputeUnwinding    = debugLevel dflags > 0
    , ncgEnableDeadCodeElimination = not (gopt Opt_InfoTableMap dflags)
                                      -- Disable when -finfo-table-map is on (#20428)


=====================================
compiler/GHC/Driver/DynFlags.hs
=====================================
@@ -1204,6 +1204,16 @@ defaultFlags settings
 
     ++ validHoleFitDefaults
 
+    -- Platform/OS specific stuff
+    ++ case platformOS platform of
+        -- On mac in edge cases we end up with very large text sections
+        -- so enable HugeTextSections by default to generate jumps compatible
+        -- with those.
+        OSDarwin
+          | platformArch platform == ArchAArch64
+          -> [Opt_HugeCodeSections]
+        _ -> []
+
 
     where platform = sTargetPlatform settings
 


=====================================
compiler/GHC/Driver/Flags.hs
=====================================
@@ -299,6 +299,7 @@ data GeneralFlag
    | Opt_CmmElimCommonBlocks
    | Opt_CmmControlFlow
    | Opt_AsmShortcutting
+   | Opt_HugeCodeSections
    | Opt_OmitYields
    | Opt_FunToThunk               -- deprecated
    | Opt_DictsStrict                     -- be strict in argument dictionaries
@@ -541,6 +542,7 @@ optimisationFlags = EnumSet.fromList
    , Opt_CmmSink
    , Opt_CmmElimCommonBlocks
    , Opt_AsmShortcutting
+   , Opt_HugeCodeSections
    , Opt_FunToThunk
    , Opt_DmdTxDictSel
    , Opt_Loopification


=====================================
compiler/GHC/Driver/Session.hs
=====================================
@@ -2412,6 +2412,7 @@ fFlagsDeps = [
   flagSpec "gen-manifest"                     Opt_GenManifest,
   flagSpec "ghci-history"                     Opt_GhciHistory,
   flagSpec "ghci-leak-check"                  Opt_GhciLeakCheck,
+  flagSpec "huge-code-sections"               Opt_HugeCodeSections,
   flagSpec "validate-ide-info"                Opt_ValidateHie,
   flagGhciSpec "local-ghci-history"           Opt_LocalGhciHistory,
   flagGhciSpec "no-it"                        Opt_NoIt,


=====================================
docs/users_guide/using-optimisation.rst
=====================================
@@ -596,6 +596,24 @@ as such you shouldn't need to set any of them explicitly. A flag
     ``-fno-full-laziness``. If that is inconvenient for you, please leave a
     comment `on the issue tracker (#21204) <https://gitlab.haskell.org/ghc/ghc/-/issues/21204>`__.
 
+.. ghc-flag:: -fhuge-text-sections
+    :shortdesc: Assume code sections can be unreasonably large.
+    :type: dynamic
+    :reverse: -fno-huge-code-sections
+    :category:
+
+    :default: Enabled by default on aarch64 macOS. Otherwise defaults to no.
+
+    In some niche scenarious one can end up with code sections large enough to prevent the
+    linker from properly relocating jumps. (:ghc-ticket:`24648`)
+
+    In such cases this flag causes all jumps to targets outside of the currently
+    compiled module to be generated in a way that allows for larger jump offsets
+    at the cost of performance.
+
+    Note that this flag currently only affects the NCG AArch64 backend.
+
+
 .. ghc-flag:: -fignore-asserts
     :shortdesc: Ignore assertions in the source. Implied by :ghc-flag:`-O`.
     :type: dynamic



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/32c94235fff1438c9b4d11015c7df4a7c85e6c62

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/32c94235fff1438c9b4d11015c7df4a7c85e6c62
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/20240425/8ee4899f/attachment-0001.html>


More information about the ghc-commits mailing list