Skip to content
Browse files

Add pass control

ConstMath passes can be inserted after just simplifier passes instead of all
passes.  This should reduce compile times with large codebases.

The default behavior is to insert a ConstMath pass after simplifier passes only
within the first ten passes performed by the compiler.
  • Loading branch information...
1 parent 6ffc64f commit f945a19946fd751e7680dd2ef78415b63671e32b @JohnLato JohnLato committed
Showing with 70 additions and 11 deletions.
  1. +3 −3 ConstMath/Pass.hs
  2. +41 −7 ConstMath/Plugin.hs
  3. +14 −1 ConstMath/Types.hs
  4. +12 −0 README.md
View
6 ConstMath/Pass.hs
@@ -19,9 +19,9 @@ import Data.Maybe
import GhcPlugins
-constMathProgram :: Opts -> [CoreBind] -> CoreM [CoreBind]
-constMathProgram opts binds = do
- traceMsg opts "\nStarting ConstMath pass"
+constMathProgram :: Int -> Opts -> [CoreBind] -> CoreM [CoreBind]
+constMathProgram n opts binds = do
+ traceMsg opts $ "\nStarting ConstMath pass - " ++ show n
mapM (subBind opts "") binds
subBind :: Opts -> String -> CoreBind -> CoreM CoreBind
View
48 ConstMath/Plugin.hs
@@ -1,3 +1,5 @@
+{-# LANGUAGE ViewPatterns #-}
+
module ConstMath.Plugin (
plugin -- :: Plugin
) where
@@ -6,8 +8,6 @@ import ConstMath.Types
import ConstMath.Pass (constMathProgram)
import GhcPlugins
-import Data.List (intersperse)
-
plugin :: Plugin
plugin = defaultPlugin {
installCoreToDos = install
@@ -16,11 +16,8 @@ plugin = defaultPlugin {
install :: [CommandLineOption] -> [CoreToDo] -> CoreM [CoreToDo]
install args todos = do
reinitializeGlobals
- let pass = CoreDoPasses [constMath]
- return $ intersperse pass todos
- where constMath = CoreDoPluginPass "Constant Math Elimination"
- (bindsOnlyPass (constMathProgram opts) )
- opts = parseOpts args
+ return $ insertPasses opts todos
+ where opts = parseOpts args
-- TODO: use a real parser
parseOpts :: [CommandLineOption] -> Opts
@@ -31,4 +28,41 @@ parseOpts = foldr ($) defaultOpts . map mkArg
| flag `elem` ["-v11", "-verbosity=11","--trace"] = setVerbosity Trace
| flag `elem` ["-q", "--quiet","--verbosity=0", "-v0"] = setVerbosity None
| flag `elem` ["--dry", "--dry-run"] = setDry
+ | flag `elem` ["--enable-always"] = setInsertion CmAlways
+ | flag `elem` ["--enable-default"] = setInsertion CmPostSimplifyEarly
+ | flag `elem` ["--enable-post-simpl"] = setInsertion CmPostSimplify
| otherwise = id
+
+----------------------------------------------------------------
+-- Phase control
+--
+
+insertPasses :: Opts -> [CoreToDo] -> [CoreToDo]
+insertPasses opts todos = foldr genPass [] $ zip todos [0..]
+ where
+ constMath n = CoreDoPluginPass ("Constant Math Elimination - " ++ show n)
+ (bindsOnlyPass (constMathProgram n opts) )
+ genPass p@(todo,n) rest
+ | matchPass opts p = todo : constMath n : rest
+ | otherwise = todo : rest
+
+-- In most cases, new replacements are only visible to this plugin after a
+-- simplifier pass, although there are often some from the beginning.
+-- Therefore, the standard strategy is to insert a ConstMath pass after a
+-- simplifier pass
+--
+-- we also always insert a pass after the initial phase, because a lot of
+-- expressions are visible then too.
+matchPass :: Opts -> (CoreToDo, Int) -> Bool
+matchPass _ (_,0) = True
+matchPass (cmInsertion -> CmAlways) _ = True
+matchPass (cmInsertion -> CmPostSimplify) (todo,_)
+ = hasSimplifierPass todo
+matchPass (cmInsertion -> CmPostSimplifyEarly) (todo,n)
+ = hasSimplifierPass todo && n <= 10
+matchPass _ _ = False
+
+hasSimplifierPass :: CoreToDo -> Bool
+hasSimplifierPass (CoreDoSimplify _ _) = True
+hasSimplifierPass (CoreDoPasses todos) = any hasSimplifierPass todos
+hasSimplifierPass _ = False
View
15 ConstMath/Types.hs
@@ -6,11 +6,13 @@ module ConstMath.Types (
-- ** Configuration Options
Opts(..)
, Verbosity(..)
+, CmInsertion(..)
, defaultOpts
-- *Configuration Functions
-- ** Opt setters
, setVerbosity
, setDry
+, setInsertion
-- ** Opt checkers
, quiet
, verbose
@@ -20,9 +22,17 @@ module ConstMath.Types (
data Verbosity = None | CmVerbose Int | Trace deriving (Eq, Show, Ord)
+-- | Controls where the ConstMath pass is performed.
+data CmInsertion =
+ CmPostSimplifyEarly -- ^ initially and after simplifier passes (within first 10 passes)
+ | CmPostSimplify -- ^ initially and after every simplifier pass
+ | CmAlways -- ^ after every pass (expensive and usually wasteful)
+ deriving (Eq, Show, Ord)
+
data Opts = Opts
{ cmVerbosity :: Verbosity
, dryRun :: Bool
+ , cmInsertion :: CmInsertion
}
deriving (Eq, Show)
@@ -32,8 +42,11 @@ setVerbosity cmVerbosity opts = opts{cmVerbosity}
setDry :: Opts -> Opts
setDry opts = opts{dryRun=True}
+setInsertion :: CmInsertion -> Opts -> Opts
+setInsertion cmInsertion opts = opts{cmInsertion}
+
defaultOpts :: Opts
-defaultOpts = Opts None False
+defaultOpts = Opts None False CmPostSimplifyEarly
quiet :: Opts -> Bool
quiet Opts{cmVerbosity} = cmVerbosity == None
View
12 README.md
@@ -70,6 +70,18 @@ Currently available arguments are:
-q, --quiet
no output
+ Phase control
+
+ --enable-default
+ run a ConstMath pass after early simplifier passes
+
+ --enable-post-simpl
+ run a ConstMath pass after every simplifier pass
+
+ --enable-always
+ run a ConstMath pass after every compiler pass (expensive)
+
+
Inspecting the result
---------------------

0 comments on commit f945a19

Please sign in to comment.
Something went wrong with that request. Please try again.