web-devkits / Wasmnizer-ts

Toolchain for compiling TypeScript to WasmGC
Apache License 2.0
302 stars 26 forks source link

wasm file generation error #138

Open louisvangeldrop opened 9 months ago

louisvangeldrop commented 9 months ago

Code: export function indexGenerator(alpha: i32): i32[] { let results: i32[] = new Array(alpha); for (let counter: i32 = 0; counter < alpha; counter++) { results[counter] = counter; } return results; };

Error: Aborted(Assertion failed: Type::isSubType(newType, oldType), at: /home/runner/work/binaryen.js/binaryen.js/binaryen/src/passes/LocalSubtyping.cpp,148,doWalkFunction) [2024-01-07T22:46:19.148] [ERROR] console - Aborted(Assertion failed: Type::isSubType(newType, oldType), at: /home/runner/work/binaryen.js/binaryen.js/binaryen/src/passes/LocalSubtyping.cpp,148,doWalkFunction). Build with -sASSERTIONS for more info. Error details is in 'C:\Users\louis\AppData\Local\Temp\ts2wasm-log-r6GkvW\error.log' Log details is in 'C:\Users\louis\AppData\Local\Temp\ts2wasm-log-r6GkvW\trace.log'

However the following code will generate the wasm-file:

New Code:

type i32_ = i32;

export function indexGenerator(alpha: i32): i32[] { let results: i32[] = new Array(alpha); for (let counter: i32 = 0; counter < alpha; counter++) { results[counter] = counter; } return results; };

xujuntwt95329 commented 9 months ago

Hi @louisvangeldrop, thanks for reporting this issue, we've fixed in https://github.com/web-devkits/Wasmnizer-ts/pull/139

Would you mind if we add this code snippet into our test case?

louisvangeldrop commented 9 months ago

Hallo @xujuntwt95329 , Of course you may use the code snippet in your test case. I feel honored that I can help you. The function belongs to a small set of functions that I use to test the performance of javascript/typescript. I hope that the performance of compiled Static Typescript to wasm is on par wih the same functions written in C/wasm.

louisvangeldrop commented 9 months ago

I also use the following function to calculate a random number:

var mw: i32 = 123456789; var mz: i32 = 987654321; var mask: i32_ = 0xffffffff;

export function random() { m_z = (36969 (m_z & 65535) + (m_z >> 16)) & mask; m_w = (18000 (m_w & 65535) + (mw >> 16)) & mask; var xx: f64 = 4294967296 // 2**32 var result: f64_ result = (((m_z << 16) + (m_w & 65535)) >>> 0) / xx return result return Math.abs(result); }

  1. The >>> 0 should turn the sign-bit off, but it doesn't.
  2. If I replace result = (((m_z << 16) + (m_w & 65535)) >>> 0) / xx by result = (((m_z << 16) + (m_w & 65535)) >>> 0) / 4294967296. I get a divide by zero error in the iwasm_gc call
xujuntwt95329 commented 9 months ago

Hallo @xujuntwt95329 , Of course you may use the code snippet in your test case. I feel honored that I can help you. The function belongs to a small set of functions that I use to test the performance of javascript/typescript. I hope that the performance of compiled Static Typescript to wasm is on par wih the same functions written in C/wasm.

Thanks a lot!

I hope that the performance of compiled Static Typescript to wasm is on par wih the same functions written in C/wasm.

Yes, this should be the expected performance result by design as long as you don't use any and interface in the TypeScript source code, please let us know if you encounter an unexpected result.

xujuntwt95329 commented 9 months ago

I also use the following function to calculate a random number:

var mw: i32 = 123456789; var mz: i32 = 987654321; var mask: i32_ = 0xffffffff;

export function random() { m_z = (36969 (m_z & 65535) + (m_z >> 16)) & mask; m_w = (18000 (m_w & 65535) + (mw >> 16)) & mask; var xx: f64 = 4294967296 // 2**32 var result: f64_ result = (((m_z << 16) + (m_w & 65535)) >>> 0) / xx return result return Math.abs(result); }

  1. The >>> 0 should turn the sign-bit off, but it doesn't.
  2. If I replace result = (((m_z << 16) + (m_w & 65535)) >>> 0) / xx by result = (((m_z << 16) + (m_w & 65535)) >>> 0) / 4294967296. I get a divide by zero error in the iwasm_gc call

Thanks for reporting this, there must be something wrong, we will fix it soon.

louisvangeldrop commented 8 months ago

This is the code I use to measure the performance. Especially the gradeUp-function is leading. The function returns an integer vector being the permutation of indices that places the vector alpha in ascending order.


type i32_ = i32;
type i64_ = i64;
type f32_ = f32;
type f64_ = f64;
type anyref = any;

export function indexGenerator(alpha: i32_): i32_[] {
    let results: i32_[] = new Array(alpha); // sneller dan []
    for (let counter: i32_ = 0; counter < alpha; counter++) {
        results[counter] = counter;
    }
    return results;
};

export function gradeUp(alpha: f64_[], indices: i32_[], low: i32_, high: i32_): i32_[] {
    if (high <= low) return indices;
    let midValue: f64_ = alpha[indices[Math.floor((low + high) / 2) as i32_]];
    let t1: i32_, t2: i32_;
    let t3: boolean, t4: boolean;
    let i = low, j = high;
    while (i <= j) {
        (t1 = indices[i]), (t2 = indices[j]);
        (t3 = alpha[t1] >= midValue), (t4 = alpha[t2] <= midValue);
        if (t3 && t4) {
            // [indices[i], indices[j]] = [indices[j], indices[i]]  // swap not supported
            indices[i] = t2;
            indices[j] = t1;
            i = i + 1;
            j = j - 1;
        }
        else {
            if (t3 === false) {
                i++;
            }
            if (t4 === false) {
                j--;
            }
        }
    }
    gradeUp(alpha, indices, low, j);
    gradeUp(alpha, indices, i, high);

     return indices;
};

var m_w: i32_ = 123456789;
var m_z: i32_ = 987654321;
var mask: i32_ = 0xffffffff;

export function random() {
    m_z = (36969 * (m_z & 65535) + (m_z >> 16)) & mask;
    m_w = (18000 * (m_w & 65535) + (m_w >> 16)) & mask;
    var xx: f64_ = 4294967296 // 2**32
    var result: f64_
    result = (((m_z << 16) + (m_w & 65535)) >>> 1) / xx
    return result
}

export function deal(l: i32_, r: i32_): i32_[] {
// Result is an integer vector obtained by making "r" random selections from indexGenerator(l) without repetition.
    let results = indexGenerator(l)
    let h: i32_, j: i32_;

    for (let i: i32_ = 0; i < r; i++) {
        j = i + Math.floor(random() * (l - i)) as i32_; // Math.floor|number j = i + (omega-i).roll

        //   [results[j], results[i]]=[results[i], results[j]]   //destructuring werkt nog niet
        h = results[i];
        results[i] = results[j];
        results[j] = h;
    }
    return results.slice(0, r);
    };

export function from(alpha: number[], omega: number[]) {
    const rho = alpha.length;
    let z = new Array(rho);
    for (let i = 0; i < rho; i++) {
        z[i] = omega[alpha[i] >= 0 ? alpha[i] : omega.length + alpha[i]];
    }
    return z;
};

export function main(length: i32_ = 1000000) {
    const dd = deal(length, length)
    const indexes = indexGenerator(length) //deal(length, length)
    const nul: i32_ = 0
    const ai = Date.now()
    const index: i32_[] = gradeUp(dd, indexes, 0, length - 1)//, indexes) //, nul, length - 1)
    console.log(Date.now() - ai)
    let test: boolean = true
    let gg = index.map((a, x, arr) => test = test && dd[a] === x)
    console.log(test)
    return index[length - 1]
    // const sorted=from(index,data)

}