Skip to content

Commit 9fbf5cf

Browse files
authored
[clang][modules] Partially revert 48d0eb5 to fix -gmodules output (#124003)
With the changes in 48d0eb5, the CodeGenOptions used to emit .pcm files with -fmodule-format=obj (-gmodules) were the ones from the original invocation, rather than the ones specifically crafted for outputting the pcm. This was causing the pcm to be written with only the debug info and without the __clangast section in some cases (e.g. -O2). This unforunately was not covered by existing tests, because compiling and loading a module within a single compilation load the ast content from the in-memory module cache rather than reading it from the pcm file that was written. This broke bootstrapping a build of clang with modules enabled on Darwin. rdar://143418834
1 parent 96dbd00 commit 9fbf5cf

File tree

5 files changed

+36
-15
lines changed

5 files changed

+36
-15
lines changed

clang/include/clang/CodeGen/BackendUtil.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ enum BackendAction {
3939
Backend_EmitObj ///< Emit native object files
4040
};
4141

42-
void emitBackendOutput(CompilerInstance &CI, StringRef TDesc, llvm::Module *M,
43-
BackendAction Action,
42+
void emitBackendOutput(CompilerInstance &CI, CodeGenOptions &CGOpts,
43+
StringRef TDesc, llvm::Module *M, BackendAction Action,
4444
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
4545
std::unique_ptr<raw_pwrite_stream> OS,
4646
BackendConsumer *BC = nullptr);

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,10 @@ class EmitAssemblyHelper {
206206
}
207207

208208
public:
209-
EmitAssemblyHelper(CompilerInstance &CI, llvm::Module *M,
209+
EmitAssemblyHelper(CompilerInstance &CI, CodeGenOptions &CGOpts,
210+
llvm::Module *M,
210211
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS)
211-
: CI(CI), Diags(CI.getDiagnostics()), CodeGenOpts(CI.getCodeGenOpts()),
212+
: CI(CI), Diags(CI.getDiagnostics()), CodeGenOpts(CGOpts),
212213
TargetOpts(CI.getTargetOpts()), LangOpts(CI.getLangOpts()),
213214
TheModule(M), VFS(std::move(VFS)),
214215
TargetTriple(TheModule->getTargetTriple()) {}
@@ -1364,14 +1365,14 @@ runThinLTOBackend(CompilerInstance &CI, ModuleSummaryIndex *CombinedIndex,
13641365
}
13651366
}
13661367

1367-
void clang::emitBackendOutput(CompilerInstance &CI, StringRef TDesc,
1368-
llvm::Module *M, BackendAction Action,
1368+
void clang::emitBackendOutput(CompilerInstance &CI, CodeGenOptions &CGOpts,
1369+
StringRef TDesc, llvm::Module *M,
1370+
BackendAction Action,
13691371
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
13701372
std::unique_ptr<raw_pwrite_stream> OS,
13711373
BackendConsumer *BC) {
13721374
llvm::TimeTraceScope TimeScope("Backend");
13731375
DiagnosticsEngine &Diags = CI.getDiagnostics();
1374-
const auto &CGOpts = CI.getCodeGenOpts();
13751376

13761377
std::unique_ptr<llvm::Module> EmptyModule;
13771378
if (!CGOpts.ThinLTOIndexFile.empty()) {
@@ -1411,7 +1412,7 @@ void clang::emitBackendOutput(CompilerInstance &CI, StringRef TDesc,
14111412
}
14121413
}
14131414

1414-
EmitAssemblyHelper AsmHelper(CI, M, VFS);
1415+
EmitAssemblyHelper AsmHelper(CI, CGOpts, M, VFS);
14151416
AsmHelper.emitAssembly(Action, std::move(OS), BC);
14161417

14171418
// Verify clang's TargetInfo DataLayout against the LLVM TargetMachine's

clang/lib/CodeGen/CodeGenAction.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,8 @@ void BackendConsumer::HandleTranslationUnit(ASTContext &C) {
312312

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

315-
emitBackendOutput(CI, C.getTargetInfo().getDataLayoutString(), getModule(),
315+
emitBackendOutput(CI, CI.getCodeGenOpts(),
316+
C.getTargetInfo().getDataLayoutString(), getModule(),
316317
Action, FS, std::move(AsmOutStream), this);
317318

318319
Ctx.setDiagnosticHandler(std::move(OldDiagnosticHandler));
@@ -1173,8 +1174,9 @@ void CodeGenAction::ExecuteAction() {
11731174
std::unique_ptr<llvm::ToolOutputFile> OptRecordFile =
11741175
std::move(*OptRecordFileOrErr);
11751176

1176-
emitBackendOutput(CI, CI.getTarget().getDataLayoutString(), TheModule.get(),
1177-
BA, CI.getFileManager().getVirtualFileSystemPtr(),
1177+
emitBackendOutput(CI, CI.getCodeGenOpts(),
1178+
CI.getTarget().getDataLayoutString(), TheModule.get(), BA,
1179+
CI.getFileManager().getVirtualFileSystemPtr(),
11781180
std::move(OS));
11791181
if (OptRecordFile)
11801182
OptRecordFile->keep();

clang/lib/CodeGen/ObjectFilePCHContainerWriter.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,16 +322,16 @@ class PCHContainerGenerator : public ASTConsumer {
322322
// Print the IR for the PCH container to the debug output.
323323
llvm::SmallString<0> Buffer;
324324
clang::emitBackendOutput(
325-
CI, Ctx.getTargetInfo().getDataLayoutString(), M.get(),
325+
CI, CodeGenOpts, Ctx.getTargetInfo().getDataLayoutString(), M.get(),
326326
BackendAction::Backend_EmitLL, FS,
327327
std::make_unique<llvm::raw_svector_ostream>(Buffer));
328328
llvm::dbgs() << Buffer;
329329
});
330330

331331
// Use the LLVM backend to emit the pch container.
332-
clang::emitBackendOutput(CI, Ctx.getTargetInfo().getDataLayoutString(),
333-
M.get(), BackendAction::Backend_EmitObj, FS,
334-
std::move(OS));
332+
clang::emitBackendOutput(CI, CodeGenOpts,
333+
Ctx.getTargetInfo().getDataLayoutString(), M.get(),
334+
BackendAction::Backend_EmitObj, FS, std::move(OS));
335335

336336
// Free the memory for the temporary buffer.
337337
llvm::SmallVector<char, 0> Empty;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// UNSUPPORTED: target={{.*}}-zos{{.*}}, target={{.*}}-aix{{.*}}
2+
// Check that the output from -gmodules can be loaded back by the compiler in
3+
// the presence of certain options like optimization level that could break
4+
// output. Note: without compiling twice the module is loaded from the in-memory
5+
// module cache not load it from the object container.
6+
7+
// RUN: rm -rf %t
8+
// RUN: %clang_cc1 -x objective-c -fmodules -fmodule-format=obj \
9+
// RUN: -fimplicit-module-maps -fmodules-cache-path=%t %s \
10+
// RUN: -I %S/Inputs -verify -O2
11+
12+
// Compile again, confirming we can load the module.
13+
// RUN: %clang_cc1 -x objective-c -fmodules -fmodule-format=obj \
14+
// RUN: -fimplicit-module-maps -fmodules-cache-path=%t %s \
15+
// RUN: -I %S/Inputs -verify -O2
16+
17+
@import DebugObjC;
18+
// expected-no-diagnostics

0 commit comments

Comments
 (0)