[libcxx-commits] [libcxx] [libc++] Add basic constant folding for std::format (PR #107197)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Sep 4 02:24:03 PDT 2024
https://github.com/philnik777 updated https://github.com/llvm/llvm-project/pull/107197
>From 3c4cb146f9b942146d575ed595198c62cb3b3bdb Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Tue, 3 Sep 2024 19:39:17 +0200
Subject: [PATCH] [libc++] Add basic constant folding for std::format
---
libcxx/include/__format/format_functions.h | 49 +++++++++++++++++++---
libcxx/test/benchmarks/format.bench.cpp | 9 ++++
2 files changed, 52 insertions(+), 6 deletions(-)
diff --git a/libcxx/include/__format/format_functions.h b/libcxx/include/__format/format_functions.h
index 1518ab5768d243..f7d68b29b611b0 100644
--- a/libcxx/include/__format/format_functions.h
+++ b/libcxx/include/__format/format_functions.h
@@ -11,6 +11,8 @@
#define _LIBCPP___FORMAT_FORMAT_FUNCTIONS
#include <__algorithm/clamp.h>
+#include <__algorithm/ranges_find_first_of.h>
+#include <__chrono/statically_widen.h>
#include <__concepts/convertible_to.h>
#include <__concepts/same_as.h>
#include <__config>
@@ -448,13 +450,44 @@ format_to(_OutIt __out_it, wformat_string<_Args...> __fmt, _Args&&... __args) {
}
# endif
+template <class _CharT>
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<basic_string<_CharT>> __try_constant_folding(
+ basic_string_view<_CharT> __fmt,
+ basic_format_args<basic_format_context<back_insert_iterator<__format::__output_buffer<_CharT>>, _CharT>> __args) {
+ return nullopt;
+ if (bool __is_identity = std::ranges::find_first_of(__fmt, array{'{', '}'}) == __fmt.end();
+ __builtin_constant_p(__is_identity) && __is_identity)
+ return basic_string<_CharT>{__fmt};
+
+ if (auto __only_first_arg = __fmt == _LIBCPP_STATICALLY_WIDEN(_CharT, "{}");
+ __builtin_constant_p(__only_first_arg) && __only_first_arg) {
+ if (auto __arg = __args.get(0); __builtin_constant_p(__arg.__type_)) {
+ return std::__visit_format_arg(
+ []<class _Tp>(_Tp&& __argument) -> optional<basic_string<_CharT>> {
+ if constexpr (is_same_v<remove_cvref_t<_Tp>, basic_string_view<_CharT>>) {
+ return basic_string<_CharT>{__argument};
+ } else {
+ return nullopt;
+ }
+ },
+ __arg);
+ }
+ }
+
+ return nullopt;
+}
+
// TODO FMT This needs to be a template or std::to_chars(floating-point) availability markup
// fires too eagerly, see http://llvm.org/PR61563.
template <class = void>
[[nodiscard]] _LIBCPP_ALWAYS_INLINE inline _LIBCPP_HIDE_FROM_ABI string vformat(string_view __fmt, format_args __args) {
- string __res;
- std::vformat_to(std::back_inserter(__res), __fmt, __args);
- return __res;
+ return std::__try_constant_folding(__fmt, __args)
+ .or_else([&]() -> optional<string> {
+ string __res;
+ std::vformat_to(std::back_inserter(__res), __fmt, __args);
+ return __res;
+ })
+ .value();
}
# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
@@ -463,9 +496,13 @@ template <class = void>
template <class = void>
[[nodiscard]] _LIBCPP_ALWAYS_INLINE inline _LIBCPP_HIDE_FROM_ABI wstring
vformat(wstring_view __fmt, wformat_args __args) {
- wstring __res;
- std::vformat_to(std::back_inserter(__res), __fmt, __args);
- return __res;
+ return std::__try_constant_folding(__fmt, __args)
+ .or_else([&]() -> optional<wstring> {
+ wstring __res;
+ std::vformat_to(std::back_inserter(__res), __fmt, __args);
+ return __res;
+ })
+ .value();
}
# endif
diff --git a/libcxx/test/benchmarks/format.bench.cpp b/libcxx/test/benchmarks/format.bench.cpp
index 17eaccb18ee1c4..5632aa5b9810f3 100644
--- a/libcxx/test/benchmarks/format.bench.cpp
+++ b/libcxx/test/benchmarks/format.bench.cpp
@@ -28,4 +28,13 @@ static void BM_format_string(benchmark::State& state) {
BENCHMARK(BM_format_string<char>)->RangeMultiplier(2)->Range(1, 1 << 20);
BENCHMARK(BM_format_string<wchar_t>)->RangeMultiplier(2)->Range(1, 1 << 20);
+template <class CharT>
+static void BM_string_without_formatting(benchmark::State& state) {
+ for (auto _ : state) {
+ benchmark::DoNotOptimize(std::format(CSTR("Hello, World!")));
+ }
+}
+BENCHMARK(BM_string_without_formatting<char>);
+BENCHMARK(BM_string_without_formatting<wchar_t>);
+
BENCHMARK_MAIN();
More information about the libcxx-commits
mailing list