Skip to content

Commit 2e5af56

Browse files
committed
[C++20] [Modules] Allow to compile a pcm with and without -fPIC
seperately We can compile a module unit in 2 phase compilaton: ``` clang++ -std=c++20 a.cppm --precompile -o a.pcm clang++ -std=c++20 a.pcm -c -o a.o ``` And it is a general requirement that we need to compile a translation unit with and without -fPIC for static and shared libraries. But for C++20 modules with 2 phase compilation, it may be waste of time to compile them 2 times completely. It may be fine to generate one BMI and compile it with and without -fPIC seperately. e.g., ``` clang++ -std=c++20 a.cppm --precompile -o a.pcm clang++ -std=c++20 a.pcm -c -o a.o clang++ -std=c++20 a.pcm -c -fPIC -o a-PIC.o ``` Then we can save the time to parse a.cppm repeatedly.
1 parent 5ccf546 commit 2e5af56

File tree

8 files changed

+54
-15
lines changed

8 files changed

+54
-15
lines changed

clang/include/clang/Frontend/ASTUnit.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -691,16 +691,19 @@ class ASTUnit {
691691
/// lifetime is expected to extend past that of the returned ASTUnit.
692692
///
693693
/// \returns - The initialized ASTUnit or null if the AST failed to load.
694-
static std::unique_ptr<ASTUnit> LoadFromASTFile(
695-
const std::string &Filename, const PCHContainerReader &PCHContainerRdr,
696-
WhatToLoad ToLoad, IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
697-
const FileSystemOptions &FileSystemOpts,
698-
std::shared_ptr<HeaderSearchOptions> HSOpts, bool OnlyLocalDecls = false,
699-
CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::None,
700-
bool AllowASTWithCompilerErrors = false,
701-
bool UserFilesAreVolatile = false,
702-
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
703-
llvm::vfs::getRealFileSystem());
694+
static std::unique_ptr<ASTUnit>
695+
LoadFromASTFile(const std::string &Filename,
696+
const PCHContainerReader &PCHContainerRdr, WhatToLoad ToLoad,
697+
IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
698+
const FileSystemOptions &FileSystemOpts,
699+
std::shared_ptr<HeaderSearchOptions> HSOpts,
700+
std::shared_ptr<LangOptions> LangOpts = nullptr,
701+
bool OnlyLocalDecls = false,
702+
CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::None,
703+
bool AllowASTWithCompilerErrors = false,
704+
bool UserFilesAreVolatile = false,
705+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
706+
llvm::vfs::getRealFileSystem());
704707

705708
private:
706709
/// Helper function for \c LoadFromCompilerInvocation() and

clang/include/clang/Frontend/CompilerInstance.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,9 @@ class CompilerInstance : public ModuleLoader {
311311

312312
LangOptions &getLangOpts() { return Invocation->getLangOpts(); }
313313
const LangOptions &getLangOpts() const { return Invocation->getLangOpts(); }
314+
std::shared_ptr<LangOptions> getLangOptsPtr() const {
315+
return Invocation->getLangOptsPtr();
316+
}
314317

315318
PreprocessorOptions &getPreprocessorOpts() {
316319
return Invocation->getPreprocessorOpts();

clang/include/clang/Frontend/CompilerInvocation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ class CompilerInvocation : public CompilerInvocationBase {
271271
std::shared_ptr<PreprocessorOptions> getPreprocessorOptsPtr() {
272272
return PPOpts;
273273
}
274+
std::shared_ptr<LangOptions> getLangOptsPtr() { return LangOpts; }
274275
/// @}
275276

276277
/// Create a compiler invocation from a list of input options.

clang/lib/Frontend/ASTUnit.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,17 @@ class ASTInfoCollector : public ASTReaderListener {
540540
if (InitializedLanguage)
541541
return false;
542542

543+
// FIXME: We did similar things in ReadHeaderSearchOptions too. But such
544+
// style is not scaling. Probably we need to invite some mechanism to
545+
// handle such patterns generally.
546+
auto PICLevel = LangOpt.PICLevel;
547+
auto PIE = LangOpt.PIE;
548+
543549
LangOpt = LangOpts;
550+
551+
LangOpt.PICLevel = PICLevel;
552+
LangOpt.PIE = PIE;
553+
544554
InitializedLanguage = true;
545555

546556
updated();
@@ -790,7 +800,8 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
790800
const std::string &Filename, const PCHContainerReader &PCHContainerRdr,
791801
WhatToLoad ToLoad, IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
792802
const FileSystemOptions &FileSystemOpts,
793-
std::shared_ptr<HeaderSearchOptions> HSOpts, bool OnlyLocalDecls,
803+
std::shared_ptr<HeaderSearchOptions> HSOpts,
804+
std::shared_ptr<LangOptions> LangOpts, bool OnlyLocalDecls,
794805
CaptureDiagsKind CaptureDiagnostics, bool AllowASTWithCompilerErrors,
795806
bool UserFilesAreVolatile, IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
796807
std::unique_ptr<ASTUnit> AST(new ASTUnit(true));
@@ -804,7 +815,7 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
804815

805816
ConfigureDiags(Diags, *AST, CaptureDiagnostics);
806817

807-
AST->LangOpts = std::make_shared<LangOptions>();
818+
AST->LangOpts = LangOpts ? LangOpts : std::make_shared<LangOptions>();
808819
AST->OnlyLocalDecls = OnlyLocalDecls;
809820
AST->CaptureDiagnostics = CaptureDiagnostics;
810821
AST->Diagnostics = Diags;

clang/lib/Frontend/FrontendAction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
689689
std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
690690
std::string(InputFile), CI.getPCHContainerReader(),
691691
ASTUnit::LoadEverything, Diags, CI.getFileSystemOpts(),
692-
CI.getHeaderSearchOptsPtr());
692+
CI.getHeaderSearchOptsPtr(), CI.getLangOptsPtr());
693693

694694
if (!AST)
695695
return false;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// REQUIRES: x86-registered-target
2+
3+
// RUN: rm -rf %t
4+
// RUN: mkdir %t
5+
6+
// RUN: %clang_cc1 -std=c++20 %s -pic-level 2 -emit-llvm -o - | FileCheck %s
7+
// RUN: %clang_cc1 -std=c++20 %s -pic-level 2 -fmodule-output=%t/m.pcm -emit-llvm -o - \
8+
// RUN: | FileCheck %s
9+
//
10+
// RUN: %clang_cc1 -std=c++20 %s -emit-module-interface -o %t/m.pcm
11+
// RUN: %clang_cc1 -std=c++20 %t/m.pcm -pic-level 2 -emit-llvm -o - | FileCheck %s
12+
// RUN: %clang_cc1 -std=c++20 %t/m.pcm -emit-llvm -o - | FileCheck %s --check-prefix=NOPIC
13+
14+
export module m;
15+
export int x;
16+
export int func() {
17+
return x;
18+
}
19+
20+
// CHECK: ![[METADATA_NUM:[0-9]+]] = !{{{.*}}, !"PIC Level", i32 2}
21+
// NOPIC-NOT: ![[METADATA_NUM:[0-9]+]] = !{{{.*}}, !"PIC Level", i32 2}

clang/tools/c-index-test/core_main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ static bool printSourceSymbolsFromModule(StringRef modulePath,
276276
CompilerInstance::createDiagnostics(new DiagnosticOptions());
277277
std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile(
278278
std::string(modulePath), *pchRdr, ASTUnit::LoadASTOnly, Diags,
279-
FileSystemOpts, HSOpts,
279+
FileSystemOpts, HSOpts, /*LangOpts=*/nullptr,
280280
/*OnlyLocalDecls=*/true, CaptureDiagsKind::None,
281281
/*AllowASTWithCompilerErrors=*/true,
282282
/*UserFilesAreVolatile=*/false);

clang/tools/libclang/CIndex.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3890,7 +3890,7 @@ enum CXErrorCode clang_createTranslationUnit2(CXIndex CIdx,
38903890
std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile(
38913891
ast_filename, CXXIdx->getPCHContainerOperations()->getRawReader(),
38923892
ASTUnit::LoadEverything, Diags, FileSystemOpts, HSOpts,
3893-
CXXIdx->getOnlyLocalDecls(), CaptureDiagsKind::All,
3893+
/*LangOpts=*/nullptr, CXXIdx->getOnlyLocalDecls(), CaptureDiagsKind::All,
38943894
/*AllowASTWithCompilerErrors=*/true,
38953895
/*UserFilesAreVolatile=*/true);
38963896
*out_TU = MakeCXTranslationUnit(CXXIdx, std::move(AU));

0 commit comments

Comments
 (0)