Skip to content
This repository was archived by the owner on Nov 24, 2022. It is now read-only.

Commit ac86348

Browse files
author
Andrea Condoluci
committed
Scavenge & dirty closures
1 parent e2571ed commit ac86348

File tree

6 files changed

+118
-13
lines changed

6 files changed

+118
-13
lines changed

asterius/rts/rts.constants.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export const offset_StgLargeBitmap_bitmap = 0x8;
4747
export const sizeof_StgMutArrPtrs = 0x18;
4848
export const offset_StgMutArrPtrs_ptrs = 0x8;
4949
export const offset_StgMutArrPtrs_payload = 0x18;
50+
export const offset_StgMutVar_var = 0x8;
5051
export const offset_StgMVar_head = 0x8;
5152
export const offset_StgMVar_tail = 0x10;
5253
export const offset_StgMVar_value = 0x18;
@@ -75,6 +76,7 @@ export const offset_StgThunk_payload = 0x10;
7576
export const offset_StgThunkInfoTable_i = 0x0;
7677
export const offset_StgThunkInfoTable_srt = 0x18;
7778
export const offset_StgTSO_id = 0x30;
79+
export const offset_StgTSO_dirty = 0x38;
7880
export const offset_StgTSO_stackobj = 0x18;
7981
export const offset_StgTSO_what_next = 0x20;
8082
export const offset_StgTSO_why_blocked = 0x22;
@@ -83,6 +85,7 @@ export const offset_StgTSO_ffi_func = 0x480;
8385
export const offset_StgTSO_ffi_return = 0x478;
8486
export const offset_StgTSO_saved_regs = 0x78;
8587
export const offset_StgStack_stack_size = 0x8;
88+
export const offset_StgStack_dirty = 0xc;
8689
export const offset_StgStack_sp = 0x10;
8790
export const offset_StgStack_stack = 0x18;
8891
export const offset_StgUpdateFrame_updatee = 0x8;
@@ -101,3 +104,4 @@ export const offset_stat_dev = 0x0;
101104
export const offset_stat_ino = 0x8;
102105
export const clock_monotonic = 0x1;
103106
export const clock_realtime = 0x0;
107+
export const MUT_ARR_PTRS_CARD_BITS = 0x7;

asterius/rts/rts.gc.mjs

Lines changed: 95 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export class GC {
107107
}
108108

109109
/**
110-
* Heap alloactes a physical copy of the given closure.
110+
* Heap allocates a physical copy of the given closure.
111111
* Used during evacuation by {@link GC#evacuateClosure}.
112112
* @param c The source address of the closure
113113
* @param bytes The size in bytes of the closure
@@ -252,7 +252,7 @@ export class GC {
252252
// a forwarding address: just follow it
253253
return Memory.setDynTag(info, tag);
254254
} else if (this.nonMovedObjects.has(untagged_c)) {
255-
// The closure is eiter pinned or static, and has
255+
// The closure is either pinned or static, and has
256256
// already been enqueued for scavenging: just return it
257257
return c;
258258
} else if (!this.memory.heapAlloced(untagged_c)) {
@@ -467,6 +467,33 @@ export class GC {
467467
for (let i = 0; i < ptrs; ++i) this.scavengeClosureAt(payload + (i << 3));
468468
}
469469

470+
471+
/**
472+
* Scavenge the pointers of a MUT_ARR, but only
473+
* the areas that are marked as dirty in the
474+
* card table.
475+
* @param p The address of the array payload
476+
* @param ptrs The number of pointers in the array
477+
*/
478+
scavengeMutArrPtrsMarked(p, ptrs) {
479+
// `cards` is the pointer to the card table
480+
const cards = p + (ptrs << 3);
481+
const c = 1 << rtsConstants.MUT_ARR_PTRS_CARD_BITS;
482+
// The length (in bytes) of the card table
483+
const mutArrPtrsCards = ((ptrs + c - 1) >> rtsConstants.MUT_ARR_PTRS_CARD_BITS);
484+
for (let m = 0; m < mutArrPtrsCards; m++) {
485+
if (this.memory.i8Load(cards + m) != 0) {
486+
this.memory.i8Store(cards + m, 0); // clean up
487+
const q = Math.min(p + c, cards);
488+
for (; p < q; p += 8) {
489+
this.scavengeClosureAt(p);
490+
}
491+
} else {
492+
p += c;
493+
}
494+
}
495+
}
496+
470497
scavengeSmallBitmap(payload, bitmap, size) {
471498
for (let i = 0; i < size; ++i)
472499
if (!(Number(bitmap >> BigInt(i)) & 1))
@@ -736,9 +763,16 @@ export class GC {
736763
this.scavengePointersFirst(c + 8, ptrs);
737764
break;
738765
}
766+
case ClosureTypes.MUT_VAR_CLEAN: {
767+
this.scavengeClosureAt(c + rtsConstants.offset_StgMutVar_var);
768+
break;
769+
}
770+
case ClosureTypes.MUT_VAR_DIRTY: {
771+
this.memory.i64Store(c, this.symbolTable["stg_MUT_VAR_CLEAN_info"]);
772+
this.scavengeClosureAt(c + rtsConstants.offset_StgMutVar_var);
773+
break;
774+
}
739775
case ClosureTypes.BLACKHOLE:
740-
case ClosureTypes.MUT_VAR_CLEAN:
741-
case ClosureTypes.MUT_VAR_DIRTY:
742776
case ClosureTypes.PRIM:
743777
case ClosureTypes.MUT_PRIM:
744778
case ClosureTypes.COMPACT_NFDATA: {
@@ -814,8 +848,14 @@ export class GC {
814848
this.scavengeClosureAt(c + rtsConstants.offset_StgIndStatic_indirectee);
815849
break;
816850
}
817-
case ClosureTypes.MVAR_CLEAN:
851+
case ClosureTypes.MVAR_CLEAN: {
852+
this.scavengeClosureAt(c + rtsConstants.offset_StgMVar_head);
853+
this.scavengeClosureAt(c + rtsConstants.offset_StgMVar_tail);
854+
this.scavengeClosureAt(c + rtsConstants.offset_StgMVar_value);
855+
break;
856+
}
818857
case ClosureTypes.MVAR_DIRTY: {
858+
this.memory.i64Store(c, this.symbolTable["stg_MVAR_CLEAN_info"]);
819859
this.scavengeClosureAt(c + rtsConstants.offset_StgMVar_head);
820860
this.scavengeClosureAt(c + rtsConstants.offset_StgMVar_tail);
821861
this.scavengeClosureAt(c + rtsConstants.offset_StgMVar_value);
@@ -825,8 +865,6 @@ export class GC {
825865
break;
826866
}
827867
case ClosureTypes.MUT_ARR_PTRS_CLEAN:
828-
case ClosureTypes.MUT_ARR_PTRS_DIRTY:
829-
case ClosureTypes.MUT_ARR_PTRS_FROZEN_DIRTY:
830868
case ClosureTypes.MUT_ARR_PTRS_FROZEN_CLEAN: {
831869
const ptrs = Number(
832870
this.memory.i64Load(c + rtsConstants.offset_StgMutArrPtrs_ptrs)
@@ -837,6 +875,28 @@ export class GC {
837875
);
838876
break;
839877
}
878+
case ClosureTypes.MUT_ARR_PTRS_DIRTY: {
879+
this.memory.i64Store(c, this.symbolTable["stg_MUT_ARR_PTRS_CLEAN_info"]);
880+
const ptrs = Number(
881+
this.memory.i64Load(c + rtsConstants.offset_StgMutArrPtrs_ptrs)
882+
);
883+
this.scavengeMutArrPtrsMarked(
884+
c + rtsConstants.offset_StgMutArrPtrs_payload,
885+
ptrs
886+
);
887+
break;
888+
}
889+
case ClosureTypes.MUT_ARR_PTRS_FROZEN_DIRTY: {
890+
this.memory.i64Store(c, this.symbolTable["stg_MUT_ARR_PTRS_FROZEN_CLEAN_info"]);
891+
const ptrs = Number(
892+
this.memory.i64Load(c + rtsConstants.offset_StgMutArrPtrs_ptrs)
893+
);
894+
this.scavengeMutArrPtrsMarked(
895+
c + rtsConstants.offset_StgMutArrPtrs_payload,
896+
ptrs
897+
);
898+
break;
899+
}
840900
case ClosureTypes.WEAK: {
841901
this.scavengeClosureAt(c + rtsConstants.offset_StgWeak_cfinalizers);
842902
this.scavengeClosureAt(c + rtsConstants.offset_StgWeak_key);
@@ -845,10 +905,12 @@ export class GC {
845905
break;
846906
}
847907
case ClosureTypes.TSO: {
908+
this.memory.i32Store(c + rtsConstants.offset_StgTSO_dirty, 0);
848909
this.scavengeClosureAt(c + rtsConstants.offset_StgTSO_stackobj);
849910
break;
850911
}
851912
case ClosureTypes.STACK: {
913+
this.memory.i32Store(c + rtsConstants.offset_StgStack_dirty, 0);
852914
const stack_size = this.memory.i32Load(
853915
c + rtsConstants.offset_StgStack_stack_size
854916
),
@@ -858,14 +920,35 @@ export class GC {
858920
break;
859921
}
860922
case ClosureTypes.SMALL_MUT_ARR_PTRS_CLEAN:
861-
case ClosureTypes.SMALL_MUT_ARR_PTRS_DIRTY:
862-
case ClosureTypes.SMALL_MUT_ARR_PTRS_FROZEN_DIRTY:
863923
case ClosureTypes.SMALL_MUT_ARR_PTRS_FROZEN_CLEAN: {
924+
const ptrs = Number(
925+
this.memory.i64Load(c + rtsConstants.offset_StgSmallMutArrPtrs_ptrs)
926+
);
864927
this.scavengePointersFirst(
865928
c + rtsConstants.offset_StgSmallMutArrPtrs_payload,
866-
Number(
867-
this.memory.i64Load(c + rtsConstants.offset_StgSmallMutArrPtrs_ptrs)
868-
)
929+
ptrs
930+
);
931+
break;
932+
}
933+
case ClosureTypes.SMALL_MUT_ARR_PTRS_DIRTY: {
934+
this.memory.i64Store(c, this.symbolTable["stg_SMALL_MUT_ARR_PTRS_CLEAN_info"]);
935+
const ptrs = Number(
936+
this.memory.i64Load(c + rtsConstants.offset_StgSmallMutArrPtrs_ptrs)
937+
);
938+
this.scavengePointersFirst(
939+
c + rtsConstants.offset_StgSmallMutArrPtrs_payload,
940+
ptrs
941+
);
942+
break;
943+
}
944+
case ClosureTypes.SMALL_MUT_ARR_PTRS_FROZEN_DIRTY: {
945+
this.memory.i64Store(c, this.symbolTable["stg_SMALL_MUT_ARR_PTRS_FROZEN_CLEAN_info"]);
946+
const ptrs = Number(
947+
this.memory.i64Load(c + rtsConstants.offset_StgSmallMutArrPtrs_ptrs)
948+
);
949+
this.scavengePointersFirst(
950+
c + rtsConstants.offset_StgSmallMutArrPtrs_payload,
951+
ptrs
869952
);
870953
break;
871954
}

asterius/src/Asterius/JSGen/Constants.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ rtsConstants =
8888
("sizeof_StgMutArrPtrs", sizeof_StgMutArrPtrs),
8989
("offset_StgMutArrPtrs_ptrs", offset_StgMutArrPtrs_ptrs),
9090
("offset_StgMutArrPtrs_payload", offset_StgMutArrPtrs_payload),
91+
("offset_StgMutVar_var", offset_StgMutVar_var),
9192
("offset_StgMVar_head", offset_StgMVar_head),
9293
("offset_StgMVar_tail", offset_StgMVar_tail),
9394
("offset_StgMVar_value", offset_StgMVar_value),
@@ -120,6 +121,7 @@ rtsConstants =
120121
("offset_StgThunkInfoTable_i", offset_StgThunkInfoTable_i),
121122
("offset_StgThunkInfoTable_srt", offset_StgThunkInfoTable_srt),
122123
("offset_StgTSO_id", offset_StgTSO_id),
124+
("offset_StgTSO_dirty", offset_StgTSO_dirty),
123125
("offset_StgTSO_stackobj", offset_StgTSO_stackobj),
124126
("offset_StgTSO_what_next", offset_StgTSO_what_next),
125127
("offset_StgTSO_why_blocked", offset_StgTSO_why_blocked),
@@ -128,6 +130,7 @@ rtsConstants =
128130
("offset_StgTSO_ffi_return", offset_StgTSO_ffi_return),
129131
("offset_StgTSO_saved_regs", offset_StgTSO_saved_regs),
130132
("offset_StgStack_stack_size", offset_StgStack_stack_size),
133+
("offset_StgStack_dirty", offset_StgStack_dirty),
131134
("offset_StgStack_sp", offset_StgStack_sp),
132135
("offset_StgStack_stack", offset_StgStack_stack),
133136
("offset_StgUpdateFrame_updatee", offset_StgUpdateFrame_updatee),
@@ -145,6 +148,7 @@ rtsConstants =
145148
("offset_stat_dev", offset_stat_dev),
146149
("offset_stat_ino", offset_stat_ino),
147150
("clock_monotonic", clock_monotonic),
148-
("clock_realtime", clock_realtime)
151+
("clock_realtime", clock_realtime),
152+
("MUT_ARR_PTRS_CARD_BITS", _MUT_ARR_PTRS_CARD_BITS)
149153
]
150154
]

asterius/src/Asterius/Ld.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ rtsUsedSymbols =
7070
"stg_BLACKHOLE_info",
7171
"stg_WHITEHOLE_info",
7272
"stg_IND_info",
73+
"stg_MVAR_CLEAN_info",
74+
"stg_MUT_ARR_CLEAN_info",
75+
"stg_MUT_ARR_PTRS_FROZEN_CLEAN_info",
76+
"stg_MUT_VAR_CLEAN_info",
77+
"stg_SMALL_MUT_ARR_PTRS_CLEAN_info",
78+
"stg_SMALL_MUT_ARR_PTRS_FROZEN_CLEAN_info",
7379
"stg_DEAD_WEAK_info",
7480
"stg_marked_upd_frame_info",
7581
"stg_NO_FINALIZER_closure",

ghc-toolkit/cbits/ghc_constants.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ HsInt offset_StgMutArrPtrs_payload() {
218218
return offsetof(StgMutArrPtrs, payload);
219219
}
220220

221+
HsInt offset_StgMutVar_var() { return offsetof(StgMutVar, var); }
222+
221223
HsInt offset_StgMVar_head() { return offsetof(StgMVar, head); }
222224

223225
HsInt offset_StgMVar_tail() { return offsetof(StgMVar, tail); }
@@ -527,3 +529,5 @@ HsInt offset_StgStableName_sn() { return offsetof(StgStableName, sn); }
527529
HsInt clock_monotonic() { return CLOCK_MONOTONIC; }
528530

529531
HsInt clock_realtime() { return CLOCK_REALTIME; }
532+
533+
HsInt _MUT_ARR_PTRS_CARD_BITS() { return MUT_ARR_PTRS_CARD_BITS; }

ghc-toolkit/src/Language/Haskell/GHC/Toolkit/Constants.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ foreign import ccall unsafe "offset_StgMutArrPtrs_size"
223223
foreign import ccall unsafe "offset_StgMutArrPtrs_payload"
224224
offset_StgMutArrPtrs_payload :: Int
225225

226+
foreign import ccall unsafe "offset_StgMutVar_var" offset_StgMutVar_var :: Int
227+
226228
foreign import ccall unsafe "offset_StgMVar_head" offset_StgMVar_head :: Int
227229

228230
foreign import ccall unsafe "offset_StgMVar_tail" offset_StgMVar_tail :: Int
@@ -587,3 +589,5 @@ foreign import ccall unsafe "offset_StgStableName_sn"
587589
foreign import ccall unsafe "clock_monotonic" clock_monotonic :: Int
588590

589591
foreign import ccall unsafe "clock_realtime" clock_realtime :: Int
592+
593+
foreign import ccall unsafe "_MUT_ARR_PTRS_CARD_BITS" _MUT_ARR_PTRS_CARD_BITS :: Int

0 commit comments

Comments
 (0)