Skip to content

Conversation

@antoyo
Copy link
Contributor

@antoyo antoyo commented Oct 26, 2025

Fix #777

  • try only setting TREE_VISITED on the ADDR_EXPR in playback::function::get_address and the ADDR_EXPR in playback::lvalue::get_address. (edit: doesn't work)

I was able to build cargo with 5-10 GB RAM while it used to take +64 GB.

This function would generate deeply nested expressions which made GCC use huge amount of RAM.

src/builder.rs Outdated
// By assigning intermediate expressions to a variable, this allow us to avoid deeply nested
// expressions and GCC will use much less RAM.
fn assign_to_var(&self, value: RValue<'gcc>) -> RValue<'gcc> {
let var = self.current_func().new_local(self.location, value.get_type(), "addResult");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let var = self.current_func().new_local(self.location, value.get_type(), "addResult");
let var = self.current_func().new_local(self.location, value.get_type(), "opResult");

self.bitcast_if_needed(res, result_type)
}

// GCC doesn't like deeply nested expressions.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is due to:

    /* Pre-mark tree nodes with TREE_VISITED so that they can be
       deeply unshared during gimplification (including across
       functions); this requires LANG_HOOKS_DEEP_UNSHARING to be true.  */
    TREE_VISITED (inner) = 1;

In jit-playback.h.

It is NOT GCC that does not like deeply nested expressions BUT rather libgccjit that does not like it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this patch and it doesn't help with memory usage:

From 6e5317dacd4930be53d0ea37de0104d009189319 Mon Sep 17 00:00:00 2001
From: Antoni Boucher <bouanto@zoho.com>
Date: Sun, 26 Oct 2025 10:06:53 -0400
Subject: [PATCH] Fix TREE_VISITED

---
 gcc/jit/jit-playback.cc | 2 ++
 gcc/jit/jit-playback.h  | 5 ++++-
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/gcc/jit/jit-playback.cc b/gcc/jit/jit-playback.cc
index e41408b635c..554057255bc 100644
--- a/gcc/jit/jit-playback.cc
+++ b/gcc/jit/jit-playback.cc
@@ -2186,6 +2186,7 @@ get_address (location *loc)
   tree t_thistype = TREE_TYPE (t_lvalue);
   tree t_ptrtype = build_pointer_type (t_thistype);
   tree ptr = fold_build1 (ADDR_EXPR, t_ptrtype, t_lvalue);
+  TREE_VISITED (ptr) = 1;
   if (loc)
     get_context ()->set_tree_location (ptr, loc);
   if (mark_addressable (loc))
@@ -2342,6 +2343,7 @@ playback::function::get_address (location *loc)
   tree t_fndecl = as_fndecl ();
   tree t_fntype = TREE_TYPE (t_fndecl);
   tree t_fnptr = build1 (ADDR_EXPR, build_pointer_type (t_fntype), t_fndecl);
+  TREE_VISITED (t_fnptr) = 1;
   if (loc)
     m_ctxt->set_tree_location (t_fnptr, loc);
   return new rvalue (m_ctxt, t_fnptr);
diff --git a/gcc/jit/jit-playback.h b/gcc/jit/jit-playback.h
index 6f297136edc..fb1914e8bc7 100644
--- a/gcc/jit/jit-playback.h
+++ b/gcc/jit/jit-playback.h
@@ -757,7 +757,10 @@ public:
     /* Pre-mark tree nodes with TREE_VISITED so that they can be
        deeply unshared during gimplification (including across
        functions); this requires LANG_HOOKS_DEEP_UNSHARING to be true.  */
-    TREE_VISITED (inner) = 1;
+    // TODO: try only setting TREE_VISITED on the ADDR_EXPR in
+    // playback::function::get_address and the ADDR_EXPR in
+    // playback::lvalue::get_address.
+    //TREE_VISITED (inner) = 1;
   }
 
   rvalue *
-- 
2.51.1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this helps, the memory usage happens in gimplify_function_tree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OOM when compiling cargo

2 participants