Skip to content

wasm2asm i32 arithmetic support#1120

Merged
kripken merged 24 commits intoWebAssembly:masterfrom
tlively:binaryen-i32
Aug 7, 2017
Merged

wasm2asm i32 arithmetic support#1120
kripken merged 24 commits intoWebAssembly:masterfrom
tlively:binaryen-i32

Conversation

@tlively
Copy link
Copy Markdown
Member

@tlively tlively commented Aug 3, 2017

Injects implementations of rotl, rotr, ctz, and popcnt instructions into modules as separate functions.

@tlively
Copy link
Copy Markdown
Member Author

tlively commented Aug 3, 2017

cc @eholk @jgravelle-google

Comment thread src/wasm2asm.h

Function* makePopcntFunc(MixedArena& allocator, bool is32Bit=true) {
Builder b(allocator);
// int c; for (c = 0; x != 0; c++) { x = x & (x - 1) }; return c
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Oh I thought this was commented-out code at first

Probably worth a comment like "This is implemented as" to make that clearer.

Comment thread src/wasm2asm.h Outdated
Ref Wasm2AsmBuilder::processWasm(Module* wasm) {
wasm->addFunction(makeCtzFunc(wasm->allocator));
wasm->addFunction(makePopcntFunc(wasm->allocator));
wasm->addFunction(makeRotFunc(wasm->allocator, true));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hm, bool parameters are kinda icky because they tend to not offer context at the call site for what true/false actually means. (https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/06/02/against-bool-parameters/)

How about makeRotFunc takes a WasmType instead of a bool, we assert(type == i32 || type == i64), and locally in makeRotFunc we have bool is32Bit = type == i32;

Edit: OH, this true/false is for left/right rotation. Maybe similar advice, but with opcodes instead of types? This actually lets you collapse the two bools, because RotLInt32 encodes all the information you need.

Comment thread src/wasm2asm.h Outdated
}

Ref Wasm2AsmBuilder::processWasm(Module* wasm) {
wasm->addFunction(makeCtzFunc(wasm->allocator));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd put these addFunction calls into their own method, addWasmCompatibilityFunctions or something. The list of functions to add is likely to grow, and it's logically one operation.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I was also thinking of lazily adding these functions as they're required. Do you think that would be worth doing?

Comment thread src/wasm2asm.h
static BinaryOp shifters[2][2] = {{ShrUInt64, ShrUInt32},
{ShlInt64, ShlInt32}};
Name funcName = names[isLRot][is32Bit];
BinaryOp lshift = shifters[isLRot][is32Bit];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Kind of interesting that lshift can be a right shift when !isLRot. That's probably fine, but a bit odd.

Comment thread src/wasm2asm.h Outdated
UnaryOp clzOp = is32Bit ? ClzInt32 : ClzInt64;
UnaryOp eqzOp = is32Bit ? EqZInt32 : EqZInt64;
WasmType argType = is32Bit ? i32 : i64;
Binary* xorExp = b.makeBinary(xorOp, b.makeGetLocal(0, i32),
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.

the convention in other code is

b.makeBinary(
  xorOp,
  b.makeGetLocal(0, i32),
  b.makeBinary(
    subOp,
    b.makeGetLocal(0, i32),
    b.makeConst(Literal(1))
  )
);

(i.e. always nest, and always 2 spaces for nesting)

also, when creating a literal, we normally use an explicit type, Literal(int32_t(1)) so there is no ambiguity. however, i suppose there is no ambiguity for int32 constants, even on 64-bit, is that right?

Copy link
Copy Markdown
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.

otherwise lgtm

Comment thread src/asmjs/shared-constants.cpp Outdated

MATH_MIN("Math_min"),
MATH_MAX("Math_max"),
CTZ32("__ctz_i32"),
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.

perhaps these should be prefixed? maybe wasm_ctz_i32 etc.?

@kripken kripken merged commit 62f6a12 into WebAssembly:master Aug 7, 2017
@tlively tlively deleted the binaryen-i32 branch April 24, 2020 22:44
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.

3 participants