2 module Vectorise( vectorise )
10 import HscTypes hiding ( MonadThings(..) )
12 import Module ( PackageId )
16 import CoreMonad ( CoreM, getHscEnv, liftIO )
20 import FamInstEnv ( extendFamInstEnvList )
29 import Literal ( Literal, mkMachInt )
34 import Control.Monad ( liftM, liftM2, zipWithM )
35 import Data.List ( sortBy, unzip4 )
37 vectorise :: PackageId -> ModGuts -> CoreM ModGuts
38 vectorise backend guts = do
40 liftIO $ vectoriseIO backend hsc_env guts
42 vectoriseIO :: PackageId -> HscEnv -> ModGuts -> IO ModGuts
43 vectoriseIO backend hsc_env guts
46 let info = hptVectInfo hsc_env `plusVectInfo` eps_vect_info eps
47 Just (info', guts') <- initV backend hsc_env guts info (vectModule guts)
48 return (guts' { mg_vect_info = info' })
50 vectModule :: ModGuts -> VM ModGuts
53 (types', fam_insts, tc_binds) <- vectTypeEnv (mg_types guts)
55 let fam_inst_env' = extendFamInstEnvList (mg_fam_inst_env guts) fam_insts
56 updGEnv (setFamInstEnv fam_inst_env')
58 -- dicts <- mapM buildPADict pa_insts
59 -- workers <- mapM vectDataConWorkers pa_insts
60 binds' <- mapM vectTopBind (mg_binds guts)
61 return $ guts { mg_types = types'
62 , mg_binds = Rec tc_binds : binds'
63 , mg_fam_inst_env = fam_inst_env'
64 , mg_fam_insts = mg_fam_insts guts ++ fam_insts
67 vectTopBind :: CoreBind -> VM CoreBind
68 vectTopBind b@(NonRec var expr)
70 var' <- vectTopBinder var
71 expr' <- vectTopRhs var expr
73 cexpr <- tryConvert var var' expr
74 return . Rec $ (var, cexpr) : (var', expr') : hs
78 vectTopBind b@(Rec bs)
80 vars' <- mapM vectTopBinder vars
81 exprs' <- zipWithM vectTopRhs vars exprs
83 cexprs <- sequence $ zipWith3 tryConvert vars vars' exprs
84 return . Rec $ zip vars cexprs ++ zip vars' exprs' ++ hs
88 (vars, exprs) = unzip bs
90 vectTopBinder :: Var -> VM Var
93 vty <- vectType (idType var)
94 var' <- cloneId mkVectOcc var vty
98 vectTopRhs :: Var -> CoreExpr -> VM CoreExpr
101 closedV . liftM vectorised
103 $ vectPolyExpr (freeVars expr)
105 tryConvert :: Var -> Var -> CoreExpr -> VM CoreExpr
106 tryConvert var vect_var rhs
107 = fromVect (idType var) (Var vect_var) `orElseV` return rhs
109 -- ----------------------------------------------------------------------------
112 vectBndr :: Var -> VM VVar
115 (vty, lty) <- vectAndLiftType (idType v)
116 let vv = v `Id.setIdType` vty
117 lv = v `Id.setIdType` lty
118 updLEnv (mapTo vv lv)
121 mapTo vv lv env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv) }
123 vectBndrNew :: Var -> FastString -> VM VVar
126 vty <- vectType (idType v)
127 vv <- newLocalVVar fs vty
131 upd vv env = env { local_vars = extendVarEnv (local_vars env) v vv }
133 vectBndrIn :: Var -> VM a -> VM (VVar, a)
141 vectBndrNewIn :: Var -> FastString -> VM a -> VM (VVar, a)
145 vv <- vectBndrNew v fs
149 vectBndrsIn :: [Var] -> VM a -> VM ([VVar], a)
153 vvs <- mapM vectBndr vs
157 -- ----------------------------------------------------------------------------
160 vectVar :: Var -> VM VExpr
165 Local (vv,lv) -> return (Var vv, Var lv)
168 lexpr <- liftPA vexpr
169 return (vexpr, lexpr)
171 vectPolyVar :: Var -> [Type] -> VM VExpr
174 vtys <- mapM vectType tys
177 Local (vv, lv) -> liftM2 (,) (polyApply (Var vv) vtys)
178 (polyApply (Var lv) vtys)
180 vexpr <- polyApply (Var poly) vtys
181 lexpr <- liftPA vexpr
182 return (vexpr, lexpr)
184 vectLiteral :: Literal -> VM VExpr
187 lexpr <- liftPA (Lit lit)
188 return (Lit lit, lexpr)
190 vectPolyExpr :: CoreExprWithFVs -> VM VExpr
191 vectPolyExpr (_, AnnNote note expr)
192 = liftM (vNote note) $ vectPolyExpr expr
194 = polyAbstract tvs $ \abstract ->
196 mono' <- vectExpr mono
197 return $ mapVect abstract mono'
199 (tvs, mono) = collectAnnTypeBinders expr
201 vectExpr :: CoreExprWithFVs -> VM VExpr
202 vectExpr (_, AnnType ty)
203 = liftM vType (vectType ty)
205 vectExpr (_, AnnVar v) = vectVar v
207 vectExpr (_, AnnLit lit) = vectLiteral lit
209 vectExpr (_, AnnNote note expr)
210 = liftM (vNote note) (vectExpr expr)
212 vectExpr e@(_, AnnApp _ arg)
214 = vectTyAppExpr fn tys
216 (fn, tys) = collectAnnTypeArgs e
218 vectExpr (_, AnnApp (_, AnnVar v) (_, AnnLit lit))
219 | Just con <- isDataConId_maybe v
222 let vexpr = App (Var v) (Lit lit)
223 lexpr <- liftPA vexpr
224 return (vexpr, lexpr)
226 is_special_con con = con `elem` [intDataCon, floatDataCon, doubleDataCon]
229 vectExpr (_, AnnApp fn arg)
231 arg_ty' <- vectType arg_ty
232 res_ty' <- vectType res_ty
235 mkClosureApp arg_ty' res_ty' fn' arg'
237 (arg_ty, res_ty) = splitFunTy . exprType $ deAnnotate fn
239 vectExpr (_, AnnCase scrut bndr ty alts)
240 | Just (tycon, ty_args) <- splitTyConApp_maybe scrut_ty
242 = vectAlgCase tycon ty_args scrut bndr ty alts
244 scrut_ty = exprType (deAnnotate scrut)
246 vectExpr (_, AnnLet (AnnNonRec bndr rhs) body)
248 vrhs <- localV . inBind bndr $ vectPolyExpr rhs
249 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
250 return $ vLet (vNonRec vbndr vrhs) vbody
252 vectExpr (_, AnnLet (AnnRec bs) body)
254 (vbndrs, (vrhss, vbody)) <- vectBndrsIn bndrs
256 (zipWithM vect_rhs bndrs rhss)
258 return $ vLet (vRec vbndrs vrhss) vbody
260 (bndrs, rhss) = unzip bs
262 vect_rhs bndr rhs = localV
266 vectExpr e@(fvs, AnnLam bndr _)
267 | isId bndr = vectLam fvs bs body
269 (bs,body) = collectAnnValBinders e
271 vectExpr e = cantVectorise "Can't vectorise expression" (ppr $ deAnnotate e)
273 vectLam :: VarSet -> [Var] -> CoreExprWithFVs -> VM VExpr
276 tyvars <- localTyVars
277 (vs, vvs) <- readLEnv $ \env ->
278 unzip [(var, vv) | var <- varSetElems fvs
279 , Just vv <- [lookupVarEnv (local_vars env) var]]
281 arg_tys <- mapM (vectType . idType) bs
282 res_ty <- vectType (exprType $ deAnnotate body)
284 buildClosures tyvars vvs arg_tys res_ty
285 . hoistPolyVExpr tyvars
287 lc <- builtin liftingContext
288 (vbndrs, vbody) <- vectBndrsIn (vs ++ bs)
290 return $ vLams lc vbndrs vbody
292 vectTyAppExpr :: CoreExprWithFVs -> [Type] -> VM VExpr
293 vectTyAppExpr (_, AnnVar v) tys = vectPolyVar v tys
294 vectTyAppExpr e tys = cantVectorise "Can't vectorise expression"
295 (ppr $ deAnnotate e `mkTyApps` tys)
299 -- case e :: t of v { ... }
303 -- V: let v' = e in case v' of _ { ... }
304 -- L: let v' = e in case v' `cast` ... of _ { ... }
306 -- When lifting, we have to do it this way because v must have the type
307 -- [:V(T):] but the scrutinee must be cast to the representation type. We also
308 -- have to handle the case where v is a wild var correctly.
311 -- FIXME: this is too lazy
312 vectAlgCase :: TyCon -> [Type] -> CoreExprWithFVs -> Var -> Type
313 -> [(AltCon, [Var], CoreExprWithFVs)]
315 vectAlgCase _tycon _ty_args scrut bndr ty [(DEFAULT, [], body)]
317 vscrut <- vectExpr scrut
318 (vty, lty) <- vectAndLiftType ty
319 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
320 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
322 vectAlgCase _tycon _ty_args scrut bndr ty [(DataAlt _, [], body)]
324 vscrut <- vectExpr scrut
325 (vty, lty) <- vectAndLiftType ty
326 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
327 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
329 vectAlgCase tycon _ty_args scrut bndr ty [(DataAlt dc, bndrs, body)]
331 vect_tc <- maybeV (lookupTyCon tycon)
332 (vty, lty) <- vectAndLiftType ty
333 vexpr <- vectExpr scrut
334 (vbndr, (vbndrs, vbody)) <- vect_scrut_bndr
338 (vscrut, arr_tc, _arg_tys) <- mkVScrut (vVar vbndr)
339 vect_dc <- maybeV (lookupDataCon dc)
340 let [arr_dc] = tyConDataCons arr_tc
341 repr <- mkRepr vect_tc
342 shape_bndrs <- arrShapeVars repr
343 return . vLet (vNonRec vbndr vexpr)
344 $ vCaseProd vscrut vty lty vect_dc arr_dc shape_bndrs vbndrs vbody
346 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
347 | otherwise = vectBndrIn bndr
349 vectAlgCase tycon _ty_args scrut bndr ty alts
351 vect_tc <- maybeV (lookupTyCon tycon)
352 (vty, lty) <- vectAndLiftType ty
353 repr <- mkRepr vect_tc
354 shape_bndrs <- arrShapeVars repr
355 (len, sel, indices) <- arrSelector repr (map Var shape_bndrs)
357 (vbndr, valts) <- vect_scrut_bndr $ mapM (proc_alt sel vty lty) alts'
358 let (vect_dcs, vect_bndrss, lift_bndrss, vbodies) = unzip4 valts
360 vexpr <- vectExpr scrut
361 (vscrut, arr_tc, _arg_tys) <- mkVScrut (vVar vbndr)
362 let [arr_dc] = tyConDataCons arr_tc
364 let (vect_scrut, lift_scrut) = vscrut
365 (vect_bodies, lift_bodies) = unzip vbodies
367 vdummy <- newDummyVar (exprType vect_scrut)
368 ldummy <- newDummyVar (exprType lift_scrut)
369 let vect_case = Case vect_scrut vdummy vty
370 (zipWith3 mk_vect_alt vect_dcs vect_bndrss vect_bodies)
372 lbody <- combinePA vty len sel indices lift_bodies
373 let lift_case = Case lift_scrut ldummy lty
374 [(DataAlt arr_dc, shape_bndrs ++ concat lift_bndrss,
377 return . vLet (vNonRec vbndr vexpr)
378 $ (vect_case, lift_case)
380 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
381 | otherwise = vectBndrIn bndr
383 alts' = sortBy (\(alt1, _, _) (alt2, _, _) -> cmp alt1 alt2) alts
385 cmp (DataAlt dc1) (DataAlt dc2) = dataConTag dc1 `compare` dataConTag dc2
386 cmp DEFAULT DEFAULT = EQ
389 cmp _ _ = panic "vectAlgCase/cmp"
391 proc_alt sel vty lty (DataAlt dc, bndrs, body)
393 vect_dc <- maybeV (lookupDataCon dc)
394 let tag = mkDataConTag vect_dc
395 fvs = freeVarsOf body `delVarSetList` bndrs
396 (vect_bndrs, lift_bndrs, vbody)
397 <- vect_alt_bndrs bndrs
398 $ \len -> packLiftingContext len sel tag fvs vty lty
401 return (vect_dc, vect_bndrs, lift_bndrs, vbody)
402 proc_alt _ _ _ _ = panic "vectAlgCase/proc_alt"
406 void_tc <- builtin voidTyCon
407 let void_ty = mkTyConApp void_tc []
408 arr_ty <- mkPArrayType void_ty
409 bndr <- newLocalVar (fsLit "voids") arr_ty
410 len <- lengthPA void_ty (Var bndr)
412 return ([], [bndr], e)
414 vect_alt_bndrs bndrs p
417 vbndrs <- mapM vectBndr bndrs
418 let (vect_bndrs, lift_bndrs) = unzip vbndrs
421 len <- lengthPA (idType vv) (Var lv)
423 return (vect_bndrs, lift_bndrs, e)
425 mk_vect_alt vect_dc bndrs body = (DataAlt vect_dc, bndrs, body)
427 packLiftingContext :: CoreExpr -> CoreExpr -> CoreExpr -> VarSet
428 -> Type -> Type -> VM VExpr -> VM VExpr
429 packLiftingContext len shape tag fvs vty lty p
431 select <- builtin selectPAIntPrimVar
432 let sel_expr = mkApps (Var select) [shape, tag]
433 sel_var <- newLocalVar (fsLit "sel#") (exprType sel_expr)
434 lc_var <- builtin liftingContext
437 bnds <- mapM (packFreeVar (Var lc_var) (Var sel_var))
442 return (vexpr, Let (NonRec sel_var sel_expr)
443 $ Case len lc_var lty
444 [(DEFAULT, [], mkLets (concat bnds) lexpr),
445 (LitAlt (mkMachInt 0), [], empty)])
447 packFreeVar :: CoreExpr -> CoreExpr -> Var -> VM [CoreBind]
448 packFreeVar len sel v
455 expr <- packPA (idType vv) (Var lv) len sel
457 return [(NonRec lv' expr)]
461 upd vv lv' env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv') }