Skip to content

Handle unchecked_ref_cast in ObjectOutliner #70243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ private func findEndCOWMutation(of object: Value) -> EndCOWMutationInst? {
if let ecm = findEndCOWMutation(of: uci) {
return ecm
}
case let urci as UncheckedRefCastInst:
if let ecm = findEndCOWMutation(of: urci) {
return ecm
}
case let mv as MoveValueInst:
if let ecm = findEndCOWMutation(of: mv) {
return ecm
Expand Down Expand Up @@ -134,6 +138,10 @@ private func findInitStores(of object: Value,
if !findInitStores(of: uci, &fieldStores, &tailStores) {
return false
}
case let urci as UncheckedRefCastInst:
if !findInitStores(of: urci, &fieldStores, &tailStores) {
return false
}
case let mvi as MoveValueInst:
if !findInitStores(of: mvi, &fieldStores, &tailStores) {
return false
Expand Down Expand Up @@ -342,6 +350,8 @@ private func rewriteUses(of startValue: Value, _ context: FunctionPassContext) {
context.erase(instruction: endMutation)
case let upCast as UpcastInst:
worklist.pushIfNotVisited(usersOf: upCast)
case let refCast as UncheckedRefCastInst:
worklist.pushIfNotVisited(usersOf: refCast)
case let moveValue as MoveValueInst:
worklist.pushIfNotVisited(usersOf: moveValue)
case is DeallocRefInst, is DeallocStackRefInst:
Expand Down
15 changes: 14 additions & 1 deletion lib/SILOptimizer/Transforms/COWOpts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ static SILValue skipStructAndExtract(SILValue value) {
}

bool COWOptsPass::optimizeBeginCOW(BeginCOWMutationInst *BCM) {
LLVM_DEBUG(llvm::dbgs() << "Looking at: ");
LLVM_DEBUG(BCM->dump());

SILFunction *function = BCM->getFunction();
StackList<EndCOWMutationInst *> endCOWMutationInsts(function);
InstructionSet endCOWMutationsFound(function);
Expand Down Expand Up @@ -187,14 +190,20 @@ bool COWOptsPass::optimizeBeginCOW(BeginCOWMutationInst *BCM) {
// Don't immediately bail on a store instruction. Instead, remember
// it and check if it interferes with any (potential) load.
if (storeAddrsFound.insert(store->getDest())) {
LLVM_DEBUG(llvm::dbgs() << "Found store escape, record: ");
LLVM_DEBUG(inst->dump());
storeAddrs.push_back(store->getDest());
numStoresFound += 1;
}
} else {
LLVM_DEBUG(llvm::dbgs() << "Found non-store escape, bailing out: ");
LLVM_DEBUG(inst->dump());
return false;
}
}
if (inst->mayReadFromMemory()) {
LLVM_DEBUG(llvm::dbgs() << "Found a may read inst, record: ");
LLVM_DEBUG(inst->dump());
potentialLoadInsts.push_back(inst);
numLoadsFound += 1;
}
Expand Down Expand Up @@ -225,8 +234,12 @@ bool COWOptsPass::optimizeBeginCOW(BeginCOWMutationInst *BCM) {
return false;
for (SILInstruction *load : potentialLoadInsts) {
for (SILValue storeAddr : storeAddrs) {
if (!AA || AA->mayReadFromMemory(load, storeAddr))
if (!AA || AA->mayReadFromMemory(load, storeAddr)) {
LLVM_DEBUG(llvm::dbgs() << "Found a store address aliasing with a load:");
LLVM_DEBUG(load->dump());
LLVM_DEBUG(storeAddr->dump());
return false;
}
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions test/SILOptimizer/objectoutliner.sil
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,24 @@ bb0:
return %r : $()
}

// CHECK-LABEL: sil @outline_global_with_uncheckedrefcast :
// CHECK: global_value
// CHECK: } // end sil function 'outline_global_with_uncheckedrefcast'
sil @outline_global_with_uncheckedrefcast : $@convention(thin) () -> () {
bb0:
%0 = integer_literal $Builtin.Word, 0
%1 = integer_literal $Builtin.Int64, 1
%4 = struct $Int64 (%1 : $Builtin.Int64)
%7 = alloc_ref [tail_elems $Int64 * %0 : $Builtin.Word] $Obj
%9 = ref_element_addr %7 : $Obj, #Obj.value
store %4 to %9 : $*Int64
%10 = unchecked_ref_cast %7 : $Obj to $Base
%11 = end_cow_mutation %10 : $Base
%12 = begin_dealloc_ref %11 : $Base of %7 : $Obj
dealloc_ref %12 : $Base
%r = tuple ()
return %r : $()
}

// CHECK-LABEL: sil @outline_global_tailelems
// CHECK: [[G:%[0-9]+]] = global_value @outline_global_tailelemsTv_ : $Obj
Expand Down