Skip to content
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

Use hierarchical namespaces in name resolution #1364

Closed
wants to merge 95 commits into from
Closed

Use hierarchical namespaces in name resolution #1364

wants to merge 95 commits into from

Conversation

sezna
Copy link
Contributor

@sezna sezna commented Apr 6, 2024

Note: I see the performance degradation in the memory and performance benchmarks, and am looking into that

This PR introduces a feature called hierarchical namespaces, a module-like system that maintains backward compatibility. It is the foundational groundwork that will pave the way for:

  1. Naked namespaces (files without namespace declarations)
  2. Package publishing and sharing
  3. Explicit item imports
  4. Exports and re-exports
  5. and the rest of the stuff here

Key changes include:

  1. VecIdent type introduction: This abstraction over Vec<Ident> represents the new namespaces in the AST. Alternate names could be DotIdent or Path, but those names were already loaded with meaning in our codebase. I'm open to feedback on nomenclature here.
  2. NamespaceId usage: This replaces our previous flat map-style Rc<str> -> Res item tracking.
  3. NamespaceTree: This tracks the namespace hierarchy separately in low-cost data structure, and allows us to keep our basic tys and terms maps relatively untouched.
  4. Name resolution algorithm update: The algorithm in resolve.rs received a heavy facelift. Not only did I try to simplify the existing code, but I also added the required functionality to check candidate partially-opened or re-opened namespaces.

This PR maintains 100% backward compatibility with the existing resolution engine, preserving features like re-opening namespaces, shadowing semantics, merging namespaces via aliases, and absolute-path opens. It also introduces the ability to reference a function in a different namespace if its parent namespace is opened.

Additional notes:

  1. Katas used a form of shadowing that wasn't tested in our compiler. This PR adds a test for that.
  2. Pursuing Use debug printing for expect_test in the parser and other places. #1321 could help differentiate user-facing and compiler-engineer-facing string representations of structs, which are increasingly conflated.
  3. There's potential for refactoring ast, fir, and hir to use shared generic types, reducing duplicate Ident/etc types.
  4. Completions functionality remains the same, but it's not yet aware of the ability to partially open namespaces (e.g. open Microsoft.Quantum. This will be addressed in future updates.

Also closes #1336

Copy link

Change in memory usage detected by benchmark.

Memory Report for 7ee63da

Test This Branch On Main Difference
compile core + standard lib 16678037 bytes 15969785 bytes 708252 bytes

Copy link

Benchmark for 7ee63da

Click to view benchmark
Test Base PR %
Array append evaluation 340.6±2.32µs 340.4±3.75µs -0.06%
Array literal evaluation 206.3±3.42µs 194.9±3.53µs -5.53%
Array update evaluation 426.7±2.34µs 424.4±1.93µs -0.54%
Core + Standard library compilation 17.2±0.52ms 21.6±1.31ms +25.58%
Deutsch-Jozsa evaluation 5.0±0.04ms 5.0±0.15ms 0.00%
Large file parity evaluation 33.6±0.28ms 33.7±0.73ms +0.30%
Large input file compilation 11.5±0.27ms 13.6±0.38ms +18.26%
Large input file compilation (interpreter) 46.1±1.62ms 52.7±3.10ms +14.32%
Large nested iteration 33.3±0.59ms 33.3±0.16ms 0.00%
Perform Runtime Capabilities Analysis (RCA) on Deutsch-Jozsa sample 1507.5±55.75µs 1491.4±31.45µs -1.07%
Perform Runtime Capabilities Analysis (RCA) on large file sample 7.9±0.15ms 7.7±0.09ms -2.53%
Perform Runtime Capabilities Analysis (RCA) on teleport sample 1419.6±77.27µs 1406.5±34.38µs -0.92%
Perform Runtime Capabilities Analysis (RCA) on the core and std libraries 21.2±0.21ms 21.3±0.62ms +0.47%
Teleport evaluation 81.0±3.99µs 78.7±3.71µs -2.84%

Copy link

Change in memory usage detected by benchmark.

Memory Report for 96435a5

Test This Branch On Main Difference
compile core + standard lib 16680437 bytes 15969785 bytes 710652 bytes

Copy link

Benchmark for 96435a5

Click to view benchmark
Test Base PR %
Array append evaluation 346.3±6.45µs 351.1±18.69µs +1.39%
Array literal evaluation 199.2±1.70µs 192.8±1.11µs -3.21%
Array update evaluation 432.0±7.60µs 438.1±7.95µs +1.41%
Core + Standard library compilation 19.5±1.05ms 97.4±0.99ms +399.49%
Deutsch-Jozsa evaluation 5.0±0.07ms 5.2±0.09ms +4.00%
Large file parity evaluation 33.8±0.21ms 34.3±0.11ms +1.48%
Large input file compilation 13.8±0.36ms 30.7±0.63ms +122.46%
Large input file compilation (interpreter) 51.2±2.26ms 147.2±4.06ms +187.50%
Large nested iteration 33.3±0.37ms 34.3±0.29ms +3.00%
Perform Runtime Capabilities Analysis (RCA) on Deutsch-Jozsa sample 1565.3±185.38µs 1507.5±42.56µs -3.69%
Perform Runtime Capabilities Analysis (RCA) on large file sample 7.8±0.12ms 7.8±0.11ms 0.00%
Perform Runtime Capabilities Analysis (RCA) on teleport sample 1473.3±161.65µs 1424.1±51.35µs -3.34%
Perform Runtime Capabilities Analysis (RCA) on the core and std libraries 22.4±0.29ms 21.6±0.26ms -3.57%
Teleport evaluation 80.6±4.19µs 83.0±4.23µs +2.98%

Copy link

Change in memory usage detected by benchmark.

Memory Report for bb286cf

Test This Branch On Main Difference
compile core + standard lib 16680437 bytes 15969785 bytes 710652 bytes

Copy link

Benchmark for bb286cf

Click to view benchmark
Test Base PR %
Array append evaluation 335.6±2.29µs 336.9±7.57µs +0.39%
Array literal evaluation 180.7±1.68µs 183.4±6.25µs +1.49%
Array update evaluation 420.5±7.16µs 418.9±3.09µs -0.38%
Core + Standard library compilation 17.0±0.29ms 18.2±0.12ms +7.06%
Deutsch-Jozsa evaluation 5.0±0.15ms 5.0±0.04ms 0.00%
Large file parity evaluation 33.6±0.32ms 34.0±0.43ms +1.19%
Large input file compilation 11.6±0.28ms 11.8±0.09ms +1.72%
Large input file compilation (interpreter) 45.4±1.84ms 46.1±1.62ms +1.54%
Large nested iteration 32.7±0.16ms 32.8±0.36ms +0.31%
Perform Runtime Capabilities Analysis (RCA) on Deutsch-Jozsa sample 1499.9±39.25µs 1493.2±40.28µs -0.45%
Perform Runtime Capabilities Analysis (RCA) on large file sample 7.8±0.27ms 7.6±0.08ms -2.56%
Perform Runtime Capabilities Analysis (RCA) on teleport sample 1420.0±75.29µs 1402.3±29.25µs -1.25%
Perform Runtime Capabilities Analysis (RCA) on the core and std libraries 21.5±0.26ms 21.5±0.19ms 0.00%
Teleport evaluation 81.2±4.35µs 81.0±6.63µs -0.25%

Copy link

Change in memory usage detected by benchmark.

Memory Report for f49b284

Test This Branch On Main Difference
compile core + standard lib 16680437 bytes 15969785 bytes 710652 bytes

Copy link

Change in memory usage detected by benchmark.

Memory Report for ae43fef

Test This Branch On Main Difference
compile core + standard lib 16680437 bytes 15969785 bytes 710652 bytes

Copy link

Benchmark for ae43fef

Click to view benchmark
Test Base PR %
Array append evaluation 342.7±11.82µs 341.3±11.54µs -0.41%
Array literal evaluation 180.1±1.13µs 176.5±1.99µs -2.00%
Array update evaluation 433.5±5.50µs 423.5±7.30µs -2.31%
Core + Standard library compilation 19.2±1.09ms 20.9±1.02ms +8.85%
Deutsch-Jozsa evaluation 5.0±0.03ms 5.0±0.05ms 0.00%
Large file parity evaluation 33.7±0.14ms 34.3±0.27ms +1.78%
Large input file compilation 12.6±0.74ms 13.5±0.67ms +7.14%
Large input file compilation (interpreter) 49.5±1.86ms 52.7±2.98ms +6.46%
Large nested iteration 33.6±0.27ms 33.1±0.31ms -1.49%
Perform Runtime Capabilities Analysis (RCA) on Deutsch-Jozsa sample 1531.9±83.24µs 1538.8±135.75µs +0.45%
Perform Runtime Capabilities Analysis (RCA) on large file sample 8.2±0.18ms 7.9±0.16ms -3.66%
Perform Runtime Capabilities Analysis (RCA) on teleport sample 1440.6±94.90µs 1446.1±72.60µs +0.38%
Perform Runtime Capabilities Analysis (RCA) on the core and std libraries 22.3±0.58ms 21.9±0.34ms -1.79%
Teleport evaluation 82.1±4.30µs 81.6±3.34µs -0.61%

@microsoft microsoft deleted a comment from github-actions bot Apr 23, 2024
@microsoft microsoft deleted a comment from github-actions bot Apr 23, 2024
@microsoft microsoft deleted a comment from github-actions bot Apr 23, 2024
@microsoft microsoft deleted a comment from github-actions bot Apr 23, 2024
@microsoft microsoft deleted a comment from github-actions bot Apr 23, 2024
@microsoft microsoft deleted a comment from github-actions bot Apr 23, 2024
@sezna sezna closed this Apr 23, 2024
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.

Language Service no longer needs to auto-add open Microsoft.Quantum.Measurement;
2 participants