@@ -1820,7 +1820,7 @@ JL_USED_FUNC static void dumpColorAssignments(const State &S, const ArrayRef<int
18201820 }
18211821}
18221822
1823- SmallVector<int , 0 > LateLowerGCFrame::ColorRoots (const State &S) {
1823+ std::pair< SmallVector<int , 0 >, int > LateLowerGCFrame::ColorRoots (const State &S) {
18241824 SmallVector<int , 0 > Colors;
18251825 Colors.resize (S.MaxPtrNumber + 1 , -1 );
18261826 PEOIterator Ordering (S.Neighbors );
@@ -1862,7 +1862,7 @@ SmallVector<int, 0> LateLowerGCFrame::ColorRoots(const State &S) {
18621862 NewColor += PreAssignedColors;
18631863 Colors[ActiveElement] = NewColor;
18641864 }
1865- return Colors;
1865+ return { Colors, PreAssignedColors} ;
18661866}
18671867
18681868// Size of T is assumed to be `sizeof(void*)`
@@ -2292,8 +2292,21 @@ void LateLowerGCFrame::PlaceGCFrameStore(State &S, unsigned R, unsigned MinColor
22922292 new StoreInst (Val, slotAddress, InsertBefore);
22932293}
22942294
2295+ void LateLowerGCFrame::PlaceGCFrameReset (State &S, unsigned R, unsigned MinColorRoot,
2296+ ArrayRef<int > Colors, Value *GCFrame,
2297+ Instruction *InsertBefore) {
2298+ // Get the slot address.
2299+ auto slotAddress = CallInst::Create (
2300+ getOrDeclare (jl_intrinsics::getGCFrameSlot),
2301+ {GCFrame, ConstantInt::get (Type::getInt32Ty (InsertBefore->getContext ()), Colors[R] + MinColorRoot)},
2302+ " gc_slot_addr_" + StringRef (std::to_string (Colors[R] + MinColorRoot)), InsertBefore);
2303+ // Reset the slot to NULL.
2304+ Value *Val = ConstantPointerNull::get (T_prjlvalue);
2305+ new StoreInst (Val, slotAddress, InsertBefore);
2306+ }
2307+
22952308void LateLowerGCFrame::PlaceGCFrameStores (State &S, unsigned MinColorRoot,
2296- ArrayRef<int > Colors, Value *GCFrame)
2309+ ArrayRef<int > Colors, int PreAssignedColors, Value *GCFrame)
22972310{
22982311 for (auto &BB : *S.F ) {
22992312 const BBState &BBS = S.BBStates [&BB];
@@ -2306,6 +2319,15 @@ void LateLowerGCFrame::PlaceGCFrameStores(State &S, unsigned MinColorRoot,
23062319 for (auto rit = BBS.Safepoints .rbegin ();
23072320 rit != BBS.Safepoints .rend (); ++rit ) {
23082321 const LargeSparseBitVector &NowLive = S.LiveSets [*rit];
2322+ // reset slots which are no longer alive
2323+ for (int Idx : *LastLive) {
2324+ if (Idx >= PreAssignedColors && !HasBitSet (NowLive, Idx)) {
2325+ PlaceGCFrameReset (S, Idx, MinColorRoot, Colors, GCFrame,
2326+ S.ReverseSafepointNumbering [*rit]);
2327+ }
2328+ }
2329+ // store values which are alive in this safepoint but
2330+ // haven't been stored in the GC frame before
23092331 for (int Idx : NowLive) {
23102332 if (!HasBitSet (*LastLive, Idx)) {
23112333 PlaceGCFrameStore (S, Idx, MinColorRoot, Colors, GCFrame,
@@ -2317,7 +2339,7 @@ void LateLowerGCFrame::PlaceGCFrameStores(State &S, unsigned MinColorRoot,
23172339 }
23182340}
23192341
2320- void LateLowerGCFrame::PlaceRootsAndUpdateCalls (SmallVectorImpl <int > & Colors, State &S,
2342+ void LateLowerGCFrame::PlaceRootsAndUpdateCalls (ArrayRef <int > Colors, int PreAssignedColors , State &S,
23212343 std::map<Value *, std::pair<int , int >>) {
23222344 auto F = S.F ;
23232345 auto T_int32 = Type::getInt32Ty (F->getContext ());
@@ -2439,7 +2461,7 @@ void LateLowerGCFrame::PlaceRootsAndUpdateCalls(SmallVectorImpl<int> &Colors, St
24392461 pushGcframe->setArgOperand (1 , NRoots);
24402462
24412463 // Insert GC frame stores
2442- PlaceGCFrameStores (S, AllocaSlot - 2 , Colors, gcframe);
2464+ PlaceGCFrameStores (S, AllocaSlot - 2 , Colors, PreAssignedColors, gcframe);
24432465 // Insert GCFrame pops
24442466 for (auto &BB : *F) {
24452467 if (isa<ReturnInst>(BB.getTerminator ())) {
@@ -2464,9 +2486,9 @@ bool LateLowerGCFrame::runOnFunction(Function &F, bool *CFGModified) {
24642486
24652487 State S = LocalScan (F);
24662488 ComputeLiveness (S);
2467- SmallVector< int , 0 > Colors = ColorRoots (S);
2489+ auto Colors = ColorRoots (S);
24682490 std::map<Value *, std::pair<int , int >> CallFrames; // = OptimizeCallFrames(S, Ordering);
2469- PlaceRootsAndUpdateCalls (Colors, S, CallFrames);
2491+ PlaceRootsAndUpdateCalls (Colors. first , Colors. second , S, CallFrames);
24702492 CleanupIR (F, &S, CFGModified);
24712493 return true ;
24722494}
0 commit comments