On Tue, 10 Nov 2026, Filip Kastl wrote:

> On Tue 2026-11-10 15:21:25, Giuliano Belinassi wrote:
> > Hello.
> > 
> > On Tue, 2026-11-10 at 14:32 +0100, Richard Biener wrote:
> > > On Mon, 9 Nov 2026, Filip Kastl wrote:
> > > 
> > > > 1. The function gimple_fold_builtin_sprintf is only used in
> > > > gimple-ssa-sprintf.cc.  Move it there and make it private.
> > > > 
> > > > 2. Previously, gimple_fold_builtin_sprintf only handled cases where
> > > > the
> > > > format string were a directiveless plain string or a single %s. 
> > > > Here
> > > > we expand this to be able to handle multiple directives within a
> > > > single
> > > > format string, transforming each directive or plain string into
> > > > calls
> > > > to memcpy and strlen/strnlen if its size is unknown in compilation
> > > > time.
> > > > 
> > > > New tests were generated by Claude.
> > > > 
> > > > gcc/ChangeLog:
> > > > 
> > > >         * gimple-fold.cc (gimple_fold_builtin_sprintf): Move to
> > > >         gimple_fold_builtin_sprintf and make static.
> > > 
> > > Is the fact that the function isn't called from fold_stmt an
> > > oversight and would it not enable the followup by means of
> > > folding earlier?  I'd say it should be called (and that done
> > > as separate upfront patch).
> > 
> > Should it? If the folding of the sprintf happens before the strlen
> > pass, then sprintf would be optimized away and the pass has nothing to
> > work with to emit a warning/error to the user.  The original form is
> > gone and what is left is a sequence of calls to strlen and memcpy.
> > 
> > Furthermore, maybe no optimization pass would ever emit an sprintf
> > call?
> 
> Yeah, why would it actually be useful to have sprintf optimization be a part 
> of
> the gimple fold machinery?  Optimizing sprintf sounds to me more like a thing
> you want to do with a specific pass once or twice in the optimization 
> pipeline.
> 
> Also, this comes to mind: The version of gimple_fold_builtin_sprintf from the
> patch series requires the format string to be parsed.  If we ran it on each
> call to fold_stmt, I think we could end up parsing a single format string many
> times.

I'm not saying your enhancement should be done from fold_stmt, I'm
saying the existing optimization not triggering there is probably
an oversight.  Other functions like printf are folded.

Richard.

> Filip
> 

-- 
Richard Biener <[email protected]>
SUSE Software Solutions Germany GmbH,
Frankenstrasse 146, 90461 Nuernberg, Germany;
GF: Stefan Gaiser, Jochen Jaser, Abhinav Puri; (HRB 36809, AG Nuernberg)

Reply via email to