Skip to content

Commit 798893d

Browse files
alex28shSpace Team
authored andcommitted
[Wasm] specialize versions of stringLiteral
1 parent 4dce1f3 commit 798893d

File tree

5 files changed

+38
-19
lines changed

5 files changed

+38
-19
lines changed

compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/WasmSymbols.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ class WasmSymbols(
223223
val boxIntrinsic: IrSimpleFunctionSymbol = getInternalFunction("boxIntrinsic")
224224
val unboxIntrinsic: IrSimpleFunctionSymbol = getInternalFunction("unboxIntrinsic")
225225

226-
val stringGetLiteral = getFunction("stringLiteral", StandardNames.BUILT_INS_PACKAGE_FQ_NAME)
226+
val stringGetLiteralUTF16 = getFunction("stringLiteralUTF16", StandardNames.BUILT_INS_PACKAGE_FQ_NAME)
227+
val stringGetLiteralLatin = getFunction("stringLiteralLatin", StandardNames.BUILT_INS_PACKAGE_FQ_NAME)
227228
val stringGetPoolSize = getInternalFunction("stringGetPoolSize")
228229

229230
val testFun = maybeGetFunction("test", kotlinTestPackageFqName)

compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/dce/WasmUsefulDeclarationProcessor.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,15 @@ internal class WasmUsefulDeclarationProcessor(
3232
override val bodyVisitor: BodyVisitorBase = object : BodyVisitorBase() {
3333
override fun visitConst(expression: IrConst, data: IrDeclaration) = when (expression.kind) {
3434
is IrConstKind.Null -> expression.type.enqueueType(data, "expression type")
35-
is IrConstKind.String -> context.wasmSymbols.stringGetLiteral.owner
36-
.enqueue(data, "String literal intrinsic getter stringGetLiteral")
35+
is IrConstKind.String -> {
36+
if ((expression.value as String).all { it.code in 0..255 }) {
37+
context.wasmSymbols.stringGetLiteralLatin.owner
38+
.enqueue(data, "String literal intrinsic getter stringGetLiteral")
39+
} else {
40+
context.wasmSymbols.stringGetLiteralUTF16.owner
41+
.enqueue(data, "String literal intrinsic getter stringGetLiteral")
42+
}
43+
}
3744
else -> Unit
3845
}
3946

compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/DeclarationGenerator.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,11 @@ fun generateConstExpression(
628628
body.buildConstI32Symbol(literalPoolId, location)
629629
body.buildConstI32Symbol(literalAddress, location)
630630
body.buildConstI32(stringValue.length, location)
631-
body.buildConstI32(if (isLatin) 1 else 0, location)
632-
body.buildCall(context.referenceFunction(backendContext.wasmSymbols.stringGetLiteral), location)
631+
if (isLatin) {
632+
body.buildCall(context.referenceFunction(backendContext.wasmSymbols.stringGetLiteralLatin), location)
633+
} else {
634+
body.buildCall(context.referenceFunction(backendContext.wasmSymbols.stringGetLiteralUTF16), location)
635+
}
633636
body.commentGroupEnd()
634637
}
635638
}

libraries/stdlib/wasm/builtins/kotlin/String.kt

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -157,24 +157,32 @@ public actual class String internal @WasmPrimitiveConstructor constructor(
157157
internal inline fun WasmCharArray.createString(): String =
158158
String(null, this.len(), this)
159159

160-
internal fun stringLiteral(poolId: Int, startAddress: Int, length: Int, isLatin: Int = 0): String {
160+
internal fun stringLiteralUTF16(poolId: Int, startAddress: Int, length: Int): String {
161161
val cached = stringPool[poolId]
162162
if (cached !== null) {
163163
return cached
164164
}
165165

166-
val chars: WasmCharArray
167-
if (isLatin == 0) {
168-
chars = array_new_data0<WasmCharArray>(startAddress, length)
169-
} else {
170-
val bytes = array_new_data0<WasmByteArray>(startAddress, length)
171-
chars = WasmCharArray(length)
172-
for (i in 0..<length) {
173-
val chr = bytes.get(i).toInt().toChar()
174-
chars.set(i, chr)
175-
}
176-
}
166+
val chars = array_new_data0<WasmCharArray>(startAddress, length)
177167
val newString = String(null, length, chars)
178168
stringPool[poolId] = newString
179169
return newString
180170
}
171+
172+
internal fun stringLiteralLatin(poolId: Int, startAddress: Int, length: Int): String {
173+
val cached = stringPool[poolId]
174+
if (cached !== null) {
175+
return cached
176+
}
177+
178+
val bytes = array_new_data0<WasmByteArray>(startAddress, length)
179+
val chars = WasmCharArray(length)
180+
for (i in 0..<length) {
181+
val chr = bytes.get(i).toInt().toChar()
182+
chars.set(i, chr)
183+
}
184+
185+
val newString = String(null, length, chars)
186+
stringPool[poolId] = newString
187+
return newString
188+
}

libraries/stdlib/wasm/internal/kotlin/wasm/internal/TypeInfo.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ internal fun getQualifiedName(rtti: kotlin.wasm.internal.reftypes.structref): St
5656
return if (packageName.isEmpty()) typeName else "$packageName.$typeName"
5757
}
5858

59-
internal fun getPackageName(rtti: kotlin.wasm.internal.reftypes.structref): String = stringLiteral(
59+
internal fun getPackageName(rtti: kotlin.wasm.internal.reftypes.structref): String = stringLiteralUTF16(
6060
startAddress = wasmGetRttiIntField(2, rtti),
6161
length = wasmGetRttiIntField(3, rtti),
6262
poolId = wasmGetRttiIntField(4, rtti),
6363
)
6464

65-
internal fun getSimpleName(rtti: kotlin.wasm.internal.reftypes.structref): String = stringLiteral(
65+
internal fun getSimpleName(rtti: kotlin.wasm.internal.reftypes.structref): String = stringLiteralUTF16(
6666
startAddress = wasmGetRttiIntField(5, rtti),
6767
length = wasmGetRttiIntField(6, rtti),
6868
poolId = wasmGetRttiIntField(7, rtti),

0 commit comments

Comments
 (0)