1 {-# LANGUAGE TypeFamilies #-}
2 -- | Optimisation fuel is used to control the amount of work the optimiser does.
4 -- Every optimisation step consumes a certain amount of fuel and stops when
5 -- it runs out of fuel. This can be used e.g. to debug optimiser bugs: Run
6 -- the optimiser with varying amount of fuel to find out the exact number of
7 -- steps where a bug is introduced in the output.
8 module OptimizationFuel
9 ( OptimizationFuel, amountOfFuel, tankFilledTo, anyFuelLeft, oneLessFuel
10 , OptFuelState, initOptFuelState
11 , FuelConsumer, FuelUsingMonad, FuelState
12 , fuelGet, fuelSet, lastFuelPass, setFuelPass
13 , fuelExhausted, fuelDec1, tryWithFuel
14 , runFuelIO, fuelConsumingPass
22 import StaticFlags (opt_Fuel)
27 import Compiler.Hoopl.GHC (getFuel, setFuel)
29 #include "HsVersions.h"
32 -- We limit the number of transactions executed using a record of flags
33 -- stored in an HscEnv. The flags store the name of the last optimization
34 -- pass and the amount of optimization fuel remaining.
36 OptFuelState { pass_ref :: IORef String
37 , fuel_ref :: IORef OptimizationFuel
39 initOptFuelState :: IO OptFuelState
41 do pass_ref' <- newIORef "unoptimized program"
42 fuel_ref' <- newIORef (tankFilledTo opt_Fuel)
43 return OptFuelState {pass_ref = pass_ref', fuel_ref = fuel_ref'}
45 type FuelConsumer a = OptimizationFuel -> (a, OptimizationFuel)
47 tankFilledTo :: Int -> OptimizationFuel
48 amountOfFuel :: OptimizationFuel -> Int
50 anyFuelLeft :: OptimizationFuel -> Bool
51 oneLessFuel :: OptimizationFuel -> OptimizationFuel
54 newtype OptimizationFuel = OptimizationFuel Int
57 tankFilledTo = OptimizationFuel
58 amountOfFuel (OptimizationFuel f) = f
60 anyFuelLeft (OptimizationFuel f) = f > 0
61 oneLessFuel (OptimizationFuel f) = ASSERT (f > 0) (OptimizationFuel (f - 1))
63 -- type OptimizationFuel = State# () -- would like this, but it won't work
64 data OptimizationFuel = OptimizationFuel
66 tankFilledTo _ = OptimizationFuel
67 amountOfFuel _ = maxBound
70 oneLessFuel _ = OptimizationFuel
73 data FuelState = FuelState { fs_fuel :: OptimizationFuel, fs_lastpass :: String }
74 newtype FuelUniqSM a = FUSM { unFUSM :: FuelState -> UniqSM (a, FuelState) }
76 fuelConsumingPass :: String -> FuelConsumer a -> FuelUniqSM a
77 fuelConsumingPass name f = do setFuelPass name
79 let (a, fuel') = f fuel
83 runFuelIO :: OptFuelState -> FuelUniqSM a -> IO a
84 runFuelIO fs (FUSM f) =
85 do pass <- readIORef (pass_ref fs)
86 fuel <- readIORef (fuel_ref fs)
87 u <- mkSplitUniqSupply 'u'
88 let (a, FuelState fuel' pass') = initUs_ u $ f (FuelState fuel pass)
89 writeIORef (pass_ref fs) pass'
90 writeIORef (fuel_ref fs) fuel'
93 instance Monad FuelUniqSM where
94 FUSM f >>= k = FUSM (\s -> f s >>= \(a, s') -> unFUSM (k a) s')
95 return a = FUSM (\s -> return (a, s))
97 instance MonadUnique FuelUniqSM where
98 getUniqueSupplyM = liftUniq getUniqueSupplyM
99 getUniqueM = liftUniq getUniqueM
100 getUniquesM = liftUniq getUniquesM
102 liftUniq :: UniqSM x -> FuelUniqSM x
103 liftUniq x = FUSM (\s -> x >>= (\u -> return (u, s)))
105 class Monad m => FuelUsingMonad m where
106 fuelGet :: m OptimizationFuel
107 fuelSet :: OptimizationFuel -> m ()
108 lastFuelPass :: m String
109 setFuelPass :: String -> m ()
111 fuelExhausted :: FuelUsingMonad m => m Bool
112 fuelExhausted = fuelGet >>= return . anyFuelLeft
114 fuelDec1 :: FuelUsingMonad m => m ()
115 fuelDec1 = fuelGet >>= fuelSet . oneLessFuel
117 tryWithFuel :: FuelUsingMonad m => a -> m (Maybe a)
118 tryWithFuel r = do f <- fuelGet
119 if anyFuelLeft f then fuelSet (oneLessFuel f) >> return (Just r)
122 instance FuelUsingMonad FuelUniqSM where
123 fuelGet = extract fs_fuel
124 lastFuelPass = extract fs_lastpass
125 fuelSet fuel = FUSM (\s -> return ((), s { fs_fuel = fuel }))
126 setFuelPass pass = FUSM (\s -> return ((), s { fs_lastpass = pass }))
128 extract :: (FuelState -> a) -> FuelUniqSM a
129 extract f = FUSM (\s -> return (f s, s))
131 instance FuelMonad FuelUniqSM where
132 getFuel = liftM amountOfFuel fuelGet
133 setFuel = fuelSet . tankFilledTo
135 -- Don't bother to checkpoint the unique supply; it doesn't matter
136 instance CheckpointMonad FuelUniqSM where
137 type Checkpoint FuelUniqSM = FuelState
138 checkpoint = FUSM $ \fuel -> return (fuel, fuel)
139 restart fuel = FUSM $ \_ -> return ((), fuel)