module Vectorise.Monad.Global (
readGEnv,
setGEnv,
updGEnv,
isVectAvoidanceAggressive,
defGlobalVar, undefGlobalVar,
lookupVectDecl,
globalParallelVars, globalParallelTyCons,
lookupTyCon,
defTyConName, defTyCon, globalVectTyCons,
lookupDataCon,
defDataCon,
lookupTyConPA,
defTyConPAs,
lookupTyConPR
) where
import Vectorise.Monad.Base
import Vectorise.Env
import CoreSyn
import Type
import TyCon
import DataCon
import DynFlags
import NameEnv
import NameSet
import Name
import VarEnv
import VarSet
import Var as Var
import Outputable
readGEnv :: (GlobalEnv -> a) -> VM a
readGEnv f = VM $ \_ genv lenv -> return (Yes genv lenv (f genv))
setGEnv :: GlobalEnv -> VM ()
setGEnv genv = VM $ \_ _ lenv -> return (Yes genv lenv ())
updGEnv :: (GlobalEnv -> GlobalEnv) -> VM ()
updGEnv f = VM $ \_ genv lenv -> return (Yes (f genv) lenv ())
isVectAvoidanceAggressive :: VM Bool
isVectAvoidanceAggressive = readGEnv global_vect_avoid
defGlobalVar :: Var -> Var -> VM ()
defGlobalVar v v'
= do { traceVt "add global var mapping:" (ppr v <+> text "-->" <+> ppr v')
; currentDef <- readGEnv $ \env -> lookupVarEnv (global_vars env) v
; case currentDef of
Just old_v' ->
do dflags <- getDynFlags
cantVectorise dflags "Variable is already vectorised:" $
ppr v <+> moduleOf v old_v'
Nothing -> return ()
; updGEnv $ \env -> env { global_vars = extendVarEnv (global_vars env) v v' }
}
where
moduleOf var var' | var == var'
= text "vectorises to itself"
| Just mod <- nameModule_maybe (Var.varName var')
= text "in module" <+> ppr mod
| otherwise
= text "in the current module"
undefGlobalVar :: Var -> VM ()
undefGlobalVar v
= do
{ traceVt "REMOVING global var mapping:" (ppr v)
; updGEnv $ \env -> env { global_vars = delVarEnv (global_vars env) v }
}
lookupVectDecl :: Var -> VM (Bool, Maybe (Type, CoreExpr))
lookupVectDecl var
= readGEnv $ \env ->
case lookupVarEnv (global_vect_decls env) var of
Nothing -> (False, Nothing)
Just Nothing -> (True, Nothing)
Just vectDecl -> (False, vectDecl)
globalParallelVars :: VM VarSet
globalParallelVars = readGEnv global_parallel_vars
globalParallelTyCons :: VM NameSet
globalParallelTyCons = readGEnv global_parallel_tycons
lookupTyCon :: TyCon -> VM (Maybe TyCon)
lookupTyCon tc
= readGEnv $ \env -> lookupNameEnv (global_tycons env) (tyConName tc)
defTyConName :: TyCon -> Name -> TyCon -> VM ()
defTyConName tc nameOfTc' tc'
= do { traceVt "add global tycon mapping:" (ppr tc <+> text "-->" <+> ppr nameOfTc')
; currentDef <- readGEnv $ \env -> lookupNameEnv (global_tycons env) (tyConName tc)
; case currentDef of
Just old_tc' ->
do dflags <- getDynFlags
cantVectorise dflags "Type constructor or class is already vectorised:" $
ppr tc <+> moduleOf tc old_tc'
Nothing -> return ()
; updGEnv $ \env ->
env { global_tycons = extendNameEnv (global_tycons env) (tyConName tc) tc' }
}
where
moduleOf tc tc' | tc == tc'
= text "vectorises to itself"
| Just mod <- nameModule_maybe (tyConName tc')
= text "in module" <+> ppr mod
| otherwise
= text "in the current module"
defTyCon :: TyCon -> TyCon -> VM ()
defTyCon tc tc' = defTyConName tc (tyConName tc') tc'
globalVectTyCons :: VM (NameEnv TyCon)
globalVectTyCons = readGEnv global_tycons
lookupDataCon :: DataCon -> VM (Maybe DataCon)
lookupDataCon dc
| isTupleTyCon (dataConTyCon dc)
= return (Just dc)
| otherwise
= readGEnv $ \env -> lookupNameEnv (global_datacons env) (dataConName dc)
defDataCon :: DataCon -> DataCon -> VM ()
defDataCon dc dc' = updGEnv $ \env ->
env { global_datacons = extendNameEnv (global_datacons env) (dataConName dc) dc' }
lookupTyConPA :: TyCon -> VM (Maybe Var)
lookupTyConPA tc
= readGEnv $ \env -> lookupNameEnv (global_pa_funs env) (tyConName tc)
defTyConPAs :: [(TyCon, Var)] -> VM ()
defTyConPAs ps = updGEnv $ \env ->
env { global_pa_funs = extendNameEnvList (global_pa_funs env)
[(tyConName tc, pa) | (tc, pa) <- ps] }
lookupTyConPR :: TyCon -> VM (Maybe Var)
lookupTyConPR tc = readGEnv $ \env -> lookupNameEnv (global_pr_funs env) (tyConName tc)