From e542dcf5054d204ef4ba8d0f628d8332460b96f5 Mon Sep 17 00:00:00 2001 From: Michal Harakal Date: Mon, 13 Apr 2026 13:16:30 +0200 Subject: [PATCH] Fix WasmJS/JS/Native compile: replace putIfAbsent with common idiom MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `MutableMap.putIfAbsent(key, value)` is a JVM-only extension (it lives on `java.util.Map`), not part of Kotlin common-stdlib. The usage introduced in #477's `collectTensorEncodings` compiled fine on JVM but broke `compileKotlinWasmJs` (and by extension JS / Native) with `Unresolved reference 'putIfAbsent'` at StableHloConverter.kt:209,213. develop has been red on the multiplatform build-job since #478 merged. Root cause: the local verification for #477 ran only `:skainet-compile:skainet-compile-hlo:jvmTest`. KMP's per-target compilation means a JVM-only stdlib call silently passes JVM checks and only fails on the first non-JVM compile. Replaces both call sites with the common-compatible idiom: if (spec.name !in result) result[spec.name] = encoding Verified locally with `./gradlew :skainet-compile:skainet-compile-hlo:allTests -x kotlinWasmStoreYarnLock` — green across jvmTest, wasmJsTest, wasmJsBrowserTest, wasmWasiTest, wasmWasiNodeTest, macosArm64Test, and iosSimulatorArm64Test (linuxX64Test skipped on macOS host). Co-Authored-By: Claude Opus 4.6 (1M context) --- .../kotlin/sk/ainet/compile/hlo/StableHloConverter.kt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/skainet-compile/skainet-compile-hlo/src/commonMain/kotlin/sk/ainet/compile/hlo/StableHloConverter.kt b/skainet-compile/skainet-compile-hlo/src/commonMain/kotlin/sk/ainet/compile/hlo/StableHloConverter.kt index 54bf4113..08c1e690 100644 --- a/skainet-compile/skainet-compile-hlo/src/commonMain/kotlin/sk/ainet/compile/hlo/StableHloConverter.kt +++ b/skainet-compile/skainet-compile-hlo/src/commonMain/kotlin/sk/ainet/compile/hlo/StableHloConverter.kt @@ -200,17 +200,22 @@ public class StableHloConverter( * `name -> encoding` map of every tensor that carries a non-null * [TensorEncoding]. Duplicates (the same name appearing in multiple * nodes) collapse to a single entry — first-writer-wins. + * + * Implementation note: uses an explicit `!in` check rather than + * `MutableMap.putIfAbsent` because the latter is a JVM-only + * extension and does not exist in Kotlin common-stdlib for WasmJS + * / JS / Native targets. */ private fun collectTensorEncodings(nodes: List): Map { val result = linkedMapOf() for (node in nodes) { for (spec in node.outputs) { val encoding = spec.tensorEncoding ?: continue - result.putIfAbsent(spec.name, encoding) + if (spec.name !in result) result[spec.name] = encoding } for (spec in node.inputs) { val encoding = spec.tensorEncoding ?: continue - result.putIfAbsent(spec.name, encoding) + if (spec.name !in result) result[spec.name] = encoding } } return result