Skip to content

Commit

Permalink
avoid array copy if no spread expr is found
Browse files Browse the repository at this point in the history
Signed-off-by: Su Yihan <[email protected]>
  • Loading branch information
yviansu committed Feb 5, 2024
1 parent 98e2c96 commit 423251b
Showing 1 changed file with 44 additions and 32 deletions.
76 changes: 44 additions & 32 deletions src/backend/binaryen/wasm_expr_gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4728,6 +4728,7 @@ export class WASMExpressionGen {
? arrType.element
: (arrType as WASMArrayType).arrayType.element;
const statementArray: binaryenCAPI.ExpressionRef[] = [];
let needCopy = false;
for (let i = 0; i < arrayLen; i++) {
let elemValue = values[i];
if (
Expand All @@ -4739,6 +4740,7 @@ export class WASMExpressionGen {
}
const elemRef = this.wasmExprGen(elemValue);
if (elemValue.kind == SemanticsValueKind.SPREAD) {
needCopy = true;
if (elemRefs.length != 0) {
const elemArrRef = binaryenCAPI._BinaryenArrayNewFixed(
this.module.ptr,
Expand Down Expand Up @@ -5000,44 +5002,54 @@ export class WASMExpressionGen {
elemRefs.push(elemRef);
}
}
if (elemRefs.length != 0) {
const elemArrRef = binaryenCAPI._BinaryenArrayNewFixed(
this.module.ptr,

const elemArrRef = binaryenCAPI._BinaryenArrayNewFixed(
this.module.ptr,
arrayOriHeapType,
arrayToPtr(elemRefs).ptr,
elemRefs.length,
);
const elemArrLocal =
this.wasmCompiler.currentFuncCtx!.insertTmpVar(arrayOriTypeRef);
const setElemArrLocalStmt = this.module.local.set(
elemArrLocal.index,
elemArrRef,
);
const getElemArrLocalStmt = this.module.local.get(
elemArrLocal.index,
elemArrLocal.type,
);
statementArray.push(setElemArrLocalStmt);
srcArrRefs.push(getElemArrLocalStmt);
elemRefs = [];

let finalArrRef: binaryen.ExpressionRef;
let finalArrLenRef: binaryen.ExpressionRef;
if (needCopy) {
const resConcatArr = this.wasmArrayConcat(
srcArrRefs,
arrayOriHeapType,
arrayToPtr(elemRefs).ptr,
elemRefs.length,
);
const elemArrLocal = this.wasmCompiler.currentFuncCtx!.insertTmpVar(
binaryen.getExpressionType(elemArrRef),
);
const setElemArrLocalStmt = this.module.local.set(
elemArrLocal.index,
elemArrRef,
statementArray,
);
const getElemArrLocalStmt = this.module.local.get(
elemArrLocal.index,
elemArrLocal.type,
const newArrLenRef = binaryenCAPI._BinaryenArrayLen(
this.module.ptr,
this.module.local.get(
resConcatArr.local.index,
resConcatArr.local.type,
),
);
statementArray.push(setElemArrLocalStmt);
srcArrRefs.push(getElemArrLocalStmt);
elemRefs = [];
finalArrRef = resConcatArr.ref;
finalArrLenRef = newArrLenRef;
} else {
statementArray.push(getElemArrLocalStmt);
finalArrRef = this.module.block(null, statementArray);
finalArrLenRef = this.module.i32.const(arrayLen);
}
const resConcatArr = this.wasmArrayConcat(
srcArrRefs,
arrayOriHeapType,
statementArray,
);
const newArrLenRef = binaryenCAPI._BinaryenArrayLen(
this.module.ptr,
this.module.local.get(
resConcatArr.local.index,
resConcatArr.local.type,
),
);

if (arrType instanceof ArrayType) {
const arrayStructRef = binaryenCAPI._BinaryenStructNew(
this.module.ptr,
arrayToPtr([resConcatArr.ref, newArrLenRef]).ptr,
arrayToPtr([finalArrRef, finalArrLenRef]).ptr,
2,
arrayStructHeapType,
);
Expand All @@ -5056,7 +5068,7 @@ export class WASMExpressionGen {
this.wasmCompiler.currentFuncCtx!.insert(setNewArrStructLocal);
return getNewArrStructLocal;
} else {
return resConcatArr.ref;
return finalArrRef;
}
}

Expand Down

0 comments on commit 423251b

Please sign in to comment.