Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC
Commits:
-
0d8e1f8d
by Sylvain Henry at 2026-11-11T13:59:05-05:00
-
5a9db1ed
by Sylvain Henry at 2026-11-11T13:59:05-05:00
9 changed files:
- + changelog.d/28101
- compiler/GHC/CmmToAsm/PPC/Ppr.hs
- compiler/GHC/CmmToAsm/X86/Ppr.hs
- compiler/GHC/CmmToC.hs
- compiler/GHC/Types/Literal/Floating.hs
- + testsuite/tests/cmm/should_run/T28101.hs
- + testsuite/tests/cmm/should_run/T28101.stdout
- + testsuite/tests/cmm/should_run/T28101_cmm.cmm
- testsuite/tests/cmm/should_run/all.T
Changes:
| 1 | +section: codegen
|
|
| 2 | +synopsis: Fix the code generation of NaN literals: the generated code did not always keep all the bits of a NaN literal.
|
|
| 3 | +mrs: !16964
|
|
| 4 | +issues: #28101 |
| ... | ... | @@ -40,6 +40,7 @@ import GHC.Utils.Panic |
| 40 | 40 | |
| 41 | 41 | import Data.Word
|
| 42 | 42 | import Data.Int
|
| 43 | +import GHC.Float (castDoubleToWord64, castFloatToWord32)
|
|
| 43 | 44 | |
| 44 | 45 | -- -----------------------------------------------------------------------------
|
| 45 | 46 | -- Printing this stuff out
|
| ... | ... | @@ -308,14 +309,15 @@ pprDataItem :: IsDoc doc => Platform -> CmmLit -> doc |
| 308 | 309 | pprDataItem platform lit
|
| 309 | 310 | = lines_ (ppr_item (cmmTypeFormat $ cmmLitType platform lit) lit)
|
| 310 | 311 | where
|
| 311 | - imm = litToImm lit
|
|
| 312 | 312 | archPPC_64 = not $ target32Bit platform
|
| 313 | 313 | |
| 314 | - ppr_item II8 _ = [text "\t.byte\t" <> pprImm platform imm]
|
|
| 315 | - ppr_item II16 _ = [text "\t.short\t" <> pprImm platform imm]
|
|
| 316 | - ppr_item II32 _ = [text "\t.long\t" <> pprImm platform imm]
|
|
| 317 | - ppr_item II64 _
|
|
| 318 | - | archPPC_64 = [text "\t.quad\t" <> pprImm platform imm]
|
|
| 314 | + ppr_imm l = pprImm platform (litToImm l)
|
|
| 315 | + |
|
| 316 | + ppr_item II8 l = [text "\t.byte\t" <> ppr_imm l]
|
|
| 317 | + ppr_item II16 l = [text "\t.short\t" <> ppr_imm l]
|
|
| 318 | + ppr_item II32 l = [text "\t.long\t" <> ppr_imm l]
|
|
| 319 | + ppr_item II64 l
|
|
| 320 | + | archPPC_64 = [text "\t.quad\t" <> ppr_imm l]
|
|
| 319 | 321 | |
| 320 | 322 | ppr_item II64 (CmmInt x _)
|
| 321 | 323 | | not archPPC_64 =
|
| ... | ... | @@ -325,18 +327,19 @@ pprDataItem platform lit |
| 325 | 327 | text "\t.long\t"
|
| 326 | 328 | <> int (fromIntegral (fromIntegral x :: Word32))]
|
| 327 | 329 | |
| 328 | - ppr_item FF32 (CmmFloat f _)
|
|
| 329 | - | litFloatingIsNonStandardNaN LitFloat f
|
|
| 330 | - = let bs = floatToBytes (litFloatingToHostFloat f)
|
|
| 331 | - in map (\b -> text "\t.byte\t" <> int (fromIntegral b)) bs
|
|
| 330 | + -- See Note [Printing NaN literals] in GHC.Types.Literal.Floating
|
|
| 331 | + ppr_item FF32 l@(CmmFloat f _)
|
|
| 332 | + | litFloatingIsNaN LitFloat f
|
|
| 333 | + , let w32 = castFloatToWord32 (litFloatingToHostFloat f)
|
|
| 334 | + = ppr_item II32 (CmmInt (toInteger w32) W32)
|
|
| 332 | 335 | | otherwise
|
| 333 | - = [text "\t.float\t" <> pprImm platform imm]
|
|
| 334 | - ppr_item FF64 (CmmFloat f _)
|
|
| 335 | - | litFloatingIsNonStandardNaN LitDouble f
|
|
| 336 | - = let bs = doubleToBytes (litFloatingToHostDouble f)
|
|
| 337 | - in map (\b -> text "\t.byte\t" <> int (fromIntegral b)) bs
|
|
| 336 | + = [text "\t.float\t" <> ppr_imm l]
|
|
| 337 | + ppr_item FF64 l@(CmmFloat f _)
|
|
| 338 | + | litFloatingIsNaN LitDouble f
|
|
| 339 | + , let w64 = castDoubleToWord64 (litFloatingToHostDouble f)
|
|
| 340 | + = ppr_item II64 (CmmInt (toInteger w64) W64)
|
|
| 338 | 341 | | otherwise
|
| 339 | - = [text "\t.double\t" <> pprImm platform imm]
|
|
| 342 | + = [text "\t.double\t" <> ppr_imm l]
|
|
| 340 | 343 | |
| 341 | 344 | ppr_item _ _
|
| 342 | 345 | = panic "PPC.Ppr.pprDataItem: no match"
|
| ... | ... | @@ -618,12 +618,13 @@ pprDataItem config lit = |
| 618 | 618 | -> (II32, ) <$> pprAsII32x2 lit
|
| 619 | 619 | FF32
|
| 620 | 620 | | CmmFloat f _ <- lit
|
| 621 | - , litFloatingIsNonStandardNaN LitFloat f
|
|
| 621 | + , litFloatingIsNaN LitFloat f
|
|
| 622 | + -- See Note [Printing NaN literals] in GHC.Types.Literal.Floating
|
|
| 622 | 623 | , let w32 = castFloatToWord32 (litFloatingToHostFloat f)
|
| 623 | 624 | -> [(II32, ppr_lit (CmmInt (fromIntegral w32) W32))]
|
| 624 | 625 | FF64
|
| 625 | 626 | | CmmFloat f _ <- lit
|
| 626 | - , litFloatingIsNonStandardNaN LitDouble f
|
|
| 627 | + , litFloatingIsNaN LitDouble f
|
|
| 627 | 628 | , let w64 = castDoubleToWord64 (litFloatingToHostDouble f)
|
| 628 | 629 | ilit = CmmInt (fromIntegral w64) W64
|
| 629 | 630 | -> if | OSDarwin <- platformOS platform
|
| ... | ... | @@ -584,12 +584,20 @@ pprLit :: Platform -> CmmLit -> SDoc |
| 584 | 584 | pprLit platform lit = case lit of
|
| 585 | 585 | CmmInt i rep -> pprHexVal platform i rep
|
| 586 | 586 | |
| 587 | - CmmFloat f fty -> parens (machRep_F_CType w) <> str
|
|
| 587 | + CmmFloat f fty
|
|
| 588 | + -- See Note [Printing NaN literals] in GHC.Types.Literal.Floating
|
|
| 589 | + | litFloatingIsNaN fty f
|
|
| 590 | + -> case fty of
|
|
| 591 | + LitFloat -> text "hs_bitcastword2float"
|
|
| 592 | + <> parens (pprLit platform (floatToWord32 f))
|
|
| 593 | + LitDouble -> text "hs_bitcastword642double"
|
|
| 594 | + <> parens (pprLit platform (doubleToWord64 f))
|
|
| 595 | + | otherwise
|
|
| 596 | + -> parens (machRep_F_CType w) <> str
|
|
| 588 | 597 | where w = litFloatingTypeWidth fty
|
| 589 | 598 | d = litFloatingToHostDouble f
|
| 590 | 599 | str | isInfinite d && d < 0 = text "-INFINITY"
|
| 591 | 600 | | isInfinite d = text "INFINITY"
|
| 592 | - | isNaN d = text "NAN"
|
|
| 593 | 601 | | otherwise = text (show d)
|
| 594 | 602 | -- these constants come from <math.h>
|
| 595 | 603 | -- see #1861
|
| ... | ... | @@ -21,7 +21,7 @@ module GHC.Types.Literal.Floating |
| 21 | 21 | , encodeLitFloat, encodeLitDouble
|
| 22 | 22 | |
| 23 | 23 | , isZeroLF, isPositiveZeroLF, isPositiveLF, isOneLF, isFiniteLF
|
| 24 | - , isPositiveZero, litFloatingIsNonStandardNaN
|
|
| 24 | + , isPositiveZero, litFloatingIsNaN
|
|
| 25 | 25 | ) where
|
| 26 | 26 | |
| 27 | 27 | import GHC.Prelude
|
| ... | ... | @@ -65,15 +65,32 @@ data ConstantFoldingPrecision |
| 65 | 65 | | DoublePrecision
|
| 66 | 66 | | ExcessPrecision
|
| 67 | 67 | |
| 68 | -litFloatingIsNonStandardNaN :: LitFloatingType -> LitFloating -> Bool
|
|
| 69 | -litFloatingIsNonStandardNaN ty lit =
|
|
| 68 | +-- | Is the literal a NaN (any sign, quiet bit or payload)?
|
|
| 69 | +--
|
|
| 70 | +-- See Note [Printing NaN literals].
|
|
| 71 | +litFloatingIsNaN :: LitFloatingType -> LitFloating -> Bool
|
|
| 72 | +litFloatingIsNaN ty lit =
|
|
| 70 | 73 | case ty of
|
| 71 | - LitFloat -> isNaN f && w /= 0x7FC00000
|
|
| 72 | - where f = litFloatingToHostFloat lit
|
|
| 73 | - w = castFloatToWord32 f
|
|
| 74 | - LitDouble -> isNaN f && w /= 0x7FF8000000000000
|
|
| 75 | - where f = litFloatingToHostDouble lit
|
|
| 76 | - w = castDoubleToWord64 f
|
|
| 74 | + LitFloat -> isNaN (litFloatingToHostFloat lit)
|
|
| 75 | + LitDouble -> isNaN (litFloatingToHostDouble lit)
|
|
| 76 | + |
|
| 77 | +-- Note [Printing NaN literals]
|
|
| 78 | +-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
| 79 | +-- Code generators that print a floating-point literal as text (assembly
|
|
| 80 | +-- or C) must not print a NaN as "NaN" or "NAN". Print the bits of the NaN
|
|
| 81 | +-- as an integer instead:
|
|
| 82 | +--
|
|
| 83 | +-- * NCG data sections: ".long"/".quad" instead of ".float"/".double".
|
|
| 84 | +-- * C backend: hs_bitcastword2float(0x7fc00000U), etc.
|
|
| 85 | +--
|
|
| 86 | +-- The assembler or the C compiler does not keep the sign and payload of
|
|
| 87 | +-- such a NaN. Also, its "standard" NaN is not ours: GNU as and clang encode
|
|
| 88 | +-- ".double NaN" as 0x7FFFFFFFFFFFFFFF, not as 0x7FF8000000000000 like we do.
|
|
| 89 | +-- Thus this Cmm code (#28101):
|
|
| 90 | +--
|
|
| 91 | +-- %f2w_bitcast(%w2f_bitcast(0x7FF8000000000000 :: bits64))
|
|
| 92 | +--
|
|
| 93 | +-- returned 0x7FFFFFFFFFFFFFFF after constant folding.
|
|
| 77 | 94 | |
| 78 | 95 | pprLitFloating :: LitFloatingType -> LitFloating -> SDoc
|
| 79 | 96 | pprLitFloating ty lit = getPprDebug $ \ dbg -> getPprStyle $ \ sty ->
|
| 1 | +{-# LANGUAGE GHCForeignImportPrim, MagicHash, UnliftedFFITypes #-}
|
|
| 2 | + |
|
| 3 | +-- Regression test for #28101. See T28101_cmm.cmm.
|
|
| 4 | + |
|
| 5 | +import GHC.Exts
|
|
| 6 | +import Numeric (showHex)
|
|
| 7 | + |
|
| 8 | +foreign import prim "T28101_f32_qnan" f32_qnan :: Word# -> Word#
|
|
| 9 | +foreign import prim "T28101_f32_neg_qnan" f32_neg_qnan :: Word# -> Word#
|
|
| 10 | +foreign import prim "T28101_f32_payload" f32_payload :: Word# -> Word#
|
|
| 11 | +foreign import prim "T28101_f64_qnan" f64_qnan :: Word# -> Word#
|
|
| 12 | +foreign import prim "T28101_f64_neg_qnan" f64_neg_qnan :: Word# -> Word#
|
|
| 13 | +foreign import prim "T28101_f64_payload" f64_payload :: Word# -> Word#
|
|
| 14 | + |
|
| 15 | +run :: String -> (Word# -> Word#) -> IO ()
|
|
| 16 | +run name f = putStrLn (name ++ ": 0x" ++ showHex (W# (f 0##)) "")
|
|
| 17 | + |
|
| 18 | +main :: IO ()
|
|
| 19 | +main = do
|
|
| 20 | + run "f32_qnan" f32_qnan
|
|
| 21 | + run "f32_neg_qnan" f32_neg_qnan
|
|
| 22 | + run "f32_payload" f32_payload
|
|
| 23 | + run "f64_qnan" f64_qnan
|
|
| 24 | + run "f64_neg_qnan" f64_neg_qnan
|
|
| 25 | + run "f64_payload" f64_payload |
| 1 | +f32_qnan: 0x7fc00000
|
|
| 2 | +f32_neg_qnan: 0xffc00000
|
|
| 3 | +f32_payload: 0x7fc00001
|
|
| 4 | +f64_qnan: 0x7ff8000000000000
|
|
| 5 | +f64_neg_qnan: 0xfff8000000000000
|
|
| 6 | +f64_payload: 0x7ff8000000000001 |
| 1 | +#include "Cmm.h"
|
|
| 2 | + |
|
| 3 | +// Regression test for #28101: the bit-cast of a NaN constant is folded into a
|
|
| 4 | +// float literal. The code generator must keep all the bits of the NaN.
|
|
| 5 | +// See Note [Printing NaN literals] in GHC.Types.Literal.Floating.
|
|
| 6 | + |
|
| 7 | +#define ROUNDTRIP32(name,w) \
|
|
| 8 | + name (bits64 unused) { \
|
|
| 9 | + bits32 r; \
|
|
| 10 | + bits64 ret; \
|
|
| 11 | + r = %f2w_bitcast(%w2f_bitcast((w :: bits32))); \
|
|
| 12 | + ret = %zx64(r); \
|
|
| 13 | + return (ret); \
|
|
| 14 | + }
|
|
| 15 | + |
|
| 16 | +#define ROUNDTRIP64(name,w) \
|
|
| 17 | + name (bits64 unused) { \
|
|
| 18 | + bits64 r; \
|
|
| 19 | + r = %f2w_bitcast(%w2f_bitcast((w :: bits64))); \
|
|
| 20 | + return (r); \
|
|
| 21 | + }
|
|
| 22 | + |
|
| 23 | +ROUNDTRIP32(T28101_f32_qnan, 0x7fc00000)
|
|
| 24 | +ROUNDTRIP32(T28101_f32_neg_qnan, 0xffc00000)
|
|
| 25 | +ROUNDTRIP32(T28101_f32_payload, 0x7fc00001)
|
|
| 26 | + |
|
| 27 | +ROUNDTRIP64(T28101_f64_qnan, 0x7ff8000000000000)
|
|
| 28 | +ROUNDTRIP64(T28101_f64_neg_qnan, 0xfff8000000000000)
|
|
| 29 | +ROUNDTRIP64(T28101_f64_payload, 0x7ff8000000000001) |
| ... | ... | @@ -63,3 +63,11 @@ test('T27447', |
| 63 | 63 | |
| 64 | 64 | test('T27722', [req_cmm], multi_compile_and_run,
|
| 65 | 65 | ['T27722', [('T27722_cmm.cmm', '')], '-O'])
|
| 66 | + |
|
| 67 | +test('T28101',
|
|
| 68 | + [ req_cmm
|
|
| 69 | + , when(wordsize(32), skip) # 64-bit Cmm literals returned in a Word#
|
|
| 70 | + , extra_ways(['optasm'])
|
|
| 71 | + ],
|
|
| 72 | + multi_compile_and_run,
|
|
| 73 | + ['T28101', [('T28101_cmm.cmm', '')], '']) |