Skip to content

[Parse] Discriminate local variables #16130

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 5 commits into from
May 11, 2018

Conversation

rintaro
Copy link
Member

@rintaro rintaro commented Apr 24, 2018

Set local discriminator for all local VarDecls. Otherwise, they cannot be discriminated with USRs. This change is needed for rename refactoring which uses USR for discrimiating variable names.

SR-7205, rdar://problem/34701880

@rintaro
Copy link
Member Author

rintaro commented Apr 24, 2018

@swift-ci Please smoke test

do {
try arg()
} catch let x as Err1 {
print(xRenamed)
Copy link
Member Author

Choose a reason for hiding this comment

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

let x above should be renamed as well. Investigating.

@rintaro
Copy link
Member Author

rintaro commented Apr 25, 2018

@swift-ci Please smoke test

@@ -581,7 +581,7 @@ std::pair<bool, Pattern*> NameMatcher::walkToPatternPre(Pattern *P) {
if (isDone() || shouldSkip(P->getSourceRange()))
return std::make_pair(false, P);

tryResolve(ASTWalker::ParentTy(P), P->getLoc());
tryResolve(ASTWalker::ParentTy(P), P->getStartLoc());
Copy link
Member Author

Choose a reason for hiding this comment

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

@nkcsgexi is this OK?

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 fine for patterns. We need to be careful for decls though, sometimes getLoc() and getStartLoc point to different position IIRC.

@rintaro
Copy link
Member Author

rintaro commented Apr 25, 2018

@rjmccall In commit message of 892a900, you explicitly excluded physical vars

Track a discriminator for named declarations (except physical vars) in local contexts, for future use in mangling.

What was the reason?

@rjmccall
Copy link
Contributor

Computing and storing a discriminator means time and memory overhead, and we don't actually need a discriminator because an ordinary local variable doesn't require the creation of anything outside the code-generation of the function.

@rjmccall
Copy link
Contributor

If you're really going to attach discriminators to every single local variable, the side-table storage approach is probably unreasonable.

@rintaro rintaro force-pushed the parse-var-discriminator branch 2 times, most recently from 95f0475 to 3b19a00 Compare May 8, 2018 12:00
@rintaro
Copy link
Member Author

rintaro commented May 8, 2018

I embedded LocalDiscriminator to ValueDecls class fields.
@rjmccall How does this look?

JustFYI, I measured how many local discriminators are actually set, using Swift stdlib source files.

Before:
Parser::setLocalDiscriminator is called 5683 times, 130 are actually in local context.
After:
Parser::setLocalDiscriminator is called 10015 times, 3861 are actually in local context.

Focused on VarDecls:
Before:
Parser::setLocalDiscriminator is called 1275 times, 0 are actually in local context.
After:
Parser::setLocalDiscriminator is called 5607 times, 3731 are actually in local context.

@rintaro
Copy link
Member Author

rintaro commented May 8, 2018

@swift-ci Please smoke test

@jrose-apple
Copy link
Contributor

I see you removing the local discriminator from serialization completely. Is it really not needed?

@rintaro
Copy link
Member Author

rintaro commented May 8, 2018

@jrose-apple I believe it's not needed because local discriminators are serialized/deserialized as a part of Decl emission for every ValueDecls in local context. https://github.com/apple/swift/blob/db423c32821a7223a13cdf4786204cf843aa2459/lib/Serialization/Serialization.cpp#L2662-L2667

@jrose-apple
Copy link
Contributor

jrose-apple commented May 8, 2018

Ah, I didn't realize that was already there! Okay then. Please bump the version number in ModuleFormat.h too! Oops, you got that too. Thank you.

@rjmccall
Copy link
Contributor

rjmccall commented May 8, 2018

Do we really only have 3700 local variable declarations in the stdlib? That seems extremely low.

Are we not setting a discriminator on parameters? I guess maybe we don't need to because we can identify them reliably relative to the function they're parameters of; that would definitely keep the count down.

@rintaro rintaro force-pushed the parse-var-discriminator branch from 3b19a00 to d086445 Compare May 9, 2018 08:35
@rintaro
Copy link
Member Author

rintaro commented May 9, 2018

Ah, I forgot about ParamDecl. And yes, we need to discriminate them.

func foo(_ arg: Int) {
  var arg = arg;
}

Updated the PR.
Now that, 10492 out of 12368 VarDecls are in local context.

As a side note, this PR impacts the parse time by around 1% (0.654s -> 0.662s) and max rss by around 1% (165,376,000 -> 167,411,712).

@rintaro
Copy link
Member Author

rintaro commented May 9, 2018

@swift-ci Please smoke test

1 similar comment
@rintaro
Copy link
Member Author

rintaro commented May 9, 2018

@swift-ci Please smoke test

@rjmccall
Copy link
Contributor

rjmccall commented May 9, 2018

Are you sure you can't distinguish ParamDecls implicitly? A ParamDecl should always be uniquely identified by (1) the function it's a parameter of, (2) the fact that it's a parameter, and (3) its index.

@rjmccall
Copy link
Contributor

rjmccall commented May 9, 2018

Not that it really hurts to discriminate them, given that we're paying the memory cost in the base class anyway.

@rintaro
Copy link
Member Author

rintaro commented May 9, 2018

Distinguishing ParamDecls without using LocalDiscriminator is technically feasible, but I think using LocalDiscriminator is easiest/convenient way to achieve that. And, as you said, we already have facility for that.
@swift-ci Please smoke test

@rintaro
Copy link
Member Author

rintaro commented May 10, 2018

@swift-ci Please smoke test

@rintaro
Copy link
Member Author

rintaro commented May 10, 2018

@rjmccall Could you approve this?

Copy link
Contributor

@rjmccall rjmccall left a comment

Choose a reason for hiding this comment

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

This PR looks good to me, but I think you should consider adding some way to test local discriminators more directly. Maybe a testing switch that prints declaration names with their line, column, and discriminator?

rintaro added 5 commits May 11, 2018 15:34
Set local discriminator for all local `VarDecl`s. Otherwise, they cannot
be discriminated with USRs. This change is needed for rename refactoring which
uses USR for discrimiating variable names.

https://bugs.swift.org/browse/SR-7205,
rdar://problem/34701880
For Example `getLoc()` for `foo as Ty`(`IsPattern`) is at 'as'. We
should try to resolve this at position of 'foo'.
Local discriminators are emitted as a part of decl.
We have to discriminate between params and local variables.
@rintaro rintaro force-pushed the parse-var-discriminator branch from d086445 to a6f7a8e Compare May 11, 2018 06:44
@rintaro
Copy link
Member Author

rintaro commented May 11, 2018

Thanks. That is actually a good idea!

@rintaro
Copy link
Member Author

rintaro commented May 11, 2018

Resolved conflicts.
@swift-ci Please smoke test

@rintaro rintaro merged commit 9dcd0f9 into swiftlang:master May 11, 2018
@rintaro rintaro deleted the parse-var-discriminator branch May 11, 2018 08:30
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.

5 participants