Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

[WIP] Add more documentation for entities #1041

Merged
merged 9 commits into from
Sep 18, 2019

Conversation

jyn514
Copy link
Contributor

@jyn514 jyn514 commented Sep 17, 2019

Addresses #1038

Document stackslots, funcrefs, heaps, sigrefs, and jumptables.
Also adds more inter-crate links.

Undocumented

Issues

I can't find a way to link to a type/function in a different crate if that crate isn't currently in scope for the code. For example, the documentation for JumpTable links to FunctionBuilder::create_jump_table, but the link is broken (it points to cranelift_codegen/ir/entities/cranelift_frontend::FunctionBuilder::create_jump_table, which is clearly wrong).

There are two ways I can think to resolve this:

  1. Link directly to docs.rs like this: https://docs.rs/cranelift-module/*/cranelift_module/struct.Module.html. This has the downside that it will always point to the latest version (and won't work locally or offline).
  2. Put the name of the type in prose and have users search for it. This is not ideal but will work the same everywhere.

Addresses bytecodealliance#1038

Document stackslots, funcrefs, heaps, sigrefs, and jumptables
@abrown
Copy link
Collaborator

abrown commented Sep 17, 2019

@jyn514 I can explain Constant: it is an integer used to index into the ConstantPool to identify constant values held there (see https://github.com/CraneStation/cranelift/blob/dcaa7f5ba618d72c9dd24a29c9a485dc24ab0d07/cranelift-codegen/src/ir/constant.rs#L97).

@jyn514
Copy link
Contributor Author

jyn514 commented Sep 17, 2019

I see, and it's public because cranelift-frontend needs to access it?

@abrown
Copy link
Collaborator

abrown commented Sep 17, 2019

I can't find a way to link to a type/function in a different crate if that crate isn't currently in scope for the code

Also, I believe I had similar issues trying to fix some documentation warnings; in the end I went with something like crate::ir::... and that seemed to work: #952. The trick is to use correct Rust paths (see links in https://stackoverflow.com/a/53504254 and rust-lang/rust#43466). I think that getting the Rust paths to work is better long-term than direct links or prose.

@jyn514
Copy link
Contributor Author

jyn514 commented Sep 17, 2019

in the end I went with something like crate::ir::...

That only works if the crate is imported into the current scope, though. I can't put extern crate cranelift_module; in the code because that breaks the code of anyone who uses cranelift for WASM.

@abrown
Copy link
Collaborator

abrown commented Sep 17, 2019

I see, and it's public because cranelift-frontend needs to access it?

Now that I look at its usages, I don't see it anywhere except in codegen so it might be able to be downgraded to pub(crate). The idea, though, is that it is part of a public API for interacting with the constant pool.

cranelift-codegen/src/ir/entities.rs Outdated Show resolved Hide resolved
cranelift-codegen/src/ir/entities.rs Outdated Show resolved Hide resolved
cranelift-codegen/src/ir/entities.rs Outdated Show resolved Hide resolved
/// [`import_signature`](cranelift_frontend::FunctionBuilder::import_signature).
///
/// You can retrieve the [`Signature`](super::Signature) that was used to create a `SigRef` with
/// [`signature`](cranelift_frontend::FunctionBuilder::signature).
Copy link
Contributor

Choose a reason for hiding this comment

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

func.dfg.signatures

@abrown
Copy link
Collaborator

abrown commented Sep 17, 2019

Maybe rust-lang/rust#43466 (comment) is fixed somewhere? Not exactly sure...

@abrown
Copy link
Collaborator

abrown commented Sep 17, 2019

Maybe rust-lang/rust#43466 (comment) is fixed somewhere? Not exactly sure...

But if there is absolutely no way to link to external crates then I guess my vote would be for the direct link.

- pointer -> reference
- mention `stack_store`
- mention `Switch` as an alternative to `br_table`
- mention `func.dfg.signatures`
can't have two periods in a sentence
@abrown
Copy link
Collaborator

abrown commented Sep 17, 2019

@jyn514, for Inst you might want to look at how it is used in DataFlowGraph.

@bnjbvr
Copy link
Member

bnjbvr commented Sep 18, 2019

Thanks for taking a stab at this!

Note that all of these implement EntityRef, so they're all handles to some data living somewhere else. For instance Constant has ConstantData, etc. I remember proposing to constantly suffix them with "Ref" or something, but this was a long time ago and I'm unsure what the outcome of the discussion was.

  • GlobalValue can be mapped to a wasm global value, but doesn't need to; it is "just" a value that may be live across the entire function lifetime, in my understanding, and that can be preloaded from other global values. (Useful when you have a VmContext, and want to load constant things from it, for instance)
  • Table is used for wasm function tables at the moment. I don't know if it's planned to use it for other table kinds in the future, could be. Useful for implementing indirect calls to known functions.
  • Inst is the handle for an instruction. I imagine most usage is internal, and I wonder if this could be made not public.

@bjorn3
Copy link
Contributor

bjorn3 commented Sep 18, 2019

Inst is the handle for an instruction. I imagine most usage is internal, and I wonder if this could be made not public.

I do use it to attach a comment to a specific instruction in the clif ir written by cg_clif.

@jyn514
Copy link
Contributor Author

jyn514 commented Sep 18, 2019

Getting slightly off-topic now - is there any way that pretty_clif.rs could be made part of the main Cranelift API? I would love to reuse that code without having to copy/paste most of the implementation.

It looks like the only rustc-specific code is CommentWriter::new, which uses an Instance, let me know if I'm wrong about that.

@jyn514
Copy link
Contributor Author

jyn514 commented Sep 18, 2019

Useful when you have a VmContext, and want to load constant things from it

Is this a WASM thing? It mentions a sandbox in the documentation, and I don't see a way to construct it from cranelift_frontend or cranelift_codegen

@bjorn3
Copy link
Contributor

bjorn3 commented Sep 18, 2019

is there any way that pretty_clif.rs could be made part of the main Cranelift API?

#379 (comment)

I would love to reuse that code without having to copy/paste most of the implementation.

Please note that currently comments get inserted after the specified inst, as there is no way to know what the next inst id will be.

@jyn514
Copy link
Contributor Author

jyn514 commented Sep 18, 2019

This is ready for review as soon as I fix the broken links. @abrown recommended permalinks to docs.rs, should I do that?

@abrown
Copy link
Collaborator

abrown commented Sep 18, 2019

@jyn514 I'm fine merging this but we need to fix the build issues: https://dev.azure.com/CraneStation/Cranelift/_build/results?buildId=897&view=logs.

@jyn514
Copy link
Contributor Author

jyn514 commented Sep 18, 2019

Ok, I'll change it to link directly to docs.rs, then.

See
bytecodealliance#1041 (comment),
there doesn't currently seem to be a way to link to a type in a
dependent crate.
@jyn514
Copy link
Contributor Author

jyn514 commented Sep 18, 2019

This is ready for review.

@abrown abrown merged commit d46311c into bytecodealliance:master Sep 18, 2019
@abrown
Copy link
Collaborator

abrown commented Sep 18, 2019

@jyn514 thanks for the fix!

@jyn514 jyn514 deleted the doc-entity branch September 18, 2019 23:54
arkpar pushed a commit to paritytech/wasmtime that referenced this pull request Mar 4, 2020
…ecodealliance#1041)

Also fixes broken links to point to docs.rs; see
bytecodealliance/cranelift#1041 (comment),
there doesn't currently seem to be a way to link to a type in a
dependent crate.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants