Constant folding for bit casts between word and float types
Summary
Bit casts between word and float types are not constant folded.
Steps to reproduce
import GHC.Float
import GHC.Word
foo :: Word64
foo = castDoubleToWord64 1.0
❯ ghc-9.8.1 -ddump-simpl -dno-typeable-binds -fforce-recomp -O2 Test.hs
[1 of 1] Compiling Test ( Test.hs, Test.o )
==================== Tidy Core ====================
Result size of Tidy Core
= {terms: 7, types: 2, coercions: 0, joins: 0/0}
-- RHS size: {terms: 6, types: 1, coercions: 0, joins: 0/0}
foo :: Word64
[GblId,
Unf=Unf{Src=<vanilla>, TopLvl=True,
Value=False, ConLike=False, WorkFree=False, Expandable=False,
Guidance=IF_ARGS [] 40 10}]
foo
= case {__ffi_static_ccall_safe base:stg_doubleToWord64zh "stg_doubleToWord64zh" :: Double#
-> Word64#}_aD3
1.0##
of ds1_aD6
{ __DEFAULT ->
GHC.Word.W64# ds1_aD6
}
Expected behavior
Core dump should contain:
foo = 4607182418800017408
Also we could use inline assembly instead of a call to a Cmm function (e.g. MOVD
instruction on x86-64).
What I would do:
-
Move these bitcast Cmm functions fromNot needed now: already inbase:GHC.Float
toghc-prim
ghc-internal
-
Replace foreign imports with real primops that are lowered to these Cmm calls -
Implement constant folding for the new primops when they are applied to literals -
Lower these primops to inline assembly in the native code generators (x86-64, aarch64...) -
NCG x86-64 -
NCG aarch64 -
LLVM -
C backend?
-
Environment
- GHC version used: 9.8.1
Edited by Sylvain Henry