Codereview

This commit is contained in:
Igor Yakovlev
2023-05-20 13:03:21 +02:00
committed by Space Team
parent 78b72efd32
commit c71a2c6dd3
4 changed files with 30 additions and 29 deletions
@@ -118,13 +118,13 @@ private val lateinitUsageLoweringPhase = makeWasmModulePhase(
private val arrayConstructorReferencePhase = makeWasmModulePhase( private val arrayConstructorReferencePhase = makeWasmModulePhase(
::WasmArrayConstructorReferenceLowering, ::WasmArrayConstructorReferenceLowering,
name = "ArrayConstructorReference", name = "ArrayConstructorReference",
description = "Transform `::Array` into a lambda" description = "Transform `::Array` into a ::create#Array"
) )
private val arrayConstructorPhase = makeWasmModulePhase( private val arrayConstructorPhase = makeWasmModulePhase(
::WasmArrayConstructorLowering, ::WasmArrayConstructorLowering,
name = "ArrayConstructor", name = "ArrayConstructor",
description = "Transform `Array(size) { index -> value }` into a loop", description = "Transform `Array(size) { index -> value }` into create#Array { index -> value } call",
prerequisite = setOf(arrayConstructorReferencePhase) prerequisite = setOf(arrayConstructorReferencePhase)
) )
@@ -629,8 +629,6 @@ val wasmPhases = SameTypeNamedCompilerPhase(
localClassesInInlineFunctionsPhase then localClassesInInlineFunctionsPhase then
localClassesExtractionFromInlineFunctionsPhase then localClassesExtractionFromInlineFunctionsPhase then
// TODO: Need some helpers from stdlib
// arrayConstructorPhase then
wrapInlineDeclarationsWithReifiedTypeParametersPhase then wrapInlineDeclarationsWithReifiedTypeParametersPhase then
functionInliningPhase then functionInliningPhase then
@@ -141,12 +141,6 @@ internal class WasmUsefulDeclarationProcessor(
irClass.getWasmArrayAnnotation()?.type irClass.getWasmArrayAnnotation()?.type
?.enqueueType(irClass, "array type for wasm array annotated") ?.enqueueType(irClass, "array type for wasm array annotated")
if (irClass.symbol in context.wasmSymbols.primitiveTypeToCreateTypedArray.keys) {
irClass.declarations.forEach {
(it as? IrField)?.enqueue(irClass, "preserve all fields for primitive arrays")
}
}
if (context.inlineClassesUtils.isClassInlineLike(irClass)) { if (context.inlineClassesUtils.isClassInlineLike(irClass)) {
irClass.declarations irClass.declarations
.firstIsInstanceOrNull<IrConstructor>() .firstIsInstanceOrNull<IrConstructor>()
@@ -187,9 +181,18 @@ internal class WasmUsefulDeclarationProcessor(
override fun processConstructor(irConstructor: IrConstructor) { override fun processConstructor(irConstructor: IrConstructor) {
super.processConstructor(irConstructor) super.processConstructor(irConstructor)
if (!context.inlineClassesUtils.isClassInlineLike(irConstructor.parentAsClass)) { val constructedClass = irConstructor.constructedClass
if (!context.inlineClassesUtils.isClassInlineLike(constructedClass)) {
processIrFunction(irConstructor) processIrFunction(irConstructor)
} }
if (irConstructor.hasWasmPrimitiveConstructorAnnotation()) {
constructedClass.declarations.forEach { declaration ->
if (declaration is IrField) {
declaration.enqueue(constructedClass, "preserve all fields for primitive constructors")
}
}
}
} }
override fun isExported(declaration: IrDeclaration): Boolean = declaration.isJsExport() override fun isExported(declaration: IrDeclaration): Boolean = declaration.isJsExport()
@@ -86,8 +86,8 @@ internal fun <T> arrayIterator(array: Array<T>) = object : Iterator<T> {
override fun next() = if (index != array.size) array[index++] else throw NoSuchElementException("$index") override fun next() = if (index != array.size) array[index++] else throw NoSuchElementException("$index")
} }
internal inline fun <reified T> createAnyArray(size: Int, invokable: (Int) -> T): Array<T> { internal inline fun <reified T> createAnyArray(size: Int, init: (Int) -> T): Array<T> {
val result = WasmAnyArray(size) val result = WasmAnyArray(size)
result.fill(size, invokable) result.fill(size, init)
return Array(result) return Array(result)
} }
+16 -16
View File
@@ -45,9 +45,9 @@ internal fun byteArrayIterator(array: ByteArray) = object : ByteIterator() {
override fun nextByte() = if (index != array.size) array[index++] else throw NoSuchElementException("$index") override fun nextByte() = if (index != array.size) array[index++] else throw NoSuchElementException("$index")
} }
internal inline fun createByteArray(size: Int, invokable: (Int) -> Byte): ByteArray { internal inline fun createByteArray(size: Int, init: (Int) -> Byte): ByteArray {
val result = WasmByteArray(size) val result = WasmByteArray(size)
result.fill(size, invokable) result.fill(size, init)
return ByteArray(result) return ByteArray(result)
} }
@@ -82,9 +82,9 @@ internal fun charArrayIterator(array: CharArray) = object : CharIterator() {
override fun nextChar() = if (index != array.size) array[index++] else throw NoSuchElementException("$index") override fun nextChar() = if (index != array.size) array[index++] else throw NoSuchElementException("$index")
} }
internal inline fun createCharArray(size: Int, invokable: (Int) -> Char): CharArray { internal inline fun createCharArray(size: Int, init: (Int) -> Char): CharArray {
val result = WasmCharArray(size) val result = WasmCharArray(size)
result.fill(size, invokable) result.fill(size, init)
return CharArray(result) return CharArray(result)
} }
@@ -119,9 +119,9 @@ internal fun shortArrayIterator(array: ShortArray) = object : ShortIterator() {
override fun nextShort() = if (index != array.size) array[index++] else throw NoSuchElementException("$index") override fun nextShort() = if (index != array.size) array[index++] else throw NoSuchElementException("$index")
} }
internal inline fun createShortArray(size: Int, invokable: (Int) -> Short): ShortArray { internal inline fun createShortArray(size: Int, init: (Int) -> Short): ShortArray {
val result = WasmShortArray(size) val result = WasmShortArray(size)
result.fill(size, invokable) result.fill(size, init)
return ShortArray(result) return ShortArray(result)
} }
@@ -156,9 +156,9 @@ internal fun intArrayIterator(array: IntArray) = object : IntIterator() {
override fun nextInt() = if (index != array.size) array[index++] else throw NoSuchElementException("$index") override fun nextInt() = if (index != array.size) array[index++] else throw NoSuchElementException("$index")
} }
internal inline fun createIntArray(size: Int, invokable: (Int) -> Int): IntArray { internal inline fun createIntArray(size: Int, init: (Int) -> Int): IntArray {
val result = WasmIntArray(size) val result = WasmIntArray(size)
result.fill(size, invokable) result.fill(size, init)
return IntArray(result) return IntArray(result)
} }
@@ -192,9 +192,9 @@ internal fun longArrayIterator(array: LongArray) = object : LongIterator() {
override fun nextLong() = if (index != array.size) array[index++] else throw NoSuchElementException("$index") override fun nextLong() = if (index != array.size) array[index++] else throw NoSuchElementException("$index")
} }
internal inline fun createLongArray(size: Int, invokable: (Int) -> Long): LongArray { internal inline fun createLongArray(size: Int, init: (Int) -> Long): LongArray {
val result = WasmLongArray(size) val result = WasmLongArray(size)
result.fill(size, invokable) result.fill(size, init)
return LongArray(result) return LongArray(result)
} }
@@ -228,9 +228,9 @@ internal fun floatArrayIterator(array: FloatArray) = object : FloatIterator() {
override fun nextFloat() = if (index != array.size) array[index++] else throw NoSuchElementException("$index") override fun nextFloat() = if (index != array.size) array[index++] else throw NoSuchElementException("$index")
} }
internal inline fun createFloatArray(size: Int, invokable: (Int) -> Float): FloatArray { internal inline fun createFloatArray(size: Int, init: (Int) -> Float): FloatArray {
val result = WasmFloatArray(size) val result = WasmFloatArray(size)
result.fill(size, invokable) result.fill(size, init)
return FloatArray(result) return FloatArray(result)
} }
@@ -264,9 +264,9 @@ internal fun doubleArrayIterator(array: DoubleArray) = object : DoubleIterator()
override fun nextDouble() = if (index != array.size) array[index++] else throw NoSuchElementException("$index") override fun nextDouble() = if (index != array.size) array[index++] else throw NoSuchElementException("$index")
} }
internal inline fun createDoubleArray(size: Int, invokable: (Int) -> Double): DoubleArray { internal inline fun createDoubleArray(size: Int, init: (Int) -> Double): DoubleArray {
val result = WasmDoubleArray(size) val result = WasmDoubleArray(size)
result.fill(size, invokable) result.fill(size, init)
return DoubleArray(result) return DoubleArray(result)
} }
@@ -304,10 +304,10 @@ internal fun booleanArrayIterator(array: BooleanArray) = object : BooleanIterato
private fun Boolean.reinterpretAsByte(): Byte = private fun Boolean.reinterpretAsByte(): Byte =
implementedAsIntrinsic implementedAsIntrinsic
internal inline fun createBooleanArray(size: Int, invokable: (Int) -> Boolean): BooleanArray { internal inline fun createBooleanArray(size: Int, init: (Int) -> Boolean): BooleanArray {
val result = WasmByteArray(size) val result = WasmByteArray(size)
result.fill(size) { result.fill(size) {
invokable(it).reinterpretAsByte() init(it).reinterpretAsByte()
} }
return BooleanArray(result) return BooleanArray(result)
} }