[libc-commits] [libc] ecca895 - [libc] add compile option for printf arg type array

Michael Jones via libc-commits libc-commits at lists.llvm.org
Fri Aug 26 11:38:13 PDT 2022


Author: Michael Jones
Date: 2022-08-26T11:38:04-07:00
New Revision: ecca895295e6efbc03a9652bf65d622bd1216ab4

URL: https://github.com/llvm/llvm-project/commit/ecca895295e6efbc03a9652bf65d622bd1216ab4
DIFF: https://github.com/llvm/llvm-project/commit/ecca895295e6efbc03a9652bf65d622bd1216ab4.diff

LOG: [libc] add compile option for printf arg type array

This patch allows for adjusting the size of the array that printf uses
to track the types of arguments in index mode. This is useful for
optimizing the tradeoff between memory usage and performance.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D131993

Added: 
    libc/src/stdio/printf_core/printf_config.h

Modified: 
    libc/src/stdio/printf_core/parser.h

Removed: 
    


################################################################################
diff  --git a/libc/src/stdio/printf_core/parser.h b/libc/src/stdio/printf_core/parser.h
index 01db9da337da5..16cfd9ec3d45a 100644
--- a/libc/src/stdio/printf_core/parser.h
+++ b/libc/src/stdio/printf_core/parser.h
@@ -11,6 +11,7 @@
 
 #include "src/__support/arg_list.h"
 #include "src/stdio/printf_core/core_structs.h"
+#include "src/stdio/printf_core/printf_config.h"
 
 #include <stddef.h>
 
@@ -41,8 +42,10 @@ class Parser {
       return (size == other.size) && (primary_type == other.primary_type);
     }
   };
-  // TODO: Make this size configurable via a compile option.
-  static constexpr size_t DESC_ARR_LEN = 32;
+
+  // Defined in printf_config.h
+  static constexpr size_t DESC_ARR_LEN = LLVM_LIBC_PRINTF_INDEX_ARR_LEN;
+
   // desc_arr stores the sizes of the variables in the ArgList. This is used in
   // index mode to reduce repeated string parsing. The sizes are stored as
   // TypeDesc objects, which store the size as well as minimal type information.

diff  --git a/libc/src/stdio/printf_core/printf_config.h b/libc/src/stdio/printf_core/printf_config.h
new file mode 100644
index 0000000000000..9667994e8a0a9
--- /dev/null
+++ b/libc/src/stdio/printf_core/printf_config.h
@@ -0,0 +1,34 @@
+//===-- Printf Configuration Handler ----------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_PRINTF_CONFIG_H
+#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_PRINTF_CONFIG_H
+
+// The index array buffer is always initialized when printf is called. In cases
+// where index mode is necessary but memory is limited, or when index mode
+// performance is important and memory is available, this compile option
+// provides a knob to adjust memory usage to an appropriate level. 128 is picked
+// as the default size since that's big enough to handle even extreme cases and
+// the runtime penalty for not having enough space is severe.
+// When an index mode argument is requested, if its index is before the most
+// recently read index, then the arg list must be restarted from the beginning,
+// and all of the arguments before the new index must be requested with the
+// correct types. The index array caches the types of the values in the arg
+// list. For every number between the last index cached in the array and the
+// requested index, the format string must be parsed again to find the
+// type of that index. As an example, if the format string has 20 indexes, and
+// the index array is 10, then when the 20th index is requested the first 10
+// types can be found immediately, and then the format string must be parsed 10
+// times to find the types of the next 10 arguments.
+#ifndef LLVM_LIBC_PRINTF_INDEX_ARR_LEN
+#define LLVM_LIBC_PRINTF_INDEX_ARR_LEN 128
+#endif
+
+// TODO(michaelrj): Move the other printf configuration options into this file.
+
+#endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_PRINTF_CONFIG_H


        


More information about the libc-commits mailing list