[clang] 6522b7c - [clang] Add option to clear AST memory before running LLVM passes

Arthur Eubanks via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 6 13:42:42 PDT 2021


Author: Arthur Eubanks
Date: 2021-10-06T13:42:22-07:00
New Revision: 6522b7cc3240626952ebc271d8714043a805911d

URL: https://github.com/llvm/llvm-project/commit/6522b7cc3240626952ebc271d8714043a805911d
DIFF: https://github.com/llvm/llvm-project/commit/6522b7cc3240626952ebc271d8714043a805911d.diff

LOG: [clang] Add option to clear AST memory before running LLVM passes

This is to save memory for Clang compiles.
Measuring building PassBuilder.cpp under /usr/bin/time, max rss goes from 0.93GB to 0.7GB.

This does not turn it by default yet.

I've turned on the option locally and run it over a good amount of files without any issues.

For more background, see
https://lists.llvm.org/pipermail/cfe-dev/2021-September/068930.html.

Reviewed By: dblaikie

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

Added: 
    clang/test/Misc/clear-ast-before-backend.c

Modified: 
    clang/include/clang/Basic/CodeGenOptions.def
    clang/include/clang/Driver/Options.td
    clang/lib/CodeGen/CodeGenAction.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/CodeGenOptions.def b/clang/include/clang/Basic/CodeGenOptions.def
index df4f8db6e16c..610123260175 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -54,6 +54,7 @@ CODEGENOPT(UniqueBasicBlockSectionNames, 1, 1) ///< Set for -funique-basic-block
 CODEGENOPT(EnableAIXExtendedAltivecABI, 1, 0) ///< Set for -mabi=vec-extabi. Enables the extended Altivec ABI on AIX.
 ENUM_CODEGENOPT(FramePointer, FramePointerKind, 2, FramePointerKind::None) /// frame-pointer: all,non-leaf,none
 
+CODEGENOPT(ClearASTBeforeBackend , 1, 0) ///< Free the AST before running backend code generation. Only works with -disable-free.
 CODEGENOPT(DisableFree       , 1, 0) ///< Don't free memory.
 CODEGENOPT(DiscardValueNames , 1, 0) ///< Discard Value Names from the IR (LLVMContext flag)
 CODEGENOPT(DisableLLVMPasses , 1, 0) ///< Don't run any LLVM IR passes to get

diff  --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index f2c119ad3794..7d1e6d181f97 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -5297,6 +5297,9 @@ def code_completion_with_fixits : Flag<["-"], "code-completion-with-fixits">,
 def disable_free : Flag<["-"], "disable-free">,
   HelpText<"Disable freeing of memory on exit">,
   MarshallingInfoFlag<FrontendOpts<"DisableFree">>;
+def clear_ast_before_backend : Flag<["-"], "clear-ast-before-backend">,
+  HelpText<"Clear the Clang AST before running backend code generation">,
+  MarshallingInfoFlag<CodeGenOpts<"ClearASTBeforeBackend">>;
 def enable_noundef_analysis : Flag<["-"], "enable-noundef-analysis">, Group<f_Group>,
   HelpText<"Enable analyzing function argument and return types for mandatory definedness">,
   MarshallingInfoFlag<CodeGenOpts<"EnableNoundefAttrs">>;

diff  --git a/clang/lib/CodeGen/CodeGenAction.cpp b/clang/lib/CodeGen/CodeGenAction.cpp
index 9fb5f29c94ab..4ca34db06ced 100644
--- a/clang/lib/CodeGen/CodeGenAction.cpp
+++ b/clang/lib/CodeGen/CodeGenAction.cpp
@@ -351,6 +351,11 @@ namespace clang {
         }
       }
 
+      // FIXME: Fix cleanup issues with clearing the AST when we properly free
+      // things.
+      if (CodeGenOpts.DisableFree && CodeGenOpts.ClearASTBeforeBackend)
+        C.getAllocator().Reset();
+
       EmbedBitcode(getModule(), CodeGenOpts, llvm::MemoryBufferRef());
 
       EmitBackendOutput(Diags, HeaderSearchOpts, CodeGenOpts, TargetOpts,

diff  --git a/clang/test/Misc/clear-ast-before-backend.c b/clang/test/Misc/clear-ast-before-backend.c
new file mode 100644
index 000000000000..8e46d3bcb068
--- /dev/null
+++ b/clang/test/Misc/clear-ast-before-backend.c
@@ -0,0 +1,3 @@
+// RUN: %clang_cc1 -clear-ast-before-backend %s -emit-obj -o /dev/null -O1
+
+void f() {}


        


More information about the cfe-commits mailing list