[WASM] Fast load string literals from data section

This commit is contained in:
Igor Yakovlev
2022-10-14 00:03:51 +02:00
committed by teamcity
parent 033e2c45f1
commit 0e16889f45
9 changed files with 42 additions and 24 deletions
@@ -168,6 +168,7 @@ class WasmSymbols(
val refTest = getInternalFunction("wasm_ref_test")
val refCast = getInternalFunction("wasm_ref_cast")
val wasmArrayCopy = getInternalFunction("wasm_array_copy")
val wasmArrayNewData0 = getInternalFunction("array_new_data0")
val intToLong = getInternalFunction("wasm_i64_extend_i32_s")
@@ -491,6 +491,13 @@ class BodyGenerator(
body.buildInstr(WasmOp.ARRAY_COPY, immediate, immediate)
}
wasmSymbols.wasmArrayNewData0 -> {
val immediate = WasmImmediate.GcType(
context.referenceGcType(call.getTypeArgument(0)!!.getRuntimeClass(irBuiltIns).symbol)
)
body.buildInstr(WasmOp.ARRAY_NEW_DATA, immediate, WasmImmediate.DataIdx(0))
}
wasmSymbols.stringGetPoolSize -> {
body.buildConstI32Symbol(context.stringPoolSize)
}
@@ -492,7 +492,6 @@ fun generateConstExpression(expression: IrConst<*>, body: WasmExpressionBuilder,
body.buildConstI32Symbol(literalPoolId)
body.buildConstI32Symbol(literalAddress)
body.buildConstI32(stringValue.length)
body.buildConstI32(stringValue.hashCode())
body.buildCall(context.referenceFunction(context.backendContext.wasmSymbols.stringGetLiteral))
}
else -> error("Unknown constant kind")
@@ -124,19 +124,6 @@ class WasmCompiledModuleFragment(val irBuiltIns: IrBuiltIns) {
currentDataSectionAddress += typeInfoElement.sizeInBytes
}
val stringDataSectionStart = currentDataSectionAddress
val stringDataSectionBytes = mutableListOf<Byte>()
var stringLiteralCount = 0
for ((string, symbol) in stringLiteralAddress.unbound) {
val constData = ConstantDataCharArray("string_literal", string.toCharArray())
stringDataSectionBytes += constData.toBytes().toList()
symbol.bind(currentDataSectionAddress)
stringLiteralPoolId.reference(string).bind(stringLiteralCount)
currentDataSectionAddress += constData.sizeInBytes
stringLiteralCount++
}
stringPoolSize.bind(stringLiteralCount)
// Reserve some memory to pass complex exported types (like strings). It's going to be accessible through 'unsafeGetScratchRawMemory'
// runtime call from stdlib.
currentDataSectionAddress = alignUp(currentDataSectionAddress, INT_SIZE_BYTES)
@@ -146,8 +133,22 @@ class WasmCompiledModuleFragment(val irBuiltIns: IrBuiltIns) {
bind(classIds.unbound, klassIds)
interfaceId.unbound.onEachIndexed { index, entry -> entry.value.bind(index) }
val data = typeInfo.buildData(address = { klassIds.getValue(it) }) +
WasmData(WasmDataMode.Active(0, stringDataSectionStart), stringDataSectionBytes.toByteArray())
val stringDataSectionBytes = mutableListOf<Byte>()
var stringDataSectionStart = 0
var stringLiteralCount = 0
for ((string, symbol) in stringLiteralAddress.unbound) {
symbol.bind(stringDataSectionStart)
stringLiteralPoolId.reference(string).bind(stringLiteralCount)
val constData = ConstantDataCharArray("string_literal", string.toCharArray())
stringDataSectionBytes += constData.toBytes().toList()
stringDataSectionStart += constData.sizeInBytes
stringLiteralCount++
}
stringPoolSize.bind(stringLiteralCount)
val data = mutableListOf<WasmData>()
data.add(WasmData(WasmDataMode.Passive, stringDataSectionBytes.toByteArray()))
typeInfo.buildData(data, address = { klassIds.getValue(it) })
val masterInitFunctionType = WasmFunctionType(emptyList(), emptyList())
val masterInitFunction = WasmFunction.Defined("__init", WasmSymbol(masterInitFunctionType))
@@ -227,8 +228,11 @@ private fun irSymbolDebugDump(symbol: Any?): String =
else -> symbol.toString()
}
inline fun WasmCompiledModuleFragment.ReferencableAndDefinable<IrClassSymbol, ConstantDataElement>.buildData(address: (IrClassSymbol) -> Int): List<WasmData> {
return elements.map {
inline fun WasmCompiledModuleFragment.ReferencableAndDefinable<IrClassSymbol, ConstantDataElement>.buildData(
into: MutableList<WasmData>,
address: (IrClassSymbol) -> Int
) {
elements.mapTo(into) {
val id = address(wasmToIr.getValue(it))
val offset = mutableListOf<WasmInstr>()
WasmIrExpressionBuilder(offset).buildConstI32(id)