Skip to content

Lazily Associate a SILRemarkStreamer with an LLVMContext at IRGen time #31167

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 2 commits into from
Apr 22, 2020

Conversation

CodaFi
Copy link
Contributor

@CodaFi CodaFi commented Apr 21, 2020

Corrects a mistake introduced in #31106

I was under the impression that the LLVMContext for an instance of
llvm::remarks::RemarkStreamer was somehow just scratch-space. It turns
out the ASMPrinters don't gather remarks data from modules, they gather
it from the remarks streamer associated with the module's context.

So, we cannot have the module's context be distinct from whatever
context the streamer is eventually associated with.

In order to bring these two notions back in harmony, introduce a simple
ownership contract to SILRemarkStreamer. That is, it starts owned by
a SILModule, in which case the SILRemarkStreamer holds onto the
underlying LLVM object as the optimizer emits remarks. When it comes
time to IRGen the module, then and only then do we install the streamer
on the module's context. This tranfers ownership of the underlying LLVM
streamer to LLVM itself, so it acts as a consuming operation. When we
are about to perform IRGeneration, the SILModule will be expiring
anyways, so the streamer was already about to be destroyed.

Corrects a mistake introduced in swiftlang#31106

I was under the impression that the LLVMContext for an instance of
llvm::remarks::RemarkStreamer was somehow just scratch-space. It turns
out the ASMPrinters don't gather remarks data from modules, they gather
it from the remark streamer associated with the module's context.

Thus, we cannot have the module's context be distinct from whatever
context the streamer is eventually associated with.

In order to bring these two systems back into harmony, introduce a simple
ownership contract to SILRemarkStreamer. That is, it starts owned by
a SILModule, in which case the SILRemarkStreamer holds onto the
underlying LLVM object as the optimizer emits remarks. When it comes
time to IRGen the module, then and only then do we install the streamer
on the module's context. This tranfers ownership of the underlying LLVM
streamer to LLVM itself, so it acts as a consuming operation. When we
are about to perform IR Generation, the SILModule will be expiring
anyways, so the streamer was already about to be destroyed.

We're just putting it to better use doing its job.
@CodaFi CodaFi requested a review from francisvm April 21, 2020 00:56
@CodaFi
Copy link
Contributor Author

CodaFi commented Apr 21, 2020

@swift-ci test

Copy link
Contributor

@francisvm francisvm left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks!

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 90d05c1

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 90d05c1

@CodaFi
Copy link
Contributor Author

CodaFi commented Apr 21, 2020

@swift-ci test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 90d05c1

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 90d05c1

Destroying the SIL remark streamer after transferring ownership to LLVM
is frought. For one, the streamer holds the remark file's stream open.
Destroying it early doesn't accomodate sil-opt, which transfers control
to LLVM before running passes that emit remarks.

Instead, just take a reference to the context that the streamer gets
parented onto. If the remarks streamer infrastructure could just hold
the file stream open for us, we wouldn't have to do any of this.
@CodaFi
Copy link
Contributor Author

CodaFi commented Apr 21, 2020

Heh, taking the streamer won't work because it'll close the underlying file stream before sil-opt has a chance to run any passes.

@CodaFi
Copy link
Contributor Author

CodaFi commented Apr 21, 2020

@swift-ci test

1 similar comment
@CodaFi
Copy link
Contributor Author

CodaFi commented Apr 21, 2020

@swift-ci test

@CodaFi
Copy link
Contributor Author

CodaFi commented Apr 21, 2020

@swift-ci test Windows

@swiftlang swiftlang deleted a comment from swift-ci Apr 21, 2020
@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - db845eb

@CodaFi
Copy link
Contributor Author

CodaFi commented Apr 21, 2020

Builder died

@swift-ci test Linux

@CodaFi CodaFi changed the title Lazily Associated a SILRemarkStreamer with an LLVMContext at IRGen time Lazily Associate a SILRemarkStreamer with an LLVMContext at IRGen time Apr 21, 2020
@CodaFi
Copy link
Contributor Author

CodaFi commented Apr 22, 2020

Note to self: Cherry-pick to 5.3

@CodaFi CodaFi merged commit 35bc2f3 into swiftlang:master Apr 22, 2020
@CodaFi CodaFi deleted the NSStream-beans branch April 22, 2020 05:06
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.

3 participants