Skip to content

Commit

Permalink
[CodeGen] Simplify EmitAssemblyHelper and emitBackendOutput
Browse files Browse the repository at this point in the history
Prepare for -ftime-report change (#122225).
  • Loading branch information
MaskRay committed Jan 10, 2025
1 parent a4394d9 commit 48d0eb5
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 71 deletions.
10 changes: 3 additions & 7 deletions clang/include/clang/CodeGen/BackendUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ class FileSystem;
} // namespace llvm

namespace clang {
class CompilerInstance;
class DiagnosticsEngine;
class HeaderSearchOptions;
class CodeGenOptions;
class TargetOptions;
class LangOptions;
class BackendConsumer;

enum BackendAction {
Expand All @@ -41,10 +39,8 @@ enum BackendAction {
Backend_EmitObj ///< Emit native object files
};

void EmitBackendOutput(DiagnosticsEngine &Diags, const HeaderSearchOptions &,
const CodeGenOptions &CGOpts, const TargetOptions &TOpts,
const LangOptions &LOpts, StringRef TDesc,
llvm::Module *M, BackendAction Action,
void emitBackendOutput(CompilerInstance &CI, StringRef TDesc, llvm::Module *M,
BackendAction Action,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
std::unique_ptr<raw_pwrite_stream> OS,
BackendConsumer *BC = nullptr);
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/CodeGen/BackendConsumer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class BackendConsumer : public ASTConsumer {
using LinkModule = CodeGenAction::LinkModule;

virtual void anchor();
CompilerInstance &CI;
DiagnosticsEngine &Diags;
const HeaderSearchOptions &HeaderSearchOpts;
const CodeGenOptions &CodeGenOpts;
const TargetOptions &TargetOpts;
const LangOptions &LangOpts;
Expand Down Expand Up @@ -70,7 +70,7 @@ class BackendConsumer : public ASTConsumer {
llvm::Module *CurLinkModule = nullptr;

public:
BackendConsumer(const CompilerInstance &CI, BackendAction Action,
BackendConsumer(CompilerInstance &CI, BackendAction Action,
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
llvm::LLVMContext &C, SmallVector<LinkModule, 4> LinkModules,
StringRef InFile, std::unique_ptr<raw_pwrite_stream> OS,
Expand Down
75 changes: 36 additions & 39 deletions clang/lib/CodeGen/BackendUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ std::string getDefaultProfileGenName() {
}

class EmitAssemblyHelper {
CompilerInstance &CI;
DiagnosticsEngine &Diags;
const HeaderSearchOptions &HSOpts;
const CodeGenOptions &CodeGenOpts;
const clang::TargetOptions &TargetOpts;
const LangOptions &LangOpts;
Expand Down Expand Up @@ -203,15 +203,11 @@ class EmitAssemblyHelper {
}

public:
EmitAssemblyHelper(DiagnosticsEngine &_Diags,
const HeaderSearchOptions &HeaderSearchOpts,
const CodeGenOptions &CGOpts,
const clang::TargetOptions &TOpts,
const LangOptions &LOpts, llvm::Module *M,
EmitAssemblyHelper(CompilerInstance &CI, llvm::Module *M,
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS)
: Diags(_Diags), HSOpts(HeaderSearchOpts), CodeGenOpts(CGOpts),
TargetOpts(TOpts), LangOpts(LOpts), TheModule(M), VFS(std::move(VFS)),
CodeGenerationTime("codegen", "Code Generation Time"),
: CI(CI), Diags(CI.getDiagnostics()), CodeGenOpts(CI.getCodeGenOpts()),
TargetOpts(CI.getTargetOpts()), LangOpts(CI.getLangOpts()),
TheModule(M), VFS(std::move(VFS)),
TargetTriple(TheModule->getTargetTriple()) {}

~EmitAssemblyHelper() {
Expand All @@ -222,7 +218,7 @@ class EmitAssemblyHelper {
std::unique_ptr<TargetMachine> TM;

// Emit output using the new pass manager for the optimization pipeline.
void EmitAssembly(BackendAction Action, std::unique_ptr<raw_pwrite_stream> OS,
void emitAssembly(BackendAction Action, std::unique_ptr<raw_pwrite_stream> OS,
BackendConsumer *BC);
};
} // namespace
Expand Down Expand Up @@ -351,12 +347,13 @@ static std::string flattenClangCommandLine(ArrayRef<std::string> Args,
return FlatCmdLine;
}

static bool initTargetOptions(DiagnosticsEngine &Diags,
llvm::TargetOptions &Options,
const CodeGenOptions &CodeGenOpts,
const clang::TargetOptions &TargetOpts,
const LangOptions &LangOpts,
const HeaderSearchOptions &HSOpts) {
static bool initTargetOptions(const CompilerInstance &CI,
DiagnosticsEngine &Diags,
llvm::TargetOptions &Options) {
const auto &CodeGenOpts = CI.getCodeGenOpts();
const auto &TargetOpts = CI.getTargetOpts();
const auto &LangOpts = CI.getLangOpts();
const auto &HSOpts = CI.getHeaderSearchOpts();
switch (LangOpts.getThreadModel()) {
case LangOptions::ThreadModelKind::POSIX:
Options.ThreadModel = llvm::ThreadModel::POSIX;
Expand Down Expand Up @@ -600,8 +597,7 @@ void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
CodeGenOptLevel OptLevel = *OptLevelOrNone;

llvm::TargetOptions Options;
if (!initTargetOptions(Diags, Options, CodeGenOpts, TargetOpts, LangOpts,
HSOpts))
if (!initTargetOptions(CI, Diags, Options))
return;
TM.reset(TheTarget->createTargetMachine(Triple, TargetOpts.CPU, FeaturesStr,
Options, RM, CM, OptLevel));
Expand Down Expand Up @@ -1207,7 +1203,7 @@ void EmitAssemblyHelper::RunCodegenPipeline(
}
}

void EmitAssemblyHelper::EmitAssembly(BackendAction Action,
void EmitAssemblyHelper::emitAssembly(BackendAction Action,
std::unique_ptr<raw_pwrite_stream> OS,
BackendConsumer *BC) {
TimeRegion Region(CodeGenOpts.TimePasses ? &CodeGenerationTime : nullptr);
Expand All @@ -1234,13 +1230,14 @@ void EmitAssemblyHelper::EmitAssembly(BackendAction Action,
DwoOS->keep();
}

static void runThinLTOBackend(
DiagnosticsEngine &Diags, ModuleSummaryIndex *CombinedIndex,
llvm::Module *M, const HeaderSearchOptions &HeaderOpts,
const CodeGenOptions &CGOpts, const clang::TargetOptions &TOpts,
const LangOptions &LOpts, std::unique_ptr<raw_pwrite_stream> OS,
std::string SampleProfile, std::string ProfileRemapping,
BackendAction Action) {
static void
runThinLTOBackend(CompilerInstance &CI, ModuleSummaryIndex *CombinedIndex,
llvm::Module *M, std::unique_ptr<raw_pwrite_stream> OS,
std::string SampleProfile, std::string ProfileRemapping,
BackendAction Action) {
DiagnosticsEngine &Diags = CI.getDiagnostics();
const auto &CGOpts = CI.getCodeGenOpts();
const auto &TOpts = CI.getTargetOpts();
DenseMap<StringRef, DenseMap<GlobalValue::GUID, GlobalValueSummary *>>
ModuleToDefinedGVSummaries;
CombinedIndex->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
Expand Down Expand Up @@ -1278,7 +1275,7 @@ static void runThinLTOBackend(
assert(OptLevelOrNone && "Invalid optimization level!");
Conf.CGOptLevel = *OptLevelOrNone;
Conf.OptLevel = CGOpts.OptimizationLevel;
initTargetOptions(Diags, Conf.Options, CGOpts, TOpts, LOpts, HeaderOpts);
initTargetOptions(CI, Diags, Conf.Options);
Conf.SampleProfile = std::move(SampleProfile);
Conf.PTO.LoopUnrolling = CGOpts.UnrollLoops;
// For historical reasons, loop interleaving is set to mirror setting for loop
Expand Down Expand Up @@ -1341,14 +1338,14 @@ static void runThinLTOBackend(
}
}

void clang::EmitBackendOutput(
DiagnosticsEngine &Diags, const HeaderSearchOptions &HeaderOpts,
const CodeGenOptions &CGOpts, const clang::TargetOptions &TOpts,
const LangOptions &LOpts, StringRef TDesc, llvm::Module *M,
BackendAction Action, IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
std::unique_ptr<raw_pwrite_stream> OS, BackendConsumer *BC) {

void clang::emitBackendOutput(CompilerInstance &CI, StringRef TDesc,
llvm::Module *M, BackendAction Action,
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
std::unique_ptr<raw_pwrite_stream> OS,
BackendConsumer *BC) {
llvm::TimeTraceScope TimeScope("Backend");
DiagnosticsEngine &Diags = CI.getDiagnostics();
const auto &CGOpts = CI.getCodeGenOpts();

std::unique_ptr<llvm::Module> EmptyModule;
if (!CGOpts.ThinLTOIndexFile.empty()) {
Expand All @@ -1371,9 +1368,9 @@ void clang::EmitBackendOutput(
// of an error).
if (CombinedIndex) {
if (!CombinedIndex->skipModuleByDistributedBackend()) {
runThinLTOBackend(Diags, CombinedIndex.get(), M, HeaderOpts, CGOpts,
TOpts, LOpts, std::move(OS), CGOpts.SampleProfileFile,
CGOpts.ProfileRemappingFile, Action);
runThinLTOBackend(CI, CombinedIndex.get(), M, std::move(OS),
CGOpts.SampleProfileFile, CGOpts.ProfileRemappingFile,
Action);
return;
}
// Distributed indexing detected that nothing from the module is needed
Expand All @@ -1388,8 +1385,8 @@ void clang::EmitBackendOutput(
}
}

EmitAssemblyHelper AsmHelper(Diags, HeaderOpts, CGOpts, TOpts, LOpts, M, VFS);
AsmHelper.EmitAssembly(Action, std::move(OS), BC);
EmitAssemblyHelper AsmHelper(CI, M, VFS);
AsmHelper.emitAssembly(Action, std::move(OS), BC);

// Verify clang's TargetInfo DataLayout against the LLVM TargetMachine's
// DataLayout.
Expand Down
30 changes: 15 additions & 15 deletions clang/lib/CodeGen/CodeGenAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,17 @@ static void reportOptRecordError(Error E, DiagnosticsEngine &Diags,
});
}

BackendConsumer::BackendConsumer(
const CompilerInstance &CI, BackendAction Action,
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, LLVMContext &C,
SmallVector<LinkModule, 4> LinkModules, StringRef InFile,
std::unique_ptr<raw_pwrite_stream> OS, CoverageSourceInfo *CoverageInfo,
llvm::Module *CurLinkModule)
: Diags(CI.getDiagnostics()), HeaderSearchOpts(CI.getHeaderSearchOpts()),
CodeGenOpts(CI.getCodeGenOpts()), TargetOpts(CI.getTargetOpts()),
LangOpts(CI.getLangOpts()), AsmOutStream(std::move(OS)), FS(VFS),
BackendConsumer::BackendConsumer(CompilerInstance &CI, BackendAction Action,
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
LLVMContext &C,
SmallVector<LinkModule, 4> LinkModules,
StringRef InFile,
std::unique_ptr<raw_pwrite_stream> OS,
CoverageSourceInfo *CoverageInfo,
llvm::Module *CurLinkModule)
: CI(CI), Diags(CI.getDiagnostics()), CodeGenOpts(CI.getCodeGenOpts()),
TargetOpts(CI.getTargetOpts()), LangOpts(CI.getLangOpts()),
AsmOutStream(std::move(OS)), FS(VFS),
LLVMIRGeneration("irgen", "LLVM IR Generation Time"), Action(Action),
Gen(CreateLLVMCodeGen(Diags, InFile, std::move(VFS),
CI.getHeaderSearchOpts(), CI.getPreprocessorOpts(),
Expand Down Expand Up @@ -321,8 +323,7 @@ void BackendConsumer::HandleTranslationUnit(ASTContext &C) {

EmbedBitcode(getModule(), CodeGenOpts, llvm::MemoryBufferRef());

EmitBackendOutput(Diags, HeaderSearchOpts, CodeGenOpts, TargetOpts, LangOpts,
C.getTargetInfo().getDataLayoutString(), getModule(),
emitBackendOutput(CI, C.getTargetInfo().getDataLayoutString(), getModule(),
Action, FS, std::move(AsmOutStream), this);

Ctx.setDiagnosticHandler(std::move(OldDiagnosticHandler));
Expand Down Expand Up @@ -1183,10 +1184,9 @@ void CodeGenAction::ExecuteAction() {
std::unique_ptr<llvm::ToolOutputFile> OptRecordFile =
std::move(*OptRecordFileOrErr);

EmitBackendOutput(
Diagnostics, CI.getHeaderSearchOpts(), CodeGenOpts, TargetOpts,
CI.getLangOpts(), CI.getTarget().getDataLayoutString(), TheModule.get(),
BA, CI.getFileManager().getVirtualFileSystemPtr(), std::move(OS));
emitBackendOutput(CI, CI.getTarget().getDataLayoutString(), TheModule.get(),
BA, CI.getFileManager().getVirtualFileSystemPtr(),
std::move(OS));
if (OptRecordFile)
OptRecordFile->keep();
}
Expand Down
15 changes: 7 additions & 8 deletions clang/lib/CodeGen/ObjectFilePCHContainerWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ using namespace clang;

namespace {
class PCHContainerGenerator : public ASTConsumer {
CompilerInstance &CI;
DiagnosticsEngine &Diags;
const std::string MainFileName;
const std::string OutputFileName;
Expand Down Expand Up @@ -139,7 +140,7 @@ class PCHContainerGenerator : public ASTConsumer {
const std::string &OutputFileName,
std::unique_ptr<raw_pwrite_stream> OS,
std::shared_ptr<PCHBuffer> Buffer)
: Diags(CI.getDiagnostics()), MainFileName(MainFileName),
: CI(CI), Diags(CI.getDiagnostics()), MainFileName(MainFileName),
OutputFileName(OutputFileName), Ctx(nullptr),
MMap(CI.getPreprocessor().getHeaderSearchInfo().getModuleMap()),
FS(&CI.getVirtualFileSystem()),
Expand Down Expand Up @@ -317,19 +318,17 @@ class PCHContainerGenerator : public ASTConsumer {
LLVM_DEBUG({
// Print the IR for the PCH container to the debug output.
llvm::SmallString<0> Buffer;
clang::EmitBackendOutput(
Diags, HeaderSearchOpts, CodeGenOpts, TargetOpts, LangOpts,
Ctx.getTargetInfo().getDataLayoutString(), M.get(),
clang::emitBackendOutput(
CI, Ctx.getTargetInfo().getDataLayoutString(), M.get(),
BackendAction::Backend_EmitLL, FS,
std::make_unique<llvm::raw_svector_ostream>(Buffer));
llvm::dbgs() << Buffer;
});

// Use the LLVM backend to emit the pch container.
clang::EmitBackendOutput(Diags, HeaderSearchOpts, CodeGenOpts, TargetOpts,
LangOpts,
Ctx.getTargetInfo().getDataLayoutString(), M.get(),
BackendAction::Backend_EmitObj, FS, std::move(OS));
clang::emitBackendOutput(CI, Ctx.getTargetInfo().getDataLayoutString(),
M.get(), BackendAction::Backend_EmitObj, FS,
std::move(OS));

// Free the memory for the temporary buffer.
llvm::SmallVector<char, 0> Empty;
Expand Down

0 comments on commit 48d0eb5

Please sign in to comment.