Skip to content

SILOptimizer: handle copies in LetPropertiesOpt. #19120

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
Sep 4, 2018
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
24 changes: 19 additions & 5 deletions lib/SILOptimizer/IPO/LetPropertiesOpts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,16 @@ bool LetPropertiesOpt::isConstantLetProperty(VarDecl *Property) {
return true;
}

static bool isProjectionOfProperty(SILValue addr, VarDecl *Property) {
if (auto *REA = dyn_cast<RefElementAddrInst>(addr)) {
return REA->getField() == Property;
}
if (auto *SEA = dyn_cast<StructElementAddrInst>(addr)) {
return SEA->getField() == Property;
}
return false;
}

// Analyze the init value being stored by the instruction into a property.
bool
LetPropertiesOpt::analyzeInitValue(SILInstruction *I, VarDecl *Property) {
Expand All @@ -398,15 +408,19 @@ LetPropertiesOpt::analyzeInitValue(SILInstruction *I, VarDecl *Property) {
} else if (auto SI = dyn_cast<StoreInst>(I)) {
auto Dest = stripAddressAccess(SI->getDest());

assert(((isa<RefElementAddrInst>(Dest) &&
cast<RefElementAddrInst>(Dest)->getField() == Property) ||
(isa<StructElementAddrInst>(Dest) &&
cast<StructElementAddrInst>(Dest)->getField() == Property)) &&
"Store instruction should store into a proper let property");
assert(isProjectionOfProperty(stripAddressAccess(SI->getDest()), Property)
&& "Store instruction should store into a proper let property");
(void) Dest;
value = SI->getSrc();
}

// Check if it's just a copy from another instance of the struct.
if (auto *LI = dyn_cast<LoadInst>(value)) {
SILValue addr = LI->getOperand();
if (isProjectionOfProperty(addr, Property))
return true;
}

// Bail if a value of a property is not a statically known constant init.
if (!analyzeStaticInitializer(value, ReverseInsns))
return false;
Expand Down
14 changes: 13 additions & 1 deletion lib/SILOptimizer/Transforms/SILSROA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ bool SROAMemoryUseAnalyzer::analyze() {
return false;
}

bool hasBenefit = false;

// Go through uses of the memory allocation of AI...
for (auto *Operand : getNonDebugUses(SILValue(AI))) {
SILInstruction *User = Operand->getUser();
Expand All @@ -150,6 +152,9 @@ bool SROAMemoryUseAnalyzer::analyze() {
LLVM_DEBUG(llvm::dbgs() << " Found a store into the "
"projection.\n");
Stores.push_back(SI);
SILValue Src = SI->getSrc();
if (isa<StructInst>(Src) || isa<TupleInst>(Src))
hasBenefit = true;
continue;
} else {
LLVM_DEBUG(llvm::dbgs() << " Found a store of the "
Expand All @@ -163,6 +168,11 @@ bool SROAMemoryUseAnalyzer::analyze() {
if (auto *LI = dyn_cast<LoadInst>(User)) {
LLVM_DEBUG(llvm::dbgs() << " Found a load of the projection.\n");
Loads.push_back(LI);
for (auto useIter = LI->use_begin(), End = LI->use_end();
!hasBenefit && useIter != End; useIter++) {
hasBenefit = (isa<StructExtractInst>(useIter->get()) ||
isa<TupleExtractInst>(useIter->get()));
}
continue;
}

Expand All @@ -171,6 +181,7 @@ bool SROAMemoryUseAnalyzer::analyze() {
if (auto *ASI = dyn_cast<StructElementAddrInst>(User)) {
LLVM_DEBUG(llvm::dbgs() << " Found a struct subprojection!\n");
ExtractInsts.push_back(ASI);
hasBenefit = true;
continue;
}

Expand All @@ -179,6 +190,7 @@ bool SROAMemoryUseAnalyzer::analyze() {
if (auto *TSI = dyn_cast<TupleElementAddrInst>(User)) {
LLVM_DEBUG(llvm::dbgs() << " Found a tuple subprojection!\n");
ExtractInsts.push_back(TSI);
hasBenefit = true;
continue;
}

Expand All @@ -195,7 +207,7 @@ bool SROAMemoryUseAnalyzer::analyze() {

// Analysis was successful. We can break up this allocation!
++NumChoppedAllocas;
return true;
return hasBenefit;
}

void
Expand Down
22 changes: 22 additions & 0 deletions test/SILOptimizer/let_properties_opts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,25 @@ public func useInitializers() -> StructWithPublicAndInternalLetProperties {
public func useInitializers() -> StructWithPublicAndInternalAndPrivateLetProperties {
return StructWithPublicAndInternalAndPrivateLetProperties(1, 1)
}

struct RACStruct {
private let end = 27

var startIndex: Int { return 0 }


// CHECK-LABEL: RACStruct.endIndex.getter
// CHECK-NEXT: sil hidden @{{.*}}endIndexSivg
// CHECK-NEXT: bb0
// CHECK-NEXT: %1 = integer_literal $Builtin.Int{{.*}}, 27
// CHECK-NEXT: %2 = struct $Int (%1 : $Builtin.Int{{.*}})
// CHECK-NEXT: return %2 : $Int
var endIndex: Int { return end }

subscript(_ bitIndex: Int) -> Bool {
get { return false }
set { }
}
}

extension RACStruct : RandomAccessCollection {}
2 changes: 2 additions & 0 deletions test/SILOptimizer/optionset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public struct TestOptions: OptionSet {
// CHECK-NEXT: bb0:
// CHECK-NEXT: integer_literal {{.*}}, 15
// CHECK-NEXT: struct $Int
// CHECK-NEXT: debug_value
// CHECK-NEXT: struct $TestOptions
// CHECK-NEXT: return
public func returnTestOptions() -> TestOptions {
Expand All @@ -26,6 +27,7 @@ public func returnTestOptions() -> TestOptions {
// CHECK-NEXT: global_addr
// CHECK-NEXT: integer_literal {{.*}}, 15
// CHECK-NEXT: struct $Int
// CHECK-NEXT: debug_value
// CHECK-NEXT: struct $TestOptions
// CHECK-NEXT: store
// CHECK-NEXT: tuple
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ ArchetypeToConcreteConvertUInt8(t: f)
// CHECK-LABEL: sil shared [noinline] @$S37specialize_unconditional_checked_cast31ArchetypeToConcreteConvertUInt8{{[_0-9a-zA-Z]*}}3Not{{.*}}Tg5 : $@convention(thin) (NotUInt8) -> NotUInt8 {
// CHECK: bb0
// CHECK-NEXT: debug_value %0
// CHECK-NEXT: debug_value %0
// CHECK-NEXT: return %0

// x -> y where y is a class but x is not.
Expand Down
17 changes: 17 additions & 0 deletions test/SILOptimizer/sroa.sil
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@ bb0(%0 : $S1):
return %6 : $()
}

// CHECK-LABEL: sil @dont_sroa_without_benefit : $@convention(thin) (S1) -> S1
// CHECK: bb0
// CHECK-NEXT: alloc_stack $S1
// CHECK-NEXT: store %0 to %1 : $*S1
// CHECK-NEXT: load %1 : $*S1
// CHECK-NEXT: dealloc_stack %1 : $*S1
// CHECK-NEXT: return
sil @dont_sroa_without_benefit : $@convention(thin) (S1) -> S1 {
bb0(%0 : $S1):
%1 = alloc_stack $S1
store %0 to %1 : $*S1
%3 = load %1 : $*S1
dealloc_stack %1 : $*S1
return %3 : $S1
}


///////////////////////////////
// Struct With Struct Fields //
///////////////////////////////
Expand Down