[libc-commits] [libc] [libc] Template the writing mode for the writer class (PR #111559)

Joseph Huber via libc-commits libc-commits at lists.llvm.org
Tue Oct 8 17:18:07 PDT 2024


jhuber6 wrote:

> I'm not a fan of templating the writer for basically the reasons described: It means that the converter code is duplicated for each printf variant, of which we currently have three but there may be more (see `syslog` and `dprintf`). Also, templating the converter just feels wrong to me since the converter isn't actually changing based on the template parameter.
> 
> That being said, I recognize that this is an important optimization for GPUs, so I have an alternate suggestion. Instead of templating the whole writer, only template `overflow_write`, then call it with a `switch` statement. This may seem like it's the same as before, but it has an important distinction: If we remove a `case` from the `switch` then that entire code path gets removed. Next, we add compile options to remove the `case`s that call the function pointer, and turn it on for GPUs, which makes it just a direct call.
> 
> Would that solution work for you?

It's not just the function pointer, but letting the compiler do more optimizations results in much better results. Without this it takes several seconds to compile a use of `sprintf` because there's so much unnecessary code in the `.o` that's never used. With this it's just a few seconds. If I template the overflow write, then I'd still need to propagate it somehow from the use, I tried simply guarding everything and hoping that the optimizer would figure it out, but no luck.

https://github.com/llvm/llvm-project/pull/111559


More information about the libc-commits mailing list