[llvm-branch-commits] [libc] [libc] Modular printf option (float only) (PR #147426)

Simon Tatham via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Jul 9 01:45:24 PDT 2025


================
@@ -0,0 +1,41 @@
+#ifdef LIBC_COPT_PRINTF_MODULAR
+#include "src/__support/arg_list.h"
+
+#define LIBC_PRINTF_DEFINE_MODULAR
+#include "src/stdio/printf_core/float_dec_converter.h"
+#include "src/stdio/printf_core/float_hex_converter.h"
+#include "src/stdio/printf_core/parser.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace printf_core {
+template class Parser<internal::ArgList>;
+template class Parser<internal::DummyArgList<false>>;
+template class Parser<internal::DummyArgList<true>>;
+template class Parser<internal::StructArgList<false>>;
+template class Parser<internal::StructArgList<true>>;
+
+#define INSTANTIATE_CONVERT_FN(NAME)                                           \
+  template int NAME<WriteMode::FILL_BUFF_AND_DROP_OVERFLOW>(                   \
+      Writer<WriteMode::FILL_BUFF_AND_DROP_OVERFLOW> * writer,                 \
+      const FormatSection &to_conv);                                           \
+  template int NAME<WriteMode::FLUSH_TO_STREAM>(                               \
+      Writer<WriteMode::FLUSH_TO_STREAM> * writer,                             \
+      const FormatSection &to_conv);                                           \
+  template int NAME<WriteMode::RESIZE_AND_FILL_BUFF>(                          \
+      Writer<WriteMode::RESIZE_AND_FILL_BUFF> * writer,                        \
+      const FormatSection &to_conv);                                           \
+  template int NAME<WriteMode::RUNTIME_DISPATCH>(                              \
+      Writer<WriteMode::RUNTIME_DISPATCH> * writer,                            \
+      const FormatSection &to_conv)
+
+INSTANTIATE_CONVERT_FN(convert_float_decimal);
+INSTANTIATE_CONVERT_FN(convert_float_dec_exp);
+INSTANTIATE_CONVERT_FN(convert_float_dec_auto);
+INSTANTIATE_CONVERT_FN(convert_float_hex_exp);
+
+} // namespace printf_core
+} // namespace LIBC_NAMESPACE_DECL
+
+// Bring this file into the link if __printf_float is referenced.
+extern "C" void __printf_float() {}
----------------
statham-arm wrote:

One risk with this strategy: what happens if this file is compiled with `-ffunction-sections`? Then `__printf_float` might be in a different code section from the template instantiations, and a linker might garbage-collect the functions you actually wanted, treating the weak references to them as not enough reason to keep them.

If I wanted to be sure of this technique, I'd either enforce via compile options that everything in this file is in the same code section, or else add further `BFD_RELOC_NONE` links from `__printf_float` to the payload functions, to make sure there's an unbroken chain of non-weak references from the import of `__printf_float` to the functions that actually do the work.

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


More information about the llvm-branch-commits mailing list