Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC
Commits:
-
2433e91d
by Cheng Shao at 2026-01-09T20:24:43-05:00
-
e5926fbe
by Cheng Shao at 2026-01-09T20:24:43-05:00
-
244d57d7
by Cheng Shao at 2026-01-09T20:24:43-05:00
8 changed files:
- compiler/GHC/Cmm.hs
- compiler/GHC/Cmm/InitFini.hs
- compiler/GHC/CmmToAsm/AArch64/Ppr.hs
- compiler/GHC/CmmToAsm/Ppr.hs
- compiler/GHC/CmmToAsm/Wasm/FromCmm.hs
- compiler/GHC/CmmToAsm/X86/Ppr.hs
- compiler/GHC/CmmToC.hs
- compiler/GHC/CmmToLlvm/Data.hs
Changes:
| ... | ... | @@ -278,8 +278,8 @@ data SectionProtection |
| 278 | 278 | deriving (Eq)
|
| 279 | 279 | |
| 280 | 280 | -- | Should a data in this section be considered constant at runtime
|
| 281 | -sectionProtection :: Section -> SectionProtection
|
|
| 282 | -sectionProtection (Section t _) = case t of
|
|
| 281 | +sectionProtection :: SectionType -> SectionProtection
|
|
| 282 | +sectionProtection t = case t of
|
|
| 283 | 283 | Text -> ReadOnlySection
|
| 284 | 284 | ReadOnlyData -> ReadOnlySection
|
| 285 | 285 | RelocatableReadOnlyData -> WriteProtectedSection
|
| ... | ... | @@ -2,6 +2,7 @@ |
| 2 | 2 | module GHC.Cmm.InitFini
|
| 3 | 3 | ( InitOrFini(..)
|
| 4 | 4 | , isInitOrFiniArray
|
| 5 | + , isInitOrFiniSection
|
|
| 5 | 6 | ) where
|
| 6 | 7 | |
| 7 | 8 | import GHC.Prelude
|
| ... | ... | @@ -63,8 +64,8 @@ finalizer CmmDecl will be emitted per module. |
| 63 | 64 | data InitOrFini = IsInitArray | IsFiniArray
|
| 64 | 65 | |
| 65 | 66 | isInitOrFiniArray :: RawCmmDecl -> Maybe (InitOrFini, [CLabel])
|
| 66 | -isInitOrFiniArray (CmmData sect (CmmStaticsRaw _ lits))
|
|
| 67 | - | Just initOrFini <- isInitOrFiniSection sect
|
|
| 67 | +isInitOrFiniArray (CmmData (Section t _) (CmmStaticsRaw _ lits))
|
|
| 68 | + | Just initOrFini <- isInitOrFiniSection t
|
|
| 68 | 69 | = Just (initOrFini, map get_label lits)
|
| 69 | 70 | where
|
| 70 | 71 | get_label :: CmmStatic -> CLabel
|
| ... | ... | @@ -72,7 +73,7 @@ isInitOrFiniArray (CmmData sect (CmmStaticsRaw _ lits)) |
| 72 | 73 | get_label static = pprPanic "isInitOrFiniArray: invalid entry" (ppr static)
|
| 73 | 74 | isInitOrFiniArray _ = Nothing
|
| 74 | 75 | |
| 75 | -isInitOrFiniSection :: Section -> Maybe InitOrFini
|
|
| 76 | -isInitOrFiniSection (Section InitArray _) = Just IsInitArray
|
|
| 77 | -isInitOrFiniSection (Section FiniArray _) = Just IsFiniArray
|
|
| 76 | +isInitOrFiniSection :: SectionType -> Maybe InitOrFini
|
|
| 77 | +isInitOrFiniSection InitArray = Just IsInitArray
|
|
| 78 | +isInitOrFiniSection FiniArray = Just IsFiniArray
|
|
| 78 | 79 | isInitOrFiniSection _ = Nothing |
| ... | ... | @@ -19,6 +19,7 @@ import GHC.Cmm.Dataflow.Label |
| 19 | 19 | |
| 20 | 20 | import GHC.Cmm.BlockId
|
| 21 | 21 | import GHC.Cmm.CLabel
|
| 22 | +import GHC.Cmm.InitFini
|
|
| 22 | 23 | |
| 23 | 24 | import GHC.Types.Unique ( pprUniqueAlways, getUnique )
|
| 24 | 25 | import GHC.Platform
|
| ... | ... | @@ -28,9 +29,7 @@ import GHC.Utils.Panic |
| 28 | 29 | |
| 29 | 30 | pprNatCmmDecl :: IsDoc doc => NCGConfig -> NatCmmDecl RawCmmStatics Instr -> doc
|
| 30 | 31 | pprNatCmmDecl config (CmmData section dats) =
|
| 31 | - let platform = ncgPlatform config
|
|
| 32 | - in
|
|
| 33 | - pprSectionAlign config section $$ pprDatas platform dats
|
|
| 32 | + pprSectionAlign config section $$ pprDatas config dats
|
|
| 34 | 33 | |
| 35 | 34 | pprNatCmmDecl config proc@(CmmProc top_info lbl _ (ListGraph blocks)) =
|
| 36 | 35 | let platform = ncgPlatform config
|
| ... | ... | @@ -91,9 +90,20 @@ pprAlignForSection _platform _seg |
| 91 | 90 | -- .balign 8
|
| 92 | 91 | --
|
| 93 | 92 | pprSectionAlign :: IsDoc doc => NCGConfig -> Section -> doc
|
| 94 | -pprSectionAlign config sec@(Section seg _) =
|
|
| 93 | +pprSectionAlign config sec@(Section seg suffix) =
|
|
| 95 | 94 | line (pprSectionHeader config sec)
|
| 95 | + $$ coffSplitSectionComdatKey
|
|
| 96 | 96 | $$ pprAlignForSection (ncgPlatform config) seg
|
| 97 | + where
|
|
| 98 | + platform = ncgPlatform config
|
|
| 99 | + -- See Note [Split sections on COFF objects]
|
|
| 100 | + coffSplitSectionComdatKey
|
|
| 101 | + | OSMinGW32 <- platformOS platform
|
|
| 102 | + , ncgSplitSections config
|
|
| 103 | + , Nothing <- isInitOrFiniSection seg
|
|
| 104 | + = line (pprCOFFComdatKey platform suffix <> colon)
|
|
| 105 | + | otherwise
|
|
| 106 | + = empty
|
|
| 97 | 107 | |
| 98 | 108 | -- | Output the ELF .size directive.
|
| 99 | 109 | pprSizeDecl :: IsDoc doc => Platform -> CLabel -> doc
|
| ... | ... | @@ -136,20 +146,26 @@ pprBasicBlock platform with_dwarf info_env (BasicBlock blockid instrs) |
| 136 | 146 | (l@LOCATION{} : _) -> pprInstr platform l
|
| 137 | 147 | _other -> empty
|
| 138 | 148 | |
| 139 | -pprDatas :: IsDoc doc => Platform -> RawCmmStatics -> doc
|
|
| 149 | +pprDatas :: IsDoc doc => NCGConfig -> RawCmmStatics -> doc
|
|
| 140 | 150 | -- See Note [emit-time elimination of static indirections] in "GHC.Cmm.CLabel".
|
| 141 | -pprDatas platform (CmmStaticsRaw alias [CmmStaticLit (CmmLabel lbl), CmmStaticLit ind, _, _])
|
|
| 151 | +pprDatas config (CmmStaticsRaw alias [CmmStaticLit (CmmLabel lbl), CmmStaticLit ind, _, _])
|
|
| 142 | 152 | | lbl == mkIndStaticInfoLabel
|
| 143 | 153 | , let labelInd (CmmLabelOff l _) = Just l
|
| 144 | 154 | labelInd (CmmLabel l) = Just l
|
| 145 | 155 | labelInd _ = Nothing
|
| 146 | 156 | , Just ind' <- labelInd ind
|
| 147 | 157 | , alias `mayRedirectTo` ind'
|
| 158 | + -- See Note [Split sections on COFF objects]
|
|
| 159 | + , not $ platformOS platform == OSMinGW32 && ncgSplitSections config
|
|
| 148 | 160 | = pprGloblDecl platform alias
|
| 149 | 161 | $$ line (text ".equiv" <+> pprAsmLabel platform alias <> comma <> pprAsmLabel platform ind')
|
| 162 | + where
|
|
| 163 | + platform = ncgPlatform config
|
|
| 150 | 164 | |
| 151 | -pprDatas platform (CmmStaticsRaw lbl dats)
|
|
| 165 | +pprDatas config (CmmStaticsRaw lbl dats)
|
|
| 152 | 166 | = vcat (pprLabel platform lbl : map (pprData platform) dats)
|
| 167 | + where
|
|
| 168 | + platform = ncgPlatform config
|
|
| 153 | 169 | |
| 154 | 170 | pprData :: IsDoc doc => Platform -> CmmStatic -> doc
|
| 155 | 171 | pprData _platform (CmmString str) = line (pprString str)
|
| 1 | 1 | {-# LANGUAGE MagicHash #-}
|
| 2 | +{-# LANGUAGE MultiWayIf #-}
|
|
| 2 | 3 | |
| 3 | 4 | -----------------------------------------------------------------------------
|
| 4 | 5 | --
|
| ... | ... | @@ -14,6 +15,7 @@ module GHC.CmmToAsm.Ppr ( |
| 14 | 15 | pprASCII,
|
| 15 | 16 | pprString,
|
| 16 | 17 | pprFileEmbed,
|
| 18 | + pprCOFFComdatKey,
|
|
| 17 | 19 | pprSectionHeader
|
| 18 | 20 | )
|
| 19 | 21 | |
| ... | ... | @@ -23,6 +25,7 @@ import GHC.Prelude |
| 23 | 25 | |
| 24 | 26 | import GHC.Utils.Asm
|
| 25 | 27 | import GHC.Cmm.CLabel
|
| 28 | +import GHC.Cmm.InitFini
|
|
| 26 | 29 | import GHC.Cmm
|
| 27 | 30 | import GHC.CmmToAsm.Config
|
| 28 | 31 | import GHC.Utils.Outputable as SDoc
|
| ... | ... | @@ -220,8 +223,8 @@ pprGNUSectionHeader config t suffix = |
| 220 | 223 | | otherwise -> text ".rodata"
|
| 221 | 224 | RelocatableReadOnlyData | OSMinGW32 <- platformOS platform
|
| 222 | 225 | -- Concept does not exist on Windows,
|
| 223 | - -- So map these to R/O data.
|
|
| 224 | - -> text ".rdata$rel.ro"
|
|
| 226 | + -- So map these to data.
|
|
| 227 | + -> text ".data"
|
|
| 225 | 228 | | otherwise -> text ".data.rel.ro"
|
| 226 | 229 | UninitialisedData -> text ".bss"
|
| 227 | 230 | InitArray
|
| ... | ... | @@ -240,24 +243,79 @@ pprGNUSectionHeader config t suffix = |
| 240 | 243 | | OSMinGW32 <- platformOS platform
|
| 241 | 244 | -> text ".rdata"
|
| 242 | 245 | | otherwise -> text ".ipe"
|
| 243 | - flags = case t of
|
|
| 244 | - Text
|
|
| 245 | - | OSMinGW32 <- platformOS platform, splitSections
|
|
| 246 | - -> text ",\"xr\""
|
|
| 247 | - | splitSections
|
|
| 248 | - -> text ",\"ax\"," <> sectionType platform "progbits"
|
|
| 249 | - CString
|
|
| 250 | - | OSMinGW32 <- platformOS platform
|
|
| 251 | - -> empty
|
|
| 252 | - | otherwise -> text ",\"aMS\"," <> sectionType platform "progbits" <> text ",1"
|
|
| 253 | - IPE
|
|
| 254 | - | OSMinGW32 <- platformOS platform
|
|
| 255 | - -> empty
|
|
| 256 | - | otherwise -> text ",\"a\"," <> sectionType platform "progbits"
|
|
| 257 | - _ -> empty
|
|
| 246 | + flags
|
|
| 247 | + -- See
|
|
| 248 | + -- https://github.com/llvm/llvm-project/blob/llvmorg-21.1.8/lld/COFF/Chunks.cpp#L54
|
|
| 249 | + -- and https://llvm.org/docs/Extensions.html#section-directive.
|
|
| 250 | + -- LLD COFF backend gc-sections only work on COMDAT sections so
|
|
| 251 | + -- we need to mark it as a COMDAT section. You can use clang64
|
|
| 252 | + -- toolchain to compile small examples with
|
|
| 253 | + -- `-ffunction-sections -fdata-sections -S` to see these section
|
|
| 254 | + -- headers in the wild. Also see Note [Split sections on COFF objects]
|
|
| 255 | + -- below.
|
|
| 256 | + | OSMinGW32 <- platformOS platform,
|
|
| 257 | + splitSections =
|
|
| 258 | + if
|
|
| 259 | + | Just _ <- isInitOrFiniSection t -> text ",\"dw\""
|
|
| 260 | + | otherwise ->
|
|
| 261 | + let coff_section_flags
|
|
| 262 | + | Text <- t = "xr"
|
|
| 263 | + | UninitialisedData <- t = "bw"
|
|
| 264 | + | ReadOnlySection <- sectionProtection t = "dr"
|
|
| 265 | + | otherwise = "dw"
|
|
| 266 | + in hcat
|
|
| 267 | + [ text ",\"",
|
|
| 268 | + text coff_section_flags,
|
|
| 269 | + text "\",one_only,",
|
|
| 270 | + pprCOFFComdatKey platform suffix
|
|
| 271 | + ]
|
|
| 272 | + | otherwise =
|
|
| 273 | + case t of
|
|
| 274 | + Text
|
|
| 275 | + | splitSections
|
|
| 276 | + -> text ",\"ax\"," <> sectionType platform "progbits"
|
|
| 277 | + CString
|
|
| 278 | + | OSMinGW32 <- platformOS platform
|
|
| 279 | + -> empty
|
|
| 280 | + | otherwise -> text ",\"aMS\"," <> sectionType platform "progbits" <> text ",1"
|
|
| 281 | + IPE
|
|
| 282 | + | OSMinGW32 <- platformOS platform
|
|
| 283 | + -> empty
|
|
| 284 | + | otherwise -> text ",\"a\"," <> sectionType platform "progbits"
|
|
| 285 | + _ -> empty
|
|
| 258 | 286 | {-# SPECIALIZE pprGNUSectionHeader :: NCGConfig -> SectionType -> CLabel -> SDoc #-}
|
| 259 | 287 | {-# SPECIALIZE pprGNUSectionHeader :: NCGConfig -> SectionType -> CLabel -> HLine #-} -- see Note [SPECIALIZE to HDoc] in GHC.Utils.Outputable
|
| 260 | 288 | |
| 289 | +-- | Note [Split sections on COFF objects]
|
|
| 290 | +-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
| 291 | +--
|
|
| 292 | +-- On Windows/COFF, LLD's gc-sections only works on COMDAT sections,
|
|
| 293 | +-- so we mark split sections as COMDAT and need to provide a unique
|
|
| 294 | +-- "key" symbol.
|
|
| 295 | +--
|
|
| 296 | +-- Important: We must not use a dot-prefixed local label (e.g.
|
|
| 297 | +-- @.L...@) as the COMDAT key symbol, because LLVM's COFF assembler
|
|
| 298 | +-- treats dot-prefixed COMDAT key symbols specially and forces them to
|
|
| 299 | +-- have value 0 (the beginning of the section). That breaks
|
|
| 300 | +-- @tablesNextToCode@, where the info label is intentionally placed
|
|
| 301 | +-- after the info table data (at a non-zero offset).
|
|
| 302 | +--
|
|
| 303 | +-- Therefore we generate a non-dot-prefixed key symbol derived from
|
|
| 304 | +-- the section suffix, and (see arch-specific 'pprSectionAlign') we
|
|
| 305 | +-- emit a label definition for it at the beginning of the section.
|
|
| 306 | +--
|
|
| 307 | +-- ctor/dtor sections are specially treated; they must be emitted as
|
|
| 308 | +-- regular data sections, otherwise LLD will drop them.
|
|
| 309 | +--
|
|
| 310 | +-- Note that we must not emit .equiv directives for COMDAT sections in
|
|
| 311 | +-- COFF objects, they seriously confuse LLD and we end up with access
|
|
| 312 | +-- violations at runtimes.
|
|
| 313 | +pprCOFFComdatKey :: IsLine doc => Platform -> CLabel -> doc
|
|
| 314 | +pprCOFFComdatKey platform suffix =
|
|
| 315 | + text "__ghc_coff_comdat_" <> pprAsmLabel platform suffix
|
|
| 316 | +{-# SPECIALIZE pprCOFFComdatKey :: Platform -> CLabel -> SDoc #-}
|
|
| 317 | +{-# SPECIALIZE pprCOFFComdatKey :: Platform -> CLabel -> HLine #-} -- see Note [SPECIALIZE to HDoc] in GHC.Utils.Outputable
|
|
| 318 | + |
|
| 261 | 319 | -- XCOFF doesn't support relocating label-differences, so we place all
|
| 262 | 320 | -- RO sections into .text[PR] sections
|
| 263 | 321 | pprXcoffSectionHeader :: IsLine doc => SectionType -> doc
|
| ... | ... | @@ -107,7 +107,7 @@ symKindFromCLabel lbl |
| 107 | 107 | -- | Calculate a data section's kind, see haddock docs of
|
| 108 | 108 | -- 'DataSectionKind' for more explanation.
|
| 109 | 109 | dataSectionKindFromCmmSection :: Section -> DataSectionKind
|
| 110 | -dataSectionKindFromCmmSection s = case sectionProtection s of
|
|
| 110 | +dataSectionKindFromCmmSection (Section t _) = case sectionProtection t of
|
|
| 111 | 111 | ReadWriteSection -> SectionData
|
| 112 | 112 | _ -> SectionROData
|
| 113 | 113 |
| ... | ... | @@ -31,6 +31,7 @@ import GHC.Cmm hiding (topInfoTable) |
| 31 | 31 | import GHC.Cmm.Dataflow.Label
|
| 32 | 32 | import GHC.Cmm.BlockId
|
| 33 | 33 | import GHC.Cmm.CLabel
|
| 34 | +import GHC.Cmm.InitFini
|
|
| 34 | 35 | import GHC.Cmm.DebugBlock (pprUnwindTable)
|
| 35 | 36 | |
| 36 | 37 | import GHC.Types.Basic (Alignment, mkAlignment, alignmentBytes)
|
| ... | ... | @@ -195,8 +196,12 @@ pprDatas config (_, CmmStaticsRaw alias [CmmStaticLit (CmmLabel lbl), CmmStaticL |
| 195 | 196 | labelInd _ = Nothing
|
| 196 | 197 | , Just ind' <- labelInd ind
|
| 197 | 198 | , alias `mayRedirectTo` ind'
|
| 199 | + -- See Note [Split sections on COFF objects]
|
|
| 200 | + , not $ platformOS platform == OSMinGW32 && ncgSplitSections config
|
|
| 198 | 201 | = pprGloblDecl (ncgPlatform config) alias
|
| 199 | 202 | $$ line (text ".equiv" <+> pprAsmLabel (ncgPlatform config) alias <> comma <> pprAsmLabel (ncgPlatform config) ind')
|
| 203 | + where
|
|
| 204 | + platform = ncgPlatform config
|
|
| 200 | 205 | |
| 201 | 206 | pprDatas config (align, (CmmStaticsRaw lbl dats))
|
| 202 | 207 | = vcat (pprAlign platform align : pprLabel platform lbl : map (pprData config) dats)
|
| ... | ... | @@ -526,9 +531,20 @@ pprAddr platform (AddrBaseIndex base index displacement) |
| 526 | 531 | |
| 527 | 532 | -- | Print section header and appropriate alignment for that section.
|
| 528 | 533 | pprSectionAlign :: IsDoc doc => NCGConfig -> Section -> doc
|
| 529 | -pprSectionAlign config sec@(Section seg _) =
|
|
| 534 | +pprSectionAlign config sec@(Section seg suffix) =
|
|
| 530 | 535 | line (pprSectionHeader config sec) $$
|
| 536 | + coffSplitSectionComdatKey $$
|
|
| 531 | 537 | pprAlignForSection (ncgPlatform config) seg
|
| 538 | + where
|
|
| 539 | + platform = ncgPlatform config
|
|
| 540 | + -- See Note [Split sections on COFF objects]
|
|
| 541 | + coffSplitSectionComdatKey
|
|
| 542 | + | OSMinGW32 <- platformOS platform
|
|
| 543 | + , ncgSplitSections config
|
|
| 544 | + , Nothing <- isInitOrFiniSection seg
|
|
| 545 | + = line (pprCOFFComdatKey platform suffix <> colon)
|
|
| 546 | + | otherwise
|
|
| 547 | + = empty
|
|
| 532 | 548 | |
| 533 | 549 | -- | Print appropriate alignment for the given section type.
|
| 534 | 550 | pprAlignForSection :: IsDoc doc => Platform -> SectionType -> doc
|
| ... | ... | @@ -121,7 +121,7 @@ pprTop platform = \case |
| 121 | 121 | pprDataExterns platform lits $$
|
| 122 | 122 | pprWordArray platform (isSecConstant section) lbl lits
|
| 123 | 123 | where
|
| 124 | - isSecConstant section = case sectionProtection section of
|
|
| 124 | + isSecConstant (Section t _) = case sectionProtection t of
|
|
| 125 | 125 | ReadOnlySection -> True
|
| 126 | 126 | WriteProtectedSection -> True
|
| 127 | 127 | _ -> False
|
| ... | ... | @@ -75,7 +75,7 @@ genLlvmData (sect, statics) |
| 75 | 75 | IsFiniArray -> fsLit "llvm.global_dtors"
|
| 76 | 76 | in genGlobalLabelArray var clbls
|
| 77 | 77 | |
| 78 | -genLlvmData (sec, CmmStaticsRaw lbl xs) = do
|
|
| 78 | +genLlvmData (sec@(Section t _), CmmStaticsRaw lbl xs) = do
|
|
| 79 | 79 | label <- strCLabel_llvm lbl
|
| 80 | 80 | static <- mapM genData xs
|
| 81 | 81 | lmsec <- llvmSection sec
|
| ... | ... | @@ -92,7 +92,7 @@ genLlvmData (sec, CmmStaticsRaw lbl xs) = do |
| 92 | 92 | then Just 2 else Just 1
|
| 93 | 93 | Section Data _ -> Just $ platformWordSizeInBytes platform
|
| 94 | 94 | _ -> Nothing
|
| 95 | - const = if sectionProtection sec == ReadOnlySection
|
|
| 95 | + const = if sectionProtection t == ReadOnlySection
|
|
| 96 | 96 | then Constant else Global
|
| 97 | 97 | varDef = LMGlobalVar label tyAlias link lmsec align const
|
| 98 | 98 | globDef = LMGlobal varDef struct
|