Skip to content

Add const expression utility to binaryen-c/.js - #1288

Merged
kripken merged 5 commits into
WebAssembly:masterfrom
dcodeIO:js-const-utility
Nov 15, 2017
Merged

kripken merged 5 commits into
WebAssembly:masterfrom
dcodeIO:js-const-utility

Conversation

@dcodeIO

@dcodeIO dcodeIO commented Nov 14, 2017 •

Copy link
Copy Markdown
Contributor

Given an i32/i64/f32/f64.const expression reference, these functions return the expression's literal value. One use case is precomputing an expression (i.e. through running the precompute pass) and then checking the precomputed value.

@kripken kripken left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice!

A few minor comments below. Also needs a test.

Comment thread src/binaryen-c.h Outdated
int32_t BinaryenConstGetValueI32(BinaryenExpressionRef expr);
// Gets the 64-bit integer value of the specified known-to-be-constant expression.
int64_t BinaryenConstGetValueI64(BinaryenExpressionRef expr);
// Gets the low 32-bits of a 64-bit integer value of the specified known-to-be-constant expression.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

i guess that low/high variants are for calling from JS? worth mentioning that i think.

Comment thread src/binaryen-c.h Outdated
BinaryenType BinaryenExpressionGetType(BinaryenExpressionRef expr);
// Print an expression to stdout. Useful for debugging.
void BinaryenExpressionPrint(BinaryenExpressionRef expr);
// Gets the 32-bit integer value of the specified known-to-be-constant expression.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

"known-to-be-constant" seems to use "constant" as a general property - we should say that the expression passed in must be a captital-C Const node.

@dcodeIO

dcodeIO commented Nov 14, 2017

Copy link
Copy Markdown
Contributor Author

Still thinking how to implement a BinaryenExpressionPrecompute function by reusing the precompute pass. That would be super useful.

@kripken

kripken commented Nov 15, 2017

Copy link
Copy Markdown
Member

Why not just run the precompute pass on the whole module? Is there a reason to do it on individual expressions?

@kripken
kripken merged commit dc0cd44 into WebAssembly:master Nov 15, 2017
@dcodeIO

dcodeIO commented Nov 15, 2017 •

Copy link
Copy Markdown
Contributor Author

Yeah, the reason is that the MVP does not allow more complex (for example binary) expressions as initializers of, let's say, globals and I am trying to avoid reimplementing precompute in my compiler. Hence, if I have a const global with an initializer like 1 << 2, I can't use it as is, but must instead make the global mutable and initialize it in the start function.

Being able to precompute just the initializer would solve this, while not affecting the entire module and not having to reinvent the wheel.

@kripken

kripken commented Nov 15, 2017

Copy link
Copy Markdown
Member

I see. One way could be, in JS, to create a module (empty except you would want the global imports), add just that expression in a function, and run the pass on that, then get that function body. We should make doing stuff like that easy anyhow from JS, I think, so this could be a good way to verify that.

@dcodeIO

dcodeIO commented Nov 15, 2017

Copy link
Copy Markdown
Contributor Author

Sure, that would be one option. This will, however, not cover things like globalB = 1 + globalA.

@kripken

kripken commented Nov 16, 2017

Copy link
Copy Markdown
Member

I don't see why not, can you elaborate? (Or do you mean you'd need to loop, continuing to work while something is reduced? Yes, that would be necessary.)

@dcodeIO

dcodeIO commented Nov 16, 2017 •

Copy link
Copy Markdown
Contributor Author

Well, because globalA then doesn't exist in the empty module (edit: unless I add all of them, as you noted). Got something working now, though, will create a PR soon. Simply reuses precomputeValue of Precompute.

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.

2 participants