Skip to content

Commit 3b5b98a

Browse files
ivandev0Space Team
authored and
Space Team
committed
[CLI] Minimize nesting in K2JsIrCompiler
#KT-67473 (cherry picked from commit 18c7da5)
1 parent 0206b52 commit 3b5b98a

File tree

1 file changed

+102
-102
lines changed

1 file changed

+102
-102
lines changed

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

+102-102
Original file line numberDiff line numberDiff line change
@@ -286,134 +286,134 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
286286

287287
}
288288

289-
if (arguments.irProduceJs) {
290-
val moduleKind = configurationJs[JSConfigurationKeys.MODULE_KIND] ?: error("cannot get 'module kind' from configuration")
291-
292-
messageCollector.report(INFO, "Produce executable: $outputDirPath")
293-
messageCollector.report(INFO, "Cache directory: ${arguments.cacheDirectory}")
294-
295-
if (icCaches != null) {
296-
val beforeIc2Js = System.currentTimeMillis()
297-
298-
// We use one cache directory for both caches: JS AST and JS code.
299-
// This guard MUST be unlocked after a successful preparing icCaches (see prepareIcCaches()).
300-
// Do not use IncrementalCacheGuard::acquire() - it may drop an entire cache here, and
301-
// it breaks the logic from JsExecutableProducer(), therefore use IncrementalCacheGuard::tryAcquire() instead
302-
// TODO: One day, when we will lower IR and produce JS AST per module,
303-
// think about using different directories for JS AST and JS code.
304-
icCaches.cacheGuard.tryAcquire()
305-
306-
val jsExecutableProducer = JsExecutableProducer(
307-
mainModuleName = moduleName,
308-
moduleKind = moduleKind,
309-
sourceMapsInfo = SourceMapsInfo.from(configurationJs),
310-
caches = icCaches.artifacts,
311-
relativeRequirePath = true
312-
)
289+
if (!arguments.irProduceJs) return OK
290+
291+
val moduleKind = configurationJs[JSConfigurationKeys.MODULE_KIND] ?: error("cannot get 'module kind' from configuration")
292+
293+
messageCollector.report(INFO, "Produce executable: $outputDirPath")
294+
messageCollector.report(INFO, "Cache directory: ${arguments.cacheDirectory}")
295+
296+
if (icCaches != null) {
297+
val beforeIc2Js = System.currentTimeMillis()
298+
299+
// We use one cache directory for both caches: JS AST and JS code.
300+
// This guard MUST be unlocked after a successful preparing icCaches (see prepareIcCaches()).
301+
// Do not use IncrementalCacheGuard::acquire() - it may drop an entire cache here, and
302+
// it breaks the logic from JsExecutableProducer(), therefore use IncrementalCacheGuard::tryAcquire() instead
303+
// TODO: One day, when we will lower IR and produce JS AST per module,
304+
// think about using different directories for JS AST and JS code.
305+
icCaches.cacheGuard.tryAcquire()
306+
307+
val jsExecutableProducer = JsExecutableProducer(
308+
mainModuleName = moduleName,
309+
moduleKind = moduleKind,
310+
sourceMapsInfo = SourceMapsInfo.from(configurationJs),
311+
caches = icCaches.artifacts,
312+
relativeRequirePath = true
313+
)
313314

314-
val (outputs, rebuiltModules) = jsExecutableProducer.buildExecutable(arguments.granularity, outJsProgram = false)
315+
val (outputs, rebuiltModules) = jsExecutableProducer.buildExecutable(arguments.granularity, outJsProgram = false)
315316
outputs.writeAll(outputDir, outputName, arguments.generateDts, moduleName, moduleKind)
316317

317-
icCaches.cacheGuard.release()
318+
icCaches.cacheGuard.release()
318319

319-
messageCollector.report(INFO, "Executable production duration (IC): ${System.currentTimeMillis() - beforeIc2Js}ms")
320-
for ((event, duration) in jsExecutableProducer.getStopwatchLaps()) {
321-
messageCollector.report(INFO, " $event: ${(duration / 1e6).toInt()}ms")
322-
}
323-
324-
for (module in rebuiltModules) {
325-
messageCollector.report(INFO, "IC module builder rebuilt JS for module [${File(module).name}]")
326-
}
320+
messageCollector.report(INFO, "Executable production duration (IC): ${System.currentTimeMillis() - beforeIc2Js}ms")
321+
for ((event, duration) in jsExecutableProducer.getStopwatchLaps()) {
322+
messageCollector.report(INFO, " $event: ${(duration / 1e6).toInt()}ms")
323+
}
327324

328-
return OK
325+
for (module in rebuiltModules) {
326+
messageCollector.report(INFO, "IC module builder rebuilt JS for module [${File(module).name}]")
329327
}
330328

331-
val phaseConfig = createPhaseConfig(jsPhases, arguments, messageCollector)
329+
return OK
330+
}
332331

333-
val module = if (includes != null) {
334-
if (sourcesFiles.isNotEmpty()) {
335-
messageCollector.report(ERROR, "Source files are not supported when -Xinclude is present")
336-
}
337-
val includesPath = File(includes).canonicalPath
338-
val mainLibPath = libraries.find { File(it).canonicalPath == includesPath }
339-
?: error("No library with name $includes ($includesPath) found")
340-
val kLib = MainModule.Klib(mainLibPath)
341-
ModulesStructure(
342-
projectJs,
343-
kLib,
344-
configurationJs,
345-
libraries,
346-
friendLibraries
347-
)
348-
} else {
349-
sourceModule!!
332+
val phaseConfig = createPhaseConfig(jsPhases, arguments, messageCollector)
333+
334+
val module = if (includes != null) {
335+
if (sourcesFiles.isNotEmpty()) {
336+
messageCollector.report(ERROR, "Source files are not supported when -Xinclude is present")
350337
}
338+
val includesPath = File(includes).canonicalPath
339+
val mainLibPath = libraries.find { File(it).canonicalPath == includesPath }
340+
?: error("No library with name $includes ($includesPath) found")
341+
val kLib = MainModule.Klib(mainLibPath)
342+
ModulesStructure(
343+
projectJs,
344+
kLib,
345+
configurationJs,
346+
libraries,
347+
friendLibraries
348+
)
349+
} else {
350+
sourceModule!!
351+
}
351352

352-
if (arguments.wasm) {
353+
if (arguments.wasm) {
353354
val (allModules, backendContext) = compileToLoweredIr(
354-
depsDescriptors = module,
355-
phaseConfig = createPhaseConfig(wasmPhases, arguments, messageCollector),
356-
irFactory = IrFactoryImpl,
357-
exportedDeclarations = setOf(FqName("main")),
358-
propertyLazyInitialization = arguments.irPropertyLazyInitialization,
359-
)
360-
val dceDumpNameCache = DceDumpNameCache()
361-
if (arguments.irDce) {
362-
eliminateDeadDeclarations(allModules, backendContext, dceDumpNameCache)
363-
}
355+
depsDescriptors = module,
356+
phaseConfig = createPhaseConfig(wasmPhases, arguments, messageCollector),
357+
irFactory = IrFactoryImpl,
358+
exportedDeclarations = setOf(FqName("main")),
359+
propertyLazyInitialization = arguments.irPropertyLazyInitialization,
360+
)
361+
val dceDumpNameCache = DceDumpNameCache()
362+
if (arguments.irDce) {
363+
eliminateDeadDeclarations(allModules, backendContext, dceDumpNameCache)
364+
}
364365

365-
dumpDeclarationIrSizesIfNeed(arguments.irDceDumpDeclarationIrSizesToFile, allModules, dceDumpNameCache)
366+
dumpDeclarationIrSizesIfNeed(arguments.irDceDumpDeclarationIrSizesToFile, allModules, dceDumpNameCache)
366367

367368
val generateSourceMaps = configuration.getBoolean(JSConfigurationKeys.SOURCE_MAP)
368369

369-
val res = compileWasm(
370-
allModules = allModules,
371-
backendContext = backendContext,
372-
baseFileName = outputName,
373-
emitNameSection = arguments.wasmDebug,
374-
allowIncompleteImplementations = arguments.irDce,
370+
val res = compileWasm(
371+
allModules = allModules,
372+
backendContext = backendContext,
373+
baseFileName = outputName,
374+
emitNameSection = arguments.wasmDebug,
375+
allowIncompleteImplementations = arguments.irDce,
375376
generateWat = configuration.get(JSConfigurationKeys.WASM_GENERATE_WAT, false),
376377
generateSourceMaps = generateSourceMaps
377-
)
378+
)
378379

379-
writeCompilationResult(
380-
result = res,
381-
dir = outputDir,
382-
fileNameBase = outputName,
383-
)
380+
writeCompilationResult(
381+
result = res,
382+
dir = outputDir,
383+
fileNameBase = outputName,
384+
)
384385

385-
return OK
386-
} else {
387-
if (arguments.irDceDumpReachabilityInfoToFile != null) {
388-
messageCollector.report(STRONG_WARNING, "Dumping the reachability info to file is supported only for Kotlin/Wasm.")
389-
}
390-
if (arguments.irDceDumpDeclarationIrSizesToFile != null) {
391-
messageCollector.report(STRONG_WARNING, "Dumping the size of declarations to file is supported only for Kotlin/Wasm.")
392-
}
386+
return OK
387+
} else {
388+
if (arguments.irDceDumpReachabilityInfoToFile != null) {
389+
messageCollector.report(STRONG_WARNING, "Dumping the reachability info to file is supported only for Kotlin/Wasm.")
390+
}
391+
if (arguments.irDceDumpDeclarationIrSizesToFile != null) {
392+
messageCollector.report(STRONG_WARNING, "Dumping the size of declarations to file is supported only for Kotlin/Wasm.")
393393
}
394+
}
394395

395-
val start = System.currentTimeMillis()
396+
val start = System.currentTimeMillis()
396397

397-
try {
398-
val ir2JsTransformer = Ir2JsTransformer(arguments, module, phaseConfig, messageCollector, mainCallArguments)
399-
val outputs = ir2JsTransformer.compileAndTransformIrNew()
398+
try {
399+
val ir2JsTransformer = Ir2JsTransformer(arguments, module, phaseConfig, messageCollector, mainCallArguments)
400+
val outputs = ir2JsTransformer.compileAndTransformIrNew()
400401

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

403404
outputs.writeAll(outputDir, outputName, arguments.generateDts, moduleName, moduleKind)
404-
} catch (e: CompilationException) {
405-
messageCollector.report(
406-
ERROR,
407-
e.stackTraceToString(),
408-
CompilerMessageLocation.create(
409-
path = e.path,
410-
line = e.line,
411-
column = e.column,
412-
lineContent = e.content
413-
)
405+
} catch (e: CompilationException) {
406+
messageCollector.report(
407+
ERROR,
408+
e.stackTraceToString(),
409+
CompilerMessageLocation.create(
410+
path = e.path,
411+
line = e.line,
412+
column = e.column,
413+
lineContent = e.content
414414
)
415-
return INTERNAL_ERROR
416-
}
415+
)
416+
return INTERNAL_ERROR
417417
}
418418

419419
return OK

0 commit comments

Comments
 (0)