Skip to content

Commit

Permalink
[clang][modules] Fix CodeGen options that can affect the AST. (#78816)
Browse files Browse the repository at this point in the history
`OptimizationLevel` and `OptimizeSize` can affect the generated AST. They indirectly affect the `Optimize` and `OptimizeSize` frontend options, which in turn set predefined macro definitions.

This fixes rdar://121228252.
  • Loading branch information
ributzka committed Jan 23, 2024
1 parent 8ed1291 commit c5a33be
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
15 changes: 13 additions & 2 deletions clang/include/clang/Basic/CodeGenOptions.def
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
// 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.
//
//===----------------------------------------------------------------------===//
#ifndef CODEGENOPT
# error Define the CODEGENOPT macro to handle language options
Expand All @@ -27,6 +30,11 @@ CODEGENOPT(Name, Bits, Default)
CODEGENOPT(Name, Bits, Default)
#endif

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

CODEGENOPT(DisableIntegratedAS, 1, 0) ///< -no-integrated-as
CODEGENOPT(RelaxELFRelocations, 1, 1) ///< -Wa,-mrelax-relocations={yes,no}
CODEGENOPT(AsmVerbose , 1, 0) ///< -dA, -fverbose-asm.
Expand Down Expand Up @@ -193,8 +201,10 @@ ENUM_CODEGENOPT(ObjCDispatchMethod, ObjCDispatchMethodKind, 2, Legacy)
CODEGENOPT(ObjCConvertMessagesToRuntimeCalls , 1, 1)
CODEGENOPT(ObjCAvoidHeapifyLocalBlocks, 1, 0)

VALUE_CODEGENOPT(OptimizationLevel, 2, 0) ///< The -O[0-3] option specified.
VALUE_CODEGENOPT(OptimizeSize, 2, 0) ///< If -Os (==1) or -Oz (==2) is specified.

// 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.

CODEGENOPT(AtomicProfileUpdate , 1, 0) ///< Set -fprofile-update=atomic
/// Choose profile instrumenation kind or no instrumentation.
Expand Down Expand Up @@ -437,3 +447,4 @@ CODEGENOPT(CtorDtorReturnThis, 1, 0)
#undef CODEGENOPT
#undef ENUM_CODEGENOPT
#undef VALUE_CODEGENOPT
#undef AFFECTING_VALUE_CODEGENOPT
2 changes: 2 additions & 0 deletions clang/lib/Basic/CodeGenOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ void CodeGenOptions::resetNonModularOptions(StringRef ModuleFormat) {
#define ENUM_DEBUGOPT(Name, Type, Bits, Default)
#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)
#include "clang/Basic/CodeGenOptions.def"

// Next reset all debug options that can always be reset, because they never
Expand Down
11 changes: 7 additions & 4 deletions clang/test/ClangScanDeps/strip-codegen-args.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
// RUN: clang-scan-deps -compilation-database %t/cdb1.json -format experimental-full > %t/result1.txt
// RUN: FileCheck %s -input-file %t/result1.txt

// This tests that codegen option that do not affect the AST or generation of a module are removed.
// This tests that codegen option that do not affect the AST or generation of a
// module are removed. It also tests that the optimization options that affect
// the AST are not reset to -O0.

// CHECK: "modules": [
// CHECK-NEXT: {
// CHECK: "command-line": [
// CHECK-NOT: "-O0"
// CHECK-NOT: "-flto"
// CHECK-NOT: "-fno-autolink"
// CHECK-NOT: "-mrelax-relocations=no"
Expand All @@ -23,17 +26,17 @@
[
{
"directory": "DIR",
"command": "clang -Imodules/A -fmodules -fmodules-cache-path=DIR/module-cache -fimplicit-modules -flto -fno-autolink -Xclang -mrelax-relocations=no -fsyntax-only DIR/t1.m",
"command": "clang -Imodules/A -fmodules -fmodules-cache-path=DIR/module-cache -fimplicit-modules -O2 -flto -fno-autolink -Xclang -mrelax-relocations=no -fsyntax-only DIR/t1.m",
"file": "DIR/t1.m"
},
{
"directory": "DIR",
"command": "clang -Imodules/A -fmodules -fmodules-cache-path=DIR/module-cache -fimplicit-modules -flto=thin -fautolink -fsyntax-only DIR/t2.m",
"command": "clang -Imodules/A -fmodules -fmodules-cache-path=DIR/module-cache -fimplicit-modules -O2 -flto=thin -fautolink -fsyntax-only DIR/t2.m",
"file": "DIR/t2.m"
},
{
"directory": "DIR",
"command": "clang -Imodules/A -fmodules -fmodules-cache-path=DIR/module-cache -fimplicit-modules -flto=full -fsyntax-only DIR/t3.m",
"command": "clang -Imodules/A -fmodules -fmodules-cache-path=DIR/module-cache -fimplicit-modules -O2 -flto=full -fsyntax-only DIR/t3.m",
"file": "DIR/t2.m"
}
]
Expand Down

0 comments on commit c5a33be

Please sign in to comment.