Constant-folding: don't pass through GHC's Int/Word (fix #11704)
Constant-folding rules for integerToWord/integerToInt were performing the following coercions at compilation time: integerToWord: target's Integer -> ghc's Word -> target's Word integerToInt : target's Integer -> ghc's Int -> target's Int 1) It was wrong for cross-compilers when GHC's word size is smaller than the target one. This patch avoids passing through GHC's word-sized types: integerToWord: target's Integer -> ghc's Integer -> target's Word integerToInt : target's Integer -> ghc's Integer -> target's Int 2) Additionally we didn't wrap the target word/int literal to make it fit into the target's range! This broke the invariant of literals only containing values in range. The existing code is wrong only with a 64-bit cross-compiling GHC, targeting a 32-bit platform, and performing constant folding on a literal that doesn't fit in a 32-bit word. If GHC was built with DEBUG, the assertion in GHC.Types.Literal.mkLitWord would fail. Otherwise the bad transformation would go unnoticed.
Showing
- compiler/GHC/Core.hs 16 additions, 14 deletionscompiler/GHC/Core.hs
- compiler/GHC/Core/Make.hs 2 additions, 6 deletionscompiler/GHC/Core/Make.hs
- compiler/GHC/Core/Opt/ConstantFold.hs 8 additions, 9 deletionscompiler/GHC/Core/Opt/ConstantFold.hs
- compiler/GHC/HsToCore/Foreign/Decl.hs 1 addition, 1 deletioncompiler/GHC/HsToCore/Foreign/Decl.hs
Loading
Please register or sign in to comment