const-math-ghc-plugin 0.3.0.1 → 1.0.0.0
raw patch · 4 files changed
+59/−12 lines, 4 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- ConstMath/Pass.hs +3/−3
- ConstMath/Plugin.hs +41/−7
- ConstMath/Types.hs +14/−1
- const-math-ghc-plugin.cabal +1/−1
ConstMath/Pass.hs view
@@ -19,9 +19,9 @@ 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
ConstMath/Plugin.hs view
@@ -1,3 +1,5 @@+{-# LANGUAGE ViewPatterns #-}+ module ConstMath.Plugin ( plugin -- :: Plugin ) where@@ -6,8 +8,6 @@ import ConstMath.Pass (constMathProgram) import GhcPlugins -import Data.List (intersperse)- plugin :: Plugin plugin = defaultPlugin { installCoreToDos = install@@ -16,11 +16,8 @@ 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 @@ | 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
ConstMath/Types.hs view
@@ -6,11 +6,13 @@ -- ** Configuration Options Opts(..) , Verbosity(..)+, CmInsertion(..) , defaultOpts -- *Configuration Functions -- ** Opt setters , setVerbosity , setDry+, setInsertion -- ** Opt checkers , quiet , verbose@@ -20,9 +22,17 @@ 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 @@ 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
const-math-ghc-plugin.cabal view
@@ -1,5 +1,5 @@ name: const-math-ghc-plugin-version: 0.3.0.1+version: 1.0.0.0 synopsis: Compiler plugin for constant math elimination description: This plugin evaluates constant math expressions at compile-time.