Skip to content

Conversation

@mobsceneZ
Copy link
Contributor

@mobsceneZ mobsceneZ commented Jan 9, 2024

Refer to issue #6214, this pull request is trying to allow Binaryen C API to append new local variables to the function (in cases that there does not exist such C API yet).

Copy link
Member

@kripken kripken left a comment

Choose a reason for hiding this comment

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

Looks like a reasonable API to add.

See comments, + also add a tiny test somewhere in test/example/c-api-kitchen-sink.c.

src/binaryen-c.h Outdated
BINARYEN_API BinaryenType BinaryenFunctionGetVar(BinaryenFunctionRef func,
BinaryenIndex index);
// Appends a local variable to the specified `Function`, returning its
// insertion index.
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
// insertion index.
// index.

auto* function = (Function*)func;
auto& list = function->vars;
auto index = list.size();
list.push_back((Type)type);
Copy link
Member

Choose a reason for hiding this comment

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

This can use Builder::addVar.

For consistency with that API, perhaps this could be BinaryenFunctionAddVar?

@mobsceneZ
Copy link
Contributor Author

Looks like a reasonable API to add.

See comments, + also add a tiny test somewhere in test/example/c-api-kitchen-sink.c.

Hey kripken, according to your suggestions I made corresponding changes which can be seen in commit 7e968bd. You can check that out to see if it's what you want ;).

Comment on lines 5883 to 5884
auto* function = (Function*)func;
return Builder::addVar(function, (Type)type);
Copy link
Member

@kripken kripken Jan 11, 2024

Choose a reason for hiding this comment

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

Suggested change
auto* function = (Function*)func;
return Builder::addVar(function, (Type)type);
return Builder::addVar((Function*)func, (Type)type);

Seems like doing both casts on that line still keeps it reasonably short.

BinaryenIndex newLocalIdx = BinaryenFunctionAddVar(sinker, BinaryenTypeFloat32());
assert(newLocalIdx == numLocals);
assert(BinaryenFunctionGetVar(sinker, newLocalIdx - numParams) == BinaryenTypeFloat32());

Copy link
Member

Choose a reason for hiding this comment

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

We can also check the number of vars was incremented properly.

Suggested change
assert(BinaryenFunctionGetNumLocals(sinker) == numLocals + 1);

@kripken
Copy link
Member

kripken commented Jan 11, 2024

See also the error on CI about some small lint changes that are required.

@mobsceneZ
Copy link
Contributor Author

See also the error on CI about some small lint changes that are required.

Hey Kripken, I made corresponding changes in commit b2a69dd (it's a rewrite of the last commit), you can check that out to see if that has further problems.

@kripken kripken merged commit 823bf60 into WebAssembly:main Jan 17, 2024
radekdoulik pushed a commit to dotnet/binaryen that referenced this pull request Jul 12, 2024
@gkdn gkdn mentioned this pull request Aug 31, 2024
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