Skip to content

Commit edebc73

Browse files
committed
Eliminate unused foreign declarations
1 parent 189173d commit edebc73

3 files changed

Lines changed: 37 additions & 367 deletions

File tree

lib/Language/PureScript/Backend/IR/DCE.hs

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,17 @@ eliminateDeadCode uber@UberModule {..} =
4141
}
4242
where
4343
preservedForeigns [(ModuleName, FilePath, NonEmpty Name)]
44-
preservedForeigns = uberModuleForeigns
44+
preservedForeigns = do
45+
(modname, path, foreignNames) annotatedForeigns
46+
case NE.nonEmpty (preservedNames foreignNames) of
47+
Nothing []
48+
Just names [(modname, path, names)]
49+
where
50+
preservedNames [(Id, Name)] [Name]
51+
preservedNames foreignNames = do
52+
(idName, foreignName) foreignNames
53+
guard $ idName `Set.member` reachableIds
54+
pure foreignName
4555

4656
preserveBindings [Grouping (QName, Exp)]
4757
preserveBindings = do
@@ -68,12 +78,15 @@ eliminateDeadCode uber@UberModule {..} =
6878

6979
annotatedExports [(Id, Name, AExp)]
7080
annotatedBindings [Grouping (Id, QName, AExp)]
71-
(annotatedExports, annotatedBindings) = runAnnM do
81+
annotatedForeigns [(ModuleName, FilePath, [(Id, Name)])]
82+
(annotatedExports, annotatedBindings, annotatedForeigns) = runAnnM do
7283
annExports forM uberModuleExports \(name, expr)
7384
(,name,) <$> nextId <*> annotateExp expr
7485
annBindings forM uberModuleBindings $ traverse \(qname, expr)
7586
(,qname,) <$> nextId <*> annotateExp expr
76-
pure (annExports, annBindings)
87+
annForeignNames forM uberModuleForeigns \(modname, path, names)
88+
(modname,path,) <$> forM (toList names) \name fmap (,name) nextId
89+
pure (annExports, annBindings, annForeignNames)
7790

7891
dceAnnotatedExp AExp Exp
7992
dceAnnotatedExp =
@@ -128,7 +141,15 @@ eliminateDeadCode uber@UberModule {..} =
128141

129142
adjacencyList [((), Id, [Id])]
130143
adjacencyList =
131-
DL.toList $ adjacencyListFromExports <> adjacencyListFromBindings
144+
DL.toList $
145+
adjacencyListFromExports
146+
<> adjacencyListFromBindings
147+
<> adjacencyListFromForeigns
148+
149+
adjacencyListFromExports DList ((), Id, [Id])
150+
adjacencyListFromExports =
151+
annotatedExports & foldMap \(nodeId, _name, expr)
152+
adjacencyListFromExport nodeId expr
132153

133154
adjacencyListFromBindings DList ((), Id, [Id])
134155
adjacencyListFromBindings =
@@ -139,18 +160,22 @@ eliminateDeadCode uber@UberModule {..} =
139160
recBinds & foldMap \(nodeId, _qname, expr)
140161
adjacencyListForExpr bindingsInScope (nodeId, expr)
141162

142-
adjacencyListFromExports DList ((), Id, [Id])
143-
adjacencyListFromExports =
144-
annotatedExports & foldMap \(nodeId, _name, expr)
145-
adjacencyListFromExport nodeId expr
163+
adjacencyListFromForeigns DList ((), Id, [Id])
164+
adjacencyListFromForeigns =
165+
annotatedForeigns & foldMap \(_modname, _path, names)
166+
DL.fromList (((),,[]) . fst <$> names)
146167

147168
bindingsInScope Map (Qualified Name, Index) Id
148169
bindingsInScope =
149-
Map.fromList
150-
[ ((Imported m name, 0), bindId)
170+
Map.fromList $
171+
[ ((Imported modname name, 0), bindId)
151172
| grouping annotatedBindings
152-
, (bindId, QName m name, _boundExpr) listGrouping grouping
173+
, (bindId, QName modname name, _boundExpr) listGrouping grouping
153174
]
175+
<> [ ((Imported modname name, 0), foreignNameId)
176+
| (modname, _path, foreignNames) annotatedForeigns
177+
, (foreignNameId, name) foreignNames
178+
]
154179

155180
adjacencyListFromExport Id AExp DList ((), Id, [Id])
156181
adjacencyListFromExport = curry (adjacencyListForExpr bindingsInScope)

test/ps/output/Golden.TestHelloPrelude/golden.ir

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -344,50 +344,9 @@ UberModule
344344
)
345345
], uberModuleForeigns =
346346
[
347-
( ModuleName "Data.Symbol", ".spago/prelude/v6.0.3/src/Data/Symbol.purs", Name "unsafeCoerce" :| []
348-
),
349347
( ModuleName "Data.Unit", ".spago/prelude/v6.0.3/src/Data/Unit.purs", Name "unit" :| [] ),
350-
( ModuleName "Record.Unsafe", ".spago/prelude/v6.0.3/src/Record/Unsafe.purs", Name "unsafeHas" :|
351-
[ Name "unsafeGet", Name "unsafeSet", Name "unsafeDelete" ]
352-
),
353-
( ModuleName "Data.HeytingAlgebra", ".spago/prelude/v6.0.3/src/Data/HeytingAlgebra.purs", Name "boolConj" :|
354-
[ Name "boolDisj", Name "boolNot" ]
355-
),
356-
( ModuleName "Data.Eq", ".spago/prelude/v6.0.3/src/Data/Eq.purs", Name "eqBooleanImpl" :|
357-
[ Name "eqIntImpl", Name "eqNumberImpl", Name "eqCharImpl", Name "eqStringImpl", Name "eqArrayImpl"
358-
]
359-
),
360-
( ModuleName "Data.Semigroup", ".spago/prelude/v6.0.3/src/Data/Semigroup.purs", Name "concatString" :|
361-
[ Name "concatArray" ]
362-
),
363-
( ModuleName "Data.Show", ".spago/prelude/v6.0.3/src/Data/Show.purs", Name "showIntImpl" :|
364-
[ Name "showNumberImpl", Name "showCharImpl", Name "showStringImpl", Name "showArrayImpl" ]
365-
),
366-
( ModuleName "Data.Semiring", ".spago/prelude/v6.0.3/src/Data/Semiring.purs", Name "intAdd" :|
367-
[ Name "intMul", Name "numAdd", Name "numMul" ]
368-
),
369-
( ModuleName "Data.Ring", ".spago/prelude/v6.0.3/src/Data/Ring.purs", Name "intSub" :|
370-
[ Name "numSub" ]
371-
),
372-
( ModuleName "Data.Ord", ".spago/prelude/v6.0.3/src/Data/Ord.purs", Name "ordBooleanImpl" :|
373-
[ Name "ordIntImpl", Name "ordNumberImpl", Name "ordStringImpl", Name "ordCharImpl", Name "ordArrayImpl"
374-
]
375-
),
376-
( ModuleName "Data.Functor", ".spago/prelude/v6.0.3/src/Data/Functor.purs", Name "arrayMap" :| []
377-
),
378-
( ModuleName "Control.Apply", ".spago/prelude/v6.0.3/src/Control/Apply.purs", Name "arrayApply" :| []
379-
),
380-
( ModuleName "Control.Bind", ".spago/prelude/v6.0.3/src/Control/Bind.purs", Name "arrayBind" :| []
381-
),
382-
( ModuleName "Data.Bounded", ".spago/prelude/v6.0.3/src/Data/Bounded.purs", Name "topInt" :|
383-
[ Name "bottomInt", Name "topChar", Name "bottomChar", Name "topNumber", Name "bottomNumber"
384-
]
385-
),
386-
( ModuleName "Data.EuclideanRing", ".spago/prelude/v6.0.3/src/Data/EuclideanRing.purs", Name "intDegree" :|
387-
[ Name "intDiv", Name "intMod", Name "numDiv" ]
388-
),
389348
( ModuleName "Effect", ".spago/effect/v4.0.0/src/Effect.purs", Name "pureE" :|
390-
[ Name "bindE", Name "untilE", Name "whileE", Name "forE", Name "foreachE" ]
349+
[ Name "bindE" ]
391350
)
392351
], uberModuleExports =
393352
[

0 commit comments

Comments
 (0)