Skip to content

[clang][modules] Partially revert 48d0eb518 to fix -gmodules output #124003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions clang/include/clang/CodeGen/BackendUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ enum BackendAction {
Backend_EmitObj ///< Emit native object files
};

void emitBackendOutput(CompilerInstance &CI, StringRef TDesc, llvm::Module *M,
BackendAction Action,
void emitBackendOutput(CompilerInstance &CI, CodeGenOptions &CGOpts,
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
13 changes: 7 additions & 6 deletions clang/lib/CodeGen/BackendUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,10 @@ class EmitAssemblyHelper {
}

public:
EmitAssemblyHelper(CompilerInstance &CI, llvm::Module *M,
EmitAssemblyHelper(CompilerInstance &CI, CodeGenOptions &CGOpts,
llvm::Module *M,
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS)
: CI(CI), Diags(CI.getDiagnostics()), CodeGenOpts(CI.getCodeGenOpts()),
: CI(CI), Diags(CI.getDiagnostics()), CodeGenOpts(CGOpts),
TargetOpts(CI.getTargetOpts()), LangOpts(CI.getLangOpts()),
TheModule(M), VFS(std::move(VFS)),
TargetTriple(TheModule->getTargetTriple()) {}
Expand Down Expand Up @@ -1363,14 +1364,14 @@ runThinLTOBackend(CompilerInstance &CI, ModuleSummaryIndex *CombinedIndex,
}
}

void clang::emitBackendOutput(CompilerInstance &CI, StringRef TDesc,
llvm::Module *M, BackendAction Action,
void clang::emitBackendOutput(CompilerInstance &CI, CodeGenOptions &CGOpts,
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 Down Expand Up @@ -1410,7 +1411,7 @@ void clang::emitBackendOutput(CompilerInstance &CI, StringRef TDesc,
}
}

EmitAssemblyHelper AsmHelper(CI, M, VFS);
EmitAssemblyHelper AsmHelper(CI, CGOpts, M, VFS);
AsmHelper.emitAssembly(Action, std::move(OS), BC);

// Verify clang's TargetInfo DataLayout against the LLVM TargetMachine's
Expand Down
8 changes: 5 additions & 3 deletions clang/lib/CodeGen/CodeGenAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,8 @@ void BackendConsumer::HandleTranslationUnit(ASTContext &C) {

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

emitBackendOutput(CI, C.getTargetInfo().getDataLayoutString(), getModule(),
emitBackendOutput(CI, CI.getCodeGenOpts(),
C.getTargetInfo().getDataLayoutString(), getModule(),
Action, FS, std::move(AsmOutStream), this);

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

emitBackendOutput(CI, CI.getTarget().getDataLayoutString(), TheModule.get(),
BA, CI.getFileManager().getVirtualFileSystemPtr(),
emitBackendOutput(CI, CI.getCodeGenOpts(),
CI.getTarget().getDataLayoutString(), TheModule.get(), BA,
CI.getFileManager().getVirtualFileSystemPtr(),
std::move(OS));
if (OptRecordFile)
OptRecordFile->keep();
Expand Down
8 changes: 4 additions & 4 deletions clang/lib/CodeGen/ObjectFilePCHContainerWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,16 +322,16 @@ class PCHContainerGenerator : public ASTConsumer {
// Print the IR for the PCH container to the debug output.
llvm::SmallString<0> Buffer;
clang::emitBackendOutput(
CI, Ctx.getTargetInfo().getDataLayoutString(), M.get(),
CI, CodeGenOpts, 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(CI, Ctx.getTargetInfo().getDataLayoutString(),
M.get(), BackendAction::Backend_EmitObj, FS,
std::move(OS));
clang::emitBackendOutput(CI, CodeGenOpts,
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
18 changes: 18 additions & 0 deletions clang/test/Modules/gmodules-codegenopts.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// UNSUPPORTED: target={{.*}}-zos{{.*}}, target={{.*}}-aix{{.*}}
// Check that the output from -gmodules can be loaded back by the compiler in
// the presence of certain options like optimization level that could break
// output. Note: without compiling twice the module is loaded from the in-memory
// module cache not load it from the object container.

// RUN: rm -rf %t
// RUN: %clang_cc1 -x objective-c -fmodules -fmodule-format=obj \
// RUN: -fimplicit-module-maps -fmodules-cache-path=%t %s \
// RUN: -I %S/Inputs -verify -O2

// Compile again, confirming we can load the module.
// RUN: %clang_cc1 -x objective-c -fmodules -fmodule-format=obj \
// RUN: -fimplicit-module-maps -fmodules-cache-path=%t %s \
// RUN: -I %S/Inputs -verify -O2

@import DebugObjC;
// expected-no-diagnostics
Loading