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

Bring emit-h back to life! #20353

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

SuperAuguste
Copy link
Sponsor Contributor

@SuperAuguste SuperAuguste commented Jun 20, 2024

See the current results here: https://zigbin.io/a90812

Some things I still need to do:

  • Use export names rather than the names of the exported decls (can be different w/ @export for example)
  • Give structs better generated names(?) Decided to keep simple, not fully-qualified names for now. Can definitely cause issues, but this requires a larger rethinking potentially that should be in a proposal issue.
  • Figure out why ZIR indices on structs, unions, and enums seem to disappear (there's probably a good reason for this that I'm not aware of, or a bug)
  • Add parent_fix from CBE logic I stole back to remove unnecessary parens
  • Extract param names from functions
  • Better styling for output code (a little cramped? maybe group functions together?)
  • Probably add a couple tests Not blocking for merge, can be added later.

But it does work 100% better than the current emit-h! :P

@mlugg
Copy link
Member

mlugg commented Jun 20, 2024

Figure out why ZIR indices on structs, unions, and enums seem to disappear

If you're referring to the zir_decl_index field on Decl, this is by design -- the doc comment explains what's going on. Unfortunately, this code still overuses Decl -- this type currently has a lot of responsibilities, and I'm actively working on splitting it into some more atomic types. However, that need not block this PR -- from a quick glance, this work is a good baseline which I can build on as a part of my work on Decl.

Copy link
Member

@mlugg mlugg left a comment

Choose a reason for hiding this comment

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

There are several TODOs you've noted, and a few more potential issues I've flagged up. However, this change overall seems pretty good. Since emit-h is completely broken today, I think this PR is reasonable to merge as-is to unblock at least some use cases (such as your own!).

Upon merge, I will open a follow-up issue detailing the remaining issues with emit-h. I will likely end up solving some of them in my upcoming Decl refactor, which is another reason I think this should be merged as-is.


std.debug.assert(emit_h.allocated_emit_h.len == emit_h.decl_table.count());

var emitter = EmitH{
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
var emitter = EmitH{
var emitter: EmitH = .{

src/Sema.zig Outdated
@@ -6572,6 +6572,7 @@ fn addExport(mod: *Module, export_init: Module.Export) error{OutOfMemory}!void {
const de_gop = mod.decl_exports.getOrPutAssumeCapacity(decl_index);
if (!de_gop.found_existing) de_gop.value_ptr.* = .{};
try de_gop.value_ptr.append(gpa, new_export);
try mod.emitHDecl(decl_index);
},
.value => |value| {
Copy link
Member

@mlugg mlugg Jun 20, 2024

Choose a reason for hiding this comment

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

We also need to handle this value case -- this corresponds to an @export of a comptime-known value. That doesn't need to block this merge.

@@ -0,0 +1,731 @@
const std = @import("std");
Copy link
Member

Choose a reason for hiding this comment

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

This file's naming is a little confusing due to the existence of Zcu.EmitH, but that need not block merge. Zcu.EmitH will need to be restructured soon anyway to deal with serialization.

}
}

if (zcu.decl_exports.get(emitter.decl_index)) |exports| {
Copy link
Member

Choose a reason for hiding this comment

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

There's a subtly weird case here: if a decl foo is @exported by two different decls bar and qux, and bar's emit_h_decl job is run before qux is analyzed, then we'll miss the export from qux.

Now that I see this issue, I think I understand why the old implementation queued this work in Zcu.semaDecl: presumably, it was (at least trying to) use export_owners to deal with the exports created by the just-analyzed Decl. However, I don't think that ought to block this PR, because using Decl in this way is currently a little borked anyway (something my upcoming refactors will improve).

.Struct => {
const should_make_opaque = switch (value_as_type.containerLayout(zcu)) {
.@"extern" => false,
.@"packed" => switch (value_as_type.bitSizeAdvanced(zcu, null) catch unreachable) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
.@"packed" => switch (value_as_type.bitSizeAdvanced(zcu, null) catch unreachable) {
.@"packed" => switch (value_as_type.bitSize(zcu)) {

@jacobly0
Copy link
Member

jacobly0 commented Jun 21, 2024

Suggested future work:

  1. I'm pretty sure emit-h should not be using zig.h (and as has been pointed out, the type emulation code performed by zig.h is not appropriate for emit-h at all), the amount of support code actually required by emit-h is very minimal and the parts that are actually used can be conditionally written to the top of the file.
  2. There's no chance that type decls are being emitted in the correct dependency order without a type pool. The emit-h-specific type transformations should be a separate mode when adding to the pool, not special-cased here.
  3. Why is this using a generic (and non-stable!) sort to perform a bucket sort, preferably just keep them in separate data structures per section to begin with.

@SuperAuguste
Copy link
Sponsor Contributor Author

Thanks for the feedback folks. I will try to address everything today.

@SuperAuguste SuperAuguste marked this pull request as draft June 21, 2024 13:52
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