[Git][ghc/ghc][master] Bignum: match on small Integer/Natural

Marge Bot gitlab at gitlab.haskell.org
Fri Oct 9 12:47:04 UTC 2020



 Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC


Commits:
12191a99 by Sylvain Henry at 2020-10-09T08:47:00-04:00
Bignum: match on small Integer/Natural

Previously we only matched on *variables* whose unfoldings were a ConApp
of the form `IS lit#` or `NS lit##`. But we forgot to match on the
ConApp directly... As a consequence, constant folding only worked after
the FloatOut pass which creates bindings for most sub-expressions. With
this patch, matching on bignums works even with -O0 (see bignumMatch
test).

- - - - -


4 changed files:

- compiler/GHC/Core/SimpleOpt.hs
- testsuite/tests/lib/integer/all.T
- + testsuite/tests/lib/integer/bignumMatch.hs
- + testsuite/tests/lib/integer/bignumMatch.stderr


Changes:

=====================================
compiler/GHC/Core/SimpleOpt.hs
=====================================
@@ -1279,13 +1279,24 @@ exprIsLiteral_maybe env@(_, id_unf) e
          -> Just l
       Var v
          | Just rhs <- expandUnfolding_maybe (id_unf v)
-         , Just (_env,_fb,dc,_tys,[arg]) <- exprIsConApp_maybe env rhs
+         , Just b <- matchBignum env rhs
+         -> Just b
+      e
+         | Just b <- matchBignum env e
+         -> Just b
+
+         | otherwise
+         -> Nothing
+  where
+    matchBignum env e
+         | Just (_env,_fb,dc,_tys,[arg]) <- exprIsConApp_maybe env e
          , Just (LitNumber _ i) <- exprIsLiteral_maybe env arg
-         -> if
+         = if
             | dc == naturalNSDataCon -> Just (mkLitNatural i)
             | dc == integerISDataCon -> Just (mkLitInteger i)
             | otherwise              -> Nothing
-      _         -> Nothing
+         | otherwise
+         = Nothing
 
 {-
 Note [exprIsLambda_maybe]


=====================================
testsuite/tests/lib/integer/all.T
=====================================
@@ -10,6 +10,7 @@ test('gcdeInteger', normal, compile_and_run, [''])
 test('integerPowMod', [], compile_and_run, [''])
 test('integerGcdExt', [], compile_and_run, [''])
 test('integerRecipMod', [], compile_and_run, [''])
+test('bignumMatch', [], compile, [''])
 
 # skip ghci as it doesn't support unboxed tuples
 test('integerImportExport', [omit_ways(['ghci'])], compile_and_run, [''])


=====================================
testsuite/tests/lib/integer/bignumMatch.hs
=====================================
@@ -0,0 +1,9 @@
+{-# LANGUAGE MagicHash #-}
+{-# OPTIONS_GHC -ddump-rule-firings -O0 -v0 #-}
+
+module Test where
+
+import GHC.Num.Integer
+
+foo :: Integer
+foo = IS 45# `integerAdd` (IS 0# `integerMul` IS 18#)


=====================================
testsuite/tests/lib/integer/bignumMatch.stderr
=====================================
@@ -0,0 +1,2 @@
+Rule fired: integerMul (BUILTIN)
+Rule fired: integerAdd (BUILTIN)



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/12191a99d3b978b697ec0fb4412276fbea5dce8f

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/12191a99d3b978b697ec0fb4412276fbea5dce8f
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20201009/db49b908/attachment-0001.html>


More information about the ghc-commits mailing list