2 -- The above warning supression flag is a temporary kludge.
3 -- While working on this module you are encouraged to remove it and fix
4 -- any warnings in the module. See
5 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
8 module Vectorise( vectorise )
11 #include "HsVersions.h"
21 import CoreLint ( showPass, endPass )
25 import SimplMonad ( SimplCount, zeroSimplCount )
26 import Rules ( RuleBase )
30 import FamInstEnv ( extendFamInstEnvList )
31 import InstEnv ( extendInstEnvList )
35 import Name ( Name, mkSysTvName, getName )
38 import MkId ( unwrapFamInstScrut )
40 import Module ( Module )
42 import DsMonad hiding (mapAndUnzipM)
43 import DsUtils ( mkCoreTup, mkCoreTupTy )
45 import Literal ( Literal )
48 import TysPrim ( intPrimTy )
49 import BasicTypes ( Boxity(..) )
53 import Control.Monad ( liftM, liftM2, zipWithM, mapAndUnzipM )
54 import Data.List ( sortBy, unzip4 )
56 vectorise :: HscEnv -> UniqSupply -> RuleBase -> ModGuts
57 -> IO (SimplCount, ModGuts)
58 vectorise hsc_env _ _ guts
60 showPass dflags "Vectorisation"
62 let info = hptVectInfo hsc_env `plusVectInfo` eps_vect_info eps
63 Just (info', guts') <- initV hsc_env guts info (vectModule guts)
64 endPass dflags "Vectorisation" Opt_D_dump_vect (mg_binds guts')
65 return (zeroSimplCount dflags, guts' { mg_vect_info = info' })
67 dflags = hsc_dflags hsc_env
69 vectModule :: ModGuts -> VM ModGuts
72 (types', fam_insts, tc_binds) <- vectTypeEnv (mg_types guts)
74 let fam_inst_env' = extendFamInstEnvList (mg_fam_inst_env guts) fam_insts
75 updGEnv (setFamInstEnv fam_inst_env')
77 -- dicts <- mapM buildPADict pa_insts
78 -- workers <- mapM vectDataConWorkers pa_insts
79 binds' <- mapM vectTopBind (mg_binds guts)
80 return $ guts { mg_types = types'
81 , mg_binds = Rec tc_binds : binds'
82 , mg_fam_inst_env = fam_inst_env'
83 , mg_fam_insts = mg_fam_insts guts ++ fam_insts
86 vectTopBind :: CoreBind -> VM CoreBind
87 vectTopBind b@(NonRec var expr)
89 var' <- vectTopBinder var
90 expr' <- vectTopRhs var expr
92 cexpr <- tryConvert var var' expr
93 return . Rec $ (var, cexpr) : (var', expr') : hs
97 vectTopBind b@(Rec bs)
99 vars' <- mapM vectTopBinder vars
100 exprs' <- zipWithM vectTopRhs vars exprs
102 cexprs <- sequence $ zipWith3 tryConvert vars vars' exprs
103 return . Rec $ zip vars cexprs ++ zip vars' exprs' ++ hs
107 (vars, exprs) = unzip bs
109 vectTopBinder :: Var -> VM Var
112 vty <- vectType (idType var)
113 var' <- cloneId mkVectOcc var vty
114 defGlobalVar var var'
117 vectTopRhs :: Var -> CoreExpr -> VM CoreExpr
120 closedV . liftM vectorised
122 $ vectPolyExpr (freeVars expr)
124 tryConvert :: Var -> Var -> CoreExpr -> VM CoreExpr
125 tryConvert var vect_var rhs
126 = fromVect (idType var) (Var vect_var) `orElseV` return rhs
128 -- ----------------------------------------------------------------------------
131 vectBndr :: Var -> VM VVar
134 vty <- vectType (idType v)
135 lty <- mkPArrayType vty
136 let vv = v `Id.setIdType` vty
137 lv = v `Id.setIdType` lty
138 updLEnv (mapTo vv lv)
141 mapTo vv lv env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv) }
143 vectBndrNew :: Var -> FastString -> VM VVar
146 vty <- vectType (idType v)
147 vv <- newLocalVVar fs vty
151 upd vv env = env { local_vars = extendVarEnv (local_vars env) v vv }
153 vectBndrIn :: Var -> VM a -> VM (VVar, a)
161 vectBndrNewIn :: Var -> FastString -> VM a -> VM (VVar, a)
165 vv <- vectBndrNew v fs
169 vectBndrIn' :: Var -> (VVar -> VM a) -> VM (VVar, a)
177 vectBndrsIn :: [Var] -> VM a -> VM ([VVar], a)
181 vvs <- mapM vectBndr vs
185 -- ----------------------------------------------------------------------------
188 vectVar :: Var -> VM VExpr
193 Local (vv,lv) -> return (Var vv, Var lv)
196 lexpr <- liftPA vexpr
197 return (vexpr, lexpr)
199 vectPolyVar :: Var -> [Type] -> VM VExpr
202 vtys <- mapM vectType tys
205 Local (vv, lv) -> liftM2 (,) (polyApply (Var vv) vtys)
206 (polyApply (Var lv) vtys)
208 vexpr <- polyApply (Var poly) vtys
209 lexpr <- liftPA vexpr
210 return (vexpr, lexpr)
212 vectLiteral :: Literal -> VM VExpr
215 lexpr <- liftPA (Lit lit)
216 return (Lit lit, lexpr)
218 vectPolyExpr :: CoreExprWithFVs -> VM VExpr
219 vectPolyExpr (_, AnnNote note expr)
220 = liftM (vNote note) $ vectPolyExpr expr
222 = polyAbstract tvs $ \abstract ->
224 mono' <- vectExpr mono
225 return $ mapVect abstract mono'
227 (tvs, mono) = collectAnnTypeBinders expr
229 vectExpr :: CoreExprWithFVs -> VM VExpr
230 vectExpr (_, AnnType ty)
231 = liftM vType (vectType ty)
233 vectExpr (_, AnnVar v) = vectVar v
235 vectExpr (_, AnnLit lit) = vectLiteral lit
237 vectExpr (_, AnnNote note expr)
238 = liftM (vNote note) (vectExpr expr)
240 vectExpr e@(_, AnnApp _ arg)
242 = vectTyAppExpr fn tys
244 (fn, tys) = collectAnnTypeArgs e
246 vectExpr (_, AnnApp (_, AnnVar v) (_, AnnLit lit))
247 | Just con <- isDataConId_maybe v
250 let vexpr = App (Var v) (Lit lit)
251 lexpr <- liftPA vexpr
252 return (vexpr, lexpr)
254 is_special_con con = con `elem` [intDataCon, floatDataCon, doubleDataCon]
257 vectExpr (_, AnnApp fn arg)
259 arg_ty' <- vectType arg_ty
260 res_ty' <- vectType res_ty
263 mkClosureApp arg_ty' res_ty' fn' arg'
265 (arg_ty, res_ty) = splitFunTy . exprType $ deAnnotate fn
267 vectExpr (_, AnnCase scrut bndr ty alts)
268 | Just (tycon, ty_args) <- splitTyConApp_maybe scrut_ty
270 = vectAlgCase tycon ty_args scrut bndr ty alts
272 scrut_ty = exprType (deAnnotate scrut)
274 vectExpr (_, AnnCase expr bndr ty alts)
275 = panic "vectExpr: case"
277 vectExpr (_, AnnLet (AnnNonRec bndr rhs) body)
279 vrhs <- localV . inBind bndr $ vectPolyExpr rhs
280 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
281 return $ vLet (vNonRec vbndr vrhs) vbody
283 vectExpr (_, AnnLet (AnnRec bs) body)
285 (vbndrs, (vrhss, vbody)) <- vectBndrsIn bndrs
287 (zipWithM vect_rhs bndrs rhss)
289 return $ vLet (vRec vbndrs vrhss) vbody
291 (bndrs, rhss) = unzip bs
293 vect_rhs bndr rhs = localV
297 vectExpr e@(fvs, AnnLam bndr _)
298 | not (isId bndr) = pprPanic "vectExpr" (ppr $ deAnnotate e)
299 | otherwise = vectLam fvs bs body
301 (bs,body) = collectAnnValBinders e
303 vectLam :: VarSet -> [Var] -> CoreExprWithFVs -> VM VExpr
306 tyvars <- localTyVars
307 (vs, vvs) <- readLEnv $ \env ->
308 unzip [(var, vv) | var <- varSetElems fvs
309 , Just vv <- [lookupVarEnv (local_vars env) var]]
311 arg_tys <- mapM (vectType . idType) bs
312 res_ty <- vectType (exprType $ deAnnotate body)
314 buildClosures tyvars vvs arg_tys res_ty
315 . hoistPolyVExpr tyvars
317 lc <- builtin liftingContext
318 (vbndrs, vbody) <- vectBndrsIn (vs ++ bs)
320 return $ vLams lc vbndrs vbody
322 vectTyAppExpr :: CoreExprWithFVs -> [Type] -> VM VExpr
323 vectTyAppExpr (_, AnnVar v) tys = vectPolyVar v tys
324 vectTyAppExpr e tys = pprPanic "vectTyAppExpr" (ppr $ deAnnotate e)
326 type CoreAltWithFVs = AnnAlt Id VarSet
330 -- case e :: t of v { ... }
334 -- V: let v' = e in case v' of _ { ... }
335 -- L: let v' = e in case v' `cast` ... of _ { ... }
337 -- When lifting, we have to do it this way because v must have the type
338 -- [:V(T):] but the scrutinee must be cast to the representation type. We also
339 -- have to handle the case where v is a wild var correctly.
342 -- FIXME: this is too lazy
343 vectAlgCase tycon ty_args scrut bndr ty [(DEFAULT, [], body)]
345 vscrut <- vectExpr scrut
347 lty <- mkPArrayType vty
348 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
349 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
351 vectAlgCase tycon ty_args scrut bndr ty [(DataAlt dc, [], body)]
353 vscrut <- vectExpr scrut
355 lty <- mkPArrayType vty
356 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
357 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
359 vectAlgCase tycon ty_args scrut bndr ty [(DataAlt dc, bndrs, body)]
361 vect_tc <- maybeV (lookupTyCon tycon)
363 lty <- mkPArrayType vty
364 vexpr <- vectExpr scrut
365 (vbndr, (vbndrs, vbody)) <- vect_scrut_bndr
369 (vscrut, arr_tc, arg_tys) <- mkVScrut (vVar vbndr)
370 vect_dc <- maybeV (lookupDataCon dc)
371 let [arr_dc] = tyConDataCons arr_tc
372 repr <- mkRepr vect_tc
373 shape_bndrs <- arrShapeVars repr
374 return . vLet (vNonRec vbndr vexpr)
375 $ vCaseProd vscrut vty lty vect_dc arr_dc shape_bndrs vbndrs vbody
377 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr FSLIT("scrut")
378 | otherwise = vectBndrIn bndr
380 vectAlgCase tycon ty_args scrut bndr ty alts
382 vect_tc <- maybeV (lookupTyCon tycon)
384 lty <- mkPArrayType vty
386 repr <- mkRepr vect_tc
387 shape_bndrs <- arrShapeVars repr
388 (len, sel, indices) <- arrSelector repr (map Var shape_bndrs)
390 (vbndr, valts) <- vect_scrut_bndr $ mapM (proc_alt sel lty) alts'
391 let (vect_dcs, vect_bndrss, lift_bndrss, vbodies) = unzip4 valts
393 vexpr <- vectExpr scrut
394 (vscrut, arr_tc, arg_tys) <- mkVScrut (vVar vbndr)
395 let [arr_dc] = tyConDataCons arr_tc
397 let (vect_scrut, lift_scrut) = vscrut
398 (vect_bodies, lift_bodies) = unzip vbodies
400 let vect_case = Case vect_scrut (mkWildId (exprType vect_scrut)) vty
401 (zipWith3 mk_vect_alt vect_dcs vect_bndrss vect_bodies)
403 lbody <- combinePA vty len sel indices lift_bodies
404 let lift_case = Case lift_scrut (mkWildId (exprType lift_scrut)) lty
405 [(DataAlt arr_dc, shape_bndrs ++ concat lift_bndrss,
408 return . vLet (vNonRec vbndr vexpr)
409 $ (vect_case, lift_case)
411 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr FSLIT("scrut")
412 | otherwise = vectBndrIn bndr
414 alts' = sortBy (\(alt1, _, _) (alt2, _, _) -> cmp alt1 alt2) alts
416 cmp (DataAlt dc1) (DataAlt dc2) = dataConTag dc1 `compare` dataConTag dc2
417 cmp DEFAULT DEFAULT = EQ
421 proc_alt sel lty (DataAlt dc, bndrs, body)
423 vect_dc <- maybeV (lookupDataCon dc)
424 let tag = mkDataConTag vect_dc
425 fvs = freeVarsOf body `delVarSetList` bndrs
426 (vect_bndrs, lift_bndrs, vbody)
427 <- vect_alt_bndrs bndrs
428 $ \len -> packLiftingContext len sel tag fvs lty
431 return (vect_dc, vect_bndrs, lift_bndrs, vbody)
435 void_tc <- builtin voidTyCon
436 let void_ty = mkTyConApp void_tc []
437 arr_ty <- mkPArrayType void_ty
438 bndr <- newLocalVar FSLIT("voids") arr_ty
439 len <- lengthPA void_ty (Var bndr)
441 return ([], [bndr], e)
443 vect_alt_bndrs bndrs p
446 vbndrs <- mapM vectBndr bndrs
447 let (vect_bndrs, lift_bndrs) = unzip vbndrs
450 len <- lengthPA (idType vv) (Var lv)
452 return (vect_bndrs, lift_bndrs, e)
454 mk_vect_alt vect_dc bndrs body = (DataAlt vect_dc, bndrs, body)
456 packLiftingContext :: CoreExpr -> CoreExpr -> CoreExpr -> VarSet -> Type -> VM VExpr -> VM VExpr
457 packLiftingContext len shape tag fvs res_ty p
459 select <- builtin selectPAIntPrimVar
460 let sel_expr = mkApps (Var select) [shape, tag]
461 sel_var <- newLocalVar FSLIT("sel#") (exprType sel_expr)
462 lc_var <- builtin liftingContext
465 bnds <- mapM (packFreeVar (Var lc_var) (Var sel_var))
469 return (vexpr, Let (NonRec sel_var sel_expr)
470 . mkLets (concat bnds)
471 $ Case len lc_var res_ty [(DEFAULT, [], lexpr)])
473 packFreeVar :: CoreExpr -> CoreExpr -> Var -> VM [CoreBind]
474 packFreeVar len sel v
481 expr <- packPA (idType vv) (Var lv) len sel
483 return [(NonRec lv' expr)]
487 upd vv lv' env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv') }