[CodeGen] Simplify EmitAssemblyHelper and emitBackendOutput

Prepare for -ftime-report change (#122225).
This commit is contained in:
Fangrui Song
2025-01-09 21:23:52 -08:00
parent a4394d9d42
commit 48d0eb5181
5 changed files with 63 additions and 71 deletions

View File

@@ -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 {
@@ -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);

View File

@@ -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;
@@ -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,

View File

@@ -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;
@@ -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() {
@@ -222,7 +218,7 @@ public:
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
@@ -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;
@@ -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));
@@ -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);
@@ -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);
@@ -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
@@ -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()) {
@@ -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
@@ -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.

View File

@@ -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(),
@@ -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));
@@ -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();
}

View File

@@ -37,6 +37,7 @@ using namespace clang;
namespace {
class PCHContainerGenerator : public ASTConsumer {
CompilerInstance &CI;
DiagnosticsEngine &Diags;
const std::string MainFileName;
const std::string OutputFileName;
@@ -139,7 +140,7 @@ public:
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()),
@@ -317,19 +318,17 @@ public:
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;