Skip to content

[clang][modules] Serialize CodeGenOptions #146422

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ ExpandModularHeadersPPCallbacks::ExpandModularHeadersPPCallbacks(
&Compiler.getTarget());

PP = std::make_unique<clang::Preprocessor>(Compiler.getPreprocessorOpts(),
Diags, LangOpts, Sources,
Diags, LangOpts, CGOpts, Sources,
*HeaderInfo, ModuleLoader,
/*IILookup=*/nullptr,
/*OwnsHeaderSearch=*/false);
PP->Initialize(Compiler.getTarget(), Compiler.getAuxTarget());
InitializePreprocessor(*PP, Compiler.getPreprocessorOpts(),
Compiler.getPCHContainerReader(),
Compiler.getFrontendOpts(), Compiler.getCodeGenOpts());
Compiler.getFrontendOpts());
ApplyHeaderSearchOptions(*HeaderInfo, HSOpts, LangOpts,
Compiler.getTarget().getTriple());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class ExpandModularHeadersPPCallbacks : public PPCallbacks {
DiagnosticOptions DiagOpts;
DiagnosticsEngine Diags;
LangOptions LangOpts;
CodeGenOptions CGOpts;
HeaderSearchOptions HSOpts;
TrivialModuleLoader ModuleLoader;

Expand Down
3 changes: 2 additions & 1 deletion clang-tools-extra/clangd/ModulesBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ bool IsModuleFileUpToDate(PathRef ModuleFilePath,

LangOptions LangOpts;
LangOpts.SkipODRCheckInGMF = true;
CodeGenOptions CGOpts;

FileManager FileMgr(FileSystemOptions(), VFS);

Expand All @@ -204,7 +205,7 @@ bool IsModuleFileUpToDate(PathRef ModuleFilePath,

PreprocessorOptions PPOpts;
TrivialModuleLoader ModuleLoader;
Preprocessor PP(PPOpts, *Diags, LangOpts, SourceMgr, HeaderInfo,
Preprocessor PP(PPOpts, *Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo,
ModuleLoader);

IntrusiveRefCntPtr<ModuleCache> ModCache = createCrossProcessModuleCache();
Expand Down
26 changes: 17 additions & 9 deletions clang/include/clang/Basic/CodeGenOptions.def
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
// that have enumeration type and VALUE_CODEGENOPT is a code
// generation option that describes a value rather than a flag.
//
// AFFECTING_VALUE_CODEGENOPT is used for code generation options that can
// affect the AST.
// COMPATIBLE_VALUE_CODEGENOPT is used for code generation options that affect
// the construction of the AST in a way that doesn't prevent
// interoperability (that is, the value can be different between an explicit
// module and the user of that module).
//
//===----------------------------------------------------------------------===//
#ifndef CODEGENOPT
Expand All @@ -30,11 +32,16 @@ CODEGENOPT(Name, Bits, Default)
CODEGENOPT(Name, Bits, Default)
#endif

#ifndef AFFECTING_VALUE_CODEGENOPT
# define AFFECTING_VALUE_CODEGENOPT(Name, Bits, Default) \
#ifndef COMPATIBLE_VALUE_CODEGENOPT
# define COMPATIBLE_VALUE_CODEGENOPT(Name, Bits, Default, Description) \
VALUE_CODEGENOPT(Name, Bits, Default)
#endif

#ifndef COMPATIBLE_ENUM_CODEGENOPT
# define COMPATIBLE_ENUM_CODEGENOPT(Name, Type, Bits, Default, Description) \
ENUM_CODEGENOPT(Name, Type, Bits, Default)
#endif

CODEGENOPT(DisableIntegratedAS, 1, 0) ///< -no-integrated-as
CODEGENOPT(Crel, 1, 0) ///< -Wa,--crel
CODEGENOPT(ImplicitMapSyms, 1, 0) ///< -Wa,-mmapsyms=implicit
Expand Down Expand Up @@ -216,9 +223,9 @@ CODEGENOPT(ObjCConvertMessagesToRuntimeCalls , 1, 1)
CODEGENOPT(ObjCAvoidHeapifyLocalBlocks, 1, 0)


// The optimization options affect frontend options, whicn in turn do affect the AST.
AFFECTING_VALUE_CODEGENOPT(OptimizationLevel, 2, 0) ///< The -O[0-3] option specified.
AFFECTING_VALUE_CODEGENOPT(OptimizeSize, 2, 0) ///< If -Os (==1) or -Oz (==2) is specified.
// The optimization options affect frontend options, which in turn do affect the AST.
COMPATIBLE_VALUE_CODEGENOPT(OptimizationLevel, 2, 0, "optimization level") ///< The -O[0-3] option specified.
COMPATIBLE_VALUE_CODEGENOPT(OptimizeSize, 2, 0, "optimizing for size") ///< If -Os (==1) or -Oz (==2) is specified.

CODEGENOPT(AtomicProfileUpdate , 1, 0) ///< Set -fprofile-update=atomic
CODEGENOPT(ContinuousProfileSync, 1, 0) ///< Enable continuous instrumentation profiling
Expand Down Expand Up @@ -383,7 +390,7 @@ VALUE_CODEGENOPT(SmallDataLimit, 32, 0)
VALUE_CODEGENOPT(SSPBufferSize, 32, 0)

/// The kind of inlining to perform.
ENUM_CODEGENOPT(Inlining, InliningMethod, 2, NormalInlining)
COMPATIBLE_ENUM_CODEGENOPT(Inlining, InliningMethod, 2, NormalInlining, "inlining kind")

/// The maximum stack size a function can have to be considered for inlining.
VALUE_CODEGENOPT(InlineMaxStackSize, 32, UINT_MAX)
Expand Down Expand Up @@ -494,4 +501,5 @@ ENUM_CODEGENOPT(WinX64EHUnwindV2, llvm::WinX64EHUnwindV2Mode,
#undef CODEGENOPT
#undef ENUM_CODEGENOPT
#undef VALUE_CODEGENOPT
#undef AFFECTING_VALUE_CODEGENOPT
#undef COMPATIBLE_VALUE_CODEGENOPT
#undef COMPATIBLE_ENUM_CODEGENOPT
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticSerializationKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ def err_ast_file_langopt_mismatch : Error<"%0 was %select{disabled|enabled}1 in
"precompiled file '%3' but is currently %select{disabled|enabled}2">;
def err_ast_file_langopt_value_mismatch : Error<
"%0 differs in precompiled file '%1' vs. current file">;
def err_ast_file_codegenopt_value_mismatch
: Error<"%0 differs in precompiled file '%1' vs. current file">;
def err_ast_file_diagopt_mismatch : Error<"%0 is currently enabled, but was not in "
"the precompiled file '%1'">;
def err_ast_file_modulecache_mismatch : Error<"precompiled file '%2' was compiled with module cache "
Expand Down
3 changes: 0 additions & 3 deletions clang/include/clang/Basic/LangOptions.def
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,6 @@ COMPATIBLE_LANGOPT(ModulesValidateTextualHeaderIncludes, 1, 1, "validation of te
BENIGN_LANGOPT(ModulesErrorRecovery, 1, 1, "automatically importing modules as needed when performing error recovery")
BENIGN_LANGOPT(ImplicitModules, 1, 1, "building modules that are not specified via -fmodule-file")
COMPATIBLE_LANGOPT(ModulesLocalVisibility, 1, 0, "local submodule visibility")
COMPATIBLE_LANGOPT(Optimize , 1, 0, "__OPTIMIZE__ predefined macro")
COMPATIBLE_LANGOPT(OptimizeSize , 1, 0, "__OPTIMIZE_SIZE__ predefined macro")
COMPATIBLE_LANGOPT(Static , 1, 0, "__STATIC__ predefined macro (as opposed to __DYNAMIC__)")
VALUE_LANGOPT(PackStruct , 32, 0,
"default struct packing maximum alignment")
Expand All @@ -224,7 +222,6 @@ COMPATIBLE_VALUE_LANGOPT(PIE , 1, 0, "is pie")
LANGOPT(ROPI , 1, 0, "Read-only position independence")
LANGOPT(RWPI , 1, 0, "Read-write position independence")
COMPATIBLE_LANGOPT(GNUInline , 1, 0, "GNU inline semantics")
COMPATIBLE_LANGOPT(NoInlineDefine , 1, 0, "__NO_INLINE__ predefined macro")
COMPATIBLE_LANGOPT(Deprecated , 1, 0, "__DEPRECATED predefined macro")
COMPATIBLE_LANGOPT(FastMath , 1, 0, "fast FP math optimizations, and __FAST_MATH__ predefined macro")
COMPATIBLE_LANGOPT(UnsafeFPMath , 1, 0, "Unsafe Floating Point Math")
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/Frontend/ASTUnit.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class ASTUnit {

private:
std::unique_ptr<LangOptions> LangOpts;
std::unique_ptr<CodeGenOptions> CGOpts = std::make_unique<CodeGenOptions>();
// FIXME: The documentation on \c LoadFrom* member functions states that the
// DiagnosticsEngine (and therefore DiagnosticOptions) must outlive the
// returned ASTUnit. This is not the case. Enfore it by storing non-owning
Expand Down
3 changes: 1 addition & 2 deletions clang/include/clang/Frontend/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ class CodeGenOptions;
/// environment ready to process a single file.
void InitializePreprocessor(Preprocessor &PP, const PreprocessorOptions &PPOpts,
const PCHContainerReader &PCHContainerRdr,
const FrontendOptions &FEOpts,
const CodeGenOptions &CodeGenOpts);
const FrontendOptions &FEOpts);

/// DoPrintPreprocessedInput - Implement -E mode.
void DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS,
Expand Down
8 changes: 6 additions & 2 deletions clang/include/clang/Lex/Preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#ifndef LLVM_CLANG_LEX_PREPROCESSOR_H
#define LLVM_CLANG_LEX_PREPROCESSOR_H

#include "clang/Basic/CodeGenOptions.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/DiagnosticIDs.h"
#include "clang/Basic/IdentifierTable.h"
Expand Down Expand Up @@ -155,6 +156,7 @@ class Preprocessor {
const PreprocessorOptions &PPOpts;
DiagnosticsEngine *Diags;
const LangOptions &LangOpts;
const CodeGenOptions &CGOpts;
const TargetInfo *Target = nullptr;
const TargetInfo *AuxTarget = nullptr;
FileManager &FileMgr;
Expand Down Expand Up @@ -1181,8 +1183,9 @@ class Preprocessor {

public:
Preprocessor(const PreprocessorOptions &PPOpts, DiagnosticsEngine &diags,
const LangOptions &LangOpts, SourceManager &SM,
HeaderSearch &Headers, ModuleLoader &TheModuleLoader,
const LangOptions &LangOpts, const CodeGenOptions &CGOPts,
SourceManager &SM, HeaderSearch &Headers,
ModuleLoader &TheModuleLoader,
IdentifierInfoLookup *IILookup = nullptr,
bool OwnsHeaderSearch = false,
TranslationUnitKind TUKind = TU_Complete);
Expand Down Expand Up @@ -1216,6 +1219,7 @@ class Preprocessor {
void setDiagnostics(DiagnosticsEngine &D) { Diags = &D; }

const LangOptions &getLangOpts() const { return LangOpts; }
const CodeGenOptions &getCodeGenOpts() const { return CGOpts; }
const TargetInfo &getTargetInfo() const { return *Target; }
const TargetInfo *getAuxTargetInfo() const { return AuxTarget; }
FileManager &getFileManager() const { return FileMgr; }
Expand Down
5 changes: 4 additions & 1 deletion clang/include/clang/Serialization/ASTBitCodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace serialization {
/// Version 4 of AST files also requires that the version control branch and
/// revision match exactly, since there is no backward compatibility of
/// AST files at this time.
const unsigned VERSION_MAJOR = 34;
const unsigned VERSION_MAJOR = 35;

/// AST file minor version number supported by this version of
/// Clang.
Expand Down Expand Up @@ -399,6 +399,9 @@ enum OptionsRecordTypes {

/// Record code for the preprocessor options table.
PREPROCESSOR_OPTIONS,

/// Record code for the codegen options table.
CODEGEN_OPTIONS,
};

/// Record codes for the unhashed control block.
Expand Down
34 changes: 26 additions & 8 deletions clang/include/clang/Serialization/ASTReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class ASTContext;
class ASTDeserializationListener;
class ASTReader;
class ASTRecordReader;
class CodeGenOptions;
class CXXTemporary;
class Decl;
class DeclarationName;
Expand Down Expand Up @@ -137,6 +138,15 @@ class ASTReaderListener {
return false;
}

/// Receives the codegen options.
///
/// \returns true to indicate the options are invalid or false otherwise.
virtual bool ReadCodeGenOptions(const CodeGenOptions &CGOpts,
StringRef ModuleFilename, bool Complain,
bool AllowCompatibleDifferences) {
return false;
}

/// Receives the target options.
///
/// \returns true to indicate the target options are invalid, or false
Expand Down Expand Up @@ -281,6 +291,9 @@ class ChainedASTReaderListener : public ASTReaderListener {
bool ReadLanguageOptions(const LangOptions &LangOpts,
StringRef ModuleFilename, bool Complain,
bool AllowCompatibleDifferences) override;
bool ReadCodeGenOptions(const CodeGenOptions &CGOpts,
StringRef ModuleFilename, bool Complain,
bool AllowCompatibleDifferences) override;
bool ReadTargetOptions(const TargetOptions &TargetOpts,
StringRef ModuleFilename, bool Complain,
bool AllowCompatibleDifferences) override;
Expand Down Expand Up @@ -322,6 +335,9 @@ class PCHValidator : public ASTReaderListener {
bool ReadLanguageOptions(const LangOptions &LangOpts,
StringRef ModuleFilename, bool Complain,
bool AllowCompatibleDifferences) override;
bool ReadCodeGenOptions(const CodeGenOptions &CGOpts,
StringRef ModuleFilename, bool Complain,
bool AllowCompatibleDifferences) override;
bool ReadTargetOptions(const TargetOptions &TargetOpts,
StringRef ModuleFilename, bool Complain,
bool AllowCompatibleDifferences) override;
Expand Down Expand Up @@ -1586,6 +1602,10 @@ class ASTReader
StringRef ModuleFilename, bool Complain,
ASTReaderListener &Listener,
bool AllowCompatibleDifferences);
static bool ParseCodeGenOptions(const RecordData &Record,
StringRef ModuleFilename, bool Complain,
ASTReaderListener &Listener,
bool AllowCompatibleDifferences);
static bool ParseTargetOptions(const RecordData &Record,
StringRef ModuleFilename, bool Complain,
ASTReaderListener &Listener,
Expand Down Expand Up @@ -1996,14 +2016,12 @@ class ASTReader

/// Determine whether the given AST file is acceptable to load into a
/// translation unit with the given language and target options.
static bool isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
const ModuleCache &ModCache,
const PCHContainerReader &PCHContainerRdr,
const LangOptions &LangOpts,
const TargetOptions &TargetOpts,
const PreprocessorOptions &PPOpts,
StringRef ExistingModuleCachePath,
bool RequireStrictOptionMatches = false);
static bool isAcceptableASTFile(
StringRef Filename, FileManager &FileMgr, const ModuleCache &ModCache,
const PCHContainerReader &PCHContainerRdr, const LangOptions &LangOpts,
const CodeGenOptions &CGOpts, const TargetOptions &TargetOpts,
const PreprocessorOptions &PPOpts, StringRef ExistingModuleCachePath,
bool RequireStrictOptionMatches = false);

/// Returns the suggested contents of the predefines buffer,
/// which contains a (typically-empty) subset of the predefines
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Basic/CodeGenOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ void CodeGenOptions::resetNonModularOptions(StringRef ModuleFormat) {
#define CODEGENOPT(Name, Bits, Default) Name = Default;
#define ENUM_CODEGENOPT(Name, Type, Bits, Default) set##Name(Default);
// Do not reset AST affecting code generation options.
#define AFFECTING_VALUE_CODEGENOPT(Name, Bits, Default)
#define COMPATIBLE_VALUE_CODEGENOPT(Name, Bits, Default, Description)
#define COMPATIBLE_ENUM_CODEGENOPT(Name, Type, Bits, Default, Description)
#include "clang/Basic/CodeGenOptions.def"

// Next reset all debug options that can always be reset, because they never
Expand Down
Loading
Loading