Skip to content

Enable CSE on OSSA #34895

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 6 commits into from
Dec 23, 2020
Merged

Enable CSE on OSSA #34895

merged 6 commits into from
Dec 23, 2020

Conversation

meg-gupta
Copy link
Contributor

No description provided.

@gottesmm
Copy link
Contributor

gottesmm commented Dec 1, 2020

I started perusing maybe I missed, do you have a bunch of tests with guaranteed values?

@meg-gupta meg-gupta force-pushed the cseossa branch 2 times, most recently from 8c462b5 to e9a610b Compare December 1, 2020 09:21
@meg-gupta
Copy link
Contributor Author

@gottesmm I added some tests with guaranteed values now.

@meg-gupta
Copy link
Contributor Author

@swift-ci test

@swift-ci
Copy link
Contributor

swift-ci commented Dec 1, 2020

Build failed
Swift Test Linux Platform
Git Sha - c035ffba046d5a1f17952503dad48155e2639a44

@swift-ci
Copy link
Contributor

swift-ci commented Dec 1, 2020

Build failed
Swift Test OS X Platform
Git Sha - c035ffba046d5a1f17952503dad48155e2639a44

// new opened archetype already registered in
// CSE::processOpenExistentialRefInst.
auto *oldOpenedArchetypeDef = oldUse->getUser();
auto &Builder = Cloner.getBuilder();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Builder -> builder.

RunsOnHighLevelSil(RunsOnHighLevelSil) {}
OpenedArchetypesTracker(OpenedArchetypesTracker), Cloner(Cloner),
RunsOnHighLevelSil(RunsOnHighLevelSil) {
auto &Builder = Cloner.getBuilder();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: also no captial. Or maybe even: Cloner.getBuilder().setOpenedArchetypesTracker...

@meg-gupta meg-gupta force-pushed the cseossa branch 2 times, most recently from 9cbf2cd to 85cfe43 Compare December 3, 2020 23:23
@meg-gupta
Copy link
Contributor Author

@swift-ci test

@swift-ci
Copy link
Contributor

swift-ci commented Dec 4, 2020

Build failed
Swift Test OS X Platform
Git Sha - 85cfe4388469cf726c3427b92132a2dc2d7b0a19

@meg-gupta meg-gupta force-pushed the cseossa branch 4 times, most recently from b5be2e3 to e999492 Compare December 15, 2020 07:52
@meg-gupta
Copy link
Contributor Author

@swift-ci test

@meg-gupta
Copy link
Contributor Author

@swift-ci test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 15c9dd1f1c89fa1a1c2dc62a78ad7241455b0d53

@meg-gupta
Copy link
Contributor Author

@swift-ci test

Copy link
Contributor

@zoecarver zoecarver left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This lgtm now. Thanks for all your work on this :)

auto *innerBorrow =
reborrowBuilder.createBeginBorrow(bi->getLoc(), innerCopy);
auto *outerEndBorrow = reborrowBuilder.createEndBorrow(bi->getLoc(), value);
SILLocation loc = RegularLocation(bi->getLoc().getSourceLoc());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: couldn't this just be bi->getLoc()?

@@ -1284,22 +1372,32 @@ class SILCSE : public SILFunctionTransform {
auto *SEA = PM->getAnalysis<SideEffectAnalysis>();
SILOptFunctionBuilder FuncBuilder(*this);

CSE C(RunsOnHighLevelSil, SEA, FuncBuilder);
auto *func = getFunction();
SILOpenedArchetypesTracker OpenedArchetypesTracker(func);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: camelCase not TitleCase.

Copy link
Contributor

@gottesmm gottesmm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some additional comments. I need to read this locally before giving it my +1. I am doing that now.

auto Operands = X->getOperandValues();
SmallVector<SILValue, 4> OperandValues;
llvm::transform(
Operands, std::back_inserter(OperandValues),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thought. Is it possible to create a range that does this transform and pass that to hash_combine instead? That way we do not intermediate arrays (and potential mallocs).

(Keep in mind I am just tagging this one, but I am talking about all of the places that this was done.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try to do this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a transform function as an optional argument to SILInstruction::getOperandValues. It may be useful in other code as well.

@@ -73,6 +73,10 @@ Optional<SILBasicBlock::iterator> swift::getInsertAfterPoint(SILValue val) {
if (isa<SingleValueInstruction>(val)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not:

if (auto *inst = val->getDefiningInstruction()) {
  return std::next(inst->getIterator());
}

@gottesmm
Copy link
Contributor

@meg-gupta I was talking with Andy about this issue around ValueLifetimeAnalysis and returns. I think we are hacking around bugs here. We need a way to have a /real/ location given a terminator insert pt and value lifetime analysis should be smart enough about return inst.

bool result = lifetimeAnalysis.computeFrontier(
frontier, ValueLifetimeAnalysis::DontModifyCFG, &ctx.deBlocks);
assert(result);
bool noCriticalEdges = lifetimeAnalysis.computeFrontier(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the wrong way to do this. Why is this needed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifically, if it is needed due to the terminator issue we spoke about before, I think that Andy's recent change adding UnownedInstantaneousUse makes that not necessary for unowned things

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, this was not needed. It turned out to be a bug where the ownership rauw was considering transitive uses of none ownership as well.

Copy link
Contributor

@gottesmm gottesmm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More initial comments.

@meg-gupta meg-gupta force-pushed the cseossa branch 2 times, most recently from b97dbb1 to 138b4e8 Compare December 21, 2020 04:25
…value/destroy_value etc

Branch instructions and frontiers can have
LocationKind::ReturnKind/ImplicitReturnKind which are not correct
locations to use for copy_value/destroy_value etc
Copy link
Contributor

@gottesmm gottesmm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some more final comments! Looks pretty good! I think we are close!

@meg-gupta
Copy link
Contributor Author

@swift-ci test

@meg-gupta
Copy link
Contributor Author

@swift-ci benchmark

@swift-ci
Copy link
Contributor

Performance: -O

Regression OLD NEW DELTA RATIO
Data.hash.Medium 25 28 +12.0% 0.89x (?)
 
Improvement OLD NEW DELTA RATIO
LessSubstringSubstring 29 22 -24.1% 1.32x
EqualSubstringSubstring 29 22 -24.1% 1.32x
EqualSubstringString 29 22 -24.1% 1.32x (?)
LessSubstringSubstringGenericComparable 29 22 -24.1% 1.32x
EqualStringSubstring 30 23 -23.3% 1.30x (?)
EqualSubstringSubstringGenericEquatable 30 23 -23.3% 1.30x
StringComparison_longSharedPrefix 358 321 -10.3% 1.12x (?)
SortStringsUnicode 2275 2080 -8.6% 1.09x (?)
UTF8Decode_InitDecoding_ascii 203 189 -6.9% 1.07x (?)
Data.init.Sequence.513B.Count0.I 322 300 -6.8% 1.07x (?)
AngryPhonebook.Armenian 136 127 -6.6% 1.07x (?)

Code size: -O

Performance: -Osize

Regression OLD NEW DELTA RATIO
StringHasPrefixAscii 980 1060 +8.2% 0.92x (?)
 
Improvement OLD NEW DELTA RATIO
LessSubstringSubstring 29 22 -24.1% 1.32x
EqualSubstringSubstring 29 22 -24.1% 1.32x (?)
EqualStringSubstring 29 22 -24.1% 1.32x
EqualSubstringSubstringGenericEquatable 29 22 -24.1% 1.32x
EqualSubstringString 29 22 -24.1% 1.32x
LessSubstringSubstringGenericComparable 29 22 -24.1% 1.32x
AngryPhonebook.Cyrillic 148 138 -6.8% 1.07x (?)
AngryPhonebook.Armenian 136 127 -6.6% 1.07x (?)

Code size: -Osize

Performance: -Onone

Regression OLD NEW DELTA RATIO
DropLastCountableRange 182 201 +10.4% 0.91x (?)
ConvertFloatingPoint.MockFloat64ToDouble 1052 1159 +10.2% 0.91x (?)
ObjectiveCBridgeStubToNSStringRef 128 141 +10.2% 0.91x (?)
PrefixCountableRange 526 573 +8.9% 0.92x (?)
Data.hash.Medium 36 39 +8.3% 0.92x (?)
ObjectiveCBridgeStubToNSDateRef 2900 3140 +8.3% 0.92x (?)
 
Improvement OLD NEW DELTA RATIO
LessSubstringSubstring 89 81 -9.0% 1.10x
NSStringConversion.MutableCopy.LongUTF8 933 851 -8.8% 1.10x (?)
LessSubstringSubstringGenericComparable 86 79 -8.1% 1.09x (?)
EqualSubstringSubstring 88 81 -8.0% 1.09x (?)
NSStringConversion.MutableCopy.Long 1335 1237 -7.3% 1.08x (?)
AngryPhonebook.Armenian 137 127 -7.3% 1.08x (?)
AngryPhonebook.Cyrillic 148 138 -6.8% 1.07x (?)

Code size: -swiftlibs

How to read the data The tables contain differences in performance which are larger than 8% and differences in code size which are larger than 1%.

If you see any unexpected regressions, you should consider fixing the
regressions before you merge the PR.

Noise: Sometimes the performance results (not code size!) contain false
alarms. Unexpected regressions which are marked with '(?)' are probably noise.
If you see regressions which you cannot explain you can try to run the
benchmarks again. If regressions still show up, please consult with the
performance team (@eeckstein).

Hardware Overview
  Model Name: Mac mini
  Model Identifier: Macmini8,1
  Processor Name: 6-Core Intel Core i7
  Processor Speed: 3.2 GHz
  Number of Processors: 1
  Total Number of Cores: 6
  L2 Cache (per Core): 256 KB
  L3 Cache: 12 MB
  Memory: 64 GB

…auw of guaranteed values

When we are populating transitive users while handling guaranteed values
in ownership rauw, we were including values with none ownership.

Example : rauw of %2 with a dominating value
   Here %arg1 was also considered a transitive use

   %2 = struct_extract %0 : $StructWithEnum2, #StructWithEnum2.val
   %copy = copy_value %2 : $FakeOptional2
   switch_enum %2 : $FakeOptional2, case #FakeOptional2.some1!enumelt:bb5, case #FakeOptional2.some2!enumelt:bb6

  bb5(%arg1 : $UInt):
   br bb7(%arg1 : $UInt)

  bb6(%arg2 : @guaranteed $Klass):
   %4 = unchecked_trivial_bit_cast %arg2 : $Klass to $UInt
   br bb7(%4 : $UInt)

This is incorrect because %arg1 is a trivial value, and this also
leads to ValueLifetimeAnalysis needing a split for finding a frontier
for the use of %arg1 in the branch instruction. In ossa, we should never
have to split edges for finding frontiers, because we do not have critical
edges.
@meg-gupta
Copy link
Contributor Author

@swift-ci test

@meg-gupta
Copy link
Contributor Author

@swift-ci test

@meg-gupta
Copy link
Contributor Author

@gottesmm I am merging this. I'll add a follow on commit if there are any other comments.

@meg-gupta meg-gupta merged commit b99533a into swiftlang:main Dec 23, 2020
@zoecarver
Copy link
Contributor

Woohoo! So excited to see ownership making its way through the SILOptimizer. Thanks for all your work on this, @meg-gupta.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants