-
Notifications
You must be signed in to change notification settings - Fork 147
/
Normalize.hs
383 lines (358 loc) · 15 KB
/
Normalize.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
{-|
Copyright : (C) 2012-2016, University of Twente,
2016 , Myrtle Software Ltd,
2017 , Google Inc.
License : BSD2 (see the file LICENSE)
Maintainer : Christiaan Baaij <christiaan.baaij@gmail.com>
Turn CoreHW terms into normalized CoreHW Terms
-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE BangPatterns #-}
{-# OPTIONS_GHC -Wno-unused-imports #-}
module Clash.Normalize where
import Data.Either
import Control.Concurrent.Supply (Supply)
import Control.Lens ((.=),(^.),_1,_4)
import qualified Control.Lens as Lens
import Control.Monad.State.Strict (State)
import Data.Binary (encode)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL
import Data.Either (partitionEithers)
import qualified Data.IntMap as IntMap
import Data.IntMap.Strict (IntMap)
import Data.List
(groupBy, intersect, mapAccumL, sortBy)
import qualified Data.Map as Map
import qualified Data.Maybe as Maybe
import qualified Data.Set as Set
import qualified Data.Set.Lens as Lens
import Data.Semigroup ((<>))
import Data.Text.Prettyprint.Doc (vcat)
import System.IO.Unsafe (unsafePerformIO)
import BasicTypes (InlineSpec (..))
import SrcLoc (SrcSpan,noSrcSpan)
import Clash.Annotations.BitRepresentation.Internal
(CustomReprs)
import Clash.Core.Evaluator (PrimEvaluator)
import Clash.Core.FreeVars
(freeLocalIds, globalIds, globalIdOccursIn, localIdDoesNotOccurIn)
import Clash.Core.Pretty (showPpr, ppr)
import Clash.Core.Subst
(deShadowTerm, extendGblSubstList, mkSubst, substTm)
import Clash.Core.Term (Term (..), collectArgsTicks)
import Clash.Core.Type (Type, splitCoreFunForallTy)
import Clash.Core.TyCon
(TyConMap, TyConName)
import Clash.Core.Util (mkApps, mkTicks, termType)
import Clash.Core.Var (Id, varName, varType)
import Clash.Core.VarEnv
(VarEnv, elemVarSet, eltsVarEnv, emptyInScopeSet, emptyVarEnv,
extendVarEnv, lookupVarEnv, mapVarEnv, mapMaybeVarEnv, mkInScopeSet,
mkVarEnv, mkVarSet, notElemVarEnv, notElemVarSet, nullVarEnv, unionVarEnv)
import Clash.Driver.Types
(BindingMap, ClashOpts (..), DebugLevel (..))
import Clash.Netlist.Types
(HWType (..), HWMap, FilteredHWType(..))
import Clash.Netlist.Util
(splitNormalized, coreTypeToHWType')
import Clash.Normalize.Strategy
import Clash.Normalize.Transformations
(appProp, bindConstantVar, caseCon, flattenLet, reduceConst, topLet,
reduceNonRepPrim, removeUnusedExpr)
import Clash.Normalize.Types
import Clash.Normalize.Util
import Clash.Primitives.Types (CompiledPrimMap)
import Clash.Rewrite.Combinators ((>->),(!->))
import Clash.Rewrite.Types
(RewriteEnv (..), RewriteState (..), bindings, curFun, dbgLevel, extra,
tcCache, topEntities, typeTranslator, customReprs, RewriteStep (..))
import Clash.Rewrite.Util
(apply, isUntranslatableType, runRewrite, runRewriteSession)
import Clash.Signal.Internal (ResetKind (..))
import Clash.Util
-- | Run a NormalizeSession in a given environment
runNormalization
:: ClashOpts
-- ^ Level of debug messages to print
-> Supply
-- ^ UniqueSupply
-> BindingMap
-- ^ Global Binders
-> (CustomReprs -> TyConMap -> Type ->
State HWMap (Maybe (Either String FilteredHWType)))
-- ^ Hardcoded Type -> HWType translator
-> CustomReprs
-> TyConMap
-- ^ TyCon cache
-> IntMap TyConName
-- ^ Tuple TyCon cache
-> PrimEvaluator
-- ^ Hardcoded evaluator (delta-reduction)
-> CompiledPrimMap
-- ^ Primitive Definitions
-> VarEnv Bool
-- ^ Map telling whether a components is part of a recursive group
-> [Id]
-- ^ topEntities
-> NormalizeSession a
-- ^ NormalizeSession to run
-> a
runNormalization opts supply globals typeTrans reprs tcm tupTcm eval primMap rcsMap topEnts
= runRewriteSession rwEnv rwState
where
rwEnv = RewriteEnv
(opt_dbgLevel opts)
typeTrans
tcm
tupTcm
eval
(mkVarSet topEnts)
reprs
rwState = RewriteState
0
globals
supply
(error $ $(curLoc) ++ "Report as bug: no curFun",noSrcSpan)
0
(IntMap.empty, 0)
normState
normState = NormalizeState
emptyVarEnv
Map.empty
emptyVarEnv
(opt_specLimit opts)
emptyVarEnv
(opt_inlineLimit opts)
(opt_inlineFunctionLimit opts)
(opt_inlineConstantLimit opts)
primMap
Map.empty
rcsMap
(opt_newInlineStrat opts)
(opt_ultra opts)
normalize
:: [Id]
-> NormalizeSession BindingMap
normalize [] = return emptyVarEnv
normalize top = do
(new,topNormalized) <- unzip <$> mapM normalize' top
newNormalized <- normalize (concat new)
return (unionVarEnv (mkVarEnv topNormalized) newNormalized)
normalize'
:: Id
-> NormalizeSession ([Id],(Id,(Id,SrcSpan,InlineSpec,Term)))
normalize' nm = do
exprM <- lookupVarEnv nm <$> Lens.use bindings
let nmS = showPpr (varName nm)
case exprM of
Just (nm',sp,inl,tm) -> do
tcm <- Lens.view tcCache
let (_,resTy) = splitCoreFunForallTy tcm (varType nm')
resTyRep <- not <$> isUntranslatableType False resTy
if resTyRep
then do
tmNorm <- normalizeTopLvlBndr nm (nm',sp,inl,tm)
let usedBndrs = Lens.toListOf globalIds (tmNorm ^. _4)
traceIf (nm `elem` usedBndrs)
(concat [ $(curLoc),"Expr belonging to bndr: ",nmS ," (:: "
, showPpr (varType (tmNorm ^. _1))
, ") remains recursive after normalization:\n"
, showPpr (tmNorm ^. _4) ])
(return ())
prevNorm <- mapVarEnv (Lens.view _1) <$> Lens.use (extra.normalized)
topEnts <- Lens.view topEntities
let toNormalize = filter (`notElemVarSet` topEnts)
$ filter (`notElemVarEnv` (extendVarEnv nm nm prevNorm)) usedBndrs
return (toNormalize,(nm,tmNorm))
else do
let usedBndrs = Lens.toListOf globalIds tm
prevNorm <- mapVarEnv (Lens.view _1) <$> Lens.use (extra.normalized)
topEnts <- Lens.view topEntities
let toNormalize = filter (`notElemVarSet` topEnts)
$ filter (`notElemVarEnv` (extendVarEnv nm nm prevNorm)) usedBndrs
lvl <- Lens.view dbgLevel
traceIf (lvl >= DebugFinal)
(concat [$(curLoc), "Expr belonging to bndr: ", nmS, " (:: "
, showPpr (varType nm')
, ") has a non-representable return type."
, " Not normalising:\n", showPpr tm] )
(return (toNormalize,(nm,(nm',sp,inl,tm))))
Nothing -> error $ $(curLoc) ++ "Expr belonging to bndr: " ++ nmS ++ " not found"
-- | Check whether the normalized bindings are non-recursive. Errors when one
-- of the components is recursive.
checkNonRecursive
:: BindingMap
-- ^ List of normalized binders
-> BindingMap
checkNonRecursive norm = case mapMaybeVarEnv go norm of
rcs | nullVarEnv rcs -> norm
rcs -> error $ $(curLoc) ++ "Callgraph after normalisation contains following recursive components: "
++ show (vcat [ ppr a <> ppr b
| (a,b) <- eltsVarEnv rcs
])
where
go (nm,_,_,tm) =
if nm `globalIdOccursIn` tm
then Just (nm,tm)
else Nothing
-- | Perform general \"clean up\" of the normalized (non-recursive) function
-- hierarchy. This includes:
--
-- * Inlining functions that simply \"wrap\" another function
cleanupGraph
:: Id
-> BindingMap
-> NormalizeSession BindingMap
cleanupGraph topEntity norm
| Just ct <- mkCallTree [] norm topEntity
= do ctFlat <- flattenCallTree ct
return (mkVarEnv $ snd $ callTreeToList [] ctFlat)
cleanupGraph _ norm = return norm
data CallTree = CLeaf (Id,(Id,SrcSpan,InlineSpec,Term))
| CBranch (Id,(Id,SrcSpan,InlineSpec,Term)) [CallTree]
mkCallTree
:: [Id]
-- ^ Visited
-> BindingMap
-- ^ Global binders
-> Id
-- ^ Root of the call graph
-> Maybe CallTree
mkCallTree visited bindingMap root
| Just rootTm <- lookupVarEnv root bindingMap
= let used = Set.toList $ Lens.setOf globalIds $ (rootTm ^. _4)
other = Maybe.mapMaybe (mkCallTree (root:visited) bindingMap) (filter (`notElem` visited) used)
in case used of
[] -> Just (CLeaf (root,rootTm))
_ -> Just (CBranch (root,rootTm) other)
mkCallTree _ _ _ = Nothing
stripArgs
:: [Id]
-> [Id]
-> [Either Term Type]
-> Maybe [Either Term Type]
stripArgs _ (_:_) [] = Nothing
stripArgs allIds [] args = if any mentionsId args
then Nothing
else Just args
where
mentionsId t = not $ null (either (Lens.toListOf freeLocalIds) (const []) t
`intersect`
allIds)
stripArgs allIds (id_:ids) (Left (Var nm):args)
| id_ == nm = stripArgs allIds ids args
| otherwise = Nothing
stripArgs _ _ _ = Nothing
flattenNode
:: CallTree
-> NormalizeSession (Either CallTree ((Id,Term),[CallTree]))
flattenNode c@(CLeaf (_,(_,_,NoInline,_))) = return (Left c)
flattenNode c@(CLeaf (nm,(_,_,_,e))) = do
isTopEntity <- elemVarSet nm <$> Lens.view topEntities
if isTopEntity then return (Left c) else do
tcm <- Lens.view tcCache
let norm = splitNormalized tcm e
case norm of
Right (ids,[(bId,bExpr)],_) -> do
let (fun,args,ticks) = collectArgsTicks bExpr
case stripArgs ids (reverse ids) (reverse args) of
Just remainder | bId `localIdDoesNotOccurIn` bExpr ->
return (Right ((nm,mkApps (mkTicks fun ticks) (reverse remainder)),[]))
_ -> return (Right ((nm,e),[]))
_ -> return (Right ((nm,e),[]))
flattenNode b@(CBranch (_,(_,_,NoInline,_)) _) =
return (Left b)
flattenNode b@(CBranch (nm,(_,_,_,e)) us) = do
isTopEntity <- elemVarSet nm <$> Lens.view topEntities
if isTopEntity then return (Left b) else do
tcm <- Lens.view tcCache
let norm = splitNormalized tcm e
case norm of
Right (ids,[(bId,bExpr)],_) -> do
let (fun,args,ticks) = collectArgsTicks bExpr
case stripArgs ids (reverse ids) (reverse args) of
Just remainder | bId `localIdDoesNotOccurIn` bExpr ->
return (Right ((nm,mkApps (mkTicks fun ticks) (reverse remainder)),us))
_ -> return (Right ((nm,e),us))
_ -> do
newInlineStrat <- Lens.use (extra.newInlineStrategy)
if newInlineStrat || isCheapFunction e
then return (Right ((nm,e),us))
else return (Left b)
flattenCallTree
:: CallTree
-> NormalizeSession CallTree
flattenCallTree c@(CLeaf _) = return c
flattenCallTree (CBranch (nm,(nm',sp,inl,tm)) used) = do
flattenedUsed <- mapM flattenCallTree used
(newUsed,il_ct) <- partitionEithers <$> mapM flattenNode flattenedUsed
let (toInline,il_used) = unzip il_ct
subst = extendGblSubstList (mkSubst emptyInScopeSet) toInline
newExpr <- case toInline of
[] -> return tm
_ -> do
-- To have a cheap `appProp` transformation we need to
-- deshadow, see also Note [AppProp no-shadow invariant]
let tm1 = deShadowTerm emptyInScopeSet (substTm "flattenCallTree.flattenExpr" subst tm)
#ifdef HISTORY
-- NB: When HISTORY is on, emit binary data holding the recorded rewrite steps
let !_ = unsafePerformIO
$ BS.appendFile "history.dat"
$ BL.toStrict
$ encode RewriteStep
{ t_ctx = []
, t_name = "INLINE"
, t_bndrS = showPpr (varName nm')
, t_before = tm
, t_after = tm1
}
#endif
rewriteExpr ("flattenExpr",flatten) (showPpr nm, tm1) (nm', sp)
let allUsed = newUsed ++ concat il_used
-- inline all components when the resulting expression after flattening
-- is still considered "cheap". This happens often at the topEntity which
-- wraps another functions and has some selectors and data-constructors.
if inl /= NoInline && isCheapFunction newExpr
then do
let (toInline',allUsed') = unzip (map goCheap allUsed)
subst' = extendGblSubstList (mkSubst emptyInScopeSet)
(Maybe.catMaybes toInline')
-- To have a cheap `appProp` transformation we need to
-- deshadow, see also Note [AppProp no-shadow invariant]
let tm1 = deShadowTerm emptyInScopeSet (substTm "flattenCallTree.flattenCheap" subst' newExpr)
newExpr' <- rewriteExpr ("flattenCheap",flatten) (showPpr nm, tm1) (nm', sp)
return (CBranch (nm,(nm',sp,inl,newExpr')) (concat allUsed'))
else return (CBranch (nm,(nm',sp,inl,newExpr)) allUsed)
where
flatten =
innerMost (apply "appProp" appProp >->
apply "bindConstantVar" bindConstantVar >->
apply "caseCon" caseCon >->
apply "reduceConst" reduceConst >->
apply "reduceNonRepPrim" reduceNonRepPrim >->
apply "removeUnusedExpr" removeUnusedExpr >->
apply "flattenLet" flattenLet) !->
topdownSucR (apply "topLet" topLet)
goCheap c@(CLeaf (nm2,(_,_,inl2,e)))
| inl2 == NoInline = (Nothing ,[c])
| otherwise = (Just (nm2,e),[])
goCheap c@(CBranch (nm2,(_,_,inl2,e)) us)
| inl2 == NoInline = (Nothing, [c])
| otherwise = (Just (nm2,e),us)
callTreeToList
:: [Id]
-> CallTree
-> ([Id],[(Id,(Id,SrcSpan,InlineSpec,Term))])
callTreeToList visited (CLeaf (nm,bndr))
| nm `elem` visited = (visited,[])
| otherwise = (nm:visited,[(nm,bndr)])
callTreeToList visited (CBranch (nm,bndr) used)
| nm `elem` visited = (visited,[])
| otherwise = (visited',(nm,bndr):(concat others))
where
(visited',others) = mapAccumL callTreeToList (nm:visited) used