-
Notifications
You must be signed in to change notification settings - Fork 83
Fix RAM usage by splitting deeply nested expressions #783
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
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"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.1There was a problem hiding this comment.
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.
Fix #777
TREE_VISITEDon theADDR_EXPRinplayback::function::get_addressand theADDR_EXPRinplayback::lvalue::get_address. (edit: doesn't work)I was able to build
cargowith 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.