Skip to content

Commit ca780a0

Browse files
committed
Wip: Introduce mutable cursors for updating cursor value in place
1 parent f352266 commit ca780a0

File tree

11 files changed

+165
-26
lines changed

11 files changed

+165
-26
lines changed

gibbon-compiler/src/Gibbon/L2/Syntax.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,7 @@ mapPacked fn t =
10011001
PtrTy -> PtrTy
10021002
CursorTy -> CursorTy
10031003
CursorArrayTy size -> CursorArrayTy size
1004+
MutCursorTy -> MutCursorTy
10041005
ArenaTy -> ArenaTy
10051006
VectorTy elty -> VectorTy elty
10061007
ListTy elty -> ListTy elty
@@ -1023,6 +1024,7 @@ constPacked c t =
10231024
PtrTy -> PtrTy
10241025
CursorTy -> CursorTy
10251026
CursorArrayTy size -> CursorArrayTy size
1027+
MutCursorTy -> MutCursorTy
10261028
ArenaTy -> ArenaTy
10271029
VectorTy el_ty -> VectorTy (constPacked c el_ty)
10281030
ListTy el_ty -> ListTy (constPacked c el_ty)

gibbon-compiler/src/Gibbon/L3/Syntax.hs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ data E3Ext loc dec =
6969
| MakeCursorArray Int [Var] -- ^ Make a Cursor Array from a list of Cursors. Returns a new variable for Cursor Array.
7070
| IndexCursorArray Var Int -- ^ Index into a Cursor Array
7171
| AddCursor Var (PreExp E3Ext loc dec) -- ^ Add a constant offset to a cursor variable
72+
| AddrOfCursor (PreExp E3Ext loc dec) -- ^ Take the address of a Cursor.
73+
| DerefMutCursor Var -- ^ Explicitly de-reference a mutable cursor
7274
| CastPtr Var dec -- ^ Cast a pointer to the specified type
7375
| SubPtr Var Var -- ^ Pointer subtraction
7476
| NewBuffer L2.Multiplicity -- ^ Create a new buffer, and return a cursor
@@ -81,7 +83,7 @@ data E3Ext loc dec =
8183
-- we'll probably represent (sizeof x) as (end_x - start_x) / INT
8284
| SizeOfScalar Var -- ^ sizeof(var)
8385
| BoundsCheck Int Var Var -- ^ Bytes required, region, write cursor
84-
| BoundsCheckVector [(Int, Var, Var)] -- ^ Bytes required, region, write cursor but for a vector of cursors and regions
86+
| BoundsCheckVector [(Int, Var, Var, (Var, Var))] -- ^ Bytes required, region, write cursor but for a vector of cursors and regions
8587
| IndirectionBarrier TyCon (Var,Var,Var,Var)
8688
-- ^ Do one of the following:
8789
-- (1) If it's a old-to-young indirection, record it in the remembered set.
@@ -288,11 +290,13 @@ cursorizeTy ty =
288290
PDictTy k v -> PDictTy (cursorizeTy k) (cursorizeTy v)
289291
PackedTy _ l -> case l of
290292
Single _ -> ProdTy [CursorTy, CursorTy]
291-
SoA _ flds -> ProdTy [CursorArrayTy (1 + length flds), CursorArrayTy (1 + length flds)]
293+
SoA _ flds -> ProdTy [CursorArrayTy (1 + length flds), CursorArrayTy (1 + length flds)]
292294
VectorTy el_ty' -> VectorTy $ cursorizeTy el_ty'
293295
ListTy el_ty' -> ListTy $ cursorizeTy el_ty'
294296
PtrTy -> PtrTy
295297
CursorTy -> CursorTy
298+
CursorArrayTy sz -> CursorArrayTy sz
299+
MutCursorTy -> MutCursorTy
296300
ArenaTy -> ArenaTy
297301
SymSetTy -> SymSetTy
298302
SymHashTy-> SymHashTy

gibbon-compiler/src/Gibbon/L3/Typecheck.hs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,11 @@ tcExp isSoA isPacked ddfs env exp = do
160160
cty <- lookupVar env cur exp
161161
ensureEqualTyModCursor isSoA exp cty CursorTy
162162
return IntTy
163-
163+
164+
{- VS: Ignoring the types of the arguments to gib grow region -}
165+
{- Should we check these? -}
164166
BoundsCheckVector bounds -> do
165-
_ <- mapM (\(_, bound, cur) -> do
167+
_ <- mapM (\(_, bound, cur, _) -> do
166168
rty <- lookupVar env bound exp
167169
ensureEqualTyModCursor isSoA exp rty CursorTy
168170
cty <- lookupVar env cur exp
@@ -249,6 +251,17 @@ tcExp isSoA isPacked ddfs env exp = do
249251
ensureEqualTyModCursor isSoA exp vty vty
250252
return CursorTy
251253

254+
AddrOfCursor expr -> do
255+
ety <- go expr
256+
ensureEqualTyModCursor isSoA expr ety CursorTy
257+
return MutCursorTy
258+
259+
DerefMutCursor v -> do
260+
vty <- lookupVar env v exp
261+
ensureEqualTyModCursor isSoA exp vty MutCursorTy
262+
return CursorTy
263+
264+
252265
MakeCursorArray _ vars -> do
253266
tys <- mapM (\v -> lookupVar env v exp) vars
254267
unless (all (== CursorTy) tys) $ throwError $ GenericTC ("Expected all vars to be of type CursorTy. Got: " ++ sdoc tys) exp

gibbon-compiler/src/Gibbon/L4/Syntax.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import Gibbon.Common
3131
import qualified Gibbon.Language as L
3232
import qualified Gibbon.L2.Syntax as L2
3333
import qualified Gibbon.L3.Syntax as L3
34+
import Gibbon.L3.Syntax (E3Ext(DerefMutCursor))
3435

3536

3637
--------------------------------------------------------------------------------
@@ -61,6 +62,7 @@ data Triv
6162
| SymTriv Word16 -- ^ An index into the symbol table.
6263
| ProdTriv [Triv] -- ^ Tuples
6364
| ProjTriv Int Triv -- ^ Projections
65+
| IndexCursorArrayTriv Int Triv -- ^ Indexing operation
6466
deriving (Show, Ord, Eq, Generic, NFData, Out)
6567

6668
typeOfTriv :: M.Map Var Ty -> Triv -> Ty
@@ -73,6 +75,7 @@ typeOfTriv env trv =
7375
BoolTriv{} -> BoolTy
7476
TagTriv{} -> TagTyPacked
7577
SymTriv{} -> SymTy
78+
IndexCursorArrayTriv{} -> CursorTy
7679
ProdTriv ts -> ProdTy (map (typeOfTriv env) ts)
7780
ProjTriv i trv1 -> case typeOfTriv env trv1 of
7881
ProdTy tys -> tys !! i
@@ -201,6 +204,7 @@ data Ty
201204
| RegionTy -- ^ Region start and a refcount
202205
| ChunkTy -- ^ Start and end pointers
203206
| CursorArrayTy Int
207+
| MutCursorTy
204208

205209
-- TODO: Make Ptrs more type safe like this:
206210
-- | StructPtrTy { fields :: [Ty] } -- ^ A pointer to a struct containing the given fields.
@@ -369,6 +373,8 @@ data Prim
369373
| IndexCursorArray
370374
| MakeCursorArray
371375
| CastPtr
376+
| AddrOfCursor
377+
| DerefMutCursor
372378

373379
deriving (Show, Ord, Eq, Generic, NFData, Out)
374380

@@ -449,6 +455,7 @@ fromL3Ty ty =
449455
L.PtrTy -> PtrTy
450456
L.CursorTy -> CursorTy
451457
L.CursorArrayTy size -> CursorArrayTy size
458+
L.MutCursorTy -> MutCursorTy
452459
-- L.PackedTy{} -> error "fromL3Ty: Cannot convert PackedTy"
453460
L.VectorTy el_ty -> VectorTy (fromL3Ty el_ty)
454461
_ -> IntTy -- [2019.06.10]: CSK, Why do we need this?

gibbon-compiler/src/Gibbon/Language/Syntax.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,9 @@ data UrTy loc
631631
| CursorArrayTy Int -- ^ An array of cursors for reading or writing multiple cursors.
632632
-- ^ The cursor may point to an unkwown type or to a fraction of a complete value.
633633
-- ^ It is a machine pointer that can point to any byte.
634-
-- ^ The Int is the number of cursors in the array.
634+
-- ^ The Int is the number of cursors in the array.
635+
| MutCursorTy -- ^ A reference to a CursorTy. This can be mutated in place.
636+
635637

636638
deriving (Show, Read, Ord, Eq, Generic, NFData, Functor, Foldable, Traversable, Out)
637639

gibbon-compiler/src/Gibbon/Passes/AddCastInstructions.hs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ addCastsExp fundef cenv env ex =
4949
bod' <- addCastsExp fundef cenv' env' bod
5050
let ex' = foldr (\expr acc -> expr acc) bod' let_expr
5151
pure $ ex'
52+
53+
LetE (v, locs, ty, rhs@(Ext (AddrOfCursor (Ext (IndexCursorArray _ _)))) ) bod -> do
54+
let new_env = extendVEnv v ty env
55+
(let_expr, cenv', env') <- case ty of
56+
MutCursorTy -> return $ ([LetE (v, locs, ty, rhs)], cenv, new_env)
57+
CursorTy -> error "Did not expect lhs of address of expression to be a cursor."
58+
CursorArrayTy _ -> error "Cannot take address of a CursorArray!\n"
59+
_ -> error "addCastsExp: Casting expressions others than cursors hot handled!\n"
60+
bod' <- addCastsExp fundef cenv' env' bod
61+
let ex' = foldr (\expr acc -> expr acc) bod' let_expr
62+
pure $ ex'
63+
5264
LetE (v, locs, ty, (Ext (MakeCursorArray len vars))) bod -> do
5365
let new_env = extendVEnv v ty env
5466
(new_insts, cenv', env', vars') <-
@@ -301,6 +313,11 @@ addCastsExp fundef cenv env ex =
301313
Nothing -> v
302314
e' <- go e
303315
pure (Ext $ AddCursor nv e')
316+
Ext (DerefMutCursor v) -> do
317+
let nv = case (M.lookup v cenv) of
318+
Just v' -> v'
319+
Nothing -> v
320+
pure (Ext $ DerefMutCursor nv)
304321
Ext (SubPtr a b) -> do
305322
let na = case (M.lookup a cenv) of
306323
Just v' -> v'
@@ -343,14 +360,20 @@ addCastsExp fundef cenv env ex =
343360
Ext (BoundsCheckVector bounds) -> do
344361
bounds' <-
345362
mapM
346-
( \(i, a, b) -> do
363+
( \(i, a, b, (a', b')) -> do
347364
let na = case (M.lookup a cenv) of
348365
Just v' -> v'
349366
Nothing -> a
350367
let nb = case (M.lookup b cenv) of
351368
Just v' -> v'
352369
Nothing -> b
353-
return $ (i, na, nb)
370+
let na' = case (M.lookup a' cenv) of
371+
Just v' -> v'
372+
Nothing -> a'
373+
let nb' = case (M.lookup b' cenv) of
374+
Just v' -> v'
375+
Nothing -> b'
376+
return $ (i, na, nb, (na', nb'))
354377
)
355378
bounds
356379
pure $ Ext (BoundsCheckVector bounds')

gibbon-compiler/src/Gibbon/Passes/Codegen.hs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import qualified Gibbon.Language as GL
3131
import Gibbon.DynFlags
3232
import Gibbon.L2.Syntax ( Multiplicity(..) )
3333
import Gibbon.L4.Syntax
34+
import Language.Haskell.Exts (var)
3435

3536
--------------------------------------------------------------------------------
3637

@@ -480,6 +481,7 @@ codegenTriv venv (ProdTriv ls) =
480481
codegenTriv venv (ProjTriv i trv) =
481482
let field = "field" ++ show i
482483
in [cexp| $(codegenTriv venv trv).$id:field |]
484+
codegenTriv venv (IndexCursorArrayTriv idx v) = [cexp| $(codegenTriv venv v)[$int:idx] |]
483485

484486

485487
-- Type environment
@@ -1072,11 +1074,17 @@ codegenTail venv fenv sort_fns (LetPrimCallT bnds prm rnds body) ty sync_deps =
10721074
--_new_chunk <- gensym "new_chunk"
10731075
--_chunk_start <- gensym "chunk_start"
10741076
--_chunk_end <- gensym "chunk_end"
1075-
ifConds <- mapM (\(ProdTriv [(IntTriv i),(VarTriv bound), (VarTriv cur)]) ->
1077+
ifConds <- mapM (\(ProdTriv [(IntTriv i),(VarTriv bound), (VarTriv cur), _]) ->
10761078
pure [cexp| ($id:cur + $int:i) > $id:bound |]
10771079
) rnds
1078-
ifBody <- mapM (\(ProdTriv [(IntTriv i),(VarTriv bound), (VarTriv cur)]) ->
1079-
pure [ C.BlockStm [cstm| gib_grow_region(& $id:cur, & $id:bound); |] ]
1080+
ifBody <- mapM (\(ProdTriv [_, _, _, ProdTriv [(VarTriv b), (VarTriv c)]]) -> do
1081+
{- TODO: VS: Maybe we should check loc too, but i think we desinged this such that it is
1082+
not needed! -}
1083+
let bty = M.lookup b venv
1084+
case bty of
1085+
Just CursorTy -> pure [ C.BlockStm [cstm| gib_grow_region(& $id:c, & $id:b); |] ]
1086+
Just MutCursorTy -> pure [ C.BlockStm [cstm| gib_grow_region(& $id:c, $id:b); |] ]
1087+
_ -> error "Did not expect variable type in gib_grow_region!\n"
10801088
) rnds
10811089
let condExpr = foldr1 (\c1 c2 -> [cexp| $exp:c1 || $exp:c2 |]) ifConds
10821090
let ifBody' = concat ifBody
@@ -1572,6 +1580,18 @@ codegenTail venv fenv sort_fns (LetPrimCallT bnds prm rnds body) ty sync_deps =
15721580
[ptr] = rnds
15731581
ptr' = codegenTriv venv ptr
15741582
return [ C.BlockDecl [cdecl| $ty:(codegenTy outT) $id:outV = ($ty:(codegenTy outT)) $exp:ptr'; |] ]
1583+
1584+
AddrOfCursor -> do
1585+
let [(outV, outT)] = bnds
1586+
[expr] = rnds
1587+
expr' = codegenTriv venv expr
1588+
return [ C.BlockDecl [cdecl| $ty:(codegenTy outT) $id:outV = &($exp:expr'); |] ]
1589+
1590+
DerefMutCursor -> do
1591+
let [(outV, outT)] = bnds
1592+
[var] = rnds
1593+
var' = codegenTriv venv var
1594+
return [ C.BlockDecl [cdecl| $ty:(codegenTy outT) $id:outV = *($exp:var'); |] ]
15751595

15761596
_ -> error $ "codegen: " ++ show prm ++ " unhandled."
15771597

@@ -1654,6 +1674,7 @@ codegenTy SymTy = [cty|typename GibSym|]
16541674
codegenTy PtrTy = [cty|typename GibPtr|] -- char* - Hack, this could be void* if we have enough casts. [2016.11.06]
16551675
codegenTy CursorTy = [cty|typename GibCursor|]
16561676
codegenTy (CursorArrayTy size) = [cty|typename GibCursor* |]
1677+
codegenTy MutCursorTy = [cty|typename GibCursor* |]
16571678
codegenTy RegionTy = [cty|typename GibChunk|]
16581679
codegenTy ChunkTy = [cty|typename GibChunk|]
16591680
codegenTy (ProdTy []) = [cty|unsigned char|]
@@ -1679,6 +1700,7 @@ makeName' SymTy = "GibSym"
16791700
makeName' BoolTy = "GibBool"
16801701
makeName' CursorTy = "GibCursor"
16811702
makeName' (CursorArrayTy{}) = "GibCursorPtr"
1703+
makeName' (MutCursorTy) = "GibMutCursor"
16821704
makeName' TagTyPacked = "GibPackedTag"
16831705
makeName' TagTyBoxed = "GibBoxedTag"
16841706
makeName' PtrTy = "GibPtr"

0 commit comments

Comments
 (0)