From 5f140dac21d8256ec4e37ef47bc9fc583223b3f2 Mon Sep 17 00:00:00 2001 From: Zalim Bashorov Date: Thu, 17 Nov 2022 18:54:14 +0100 Subject: [PATCH] [Wasm] Remove `wasm_ref_cast` and use `wasm_ref_cast_null` inside the compiler --- .../org/jetbrains/kotlin/backend/wasm/WasmSymbols.kt | 2 +- .../wasm/dce/WasmUsefulDeclarationProcessor.kt | 5 ++--- .../kotlin/backend/wasm/ir2wasm/BodyGenerator.kt | 12 ++++++------ .../backend/wasm/lower/JsInteropFunctionsLowering.kt | 5 ++--- .../backend/wasm/lower/WasmTypeOperatorLowering.kt | 2 +- .../kotlin/wasm/internal/WasmInstructions.kt | 7 ++----- 6 files changed, 14 insertions(+), 19 deletions(-) diff --git a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/WasmSymbols.kt b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/WasmSymbols.kt index 8eb33a19cf5..e4bb914576c 100644 --- a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/WasmSymbols.kt +++ b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/WasmSymbols.kt @@ -180,7 +180,7 @@ class WasmSymbols( val refIsNull = getInternalFunction("wasm_ref_is_null") val externRefIsNull = getInternalFunction("wasm_externref_is_null") 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 wasmArrayNewData0 = getInternalFunction("array_new_data0") diff --git a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/dce/WasmUsefulDeclarationProcessor.kt b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/dce/WasmUsefulDeclarationProcessor.kt index 09fda8dc046..5c4dc8a6870 100644 --- a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/dce/WasmUsefulDeclarationProcessor.kt +++ b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/dce/WasmUsefulDeclarationProcessor.kt @@ -15,7 +15,6 @@ import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull -import org.jetbrains.kotlin.wasm.ir.* internal class WasmUsefulDeclarationProcessor( override val context: WasmBackendContext, @@ -59,7 +58,7 @@ internal class WasmUsefulDeclarationProcessor( context.wasmSymbols.wasmClassId, context.wasmSymbols.wasmInterfaceId, - context.wasmSymbols.refCast, + context.wasmSymbols.refCastNull, context.wasmSymbols.refTest, context.wasmSymbols.boxIntrinsic, context.wasmSymbols.wasmArrayCopy -> { @@ -183,4 +182,4 @@ internal class WasmUsefulDeclarationProcessor( } override fun isExported(declaration: IrDeclaration): Boolean = declaration.isJsExport() -} \ No newline at end of file +} diff --git a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/BodyGenerator.kt b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/BodyGenerator.kt index 750baeb72a4..746763b46bf 100644 --- a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/BodyGenerator.kt +++ b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/BodyGenerator.kt @@ -392,7 +392,7 @@ class BodyGenerator( generateExpression(receiver) //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.referenceVTableGcType(klass.symbol), WasmSymbol(vfSlot)) @@ -428,7 +428,7 @@ class BodyGenerator( body.buildGetUnit() } - private fun generateRefCast(fromType: IrType, toType: IrType) { + private fun generateRefNullCast(fromType: IrType, toType: IrType) { if (!isDownCastAlwaysSuccessInRuntime(fromType, toType)) { body.buildRefCastNullStatic( toType = context.referenceGcType(toType.getRuntimeClass(irBuiltIns).symbol) @@ -507,8 +507,8 @@ class BodyGenerator( } } - wasmSymbols.refCast -> { - generateRefCast( + wasmSymbols.refCastNull -> { + generateRefNullCast( fromType = call.getValueArgument(0)!!.type, toType = call.getTypeArgument(0)!! ) @@ -540,7 +540,7 @@ class BodyGenerator( val klass: IrClass = backendContext.inlineClassesUtils.getInlinedClass(toType)!! val field = getInlineClassBackingField(klass) - generateRefCast(fromType, toType) + generateRefNullCast(fromType, toType) generateInstanceFieldAccess(field) } @@ -722,7 +722,7 @@ class BodyGenerator( // REF -> REF -> REF_CAST if (!expectedIsPrimitive) { - generateRefCast(actualTypeErased, expectedTypeErased) + generateRefNullCast(actualTypeErased, expectedTypeErased) } } diff --git a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/lower/JsInteropFunctionsLowering.kt b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/lower/JsInteropFunctionsLowering.kt index b6d17b6156b..6bf58063182 100644 --- a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/lower/JsInteropFunctionsLowering.kt +++ b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/lower/JsInteropFunctionsLowering.kt @@ -22,7 +22,6 @@ import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.expressions.IrBody import org.jetbrains.kotlin.ir.expressions.IrCall 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.types.* import org.jetbrains.kotlin.ir.util.* @@ -674,7 +673,7 @@ class JsInteropFunctionsLowering(val context: WasmBackendContext) : DeclarationT ) : InteropTypeAdapter { override val fromType: IrType = context.wasmSymbols.wasmDataRefType 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.putTypeArgument(0, toType) return call @@ -772,4 +771,4 @@ class JsInteropFunctionCallsLowering(val context: WasmBackendContext) : BodyLowe } }) } -} \ No newline at end of file +} diff --git a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/lower/WasmTypeOperatorLowering.kt b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/lower/WasmTypeOperatorLowering.kt index 6c2874e00c8..c89585d1cde 100644 --- a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/lower/WasmTypeOperatorLowering.kt +++ b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/lower/WasmTypeOperatorLowering.kt @@ -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) putValueArgument(0, value) } diff --git a/libraries/stdlib/wasm/internal/kotlin/wasm/internal/WasmInstructions.kt b/libraries/stdlib/wasm/internal/kotlin/wasm/internal/WasmInstructions.kt index b4a347fe318..a9546e227df 100644 --- a/libraries/stdlib/wasm/internal/kotlin/wasm/internal/WasmInstructions.kt +++ b/libraries/stdlib/wasm/internal/kotlin/wasm/internal/WasmInstructions.kt @@ -19,15 +19,12 @@ import kotlin.wasm.internal.ExternalInterfaceType internal fun wasm_unreachable(): Nothing = implementedAsIntrinsic -internal fun wasm_ref_cast(a: Any?): To = - implementedAsIntrinsic - @Suppress("REIFIED_TYPE_PARAMETER_NO_INLINE") -@WasmOp(WasmOp.REF_CAST_NULL) internal fun wasm_ref_cast_null(a: Any?): To = implementedAsIntrinsic -internal fun wasm_ref_test(a: Any?): Boolean = +@Suppress("REIFIED_TYPE_PARAMETER_NO_INLINE") +internal fun wasm_ref_test(a: Any?): Boolean = implementedAsIntrinsic @Suppress("REIFIED_TYPE_PARAMETER_NO_INLINE")