Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import module collisions #235

Open
wants to merge 74 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
e1ec3aa
collision example
0xtimmy Oct 25, 2023
4e81af3
repeatable module import collisions
0xtimmy Oct 27, 2023
a2deafb
declaration renaming works
0xtimmy Nov 6, 2023
7ff377a
module rename can modify function callsites and function decalrations…
0xtimmy Nov 8, 2023
1b2cfaf
finished basic module import resolution
0xtimmy Nov 10, 2023
f05cbdd
parses modules in defs and main as well
0xtimmy Nov 11, 2023
2776226
added a pass to resolve imports in desugarModule
0xtimmy Nov 21, 2023
3d80c84
minor cleanup of README and gibbon-compiler/gibbon.cabal
ulysses4ever Nov 22, 2023
6145e02
aliases work
0xtimmy Nov 22, 2023
434712c
qualified imports
0xtimmy Nov 22, 2023
34e6618
spec imports
0xtimmy Nov 23, 2023
c8bb2d1
finished test cases
0xtimmy Nov 30, 2023
eca7cdd
Merge pull request #233 from ulysses4ever/ap-minor-cleanup
rrnewton Dec 6, 2023
97b9c39
fix? handling local identifier
0xtimmy Dec 7, 2023
2659da7
some testing
0xtimmy Dec 7, 2023
c3e9aca
fixed interp
0xtimmy Dec 10, 2023
22cc526
fixing a test (did not specify the sum function)
0xtimmy Dec 10, 2023
ddbae71
added ext parsing
0xtimmy Dec 11, 2023
cd2ec79
fixed an issue with LetE
0xtimmy Dec 11, 2023
b114741
some oddness
0xtimmy Dec 11, 2023
2f4992c
more bug fixing
0xtimmy Jan 11, 2024
19d4a65
add fixed linear ext handling
0xtimmy Jan 11, 2024
9bc6837
let _default contructors pass through
0xtimmy Jan 11, 2024
09c85cb
collision example
0xtimmy Oct 25, 2023
6020508
repeatable module import collisions
0xtimmy Oct 27, 2023
33f7d48
declaration renaming works
0xtimmy Nov 6, 2023
2a2c42a
module rename can modify function callsites and function decalrations…
0xtimmy Nov 8, 2023
c5f48c6
finished basic module import resolution
0xtimmy Nov 10, 2023
826eb0c
parses modules in defs and main as well
0xtimmy Nov 11, 2023
5acadd1
added a pass to resolve imports in desugarModule
0xtimmy Nov 21, 2023
27269e6
aliases work
0xtimmy Nov 22, 2023
7d61d81
qualified imports
0xtimmy Nov 22, 2023
0aa6bb3
spec imports
0xtimmy Nov 23, 2023
d42d258
finished test cases
0xtimmy Nov 30, 2023
a430af3
fix? handling local identifier
0xtimmy Dec 7, 2023
375baae
some testing
0xtimmy Dec 7, 2023
6847ca9
fixed interp
0xtimmy Dec 10, 2023
0350baf
fixing a test (did not specify the sum function)
0xtimmy Dec 10, 2023
da753a5
added ext parsing
0xtimmy Dec 11, 2023
f886311
fixed an issue with LetE
0xtimmy Dec 11, 2023
fc54de8
some oddness
0xtimmy Dec 11, 2023
fbeca95
more bug fixing
0xtimmy Jan 11, 2024
c3232d4
add fixed linear ext handling
0xtimmy Jan 11, 2024
8ce0e24
let _default contructors pass through
0xtimmy Jan 11, 2024
f20862f
notes from artem
0xtimmy Jan 31, 2024
ecc51cd
built bundler & added bundle flow to the parser and compiler
0xtimmy Jan 31, 2024
2e2e322
passes some tests
0xtimmy Feb 14, 2024
d5cf3f2
Merge branch 'import-module-collisions' into module-bundler
0xtimmy Feb 14, 2024
c3e1d6d
Merge pull request #255 from iu-parfunc/module-bundler
0xtimmy Feb 14, 2024
0df2511
fixing merge issue
0xtimmy Feb 14, 2024
9824c51
removed qualification from unique name (gets rid of the ugly {package…
0xtimmy Feb 14, 2024
ba66015
fixed .gib extension
0xtimmy Feb 15, 2024
237b0e5
fixed verbodity issue
0xtimmy Feb 17, 2024
e10b206
adding the fix to the interpreter
0xtimmy Feb 17, 2024
9662f04
PR cleanup
0xtimmy Feb 20, 2024
f66afdc
cleaning up build file
0xtimmy Feb 20, 2024
41d3b82
reverting common.hs
0xtimmy Feb 20, 2024
1349a7c
some more test fixes
0xtimmy Feb 21, 2024
86c50d6
some more test changes
0xtimmy Feb 21, 2024
6062341
alternatives for showing internal type names
0xtimmy Feb 21, 2024
d1ff39b
Merge branch 'main' of github.com:iu-parfunc/gibbon
0xtimmy Feb 21, 2024
c6b38d2
changed layout benches to print the blogs in a way that's independent…
0xtimmy Feb 21, 2024
24b33ba
removing interp changes
0xtimmy Feb 21, 2024
55373d2
handling _default in freshBundle CaseE
0xtimmy Feb 21, 2024
1387304
fixed more test cases
0xtimmy Feb 21, 2024
1d9fed9
fixing manyFuncs
0xtimmy Feb 21, 2024
4330552
solve layout constrs error
0xtimmy Feb 21, 2024
0d17d3a
test cases pass
0xtimmy Feb 21, 2024
65ab88c
Merge pull request #256 from iu-parfunc/test-case-changes
0xtimmy Feb 21, 2024
bbaa2d7
removed some tests missing from main
0xtimmy Feb 21, 2024
97eff1d
adressed comments and added more robust descriptions to all new modules
0xtimmy Feb 28, 2024
a335c8f
added a pass to resolve imports in desugarModule
0xtimmy Mar 8, 2024
ecac589
merged in main
0xtimmy Mar 8, 2024
6756e1d
removing extra files from diff
0xtimmy Jun 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
parses modules in defs and main as well
0xtimmy committed Nov 11, 2023
commit f05cbddbd330f443b90a255e897d3dce6f6f24be
2,757 changes: 2,735 additions & 22 deletions gibbon-compiler/ir.log

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion gibbon-compiler/src/Gibbon/HaskellFrontend.hs
Original file line number Diff line number Diff line change
@@ -200,6 +200,7 @@ desugarModule cfg pstate_ref import_route dir (Module _ head_mb _pragmas imports
let (defs, _vars, funs, inlines, main, optimizeDcons, userOrderings) =
foldr classify init_acc toplevels
userOrderings' = M.fromList $ coalese_constraints userOrderings
defs' = M.mapWithKey (\k dDef -> dDef {tyName = k }) (M.mapKeys (\k -> toVar (mod_name ++ "." ++ (fromVar k))) defs)
funs' = M.mapWithKey (\k funDef -> funDef {funName = k }) (M.mapKeys (\k -> toVar (mod_name ++ "." ++ (fromVar k))) funs) -- can insert function name here
--funs' = M.map (\funDef -> funDef {funMeta = funMeta {funModule = mod_name}}) funs -- can insert function name here
funs'' =
@@ -277,7 +278,7 @@ desugarModule cfg pstate_ref import_route dir (Module _ head_mb _pragmas imports
error $
"Conflicting definitions of " ++
show (S.toList em2) ++ " found in " ++ mod_name)
(defs, funs''')
(defs', funs''')
imported_progs'
pure (Prog defs0 funs0 main) --dbgTraceIt (sdoc funs) dbgTraceIt "\n" dbgTraceIt (sdoc funs''') dbgTraceIt (sdoc userOrderings') dbgTraceIt "\n" dbgTraceIt (sdoc userOrderings)
pure prog
175 changes: 117 additions & 58 deletions gibbon-compiler/src/Gibbon/Passes/ModuleRename.hs
Original file line number Diff line number Diff line change
@@ -24,65 +24,93 @@ type TyVarEnv t = M.Map TyVar t

moduleRename :: Prog0 -> PassM Prog0
moduleRename (Prog defs funs main) =
do --main' <- case main of
-- Nothing -> return Nothing
-- Just (m,ty) -> do m' <- freshExp M.empty M.empty m
-- return $ Just (m',ty)
--defs' <- traverse freshDDef defs
--funs' <- *traverse map* resolveFunModuleNames funs
do
-- defs
renamedDefs <- mapM (\k -> (gensym k)) (M.keys defs)
let defNameMap = M.fromList $ zip (M.keys defs) renamedDefs
let initDefEnv :: VarEnv = M.empty
let (defenv, _) = M.mapAccumWithKey buildEnv initDefEnv defNameMap
let transformDefKey :: Var -> Var
transformDefKey k = do
let (mod, name) = parseOutMod k
resolveNameInEnv mod name defenv

-- funs
renamedFuns <- mapM (\k -> (gensym k)) (M.keys funs)
let nameMap = M.fromList $ zip (M.keys funs) renamedFuns
let init :: VarEnv = M.empty
let (env, _) = M.mapAccumWithKey buildEnv init nameMap
--let funs' = M.mapKeys (\k -> resolveNameInEnv () env) funs
let _ = dbgPrintLn 2 "hello?"
let transformKey :: Var -> Var
transformKey k = do
let funNameMap = M.fromList $ zip (M.keys funs) renamedFuns
let initFunEnv :: VarEnv = M.empty
let (funenv, _) = M.mapAccumWithKey buildEnv initFunEnv funNameMap
let transformFunKey :: Var -> Var
transformFunKey k = do
let (mod, name) = parseOutMod k
resolveNameInEnv mod name env
funs' <- traverse (\v -> resolveModsInFuns v defs env) funs
let funs'' = M.mapKeys transformKey funs'
return $ Prog defs funs'' main
resolveNameInEnv mod name funenv

buildEnv :: VarEnv -> Var -> Var -> (VarEnv, Var)
buildEnv env k v = do
let (mod, name) = parseOutMod k
let mod' = case mod of
Just m -> m
Nothing -> (toVar "")
case (M.lookup name env) of
Just m ->
if (M.member mod' m) then error $ "duplicate function call in mod" ++ (fromVar mod')
else ((M.insert name (M.insert mod' v m) env), v)
Nothing -> ((M.insert name (M.singleton mod' v) env), v)
-- main
main' <- case main of
Nothing -> return Nothing
Just (m,ty) -> do m' <- resolveModInExp m defenv funenv
return $ Just (m',ty)

defs' <- traverse (\v -> resolveModsInDefs v defenv funenv) defs
funs' <- traverse (\v -> resolveModsInFuns v defenv funenv) funs

resolveNameInEnv :: Maybe Var -> Var -> VarEnv -> Var
resolveNameInEnv mod name e =
do case (M.lookup name e) of
Just modspace -> case mod of
Just m -> case (M.lookup m modspace) of
Just n -> n
Nothing -> error $ "can't find function " ++ (fromVar name) ++ " in module " ++ (fromVar m)
Nothing -> if(M.size modspace == 1) then head $ M.elems modspace
else error $ "can't find function " ++ (fromVar name)
Nothing -> error $ "can't find function " ++ (fromVar name)

freshDDef :: DDef Ty0 -> PassM (DDef Ty0)
freshDDef DDef{tyName,tyArgs,dataCons} =
do pure $ DDef tyName tyArgs dataCons
let defs'' = M.mapKeys transformDefKey defs'
let funs'' = M.mapKeys transformFunKey funs'

--funRename :: FunDef Exp0 -> FunDef Exp0
--funRename (FunDef nam nargs funty bod meta) = FunDef (gensym nam) nargs funty bod meta
return $ Prog defs'' funs'' main'

resolveModsInFuns :: FunDef Exp0 -> DDefs Ty0 -> VarEnv -> PassM (FunDef Exp0)
resolveModsInFuns (FunDef nam nargs funty bod meta) defs env =
resolveModsInDefs :: DDef Ty0 -> VarEnv -> VarEnv -> PassM (DDef Ty0)
resolveModsInDefs (DDef tyName tyArgs dataCons) defenv funenv =
do
let (mod, name) = parseOutMod nam
bod' <- resolveModInBod bod env
pure $ FunDef (resolveNameInEnv mod name env) nargs funty bod' meta
let (mod, name) = parseOutMod tyName
let dataCons' = map (\v -> resolveModsInDataCons v defenv funenv) dataCons
pure $ DDef (resolveNameInEnv mod name defenv) tyArgs dataCons'

resolveModInBod :: Exp0 -> VarEnv -> PassM Exp0
resolveModInBod exp env =
resolveModsInFuns :: FunDef Exp0 -> VarEnv -> VarEnv -> PassM (FunDef Exp0)
resolveModsInFuns (FunDef nam nargs funty bod meta) defenv funenv =
do
let nam' = parseAndResolve nam funenv
let funty' = resolveModsInTyScheme funty defenv
bod' <- resolveModInExp bod defenv funenv
pure $ FunDef nam' nargs funty' bod' meta

resolveModsInTyScheme :: TyScheme -> VarEnv -> TyScheme
resolveModsInTyScheme (ForAll tvs ty) defenv = do
let ty' = resolveModInTy ty defenv
ForAll tvs ty'

resolveModsInDataCons :: (DataCon, [(IsBoxed, Ty0)]) -> VarEnv -> VarEnv -> (DataCon, [(IsBoxed, Ty0)])
resolveModsInDataCons (con, tys) defenv funenv =
do
let tys' = map (\(boxed, ty) -> (boxed, (resolveModInTy ty defenv))) tys
(con, tys')

resolveModInTy :: Ty0 -> VarEnv -> Ty0
resolveModInTy ty defenv =
case ty of
IntTy -> ty
CharTy -> ty
FloatTy -> ty
SymTy0 -> ty
BoolTy -> ty
ArenaTy -> ty
SymSetTy -> ty
SymHashTy -> ty
MetaTv{} -> ty
TyVar tv -> ty
ProdTy tys -> ProdTy $ map (\v -> resolveModInTy v defenv) tys
SymDictTy v ty -> SymDictTy v $ resolveModInTy ty defenv
PDictTy k v -> PDictTy (resolveModInTy k defenv) (resolveModInTy v defenv)
ArrowTy tys t -> ArrowTy (map (\v -> resolveModInTy v defenv) tys) $ resolveModInTy t defenv

PackedTy tycon tys -> PackedTy (fromVar (parseAndResolve (toVar tycon) defenv)) $ map (\v -> resolveModInTy v defenv) tys

VectorTy el_t -> VectorTy $ resolveModInTy el_t defenv
ListTy el_t -> ListTy $ resolveModInTy el_t defenv
IntHashTy -> ty

resolveModInExp :: Exp0 -> VarEnv -> VarEnv -> PassM Exp0
resolveModInExp exp defenv funenv =
case exp of
LitE i -> return $ LitE i
CharE c -> return $ CharE c
@@ -93,29 +121,29 @@ resolveModInBod exp env =

AppE v locs ls -> do
let (mod, fun) = parseOutMod v
let v' = resolveNameInEnv mod fun env
let v' = resolveNameInEnv mod fun funenv
return $ AppE v' locs ls

PrimAppE p es -> return $ PrimAppE p es

LetE (v,_locs,ty, e1) e2 -> do
e1' <- resolveModInBod e1 env
e2' <- resolveModInBod e2 env
e1' <- resolveModInExp e1 defenv funenv
e2' <- resolveModInExp e2 defenv funenv
return $ LetE (v, [], ty, e1') e2'

IfE e1 e2 e3 -> return $ IfE e1 e2 e3
ProjE i e -> return $ ProjE i e
MkProdE es -> return $ MkProdE es

CaseE e mp -> do
e' <- resolveModInBod e env
e' <- resolveModInExp e defenv funenv
mp' <- mapM (\(c,prs,ae) -> do
ae' <- resolveModInBod ae env
ae' <- resolveModInExp ae defenv funenv
return (c, prs, ae')) mp
return $ CaseE e' mp'

DataConE loc c es -> do
es' <- traverse (\v -> resolveModInBod v env) es
es' <- traverse (\v -> resolveModInExp v defenv funenv) es
return $ DataConE loc c es'

TimeIt e t b -> return $ TimeIt e t b
@@ -126,9 +154,40 @@ resolveModInBod exp env =
FoldE e1 e2 e3 -> return $ FoldE e1 e2 e3
Ext ext -> return $ Ext ext


-- environment interactions

buildEnv :: VarEnv -> Var -> Var -> (VarEnv, Var)
buildEnv env k v = do
let (mod, name) = parseOutMod k
let mod' = case mod of
Just m -> m
Nothing -> (toVar "")
case (M.lookup name env) of
Just m ->
if (M.member mod' m) then error $ "duplicate function call in mod" ++ (fromVar mod')
else ((M.insert name (M.insert mod' v m) env), v)
Nothing -> ((M.insert name (M.singleton mod' v) env), v)

parseAndResolve :: Var -> VarEnv -> Var
parseAndResolve v env = do
let (mod, name) = parseOutMod v
resolveNameInEnv mod name env

parseOutMod :: Var -> (Maybe Var, Var)
parseOutMod v = do
let str = (fromVar v)
case (L.elemIndices '.' str) of
[] -> (Nothing, v)
x -> (Just (toVar (L.take (L.last x) str)), (toVar (L.drop ((L.last x)+1) str)))
x -> (Just (toVar (L.take (L.last x) str)), (toVar (L.drop ((L.last x)+1) str)))

resolveNameInEnv :: Maybe Var -> Var -> VarEnv -> Var
resolveNameInEnv mod name e =
do case (M.lookup name e) of
Just modspace -> case mod of
Just m -> case (M.lookup m modspace) of
Just n -> n
Nothing -> error $ "can't find " ++ (fromVar name) ++ " in module " ++ (fromVar m)
Nothing -> if(M.size modspace == 1) then head $ M.elems modspace
else error $ "can't find " ++ (fromVar name)
Nothing -> error $ "can't find " ++ (fromVar name)