Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC Commits: 0d8e1f8d by Sylvain Henry at 2026-11-11T13:59:05-05:00 PPC NCG: print non-standard NaN literals in target byte order The PowerPC NCG printed a non-standard NaN literal byte by byte with floatToBytes/doubleToBytes, which use the byte order of the host. This is wrong when the host and the target have a different endianness (e.g. from x86_64 to big-endian PowerPC). Print the bits with `.long`/`.quad` instead, so that the assembler uses the target byte order. Also make `ppr_item` print its argument: some clauses printed the literal of `pprDataItem` instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> - - - - - 5a9db1ed by Sylvain Henry at 2026-11-11T13:59:05-05:00 Print all NaN literals as integer bits (#28101) The x86 and PowerPC NCGs printed the quiet NaNs 0x7fc00000 and 0x7ff8000000000000 as `.float NaN`/`.double NaN`. GNU as and clang encode these with all mantissa bits set, so constant-folded bit casts of these values gave 0x7fffffff/0x7fffffffffffffff. The C backend printed every NaN as `NAN`, losing its sign and payload. Now the NCGs print all NaNs with `.long`/`.quad` and the C backend with hs_bitcastword2float/hs_bitcastword642double. See Note [Printing NaN literals]. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> - - - - - 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: ===================================== changelog.d/28101 ===================================== @@ -0,0 +1,4 @@ +section: codegen +synopsis: Fix the code generation of NaN literals: the generated code did not always keep all the bits of a NaN literal. +mrs: !16964 +issues: #28101 ===================================== compiler/GHC/CmmToAsm/PPC/Ppr.hs ===================================== @@ -40,6 +40,7 @@ import GHC.Utils.Panic import Data.Word import Data.Int +import GHC.Float (castDoubleToWord64, castFloatToWord32) -- ----------------------------------------------------------------------------- -- Printing this stuff out @@ -308,14 +309,15 @@ pprDataItem :: IsDoc doc => Platform -> CmmLit -> doc pprDataItem platform lit = lines_ (ppr_item (cmmTypeFormat $ cmmLitType platform lit) lit) where - imm = litToImm lit archPPC_64 = not $ target32Bit platform - ppr_item II8 _ = [text "\t.byte\t" <> pprImm platform imm] - ppr_item II16 _ = [text "\t.short\t" <> pprImm platform imm] - ppr_item II32 _ = [text "\t.long\t" <> pprImm platform imm] - ppr_item II64 _ - | archPPC_64 = [text "\t.quad\t" <> pprImm platform imm] + ppr_imm l = pprImm platform (litToImm l) + + ppr_item II8 l = [text "\t.byte\t" <> ppr_imm l] + ppr_item II16 l = [text "\t.short\t" <> ppr_imm l] + ppr_item II32 l = [text "\t.long\t" <> ppr_imm l] + ppr_item II64 l + | archPPC_64 = [text "\t.quad\t" <> ppr_imm l] ppr_item II64 (CmmInt x _) | not archPPC_64 = @@ -325,18 +327,19 @@ pprDataItem platform lit text "\t.long\t" <> int (fromIntegral (fromIntegral x :: Word32))] - ppr_item FF32 (CmmFloat f _) - | litFloatingIsNonStandardNaN LitFloat f - = let bs = floatToBytes (litFloatingToHostFloat f) - in map (\b -> text "\t.byte\t" <> int (fromIntegral b)) bs + -- See Note [Printing NaN literals] in GHC.Types.Literal.Floating + ppr_item FF32 l@(CmmFloat f _) + | litFloatingIsNaN LitFloat f + , let w32 = castFloatToWord32 (litFloatingToHostFloat f) + = ppr_item II32 (CmmInt (toInteger w32) W32) | otherwise - = [text "\t.float\t" <> pprImm platform imm] - ppr_item FF64 (CmmFloat f _) - | litFloatingIsNonStandardNaN LitDouble f - = let bs = doubleToBytes (litFloatingToHostDouble f) - in map (\b -> text "\t.byte\t" <> int (fromIntegral b)) bs + = [text "\t.float\t" <> ppr_imm l] + ppr_item FF64 l@(CmmFloat f _) + | litFloatingIsNaN LitDouble f + , let w64 = castDoubleToWord64 (litFloatingToHostDouble f) + = ppr_item II64 (CmmInt (toInteger w64) W64) | otherwise - = [text "\t.double\t" <> pprImm platform imm] + = [text "\t.double\t" <> ppr_imm l] ppr_item _ _ = panic "PPC.Ppr.pprDataItem: no match" ===================================== compiler/GHC/CmmToAsm/X86/Ppr.hs ===================================== @@ -618,12 +618,13 @@ pprDataItem config lit = -> (II32, ) <$> pprAsII32x2 lit FF32 | CmmFloat f _ <- lit - , litFloatingIsNonStandardNaN LitFloat f + , litFloatingIsNaN LitFloat f + -- See Note [Printing NaN literals] in GHC.Types.Literal.Floating , let w32 = castFloatToWord32 (litFloatingToHostFloat f) -> [(II32, ppr_lit (CmmInt (fromIntegral w32) W32))] FF64 | CmmFloat f _ <- lit - , litFloatingIsNonStandardNaN LitDouble f + , litFloatingIsNaN LitDouble f , let w64 = castDoubleToWord64 (litFloatingToHostDouble f) ilit = CmmInt (fromIntegral w64) W64 -> if | OSDarwin <- platformOS platform ===================================== compiler/GHC/CmmToC.hs ===================================== @@ -584,12 +584,20 @@ pprLit :: Platform -> CmmLit -> SDoc pprLit platform lit = case lit of CmmInt i rep -> pprHexVal platform i rep - CmmFloat f fty -> parens (machRep_F_CType w) <> str + CmmFloat f fty + -- See Note [Printing NaN literals] in GHC.Types.Literal.Floating + | litFloatingIsNaN fty f + -> case fty of + LitFloat -> text "hs_bitcastword2float" + <> parens (pprLit platform (floatToWord32 f)) + LitDouble -> text "hs_bitcastword642double" + <> parens (pprLit platform (doubleToWord64 f)) + | otherwise + -> parens (machRep_F_CType w) <> str where w = litFloatingTypeWidth fty d = litFloatingToHostDouble f str | isInfinite d && d < 0 = text "-INFINITY" | isInfinite d = text "INFINITY" - | isNaN d = text "NAN" | otherwise = text (show d) -- these constants come from <math.h> -- see #1861 ===================================== compiler/GHC/Types/Literal/Floating.hs ===================================== @@ -21,7 +21,7 @@ module GHC.Types.Literal.Floating , encodeLitFloat, encodeLitDouble , isZeroLF, isPositiveZeroLF, isPositiveLF, isOneLF, isFiniteLF - , isPositiveZero, litFloatingIsNonStandardNaN + , isPositiveZero, litFloatingIsNaN ) where import GHC.Prelude @@ -65,15 +65,32 @@ data ConstantFoldingPrecision | DoublePrecision | ExcessPrecision -litFloatingIsNonStandardNaN :: LitFloatingType -> LitFloating -> Bool -litFloatingIsNonStandardNaN ty lit = +-- | Is the literal a NaN (any sign, quiet bit or payload)? +-- +-- See Note [Printing NaN literals]. +litFloatingIsNaN :: LitFloatingType -> LitFloating -> Bool +litFloatingIsNaN ty lit = case ty of - LitFloat -> isNaN f && w /= 0x7FC00000 - where f = litFloatingToHostFloat lit - w = castFloatToWord32 f - LitDouble -> isNaN f && w /= 0x7FF8000000000000 - where f = litFloatingToHostDouble lit - w = castDoubleToWord64 f + LitFloat -> isNaN (litFloatingToHostFloat lit) + LitDouble -> isNaN (litFloatingToHostDouble lit) + +-- Note [Printing NaN literals] +-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +-- Code generators that print a floating-point literal as text (assembly +-- or C) must not print a NaN as "NaN" or "NAN". Print the bits of the NaN +-- as an integer instead: +-- +-- * NCG data sections: ".long"/".quad" instead of ".float"/".double". +-- * C backend: hs_bitcastword2float(0x7fc00000U), etc. +-- +-- The assembler or the C compiler does not keep the sign and payload of +-- such a NaN. Also, its "standard" NaN is not ours: GNU as and clang encode +-- ".double NaN" as 0x7FFFFFFFFFFFFFFF, not as 0x7FF8000000000000 like we do. +-- Thus this Cmm code (#28101): +-- +-- %f2w_bitcast(%w2f_bitcast(0x7FF8000000000000 :: bits64)) +-- +-- returned 0x7FFFFFFFFFFFFFFF after constant folding. pprLitFloating :: LitFloatingType -> LitFloating -> SDoc pprLitFloating ty lit = getPprDebug $ \ dbg -> getPprStyle $ \ sty -> ===================================== testsuite/tests/cmm/should_run/T28101.hs ===================================== @@ -0,0 +1,25 @@ +{-# LANGUAGE GHCForeignImportPrim, MagicHash, UnliftedFFITypes #-} + +-- Regression test for #28101. See T28101_cmm.cmm. + +import GHC.Exts +import Numeric (showHex) + +foreign import prim "T28101_f32_qnan" f32_qnan :: Word# -> Word# +foreign import prim "T28101_f32_neg_qnan" f32_neg_qnan :: Word# -> Word# +foreign import prim "T28101_f32_payload" f32_payload :: Word# -> Word# +foreign import prim "T28101_f64_qnan" f64_qnan :: Word# -> Word# +foreign import prim "T28101_f64_neg_qnan" f64_neg_qnan :: Word# -> Word# +foreign import prim "T28101_f64_payload" f64_payload :: Word# -> Word# + +run :: String -> (Word# -> Word#) -> IO () +run name f = putStrLn (name ++ ": 0x" ++ showHex (W# (f 0##)) "") + +main :: IO () +main = do + run "f32_qnan" f32_qnan + run "f32_neg_qnan" f32_neg_qnan + run "f32_payload" f32_payload + run "f64_qnan" f64_qnan + run "f64_neg_qnan" f64_neg_qnan + run "f64_payload" f64_payload ===================================== testsuite/tests/cmm/should_run/T28101.stdout ===================================== @@ -0,0 +1,6 @@ +f32_qnan: 0x7fc00000 +f32_neg_qnan: 0xffc00000 +f32_payload: 0x7fc00001 +f64_qnan: 0x7ff8000000000000 +f64_neg_qnan: 0xfff8000000000000 +f64_payload: 0x7ff8000000000001 ===================================== testsuite/tests/cmm/should_run/T28101_cmm.cmm ===================================== @@ -0,0 +1,29 @@ +#include "Cmm.h" + +// Regression test for #28101: the bit-cast of a NaN constant is folded into a +// float literal. The code generator must keep all the bits of the NaN. +// See Note [Printing NaN literals] in GHC.Types.Literal.Floating. + +#define ROUNDTRIP32(name,w) \ + name (bits64 unused) { \ + bits32 r; \ + bits64 ret; \ + r = %f2w_bitcast(%w2f_bitcast((w :: bits32))); \ + ret = %zx64(r); \ + return (ret); \ + } + +#define ROUNDTRIP64(name,w) \ + name (bits64 unused) { \ + bits64 r; \ + r = %f2w_bitcast(%w2f_bitcast((w :: bits64))); \ + return (r); \ + } + +ROUNDTRIP32(T28101_f32_qnan, 0x7fc00000) +ROUNDTRIP32(T28101_f32_neg_qnan, 0xffc00000) +ROUNDTRIP32(T28101_f32_payload, 0x7fc00001) + +ROUNDTRIP64(T28101_f64_qnan, 0x7ff8000000000000) +ROUNDTRIP64(T28101_f64_neg_qnan, 0xfff8000000000000) +ROUNDTRIP64(T28101_f64_payload, 0x7ff8000000000001) ===================================== testsuite/tests/cmm/should_run/all.T ===================================== @@ -63,3 +63,11 @@ test('T27447', test('T27722', [req_cmm], multi_compile_and_run, ['T27722', [('T27722_cmm.cmm', '')], '-O']) + +test('T28101', + [ req_cmm + , when(wordsize(32), skip) # 64-bit Cmm literals returned in a Word# + , extra_ways(['optasm']) + ], + multi_compile_and_run, + ['T28101', [('T28101_cmm.cmm', '')], '']) View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/744b30365b9178fddc55e55ba6a2560... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/744b30365b9178fddc55e55ba6a2560... You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help