GHC issues
https://gitlab.haskell.org/ghc/ghc/-/issues
2020-09-30T20:15:39Z
https://gitlab.haskell.org/ghc/ghc/-/issues/15350
gcdExtInteger violates assertion
2020-09-30T20:15:39Z
Bodigrim
gcdExtInteger violates assertion
```haskell
{-# LANGUAGE UnboxedTuples #-}
import GHC.Integer.GMP.Internals
main = let (# _, s #) = gcdExtInteger 2 (2^65 + 1) in print s
```
fails with
```haskell
Assertion failed: (sn <= mp_size_abs(xn)), function integer_gmp_gcdext,...
```haskell
{-# LANGUAGE UnboxedTuples #-}
import GHC.Integer.GMP.Internals
main = let (# _, s #) = gcdExtInteger 2 (2^65 + 1) in print s
```
fails with
```haskell
Assertion failed: (sn <= mp_size_abs(xn)), function integer_gmp_gcdext, file libraries/integer-gmp/cbits/wrappers.c, line 316.
Abort trap: 6
```
It happens because `s = -2^64` and does not fit one-limbed `BigNat`. The implementation of `gcdExtInteger x y` (https://github.com/ghc/ghc/blob/master/libraries/integer-gmp/src/GHC/Integer/Type.hs\#L1392) allocates for `s` a buffer, equal to size of `x` (one limb in our case), but according to GMP manual (https://gmplib.org/manual/Number-Theoretic-Functions.html\#index-mpz_005fgcdext) it should be equal to size of `y` (two limbs in our case).
Hopefully, the diff is simple enough for a PR on GitHub (https://github.com/ghc/ghc/pull/163). Otherwise I'll be happy to prepare a patch for Phabricator.
```diff
- s@(MBN# s#) <- newBigNat# (absI# xn#)
+ s@(MBN# s#) <- newBigNat# (absI# yn#)
```
---
Reopening, because
```haskell
{-# LANGUAGE UnboxedTuples #-}
import GHC.Integer.GMP.Internals
main = let (# _, s #) = gcdExtInteger (- (2^63 - 1) * 2^63) 0 in print s
```
fails in GHC 8.6.1 with
```haskell
Assertion failed: (0 <= gn && gn <= gn0), function integer_gmp_gcdext, file libraries/integer-gmp/cbits/wrappers.c, line 309.
Abort trap: 6
```
I have not yet understood what is going on.
8.6.1
Bodigrim
Bodigrim
https://gitlab.haskell.org/ghc/ghc/-/issues/14170
8.2.1 regression: GHC fails to simplify `natVal`
2019-07-07T18:18:10Z
Alexey Vagarenko
8.2.1 regression: GHC fails to simplify `natVal`
When GHC 8.2.1 compiles this code with `-O`:
```hs
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeInType #-}
module NatVal where
import Data.Proxy
import GHC.TypeLits
foo = natVal $ Proxy @0
```
it produces the f...
When GHC 8.2.1 compiles this code with `-O`:
```hs
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeInType #-}
module NatVal where
import Data.Proxy
import GHC.TypeLits
foo = natVal $ Proxy @0
```
it produces the following Core:
```hs
-- RHS size: {terms: 1, types: 0, coercions: 0, joins: 0/0}
NatVal.foo1 :: Integer
NatVal.foo1 = 0
-- RHS size: {terms: 41, types: 18, coercions: 0, joins: 0/0}
foo :: Integer
foo
= case NatVal.foo1 of wild_a1iV {
integer-gmp-1.0.1.0:GHC.Integer.Type.S# i#_a2ke ->
case GHC.Prim.tagToEnum# @ Bool (GHC.Prim.>=# i#_a2ke 0#) of {
False -> case GHC.Natural.underflowError of wild2_00 { };
True ->
integer-gmp-1.0.1.0:GHC.Integer.Type.wordToInteger
(GHC.Prim.int2Word# i#_a2ke)
};
integer-gmp-1.0.1.0:GHC.Integer.Type.Jp# dt_a2km ->
case GHC.Prim.uncheckedIShiftRL#
(GHC.Prim.sizeofByteArray# dt_a2km) 3#
of {
__DEFAULT ->
case GHC.Prim.sizeofByteArray# dt_a2km of {
__DEFAULT -> wild_a1iV;
0# -> case GHC.Natural.underflowError of wild4_00 { }
};
1# ->
case GHC.Prim.indexWordArray# dt_a2km 0# of wild2_a2kq
{ __DEFAULT ->
integer-gmp-1.0.1.0:GHC.Integer.Type.wordToInteger wild2_a2kq
}
};
integer-gmp-1.0.1.0:GHC.Integer.Type.Jn# ipv_a2kt ->
case GHC.Natural.underflowError of wild1_00 { }
}
```
while GHC-8.0.1 does the right thing:
```hs
-- RHS size: {terms: 1, types: 0, coercions: 0}
foo :: Integer
foo = 0
```
<details><summary>Trac metadata</summary>
| Trac field | Value |
| ---------------------- | ------------ |
| Version | 8.2.1 |
| Type | Bug |
| TypeOfFailure | OtherFailure |
| Priority | high |
| Resolution | Unresolved |
| Component | Compiler |
| Test case | |
| Differential revisions | |
| BlockedBy | |
| Related | |
| Blocking | |
| CC | |
| Operating system | |
| Architecture | |
</details>
<!-- {"blocked_by":[],"summary":"8.2.1 regression: GHC fails to simplify `natVal`","status":"New","operating_system":"","component":"Compiler","related":[],"milestone":"8.2.2","resolution":"Unresolved","owner":{"tag":"Unowned"},"version":"8.2.1","keywords":[],"differentials":[],"test_case":"","architecture":"","cc":[""],"type":"Bug","description":"When GHC 8.2.1 compiles this code with `-O`:\r\n{{{#!hs\r\n{-# LANGUAGE TypeApplications #-}\r\n{-# LANGUAGE TypeInType #-}\r\n\r\nmodule NatVal where\r\n\r\nimport Data.Proxy\r\nimport GHC.TypeLits\r\n\r\nfoo = natVal $ Proxy @0\r\n}}}\r\n\r\nit produces the following Core:\r\n\r\n{{{#!hs\r\n-- RHS size: {terms: 1, types: 0, coercions: 0, joins: 0/0}\r\nNatVal.foo1 :: Integer\r\nNatVal.foo1 = 0\r\n\r\n-- RHS size: {terms: 41, types: 18, coercions: 0, joins: 0/0}\r\nfoo :: Integer\r\nfoo\r\n = case NatVal.foo1 of wild_a1iV {\r\n integer-gmp-1.0.1.0:GHC.Integer.Type.S# i#_a2ke ->\r\n case GHC.Prim.tagToEnum# @ Bool (GHC.Prim.>=# i#_a2ke 0#) of {\r\n False -> case GHC.Natural.underflowError of wild2_00 { };\r\n True ->\r\n integer-gmp-1.0.1.0:GHC.Integer.Type.wordToInteger\r\n (GHC.Prim.int2Word# i#_a2ke)\r\n };\r\n integer-gmp-1.0.1.0:GHC.Integer.Type.Jp# dt_a2km ->\r\n case GHC.Prim.uncheckedIShiftRL#\r\n (GHC.Prim.sizeofByteArray# dt_a2km) 3#\r\n of {\r\n __DEFAULT ->\r\n case GHC.Prim.sizeofByteArray# dt_a2km of {\r\n __DEFAULT -> wild_a1iV;\r\n 0# -> case GHC.Natural.underflowError of wild4_00 { }\r\n };\r\n 1# ->\r\n case GHC.Prim.indexWordArray# dt_a2km 0# of wild2_a2kq\r\n { __DEFAULT ->\r\n integer-gmp-1.0.1.0:GHC.Integer.Type.wordToInteger wild2_a2kq\r\n }\r\n };\r\n integer-gmp-1.0.1.0:GHC.Integer.Type.Jn# ipv_a2kt ->\r\n case GHC.Natural.underflowError of wild1_00 { }\r\n }\r\n}}}\r\n\r\nwhile GHC-8.0.1 does the right thing:\r\n{{{#!hs\r\n-- RHS size: {terms: 1, types: 0, coercions: 0}\r\nfoo :: Integer\r\nfoo = 0\r\n}}}\r\n","type_of_failure":"OtherFailure","blocking":[]} -->
8.6.1
Bodigrim
Bodigrim