1c185bdfd9732018e230838509a64623a562421f
[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 <- vectType (idType v)
133 lty <- mkPArrayType vty
134 let vv = v `Id.setIdType` vty
135 lv = v `Id.setIdType` lty
136 updLEnv (mapTo vv lv)
137 return (vv, lv)
138 where
139 mapTo vv lv env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv) }
140
141 vectBndrNew :: Var -> FastString -> VM VVar
142 vectBndrNew v fs
143 = do
144 vty <- vectType (idType v)
145 vv <- newLocalVVar fs vty
146 updLEnv (upd vv)
147 return vv
148 where
149 upd vv env = env { local_vars = extendVarEnv (local_vars env) v vv }
150
151 vectBndrIn :: Var -> VM a -> VM (VVar, a)
152 vectBndrIn v p
153 = localV
154 $ do
155 vv <- vectBndr v
156 x <- p
157 return (vv, x)
158
159 vectBndrNewIn :: Var -> FastString -> VM a -> VM (VVar, a)
160 vectBndrNewIn v fs p
161 = localV
162 $ do
163 vv <- vectBndrNew v fs
164 x <- p
165 return (vv, x)
166
167 vectBndrIn' :: Var -> (VVar -> VM a) -> VM (VVar, a)
168 vectBndrIn' v p
169 = localV
170 $ do
171 vv <- vectBndr v
172 x <- p vv
173 return (vv, x)
174
175 vectBndrsIn :: [Var] -> VM a -> VM ([VVar], a)
176 vectBndrsIn vs p
177 = localV
178 $ do
179 vvs <- mapM vectBndr vs
180 x <- p
181 return (vvs, x)
182
183 -- ----------------------------------------------------------------------------
184 -- Expressions
185
186 vectVar :: Var -> VM VExpr
187 vectVar v
188 = do
189 r <- lookupVar v
190 case r of
191 Local (vv,lv) -> return (Var vv, Var lv)
192 Global vv -> do
193 let vexpr = Var vv
194 lexpr <- liftPA vexpr
195 return (vexpr, lexpr)
196
197 vectPolyVar :: Var -> [Type] -> VM VExpr
198 vectPolyVar v tys
199 = do
200 vtys <- mapM vectType tys
201 r <- lookupVar v
202 case r of
203 Local (vv, lv) -> liftM2 (,) (polyApply (Var vv) vtys)
204 (polyApply (Var lv) vtys)
205 Global poly -> do
206 vexpr <- polyApply (Var poly) vtys
207 lexpr <- liftPA vexpr
208 return (vexpr, lexpr)
209
210 vectLiteral :: Literal -> VM VExpr
211 vectLiteral lit
212 = do
213 lexpr <- liftPA (Lit lit)
214 return (Lit lit, lexpr)
215
216 vectPolyExpr :: CoreExprWithFVs -> VM VExpr
217 vectPolyExpr (_, AnnNote note expr)
218 = liftM (vNote note) $ vectPolyExpr expr
219 vectPolyExpr expr
220 = polyAbstract tvs $ \abstract ->
221 do
222 mono' <- vectExpr mono
223 return $ mapVect abstract mono'
224 where
225 (tvs, mono) = collectAnnTypeBinders expr
226
227 vectExpr :: CoreExprWithFVs -> VM VExpr
228 vectExpr (_, AnnType ty)
229 = liftM vType (vectType ty)
230
231 vectExpr (_, AnnVar v) = vectVar v
232
233 vectExpr (_, AnnLit lit) = vectLiteral lit
234
235 vectExpr (_, AnnNote note expr)
236 = liftM (vNote note) (vectExpr expr)
237
238 vectExpr e@(_, AnnApp _ arg)
239 | isAnnTypeArg arg
240 = vectTyAppExpr fn tys
241 where
242 (fn, tys) = collectAnnTypeArgs e
243
244 vectExpr (_, AnnApp (_, AnnVar v) (_, AnnLit lit))
245 | Just con <- isDataConId_maybe v
246 , is_special_con con
247 = do
248 let vexpr = App (Var v) (Lit lit)
249 lexpr <- liftPA vexpr
250 return (vexpr, lexpr)
251 where
252 is_special_con con = con `elem` [intDataCon, floatDataCon, doubleDataCon]
253
254
255 vectExpr (_, AnnApp fn arg)
256 = do
257 arg_ty' <- vectType arg_ty
258 res_ty' <- vectType res_ty
259 fn' <- vectExpr fn
260 arg' <- vectExpr arg
261 mkClosureApp arg_ty' res_ty' fn' arg'
262 where
263 (arg_ty, res_ty) = splitFunTy . exprType $ deAnnotate fn
264
265 vectExpr (_, AnnCase scrut bndr ty alts)
266 | Just (tycon, ty_args) <- splitTyConApp_maybe scrut_ty
267 , isAlgTyCon tycon
268 = vectAlgCase tycon ty_args scrut bndr ty alts
269 where
270 scrut_ty = exprType (deAnnotate scrut)
271
272 vectExpr (_, AnnCase expr bndr ty alts)
273 = panic "vectExpr: case"
274
275 vectExpr (_, AnnLet (AnnNonRec bndr rhs) body)
276 = do
277 vrhs <- localV . inBind bndr $ vectPolyExpr rhs
278 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
279 return $ vLet (vNonRec vbndr vrhs) vbody
280
281 vectExpr (_, AnnLet (AnnRec bs) body)
282 = do
283 (vbndrs, (vrhss, vbody)) <- vectBndrsIn bndrs
284 $ liftM2 (,)
285 (zipWithM vect_rhs bndrs rhss)
286 (vectPolyExpr body)
287 return $ vLet (vRec vbndrs vrhss) vbody
288 where
289 (bndrs, rhss) = unzip bs
290
291 vect_rhs bndr rhs = localV
292 . inBind bndr
293 $ vectExpr rhs
294
295 vectExpr e@(fvs, AnnLam bndr _)
296 | not (isId bndr) = pprPanic "vectExpr" (ppr $ deAnnotate e)
297 | otherwise = vectLam fvs bs body
298 where
299 (bs,body) = collectAnnValBinders e
300
301 vectExpr e = pprPanic "vectExpr" (ppr $ deAnnotate e)
302
303 vectLam :: VarSet -> [Var] -> CoreExprWithFVs -> VM VExpr
304 vectLam fvs bs body
305 = do
306 tyvars <- localTyVars
307 (vs, vvs) <- readLEnv $ \env ->
308 unzip [(var, vv) | var <- varSetElems fvs
309 , Just vv <- [lookupVarEnv (local_vars env) var]]
310
311 arg_tys <- mapM (vectType . idType) bs
312 res_ty <- vectType (exprType $ deAnnotate body)
313
314 buildClosures tyvars vvs arg_tys res_ty
315 . hoistPolyVExpr tyvars
316 $ do
317 lc <- builtin liftingContext
318 (vbndrs, vbody) <- vectBndrsIn (vs ++ bs)
319 (vectExpr body)
320 return $ vLams lc vbndrs vbody
321
322 vectTyAppExpr :: CoreExprWithFVs -> [Type] -> VM VExpr
323 vectTyAppExpr (_, AnnVar v) tys = vectPolyVar v tys
324 vectTyAppExpr e tys = pprPanic "vectTyAppExpr" (ppr $ deAnnotate e)
325
326 type CoreAltWithFVs = AnnAlt Id VarSet
327
328 -- We convert
329 --
330 -- case e :: t of v { ... }
331 --
332 -- to
333 --
334 -- V: let v' = e in case v' of _ { ... }
335 -- L: let v' = e in case v' `cast` ... of _ { ... }
336 --
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.
340 --
341
342 -- FIXME: this is too lazy
343 vectAlgCase tycon ty_args scrut bndr ty [(DEFAULT, [], body)]
344 = do
345 vscrut <- vectExpr scrut
346 vty <- vectType ty
347 lty <- mkPArrayType vty
348 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
349 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
350
351 vectAlgCase tycon ty_args scrut bndr ty [(DataAlt dc, [], body)]
352 = do
353 vscrut <- vectExpr scrut
354 vty <- vectType ty
355 lty <- mkPArrayType vty
356 (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
357 return $ vCaseDEFAULT vscrut vbndr vty lty vbody
358
359 vectAlgCase tycon ty_args scrut bndr ty [(DataAlt dc, bndrs, body)]
360 = do
361 vect_tc <- maybeV (lookupTyCon tycon)
362 vty <- vectType ty
363 lty <- mkPArrayType vty
364 vexpr <- vectExpr scrut
365 (vbndr, (vbndrs, vbody)) <- vect_scrut_bndr
366 . vectBndrsIn bndrs
367 $ vectExpr body
368
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
376 where
377 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
378 | otherwise = vectBndrIn bndr
379
380 vectAlgCase tycon ty_args scrut bndr ty alts
381 = do
382 vect_tc <- maybeV (lookupTyCon tycon)
383 vty <- vectType ty
384 lty <- mkPArrayType vty
385
386 repr <- mkRepr vect_tc
387 shape_bndrs <- arrShapeVars repr
388 (len, sel, indices) <- arrSelector repr (map Var shape_bndrs)
389
390 (vbndr, valts) <- vect_scrut_bndr $ mapM (proc_alt sel vty lty) alts'
391 let (vect_dcs, vect_bndrss, lift_bndrss, vbodies) = unzip4 valts
392
393 vexpr <- vectExpr scrut
394 (vscrut, arr_tc, arg_tys) <- mkVScrut (vVar vbndr)
395 let [arr_dc] = tyConDataCons arr_tc
396
397 let (vect_scrut, lift_scrut) = vscrut
398 (vect_bodies, lift_bodies) = unzip vbodies
399
400 let vect_case = Case vect_scrut (mkWildId (exprType vect_scrut)) vty
401 (zipWith3 mk_vect_alt vect_dcs vect_bndrss vect_bodies)
402
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,
406 lbody)]
407
408 return . vLet (vNonRec vbndr vexpr)
409 $ (vect_case, lift_case)
410 where
411 vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr (fsLit "scrut")
412 | otherwise = vectBndrIn bndr
413
414 alts' = sortBy (\(alt1, _, _) (alt2, _, _) -> cmp alt1 alt2) alts
415
416 cmp (DataAlt dc1) (DataAlt dc2) = dataConTag dc1 `compare` dataConTag dc2
417 cmp DEFAULT DEFAULT = EQ
418 cmp DEFAULT _ = LT
419 cmp _ DEFAULT = GT
420
421 proc_alt sel vty lty (DataAlt dc, bndrs, body)
422 = do
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 vty lty
429 $ vectExpr body
430
431 return (vect_dc, vect_bndrs, lift_bndrs, vbody)
432
433 vect_alt_bndrs [] p
434 = do
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)
440 e <- p len
441 return ([], [bndr], e)
442
443 vect_alt_bndrs bndrs p
444 = localV
445 $ do
446 vbndrs <- mapM vectBndr bndrs
447 let (vect_bndrs, lift_bndrs) = unzip vbndrs
448 vv : _ = vect_bndrs
449 lv : _ = lift_bndrs
450 len <- lengthPA (idType vv) (Var lv)
451 e <- p len
452 return (vect_bndrs, lift_bndrs, e)
453
454 mk_vect_alt vect_dc bndrs body = (DataAlt vect_dc, bndrs, body)
455
456 packLiftingContext :: CoreExpr -> CoreExpr -> CoreExpr -> VarSet
457 -> Type -> Type -> VM VExpr -> VM VExpr
458 packLiftingContext len shape tag fvs vty lty p
459 = do
460 select <- builtin selectPAIntPrimVar
461 let sel_expr = mkApps (Var select) [shape, tag]
462 sel_var <- newLocalVar (fsLit "sel#") (exprType sel_expr)
463 lc_var <- builtin liftingContext
464 localV $
465 do
466 bnds <- mapM (packFreeVar (Var lc_var) (Var sel_var))
467 . filter isLocalId
468 $ varSetElems fvs
469 (vexpr, lexpr) <- p
470 empty <- emptyPA vty
471 return (vexpr, Let (NonRec sel_var sel_expr)
472 $ Case len lc_var lty
473 [(DEFAULT, [], mkLets (concat bnds) lexpr),
474 (LitAlt (mkMachInt 0), [], empty)])
475
476 packFreeVar :: CoreExpr -> CoreExpr -> Var -> VM [CoreBind]
477 packFreeVar len sel v
478 = do
479 r <- lookupVar v
480 case r of
481 Local (vv,lv) ->
482 do
483 lv' <- cloneVar lv
484 expr <- packPA (idType vv) (Var lv) len sel
485 updLEnv (upd vv lv')
486 return [(NonRec lv' expr)]
487
488 _ -> return []
489 where
490 upd vv lv' env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv') }
491