Skip to content

Commit 18c7da5

Browse files
ivandev0Space Team
authored and
Space Team
committed
[CLI] Minimize nesting in K2JsIrCompiler
#KT-67473
1 parent 4e4a588 commit 18c7da5

File tree

1 file changed

+111
-111
lines changed

1 file changed

+111
-111
lines changed

compiler/cli/cli-js/src/org/jetbrains/kotlin/cli/js/K2JsIrCompiler.kt

+111-111
Original file line numberDiff line numberDiff line change
@@ -327,135 +327,135 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
327327

328328
}
329329

330-
if (arguments.irProduceJs) {
331-
messageCollector.report(INFO, "Produce executable: $outputDirPath")
332-
messageCollector.report(INFO, "Cache directory: ${arguments.cacheDirectory}")
333-
334-
if (icCaches != null) {
335-
val beforeIc2Js = System.currentTimeMillis()
336-
337-
// We use one cache directory for both caches: JS AST and JS code.
338-
// This guard MUST be unlocked after a successful preparing icCaches (see prepareIcCaches()).
339-
// Do not use IncrementalCacheGuard::acquire() - it may drop an entire cache here, and
340-
// it breaks the logic from JsExecutableProducer(), therefore use IncrementalCacheGuard::tryAcquire() instead
341-
// TODO: One day, when we will lower IR and produce JS AST per module,
342-
// think about using different directories for JS AST and JS code.
343-
icCaches.cacheGuard.tryAcquire()
344-
345-
val jsExecutableProducer = JsExecutableProducer(
346-
mainModuleName = moduleName,
347-
moduleKind = moduleKind,
348-
sourceMapsInfo = SourceMapsInfo.from(configurationJs),
349-
caches = icCaches.artifacts,
350-
relativeRequirePath = true
351-
)
352-
353-
val (outputs, rebuiltModules) = jsExecutableProducer.buildExecutable(arguments.granularity, outJsProgram = false)
354-
outputs.writeAll(outputDir, outputName, arguments.dtsStrategy, moduleName, moduleKind)
330+
if (!arguments.irProduceJs) return OK
331+
332+
messageCollector.report(INFO, "Produce executable: $outputDirPath")
333+
messageCollector.report(INFO, "Cache directory: ${arguments.cacheDirectory}")
334+
335+
if (icCaches != null) {
336+
val beforeIc2Js = System.currentTimeMillis()
337+
338+
// We use one cache directory for both caches: JS AST and JS code.
339+
// This guard MUST be unlocked after a successful preparing icCaches (see prepareIcCaches()).
340+
// Do not use IncrementalCacheGuard::acquire() - it may drop an entire cache here, and
341+
// it breaks the logic from JsExecutableProducer(), therefore use IncrementalCacheGuard::tryAcquire() instead
342+
// TODO: One day, when we will lower IR and produce JS AST per module,
343+
// think about using different directories for JS AST and JS code.
344+
icCaches.cacheGuard.tryAcquire()
345+
346+
val jsExecutableProducer = JsExecutableProducer(
347+
mainModuleName = moduleName,
348+
moduleKind = moduleKind,
349+
sourceMapsInfo = SourceMapsInfo.from(configurationJs),
350+
caches = icCaches.artifacts,
351+
relativeRequirePath = true
352+
)
355353

356-
icCaches.cacheGuard.release()
354+
val (outputs, rebuiltModules) = jsExecutableProducer.buildExecutable(arguments.granularity, outJsProgram = false)
355+
outputs.writeAll(outputDir, outputName, arguments.dtsStrategy, moduleName, moduleKind)
357356

358-
messageCollector.report(INFO, "Executable production duration (IC): ${System.currentTimeMillis() - beforeIc2Js}ms")
359-
for ((event, duration) in jsExecutableProducer.getStopwatchLaps()) {
360-
messageCollector.report(INFO, " $event: ${(duration / 1e6).toInt()}ms")
361-
}
357+
icCaches.cacheGuard.release()
362358

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

367-
return OK
364+
for (module in rebuiltModules) {
365+
messageCollector.report(INFO, "IC module builder rebuilt JS for module [${File(module).name}]")
368366
}
369367

370-
val phaseConfig = createPhaseConfig(jsPhases, arguments, messageCollector)
368+
return OK
369+
}
371370

372-
val module = if (includes != null) {
373-
if (sourcesFiles.isNotEmpty()) {
374-
messageCollector.report(ERROR, "Source files are not supported when -Xinclude is present")
375-
}
376-
val includesPath = File(includes).canonicalPath
377-
val mainLibPath = libraries.find { File(it).canonicalPath == includesPath }
378-
?: error("No library with name $includes ($includesPath) found")
379-
val kLib = MainModule.Klib(mainLibPath)
380-
ModulesStructure(
381-
projectJs,
382-
kLib,
383-
configurationJs,
384-
libraries,
385-
friendLibraries
386-
)
387-
} else {
388-
sourceModule!!
371+
val phaseConfig = createPhaseConfig(jsPhases, arguments, messageCollector)
372+
373+
val module = if (includes != null) {
374+
if (sourcesFiles.isNotEmpty()) {
375+
messageCollector.report(ERROR, "Source files are not supported when -Xinclude is present")
389376
}
377+
val includesPath = File(includes).canonicalPath
378+
val mainLibPath = libraries.find { File(it).canonicalPath == includesPath }
379+
?: error("No library with name $includes ($includesPath) found")
380+
val kLib = MainModule.Klib(mainLibPath)
381+
ModulesStructure(
382+
projectJs,
383+
kLib,
384+
configurationJs,
385+
libraries,
386+
friendLibraries
387+
)
388+
} else {
389+
sourceModule!!
390+
}
390391

391-
if (arguments.wasm) {
392-
val generateDts = configuration.getBoolean(JSConfigurationKeys.GENERATE_DTS)
393-
val generateSourceMaps = configuration.getBoolean(JSConfigurationKeys.SOURCE_MAP)
394-
395-
val (allModules, backendContext, typeScriptFragment) = compileToLoweredIr(
396-
depsDescriptors = module,
397-
phaseConfig = createPhaseConfig(wasmPhases, arguments, messageCollector),
398-
irFactory = IrFactoryImpl,
399-
exportedDeclarations = setOf(FqName("main")),
400-
generateTypeScriptFragment = generateDts,
401-
propertyLazyInitialization = arguments.irPropertyLazyInitialization,
402-
)
403-
val dceDumpNameCache = DceDumpNameCache()
404-
if (arguments.irDce) {
405-
eliminateDeadDeclarations(allModules, backendContext, dceDumpNameCache)
406-
}
392+
if (arguments.wasm) {
393+
val generateDts = configuration.getBoolean(JSConfigurationKeys.GENERATE_DTS)
394+
val generateSourceMaps = configuration.getBoolean(JSConfigurationKeys.SOURCE_MAP)
395+
396+
val (allModules, backendContext, typeScriptFragment) = compileToLoweredIr(
397+
depsDescriptors = module,
398+
phaseConfig = createPhaseConfig(wasmPhases, arguments, messageCollector),
399+
irFactory = IrFactoryImpl,
400+
exportedDeclarations = setOf(FqName("main")),
401+
generateTypeScriptFragment = generateDts,
402+
propertyLazyInitialization = arguments.irPropertyLazyInitialization,
403+
)
404+
val dceDumpNameCache = DceDumpNameCache()
405+
if (arguments.irDce) {
406+
eliminateDeadDeclarations(allModules, backendContext, dceDumpNameCache)
407+
}
407408

408-
dumpDeclarationIrSizesIfNeed(arguments.irDceDumpDeclarationIrSizesToFile, allModules, dceDumpNameCache)
409+
dumpDeclarationIrSizesIfNeed(arguments.irDceDumpDeclarationIrSizesToFile, allModules, dceDumpNameCache)
409410

410-
val res = compileWasm(
411-
allModules = allModules,
412-
backendContext = backendContext,
413-
typeScriptFragment = typeScriptFragment,
414-
baseFileName = outputName,
415-
emitNameSection = arguments.wasmDebug,
416-
allowIncompleteImplementations = arguments.irDce,
411+
val res = compileWasm(
412+
allModules = allModules,
413+
backendContext = backendContext,
414+
typeScriptFragment = typeScriptFragment,
415+
baseFileName = outputName,
416+
emitNameSection = arguments.wasmDebug,
417+
allowIncompleteImplementations = arguments.irDce,
417418
generateWat = configuration.get(WasmConfigurationKeys.WASM_GENERATE_WAT, false),
418-
generateSourceMaps = generateSourceMaps,
419-
)
419+
generateSourceMaps = generateSourceMaps,
420+
)
420421

421-
writeCompilationResult(
422-
result = res,
423-
dir = outputDir,
424-
fileNameBase = outputName,
425-
)
422+
writeCompilationResult(
423+
result = res,
424+
dir = outputDir,
425+
fileNameBase = outputName,
426+
)
426427

427-
return OK
428-
} else {
429-
if (arguments.irDceDumpReachabilityInfoToFile != null) {
430-
messageCollector.report(STRONG_WARNING, "Dumping the reachability info to file is supported only for Kotlin/Wasm.")
431-
}
432-
if (arguments.irDceDumpDeclarationIrSizesToFile != null) {
433-
messageCollector.report(STRONG_WARNING, "Dumping the size of declarations to file is supported only for Kotlin/Wasm.")
434-
}
428+
return OK
429+
} else {
430+
if (arguments.irDceDumpReachabilityInfoToFile != null) {
431+
messageCollector.report(STRONG_WARNING, "Dumping the reachability info to file is supported only for Kotlin/Wasm.")
432+
}
433+
if (arguments.irDceDumpDeclarationIrSizesToFile != null) {
434+
messageCollector.report(STRONG_WARNING, "Dumping the size of declarations to file is supported only for Kotlin/Wasm.")
435435
}
436+
}
436437

437-
val start = System.currentTimeMillis()
438+
val start = System.currentTimeMillis()
438439

439-
try {
440-
val ir2JsTransformer = Ir2JsTransformer(arguments, module, phaseConfig, messageCollector, mainCallArguments)
441-
val outputs = ir2JsTransformer.compileAndTransformIrNew()
442-
443-
messageCollector.report(INFO, "Executable production duration: ${System.currentTimeMillis() - start}ms")
444-
445-
outputs.writeAll(outputDir, outputName, arguments.dtsStrategy, moduleName, moduleKind)
446-
} catch (e: CompilationException) {
447-
messageCollector.report(
448-
ERROR,
449-
e.stackTraceToString(),
450-
CompilerMessageLocation.create(
451-
path = e.path,
452-
line = e.line,
453-
column = e.column,
454-
lineContent = e.content
455-
)
440+
try {
441+
val ir2JsTransformer = Ir2JsTransformer(arguments, module, phaseConfig, messageCollector, mainCallArguments)
442+
val outputs = ir2JsTransformer.compileAndTransformIrNew()
443+
444+
messageCollector.report(INFO, "Executable production duration: ${System.currentTimeMillis() - start}ms")
445+
446+
outputs.writeAll(outputDir, outputName, arguments.dtsStrategy, moduleName, moduleKind)
447+
} catch (e: CompilationException) {
448+
messageCollector.report(
449+
ERROR,
450+
e.stackTraceToString(),
451+
CompilerMessageLocation.create(
452+
path = e.path,
453+
line = e.line,
454+
column = e.column,
455+
lineContent = e.content
456456
)
457-
return INTERNAL_ERROR
458-
}
457+
)
458+
return INTERNAL_ERROR
459459
}
460460

461461
return OK

0 commit comments

Comments
 (0)