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

Verify instruction encodings #74

Merged
merged 4 commits into from Apr 24, 2017

Conversation

anholt
Copy link
Contributor

@anholt anholt commented Apr 23, 2017

No description provided.

@anholt
Copy link
Contributor Author

anholt commented Apr 23, 2017

Here we go. The first patch is the one that really matters in this commit. Second is just a little fix I found along the way.

The last two are totally optional, so feel free to drop them -- it did seem suspicious that we could exit legalize() without encodings[] for all instructions (later passes seem to dereference encodings[] without any protection), but we're probably guaranteed to not do anything else if legalization failed.

@stoklund
Copy link
Contributor

Thanks! I'll review this shortly.

The design around unencoded instructions isn't completely crystallized yet. I think it is probably too strict to require every instruction to have an encoding — it would require the legalizer to clean up after itself. Instead, I expect we'll allow unencoded instructions to linger as a kind of 'ghost instructions'. The requirement will be that a proper encoded instruction is not allowed to depend on the result of an unencoded instruction, and unencoded instructions can't have side effects.

I expect that unencoded instructions will be used to inform metadata like DWARF expressions or bailout information for a JIT compiler. It's for cases where metadata needs to describe how to compute values that have been optimized out.

Copy link
Contributor

@stoklund stoklund left a comment

Choose a reason for hiding this comment

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

This looks great with two minor changes.

@@ -54,7 +54,7 @@ impl Context {
/// The `TargetIsa` argument is currently unused, but the verifier will soon be able to also
/// check ISA-dependent constraints.
pub fn verify<'a, ISA: Into<Option<&'a TargetIsa>>>(&self, _isa: ISA) -> verifier::Result {
verifier::verify_context(&self.func, &self.cfg, &self.domtree)
verifier::verify_context(&self.func, &self.cfg, &self.domtree, _isa.into())
Copy link
Contributor

Choose a reason for hiding this comment

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

The leading underscore on _isa serves to silence Rust's dead code warning. I remove them when I start using an argument.

@@ -91,6 +91,7 @@ pub fn legalize_function(func: &mut Function, cfg: &mut ControlFlowGraph, isa: &
// unsound. Should we attempt to detect that?
if changed {
pos.set_position(prev_pos);
continue;
Copy link
Contributor

Choose a reason for hiding this comment

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

Yep. Nice catch.

@@ -92,6 +92,8 @@ pub fn legalize_function(func: &mut Function, cfg: &mut ControlFlowGraph, isa: &
if changed {
pos.set_position(prev_pos);
continue;
} else {
func.encodings.ensure(inst);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a good invariant to enforce. I wonder if it is simpler and safer to call encodings.resize(dfg.num_insts()) after the legalizer pass, though?

The other legalizer cases have a continue after setting the position
to double back, while this one didn't.  Make sure that we do, in case
another legalizer block is added after this one.
…ize().

If we generated new instructions as part of legalize, and the new
instructions failed to legalize, we'd be left with a func.encodings[]
that would panic when you dereferenced the inst.
@anholt
Copy link
Contributor Author

anholt commented Apr 23, 2017

I like both those changes. Done.

@stoklund stoklund merged commit a332c3d into bytecodealliance:master Apr 24, 2017
@stoklund
Copy link
Contributor

Awesome. Thanks!

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.

None yet

2 participants