Skip to content

Commit 48d0eb5

Browse files
committed
[CodeGen] Simplify EmitAssemblyHelper and emitBackendOutput
Prepare for -ftime-report change (llvm#122225).
1 parent a4394d9 commit 48d0eb5

File tree

5 files changed

+63
-71
lines changed

5 files changed

+63
-71
lines changed

clang/include/clang/CodeGen/BackendUtil.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@ class FileSystem;
2525
} // namespace llvm
2626

2727
namespace clang {
28+
class CompilerInstance;
2829
class DiagnosticsEngine;
29-
class HeaderSearchOptions;
3030
class CodeGenOptions;
31-
class TargetOptions;
32-
class LangOptions;
3331
class BackendConsumer;
3432

3533
enum BackendAction {
@@ -41,10 +39,8 @@ enum BackendAction {
4139
Backend_EmitObj ///< Emit native object files
4240
};
4341

44-
void EmitBackendOutput(DiagnosticsEngine &Diags, const HeaderSearchOptions &,
45-
const CodeGenOptions &CGOpts, const TargetOptions &TOpts,
46-
const LangOptions &LOpts, StringRef TDesc,
47-
llvm::Module *M, BackendAction Action,
42+
void emitBackendOutput(CompilerInstance &CI, StringRef TDesc, llvm::Module *M,
43+
BackendAction Action,
4844
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
4945
std::unique_ptr<raw_pwrite_stream> OS,
5046
BackendConsumer *BC = nullptr);

clang/lib/CodeGen/BackendConsumer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ class BackendConsumer : public ASTConsumer {
2828
using LinkModule = CodeGenAction::LinkModule;
2929

3030
virtual void anchor();
31+
CompilerInstance &CI;
3132
DiagnosticsEngine &Diags;
32-
const HeaderSearchOptions &HeaderSearchOpts;
3333
const CodeGenOptions &CodeGenOpts;
3434
const TargetOptions &TargetOpts;
3535
const LangOptions &LangOpts;
@@ -70,7 +70,7 @@ class BackendConsumer : public ASTConsumer {
7070
llvm::Module *CurLinkModule = nullptr;
7171

7272
public:
73-
BackendConsumer(const CompilerInstance &CI, BackendAction Action,
73+
BackendConsumer(CompilerInstance &CI, BackendAction Action,
7474
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
7575
llvm::LLVMContext &C, SmallVector<LinkModule, 4> LinkModules,
7676
StringRef InFile, std::unique_ptr<raw_pwrite_stream> OS,

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ std::string getDefaultProfileGenName() {
129129
}
130130

131131
class EmitAssemblyHelper {
132+
CompilerInstance &CI;
132133
DiagnosticsEngine &Diags;
133-
const HeaderSearchOptions &HSOpts;
134134
const CodeGenOptions &CodeGenOpts;
135135
const clang::TargetOptions &TargetOpts;
136136
const LangOptions &LangOpts;
@@ -203,15 +203,11 @@ class EmitAssemblyHelper {
203203
}
204204

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

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

224220
// Emit output using the new pass manager for the optimization pipeline.
225-
void EmitAssembly(BackendAction Action, std::unique_ptr<raw_pwrite_stream> OS,
221+
void emitAssembly(BackendAction Action, std::unique_ptr<raw_pwrite_stream> OS,
226222
BackendConsumer *BC);
227223
};
228224
} // namespace
@@ -351,12 +347,13 @@ static std::string flattenClangCommandLine(ArrayRef<std::string> Args,
351347
return FlatCmdLine;
352348
}
353349

354-
static bool initTargetOptions(DiagnosticsEngine &Diags,
355-
llvm::TargetOptions &Options,
356-
const CodeGenOptions &CodeGenOpts,
357-
const clang::TargetOptions &TargetOpts,
358-
const LangOptions &LangOpts,
359-
const HeaderSearchOptions &HSOpts) {
350+
static bool initTargetOptions(const CompilerInstance &CI,
351+
DiagnosticsEngine &Diags,
352+
llvm::TargetOptions &Options) {
353+
const auto &CodeGenOpts = CI.getCodeGenOpts();
354+
const auto &TargetOpts = CI.getTargetOpts();
355+
const auto &LangOpts = CI.getLangOpts();
356+
const auto &HSOpts = CI.getHeaderSearchOpts();
360357
switch (LangOpts.getThreadModel()) {
361358
case LangOptions::ThreadModelKind::POSIX:
362359
Options.ThreadModel = llvm::ThreadModel::POSIX;
@@ -600,8 +597,7 @@ void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
600597
CodeGenOptLevel OptLevel = *OptLevelOrNone;
601598

602599
llvm::TargetOptions Options;
603-
if (!initTargetOptions(Diags, Options, CodeGenOpts, TargetOpts, LangOpts,
604-
HSOpts))
600+
if (!initTargetOptions(CI, Diags, Options))
605601
return;
606602
TM.reset(TheTarget->createTargetMachine(Triple, TargetOpts.CPU, FeaturesStr,
607603
Options, RM, CM, OptLevel));
@@ -1207,7 +1203,7 @@ void EmitAssemblyHelper::RunCodegenPipeline(
12071203
}
12081204
}
12091205

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

1237-
static void runThinLTOBackend(
1238-
DiagnosticsEngine &Diags, ModuleSummaryIndex *CombinedIndex,
1239-
llvm::Module *M, const HeaderSearchOptions &HeaderOpts,
1240-
const CodeGenOptions &CGOpts, const clang::TargetOptions &TOpts,
1241-
const LangOptions &LOpts, std::unique_ptr<raw_pwrite_stream> OS,
1242-
std::string SampleProfile, std::string ProfileRemapping,
1243-
BackendAction Action) {
1233+
static void
1234+
runThinLTOBackend(CompilerInstance &CI, ModuleSummaryIndex *CombinedIndex,
1235+
llvm::Module *M, std::unique_ptr<raw_pwrite_stream> OS,
1236+
std::string SampleProfile, std::string ProfileRemapping,
1237+
BackendAction Action) {
1238+
DiagnosticsEngine &Diags = CI.getDiagnostics();
1239+
const auto &CGOpts = CI.getCodeGenOpts();
1240+
const auto &TOpts = CI.getTargetOpts();
12441241
DenseMap<StringRef, DenseMap<GlobalValue::GUID, GlobalValueSummary *>>
12451242
ModuleToDefinedGVSummaries;
12461243
CombinedIndex->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
@@ -1278,7 +1275,7 @@ static void runThinLTOBackend(
12781275
assert(OptLevelOrNone && "Invalid optimization level!");
12791276
Conf.CGOptLevel = *OptLevelOrNone;
12801277
Conf.OptLevel = CGOpts.OptimizationLevel;
1281-
initTargetOptions(Diags, Conf.Options, CGOpts, TOpts, LOpts, HeaderOpts);
1278+
initTargetOptions(CI, Diags, Conf.Options);
12821279
Conf.SampleProfile = std::move(SampleProfile);
12831280
Conf.PTO.LoopUnrolling = CGOpts.UnrollLoops;
12841281
// For historical reasons, loop interleaving is set to mirror setting for loop
@@ -1341,14 +1338,14 @@ static void runThinLTOBackend(
13411338
}
13421339
}
13431340

1344-
void clang::EmitBackendOutput(
1345-
DiagnosticsEngine &Diags, const HeaderSearchOptions &HeaderOpts,
1346-
const CodeGenOptions &CGOpts, const clang::TargetOptions &TOpts,
1347-
const LangOptions &LOpts, StringRef TDesc, llvm::Module *M,
1348-
BackendAction Action, IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
1349-
std::unique_ptr<raw_pwrite_stream> OS, BackendConsumer *BC) {
1350-
1341+
void clang::emitBackendOutput(CompilerInstance &CI, StringRef TDesc,
1342+
llvm::Module *M, BackendAction Action,
1343+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
1344+
std::unique_ptr<raw_pwrite_stream> OS,
1345+
BackendConsumer *BC) {
13511346
llvm::TimeTraceScope TimeScope("Backend");
1347+
DiagnosticsEngine &Diags = CI.getDiagnostics();
1348+
const auto &CGOpts = CI.getCodeGenOpts();
13521349

13531350
std::unique_ptr<llvm::Module> EmptyModule;
13541351
if (!CGOpts.ThinLTOIndexFile.empty()) {
@@ -1371,9 +1368,9 @@ void clang::EmitBackendOutput(
13711368
// of an error).
13721369
if (CombinedIndex) {
13731370
if (!CombinedIndex->skipModuleByDistributedBackend()) {
1374-
runThinLTOBackend(Diags, CombinedIndex.get(), M, HeaderOpts, CGOpts,
1375-
TOpts, LOpts, std::move(OS), CGOpts.SampleProfileFile,
1376-
CGOpts.ProfileRemappingFile, Action);
1371+
runThinLTOBackend(CI, CombinedIndex.get(), M, std::move(OS),
1372+
CGOpts.SampleProfileFile, CGOpts.ProfileRemappingFile,
1373+
Action);
13771374
return;
13781375
}
13791376
// Distributed indexing detected that nothing from the module is needed
@@ -1388,8 +1385,8 @@ void clang::EmitBackendOutput(
13881385
}
13891386
}
13901387

1391-
EmitAssemblyHelper AsmHelper(Diags, HeaderOpts, CGOpts, TOpts, LOpts, M, VFS);
1392-
AsmHelper.EmitAssembly(Action, std::move(OS), BC);
1388+
EmitAssemblyHelper AsmHelper(CI, M, VFS);
1389+
AsmHelper.emitAssembly(Action, std::move(OS), BC);
13931390

13941391
// Verify clang's TargetInfo DataLayout against the LLVM TargetMachine's
13951392
// DataLayout.

clang/lib/CodeGen/CodeGenAction.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,17 @@ static void reportOptRecordError(Error E, DiagnosticsEngine &Diags,
105105
});
106106
}
107107

108-
BackendConsumer::BackendConsumer(
109-
const CompilerInstance &CI, BackendAction Action,
110-
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, LLVMContext &C,
111-
SmallVector<LinkModule, 4> LinkModules, StringRef InFile,
112-
std::unique_ptr<raw_pwrite_stream> OS, CoverageSourceInfo *CoverageInfo,
113-
llvm::Module *CurLinkModule)
114-
: Diags(CI.getDiagnostics()), HeaderSearchOpts(CI.getHeaderSearchOpts()),
115-
CodeGenOpts(CI.getCodeGenOpts()), TargetOpts(CI.getTargetOpts()),
116-
LangOpts(CI.getLangOpts()), AsmOutStream(std::move(OS)), FS(VFS),
108+
BackendConsumer::BackendConsumer(CompilerInstance &CI, BackendAction Action,
109+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
110+
LLVMContext &C,
111+
SmallVector<LinkModule, 4> LinkModules,
112+
StringRef InFile,
113+
std::unique_ptr<raw_pwrite_stream> OS,
114+
CoverageSourceInfo *CoverageInfo,
115+
llvm::Module *CurLinkModule)
116+
: CI(CI), Diags(CI.getDiagnostics()), CodeGenOpts(CI.getCodeGenOpts()),
117+
TargetOpts(CI.getTargetOpts()), LangOpts(CI.getLangOpts()),
118+
AsmOutStream(std::move(OS)), FS(VFS),
117119
LLVMIRGeneration("irgen", "LLVM IR Generation Time"), Action(Action),
118120
Gen(CreateLLVMCodeGen(Diags, InFile, std::move(VFS),
119121
CI.getHeaderSearchOpts(), CI.getPreprocessorOpts(),
@@ -321,8 +323,7 @@ void BackendConsumer::HandleTranslationUnit(ASTContext &C) {
321323

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

324-
EmitBackendOutput(Diags, HeaderSearchOpts, CodeGenOpts, TargetOpts, LangOpts,
325-
C.getTargetInfo().getDataLayoutString(), getModule(),
326+
emitBackendOutput(CI, C.getTargetInfo().getDataLayoutString(), getModule(),
326327
Action, FS, std::move(AsmOutStream), this);
327328

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

1186-
EmitBackendOutput(
1187-
Diagnostics, CI.getHeaderSearchOpts(), CodeGenOpts, TargetOpts,
1188-
CI.getLangOpts(), CI.getTarget().getDataLayoutString(), TheModule.get(),
1189-
BA, CI.getFileManager().getVirtualFileSystemPtr(), std::move(OS));
1187+
emitBackendOutput(CI, CI.getTarget().getDataLayoutString(), TheModule.get(),
1188+
BA, CI.getFileManager().getVirtualFileSystemPtr(),
1189+
std::move(OS));
11901190
if (OptRecordFile)
11911191
OptRecordFile->keep();
11921192
}

clang/lib/CodeGen/ObjectFilePCHContainerWriter.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ using namespace clang;
3737

3838
namespace {
3939
class PCHContainerGenerator : public ASTConsumer {
40+
CompilerInstance &CI;
4041
DiagnosticsEngine &Diags;
4142
const std::string MainFileName;
4243
const std::string OutputFileName;
@@ -139,7 +140,7 @@ class PCHContainerGenerator : public ASTConsumer {
139140
const std::string &OutputFileName,
140141
std::unique_ptr<raw_pwrite_stream> OS,
141142
std::shared_ptr<PCHBuffer> Buffer)
142-
: Diags(CI.getDiagnostics()), MainFileName(MainFileName),
143+
: CI(CI), Diags(CI.getDiagnostics()), MainFileName(MainFileName),
143144
OutputFileName(OutputFileName), Ctx(nullptr),
144145
MMap(CI.getPreprocessor().getHeaderSearchInfo().getModuleMap()),
145146
FS(&CI.getVirtualFileSystem()),
@@ -317,19 +318,17 @@ class PCHContainerGenerator : public ASTConsumer {
317318
LLVM_DEBUG({
318319
// Print the IR for the PCH container to the debug output.
319320
llvm::SmallString<0> Buffer;
320-
clang::EmitBackendOutput(
321-
Diags, HeaderSearchOpts, CodeGenOpts, TargetOpts, LangOpts,
322-
Ctx.getTargetInfo().getDataLayoutString(), M.get(),
321+
clang::emitBackendOutput(
322+
CI, Ctx.getTargetInfo().getDataLayoutString(), M.get(),
323323
BackendAction::Backend_EmitLL, FS,
324324
std::make_unique<llvm::raw_svector_ostream>(Buffer));
325325
llvm::dbgs() << Buffer;
326326
});
327327

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

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

0 commit comments

Comments
 (0)