1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
|
/* -----------------------------------------------------------------------------
*
* (c) The GHC Team, 2015-2016
*
* Support for compact regions. See Note [Compact Normal Forms] in
* rts/sm/CNF.c
*
* ---------------------------------------------------------------------------*/
#include "Cmm.h"
#include "sm/ShouldCompact.h"
import CLOSURE ghczminternal_GHCziInternalziIOziException_cannotCompactFunction_closure;
import CLOSURE ghczminternal_GHCziInternalziIOziException_cannotCompactMutable_closure;
import CLOSURE ghczminternal_GHCziInternalziIOziException_cannotCompactPinned_closure;
#if !defined(UnregisterisedCompiler)
import CLOSURE g0;
import CLOSURE large_alloc_lim;
#endif
//
// Allocate space for a new object in the compact region. We first try
// the fast method using the hp/hpLim fields of StgCompactNFData, and
// if that fails we fall back to calling allocateForCompact() which
// will append a new block if necessary.
//
#define ALLOCATE(compact,sizeW,p,to, tag) \
hp = StgCompactNFData_hp(compact); \
if (hp + WDS(sizeW) <= StgCompactNFData_hpLim(compact)) { \
to = hp; \
StgCompactNFData_hp(compact) = hp + WDS(sizeW); \
} else { \
("ptr" to) = ccall allocateForCompact( \
MyCapability() "ptr", compact "ptr", sizeW); \
} \
if (StgCompactNFData_hash(compact) != NULL) { \
ccall insertCompactHash(MyCapability(), compact, p, tag | to); \
}
//
// Look up a pointer in the hash table if we're doing sharing.
//
#define CHECK_HASH() \
hash = StgCompactNFData_hash(compact); \
if (hash != NULL) { \
("ptr" hashed) = ccall lookupHashTable(hash "ptr", p "ptr"); \
if (hashed != NULL) { \
P_[pp] = hashed; \
return (); \
} \
}
//
// Evacuate and copy an object and its transitive closure into a
// compact. This function is called recursively as we traverse the
// data structure. It takes the location to store the address of the
// compacted object as an argument, so that it can be tail-recursive.
//
// N.B. No memory barrier (see Note [Heap memory barriers] in SMP.h) is needed
// here since this is essentially an allocation of a new object which won't
// be visible to other cores until after we return.
stg_compactAddWorkerzh (
P_ compact, // The Compact# object
P_ p, // The object to compact
W_ pp) // Where to store a pointer to the compacted object
{
W_ type, info, should, hash, hp, tag;
P_ p;
P_ hashed;
again: MAYBE_GC(again);
STK_CHK_GEN();
eval:
tag = GETTAG(p);
p = UNTAG(p);
info = %INFO_PTR(p);
type = TO_W_(%INFO_TYPE(%STD_INFO(info)));
switch [0 .. N_CLOSURE_TYPES] type {
// Unevaluated things must be evaluated first:
case
THUNK,
THUNK_1_0,
THUNK_0_1,
THUNK_2_0,
THUNK_1_1,
THUNK_0_2,
THUNK_STATIC,
AP,
AP_STACK,
BLACKHOLE,
THUNK_SELECTOR : {
(P_ evald) = call %ENTRY_CODE(info) (p);
p = evald;
goto eval;
}
// Follow indirections:
case IND, IND_STATIC: {
p = %acquire StgInd_indirectee(p);
goto eval;
}
// Mutable things are not allowed:
case
MVAR_CLEAN,
MVAR_DIRTY,
TVAR,
MUT_ARR_PTRS_CLEAN,
MUT_ARR_PTRS_DIRTY,
MUT_ARR_PTRS_CLEAN,
MUT_VAR_CLEAN,
MUT_VAR_DIRTY,
WEAK,
PRIM,
MUT_PRIM,
TSO,
STACK,
TREC_CHUNK,
WHITEHOLE,
SMALL_MUT_ARR_PTRS_CLEAN,
SMALL_MUT_ARR_PTRS_DIRTY,
COMPACT_NFDATA: {
jump stg_raisezh(ghczminternal_GHCziInternalziIOziException_cannotCompactMutable_closure);
}
// We shouldn't see any functions, if this data structure was NFData.
case
FUN,
FUN_1_0,
FUN_0_1,
FUN_2_0,
FUN_1_1,
FUN_0_2,
FUN_STATIC,
BCO,
PAP,
CONTINUATION: {
jump stg_raisezh(ghczminternal_GHCziInternalziIOziException_cannotCompactFunction_closure);
}
case ARR_WORDS: {
(should) = ccall shouldCompact(compact "ptr", p "ptr");
if (should == SHOULDCOMPACT_IN_CNF) { P_[pp] = p; return(); }
if (should == SHOULDCOMPACT_PINNED) {
jump stg_raisezh(ghczminternal_GHCziInternalziIOziException_cannotCompactPinned_closure);
}
CHECK_HASH();
P_ to;
W_ size;
size = SIZEOF_StgArrBytes + StgArrBytes_bytes(p);
ALLOCATE(compact, ROUNDUP_BYTES_TO_WDS(size), p, to, tag);
P_[pp] = to;
prim %memcpy(to, p, size, 1);
return();
}
case
MUT_ARR_PTRS_FROZEN_DIRTY,
MUT_ARR_PTRS_FROZEN_CLEAN: {
(should) = ccall shouldCompact(compact "ptr", p "ptr");
if (should == SHOULDCOMPACT_IN_CNF) { P_[pp] = p; return(); }
CHECK_HASH();
W_ i, size, cards, ptrs;
size = SIZEOF_StgMutArrPtrs + WDS(StgMutArrPtrs_size(p));
ptrs = StgMutArrPtrs_ptrs(p);
cards = SIZEOF_StgMutArrPtrs + WDS(ptrs);
ALLOCATE(compact, BYTES_TO_WDS(size), p, to, tag);
P_[pp] = tag | to;
SET_HDR(to, StgHeader_info(p), StgHeader_ccs(p));
StgMutArrPtrs_ptrs(to) = ptrs;
StgMutArrPtrs_size(to) = StgMutArrPtrs_size(p);
prim %memcpy(to + cards, p + cards , size - cards, 1);
i = 0;
loop0:
if (i < ptrs) ( likely: True ) {
W_ q;
q = to + SIZEOF_StgMutArrPtrs + WDS(i);
call stg_compactAddWorkerzh(
compact, P_[p + SIZEOF_StgMutArrPtrs + WDS(i)], q);
i = i + 1;
goto loop0;
}
return();
}
case
SMALL_MUT_ARR_PTRS_FROZEN_DIRTY,
SMALL_MUT_ARR_PTRS_FROZEN_CLEAN: {
(should) = ccall shouldCompact(compact "ptr", p "ptr");
if (should == SHOULDCOMPACT_IN_CNF) { P_[pp] = p; return(); }
CHECK_HASH();
W_ i, ptrs;
ptrs = StgSmallMutArrPtrs_ptrs(p);
ALLOCATE(compact, BYTES_TO_WDS(SIZEOF_StgSmallMutArrPtrs) + ptrs, p, to, tag);
P_[pp] = tag | to;
SET_HDR(to, StgHeader_info(p), StgHeader_ccs(p));
StgSmallMutArrPtrs_ptrs(to) = ptrs;
i = 0;
loop1:
if (i < ptrs) ( likely: True ) {
W_ q;
q = to + SIZEOF_StgSmallMutArrPtrs + WDS(i);
call stg_compactAddWorkerzh(
compact, P_[p + SIZEOF_StgSmallMutArrPtrs + WDS(i)], q);
i = i + 1;
goto loop1;
}
return();
}
// Everything else we should copy and evaluate the components:
case
CONSTR,
CONSTR_1_0,
CONSTR_2_0,
CONSTR_1_1: {
(should) = ccall shouldCompact(compact "ptr", p "ptr");
if (should == SHOULDCOMPACT_IN_CNF) { P_[pp] = tag | p; return(); }
constructor:
CHECK_HASH();
W_ i, ptrs, nptrs, size;
P_ to;
ptrs = TO_W_(%INFO_PTRS(%STD_INFO(info)));
nptrs = TO_W_(%INFO_NPTRS(%STD_INFO(info)));
size = BYTES_TO_WDS(SIZEOF_StgHeader) + ptrs + nptrs;
ALLOCATE(compact, size, p, to, tag);
P_[pp] = tag | to;
SET_HDR(to, StgHeader_info(p), StgHeader_ccs(p));
// First, copy the non-pointers
if (nptrs > 0) {
i = ptrs;
loop2:
StgClosure_payload(to,i) = StgClosure_payload(p,i);
i = i + 1;
if (i < ptrs + nptrs) ( likely: True ) goto loop2;
}
// Next, recursively compact and copy the pointers
if (ptrs == 0) { return(); }
i = 0;
loop3:
W_ q;
q = to + SIZEOF_StgHeader + OFFSET_StgClosure_payload + WDS(i);
// Tail-call the last one. This means we don't build up a deep
// stack when compacting lists.
if (i == ptrs - 1) {
jump stg_compactAddWorkerzh(compact, StgClosure_payload(p,i), q);
}
call stg_compactAddWorkerzh(compact, StgClosure_payload(p,i), q);
i = i + 1;
goto loop3;
}
// these might be static closures that we can avoid copying into
// the compact if they don't refer to CAFs.
case
CONSTR_0_1,
CONSTR_0_2,
CONSTR_NOCAF: {
(should) = ccall shouldCompact(compact "ptr", p "ptr");
if (should == SHOULDCOMPACT_IN_CNF ||
should == SHOULDCOMPACT_STATIC) { P_[pp] = tag | p; return(); }
goto constructor;
}}
ccall barf("stg_compactWorkerzh", NULL);
}
//
// compactAddWithSharing#
// :: State# RealWorld
// -> Compact#
// -> a
// -> (# State# RealWorld, a #)
//
stg_compactAddWithSharingzh (P_ compact, P_ p)
{
W_ hash;
ASSERT(StgCompactNFData_hash(compact) == NULL);
(hash) = ccall allocHashTable();
StgCompactNFData_hash(compact) = hash;
// Note [compactAddWorker result]
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// compactAddWorker needs somewhere to store the result - this is
// so that it can be tail-recursive. It must be an address that
// doesn't move during GC, so we can't use heap or stack.
// Therefore we have a special field in the StgCompactNFData
// object to hold the final result of compaction.
W_ pp;
pp = compact + SIZEOF_StgHeader + OFFSET_StgCompactNFData_result;
call stg_compactAddWorkerzh(compact, p, pp);
ccall freeHashTable(StgCompactNFData_hash(compact), NULL);
StgCompactNFData_hash(compact) = NULL;
#if defined(DEBUG)
ccall verifyCompact(compact);
#endif
return (P_[pp]);
}
//
// compactAdd#
// :: State# RealWorld
// -> Compact#
// -> a
// -> (# State# RealWorld, a #)
//
stg_compactAddzh (P_ compact, P_ p)
{
ASSERT(StgCompactNFData_hash(compact) == NULL);
W_ pp; // See Note [compactAddWorker result]
pp = compact + SIZEOF_StgHeader + OFFSET_StgCompactNFData_result;
call stg_compactAddWorkerzh(compact, p, pp);
#if defined(DEBUG)
ccall verifyCompact(compact);
#endif
return (P_[pp]);
}
stg_compactSizzezh (P_ compact)
{
return (StgCompactNFData_totalW(compact) * SIZEOF_W);
}
stg_compactNewzh ( W_ size )
{
P_ str;
again: MAYBE_GC(again);
("ptr" str) = ccall compactNew(MyCapability() "ptr", size);
return (str);
}
stg_compactResizzezh ( P_ str, W_ new_size )
{
again: MAYBE_GC(again);
ccall compactResize(MyCapability() "ptr", str "ptr", new_size);
return ();
}
stg_compactContainszh ( P_ str, P_ val )
{
W_ rval;
(rval) = ccall compactContains(str "ptr", val "ptr");
return (rval);
}
stg_compactContainsAnyzh ( P_ val )
{
W_ rval;
(rval) = ccall compactContains(0 "ptr", val "ptr");
return (rval);
}
stg_compactGetFirstBlockzh ( P_ str )
{
/* W_, not P_, because it is not a gc pointer */
W_ block;
W_ bd;
W_ size;
block = str - SIZEOF_StgCompactNFDataBlock::W_;
ASSERT(StgCompactNFDataBlock_owner(block) == str);
// We have to save Hp back to the nursery, otherwise the size will
// be wrong.
bd = Bdescr(StgCompactNFData_nursery(str));
bdescr_free(bd) = StgCompactNFData_hp(str);
bd = Bdescr(str);
size = bdescr_free(bd) - bdescr_start(bd);
ASSERT(size <= TO_W_(bdescr_blocks(bd)) * BLOCK_SIZE);
return (block, size);
}
stg_compactGetNextBlockzh ( P_ str, W_ block )
{
/* str is a pointer to the closure holding the Compact#
it is there primarily to keep everything reachable from
the GC: by having it on the stack of type P_, the GC will
see all the blocks as live (any pointer in the Compact#
keeps it alive), and will not collect the block
We don't run a GC inside this primop, but it could
happen right after, or we could be preempted.
str is also useful for debugging, as it can be casted
to a useful C struct from the gdb command line and all
blocks can be inspected
*/
W_ bd;
W_ next_block;
W_ size;
next_block = StgCompactNFDataBlock_next(block);
if (next_block == 0::W_) {
return (0::W_, 0::W_);
}
ASSERT(StgCompactNFDataBlock_owner(next_block) == str ||
StgCompactNFDataBlock_owner(next_block) == NULL);
bd = Bdescr(next_block);
size = bdescr_free(bd) - bdescr_start(bd);
ASSERT(size <= TO_W_(bdescr_blocks(bd)) * BLOCK_SIZE);
return (next_block, size);
}
stg_compactAllocateBlockzh ( W_ size, W_ previous )
{
W_ actual_block;
again: MAYBE_GC(again);
("ptr" actual_block) = ccall compactAllocateBlock(MyCapability(),
size,
previous "ptr");
return (actual_block);
}
stg_compactFixupPointerszh ( W_ first_block, W_ root )
{
W_ str;
P_ gcstr;
W_ ok;
str = first_block + SIZEOF_StgCompactNFDataBlock::W_;
(ok) = ccall compactFixupPointers (str "ptr", root "ptr");
// Now we can let the GC know about str, because it was linked
// into the generation list and the book-keeping pointers are
// guaranteed to be valid
// (this is true even if the fixup phase failed)
gcstr = str;
return (gcstr, ok);
}
|