Skip to content

More closely mimic the Clang compilation #5543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from

Conversation

ilya-biryukov
Copy link
Contributor

Instead of using ASTUnit and tooling APIs, directly mimic what Clang does during the compilation.

Run the Clang frontend until the translation unit parsing is done, at which point switch back to Carbon to finish the Check face and interface with Clang through ASTContext and Sema. In lower, finish the corresponding Clang compilation phase, i.e. CodeGen.

Clang does not have the corresponding APIs and instead provides a callback-based mechanism. To map this back to Carbon APIs, we create a separate thread that gives control back to Carbon through the callbacks, and then finishes the compilation when the Carbon code is done.

Although essentially a hack, this allows to easily fit into the Carbon codebase quickly and we should have an option of refactoring Clang code in LLVM upstream if this approach proves fruitful.

Replace this paragraph with a description of what this PR is changing or
adding, and why.

Closes #ISSUE

Instead of using ASTUnit and tooling APIs, directly mimic what Clang
does during the compilation.

Run the Clang frontend until the translation unit parsing is done, at
which point switch back to Carbon to finish the `Check` face and
interface with Clang through `ASTContext` and `Sema`. In lower, finish
the corresponding Clang compilation phase, i.e. CodeGen.

Clang does not have the corresponding APIs and instead provides a
callback-based mechanism. To map this back to Carbon APIs, we create a
separate thread that gives control back to Carbon through the callbacks,
and then finishes the compilation when the Carbon code is done.

Although essentially a hack, this allows to easily fit into the Carbon
codebase quickly and we should have an option of refactoring Clang code
in LLVM upstream if this approach proves fruitful.
@danakj
Copy link
Contributor

danakj commented May 27, 2025

Run the Clang frontend until the translation unit parsing is done, at which point switch back to Carbon to finish the Check face

Did you mean Check phase?

@ilya-biryukov
Copy link
Contributor Author

This is very raw: tests fail, an approach to codegen needs to be updated, the documentation and PR description needs to be improved.

Still posting this to get early feedback about the feasibility of this approach, especially the dance with multiple threads.
Sharing the same CompilerInstance, AST, Sema, etc between multiple threads is not unheard of, but, like other use-cases (e.g. clangd), it's unusual in its own way. LLVM should be prepared to handle that, though. Even if there are few global variables / thread locals left, they should be easily fixable.

@bricknerb bricknerb self-requested a review May 27, 2025 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants