r309940 - [mips] Implement -muninit-const-in-rodata

Simon Dardis via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 3 07:01:17 PDT 2017


Author: sdardis
Date: Thu Aug  3 07:01:17 2017
New Revision: 309940

URL: http://llvm.org/viewvc/llvm-project?rev=309940&view=rev
Log:
[mips] Implement -muninit-const-in-rodata

This option when combined with -mgpopt and -membedded-data places all
uninitialized constant variables in the read-only section.

Reviewers: atanasyan, nitesh.jain

Differential Revision: https://reviews.llvm.org/D35917

Added:
    cfe/trunk/test/CodeGen/mips-uninit-const-in-ro.c
Modified:
    cfe/trunk/include/clang/Driver/Options.td
    cfe/trunk/include/clang/Frontend/CodeGenOptions.def
    cfe/trunk/lib/CodeGen/TargetInfo.cpp
    cfe/trunk/lib/Driver/ToolChains/Clang.cpp
    cfe/trunk/lib/Frontend/CompilerInvocation.cpp

Modified: cfe/trunk/include/clang/Driver/Options.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/Options.td?rev=309940&r1=309939&r2=309940&view=diff
==============================================================================
--- cfe/trunk/include/clang/Driver/Options.td (original)
+++ cfe/trunk/include/clang/Driver/Options.td Thu Aug  3 07:01:17 2017
@@ -2057,6 +2057,14 @@ def membedded_data : Flag<["-"], "membed
 def mno_embedded_data : Flag<["-"], "mno-embedded-data">, Group<m_Group>,
   HelpText<"Do not place constants in the .rodata section instead of the "
            ".sdata if they meet the -G <size> threshold (MIPS)">;
+def muninit_const_in_rodata : Flag<["-"], "muninit-const-in-rodata">,
+  Group<m_Group>, Flags<[DriverOption,CC1Option]>, HelpText<"Place "
+  "uninitialized constants in the read-only data section instead of"
+  " the common section (MIPS)">;
+def mno_uninit_const_in_rodata : Flag<["-"], "mno-uninit-const-in-rodata">,
+  Group<m_Group>, HelpText<"Do not place uninitialized constants in the "
+                           "read-only data section instead of the common"
+                           " section (MIPS)">;
 def mnan_EQ : Joined<["-"], "mnan=">, Group<m_Group>;
 def mabicalls : Flag<["-"], "mabicalls">, Group<m_Group>,
   HelpText<"Enable SVR4-style position-independent code (Mips only)">;

Modified: cfe/trunk/include/clang/Frontend/CodeGenOptions.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/CodeGenOptions.def?rev=309940&r1=309939&r2=309940&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/CodeGenOptions.def (original)
+++ cfe/trunk/include/clang/Frontend/CodeGenOptions.def Thu Aug  3 07:01:17 2017
@@ -101,6 +101,8 @@ CODEGENOPT(MergeAllConstants , 1, 1) ///
 CODEGENOPT(MergeFunctions    , 1, 0) ///< Set when -fmerge-functions is enabled.
 CODEGENOPT(MSVolatile        , 1, 0) ///< Set when /volatile:ms is enabled.
 CODEGENOPT(NoCommon          , 1, 0) ///< Set when -fno-common or C++ is enabled.
+CODEGENOPT(UInitCstDataInROData, 1, 0) ///< Set when -mgpopt & -membedded-data
+                                       ///< & -muinit-const-in-rodata is set
 CODEGENOPT(NoDwarfDirectoryAsm , 1, 0) ///< Set when -fno-dwarf-directory-asm is
                                        ///< enabled.
 CODEGENOPT(NoExecStack       , 1, 0) ///< Set when -Wa,--noexecstack is enabled.

Modified: cfe/trunk/lib/CodeGen/TargetInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/TargetInfo.cpp?rev=309940&r1=309939&r2=309940&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/TargetInfo.cpp (original)
+++ cfe/trunk/lib/CodeGen/TargetInfo.cpp Thu Aug  3 07:01:17 2017
@@ -6656,6 +6656,20 @@ public:
   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
                            CodeGen::CodeGenModule &CGM,
                            ForDefinition_t IsForDefinition) const override {
+
+    if (const VarDecl *VD = dyn_cast_or_null<VarDecl>(D)) {
+      if (CGM.getCodeGenOpts().UInitCstDataInROData &&
+          VD->getType().isConstQualified() && !VD->hasInit()) {
+        llvm::GlobalVariable *GVar = dyn_cast_or_null<llvm::GlobalVariable>(GV);
+        if (GVar && !GVar->hasSection()) {
+          GVar->setLinkage(llvm::GlobalValue::ExternalLinkage);
+          GVar->setSection("rodata");
+        }
+      }
+
+      return;
+    }
+
     const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
     if (!FD) return;
     llvm::Function *Fn = cast<llvm::Function>(GV);

Modified: cfe/trunk/lib/Driver/ToolChains/Clang.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/ToolChains/Clang.cpp?rev=309940&r1=309939&r2=309940&view=diff
==============================================================================
--- cfe/trunk/lib/Driver/ToolChains/Clang.cpp (original)
+++ cfe/trunk/lib/Driver/ToolChains/Clang.cpp Thu Aug  3 07:01:17 2017
@@ -1515,6 +1515,14 @@ void Clang::AddMIPSTargetArgs(const ArgL
         CmdArgs.push_back("-membedded-data=0");
       }
       EmbeddedData->claim();
+
+      if (Arg *A = Args.getLastArg(options::OPT_muninit_const_in_rodata,
+                                   options::OPT_mno_uninit_const_in_rodata)) {
+        if (A->getOption().matches(options::OPT_muninit_const_in_rodata)) {
+          CmdArgs.push_back("-muninit-const-in-rodata");
+          A->claim();
+        }
+      }
     }
 
   } else if ((!ABICalls || (!NoABICalls && ABICalls)) && WantGPOpt)

Modified: cfe/trunk/lib/Frontend/CompilerInvocation.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInvocation.cpp?rev=309940&r1=309939&r2=309940&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/CompilerInvocation.cpp (original)
+++ cfe/trunk/lib/Frontend/CompilerInvocation.cpp Thu Aug  3 07:01:17 2017
@@ -950,6 +950,8 @@ static bool ParseCodeGenArgs(CodeGenOpti
 
   Opts.Backchain = Args.hasArg(OPT_mbackchain);
 
+  Opts.UInitCstDataInROData = Args.hasArg(OPT_muninit_const_in_rodata);
+
   Opts.EmitCheckPathComponentsToStrip = getLastArgIntValue(
       Args, OPT_fsanitize_undefined_strip_path_components_EQ, 0, Diags);
 

Added: cfe/trunk/test/CodeGen/mips-uninit-const-in-ro.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/mips-uninit-const-in-ro.c?rev=309940&view=auto
==============================================================================
--- cfe/trunk/test/CodeGen/mips-uninit-const-in-ro.c (added)
+++ cfe/trunk/test/CodeGen/mips-uninit-const-in-ro.c Thu Aug  3 07:01:17 2017
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -triple mips-mti--elf -emit-llvm -mrelocation-model static \
+// RUN:            -target-feature +noabicalls -mllvm -mgpopt -mllvm \
+// RUN:            -membedded-data=1 -muninit-const-in-rodata -o - %s | \
+// RUN:   FileCheck %s
+
+// Test that -muninit-const-in-rodata places constant uninitialized structures
+// in the .rodata section rather than the commeon section.
+
+// CHECK: @a = global [8 x i32] zeroinitializer, section "rodata", align 4
+const int a[8];




More information about the cfe-commits mailing list