Skip to content

Support for lowering references to imported vars. #5513

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

Open
wants to merge 11 commits into
base: trunk
Choose a base branch
from

Conversation

zygoloid
Copy link
Contributor

Previously we walked the global variables defined by the current file and emitted an LLVM global variable definition for each of them. Now instead, when emitting a constant reference to a global variable, we emit an LLVM global variable declaration, and we then subsequently walk the global variables defined by the current file and convert each of them from a declaration to a definition.

In order to make import of names of global variables work, add support for import of var, as well as support for importing tuple_access and tuple_pattern in the case where the var has a tuple pattern in its declaration. Also treat bind_names that are reference bindings to vars as having the same constant reference value as their var so that we can properly import and lower them.

zygoloid added 3 commits May 22, 2025 00:59
Emit a var declaration when lowering a constant reference to a `var`,
and when emitting the definition, convert that declaration into a
definition.

Model reference name bindings as being constants so that we can look
through them when importing and when lowering.
@github-actions github-actions bot requested a review from geoffromer May 22, 2025 01:05
Copy link
Contributor

@jonmeow jonmeow left a comment

Choose a reason for hiding this comment

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

Nice!

@@ -80,6 +80,10 @@ enum class InstConstantKind : int8_t {
// the constants block.
// TODO: Decide if this is the model we want for these cases.
Unique,
// Same as unique, except that the instruction may or may not be constant.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Same as unique, except that the instruction may or may not be constant.
// Similar to `Unique`, except that the instruction may or may not be constant.

It's different so not the same. ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For me, "Same as X except Y" is saying the only difference is Y, whereas "Similar to X except Y" leaves open the possibility that there are other differences too (it's mostly similar to X, but in case Y it's not even similar).

How about:

Suggested change
// Same as unique, except that the instruction may or may not be constant.
// The instruction may be a unique constant, as described for `Unique`.
// Otherwise the instruction is not constant.

Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW, mostly I wanted "unique' backticked, the "same" doesn't bother me that much.

Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW, maybe also Unique -> AlwaysUnique?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed.

Comment on lines 1242 to 1246
static_assert(
InstT::Kind.constant_kind() == SemIR::InstConstantKind::Unique ||
InstT::Kind.constant_kind() ==
SemIR::InstConstantKind::ConditionalUnique,
"Use ResolveAsDeduplicated");
Copy link
Contributor

Choose a reason for hiding this comment

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

Could this be a requires instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it could be, but we can't provide a custom error message so easily that way. Are you thinking that we give the two ResolveAs... functions the same name again? If we keep them separate, a diagnostic that mentions the other function name seems at least a little valuable.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe I'm just ambivalent about the ResolveAsDeduplicated, on two ends:

  • I think people will figure it out from a simple compile error, a comment seems equivalent (this isn't a high-use API).
  • I don't see why the name changed, when requires could have achieved equivalent enforcement. But, I didn't suggest/review it so I don't want to push too much.

I mainly noted it, though, since I've recently bumped into issues with static_assert and having to migrate things to requires. :)

Copy link
Contributor

Choose a reason for hiding this comment

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

But note, I also am hesitant to just churn code when I don't see a benefit. It's not like I think overloading would really fix an issue. I just am losing my love for static_assert when requires would suffice. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Switched to a concept.

zygoloid and others added 2 commits May 22, 2025 22:02
Co-authored-by: Jon Ross-Perkins <[email protected]>
@zygoloid zygoloid enabled auto-merge May 22, 2025 22:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants