Skip to content

Commit

Permalink
loom: compile-time pointer compression with migration
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-levan committed Sep 15, 2023
1 parent 78b9e7a commit 4cc9d1e
Show file tree
Hide file tree
Showing 37 changed files with 1,436 additions and 337 deletions.
5 changes: 2 additions & 3 deletions pkg/noun/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ vere_library(
name = "noun",
srcs = glob(
[
"*.c",
"*.h",
"jets/tree.c",
"**/*.c",
"**/*.h",
"jets/*.h",
"jets/**/*.c",
],
Expand Down
55 changes: 16 additions & 39 deletions pkg/noun/allocate.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,6 @@ u3_road* u3a_Road;
c3_w u3_Code;
#endif

// declarations of inline functions
//

void u3a_config_loom(c3_w ver_w);
void *u3a_into(c3_w x);
c3_w u3a_outa(void *p);
c3_w u3a_to_off(c3_w som);
void *u3a_to_ptr(c3_w som);
c3_w *u3a_to_wtr(c3_w som);
c3_w u3a_to_pug(c3_w off);
c3_w u3a_to_pom(c3_w off);

void
u3a_drop(const u3a_pile* pil_u);
void*
Expand Down Expand Up @@ -67,12 +55,12 @@ _box_count(c3_ws siz_ws) { }
_box_count, (others?) should have perhaps its own header and certainly its
own prefix. having to remind yourself that _box_count doesn't actually do
anything unless U3_CPU_DEBUG is defined is annoying. */
#define _box_vaal(box_u) \
do { \
c3_dessert(((uintptr_t)u3a_boxto(box_u) \
& u3C.balign_d-1) == 0); \
c3_dessert((((u3a_box*)(box_u))->siz_w \
& u3C.walign_w-1) == 0); \
#define _box_vaal(box_u) \
do { \
c3_dessert(((uintptr_t)u3a_boxto(box_u) \
& u3a_balign-1) == 0); \
c3_dessert((((u3a_box*)(box_u))->siz_w \
& u3a_walign-1) == 0); \
} while(0)

/* _box_slot(): select the right free list to search for a block.
Expand Down Expand Up @@ -302,7 +290,7 @@ _ca_box_make_hat(c3_w len_w, c3_w ald_w, c3_w off_w, c3_w use_w)
all_p += c3_wiseof(u3a_box) + off_w;
pad_w = c3_align(all_p, ald_w, C3_ALGHI)
- all_p;
siz_w = c3_align(len_w + pad_w, u3C.walign_w, C3_ALGHI);
siz_w = c3_align(len_w + pad_w, u3a_walign, C3_ALGHI);

// hand-inlined: siz_w >= u3a_open(u3R)
//
Expand All @@ -316,7 +304,7 @@ _ca_box_make_hat(c3_w len_w, c3_w ald_w, c3_w off_w, c3_w use_w)
all_p += c3_wiseof(u3a_box) + off_w;
pad_w = all_p
- c3_align(all_p, ald_w, C3_ALGLO);
siz_w = c3_align(len_w + pad_w, u3C.walign_w, C3_ALGHI);
siz_w = c3_align(len_w + pad_w, u3a_walign, C3_ALGHI);

// hand-inlined: siz_w >= u3a_open(u3R)
//
Expand Down Expand Up @@ -508,7 +496,7 @@ _ca_willoc(c3_w len_w, c3_w ald_w, c3_w off_w)
box_p = all_p = *pfr_p;
all_p += c3_wiseof(u3a_box) + off_w;
c3_w pad_w = c3_align(all_p, ald_w, C3_ALGHI) - all_p;
c3_w des_w = c3_align(siz_w + pad_w, u3C.walign_w, C3_ALGHI);
c3_w des_w = c3_align(siz_w + pad_w, u3a_walign, C3_ALGHI);

/* calls maximally requesting DWORD alignment of returned pointer
shouldn't require padding. */
Expand Down Expand Up @@ -711,17 +699,17 @@ u3a_wtrim(void* tox_v, c3_w old_w, c3_w len_w)
c3_w* box_w = (void*)u3a_botox(nov_w);

c3_w* end_w = c3_align(nov_w + len_w + 1, /* +1 for trailing allocation size */
u3C.balign_d,
u3a_balign,
C3_ALGHI);

c3_w asz_w = (end_w - box_w); /* total size in words of new allocation */
if (box_u->siz_w <= asz_w) return;
c3_w bsz_w = box_u->siz_w - asz_w; /* size diff in words between old and new */

c3_dessert(asz_w && ((asz_w & u3C.walign_w-1) == 0)); /* new allocation size must be non-zero and DWORD multiple */
c3_dessert(asz_w && ((asz_w & u3a_walign-1) == 0)); /* new allocation size must be non-zero and DWORD multiple */
c3_dessert(end_w < (box_w + box_u->siz_w)); /* desired alloc end must not exceed existing boundaries */
c3_dessert(((uintptr_t)end_w & u3C.balign_d-1) == 0); /* address of box getting freed must be DWORD aligned */
c3_dessert((bsz_w & u3C.walign_w-1) == 0); /* size of box getting freed must be DWORD multiple */
c3_dessert(((uintptr_t)end_w & u3a_balign-1) == 0); /* address of box getting freed must be DWORD aligned */
c3_dessert((bsz_w & u3a_walign-1) == 0); /* size of box getting freed must be DWORD multiple */

_box_attach(_box_make(end_w, bsz_w, 0)); /* free the unneeded space */

Expand Down Expand Up @@ -1747,24 +1735,12 @@ u3a_rewritten_noun(u3_noun som)
return som;
}
u3_post som_p = u3a_rewritten(u3a_to_off(som));

/* If this is being called during a migration, one-bit pointer compression
needs to be temporarily enabled so the rewritten reference is compressed */
if (u3C.migration_state == MIG_REWRITE_COMPRESSED)
u3C.vits_w = 1;

if ( c3y == u3a_is_pug(som) ) {
som_p = u3a_to_pug(som_p);
return u3a_to_pug(som_p);
}
else {
som_p = u3a_to_pom(som_p);
return u3a_to_pom(som_p);
}

/* likewise, pointer compression is disabled until migration is complete */
if (u3C.migration_state == MIG_REWRITE_COMPRESSED)
u3C.vits_w = 0;

return som_p;
}

/* u3a_mark_mptr(): mark a malloc-allocated ptr for gc.
Expand Down Expand Up @@ -2170,6 +2146,7 @@ u3a_reclaim(void)
/* u3a_rewrite_compact(): rewrite pointers in ad-hoc persistent road structures.
*/
void
// u3a_v1_rewrite_compact(u3a_v1_road *rod_u)
u3a_rewrite_compact(void)
{
u3a_rewrite_noun(u3R->ski.gul);
Expand Down
72 changes: 22 additions & 50 deletions pkg/noun/allocate.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,40 @@

#include "error.h"
#include "manage.h"
#include "options.h"

/** Constants.
**/
/* u3a_bits: number of bits in word-addressed pointer. 29 == 2GB.
*/
# define u3a_bits U3_OS_LoomBits /* 30 */

/* u3a_vits_max: number of virtual bits in a reference gained via pointer
compression
/* u3a_vits: number of virtual bits in a noun reference gained via shifting
*/
# define u3a_vits_max 1
# define u3a_vits 1

/* u3a_walign: references into the loom are guaranteed to be word-aligned to:
*/
# define u3a_walign (1 << u3a_vits)

/* u3a_balign: u3a_walign in bytes
*/
# define u3a_balign (sizeof(c3_w)*u3a_walign)

/* u3a_bits_max: max loom bex
*/
# define u3a_bits_max (8 * sizeof(c3_w) + u3a_vits_max)
# define u3a_bits_max (8 * sizeof(c3_w) + u3a_vits)

/* u3a_page: number of bits in word-addressed page. 12 == 16K page
*/
# define u3a_page 12ULL

/* u3a_pages: maximum number of pages in memory.
*/
# define u3a_pages (1ULL << (u3a_bits + u3a_vits_max - u3a_page) )
# define u3a_pages (1ULL << (u3a_bits + u3a_vits - u3a_page) )

/* u3a_words: maximum number of words in memory.
*/
# define u3a_words ( 1ULL << (u3a_bits + u3a_vits_max ))
# define u3a_words ( 1ULL << (u3a_bits + u3a_vits))

/* u3a_bytes: maximum number of bytes in memory.
*/
Expand Down Expand Up @@ -355,7 +361,7 @@
# define _rod_vaal(rod_u) \
do { \
c3_dessert(((uintptr_t)((u3a_road*)(rod_u))->hat_p \
& u3C.walign_w-1) == 0); \
& u3a_walign-1) == 0); \
} while(0)


Expand All @@ -374,69 +380,35 @@

# define u3_Loom ((c3_w *)(void *)U3_OS_LoomBase)

/** inline functions.
/** Inline functions.
**/
/* u3a_config_loom(): configure loom information by u3v version
*/
inline void u3a_config_loom(c3_w ver_w) {
switch (ver_w) {
case U3V_VER1:
u3C.vits_w = 0;
break;
case U3V_VER2:
u3C.vits_w = 1;
break;
default:
u3_assert(0);
}

u3C.walign_w = 1 << u3C.vits_w;
u3C.balign_d = sizeof(c3_w) * u3C.walign_w;
}

/* u3a_into(): convert loom offset [x] into generic pointer.
*/
inline void *u3a_into(c3_w x) {
return u3_Loom + x;
}
# define u3a_into(x) ((void *)(u3_Loom + (x)))

/* u3a_outa(): convert pointer [p] into word offset into loom.
*/
inline c3_w u3a_outa(void *p) {
return ((c3_w *)p) - u3_Loom;
}
# define u3a_outa(p) ((c3_w *)(void *)(p) - u3_Loom)

/* u3a_to_off(): mask off bits 30 and 31 from noun [som].
*/
inline c3_w u3a_to_off(c3_w som) {
return (som & 0x3fffffff) << u3C.vits_w;
}
# define u3a_to_off(som) (((som) & 0x3fffffff) << u3a_vits)

/* u3a_to_ptr(): convert noun [som] into generic pointer into loom.
*/
inline void *u3a_to_ptr(c3_w som) {
return u3a_into(u3a_to_off(som));
}
# define u3a_to_ptr(som) (u3a_into(u3a_to_off(som)))

/* u3a_to_wtr(): convert noun [som] into word pointer into loom.
*/
inline c3_w *u3a_to_wtr(c3_w som) {
return (c3_w *)u3a_to_ptr(som);
}
# define u3a_to_wtr(som) ((c3_w *)u3a_to_ptr(som))

/* u3a_to_pug(): set bit 31 of [off].
*/
inline c3_w u3a_to_pug(c3_w off) {
c3_dessert((off & u3C.walign_w-1) == 0);
return (off >> u3C.vits_w) | 0x80000000;
}
# define u3a_to_pug(off) ((off >> u3a_vits) | 0x80000000)

/* u3a_to_pom(): set bits 30 and 31 of [off].
*/
inline c3_w u3a_to_pom(c3_w off) {
c3_dessert((off & u3C.walign_w-1) == 0);
return (off >> u3C.vits_w) | 0xc0000000;
}
# define u3a_to_pom(off) ((off >> u3a_vits) | 0xc0000000)

/** road stack.
**/
Expand Down
14 changes: 0 additions & 14 deletions pkg/noun/hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -1017,15 +1017,8 @@ _ch_rewrite_node(u3h_node* han_u, c3_w lef_w)
else {
void* hav_v = u3h_slot_to_node(sot_w);
u3h_node* nod_u = u3to(u3h_node,u3a_rewritten(u3of(u3h_node,hav_v)));

if (u3C.migration_state == MIG_REWRITE_COMPRESSED)
u3C.vits_w = 1;

han_u->sot_w[i_w] = u3h_node_to_slot(nod_u);

if (u3C.migration_state == MIG_REWRITE_COMPRESSED)
u3C.vits_w = 0;

if ( 0 == lef_w ) {
_ch_rewrite_buck(hav_v);
} else {
Expand Down Expand Up @@ -1057,15 +1050,8 @@ u3h_rewrite(u3p(u3h_root) har_p)
else if ( _(u3h_slot_is_node(sot_w)) ) {
u3h_node* han_u = u3h_slot_to_node(sot_w);
u3h_node* nod_u = u3to(u3h_node,u3a_rewritten(u3of(u3h_node,han_u)));

if (u3C.migration_state == MIG_REWRITE_COMPRESSED)
u3C.vits_w = 1;

har_u->sot_w[i_w] = u3h_node_to_slot(nod_u);

if (u3C.migration_state == MIG_REWRITE_COMPRESSED)
u3C.vits_w = 0;

_ch_rewrite_node(han_u, 25);
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/noun/hashtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@
# define u3h_slot_is_node(sot) ((1 == ((sot) >> 30)) ? c3y : c3n)
# define u3h_slot_is_noun(sot) ((1 == ((sot) >> 31)) ? c3y : c3n)
# define u3h_slot_is_warm(sot) (((sot) & 0x40000000) ? c3y : c3n)
# define u3h_slot_to_node(sot) (u3a_into(((sot) & 0x3fffffff) << u3C.vits_w))
# define u3h_node_to_slot(ptr) ((u3a_outa((ptr)) >> u3C.vits_w) | 0x40000000)
# define u3h_slot_to_node(sot) (u3a_into(((sot) & 0x3fffffff) << u3a_vits))
# define u3h_node_to_slot(ptr) ((u3a_outa((ptr)) >> u3a_vits) | 0x40000000)
# define u3h_noun_be_warm(sot) ((sot) | 0x40000000)
# define u3h_noun_be_cold(sot) ((sot) & ~0x40000000)
# define u3h_slot_to_noun(sot) (0x40000000 | (sot))
Expand Down
Loading

0 comments on commit 4cc9d1e

Please sign in to comment.