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

simd: apply some fixes to test generation #686

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

Conversation

evacchi
Copy link
Collaborator

@evacchi evacchi commented Nov 25, 2024

This fixes some aspects of the SIMD test codegen, introducing some (debatable) wider changes, so I'm going to push this as a draft, to let you judge :) some of these changes could be also applied to a utility class, but this should be the gist anyway

  • I am now passing args through an MStack so that we can push V128s onto it: the reason is that codegen might become complicated otherwise: each v128 is a long[] so, you would have to "unpack" each long to several pushes. This way you can just push it all at once.
  • I have added an Mstack.pushAll(long[]) method, so that we can easily push vectors all at once.
  • I have added an MStack.slice(start, len) method returning a copy of the array (thus, only useful for tests), so that
    a. all places expecting a given arg size (e.g. I have seen this behavior in the new tail call feature) do not get confused by a larger input array
    b. we can pass in a number of values we want to pull; this is important when there are v128 values, since they are size=2
  • I have added a WasmValueType.size() that is 2 for v128 and 1 otherwise

I have also added a few more missing (?) lanes (i64x2, F64x2) and the reverse of vecTo32(), etc...

I will add some other notes inline.

@mfirry this should unblock you!

folks, feel free to push directly to this branch to apply your fixes on top

Comment on lines +193 to +194
sb.append("Integer.parseUnsignedInt(\"" + v + "\")");
break;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think we don't need to parse these into Float bits in this case, the raw integer will do


var invocationMethod = ".apply(" + args + ")";
var invocationMethod = ".apply(" + String.join(",", args) + ")";
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

notice, this is not correct, but works for now. It should be adapted to do same thing as the lines below, but it required a little more refactoring, and I wanted to push this out quickly to unblock @mfirry

Signed-off-by: Edoardo Vacchi <[email protected]>
Copy link

cloudflare-workers-and-pages bot commented Nov 25, 2024

Deploying chicory with  Cloudflare Pages  Cloudflare Pages

Latest commit: a4b4b0a
Status: ✅  Deploy successful!
Preview URL: https://c1bbb65c.chicory.pages.dev
Branch Preview URL: https://simd-testgen-fixes.chicory.pages.dev

View logs

@evacchi
Copy link
Collaborator Author

evacchi commented Nov 26, 2024

here's an example of a test (simd_bitwise) after the change:

    @Test()
    @Order(9)
    @DisplayName("simd_bitwise.wast:31")
    public void test9() {
        ExportFunction varAnd = testModule0Instance.export("and");
        var args = new MStack();
        args.pushAll(i32ToVec( new long[] { Long.parseUnsignedLong("0"), Long.parseUnsignedLong("0"), Long.parseUnsignedLong("4294967295"), Long.parseUnsignedLong("4294967295") }));
        args.pushAll(i32ToVec( new long[] { Long.parseUnsignedLong("0"), Long.parseUnsignedLong("4294967295"), Long.parseUnsignedLong("0"), Long.parseUnsignedLong("4294967295") }));
        var results = varAnd.apply(args.slice(0, 4));
        var expected = new long[] {Long.parseUnsignedLong("0"), Long.parseUnsignedLong("0"), Long.parseUnsignedLong("0"), Long.parseUnsignedLong("4294967295") };
        assertArrayEquals(expected, vecTo32(results));
    }

vs before:

    @Test()
    @Order(9)
    @DisplayName("simd_bitwise.wast:31")
    public void test9() {
        ExportFunction varAnd = testModule0Instance.export("and");
        var results = varAnd.apply(new long[] { 0, 0, 4294967295, 4294967295 }, new long[] { 0, 4294967295, 0, 4294967295 });
        var expected = new long[] {Long.parseUnsignedLong("0"), Long.parseUnsignedLong("0"), Long.parseUnsignedLong("0"), Long.parseUnsignedLong("4294967295") };
        assertArrayEquals(expected, vecTo32(results));
    }

notice:

  • varAnd.apply() took 2 long arrays (incorrect)
  • we wrap the two arg vectors intoi32ToVec(), because even though it's 4 longs each, they should be treated as i32x4 consts, i.e. 4 integers for a v128 value
  • we avoid having to concat the 2 resulting long[2] arrs by pushing to a stack instead
  • 4294967295 is an integer literal so it errors out because it won't fit (you can add L, but it's more correct to treat it as unsigned)

notice that it's not strictly necessary to modify MStack, we can also implement and ad-hoc stack or utility class (e.g. "TestArgs") only for tests

@evacchi
Copy link
Collaborator Author

evacchi commented Nov 26, 2024

FYI, I'll be AFK for the rest of the week, so feel free to rebase/change/rewrite this PR 👋

@andreaTP
Copy link
Collaborator

Thanks @evacchi for the effort!
For anyone landing here, I like where this is going, and I think is close enough.
Two missing steps:

  • do not use MStack directly in the tests, if there are no cases where the types of the arguments are diverging I'd like to see something like:
var results = varAnd.apply(i32ToVec(new long[] { 0, 0, 4294967295, 4294967295 }, new long[] { 0, 4294967295, 0, 4294967295 }));
  • add some generated tests to guarantee the fix is working

if anyone wants to step up and bring this to conclusion, just drop a message! 🙏

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