Skip to content

Commit

Permalink
[Wasm] #1
Browse files Browse the repository at this point in the history
  • Loading branch information
ilgonmic committed Apr 4, 2024
1 parent 5d7c24a commit 2624ad7
Show file tree
Hide file tree
Showing 17 changed files with 126 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import org.jetbrains.kotlin.platform.jvm.JvmPlatforms.allJvmPlatforms
import org.jetbrains.kotlin.platform.jvm.JvmPlatforms.unspecifiedJvmPlatform
import org.jetbrains.kotlin.platform.konan.NativePlatforms.allNativePlatforms
import org.jetbrains.kotlin.platform.konan.NativePlatforms.unspecifiedNativePlatform
import org.jetbrains.kotlin.platform.wasm.WasmPlatforms
import org.jetbrains.kotlin.platform.wasm.WasmPlatforms.allWasmPlatforms
import org.jetbrains.kotlin.platform.wasm.WasmPlatforms.unspecifiedWasmPlatform

@Suppress("DEPRECATION_ERROR")
object CommonPlatforms {
Expand All @@ -24,7 +25,7 @@ object CommonPlatforms {
setOf(
unspecifiedJvmPlatform.single(),
defaultJsPlatform.single(),
WasmPlatforms.Default.single(),
unspecifiedWasmPlatform.single(),
unspecifiedNativePlatform.single()
)
), org.jetbrains.kotlin.analyzer.common.CommonPlatform {
Expand All @@ -41,7 +42,7 @@ object CommonPlatforms {
yieldAll(allJvmPlatforms)
yieldAll(allNativePlatforms)
yieldAll(allJsPlatforms)
yield(WasmPlatforms.Default)
yieldAll(allWasmPlatforms)

// TODO(dsavvinov): extensions points?
}.toList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import org.jetbrains.kotlin.wasm.resolve.WasmJsPlatformAnalyzerServices
import org.jetbrains.kotlin.wasm.resolve.WasmWasiPlatformAnalyzerServices

abstract class TopDownAnalyzerFacadeForWasm : AbstractTopDownAnalyzerFacadeForWeb() {
override val platform: TargetPlatform = WasmPlatforms.Default

override fun loadIncrementalCacheMetadata(
incrementalData: IncrementalDataProvider,
moduleContext: ModuleContext,
Expand All @@ -47,9 +45,13 @@ abstract class TopDownAnalyzerFacadeForWasm : AbstractTopDownAnalyzerFacadeForWe
}

object TopDownAnalyzerFacadeForWasmJs : TopDownAnalyzerFacadeForWasm() {
override val platform: TargetPlatform = WasmPlatforms.wasmJs

override val analyzerServices: PlatformDependentAnalyzerServices = WasmJsPlatformAnalyzerServices
}

object TopDownAnalyzerFacadeForWasmWasi : TopDownAnalyzerFacadeForWasm() {
override val platform: TargetPlatform = WasmPlatforms.wasmWasi

override val analyzerServices: PlatformDependentAnalyzerServices = WasmWasiPlatformAnalyzerServices
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,14 @@ inline fun <F> compileModuleToAnalyzedFir(

val mainModuleName = moduleStructure.compilerConfiguration.get(CommonConfigurationKeys.MODULE_NAME)!!
val escapedMainModuleName = Name.special("<$mainModuleName>")
val platform = if (useWasmPlatform) WasmPlatforms.Default else JsPlatforms.defaultJsPlatform
val platformAnalyzerServices = if (useWasmPlatform) {
val (platform, platformAnalyzerServices) = if (useWasmPlatform) {
when (moduleStructure.compilerConfiguration.get(WasmConfigurationKeys.WASM_TARGET, WasmTarget.JS)) {
WasmTarget.JS -> WasmJsPlatformAnalyzerServices
WasmTarget.WASI -> WasmWasiPlatformAnalyzerServices
WasmTarget.JS -> WasmPlatforms.wasmJs to WasmJsPlatformAnalyzerServices
WasmTarget.WASI -> WasmPlatforms.wasmWasi to WasmWasiPlatformAnalyzerServices
}
} else JsPlatformAnalyzerServices
} else {
JsPlatforms.defaultJsPlatform to JsPlatformAnalyzerServices
}

val binaryModuleData = BinaryModuleData.initialize(escapedMainModuleName, platform, platformAnalyzerServices)
val dependencyList = DependencyListForCliModule.build(binaryModuleData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,12 @@ fun <F> prepareWasmSessions(
lookupTracker: LookupTracker?,
icData: KlibIcData?,
): List<SessionWithSources<F>> {
val analyzerServices = when (configuration.get(WasmConfigurationKeys.WASM_TARGET, WasmTarget.JS)) {
WasmTarget.JS -> WasmJsPlatformAnalyzerServices
WasmTarget.WASI -> WasmWasiPlatformAnalyzerServices
val (platform, analyzerServices) = when (configuration.get(WasmConfigurationKeys.WASM_TARGET, WasmTarget.JS)) {
WasmTarget.JS -> WasmPlatforms.wasmJs to WasmJsPlatformAnalyzerServices
WasmTarget.WASI -> WasmPlatforms.wasmWasi to WasmWasiPlatformAnalyzerServices
}
return prepareSessions(
files, configuration, rootModuleName, WasmPlatforms.Default, analyzerServices,
files, configuration, rootModuleName, platform, analyzerServices,
metadataCompilationMode = false, libraryList, isCommonSource, isScript = { false },
fileBelongsToModule,
createLibrarySession = { sessionProvider ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import org.jetbrains.kotlin.ir.backend.js.lower.coroutines.JsSuspendFunctionsLow
import org.jetbrains.kotlin.ir.backend.js.lower.inline.RemoveInlineDeclarationsWithReifiedTypeParametersLowering
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
import org.jetbrains.kotlin.ir.interpreter.IrInterpreterConfiguration
import org.jetbrains.kotlin.platform.WasmPlatform
import org.jetbrains.kotlin.platform.toTargetPlatform
import org.jetbrains.kotlin.platform.wasm.WasmPlatforms

private fun List<CompilerPhase<WasmBackendContext, IrModuleFragment, IrModuleFragment>>.toCompilerPhase() =
reduce { acc, lowering -> acc.then(lowering) }
Expand Down Expand Up @@ -618,7 +617,7 @@ val constEvaluationPhase = makeIrModulePhase(
{ context ->
val configuration = IrInterpreterConfiguration(
printOnlyExceptionMessage = true,
platform = WasmPlatform.toTargetPlatform(),
platform = WasmPlatforms.unspecifiedWasmPlatform,
)
ConstEvaluationLowering(context, configuration = configuration)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ package org.jetbrains.kotlin.test.directives

import org.jetbrains.kotlin.config.JvmTarget
import org.jetbrains.kotlin.platform.TargetPlatform
import org.jetbrains.kotlin.platform.WasmPlatform
import org.jetbrains.kotlin.platform.js.JsPlatforms
import org.jetbrains.kotlin.platform.jvm.JdkPlatform
import org.jetbrains.kotlin.platform.jvm.JvmPlatforms
import org.jetbrains.kotlin.platform.konan.NativePlatformUnspecifiedTarget
import org.jetbrains.kotlin.platform.konan.NativePlatforms
import org.jetbrains.kotlin.platform.wasm.WasmPlatformUnspecifiedTarget
import org.jetbrains.kotlin.platform.wasm.WasmPlatforms

enum class TargetPlatformEnum(val targetPlatform: TargetPlatform) {
Expand All @@ -21,7 +21,7 @@ enum class TargetPlatformEnum(val targetPlatform: TargetPlatform) {
setOf(
JdkPlatform(JvmTarget.DEFAULT),
JsPlatforms.DefaultSimpleJsPlatform,
WasmPlatform,
WasmPlatformUnspecifiedTarget,
NativePlatformUnspecifiedTarget
)
)
Expand All @@ -32,6 +32,6 @@ enum class TargetPlatformEnum(val targetPlatform: TargetPlatform) {
JVM_1_8(JvmPlatforms.jvm8),

JS(JsPlatforms.defaultJsPlatform),
Wasm(WasmPlatforms.Default),
Wasm(WasmPlatforms.unspecifiedWasmPlatform),
Native(NativePlatforms.unspecifiedNativePlatform)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package org.jetbrains.kotlin.test.services.impl

import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.platform.CommonPlatforms
import org.jetbrains.kotlin.platform.TargetPlatform
import org.jetbrains.kotlin.platform.js.JsPlatforms
Expand Down Expand Up @@ -377,7 +376,7 @@ class ModuleStructureExtractorImpl(
nameSuffix == "COMMON" -> CommonPlatforms.defaultCommonPlatform
nameSuffix == "JVM" -> JvmPlatforms.unspecifiedJvmPlatform // TODO(dsavvinov): determine JvmTarget precisely
nameSuffix == "JS" -> JsPlatforms.defaultJsPlatform
nameSuffix == "WASM" -> WasmPlatforms.Default
nameSuffix == "WASM" -> WasmPlatforms.unspecifiedWasmPlatform
nameSuffix == "NATIVE" -> NativePlatforms.unspecifiedNativePlatform
nameSuffix.isEmpty() -> null // TODO(dsavvinov): this leads to 'null'-platform in ModuleDescriptor
else -> throw IllegalStateException("Can't determine platform by name $nameSuffix")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package org.jetbrains.kotlin.platform

object WasmPlatform : SimplePlatform("Wasm") {
abstract class WasmPlatform : SimplePlatform("Wasm") {
override val oldFashionedDescription: String
get() = "Wasm "
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@ package org.jetbrains.kotlin.platform.compat
import org.jetbrains.kotlin.config.JvmTarget
import org.jetbrains.kotlin.platform.CommonPlatforms
import org.jetbrains.kotlin.platform.IdePlatform
import org.jetbrains.kotlin.platform.impl.CommonIdePlatformKind
import org.jetbrains.kotlin.platform.impl.JsIdePlatformKind
import org.jetbrains.kotlin.platform.impl.JvmIdePlatformKind
import org.jetbrains.kotlin.platform.impl.NativeIdePlatformKind
import org.jetbrains.kotlin.platform.JsPlatform
import org.jetbrains.kotlin.platform.WasmPlatform
import org.jetbrains.kotlin.platform.impl.WasmIdePlatformKind
import org.jetbrains.kotlin.platform.impl.*
import org.jetbrains.kotlin.platform.js.JsPlatforms
import org.jetbrains.kotlin.platform.jvm.JdkPlatform
import org.jetbrains.kotlin.platform.jvm.JvmPlatform
import org.jetbrains.kotlin.platform.jvm.JvmPlatforms
import org.jetbrains.kotlin.platform.konan.NativePlatforms
import org.jetbrains.kotlin.platform.konan.NativePlatform
import org.jetbrains.kotlin.platform.konan.NativePlatforms
import org.jetbrains.kotlin.platform.wasm.WasmPlatforms

typealias OldPlatform = org.jetbrains.kotlin.resolve.TargetPlatform
Expand Down Expand Up @@ -54,7 +50,7 @@ fun IdePlatform<*, *>.toNewPlatform(): NewPlatform = when (this) {
is CommonIdePlatformKind.Platform -> CommonPlatforms.defaultCommonPlatform
is JvmIdePlatformKind.Platform -> JvmPlatforms.jvmPlatformByTargetVersion(this.version)
is JsIdePlatformKind.Platform -> JsPlatforms.defaultJsPlatform
is WasmIdePlatformKind.Platform -> WasmPlatforms.Default
is WasmIdePlatformKind.Platform -> WasmPlatforms.unspecifiedWasmPlatform
is NativeIdePlatformKind.Platform -> NativePlatforms.unspecifiedNativePlatform
else -> error("Unknown platform $this")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,25 @@ import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments
import org.jetbrains.kotlin.platform.*
import org.jetbrains.kotlin.platform.wasm.WasmPlatforms
import org.jetbrains.kotlin.platform.wasm.WasmTarget

object WasmIdePlatformKind : IdePlatformKind() {
override fun supportsTargetPlatform(platform: TargetPlatform): Boolean = platform.isWasm()

override fun platformByCompilerArguments(arguments: CommonCompilerArguments): TargetPlatform? {
return if (arguments is K2JSCompilerArguments && arguments.wasm)
WasmPlatforms.Default
else
null
return if (arguments is K2JSCompilerArguments && arguments.wasm) {
val wasmTarget = arguments.wasmTarget?.let { WasmTarget.fromName(it) }
wasmTarget?.let {
WasmPlatforms.wasmPlatformByTargetVersion(it)
}
} else null
}

val platforms get() = listOf(WasmPlatforms.Default)
override val defaultPlatform get() = WasmPlatforms.Default
val platforms
get() = WasmTarget.values()
.map { target -> WasmPlatforms.wasmPlatformByTargetVersion(target) } + listOf(WasmPlatforms.unspecifiedWasmPlatform)

override val defaultPlatform get() = WasmPlatforms.unspecifiedWasmPlatform

@Deprecated(
message = "IdePlatform is deprecated and will be removed soon, please, migrate to org.jetbrains.kotlin.platform.TargetPlatform",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,41 @@
package org.jetbrains.kotlin.platform.wasm

import org.jetbrains.kotlin.platform.TargetPlatform
import org.jetbrains.kotlin.platform.WasmPlatform
import org.jetbrains.kotlin.platform.toTargetPlatform
import org.jetbrains.kotlin.platform.WasmPlatform as CoreWasmPlatform

abstract class WasmPlatform : CoreWasmPlatform() {
override val oldFashionedDescription: String
get() = "Wasm"
}

object WasmPlatformUnspecifiedTarget : WasmPlatform() {
override val targetName: String
get() = "general"
}

class WasmPlatformWithTarget(val target: WasmTarget) : WasmPlatform() {
override val targetName: String
get() = target.name
}

object WasmPlatforms {
object Default : TargetPlatform(setOf(WasmPlatform))
}
private val platforms: Map<WasmTarget, TargetPlatform> =
WasmTarget.entries.associateWith { WasmPlatformWithTarget(it).toTargetPlatform() }

@Suppress("DEPRECATION_ERROR")
val unspecifiedWasmPlatform: TargetPlatform
get() = Default

val wasmJs = platforms[WasmTarget.JS]!!
val wasmWasi = platforms[WasmTarget.WASI]!!

fun wasmPlatformByTargetVersion(targetVersion: WasmTarget): TargetPlatform =
platforms[targetVersion]!!

val allWasmPlatforms: List<TargetPlatform> = listOf(unspecifiedWasmPlatform) + platforms.values

object Default : TargetPlatform(setOf(WasmPlatformUnspecifiedTarget))
}

fun TargetPlatform?.isWasm(): Boolean = this?.singleOrNull() is WasmPlatform
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,27 @@

package org.jetbrains.kotlin.wasm.test

import org.jetbrains.kotlin.platform.TargetPlatform
import org.jetbrains.kotlin.platform.wasm.WasmPlatforms
import org.jetbrains.kotlin.test.Constructor
import org.jetbrains.kotlin.test.FirParser
import org.jetbrains.kotlin.test.TargetBackend
import org.jetbrains.kotlin.test.backend.ir.IrBackendInput
import org.jetbrains.kotlin.test.builders.*
import org.jetbrains.kotlin.test.builders.TestConfigurationBuilder
import org.jetbrains.kotlin.test.builders.configureFirHandlersStep
import org.jetbrains.kotlin.test.builders.firHandlersStep
import org.jetbrains.kotlin.test.directives.DiagnosticsDirectives
import org.jetbrains.kotlin.test.directives.FirDiagnosticsDirectives
import org.jetbrains.kotlin.test.directives.LanguageSettingsDirectives
import org.jetbrains.kotlin.test.directives.WasmEnvironmentConfigurationDirectives
import org.jetbrains.kotlin.test.frontend.fir.*
import org.jetbrains.kotlin.test.frontend.fir.handlers.*
import org.jetbrains.kotlin.test.frontend.fir.Fir2IrWasmResultsConverter
import org.jetbrains.kotlin.test.frontend.fir.FirFrontendFacade
import org.jetbrains.kotlin.test.frontend.fir.FirMetaInfoDiffSuppressor
import org.jetbrains.kotlin.test.frontend.fir.FirOutputArtifact
import org.jetbrains.kotlin.test.frontend.fir.handlers.FirCfgConsistencyHandler
import org.jetbrains.kotlin.test.frontend.fir.handlers.FirCfgDumpHandler
import org.jetbrains.kotlin.test.frontend.fir.handlers.FirDumpHandler
import org.jetbrains.kotlin.test.frontend.fir.handlers.FirResolvedTypesVerifier
import org.jetbrains.kotlin.test.model.*
import org.jetbrains.kotlin.test.runners.codegen.commonFirHandlersForCodegenTest
import org.jetbrains.kotlin.test.services.AdditionalSourceProvider
Expand All @@ -29,10 +39,11 @@ import org.jetbrains.kotlin.wasm.test.handlers.WasmBoxRunner
import org.jetbrains.kotlin.wasm.test.handlers.WasmDebugRunner

abstract class AbstractFirWasmTest(
targetPlatform: TargetPlatform,
pathToTestDir: String,
testGroupOutputDirPrefix: String,
) : AbstractWasmBlackBoxCodegenTestBase<FirOutputArtifact, IrBackendInput, BinaryArtifacts.KLib>(
FrontendKinds.FIR, TargetBackend.WASM, pathToTestDir, testGroupOutputDirPrefix
FrontendKinds.FIR, TargetBackend.WASM, targetPlatform, pathToTestDir, testGroupOutputDirPrefix
) {
override val frontendFacade: Constructor<FrontendFacade<FirOutputArtifact>>
get() = ::FirFrontendFacade
Expand Down Expand Up @@ -70,7 +81,7 @@ abstract class AbstractFirWasmTest(
open class AbstractFirWasmJsTest(
pathToTestDir: String,
testGroupOutputDirPrefix: String,
) : AbstractFirWasmTest(pathToTestDir, testGroupOutputDirPrefix) {
) : AbstractFirWasmTest(WasmPlatforms.wasmJs, pathToTestDir, testGroupOutputDirPrefix) {
override val wasmBoxTestRunner: Constructor<AnalysisHandler<BinaryArtifacts.Wasm>>
get() = ::WasmBoxRunner

Expand Down Expand Up @@ -128,7 +139,7 @@ open class AbstractFirWasmJsSteppingTest : AbstractFirWasmJsTest(
open class AbstractFirWasmWasiTest(
pathToTestDir: String,
testGroupOutputDirPrefix: String,
) : AbstractFirWasmTest(pathToTestDir, testGroupOutputDirPrefix) {
) : AbstractFirWasmTest(WasmPlatforms.wasmWasi, pathToTestDir, testGroupOutputDirPrefix) {
override val wasmBoxTestRunner: Constructor<AnalysisHandler<BinaryArtifacts.Wasm>>
get() = ::WasiBoxRunner

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

package org.jetbrains.kotlin.wasm.test

import org.jetbrains.kotlin.platform.wasm.WasmPlatforms
import org.jetbrains.kotlin.test.Constructor
import org.jetbrains.kotlin.test.TargetBackend
import org.jetbrains.kotlin.test.backend.ir.IrBackendInput
import org.jetbrains.kotlin.test.builders.TestConfigurationBuilder
import org.jetbrains.kotlin.test.builders.wasmArtifactsHandlersStep
import org.jetbrains.kotlin.test.directives.WasmEnvironmentConfigurationDirectives
import org.jetbrains.kotlin.test.frontend.classic.ClassicFrontend2IrConverter
import org.jetbrains.kotlin.test.frontend.classic.ClassicFrontendFacade
Expand All @@ -27,7 +27,7 @@ abstract class AbstractK1WasmTest(
pathToTestDir: String,
testGroupOutputDirPrefix: String,
) : AbstractWasmBlackBoxCodegenTestBase<ClassicFrontendOutputArtifact, IrBackendInput, BinaryArtifacts.KLib>(
FrontendKinds.ClassicFrontend, TargetBackend.WASM, pathToTestDir, testGroupOutputDirPrefix
FrontendKinds.ClassicFrontend, TargetBackend.WASM, WasmPlatforms.wasmJs, pathToTestDir, testGroupOutputDirPrefix
) {
override val frontendFacade: Constructor<FrontendFacade<ClassicFrontendOutputArtifact>>
get() = ::ClassicFrontendFacade
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

package org.jetbrains.kotlin.wasm.test

import org.jetbrains.kotlin.platform.wasm.WasmPlatforms
import org.jetbrains.kotlin.test.Constructor
import org.jetbrains.kotlin.test.TargetBackend
import org.jetbrains.kotlin.test.backend.ir.IrBackendInput
import org.jetbrains.kotlin.test.builders.TestConfigurationBuilder
import org.jetbrains.kotlin.test.frontend.classic.ClassicFrontend2IrConverter
import org.jetbrains.kotlin.test.frontend.classic.ClassicFrontendFacade
import org.jetbrains.kotlin.test.frontend.classic.ClassicFrontendOutputArtifact
Expand All @@ -24,7 +24,7 @@ abstract class AbstractK1WasmWasiTest(
pathToTestDir: String,
testGroupOutputDirPrefix: String,
) : AbstractWasmBlackBoxCodegenTestBase<ClassicFrontendOutputArtifact, IrBackendInput, BinaryArtifacts.KLib>(
FrontendKinds.ClassicFrontend, TargetBackend.WASM, pathToTestDir, testGroupOutputDirPrefix
FrontendKinds.ClassicFrontend, TargetBackend.WASM, WasmPlatforms.wasmWasi, pathToTestDir, testGroupOutputDirPrefix
) {
override val frontendFacade: Constructor<FrontendFacade<ClassicFrontendOutputArtifact>>
get() = ::ClassicFrontendFacade
Expand Down
Loading

0 comments on commit 2624ad7

Please sign in to comment.