Skip to content

Commit a3b2552

Browse files
committed
Fix for PR44000. Optimization record for bytecode input missing.
Review is here: https://reviews.llvm.org/D70691
1 parent c5adbac commit a3b2552

File tree

2 files changed

+65
-25
lines changed

2 files changed

+65
-25
lines changed

clang/lib/CodeGen/CodeGenAction.cpp

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,24 @@ namespace clang {
8282
BackendConsumer *BackendCon;
8383
};
8484

85+
static void reportOptRecordError(Error E, DiagnosticsEngine &Diags,
86+
const CodeGenOptions CodeGenOpts) {
87+
handleAllErrors(
88+
std::move(E),
89+
[&](const RemarkSetupFileError &E) {
90+
Diags.Report(diag::err_cannot_open_file)
91+
<< CodeGenOpts.OptRecordFile << E.message();
92+
},
93+
[&](const RemarkSetupPatternError &E) {
94+
Diags.Report(diag::err_drv_optimization_remark_pattern)
95+
<< E.message() << CodeGenOpts.OptRecordPasses;
96+
},
97+
[&](const RemarkSetupFormatError &E) {
98+
Diags.Report(diag::err_drv_optimization_remark_format)
99+
<< CodeGenOpts.OptRecordFormat;
100+
});
101+
}
102+
85103
class BackendConsumer : public ASTConsumer {
86104
using LinkModule = CodeGenAction::LinkModule;
87105

@@ -268,29 +286,16 @@ namespace clang {
268286
CodeGenOpts, this));
269287

270288
Expected<std::unique_ptr<llvm::ToolOutputFile>> OptRecordFileOrErr =
271-
setupOptimizationRemarks(Ctx, CodeGenOpts.OptRecordFile,
272-
CodeGenOpts.OptRecordPasses,
273-
CodeGenOpts.OptRecordFormat,
274-
CodeGenOpts.DiagnosticsWithHotness,
275-
CodeGenOpts.DiagnosticsHotnessThreshold);
289+
setupOptimizationRemarks(
290+
Ctx, CodeGenOpts.OptRecordFile, CodeGenOpts.OptRecordPasses,
291+
CodeGenOpts.OptRecordFormat, CodeGenOpts.DiagnosticsWithHotness,
292+
CodeGenOpts.DiagnosticsHotnessThreshold);
276293

277294
if (Error E = OptRecordFileOrErr.takeError()) {
278-
handleAllErrors(
279-
std::move(E),
280-
[&](const RemarkSetupFileError &E) {
281-
Diags.Report(diag::err_cannot_open_file)
282-
<< CodeGenOpts.OptRecordFile << E.message();
283-
},
284-
[&](const RemarkSetupPatternError &E) {
285-
Diags.Report(diag::err_drv_optimization_remark_pattern)
286-
<< E.message() << CodeGenOpts.OptRecordPasses;
287-
},
288-
[&](const RemarkSetupFormatError &E) {
289-
Diags.Report(diag::err_drv_optimization_remark_format)
290-
<< CodeGenOpts.OptRecordFormat;
291-
});
295+
reportOptRecordError(std::move(E), Diags, CodeGenOpts);
292296
return;
293297
}
298+
294299
std::unique_ptr<llvm::ToolOutputFile> OptRecordFile =
295300
std::move(*OptRecordFileOrErr);
296301

@@ -1046,6 +1051,8 @@ void CodeGenAction::ExecuteAction() {
10461051
if (getCurrentFileKind().getLanguage() == Language::LLVM_IR) {
10471052
BackendAction BA = static_cast<BackendAction>(Act);
10481053
CompilerInstance &CI = getCompilerInstance();
1054+
auto &CodeGenOpts = CI.getCodeGenOpts();
1055+
auto &Diagnostics = CI.getDiagnostics();
10491056
std::unique_ptr<raw_pwrite_stream> OS =
10501057
GetOutputStream(CI, getCurrentFile(), BA);
10511058
if (BA != Backend_EmitNothing && !OS)
@@ -1064,23 +1071,41 @@ void CodeGenAction::ExecuteAction() {
10641071

10651072
const TargetOptions &TargetOpts = CI.getTargetOpts();
10661073
if (TheModule->getTargetTriple() != TargetOpts.Triple) {
1067-
CI.getDiagnostics().Report(SourceLocation(),
1068-
diag::warn_fe_override_module)
1074+
Diagnostics.Report(SourceLocation(),
1075+
diag::warn_fe_override_module)
10691076
<< TargetOpts.Triple;
10701077
TheModule->setTargetTriple(TargetOpts.Triple);
10711078
}
10721079

1073-
EmbedBitcode(TheModule.get(), CI.getCodeGenOpts(),
1080+
EmbedBitcode(TheModule.get(), CodeGenOpts,
10741081
MainFile->getMemBufferRef());
10751082

10761083
LLVMContext &Ctx = TheModule->getContext();
10771084
Ctx.setInlineAsmDiagnosticHandler(BitcodeInlineAsmDiagHandler,
1078-
&CI.getDiagnostics());
1085+
&Diagnostics);
1086+
1087+
Expected<std::unique_ptr<llvm::ToolOutputFile>> OptRecordFileOrErr =
1088+
setupOptimizationRemarks(
1089+
Ctx, CodeGenOpts.OptRecordFile,
1090+
CodeGenOpts.OptRecordPasses,
1091+
CodeGenOpts.OptRecordFormat,
1092+
CodeGenOpts.DiagnosticsWithHotness,
1093+
CodeGenOpts.DiagnosticsHotnessThreshold);
1094+
1095+
if (Error E = OptRecordFileOrErr.takeError()) {
1096+
reportOptRecordError(std::move(E), Diagnostics, CodeGenOpts);
1097+
return;
1098+
}
1099+
std::unique_ptr<llvm::ToolOutputFile> OptRecordFile =
1100+
std::move(*OptRecordFileOrErr);
10791101

1080-
EmitBackendOutput(CI.getDiagnostics(), CI.getHeaderSearchOpts(),
1081-
CI.getCodeGenOpts(), TargetOpts, CI.getLangOpts(),
1102+
EmitBackendOutput(Diagnostics, CI.getHeaderSearchOpts(),
1103+
CodeGenOpts, TargetOpts, CI.getLangOpts(),
10821104
CI.getTarget().getDataLayout(), TheModule.get(), BA,
10831105
std::move(OS));
1106+
1107+
if (OptRecordFile)
1108+
OptRecordFile->keep();
10841109
return;
10851110
}
10861111

clang/test/CodeGen/opt-record-1.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %clang_cc1 %s -opt-record-file=t1.opt -fopenmp -emit-llvm-bc -o %t.bc
2+
// RUN: %clang_cc1 -x ir %t.bc -opt-record-file %t.opt -fopenmp -emit-obj
3+
// RUN: cat %t.opt | FileCheck -check-prefix=CHECK %s
4+
5+
void foo(int *a, int *b, int *c) {
6+
#pragma omp parallel for
7+
for (int i = 0; i < 100; i++) {
8+
a[i] = b[i] + c[i];
9+
}
10+
}
11+
12+
// CHECK: --- !Missed
13+
// CHECK: Pass: inline
14+
// CHECK: Name: NoDefinition
15+
// CHECK: Function: foo

0 commit comments

Comments
 (0)