Skip to content

Commit

Permalink
Work on frontend stuff for fusion, in variant 93:
Browse files Browse the repository at this point in the history
- Propagation of the necessary info uptil Core, where the info is made available in map, separate from the AST.

Example, in which 'fuse f' is intended to switch on fusion for the
function 'f', 'convert g,h' signals a build/unbuild pair (this is likely
to change depending on how implementation further proceeds):

{-# LANGUAGE Fusion #-}
{-# LANGUAGE NoGenericDeriving #-}

module Fuse1 where

f x = x
fuse f

data X a = X a
data Y a = Y a

h :: X a -> Y a
h (X a) = Y a

g :: Y a -> X a
g (Y a) = X a

convert g, h

main = return ()
  • Loading branch information
atzedijkstra committed Sep 6, 2011
1 parent 25120c0 commit 62c263b
Show file tree
Hide file tree
Showing 19 changed files with 249 additions and 74 deletions.
13 changes: 13 additions & 0 deletions EHC/src/ehc/AbstractCore.chs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ data ACoreBindAspectKey
| ACoreBindAspectKey_Strict -- the as strict as possible variant
| ACoreBindAspectKey_Debug -- internal debugging only
| ACoreBindAspectKey_Core -- core
%%[[93
| ACoreBindAspectKey_FusionRole -- fusion role
%%]]
deriving (Eq,Ord,Enum)

instance Show ACoreBindAspectKey where
Expand All @@ -263,6 +266,9 @@ instance Show ACoreBindAspectKey where
show ACoreBindAspectKey_RelevTy = "rty"
show ACoreBindAspectKey_Debug = "dbg"
show ACoreBindAspectKey_Core = "core"
%%[[93
show ACoreBindAspectKey_FusionRole = "fusionrole"
%%]]

instance PP ACoreBindAspectKey where
pp = pp . show
Expand Down Expand Up @@ -311,6 +317,13 @@ acbaspkeyDebug = acbaspkeyMk
[ ACoreBindAspectKey_Debug ]
%%]

%%[(93 codegen) hs export(acbaspkeyFusionRole)
-- | predefined:
acbaspkeyFusionRole :: ACoreBindAspectKeyS
acbaspkeyFusionRole = acbaspkeyMk
[ ACoreBindAspectKey_FusionRole ]
%%]

%%[(8 codegen) hs export(ppACBaspKeyS)
ppACBaspKeyS :: ACoreBindAspectKeyS -> PP_Doc
ppACBaspKeyS = ppCurlysCommas . Set.toList
Expand Down
4 changes: 2 additions & 2 deletions EHC/src/ehc/Core/CommonGathLamInfo.cag
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ ATTR
[ | | gathLamMp: LamMp ]

SEM CExpr
| Let lhs . gathLamMp = @binds.bindLamMp `Map.union` @body.gathLamMp
| Let lhs . gathLamMp = @binds.bindLamMp `Map.union` @body.gathLamMp
| * - Let Ann CaseAltFail
lhs . gathLamMp = Map.empty
lhs . gathLamMp = Map.empty
%%]

%%[(8 codegen)
Expand Down
28 changes: 21 additions & 7 deletions EHC/src/ehc/Core/Pretty.cag
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

%%[(8 codegen) hs import({%{EH}AnaDomain.Pretty})
%%]
%%[(8 codegen) hs import({%{EH}LamInfo})
%%]

%%[(8 codegen) hs import(qualified Data.Map as Map,qualified Data.Set as Set,{%{EH}Ty.Pretty})
%%]
Expand All @@ -28,16 +30,20 @@ WRAPPER
%%]

%%[(8 codegen) hs
ppCModule :: EHCOpts -> CModule -> PP_Doc
ppCModule opts cmod
= let t = wrap_CodeAGItf (sem_CodeAGItf (CodeAGItf_AGItf cmod)) Inh_CodeAGItf
ppCModule :: EHCOpts -> LamMp -> CModule -> PP_Doc
ppCModule opts lamMp cmod
= let t = wrap_CodeAGItf (sem_CodeAGItf (CodeAGItf_AGItf cmod))
(Inh_CodeAGItf
{ lamMp_Inh_CodeAGItf = lamMp
})
in (pp_Syn_CodeAGItf t)

ppCExpr :: CExpr -> PP_Doc
ppCExpr ce
= let t = wrap_CExpr (sem_CExpr ce)
(Inh_CExpr
{ varPPMp_Inh_CExpr = Map.empty
, lamMp_Inh_CExpr = Map.empty
})
in (pp_Syn_CExpr t)

Expand Down Expand Up @@ -71,9 +77,9 @@ ppHole i = "<" >|< pp i >|< ">"
ppOptCMetas :: CMetas -> PP_Doc
ppOptCMetas x
= let t = wrap_CMetas (sem_CMetas x)
(Inh_CMetas)
-- { varMp_Inh_CMetas = Map.empty
-- })
(Inh_CMetas
{ lamMp_Inh_CMetas = Map.empty
})
in (pp_Syn_CMetas t)
%%]

Expand Down Expand Up @@ -121,6 +127,14 @@ ppCurlyList pL xs = ppListSep "{ " " }" ", " $ map pL xs

%%]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Context
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%[(8 codegen)
ATTR CodeAGItf AllCodeNT [ lamMp: LamMp | | ]
%%]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Pretty printed code
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down Expand Up @@ -184,7 +198,7 @@ SEM CExpr
SEM CBind
| Bind lhs . pp = let p [a] = a
p as = ppCurlysSemisBlock as
in ppDef (ppCNm @nm) (p @bindAspects.ppL)
in ppDef (ppCNm @nm) (p $ @bindAspects.ppL ++ (maybe [] (\x -> [pp x]) $ Map.lookup @nm @lhs.lamMp))

SEM CBindAspect
| Bind lhs . pp = @bindMeta.pp >-< "=" >#< @expr.pp
Expand Down
10 changes: 9 additions & 1 deletion EHC/src/ehc/Core/Subst.cag
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ instance CSubstitutable CExpr CMetaVal CBind CBindAspect Ty CExpr where
= cSubstAppExpr False cs ce Nothing
%%]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Dummy value to stop higher order AG subst
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%[(8 codegen) hs
cStopSubst = CExpr_String "Core.Subst.cStopSubst: may not happen"
%%]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Deepness/shallowness of subst
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down Expand Up @@ -160,7 +168,7 @@ SEM CExpr
Hole HoleLet CoeArg ImplsApp ImplsLam
%%]]
inst . repl' : CExpr
inst . repl' = if @lhs.doDeepSubst && @isRepl then @replv else CExpr_CoeArg
inst . repl' = if @lhs.doDeepSubst && @isRepl then @replv else cStopSubst
%%]

%%[(8 codegen)
Expand Down
18 changes: 15 additions & 3 deletions EHC/src/ehc/EH/ExtraChecks.cag
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,20 @@ SEM Expr

%%[(4 hmtyinfer)
SEM Expr
| Let loc . occCycTyErrs = rngLift @range varmpOccurErr @tyVarMpDeclsL0 $ @cycTyVarMp_l {- `varmpUnion` @cycTyVarMp_g -}
| Let loc . occCycTyErrs = rngLift @range varmpOccurErr @tyVarMpDeclsL0 $ @cycTyVarMp_l {- `varmpUnion` @cycTyVarMp_g -}
%%]

%%[(5 hmtyinfer).Expr.TypeAs.cycVarMp
SEM Expr
| TypeAs loc . occCycTyErrs = rngLift @range varmpOccurErr @lhs.tyVarMp @cycVarMp
| TypeAs loc . occCycTyErrs = rngLift @range varmpOccurErr @lhs.tyVarMp @cycVarMp
%%]

%%[(99 hmtyinfer) -5.Expr.TypeAs.cycVarMp
%%]

%%[(6 hmtyinfer)
SEM Expr
| Let loc . occCycTyKiErrs = rngLift @range varmpOccurErr @decls.kiVarMp $ @cycTyKiVarMp_l {- `varmpUnion` @cycTyKiVarMp_g -}
| Let loc . occCycTyKiErrs = rngLift @range varmpOccurErr @decls.kiVarMp $ @cycTyKiVarMp_l {- `varmpUnion` @cycTyKiVarMp_g -}
%%]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down Expand Up @@ -220,3 +220,15 @@ SEM Decl
-> mke "has >1 constructors, of which >0 with fields"
_ -> []
%%]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Fusion
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%[(93 hmtyinfer)
SEM Decl
| FusionConv loc . foBuildInverse = let opts = unifyFIOpts { fiMbMkErrClash = Just $ rngLift @range Err_FusionBuildInverse }
in fitsIn opts @fe @lUniq_buildInverse @lhs.finTyVarMp @finConTy (tyArrowInverse @finAbsTy)
loc . lUniq_buildInverse : UNIQUEREF gUniq
%%]

12 changes: 9 additions & 3 deletions EHC/src/ehc/EH/FinalInfo.cag
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,19 @@ SEM AGItf

%%[(2 hmtyinfer)
SEM Decl
| TySig loc . finalTy = vgiTy $ panicJust "EH.Infer.Decl.TySig" $ valGamLookup @nm $ @lhs.finValGam
| Val loc . finalTy = maybe Ty_Any vgiTy $ valGamLookup @patExpr.topNm $ @lhs.finValGam
| TySig loc . finalTy = vgiTy $ panicJust "EH.Infer.Decl.TySig" $ valGamLookup @nm @lhs.finValGam
| Val loc . finalTy = maybe Ty_Any vgiTy $ valGamLookup @patExpr.topNm @lhs.finValGam
%%[[9
| Instance loc . finalTy = maybe Ty_Any vgiTy $ valGamLookup @dictNm $ @lhs.finValGam
| Instance loc . finalTy = maybe Ty_Any vgiTy $ valGamLookup @dictNm @lhs.finValGam
%%]]
%%]

%%[(93 hmtyinfer)
SEM Decl
| FusionConv loc . finConTy = vgiTy $ panicJust ("EH.Infer.Decl.FusionConv: " ++ show @conNm) $ valGamLookup @conNm @lhs.finValGam
. finAbsTy = vgiTy $ panicJust ("EH.Infer.Decl.FusionConv: " ++ show @absNm) $ valGamLookup @absNm @lhs.finValGam
%%]

%%[(8 hmtyinfer)
SEM PatExpr
| Var VarAs AppTop Rec DataFields Con IConst CConst SConst Irrefutable App
Expand Down
5 changes: 5 additions & 0 deletions EHC/src/ehc/EH/GatherError.cag
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,11 @@ SEM Decl
| FFI lhs . errSq = rngLift @range mkNestErr' @pp [foErrSq @foFFI]
%%]

%%[(93 hmtyinfer)
SEM Decl
| FusionConv lhs . errSq = rngLift @range mkNestErr' @pp [foErrSq @foBuildInverse]
%%]

%%[40
SEM DataConstr
| Constr lhs . errSq := rngLift @range mkNestErr' @pp [@fields.errSq, @eqs.errSq, foErrSq @fo_]
Expand Down
23 changes: 23 additions & 0 deletions EHC/src/ehc/EH/GatherMisc.cag
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,26 @@ SEM Decl
| Class lhs . gathHiddenExports = Seq.fromList [ (dflt,IdOcc_Val) | (_,dflt) <- @generDerivs ]
%%]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Gathering LamInfo relevant to any subsequent code generation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%[(8 codegen)
ATTR AGItf Expr AllDecl [ | | gathLamMp USE {`lamMpUnionBindAspMp`} {Map.empty}: LamMp ]

%%]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Gathering Fusion related LamInfo (probably better in separate file)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%[(93 codegen)
SEM Decl
| FusionDecl lhs . gathLamMp = Map.singleton
@fuseNm
(emptyLamInfo {laminfoBindAspMp = Map.singleton acbaspkeyFusionRole (LamInfoBindAsp_FusionRole FusionRole_Fuse)})
| FusionConv lhs . gathLamMp = Map.fromList
[ ( @conNm, emptyLamInfo {laminfoBindAspMp = Map.singleton acbaspkeyFusionRole (LamInfoBindAsp_FusionRole FusionRole_BuildLeft)} )
, ( @absNm, emptyLamInfo {laminfoBindAspMp = Map.singleton acbaspkeyFusionRole (LamInfoBindAsp_FusionRole FusionRole_BuildRight)} )
]
%%]
7 changes: 4 additions & 3 deletions EHC/src/ehc/EH/MainAG.cag
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
%%]
%%[(8 codegen) hs import({%{EH}Foreign.Extract})
%%]
%%[(8 codegen) hs import({%{EH}LamInfo})
%%]
%%[(90 codegen) hs import({%{EH}BuiltinPrims},{%{EH}Foreign})
%%]

Expand Down Expand Up @@ -189,6 +191,8 @@ WRAPPER AGItf
%%]
%%[(8 codegen tycore) ag import({EH/ToTyCore})
%%]
%%[8 ag import({EH/GatherMisc})
%%]

%%[(9 hmtyinfer) ag import({EH/ResolvePred},{EH/InferClass},{EH/InferClassCHR},{EH/ResolvePredCHR},{EH/InferDefault})
%%]
Expand All @@ -202,9 +206,6 @@ WRAPPER AGItf
%%[50 ag import({EH/UsedNames})
%%]

%%[92 ag import({EH/GatherMisc})
%%]

%%[92 ag import({EH/Generics})
%%]

Expand Down
33 changes: 15 additions & 18 deletions EHC/src/ehc/EHC/CompilePhase/FlowBetweenPhase.chs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ XXX
%%[9999 import({%{EH}Base.ForceEval})
%%]

-- Misc info: LamInfo/LamMp
%%[(8 codegen) hs import({%{EH}LamInfo})
%%]

-- for debug
%%[50 hs import({%{EH}Base.Debug},EH.Util.Pretty)
%%]
Expand Down Expand Up @@ -145,6 +149,9 @@ cpFlowEHSem1 modNm
dfg = prepFlow $! EHSem.gathClDfGam_Syn_AGItf ehSem
cs = prepFlow $! EHSem.gathChrStore_Syn_AGItf ehSem
%%]]
%%[[(50 hmtyinfer)
lm = prepFlow $! EHSem.gathLamMp_Syn_AGItf ehSem
%%]]
%%[[50
mmi = panicJust "cpFlowEHSem1.crsiModMp" $ Map.lookup modNm $ crsiModMp crsi
hii = ecuHIInfo ecu
Expand Down Expand Up @@ -176,15 +183,18 @@ cpFlowEHSem1 modNm
, HI.hiiClGam = clg
, HI.hiiClDfGam = dfg
, HI.hiiCHRStore = {- HI.hiiScopedPredStoreToList -} cs
-- , HI.hiiLamMp = lm
%%]]
}
%%]]
%%[[(8 codegen)
coreInh' = coreInh
%%[[8
{ Core2GrSem.dataGam_Inh_CodeAGItf = EHSem.gathDataGam_Syn_AGItf ehSem
{ Core2GrSem.dataGam_Inh_CodeAGItf = EHSem.gathDataGam_Syn_AGItf ehSem
, Core2GrSem.lamMp_Inh_CodeAGItf = EHSem.gathLamMp_Syn_AGItf ehSem
%%][50
{ Core2GrSem.dataGam_Inh_CodeAGItf = EHSem.dataGam_Inh_AGItf ehInh'
{ Core2GrSem.dataGam_Inh_CodeAGItf = EHSem.dataGam_Inh_AGItf ehInh'
, Core2GrSem.lamMp_Inh_CodeAGItf = lm `lamMpUnionBindAspMp` Core2GrSem.lamMp_Inh_CodeAGItf coreInh -- assumption: no duplicates, otherwise merging as done later has to be done
%%]]
}
%%]]
Expand All @@ -207,19 +217,6 @@ cpFlowEHSem1 modNm
%%]]
)
%%]]
%%[[102
; when (ehcOptVerbosity opts >= VerboseDebug)
(do { lift $ putStrLn $ fevShow "gathDataGam" dg
; lift $ putStrLn $ fevShow "gathValGam" vg
; lift $ putStrLn $ fevShow "gathTyGam" tg
; lift $ putStrLn $ fevShow "gathTyKiGam" tkg
; lift $ putStrLn $ fevShow "gathPolGam" pg
; lift $ putStrLn $ fevShow "gathKiGam" kg
; lift $ putStrLn $ fevShow "gathClGam" clg
; lift $ putStrLn $ fevShow "gathChrStore" cs
; lift $ putStrLn $ fevShow "cmodule" $ EHSem.cmodule_Syn_AGItf ehSem
})
%%]]
%%[[92
-- put back additional hidden exports
; cpUpdHiddenExports modNm $ Seq.toList $ EHSem.gathHiddenExports_Syn_AGItf ehSem
Expand Down Expand Up @@ -258,7 +255,7 @@ cpFlowHISem modNm
%%[[(50 codegen)
coreInh = crsiCoreInh crsi
coreInh' = coreInh
{ Core2GrSem.lamMp_Inh_CodeAGItf = (HI.hiiLamMp hiInfo) `Map.union` Core2GrSem.lamMp_Inh_CodeAGItf coreInh
{ Core2GrSem.lamMp_Inh_CodeAGItf = (HI.hiiLamMp hiInfo) `lamMpUnionBindAspMp` Core2GrSem.lamMp_Inh_CodeAGItf coreInh
}
%%]]
optim = crsiOptim crsi
Expand Down Expand Up @@ -294,7 +291,7 @@ cpFlowCoreSem modNm
hii = ecuHIInfo ecu
am = prepFlow $! Core2GrSem.gathLamMp_Syn_CodeAGItf coreSem
coreInh' = coreInh
{ Core2GrSem.lamMp_Inh_CodeAGItf = am `Map.union` Core2GrSem.lamMp_Inh_CodeAGItf coreInh
{ Core2GrSem.lamMp_Inh_CodeAGItf = am `lamMpUnionBindAspMp` Core2GrSem.lamMp_Inh_CodeAGItf coreInh -- assumption: old info can be overridden, otherwise merge should be done here
}
hii' = hii
%%[[(50 codegen grin)
Expand Down Expand Up @@ -323,7 +320,7 @@ cpFlowHILamMp modNm
hii = ecuHIInfo ecu

-- put back result: call info map (lambda arity, ...), overwriting previous entries
; cpUpdSI (\crsi -> crsi {crsiCoreInh = coreInh {Core2GrSem.lamMp_Inh_CodeAGItf = HI.hiiLamMp hii `Map.union` Core2GrSem.lamMp_Inh_CodeAGItf coreInh}})
; cpUpdSI (\crsi -> crsi {crsiCoreInh = coreInh {Core2GrSem.lamMp_Inh_CodeAGItf = HI.hiiLamMp hii `lamMpUnionBindAspMp` Core2GrSem.lamMp_Inh_CodeAGItf coreInh}})
}
%%]

Expand Down
15 changes: 11 additions & 4 deletions EHC/src/ehc/EHC/CompilePhase/Output.chs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ Output generation, on stdout or file
%%[50 import(qualified {%{EH}HI} as HI)
%%]

-- Core semantics
-- TBD: this depends on grin gen, but should also be available for Core, so in a CoreXXXSem
%%[(8 codegen grin) import(qualified {%{EH}Core.ToGrin} as Core2GrSem)
%%]

-- Core output
%%[(8 codegen) import({%{EH}Core},{%{EH}Core.Pretty})
%%]
Expand Down Expand Up @@ -100,16 +105,18 @@ cpOutputCoreModule :: Bool -> String -> String -> HsName -> CModule -> EHCompile
cpOutputCoreModule binary nmsuff suff modNm cMod
= do { cr <- get
; let (ecu,crsi,opts,fp) = crBaseInfo modNm cr
fpC = mkOutputFPath opts modNm fp (suff ++ nmsuff) -- for now nmsuff after suff, but should be inside name
fnC = fpathToStr fpC
fpC = mkOutputFPath opts modNm fp (suff ++ nmsuff) -- for now nmsuff after suff, but should be inside name
fnC = fpathToStr fpC
coreInh = crsiCoreInh crsi
lm = Core2GrSem.lamMp_Inh_CodeAGItf coreInh
%%[[8
; lift $ putPPFPath fpC (ppCModule opts cMod) 100
; lift $ putPPFPath fpC (ppCModule opts lm cMod) 100
%%][50
; lift (if binary
then do { fpathEnsureExists fpC -- should be in FPath equivalent of putSerializeFile
; putSerializeFile fnC cMod
}
else putPPFPath fpC (ppCModule opts cMod) 100
else putPPFPath fpC (ppCModule opts lm cMod) 100
)
%%]]
}
Expand Down
Loading

0 comments on commit 62c263b

Please sign in to comment.