Skip to content

Commit

Permalink
live vars
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszcz authored and janmasrovira committed Jun 18, 2024
1 parent 320e2cd commit 69c10ca
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/Juvix/Compiler/Reg/Data/TransformationId.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ data PipelineId

type TransformationLikeId = TransformationLikeId' TransformationId PipelineId

-- Note: this works only because for now we mark all variables as live. Liveness
-- information needs to be re-computed after copy propagation.
toCTransformations :: [TransformationId]
toCTransformations = [Cleanup, CopyPropagation]

Expand Down
10 changes: 10 additions & 0 deletions src/Juvix/Compiler/Reg/Extra/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,13 @@ overValueRefs f = \case

goBlock :: InstrBlock -> InstrBlock
goBlock x = x

updateLiveVars' :: (VarRef -> Maybe VarRef) -> Instruction -> Instruction
updateLiveVars' f = \case
Prealloc x -> Prealloc $ over instrPreallocLiveVars (mapMaybe f) x
Call x -> Call $ over instrCallLiveVars (mapMaybe f) x
CallClosures x -> CallClosures $ over instrCallClosuresLiveVars (mapMaybe f) x
instr -> instr

updateLiveVars :: (VarRef -> VarRef) -> Instruction -> Instruction
updateLiveVars f = updateLiveVars' (Just . f)
2 changes: 1 addition & 1 deletion src/Juvix/Compiler/Reg/Transformation/CopyPropagation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ copyPropagateFunction =
(mpv', instr')
where
instr' = overValueRefs (adjustVarRef mpv) instr
mpv' = maybe mpv (filterOutVars mpv) (getResultVar instr')
mpv' = maybe mpv (filterOutVars mpv) (getResultVar instr)

filterOutVars :: VarMap -> VarRef -> VarMap
filterOutVars mpv v = HashMap.delete v $ HashMap.filter (/= v) mpv
Expand Down
11 changes: 2 additions & 9 deletions src/Juvix/Compiler/Reg/Transformation/SSA.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,13 @@ computeFunctionSSA =
where
go :: Instruction -> IndexMap VarRef -> (IndexMap VarRef, Instruction)
go instr mp = case getResultVar instr' of
Just vref -> (mp', updateLiveVars mp' (setResultVar instr' (mkVarRef VarGroupLocal idx)))
Just vref -> (mp', updateLiveVars' (adjustVarRef' mp') (setResultVar instr' (mkVarRef VarGroupLocal idx)))
where
(idx, mp') = IndexMap.assign mp vref
Nothing -> (mp, updateLiveVars mp instr')
Nothing -> (mp, updateLiveVars' (adjustVarRef' mp) instr')
where
instr' = overValueRefs (adjustVarRef mp) instr

updateLiveVars :: IndexMap VarRef -> Instruction -> Instruction
updateLiveVars mp = \case
Prealloc x -> Prealloc $ over instrPreallocLiveVars (mapMaybe (adjustVarRef' mp)) x
Call x -> Call $ over instrCallLiveVars (mapMaybe (adjustVarRef' mp)) x
CallClosures x -> CallClosures $ over instrCallClosuresLiveVars (mapMaybe (adjustVarRef' mp)) x
instr -> instr

-- For branches, when necessary we insert assignments unifying the renamed
-- output variables into a single output variable for both branches.
combine :: Instruction -> NonEmpty (IndexMap VarRef) -> (IndexMap VarRef, Instruction)
Expand Down

0 comments on commit 69c10ca

Please sign in to comment.