qianzhen created this revision.
Herald added a project: All.
qianzhen requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay.
Herald added a project: clang.
This patch adds a new option -fkeep-static-variables to emit all static
variables if required. This could be useful in cases where the presence of all
static variables as symbols in the object file are required.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D150221
Files:
clang/include/clang/Basic/CodeGenOptions.def
clang/include/clang/Driver/Options.td
clang/lib/CodeGen/CodeGenModule.cpp
clang/lib/Driver/ToolChains/Clang.cpp
clang/test/CodeGen/keep-static-variables.cpp
Index: clang/test/CodeGen/keep-static-variables.cpp
===================================================================
--- /dev/null
+++ clang/test/CodeGen/keep-static-variables.cpp
@@ -0,0 +1,23 @@
+// RUN: %clang_cc1 -fkeep-static-variables -emit-llvm %s -o - -triple=x86_64-unknown-linux-gnu | FileCheck %s --check-prefixes=CHECK,CHECK-ELF
+// RUN: %clang_cc1 -fkeep-static-variables -emit-llvm %s -o - -triple=powerpc64-ibm-aix-xcoff | FileCheck %s --check-prefixes=CHECK,CHECK-NON-ELF
+
+// CHECK: @_ZL2g1 = internal global i32 0, align 4
+// CHECK: @_ZL2g2 = internal global i32 1, align 4
+// CHECK: @_ZL2g3 = internal global i32 0, align 4
+// CHECK: @_ZL2g4 = internal global i32 2, align 4
+// CHECK-ELF: @llvm.compiler.used = appending global [4 x ptr] [ptr @_ZL2g1, ptr @_ZL2g2, ptr @_ZL2g3, ptr @_ZL2g4], section "llvm.metadata"
+// CHECK-NON-ELF: @llvm.used = appending global [4 x ptr] [ptr @_ZL2g1, ptr @_ZL2g2, ptr @_ZL2g3, ptr @_ZL2g4], section "llvm.metadata"
+
+static int g1;
+static int g2 = 1;
+static int g3;
+static int g4 = 2;
+
+int test1() {
+ g1 = 3;
+ return g1;
+}
+
+int test2() {
+ return g2;
+}
Index: clang/lib/Driver/ToolChains/Clang.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -7294,6 +7294,8 @@
Args.addOptInFlag(CmdArgs, options::OPT_fkeep_static_consts,
options::OPT_fno_keep_static_consts);
+ Args.addOptInFlag(CmdArgs, options::OPT_fkeep_static_variables,
+ options::OPT_fno_keep_static_variables);
Args.addOptInFlag(CmdArgs, options::OPT_fcomplete_member_pointers,
options::OPT_fno_complete_member_pointers);
Args.addOptOutFlag(CmdArgs, options::OPT_fcxx_static_destructors,
Index: clang/lib/CodeGen/CodeGenModule.cpp
===================================================================
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -2198,11 +2198,15 @@
if (D && D->hasAttr<UsedAttr>())
addUsedOrCompilerUsedGlobal(GV);
- if (CodeGenOpts.KeepStaticConsts && D && isa<VarDecl>(D)) {
+ if ((CodeGenOpts.KeepStaticConsts || CodeGenOpts.KeepStaticVariables) && D &&
+ isa<VarDecl>(D)) {
const auto *VD = cast<VarDecl>(D);
- if (VD->getType().isConstQualified() &&
- VD->getStorageDuration() == SD_Static)
- addUsedOrCompilerUsedGlobal(GV);
+ if (VD->getStorageDuration() == SD_Static) {
+ if (CodeGenOpts.KeepStaticVariables)
+ addUsedOrCompilerUsedGlobal(GV);
+ else if (CodeGenOpts.KeepStaticConsts && VD->getType().isConstQualified())
+ addUsedOrCompilerUsedGlobal(GV);
+ }
}
}
@@ -3084,11 +3088,14 @@
if (LangOpts.EmitAllDecls)
return true;
- if (CodeGenOpts.KeepStaticConsts) {
+ if (CodeGenOpts.KeepStaticConsts || CodeGenOpts.KeepStaticVariables) {
const auto *VD = dyn_cast<VarDecl>(Global);
- if (VD && VD->getType().isConstQualified() &&
- VD->getStorageDuration() == SD_Static)
- return true;
+ if (VD && VD->getStorageDuration() == SD_Static) {
+ if (CodeGenOpts.KeepStaticVariables)
+ return true;
+ else if (CodeGenOpts.KeepStaticConsts && VD->getType().isConstQualified())
+ return true;
+ }
}
return getContext().DeclMustBeEmitted(Global);
Index: clang/include/clang/Driver/Options.td
===================================================================
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -1697,6 +1697,10 @@
CodeGenOpts<"KeepStaticConsts">, DefaultFalse,
PosFlag<SetTrue, [CC1Option], "Keep">, NegFlag<SetFalse, [], "Don't keep">,
BothFlags<[NoXarchOption], " static const variables if unused">>;
+defm keep_static_variables : BoolFOption<"keep-static-variables",
+ CodeGenOpts<"KeepStaticVariables">, DefaultFalse,
+ PosFlag<SetTrue, [CC1Option], "Keep">, NegFlag<SetFalse, [], "Don't keep">,
+ BothFlags<[NoXarchOption], " static variables if unused">>;
defm fixed_point : BoolFOption<"fixed-point",
LangOpts<"FixedPoint">, DefaultFalse,
PosFlag<SetTrue, [CC1Option], "Enable">, NegFlag<SetFalse, [], "Disable">,
Index: clang/include/clang/Basic/CodeGenOptions.def
===================================================================
--- clang/include/clang/Basic/CodeGenOptions.def
+++ clang/include/clang/Basic/CodeGenOptions.def
@@ -472,6 +472,9 @@
/// Whether to emit unused static constants.
CODEGENOPT(KeepStaticConsts, 1, 0)
+/// Whether to emit all static variables.
+CODEGENOPT(KeepStaticVariables, 1, 0)
+
/// Whether to follow the AAPCS enforcing at least one read before storing to a volatile bitfield
CODEGENOPT(ForceAAPCSBitfieldLoad, 1, 0)
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits