[Wasm] Remove wasm_ref_cast and use wasm_ref_cast_null inside the compiler

This commit is contained in:
Zalim Bashorov
2022-11-17 18:54:14 +01:00
parent e7d0e451e7
commit 5f140dac21
6 changed files with 14 additions and 19 deletions
@@ -180,7 +180,7 @@ class WasmSymbols(
val refIsNull = getInternalFunction("wasm_ref_is_null") val refIsNull = getInternalFunction("wasm_ref_is_null")
val externRefIsNull = getInternalFunction("wasm_externref_is_null") val externRefIsNull = getInternalFunction("wasm_externref_is_null")
val refTest = getInternalFunction("wasm_ref_test") val refTest = getInternalFunction("wasm_ref_test")
val refCast = getInternalFunction("wasm_ref_cast") val refCastNull = getInternalFunction("wasm_ref_cast_null")
val wasmArrayCopy = getInternalFunction("wasm_array_copy") val wasmArrayCopy = getInternalFunction("wasm_array_copy")
val wasmArrayNewData0 = getInternalFunction("array_new_data0") val wasmArrayNewData0 = getInternalFunction("array_new_data0")
@@ -15,7 +15,6 @@ import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
import org.jetbrains.kotlin.wasm.ir.*
internal class WasmUsefulDeclarationProcessor( internal class WasmUsefulDeclarationProcessor(
override val context: WasmBackendContext, override val context: WasmBackendContext,
@@ -59,7 +58,7 @@ internal class WasmUsefulDeclarationProcessor(
context.wasmSymbols.wasmClassId, context.wasmSymbols.wasmClassId,
context.wasmSymbols.wasmInterfaceId, context.wasmSymbols.wasmInterfaceId,
context.wasmSymbols.refCast, context.wasmSymbols.refCastNull,
context.wasmSymbols.refTest, context.wasmSymbols.refTest,
context.wasmSymbols.boxIntrinsic, context.wasmSymbols.boxIntrinsic,
context.wasmSymbols.wasmArrayCopy -> { context.wasmSymbols.wasmArrayCopy -> {
@@ -392,7 +392,7 @@ class BodyGenerator(
generateExpression(receiver) generateExpression(receiver)
//TODO: check why it could be needed //TODO: check why it could be needed
generateRefCast(receiver.type, klass.defaultType) generateRefNullCast(receiver.type, klass.defaultType)
body.buildStructGet(context.referenceGcType(klass.symbol), WasmSymbol(0)) body.buildStructGet(context.referenceGcType(klass.symbol), WasmSymbol(0))
body.buildStructGet(context.referenceVTableGcType(klass.symbol), WasmSymbol(vfSlot)) body.buildStructGet(context.referenceVTableGcType(klass.symbol), WasmSymbol(vfSlot))
@@ -428,7 +428,7 @@ class BodyGenerator(
body.buildGetUnit() body.buildGetUnit()
} }
private fun generateRefCast(fromType: IrType, toType: IrType) { private fun generateRefNullCast(fromType: IrType, toType: IrType) {
if (!isDownCastAlwaysSuccessInRuntime(fromType, toType)) { if (!isDownCastAlwaysSuccessInRuntime(fromType, toType)) {
body.buildRefCastNullStatic( body.buildRefCastNullStatic(
toType = context.referenceGcType(toType.getRuntimeClass(irBuiltIns).symbol) toType = context.referenceGcType(toType.getRuntimeClass(irBuiltIns).symbol)
@@ -507,8 +507,8 @@ class BodyGenerator(
} }
} }
wasmSymbols.refCast -> { wasmSymbols.refCastNull -> {
generateRefCast( generateRefNullCast(
fromType = call.getValueArgument(0)!!.type, fromType = call.getValueArgument(0)!!.type,
toType = call.getTypeArgument(0)!! toType = call.getTypeArgument(0)!!
) )
@@ -540,7 +540,7 @@ class BodyGenerator(
val klass: IrClass = backendContext.inlineClassesUtils.getInlinedClass(toType)!! val klass: IrClass = backendContext.inlineClassesUtils.getInlinedClass(toType)!!
val field = getInlineClassBackingField(klass) val field = getInlineClassBackingField(klass)
generateRefCast(fromType, toType) generateRefNullCast(fromType, toType)
generateInstanceFieldAccess(field) generateInstanceFieldAccess(field)
} }
@@ -722,7 +722,7 @@ class BodyGenerator(
// REF -> REF -> REF_CAST // REF -> REF -> REF_CAST
if (!expectedIsPrimitive) { if (!expectedIsPrimitive) {
generateRefCast(actualTypeErased, expectedTypeErased) generateRefNullCast(actualTypeErased, expectedTypeErased)
} }
} }
@@ -22,7 +22,6 @@ import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.IrBody import org.jetbrains.kotlin.ir.expressions.IrBody
import org.jetbrains.kotlin.ir.expressions.IrCall import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.IrExpression import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrStatementOriginImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrInstanceInitializerCallImpl import org.jetbrains.kotlin.ir.expressions.impl.IrInstanceInitializerCallImpl
import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.util.*
@@ -674,7 +673,7 @@ class JsInteropFunctionsLowering(val context: WasmBackendContext) : DeclarationT
) : InteropTypeAdapter { ) : InteropTypeAdapter {
override val fromType: IrType = context.wasmSymbols.wasmDataRefType override val fromType: IrType = context.wasmSymbols.wasmDataRefType
override fun adapt(expression: IrExpression, builder: IrBuilderWithScope): IrExpression { override fun adapt(expression: IrExpression, builder: IrBuilderWithScope): IrExpression {
val call = builder.irCall(context.wasmSymbols.refCast) val call = builder.irCall(context.wasmSymbols.refCastNull)
call.putValueArgument(0, expression) call.putValueArgument(0, expression)
call.putTypeArgument(0, toType) call.putTypeArgument(0, toType)
return call return call
@@ -242,7 +242,7 @@ class WasmBaseTypeOperatorTransformer(val context: WasmBackendContext) : IrEleme
} }
} }
return builder.irCall(symbols.refCast, type = toType).apply { return builder.irCall(symbols.refCastNull, type = toType).apply {
putTypeArgument(0, toType) putTypeArgument(0, toType)
putValueArgument(0, value) putValueArgument(0, value)
} }
@@ -19,15 +19,12 @@ import kotlin.wasm.internal.ExternalInterfaceType
internal fun wasm_unreachable(): Nothing = internal fun wasm_unreachable(): Nothing =
implementedAsIntrinsic implementedAsIntrinsic
internal fun <To> wasm_ref_cast(a: Any?): To =
implementedAsIntrinsic
@Suppress("REIFIED_TYPE_PARAMETER_NO_INLINE") @Suppress("REIFIED_TYPE_PARAMETER_NO_INLINE")
@WasmOp(WasmOp.REF_CAST_NULL)
internal fun <reified To> wasm_ref_cast_null(a: Any?): To = internal fun <reified To> wasm_ref_cast_null(a: Any?): To =
implementedAsIntrinsic implementedAsIntrinsic
internal fun <To> wasm_ref_test(a: Any?): Boolean = @Suppress("REIFIED_TYPE_PARAMETER_NO_INLINE")
internal fun <reified To> wasm_ref_test(a: Any?): Boolean =
implementedAsIntrinsic implementedAsIntrinsic
@Suppress("REIFIED_TYPE_PARAMETER_NO_INLINE") @Suppress("REIFIED_TYPE_PARAMETER_NO_INLINE")