Skip to content

Commit

Permalink
[CLI] Minimize nesting in K2JsIrCompiler
Browse files Browse the repository at this point in the history
#KT-67473

(cherry picked from commit 18c7da5)
  • Loading branch information
ivandev0 authored and Space Team committed Apr 30, 2024
1 parent 0206b52 commit 3b5b98a
Showing 1 changed file with 102 additions and 102 deletions.
204 changes: 102 additions & 102 deletions compiler/cli/cli-js/src/org/jetbrains/kotlin/cli/js/K2JsIrCompiler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -286,134 +286,134 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {

}

if (arguments.irProduceJs) {
val moduleKind = configurationJs[JSConfigurationKeys.MODULE_KIND] ?: error("cannot get 'module kind' from configuration")

messageCollector.report(INFO, "Produce executable: $outputDirPath")
messageCollector.report(INFO, "Cache directory: ${arguments.cacheDirectory}")

if (icCaches != null) {
val beforeIc2Js = System.currentTimeMillis()

// We use one cache directory for both caches: JS AST and JS code.
// This guard MUST be unlocked after a successful preparing icCaches (see prepareIcCaches()).
// Do not use IncrementalCacheGuard::acquire() - it may drop an entire cache here, and
// it breaks the logic from JsExecutableProducer(), therefore use IncrementalCacheGuard::tryAcquire() instead
// TODO: One day, when we will lower IR and produce JS AST per module,
// think about using different directories for JS AST and JS code.
icCaches.cacheGuard.tryAcquire()

val jsExecutableProducer = JsExecutableProducer(
mainModuleName = moduleName,
moduleKind = moduleKind,
sourceMapsInfo = SourceMapsInfo.from(configurationJs),
caches = icCaches.artifacts,
relativeRequirePath = true
)
if (!arguments.irProduceJs) return OK

val moduleKind = configurationJs[JSConfigurationKeys.MODULE_KIND] ?: error("cannot get 'module kind' from configuration")

messageCollector.report(INFO, "Produce executable: $outputDirPath")
messageCollector.report(INFO, "Cache directory: ${arguments.cacheDirectory}")

if (icCaches != null) {
val beforeIc2Js = System.currentTimeMillis()

// We use one cache directory for both caches: JS AST and JS code.
// This guard MUST be unlocked after a successful preparing icCaches (see prepareIcCaches()).
// Do not use IncrementalCacheGuard::acquire() - it may drop an entire cache here, and
// it breaks the logic from JsExecutableProducer(), therefore use IncrementalCacheGuard::tryAcquire() instead
// TODO: One day, when we will lower IR and produce JS AST per module,
// think about using different directories for JS AST and JS code.
icCaches.cacheGuard.tryAcquire()

val jsExecutableProducer = JsExecutableProducer(
mainModuleName = moduleName,
moduleKind = moduleKind,
sourceMapsInfo = SourceMapsInfo.from(configurationJs),
caches = icCaches.artifacts,
relativeRequirePath = true
)

val (outputs, rebuiltModules) = jsExecutableProducer.buildExecutable(arguments.granularity, outJsProgram = false)
val (outputs, rebuiltModules) = jsExecutableProducer.buildExecutable(arguments.granularity, outJsProgram = false)
outputs.writeAll(outputDir, outputName, arguments.generateDts, moduleName, moduleKind)

icCaches.cacheGuard.release()
icCaches.cacheGuard.release()

messageCollector.report(INFO, "Executable production duration (IC): ${System.currentTimeMillis() - beforeIc2Js}ms")
for ((event, duration) in jsExecutableProducer.getStopwatchLaps()) {
messageCollector.report(INFO, " $event: ${(duration / 1e6).toInt()}ms")
}

for (module in rebuiltModules) {
messageCollector.report(INFO, "IC module builder rebuilt JS for module [${File(module).name}]")
}
messageCollector.report(INFO, "Executable production duration (IC): ${System.currentTimeMillis() - beforeIc2Js}ms")
for ((event, duration) in jsExecutableProducer.getStopwatchLaps()) {
messageCollector.report(INFO, " $event: ${(duration / 1e6).toInt()}ms")
}

return OK
for (module in rebuiltModules) {
messageCollector.report(INFO, "IC module builder rebuilt JS for module [${File(module).name}]")
}

val phaseConfig = createPhaseConfig(jsPhases, arguments, messageCollector)
return OK
}

val module = if (includes != null) {
if (sourcesFiles.isNotEmpty()) {
messageCollector.report(ERROR, "Source files are not supported when -Xinclude is present")
}
val includesPath = File(includes).canonicalPath
val mainLibPath = libraries.find { File(it).canonicalPath == includesPath }
?: error("No library with name $includes ($includesPath) found")
val kLib = MainModule.Klib(mainLibPath)
ModulesStructure(
projectJs,
kLib,
configurationJs,
libraries,
friendLibraries
)
} else {
sourceModule!!
val phaseConfig = createPhaseConfig(jsPhases, arguments, messageCollector)

val module = if (includes != null) {
if (sourcesFiles.isNotEmpty()) {
messageCollector.report(ERROR, "Source files are not supported when -Xinclude is present")
}
val includesPath = File(includes).canonicalPath
val mainLibPath = libraries.find { File(it).canonicalPath == includesPath }
?: error("No library with name $includes ($includesPath) found")
val kLib = MainModule.Klib(mainLibPath)
ModulesStructure(
projectJs,
kLib,
configurationJs,
libraries,
friendLibraries
)
} else {
sourceModule!!
}

if (arguments.wasm) {
if (arguments.wasm) {
val (allModules, backendContext) = compileToLoweredIr(
depsDescriptors = module,
phaseConfig = createPhaseConfig(wasmPhases, arguments, messageCollector),
irFactory = IrFactoryImpl,
exportedDeclarations = setOf(FqName("main")),
propertyLazyInitialization = arguments.irPropertyLazyInitialization,
)
val dceDumpNameCache = DceDumpNameCache()
if (arguments.irDce) {
eliminateDeadDeclarations(allModules, backendContext, dceDumpNameCache)
}
depsDescriptors = module,
phaseConfig = createPhaseConfig(wasmPhases, arguments, messageCollector),
irFactory = IrFactoryImpl,
exportedDeclarations = setOf(FqName("main")),
propertyLazyInitialization = arguments.irPropertyLazyInitialization,
)
val dceDumpNameCache = DceDumpNameCache()
if (arguments.irDce) {
eliminateDeadDeclarations(allModules, backendContext, dceDumpNameCache)
}

dumpDeclarationIrSizesIfNeed(arguments.irDceDumpDeclarationIrSizesToFile, allModules, dceDumpNameCache)
dumpDeclarationIrSizesIfNeed(arguments.irDceDumpDeclarationIrSizesToFile, allModules, dceDumpNameCache)

val generateSourceMaps = configuration.getBoolean(JSConfigurationKeys.SOURCE_MAP)

val res = compileWasm(
allModules = allModules,
backendContext = backendContext,
baseFileName = outputName,
emitNameSection = arguments.wasmDebug,
allowIncompleteImplementations = arguments.irDce,
val res = compileWasm(
allModules = allModules,
backendContext = backendContext,
baseFileName = outputName,
emitNameSection = arguments.wasmDebug,
allowIncompleteImplementations = arguments.irDce,
generateWat = configuration.get(JSConfigurationKeys.WASM_GENERATE_WAT, false),
generateSourceMaps = generateSourceMaps
)
)

writeCompilationResult(
result = res,
dir = outputDir,
fileNameBase = outputName,
)
writeCompilationResult(
result = res,
dir = outputDir,
fileNameBase = outputName,
)

return OK
} else {
if (arguments.irDceDumpReachabilityInfoToFile != null) {
messageCollector.report(STRONG_WARNING, "Dumping the reachability info to file is supported only for Kotlin/Wasm.")
}
if (arguments.irDceDumpDeclarationIrSizesToFile != null) {
messageCollector.report(STRONG_WARNING, "Dumping the size of declarations to file is supported only for Kotlin/Wasm.")
}
return OK
} else {
if (arguments.irDceDumpReachabilityInfoToFile != null) {
messageCollector.report(STRONG_WARNING, "Dumping the reachability info to file is supported only for Kotlin/Wasm.")
}
if (arguments.irDceDumpDeclarationIrSizesToFile != null) {
messageCollector.report(STRONG_WARNING, "Dumping the size of declarations to file is supported only for Kotlin/Wasm.")
}
}

val start = System.currentTimeMillis()
val start = System.currentTimeMillis()

try {
val ir2JsTransformer = Ir2JsTransformer(arguments, module, phaseConfig, messageCollector, mainCallArguments)
val outputs = ir2JsTransformer.compileAndTransformIrNew()
try {
val ir2JsTransformer = Ir2JsTransformer(arguments, module, phaseConfig, messageCollector, mainCallArguments)
val outputs = ir2JsTransformer.compileAndTransformIrNew()

messageCollector.report(INFO, "Executable production duration: ${System.currentTimeMillis() - start}ms")
messageCollector.report(INFO, "Executable production duration: ${System.currentTimeMillis() - start}ms")

outputs.writeAll(outputDir, outputName, arguments.generateDts, moduleName, moduleKind)
} catch (e: CompilationException) {
messageCollector.report(
ERROR,
e.stackTraceToString(),
CompilerMessageLocation.create(
path = e.path,
line = e.line,
column = e.column,
lineContent = e.content
)
} catch (e: CompilationException) {
messageCollector.report(
ERROR,
e.stackTraceToString(),
CompilerMessageLocation.create(
path = e.path,
line = e.line,
column = e.column,
lineContent = e.content
)
return INTERNAL_ERROR
}
)
return INTERNAL_ERROR
}

return OK
Expand Down

0 comments on commit 3b5b98a

Please sign in to comment.