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

feat(compiler): Refactor exports #1244

Merged
merged 4 commits into from
May 27, 2022
Merged

feat(compiler): Refactor exports #1244

merged 4 commits into from
May 27, 2022

Conversation

ospencer
Copy link
Member

This PR refactors how exports are compiled in Grain. Namely,

  • Exports are now fully signature-driven. When determining what to export from the module, we iterate the module signature (rather than collecting things to export along the way).
  • A scoping bug that put exported names in scope is now resolved. For example,
let foo = 5
export foo as bar
print(bar)

will now properly result in a type error, whereas previously bar was unexpectedly in scope.

  • Re-exports from one module to another now incur no runtime cost whatsoever.

Because of these changes, the hello world module size is reduced again by 25%, coming in under 39k.

@ospencer ospencer requested a review from a team May 22, 2022 03:27
@ospencer ospencer self-assigned this May 22, 2022
Copy link
Member

@phated phated left a comment

Choose a reason for hiding this comment

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

This PR is 🔥 - I had a few questions/thoughts

compiler/src/codegen/compcore.re Outdated Show resolved Hide resolved
compiler/src/codegen/compcore.re Outdated Show resolved Hide resolved
compiler/src/codegen/compcore.re Show resolved Hide resolved
compiler/src/codegen/transl_anf.re Show resolved Hide resolved
compiler/src/codegen/transl_anf.re Outdated Show resolved Hide resolved
compiler/src/codegen/transl_anf.re Outdated Show resolved Hide resolved
@@ -14,21 +17,61 @@ let safe_to_remove_import = i =>
| _ => false
};

let is_used = ident =>
Option.is_some(Ident.find_same_opt(ident, used_symbols^));
Copy link
Member

Choose a reason for hiding this comment

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

We should add some utilities to the Ident module (like contains). Probably not needed in this PR.

compiler/src/typed/types.re Show resolved Hide resolved
Base automatically changed from oscar/fix-reading-custom-sections to main May 22, 2022 22:09
Copy link
Member

@phated phated left a comment

Choose a reason for hiding this comment

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

Approved! 🎉

Copy link
Member

@peblair peblair left a comment

Choose a reason for hiding this comment

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

I mostly follow this, and it looks good to me. One very minor request for information, and then I think it's good to go.

| Path.PExternal(Path.PIdent(mod_) as p, ident, _) =>
let allocation_type = get_allocation_type(env, val_type);
let mod_decl = Env.find_module(p, None, env);
// Lookup external exports to import them into the module
Copy link
Member

Choose a reason for hiding this comment

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

I'm a little confused what this is doing. Can you clarify a little more?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah! So the way imports actually make it into the WebAssembly module is via lookup_symbol registering the individual values and functions to include them in the module. Previously doing export foo was sugar for export let foo = foo, making foo get used and thus be included as an import, but now an export statement skips the extra binding. The lookup here is effectively us saying that we plan to use that value so it actually gets imported.

Copy link
Member

@marcusroberts marcusroberts left a comment

Choose a reason for hiding this comment

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

I'm going to give this a LGTM from my skim read of what's going on.

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.

4 participants