Skip to content

[SourceLocation] Extract and rework line tables #2900

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 1 commit into from
Nov 21, 2024

Conversation

rintaro
Copy link
Member

@rintaro rintaro commented Nov 19, 2024

  • computeLines now produces newly introducded 'SourceLineTable'.
  • SourceLineTable is factored out line tables and #sourceLocation directive info, extracted from SourceLocationConverter.
  • forEachLineLength is now forEachEndOfLine which emits the absolute position of each end-of-line, instead of the line length, because the caller can easily derive the start-of-line position (i.e. the last end-of-line).
  • #sourceLocation info are now handled differently. Specifically, while traversing the syntax tree, it only collects the raw syntax, then post-process those info to produce the newly introduced VirtualFile. VirtualFile is a type that have enough infomation for swift::SourceMamanger in the compiler. Post-processing is needed because we want the start position of the next line of the end of #sourceLocation directive, which might still not available when the first pass sees the directive.
  • In the future, the compiler shoud only use SourceLineTable instead of SourceLocationConverter as swift::SourceManager already know the file name and the whole source buffer.

No significant performance change by this.
before:

measured [Time, seconds] average: 4.666, relative standard deviation: 0.639%, values: [4.714070, 4.638068, 4.647662, 4.643069, 4.649315, 4.630860, 4.702791, 4.700956, 4.687687, 4.646881], performanceMetricID:com.apple.XCTPerformanceMetric_WallClockTime, baselineName: "", baselineAverage: , polarity: prefers smaller, maxPercentRegression: 10.000%, maxPercentRelativeStandardDeviation: 10.000%, maxRegression: 0.100, maxStandardDeviation: 0.100

after

measured [Time, seconds] average: 4.452, relative standard deviation: 0.508%, values: [4.455549, 4.432555, 4.496990, 4.487179, 4.457557, 4.429874, 4.448766, 4.449664, 4.427860, 4.433717], performanceMetricID:com.apple.XCTPerformanceMetric_WallClockTime, baselineName: "", baselineAverage: , polarity: prefers smaller, maxPercentRegression: 10.000%, maxPercentRelativeStandardDeviation: 10.000%, maxRegression: 0.100, maxStandardDeviation: 0.100

@rintaro
Copy link
Member Author

rintaro commented Nov 19, 2024

@swift-ci Please test

@rintaro rintaro marked this pull request as ready for review November 19, 2024 23:29
@rintaro
Copy link
Member Author

rintaro commented Nov 20, 2024

@swift-ci Please test

@rintaro rintaro force-pushed the linetables-sourceloc branch from 3848831 to 74765fd Compare November 20, 2024 16:10
@rintaro
Copy link
Member Author

rintaro commented Nov 20, 2024

@swift-ci Please test

@rintaro
Copy link
Member Author

rintaro commented Nov 20, 2024

@swift-ci Please test Windows

@rintaro rintaro requested a review from hamishknight November 20, 2024 19:43
Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Nice

public var fileName: String
/// Line number offset from the physical line number.
public var lineOffset: Int
/// The position of the filename literal.
Copy link
Member

Choose a reason for hiding this comment

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

Could you elaborate what fileNamePosition is, maybe with an example? It’s not immediately clear to me.

Copy link
Member Author

Choose a reason for hiding this comment

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

For example, for #sourceLocation(file: "input", line: 12), fileNamePosition is the position of "input". This is needed by the compiler to diagnose them later in the compiler. See https://github.com/swiftlang/swift/blob/50175c7e41120facbea67fc57af810e15f646675/test/ASTGen/sourcelocation.swift#L28-L39

Copy link
Member

Choose a reason for hiding this comment

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

Would you mind adding a comment for that?


extension SortedArray: Sendable where Element: Sendable {}

/// Represent a virtual file region in the source file.
Copy link
Member

Choose a reason for hiding this comment

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

Could you give an example what virtual files are used for here? Does #sourceLocation create a virtual file or are virtual files for macro expansions etc.?

Copy link
Member Author

Choose a reason for hiding this comment

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

VirtualFile corresponds to swift::SourceManager::VirtualFile
This will be used for populating swift::SourceManager's table see swiftlang/swift#77721

Copy link
Member

Choose a reason for hiding this comment

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

I think a doc comment would be helpful for this.

@rintaro rintaro force-pushed the linetables-sourceloc branch from 74765fd to 1250cfd Compare November 21, 2024 14:02
@rintaro
Copy link
Member Author

rintaro commented Nov 21, 2024

@swift-ci Please test

@rintaro
Copy link
Member Author

rintaro commented Nov 21, 2024

@swift-ci Please smoke test

* `computeLines` now produces newly introducded 'SourceLineTable'.
* `SourceLineTable` is factored out line tables and `#sourceLocation`
  directive info, extracted from `SourceLocationConverter`.
* `forEachLineLength` is now `forEachEndOfLine` which emits the absolute
  position of each end-of-line, instead of the line length, because the
  caller can easily derive the start-of-line position (i.e. the last
  end-of-line).
* `#sourceLocation` info are now handled differently. Specifically, while
  traversing the syntax tree, it only collects the raw syntax, then
  post-process those info to produce the newly introduced `VirtualFile`.
  `VirtualFile` is a type that have enough infomation for
  `swift::SourceMamanger` in the compiler. Post-processing is needed
  because we want the start position of the _next_ line of the end of
  `#sourceLocation` directive, which might still not available when the
  first pass sees the directive.
* In the future, the compiler shoud only use `SourceLineTable` instead
  of `SourceLocationConverter` as `swift::SourceManager` already know the
  file name and the whole source buffer.
@rintaro rintaro force-pushed the linetables-sourceloc branch from 1250cfd to 42cff3a Compare November 21, 2024 16:10
@rintaro
Copy link
Member Author

rintaro commented Nov 21, 2024

@swift-ci Please test

@rintaro
Copy link
Member Author

rintaro commented Nov 21, 2024

@swift-ci Please test Windows

@rintaro
Copy link
Member Author

rintaro commented Nov 21, 2024

@swift-ci Please test

@rintaro rintaro enabled auto-merge November 21, 2024 16:24
@rintaro rintaro merged commit b721fa8 into swiftlang:main Nov 21, 2024
3 checks passed
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.

3 participants