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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/swift/SIL/SILModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ class SILModule {
swift::SILRemarkStreamer *getSILRemarkStreamer() {
return silRemarkStreamer.get();
}

void installSILRemarkStreamer();

// This is currently limited to VarDecl because the visibility of global
Expand Down
22 changes: 20 additions & 2 deletions include/swift/SIL/SILRemarkStreamer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,20 @@ namespace swift {

class SILRemarkStreamer {
private:
/// The \c LLVMContext the underlying streamer uses for scratch space.
std::unique_ptr<llvm::LLVMContext> streamerContext;
enum class Owner {
SILModule,
LLVM,
} owner;

/// The underlying LLVM streamer.
///
/// If owned by a SILModule, this will be non-null.
std::unique_ptr<llvm::remarks::RemarkStreamer> streamer;
/// The owning LLVM context.
///
/// If owned by LLVM, this will be non-null.
llvm::LLVMContext *context;

/// The remark output stream used to record SIL remarks to a file.
std::unique_ptr<llvm::raw_fd_ostream> remarkStream;

Expand All @@ -53,6 +65,12 @@ class SILRemarkStreamer {

const ASTContext &getASTContext() const { return ctx; }

public:
/// Perform a one-time ownership transfer to associate the underlying
/// \c llvm::remarks::RemarkStreamer with the given \c LLVMContext.
void intoLLVMContext(llvm::LLVMContext &Ctx) &;

public:
/// Emit a remark through the streamer.
template <typename RemarkT>
void emit(const OptRemark::Remark<RemarkT> &remark);
Expand Down
20 changes: 13 additions & 7 deletions lib/IRGen/IRGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "swift/LLVMPasses/Passes.h"
#include "swift/LLVMPasses/PassesFwd.h"
#include "swift/SIL/SILModule.h"
#include "swift/SIL/SILRemarkStreamer.h"
#include "swift/SILOptimizer/PassManager/PassManager.h"
#include "swift/SILOptimizer/PassManager/PassPipeline.h"
#include "swift/SILOptimizer/PassManager/Passes.h"
Expand Down Expand Up @@ -884,7 +885,7 @@ static void embedBitcode(llvm::Module *M, const IRGenOptions &Opts)
NewUsed->setSection("llvm.metadata");
}

static void initLLVMModule(const IRGenModule &IGM, ModuleDecl &M) {
static void initLLVMModule(const IRGenModule &IGM, SILModule &SIL) {
auto *Module = IGM.getModule();
assert(Module && "Expected llvm:Module for IR generation!");

Expand All @@ -902,12 +903,17 @@ static void initLLVMModule(const IRGenModule &IGM, ModuleDecl &M) {

auto *MDNode = IGM.getModule()->getOrInsertNamedMetadata("swift.module.flags");
auto &Context = IGM.getModule()->getContext();
auto *Value = M.isStdlibModule() ? llvm::ConstantInt::getTrue(Context)
: llvm::ConstantInt::getFalse(Context);
auto *Value = SIL.getSwiftModule()->isStdlibModule()
? llvm::ConstantInt::getTrue(Context)
: llvm::ConstantInt::getFalse(Context);
MDNode->addOperand(llvm::MDTuple::get(Context,
{llvm::MDString::get(Context,
"standard-library"),
llvm::ConstantAsMetadata::get(Value)}));

if (auto *streamer = SIL.getSILRemarkStreamer()) {
streamer->intoLLVMContext(Module->getContext());
}
}

std::pair<IRGenerator *, IRGenModule *>
Expand All @@ -926,7 +932,7 @@ swift::irgen::createIRGenModule(SILModule *SILMod, StringRef OutputFilename,
*irgen, std::move(targetMachine), nullptr, "", OutputFilename,
MainInputFilenameForDebugInfo, PrivateDiscriminator);

initLLVMModule(*IGM, *SILMod->getSwiftModule());
initLLVMModule(*IGM, *SILMod);

return std::pair<IRGenerator *, IRGenModule *>(irgen, IGM);
}
Expand Down Expand Up @@ -969,7 +975,7 @@ performIRGeneration(const IRGenOptions &Opts, ModuleDecl *M,
PSPs.OutputFilename, PSPs.MainInputFilenameForDebugInfo,
PrivateDiscriminator);

initLLVMModule(IGM, *SILMod->getSwiftModule());
initLLVMModule(IGM, *SILMod);

// Run SIL level IRGen preparation passes.
runIRGenPreparePasses(*SILMod, IGM);
Expand Down Expand Up @@ -1217,7 +1223,7 @@ static void performParallelIRGeneration(
nextSF->getPrivateDiscriminator().str());
IGMcreated = true;

initLLVMModule(*IGM, *SILMod->getSwiftModule());
initLLVMModule(*IGM, *SILMod);
if (!DidRunSILCodeGenPreparePasses) {
// Run SIL level IRGen preparation passes on the module the first time
// around.
Expand Down Expand Up @@ -1431,7 +1437,7 @@ swift::createSwiftModuleObjectFile(SILModule &SILMod, StringRef Buffer,

IRGenModule IGM(irgen, std::move(targetMachine), nullptr,
OutputPath, OutputPath, "", "");
initLLVMModule(IGM, *SILMod.getSwiftModule());
initLLVMModule(IGM, SILMod);
auto *Ty = llvm::ArrayType::get(IGM.Int8Ty, Buffer.size());
auto *Data =
llvm::ConstantDataArray::getString(IGM.getLLVMContext(),
Expand Down
29 changes: 23 additions & 6 deletions lib/SIL/Utils/SILRemarkStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,35 @@ using namespace swift;
SILRemarkStreamer::SILRemarkStreamer(
std::unique_ptr<llvm::remarks::RemarkStreamer> &&streamer,
std::unique_ptr<llvm::raw_fd_ostream> &&stream, const ASTContext &Ctx)
: streamerContext(std::make_unique<llvm::LLVMContext>()),
remarkStream(std::move(stream)), ctx(Ctx) {
streamerContext->setMainRemarkStreamer(std::move(streamer));
}
: owner(Owner::SILModule), streamer(std::move(streamer)), context(nullptr),
remarkStream(std::move(stream)), ctx(Ctx) { }

llvm::remarks::RemarkStreamer &SILRemarkStreamer::getLLVMStreamer() {
return *streamerContext->getMainRemarkStreamer();
switch (owner) {
case Owner::SILModule:
return *streamer.get();
case Owner::LLVM:
return *context->getMainRemarkStreamer();
}
return *streamer.get();
}

const llvm::remarks::RemarkStreamer &
SILRemarkStreamer::getLLVMStreamer() const {
return *streamerContext->getMainRemarkStreamer();
switch (owner) {
case Owner::SILModule:
return *streamer.get();
case Owner::LLVM:
return *context->getMainRemarkStreamer();
}
return *streamer.get();
}

void SILRemarkStreamer::intoLLVMContext(llvm::LLVMContext &Ctx) & {
assert(owner == Owner::SILModule);
Ctx.setMainRemarkStreamer(std::move(streamer));
context = &Ctx;
owner = Owner::LLVM;
}

std::unique_ptr<SILRemarkStreamer>
Expand Down
7 changes: 6 additions & 1 deletion test/Driver/opt-record-bitstream.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
// RUN: %empty-directory(%t)
// RUN: %target-swiftc_driver -O -wmo -save-optimization-record=bitstream %s -module-name optrecordmod -o %t/opt-record 2>&1 | %FileCheck -allow-empty %s
// RUN: %target-swift-frontend -c -O -wmo -save-optimization-record=bitstream -save-optimization-record-path %t/optrecordmod.opt.bitstream %s -module-name optrecordmod -o %t/opt-record.o 2>&1 | %FileCheck -allow-empty %s
// RUN: llvm-bcanalyzer -dump %t/optrecordmod.opt.bitstream | %FileCheck -check-prefix=BITSTREAM %s
// RUN: otool -l %t/opt-record.o | %FileCheck -check-prefix=OBJ %s

// REQUIRES: OS=macosx || OS=ios || OS=tvos || OS=watchos

// Ensure we emitted the appropriate section

// OBJ: sectname __remarks

// CHECK-NOT: remark

var a: Int = 1
Expand Down
2 changes: 1 addition & 1 deletion tools/sil-opt/SILOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ int main(int argc, char **argv) {
if (CI.getSILModule())
CI.getSILModule()->setSerializeSILAction([]{});

if (RemarksFilename != "") {
if (!RemarksFilename.empty()) {
llvm::Expected<llvm::remarks::Format> formatOrErr =
llvm::remarks::parseFormat(RemarksFormat);
if (llvm::Error E = formatOrErr.takeError()) {
Expand Down