Vectorise polymorphic let bindings
[ghc.git] / compiler / vectorise / Vectorise.hs
1 {-# OPTIONS -w #-}
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
6 -- for details
7
8 module Vectorise( vectorise )
9 where
10
11 import VectMonad
12 import VectUtils
13 import VectType
14 import VectCore
15
16 import DynFlags
17 import HscTypes
18
19 import CoreLint ( showPass, endPass )
20 import CoreSyn
21 import CoreUtils
22 import CoreFVs
23 import SimplMonad ( SimplCount, zeroSimplCount )
24 import Rules ( RuleBase )
25 import DataCon
26 import TyCon
27 import Type
28 import FamInstEnv ( extendFamInstEnvList )
29 import InstEnv ( extendInstEnvList )
30 import Var
31 import VarEnv
32 import VarSet
33 import Name ( Name, mkSysTvName, getName )
34 import NameEnv
35 import Id
36 import MkId ( unwrapFamInstScrut )
37 import OccName
38 import Module ( Module )
39
40 import DsMonad hiding (mapAndUnzipM)
41 import DsUtils ( mkCoreTup, mkCoreTupTy )
42
43 import Literal ( Literal, mkMachInt )
44 import PrelNames
45 import TysWiredIn
46 import TysPrim ( intPrimTy )
47 import BasicTypes ( Boxity(..) )
48
49 import Outputable
50 import FastString
51 import Control.Monad ( liftM, liftM2, zipWithM, mapAndUnzipM )
52 import Data.List ( sortBy, unzip4 )
53
54 vectorise :: HscEnv -> UniqSupply -> RuleBase -> ModGuts
55 -> IO (SimplCount, ModGuts)
56 vectorise hsc_env _ _ guts
57 = do
58 showPass dflags "Vectorisation"
59 eps <- hscEPS hsc_env
60 let info = hptVectInfo hsc_env `plusVectInfo` eps_vect_info eps
61 Just (info', guts') <- initV hsc_env guts info (vectModule guts)
62 endPass dflags "Vectorisation" Opt_D_dump_vect (mg_binds guts')
63 return (zeroSimplCount dflags, guts' { mg_vect_info = info' })
64 where
65 dflags = hsc_dflags hsc_env
66
67 vectModule :: ModGuts -> VM ModGuts
68 vectModule guts
69 = do
70 (types', fam_insts, tc_binds) <- vectTypeEnv (mg_types guts)
71
72 let fam_inst_env' = extendFamInstEnvList (mg_fam_inst_env guts) fam_insts
73 updGEnv (setFamInstEnv fam_inst_env')
74
75 -- dicts <- mapM buildPADict pa_insts
76 -- workers <- mapM vectDataConWorkers pa_insts
77 binds' <- mapM vectTopBind (mg_binds guts)
78 return $ guts { mg_types = types'
79 , mg_binds = Rec tc_binds : binds'
80 , mg_fam_inst_env = fam_inst_env'
81 , mg_fam_insts = mg_fam_insts guts ++ fam_insts
82 }
83
84 vectTopBind :: CoreBind -> VM CoreBind
85 vectTopBind b@(NonRec var expr)
86 = do
87 var' <- vectTopBinder var
88 expr' <- vectTopRhs var expr
89 hs <- takeHoisted
90 cexpr <- tryConvert var var' expr
91 return . Rec $ (var, cexpr) : (var', expr') : hs
92 `orElseV`
93 return b
94
95 vectTopBind b@(Rec bs)
96 = do
97 vars' <- mapM vectTopBinder vars
98 exprs' <- zipWithM vectTopRhs vars exprs
99 hs <- takeHoisted
100 cexprs <- sequence $ zipWith3 tryConvert vars vars' exprs
101 return . Rec $ zip vars cexprs ++ zip vars' exprs' ++ hs
102 `orElseV`
103 return b
104 where
105 (vars, exprs) = unzip bs
106
107 vectTopBinder :: Var -> VM Var
108 vectTopBinder var
109 = do
110 vty <- vectType (idType var)
111 var' <- cloneId mkVectOcc var vty
112 defGlobalVar var var'
113 return var'
114
115 vectTopRhs :: Var -> CoreExpr -> VM CoreExpr
116 vectTopRhs var expr
117 = do
118 closedV . liftM vectorised
119 . inBind var
120 $ vectPolyExpr (freeVars expr)
121
122 tryConvert :: Var -> Var -> CoreExpr -> VM CoreExpr
123 tryConvert var vect_var rhs
124 = fromVect (idType var) (Var vect_var) `orElseV` return rhs
125
126 -- ----------------------------------------------------------------------------
127 -- Bindings
128
129 vectBndr :: Var -> VM VVar
130 vectBndr v
131 = do
132 (vty, lty) <- vectAndLiftType (idType v)
133 let vv = v `Id.setIdType` vty
134 lv = v `Id.setIdType` lty
135 updLEnv (mapTo vv lv)
136 return (vv, lv)
137 where
138 mapTo vv lv env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv) }
139
140 vectBndrNew :: Var -> FastString -> VM VVar
141 vectBndrNew v fs
142 = do
143 vty <- vectType (idType v)
144 vv <- newLocalVVar fs vty
145 updLEnv (upd vv)
146 return vv
147 where
148 upd vv env = env { local_vars = extendVarEnv (local_vars env) v vv }
149
150 vectBndrIn :: Var -> VM a -> VM (VVar, a)
151 vectBndrIn v p
152 = localV
153 $ do
154 vv <- vectBndr v
155 x <- p
156 return (vv, x)
157
158 vectBndrNewIn :: Var -> FastString -> VM a -> VM (VVar, a)
159 vectBndrNewIn v fs p
160 = localV
161 $ do
162 vv <- vectBndrNew v fs
163 x <- p
164 return (vv, x)
165
166 vectBndrIn' :: Var -> (VVar -> VM a) -> VM (VVar, a)
167 vectBndrIn' v p
168 = localV
169 $ do
170 vv <- vectBndr v
171 x <- p vv
172 return (vv, x)
173
174 vectBndrsIn :: [Var] -> VM a -> VM ([VVar], a)
175 vectBndrsIn vs p
176 = localV
177 $ do
178 vvs <- mapM vectBndr vs
179 x <- p
180 return (vvs, x)
181
182 -- ----------------------------------------------------------------------------
183 -- Expressions
184
185 vectVar :: Var -> VM VExpr
186 vectVar v
187 = do
188 r <- lookupVar v
189 case r of
190 Local (vv,lv) -> return (Var vv, Var lv)
191 Global vv -> do
192 let vexpr = Var vv
193 lexpr <- liftPA vexpr
194 return (vexpr, lexpr)
195
196 vectPolyVar :: Var -> [Type] -> VM VExpr
197 vectPolyVar v tys
198 = do
199 vtys <- mapM vectType tys
200 r <- lookupVar v
201 case r of
202 Local (vv, lv) -> liftM2 (,) (polyApply (Var vv) vtys)
203 (polyApply (Var lv) vtys)
204 Global poly -> do
205 vexpr <- polyApply (Var poly) vtys
206 lexpr <- liftPA vexpr
207 return (vexpr, lexpr)
208
209 vectLiteral :: Literal -> VM VExpr
210 vectLiteral lit
211 = do
212 lexpr <- liftPA (Lit lit)
213 return (Lit lit, lexpr)
214
215 vectPolyExpr :: CoreExprWithFVs -> VM VExpr
216 vectPolyExpr (_, AnnNote note expr)
217 = liftM (vNote note) $ vectPolyExpr expr
218 vectPolyExpr expr
219 = polyAbstract tvs $ \abstract ->
220 do
221 mono' <- vectExpr mono
222 return $ mapVect abstract mono'
223 where
224 (tvs, mono) = collectAnnTypeBinders expr
225
226 vectExpr :: CoreExprWithFVs -> VM VExpr
227 vectExpr (_, AnnType ty)
228 = liftM vType (vectType ty)
229
230 vectExpr (_, AnnVar v) = vectVar v
231
232 vectExpr (_, AnnLit lit) = vectLiteral lit
233
234 vectExpr (_, AnnNote note expr)
235 = liftM (vNote note) (vectExpr expr)
236
237 vectExpr e@(_, AnnApp _ arg)
238 | isAnnTypeArg arg
239 = vectTyAppExpr fn tys
240 where
241 (fn, tys) = collectAnnTypeArgs e
242
243 vectExpr (_, AnnApp (_, AnnVar v) (_, AnnLit lit))
244 | Just con <- isDataConId_maybe v
245 , is_special_con con
246 = do
247 let vexpr = App (Var v) (Lit lit)
248 lexpr <- liftPA vexpr
249 return (vexpr, lexpr)
250 where
251 is_special_con con = con `elem` [intDataCon, floatDataCon, doubleDataCon]
252
253
254 vectExpr (_, AnnApp fn arg)
255 = do
256 arg_ty' <- vectType arg_ty
257 res_ty' <- vectType res_ty
258 fn' <- vectExpr fn
259 arg' <- vectExpr arg
260 mkClosureApp arg_ty' res_ty' fn' arg'
261 where
262 (arg_ty, res_ty) = splitFunTy . exprType $ deAnnotate fn
263
264 vectExpr (_, AnnCase scrut bndr ty alts)
265 | Just (tycon, ty_args) <- splitTyConApp_maybe scrut_ty
266 , isAlgTyCon tycon
267 = vectAlgCase tycon ty_args scrut bndr ty alts
268 where
269 scrut_ty = exprType (deAnnotate scrut)
270
271 vectExpr (_, AnnCase expr bndr ty alts)
272 = panic "vectExpr: case"
273
274 vectExpr (_, AnnLet (AnnNonRec bndr rhs) body)
275 = do
276 vrhs <- localV . inBind bndr $ vectPolyExpr rhs
277 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
278 return $ vLet (vNonRec vbndr vrhs) vbody
279
280 vectExpr (_, AnnLet (AnnRec bs) body)
281 = do
282 (vbndrs, (vrhss, vbody)) <- vectBndrsIn bndrs
283 $ liftM2 (,)
284 (zipWithM vect_rhs bndrs rhss)
285 (vectPolyExpr body)
286 return $ vLet (vRec vbndrs vrhss) vbody
287 where
288 (bndrs, rhss) = unzip bs
289
290 vect_rhs bndr rhs = localV
291 . inBind bndr
292 $ vectExpr rhs
293
294 vectExpr e@(fvs, AnnLam bndr _)
295 | not (isId bndr) = pprPanic "vectExpr" (ppr $ deAnnotate e)
296 | otherwise = vectLam fvs bs body
297 where
298 (bs,body) = collectAnnValBinders e
299
300 vectExpr e = pprPanic "vectExpr" (ppr $ deAnnotate e)
301
302 vectLam :: VarSet -> [Var] -> CoreExprWithFVs -> VM VExpr
303 vectLam fvs bs body
304 = do
305 tyvars <- localTyVars
306 (vs, vvs) <- readLEnv $ \env ->
307 unzip [(var, vv) | var <- varSetElems fvs
308 , Just vv <- [lookupVarEnv (local_vars env) var]]
309
310 arg_tys <- mapM (vectType . idType) bs
311 res_ty <- vectType (exprType $ deAnnotate body)
312
313 buildClosures tyvars vvs arg_tys res_ty
314 . hoistPolyVExpr tyvars
315 $ do
316 lc <- builtin liftingContext
317 (vbndrs, vbody) <- vectBndrsIn (vs ++ bs)
318 (vectExpr body)
319 return $ vLams lc vbndrs vbody
320
321 vectTyAppExpr :: CoreExprWithFVs -> [Type] -> VM VExpr
322 vectTyAppExpr (_, AnnVar v) tys = vectPolyVar v tys
323 vectTyAppExpr e tys = pprPanic "vectTyAppExpr" (ppr $ deAnnotate e)
324
325 type CoreAltWithFVs = AnnAlt Id VarSet
326
327 -- We convert
328 --
329 -- case e :: t of v { ... }
330 --
331 -- to
332 --
333 -- V: let v' = e in case v' of _ { ... }
334 -- L: let v' = e in case v' `cast` ... of _ { ... }
335 --
336 -- When lifting, we have to do it this way because v must have the type
337 -- [:V(T):] but the scrutinee must be cast to the representation type. We also
338 -- have to handle the case where v is a wild var correctly.
339 --
340
341 -- FIXME: this is too lazy
342 vectAlgCase tycon ty_args scrut bndr ty [(DEFAULT, [], body)]
343 = do
344 vscrut <- vectExpr scrut
345 (vty, lty) <- vectAndLiftType ty
346 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
347 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
348
349 vectAlgCase tycon ty_args scrut bndr ty [(DataAlt dc, [], body)]
350 = do
351 vscrut <- vectExpr scrut
352 (vty, lty) <- vectAndLiftType ty
353 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
354 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
355
356 vectAlgCase tycon ty_args scrut bndr ty [(DataAlt dc, bndrs, body)]
357 = do
358 vect_tc <- maybeV (lookupTyCon tycon)
359 (vty, lty) <- vectAndLiftType ty
360 vexpr <- vectExpr scrut
361 (vbndr, (vbndrs, vbody)) <- vect_scrut_bndr
362 . vectBndrsIn bndrs
363 $ vectExpr body
364
365 (vscrut, arr_tc, arg_tys) <- mkVScrut (vVar vbndr)
366 vect_dc <- maybeV (lookupDataCon dc)
367 let [arr_dc] = tyConDataCons arr_tc
368 repr <- mkRepr vect_tc
369 shape_bndrs <- arrShapeVars repr
370 return . vLet (vNonRec vbndr vexpr)
371 $ vCaseProd vscrut vty lty vect_dc arr_dc shape_bndrs vbndrs vbody
372 where
373 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
374 | otherwise = vectBndrIn bndr
375
376 vectAlgCase tycon ty_args scrut bndr ty alts
377 = do
378 vect_tc <- maybeV (lookupTyCon tycon)
379 (vty, lty) <- vectAndLiftType ty
380 repr <- mkRepr vect_tc
381 shape_bndrs <- arrShapeVars repr
382 (len, sel, indices) <- arrSelector repr (map Var shape_bndrs)
383
384 (vbndr, valts) <- vect_scrut_bndr $ mapM (proc_alt sel vty lty) alts'
385 let (vect_dcs, vect_bndrss, lift_bndrss, vbodies) = unzip4 valts
386
387 vexpr <- vectExpr scrut
388 (vscrut, arr_tc, arg_tys) <- mkVScrut (vVar vbndr)
389 let [arr_dc] = tyConDataCons arr_tc
390
391 let (vect_scrut, lift_scrut) = vscrut
392 (vect_bodies, lift_bodies) = unzip vbodies
393
394 let vect_case = Case vect_scrut (mkWildId (exprType vect_scrut)) vty
395 (zipWith3 mk_vect_alt vect_dcs vect_bndrss vect_bodies)
396
397 lbody <- combinePA vty len sel indices lift_bodies
398 let lift_case = Case lift_scrut (mkWildId (exprType lift_scrut)) lty
399 [(DataAlt arr_dc, shape_bndrs ++ concat lift_bndrss,
400 lbody)]
401
402 return . vLet (vNonRec vbndr vexpr)
403 $ (vect_case, lift_case)
404 where
405 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
406 | otherwise = vectBndrIn bndr
407
408 alts' = sortBy (\(alt1, _, _) (alt2, _, _) -> cmp alt1 alt2) alts
409
410 cmp (DataAlt dc1) (DataAlt dc2) = dataConTag dc1 `compare` dataConTag dc2
411 cmp DEFAULT DEFAULT = EQ
412 cmp DEFAULT _ = LT
413 cmp _ DEFAULT = GT
414
415 proc_alt sel vty lty (DataAlt dc, bndrs, body)
416 = do
417 vect_dc <- maybeV (lookupDataCon dc)
418 let tag = mkDataConTag vect_dc
419 fvs = freeVarsOf body `delVarSetList` bndrs
420 (vect_bndrs, lift_bndrs, vbody)
421 <- vect_alt_bndrs bndrs
422 $ \len -> packLiftingContext len sel tag fvs vty lty
423 $ vectExpr body
424
425 return (vect_dc, vect_bndrs, lift_bndrs, vbody)
426
427 vect_alt_bndrs [] p
428 = do
429 void_tc <- builtin voidTyCon
430 let void_ty = mkTyConApp void_tc []
431 arr_ty <- mkPArrayType void_ty
432 bndr <- newLocalVar (fsLit "voids") arr_ty
433 len <- lengthPA void_ty (Var bndr)
434 e <- p len
435 return ([], [bndr], e)
436
437 vect_alt_bndrs bndrs p
438 = localV
439 $ do
440 vbndrs <- mapM vectBndr bndrs
441 let (vect_bndrs, lift_bndrs) = unzip vbndrs
442 vv : _ = vect_bndrs
443 lv : _ = lift_bndrs
444 len <- lengthPA (idType vv) (Var lv)
445 e <- p len
446 return (vect_bndrs, lift_bndrs, e)
447
448 mk_vect_alt vect_dc bndrs body = (DataAlt vect_dc, bndrs, body)
449
450 packLiftingContext :: CoreExpr -> CoreExpr -> CoreExpr -> VarSet
451 -> Type -> Type -> VM VExpr -> VM VExpr
452 packLiftingContext len shape tag fvs vty lty p
453 = do
454 select <- builtin selectPAIntPrimVar
455 let sel_expr = mkApps (Var select) [shape, tag]
456 sel_var <- newLocalVar (fsLit "sel#") (exprType sel_expr)
457 lc_var <- builtin liftingContext
458 localV $
459 do
460 bnds <- mapM (packFreeVar (Var lc_var) (Var sel_var))
461 . filter isLocalId
462 $ varSetElems fvs
463 (vexpr, lexpr) <- p
464 empty <- emptyPA vty
465 return (vexpr, Let (NonRec sel_var sel_expr)
466 $ Case len lc_var lty
467 [(DEFAULT, [], mkLets (concat bnds) lexpr),
468 (LitAlt (mkMachInt 0), [], empty)])
469
470 packFreeVar :: CoreExpr -> CoreExpr -> Var -> VM [CoreBind]
471 packFreeVar len sel v
472 = do
473 r <- lookupVar v
474 case r of
475 Local (vv,lv) ->
476 do
477 lv' <- cloneVar lv
478 expr <- packPA (idType vv) (Var lv) len sel
479 updLEnv (upd vv lv')
480 return [(NonRec lv' expr)]
481
482 _ -> return []
483 where
484 upd vv lv' env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv') }
485