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

mir: improve set construction syntax #1264

Merged
merged 8 commits into from
Apr 2, 2024

Conversation

zerbina
Copy link
Collaborator

@zerbina zerbina commented Apr 1, 2024

Summary

Introduce a dedicated node representation for set constructions in the
MIR, making their data layout more terse and fixing a set construction
like {a .. b} crashing the compiler when a or b are non-constant
expressions.

Details

  • add the mnkSetConstr and mnkRange node kinds
    • since sets are not aggregate types, wrapping the operands of
      the construction in argument nodes is unnecessary
    • mnkSetConstr is pretty-printed as {...} instead of the generic
      construct (...)
  • don't treat nkRange as literal data
  • translate nkRange to mnkRange for both branch labels and in set
    constructions
    • dynamic ranges are now properly considered by mirgen
  • hashing/comparison for DataTable, constant expression
    serialization, and data-flow graph creation are adjusted to the new
    tree shapes
  • translate mnkSetConstr and mnkRange to cnkSetConstr and
    cnkRange in cgirgen
    • both set construction elements and branch labels use
      setElementToIr, even though this doesn't reject incorrect syntax
      for branch labels

In addition to fixing the aforementioned compiler crash, the dedicated
mnkSetConstr and mnkRange node kinds:

  • remove a difference between the MIR and CGIR
  • encode set construction semantics directly in the MIR, removing a
    source of having to dispatch over the nodes' type

zerbina added 8 commits April 1, 2024 16:49
Instead of the generic `mnkConstr`, set constructions now use
`mnkSetConstr`, encoding the semantics directly in the tree and not
requiring inspection of the type.

Sets, unlike the other types to which `mnkConstr` applies, is not an
aggregate type, so the extra `mnkArg` wrapping is unnecessary (the
operands are never consumed), saving space.

Finally, `mnkRange` is introduced for representing range constructions.
Previously, an `nkRange` was treated as a literal (`mnkLiteral`), which
is not always correct for set constructions, where the range
construction's operands can be dynamic!
* set constructions, in both constant and non-constant expression now
  use `mnkSetConstr` and `mnkRange`
* `nkRange` is no longer considered a literal (it never really was
  one)
* implement support for the new nodes
* ranges are rendered as `x .. y`
* set constructions are rendered as `{...}` instead of
  `construct (...)`
The new tree shapes need to be handled by all users of MIR constant
expressions:

* `DataTable` comparison and hashing
* constant-expression to `PackedEnv` serialization
* loading constant expression into VM memory
* MIR tree to CGIR translation

One benefit is that type inspection is no longer required for figuring
out that a construction is that of a set.
In the context of temporary-elimination, set constructions are safe:
the operands cannot ever overlap with the destination in memory.
For simplicity, and given that `cgirgen` is eventually going to be
removed anyway, both branch labels and set construction elements share
the same translation logic, even though the former only supports a
subset of the latter.
Using dynamic range (range syntax with run-time operands) in set
constructions crashed the compiler, due to `nkRange` always being
considered a literal value.
@zerbina zerbina added bug Something isn't working refactor Implementation refactor compiler/backend Related to backend system of the compiler labels Apr 1, 2024
@zerbina zerbina added this to the MIR phase milestone Apr 1, 2024
@saem
Copy link
Collaborator

saem commented Apr 2, 2024

/merge

Copy link

github-actions bot commented Apr 2, 2024

Merge requested by: @saem

Contents after the first section break of the PR description has been removed and preserved below:


Notes for Reviewers

  • yet more progress towards removing the differences between the MIR and CGIR
  • removing dispatching over types is going to make MIR processing easier once querying types requires access to the type environment

@chore-runner chore-runner bot added this pull request to the merge queue Apr 2, 2024
Merged via the queue into nim-works:devel with commit df9b988 Apr 2, 2024
31 checks passed
@zerbina zerbina deleted the mir-proper-range-handling branch April 8, 2024 20:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler/backend Related to backend system of the compiler refactor Implementation refactor
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants