From 9760e3ee1e97c59e2e39ea0b7d4d12039fd95d28 Mon Sep 17 00:00:00 2001 From: Zalim Bashorov Date: Thu, 30 Mar 2023 20:50:07 +0200 Subject: [PATCH] [Wasm] Introduce `check` and `error` specialized for String to reduce output size --- compiler/testData/codegen/box/size/add.kt | 4 +-- .../testData/codegen/box/size/helloWorld.kt | 2 +- .../codegen/box/size/helloWorldDOM.kt | 4 +-- compiler/testData/codegen/box/size/ok.kt | 4 +-- .../wasm/src/kotlin/util/PreconditionsWasm.kt | 28 +++++++++++++++++++ 5 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 libraries/stdlib/wasm/src/kotlin/util/PreconditionsWasm.kt diff --git a/compiler/testData/codegen/box/size/add.kt b/compiler/testData/codegen/box/size/add.kt index 3a160b4105d..00e08c6584d 100644 --- a/compiler/testData/codegen/box/size/add.kt +++ b/compiler/testData/codegen/box/size/add.kt @@ -1,7 +1,7 @@ // TARGET_BACKEND: WASM -// WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 65_583 -// WASM_DCE_EXPECTED_OUTPUT_SIZE: mjs 6_517 +// WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 34_990 +// WASM_DCE_EXPECTED_OUTPUT_SIZE: mjs 6_359 // FILE: test.kt diff --git a/compiler/testData/codegen/box/size/helloWorld.kt b/compiler/testData/codegen/box/size/helloWorld.kt index a2c991f6d48..8de2bba184b 100644 --- a/compiler/testData/codegen/box/size/helloWorld.kt +++ b/compiler/testData/codegen/box/size/helloWorld.kt @@ -1,6 +1,6 @@ // TARGET_BACKEND: WASM -// WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 65_908 +// WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 65_772 // WASM_DCE_EXPECTED_OUTPUT_SIZE: mjs 6_458 fun box(): String { diff --git a/compiler/testData/codegen/box/size/helloWorldDOM.kt b/compiler/testData/codegen/box/size/helloWorldDOM.kt index 6963a039d00..de06e869bd7 100644 --- a/compiler/testData/codegen/box/size/helloWorldDOM.kt +++ b/compiler/testData/codegen/box/size/helloWorldDOM.kt @@ -1,7 +1,7 @@ // TARGET_BACKEND: WASM -// WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 77_603 -// WASM_DCE_EXPECTED_OUTPUT_SIZE: mjs 6_956 +// WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 38_291 +// WASM_DCE_EXPECTED_OUTPUT_SIZE: mjs 6_798 // FILE: test.kt diff --git a/compiler/testData/codegen/box/size/ok.kt b/compiler/testData/codegen/box/size/ok.kt index 33c8ccfbc1e..e03504fbc8b 100644 --- a/compiler/testData/codegen/box/size/ok.kt +++ b/compiler/testData/codegen/box/size/ok.kt @@ -1,6 +1,6 @@ // TARGET_BACKEND: WASM -// WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 65_634 -// WASM_DCE_EXPECTED_OUTPUT_SIZE: mjs 6_389 +// WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 35_038 +// WASM_DCE_EXPECTED_OUTPUT_SIZE: mjs 6_231 fun box() = "OK" \ No newline at end of file diff --git a/libraries/stdlib/wasm/src/kotlin/util/PreconditionsWasm.kt b/libraries/stdlib/wasm/src/kotlin/util/PreconditionsWasm.kt new file mode 100644 index 00000000000..47631c678f1 --- /dev/null +++ b/libraries/stdlib/wasm/src/kotlin/util/PreconditionsWasm.kt @@ -0,0 +1,28 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package kotlin + +import kotlin.contracts.contract + +// Here are functions specialized with String type to avoid marking Any.toString and its overrides as reachable in DCE. +// Just making the declarations public is impossible since it may change the resolution in a user code. +// TODO: investigate other ways to achieve the same, preferably covering not stdlib only but user cases too. + +@PublishedApi +@kotlin.internal.InlineOnly +internal inline fun check(value: Boolean, lazyMessage: () -> String): Unit { + contract { + returns() implies value + } + if (!value) { + val message = lazyMessage() + throw IllegalStateException(message) + } +} + +@PublishedApi +@kotlin.internal.InlineOnly +internal inline fun error(message: String): Nothing = throw IllegalStateException(message)