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

Fix importing of generic types. #4196

Merged

Conversation

zygoloid
Copy link
Contributor

@zygoloid zygoloid commented Aug 7, 2024

Ensure we don't lose the symbolic constant value by mapping through to the underlying inst ID.

Ensure we don't lose the symbolic constant value by mapping through to
the underlying inst ID.
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.

LG, though might want to double-check test cases

Comment on lines 672 to 673
auto bind_const_id =
import_ir_.insts().Is<SemIR::BindSymbolicName>(bind_id)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'll note, this looks slightly redundant with the above TryGetAs. If the original bind_id isn't an AddrPattern, it'll fetch the same inst twice. I think that's the common case, too.

You might consider something like:

auto bind_inst = import_ir_.insts().Get(bind_id);
if (auto addr = bind_inst.TryAs<SemIR::AddrPattern>()) {
  bind_id = addr->inner_id;
  bind_inst = import_ir_.insts().Get(bind_id);
}
auto bind_const_id = bind_inst.Is<SemIR::BindSymbolicName>()

Although maybe this will be in the noise...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

I was curious whether this would make any difference, so I took a look at the assembly. Looks like insts().Get is generally not getting inlined (probably because the CHECK makes it quite large), so my conclusion is... yes, this probably helps, but I'm sad that it's needed. Maybe we should use a DCHECK in ValueStore::Get?

Comment on lines 742 to 743
context_.constant_values().GetInstId(
param_data.bind_const_id));
Copy link
Contributor

Choose a reason for hiding this comment

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

Can bind_const_id ever point at an error? i.e., somewhat concerned that this might fail on an import of an interface with an invalid parameter. How tested are invalid generic imports?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we get here then in the imported IR we had a BindSymbolicName, and the bind_const_id is the result of importing that BindSymbolicName, which can't produce an error. We only support a few very specific instruction patterns here, and hit a fatal error a few lines below if we see anything not matching those patterns (unchanged in this PR).

To your broader point, I don't think we have good checking for imports of invalid generics. Is that something we're generally testing and expecting to work currently? I can see tooling use cases where it'd be important, but it's not something I've really been thinking about.

Copy link
Contributor

Choose a reason for hiding this comment

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

The important thing to me is really just that check shouldn't crash. Imports of invalid generics shouldn't cause a crash. I've added tests for some various invalid import cases. I don't think you've added tests for that as part of adding generics.

Looks like this doesn't get optimized away in practice.
@zygoloid zygoloid enabled auto-merge August 7, 2024 19:06
@zygoloid zygoloid added this pull request to the merge queue Aug 7, 2024
Merged via the queue into carbon-language:trunk with commit 91f56f7 Aug 7, 2024
7 checks passed
@zygoloid zygoloid deleted the toolchain-generic-type-import branch August 7, 2024 19:19
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