Skip to content

Stop capturing a Context in KnownBTypes#26174

Open
SolalPirelli wants to merge 15 commits into
scala:mainfrom
dotty-staging:solal/backendutils
Open

Stop capturing a Context in KnownBTypes#26174
SolalPirelli wants to merge 15 commits into
scala:mainfrom
dotty-staging:solal/backendutils

Conversation

@SolalPirelli
Copy link
Copy Markdown
Contributor

@SolalPirelli SolalPirelli commented May 28, 2026

Part of #25218

Next step towards cleaning up the backend and eventually enabling parallel classfile emitting / optimization with confidence.

Review commit by commit, they're all fairly straightforward.

This PR:

  • Eagerly loads well-known BTypes
  • Splits said well-known BTypes into those required for codegen and those only for the optimizer
  • Ensures the latter don't need to be loaded if the optimizer is disabled
  • Splits BackendUtils into AnalysisUtils, OptimizerUtils, and the few things not for the optimizer moved somewhere else
  • Removes PostProcessorFrontendAccess.Lazy as it is no longer needed

Next steps:

  • Refactor the optimizer warning logic so we don't need PPFA, which is the last thing that captures a Context after this and Stop capturing a Context in ScalaPrimitives #26177
  • Refactor the logic around storing the call graph & the INDY lambda tracking so we can have a fully layered architecture instead of the current awkward circular dependency for loading inline info

How much have you relied on LLM-based tools in this contribution?

Not at all

How was the solution tested?

Covered by existing tests (this is a refactoring)

@SolalPirelli SolalPirelli requested a review from lrytz May 28, 2026 09:34
Copy link
Copy Markdown
Member

@lrytz lrytz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!


def close(): Unit =
protected def runLocalOptimizations(classNode: ClassNode): Unit =
() // no optimizations by default
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, PostProcessorWithOptimizations is only created when global optimizations are enabled. But we should still run local optimizations (if enabled) in the plain PostProcessor, no?

In Scala 2, dce/removeUnreachableCode is enabled by default. Is that also the case in Scala 3?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of them are enabled by default, though we remove unused line numbers unconditionally (I'm not sure if that can have any effect in practice if no opts have run).

}
}

final class PostProcessorWithOptimizations(frontendAccess: PostProcessorFrontendAccess,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PostProcessorWithGlobalOptimizations?

if _codeGen eq null then
_codeGen = new CodeGen(backendUtils, primitives, frontendAccess, callGraph, bTypeLoader, wellKnownBTypes, generatedClassHandler)
val cg = Option.when(ctx.settings.optInlineEnabled || ctx.settings.optClosureInvocations)(callGraph)
_codeGen = new CodeGen(primitives, cg, bTypeLoader, knownBTypes, generatedClassHandler)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the optimizer is enabled, should we pass optimizerKnownBTypes here? Otherwise the btypes in the base class KnownBTypes are initialized twice. I guess correctness is OK because ClassBTypes are interened, so we get the same instances in both and reference equality holds.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try, just need to double-check it won't cause anything to be double-initialized due to cycles either. I really need to refactor the collection of indy lambdas / call graph edges so we don't need all this ceremony to instantiate things...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, I changed knownBTypes to return the optimizer version when enabled

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants