Skip to content

Make swift::SourceLoc and swift::SourceRange usable as a key type for DenseSet/DenseMap. #18417

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 3 commits into from
Aug 6, 2018

Conversation

mhong
Copy link

@mhong mhong commented Jul 31, 2018

One use case is using DenseSet to manage a set of source locations, so that we
emit at most one compiler diagnostic per location (see
#18402).

One use case is using DenseSet to manage a set of source locations, so that we
emit at most one compiler diagnostic per location (see
swiftlang#18402).
@mhong
Copy link
Author

mhong commented Jul 31, 2018

@lattner Who would you suggest we loop in for review? Thanks.

@mhong mhong requested a review from lattner July 31, 2018 21:35
template <typename T> struct DenseMapInfo;

template <> struct DenseMapInfo<swift::SourceRange> {
static swift::SourceRange getEmptyKey() { return swift::SourceRange(); }
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd suggest changing this to:

SMLoc::getFromPointer(DenseMapInfo<const char *>::getEmptyKey())))

for consistency.

@lattner
Copy link
Contributor

lattner commented Jul 31, 2018

I can review it, LGTM.

@jrose-apple
Copy link
Contributor

I do think you should use SourceLoc for your original test case, though. No reason to store twice as many pointers.

@mhong
Copy link
Author

mhong commented Aug 1, 2018

@jrose-apple, I tried out a simple example like:

public func test() {
  var a = 1.0
  a += 2.0
}

In the SIL code, I find that the insts that read the old a in a += 2.0 and the insts that wrte to the new a in a += 2.0 could share the same start loc, but have a different end loc.

e.g. I saw these two insts:

%4 = begin_access [modify] [static] %0 : $*Double

1: inst->getLoc().getSourceRange() = {Start = {Value = {
      Ptr = 0x877d061 "a += 2.0\n\n  let x = Tensor<Float>(1.0)\n}\n"}}, End = {Value = {
      Ptr = 0x877d061 "a += 2.0\n\n  let x = Tensor<Float>(1.0)\n}\n"}}}

vs

%5 = struct_element_addr %4 : $*Double, #Double._value

1: inst->getLoc().getSourceRange() = {Start = {Value = {
      Ptr = 0x877d061 "a += 2.0\n\n  let x = Tensor<Float>(1.0)\n}\n"}}, End = {Value = {
      Ptr = 0x877d066 "2.0\n\n  let x = Tensor<Float>(1.0)\n}\n"}}}

The former reads the old a, and the latter starts the computation for doing +=.

So it'd be safer to store the loc pair. We don't expect to generate a lot of sends/recvs warnings, but if performance ever becomes a concern, we can revisit this (e.g. maybe just warn on the first K occurrences globally, or within each function.)

@mhong
Copy link
Author

mhong commented Aug 1, 2018

@swift-ci please test

@jrose-apple
Copy link
Contributor

I mean, even the range isn't unique per instruction, because of implicit expressions, or just AST-level expressions that take more than one instruction. You could also have unrelated diagnostics at the same location.

…te can

decide whether to use a map key based on SourceLoc or SourceRange.
@mhong mhong changed the title Make swift::SourceRange usable as a key type for DenseSet/DenseMap. Make swift::SourceLoc and swift::SourceRange usable as a key type for DenseSet/DenseMap. Aug 2, 2018
@mhong
Copy link
Author

mhong commented Aug 2, 2018

@jrose-apple Based on the feedback, I added DenseMapInfo specialization for SourceLoc as well. Once we downstream this patch into tensorflow branch, I can change the callsite to use SourceLoc as the key.

Please let me know if there are more comments. Thank you!

@mhong
Copy link
Author

mhong commented Aug 3, 2018

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Aug 3, 2018

Build failed
Swift Test OS X Platform
Git Sha - 0a09541

@swift-ci
Copy link
Contributor

swift-ci commented Aug 3, 2018

Build failed
Swift Test Linux Platform
Git Sha - 0a09541

Copy link
Contributor

@jrose-apple jrose-apple left a comment

Choose a reason for hiding this comment

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

This seems reasonable to me!

@mhong
Copy link
Author

mhong commented Aug 3, 2018

Thank you @jrose-apple! I'll attempt a merge.

@mhong
Copy link
Author

mhong commented Aug 3, 2018

@swift-ci please test and merge

1 similar comment
@mhong
Copy link
Author

mhong commented Aug 6, 2018

@swift-ci please test and merge

@swift-ci swift-ci merged commit db1fb38 into swiftlang:master Aug 6, 2018
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