Skip to content

Requestify SIL parsing #31609

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

Merged
merged 5 commits into from
May 8, 2020

Conversation

hamishknight
Copy link
Contributor

Introduce ParseSILModuleRequest to replace parseSourceFileSIL, and call it from SIL generation instead of inside performSemaUpTo. This then allows us to remove the SILModule stored in the Frontend.

Replace `parseSourceFileSIL` with
ParseSILModuleRequest, and add some convenience
members to SILGenDescriptor to retrieve the
SourceFile to be parsed.
Rather than eagerly parsing an input .sil file
in `performSemaUpTo`, trigger it from
`performSILGeneration`. This will allow us to
remove the SILModule stored on the
CompilerInstance and will eventually allow the
various SIL tools to just call into
`performSILGeneration` without needing to call
`performSema`.
Because we were previously performing SIL parsing
during `performSema`, we were relying on the
pipeline being stopped before reaching the SIL
pipeline passes.

However under a lazy evaluation model, we can't
rely on that. Instead, just return an empty
SILModule if we encounter a parsing error.
Have the tools call into `performSILGeneration` in
order to retrieve the SILModule rather than getting
it from the CompilerInstance.
Now that SIL parsing is handled lazily, the
CompilerInstance no longer needs to hang onto a
SILModule.
@hamishknight
Copy link
Contributor Author

@swift-ci please test

@hamishknight hamishknight requested review from CodaFi and gottesmm May 7, 2020 03:16
@swiftlang swiftlang deleted a comment from swift-ci May 7, 2020
@hamishknight
Copy link
Contributor Author

@swift-ci please test macOS

1 similar comment
@shahmishal
Copy link
Member

@swift-ci please test macOS


// Given the above precondition that a .sil file isn't mixed with other
// SourceFiles, we can return a SIL file if we have it, or return nullptr.
if (SF->Kind == SourceFileKind::SIL) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be difficult to allow mixing SIL with swift source in the same module?

Copy link
Contributor

Choose a reason for hiding this comment

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

(This could allow writing parts of the stdlib in SIL, which was an idea @jckarter had at some point...)

Copy link
Contributor

Choose a reason for hiding this comment

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

Well, if we were going to allow that, I think we would still only want to do so in a particular SourceFileKind, since we wouldn't want SIL parsing to be able to generally affect the behavior of most code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@slavapestov I don't think it would be too difficult, we'd just need to make sure that we don't mix canonical parsed SIL with raw generated SIL.

// The rest of the SIL pipeline expects well-formed SIL, so if we encounter
// a parsing error, just return an empty SIL module.
return SILModule::createEmptyModule(mod, desc.conv, desc.opts,
desc.isWholeModule());
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just return the incomplete silMod you already started parsing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@slavapestov The rest of the SIL pipeline cannot handle the invalid SIL it might contain, e.g not being in SSA form, type mismatches etc. We could probably recover better than just throwing everything away, but given SIL parsing is currently mostly used for testing, I'm not sure how much benefit we'd get from recovery.

@@ -425,7 +425,6 @@ class CompilerInstance {
DiagnosticEngine Diagnostics{SourceMgr};
std::unique_ptr<ASTContext> Context;
std::unique_ptr<Lowering::TypeConverter> TheSILTypes;
std::unique_ptr<SILModule> TheSILModule;
Copy link
Contributor

Choose a reason for hiding this comment

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

Really happy to see this go away!

@swiftlang swiftlang deleted a comment from swift-ci May 7, 2020

std::unique_ptr<SILModule> SILMod;
if (PerformWMO) {
SILMod = performSILGeneration(mod, CI.getSILTypes(), CI.getSILOptions());
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we use a different name than performSILGeneration. It makes it sound like you are SILGening code. It should be really clear that we are parsing SIL from the name of this method. One thought is imagine if we had a helper method that called performSILGeneration internally and takes the compiler instance, but with a name like performSILParsing?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, while you are touching this code, do you think we can centralize any of this code that is in the various tools into helper code rather than duplicating?

Copy link
Contributor

Choose a reason for hiding this comment

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

If this doesn't actually need to perform code generation (is there a canonicalization step that needs to get done here?), an entrypoint to ParseSILModuleRequest, or even invoking it directly would work here.

Copy link
Contributor Author

@hamishknight hamishknight May 7, 2020

Choose a reason for hiding this comment

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

Hmm, we can't call into ParseSILModuleRequest directly (at least not without adding a special case for when we have a .sil file input), as we also need to be handle SIB. IMO the SIB case might also make something like performSILParsing confusing, as then we'd be deserializing instead of parsing SIL.

My goal with this was for performSILGeneration to be the one function you call to turn a ModuleDecl * or FileUnit * into a SILModule, regardless of specifics of what the AST nodes contain. In the future, it might perform both SIL parsing and SIL generation if we allow SIL files to mix with Swift files.

Perhaps a better name would be something more general like lowerAST or performASTLowering?

@hamishknight
Copy link
Contributor Author

Talked with @gottesmm offline, will take care of his review feedback in a follow-up.

@hamishknight hamishknight merged commit 98d3a81 into swiftlang:master May 8, 2020
@hamishknight hamishknight deleted the ill-sil-you-in-later branch May 8, 2020 01:02
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.

6 participants