[Git][ghc/ghc][wip/andreask/inline_div] Always inline divInt and modInt in phase zero.

Andreas Klebinger gitlab at gitlab.haskell.org
Sun Apr 19 10:27:54 UTC 2020



Andreas Klebinger pushed to branch wip/andreask/inline_div at Glasgow Haskell Compiler / GHC


Commits:
0ed37369 by Andreas Klebinger at 2020-04-19T12:27:44+02:00
Always inline divInt and modInt in phase zero.

This prevents the overhead of a function call for
operations which really only need to be a few instructions.

By always inlining them in phase zero we can:
* Match on them via rules in earlier phases.
* Can constant fold on them in phase zero by
  rules on their underlying primitives.

This fixes #18067

- - - - -


2 changed files:

- libraries/base/GHC/Base.hs
- libraries/ghc-prim/GHC/Classes.hs


Changes:

=====================================
libraries/base/GHC/Base.hs
=====================================
@@ -1545,9 +1545,36 @@ getTag x = dataToTag# x
 -- Definitions of the boxed PrimOps; these will be
 -- used in the case of partial applications, etc.
 
+{- Note [Inlining divInt, modInt]
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+   divInt and modInt are implemented by calling unboxed
+   variants, which themselves are implemented in terms of
+   the quotInt#/remInt# primOps.
+
+   My marking the boxed versions as INLINE[1] we achieve two things:
+
+   * The strength reduction rules which are operating on divInt#/remInt#
+     can already fire in phase 1.
+   * We can inline any constant argument in Phase zero optimizing the
+     general case somewhat.
+
+   This solves #18067 where we observed divInt ending up
+   as a uninlined call to divInt# at times.
+
+   TODO: It might be good to apply the same pattern to
+   quotRemInt and divModInt. But I have not looked at this
+   yet.
+
+-}
+
 {-# INLINE quotInt #-}
 {-# INLINE remInt #-}
 
+-- See Note [Inlining divInt, modInt]
+{-# INLINE[1] divInt #-}
+{-# INLINE[1] modInt #-}
+
 quotInt, remInt, divInt, modInt :: Int -> Int -> Int
 (I# x) `quotInt`  (I# y) = I# (x `quotInt#` y)
 (I# x) `remInt`   (I# y) = I# (x `remInt#`  y)


=====================================
libraries/ghc-prim/GHC/Classes.hs
=====================================
@@ -545,8 +545,8 @@ not False               =  True
 -- put them
 
 -- These functions have built-in rules.
-{-# NOINLINE [0] divInt# #-}
-{-# NOINLINE [0] modInt# #-}
+{-# INLINE [0] divInt# #-}
+{-# INLINE [0] modInt# #-}
 divInt# :: Int# -> Int# -> Int#
 x# `divInt#` y#
         -- Be careful NOT to overflow if we do any additional arithmetic



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/0ed373698572aed88cf91553368a1dfa9767b749

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/0ed373698572aed88cf91553368a1dfa9767b749
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/20200419/14d93a3a/attachment-0001.html>


More information about the ghc-commits mailing list