[Wasm] Add fast path for null in externRefToAny

- Introduce new intrinsics `wasm_ref_cast_null` & `wasm_ref_test_null`.
- Support getting type for immediate from generic argument.
- Remove redundant null check in `tryGetOrSetExternrefBox`.
This commit is contained in:
Zalim Bashorov
2022-11-15 13:39:40 +01:00
parent b31a657494
commit 919029f34f
4 changed files with 23 additions and 8 deletions
@@ -109,8 +109,8 @@ fun WasmCompiledModuleFragment.generateJs(): String {
//language=js
val runtime = """
const externrefBoxes = new WeakMap();
// ref must be non-null
function tryGetOrSetExternrefBox(ref, ifNotCached) {
if (ref == null) return null;
if (typeof ref !== 'object') return ifNotCached;
const cachedBox = externrefBoxes.get(ref);
if (cachedBox !== void 0) return cachedBox;
@@ -461,7 +461,7 @@ class BodyGenerator(
call: IrFunctionAccessExpression,
function: IrFunction
): Boolean {
if (tryToGenerateWasmOpIntrinsicCall(function)) {
if (tryToGenerateWasmOpIntrinsicCall(call, function)) {
return true
}
@@ -840,7 +840,7 @@ class BodyGenerator(
}
// Return true if function is recognized as intrinsic.
private fun tryToGenerateWasmOpIntrinsicCall(function: IrFunction): Boolean {
private fun tryToGenerateWasmOpIntrinsicCall(call: IrFunctionAccessExpression, function: IrFunction): Boolean {
if (function.hasWasmNoOpCastAnnotation()) {
return true
}
@@ -853,14 +853,19 @@ class BodyGenerator(
body.buildInstr(op)
}
1 -> {
fun getReferenceGcType(): WasmSymbol<WasmTypeDeclaration> {
val type = function.dispatchReceiverParameter?.type ?: call.getTypeArgument(0)!!
return context.referenceGcType(type.classOrNull!!)
}
val immediates = arrayOf(
when (val imm = op.immediates[0]) {
WasmImmediateKind.MEM_ARG ->
WasmImmediate.MemArg(0u, 0u)
WasmImmediateKind.STRUCT_TYPE_IDX ->
WasmImmediate.GcType(context.referenceGcType(function.dispatchReceiverParameter!!.type.classOrNull!!))
WasmImmediate.GcType(getReferenceGcType())
WasmImmediateKind.TYPE_IDX ->
WasmImmediate.TypeIdx(context.referenceGcType(function.dispatchReceiverParameter!!.type.classOrNull!!))
WasmImmediate.TypeIdx(getReferenceGcType())
else ->
error("Immediate $imm is unsupported")