MLIR 23.0.0git
Format.h
Go to the documentation of this file.
1//===- Format.h - Utilities for String Format -------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file declares utilities for formatting strings. They are specially
10// tailored to the needs of TableGen'ing op definitions and rewrite rules,
11// so they are not expected to be used as widely applicable utilities.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef MLIR_TABLEGEN_FORMAT_H_
16#define MLIR_TABLEGEN_FORMAT_H_
17
18#include "mlir/Support/LLVM.h"
19#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/StringMap.h"
21#include "llvm/Support/FormatVariadic.h"
22
23namespace mlir {
24namespace tblgen {
25
26/// Format context containing substitutions for special placeholders.
27///
28/// This context divides special placeholders into two categories: builtin ones
29/// and custom ones.
30///
31/// Builtin placeholders are baked into `FmtContext` and each one of them has a
32/// dedicated setter. They can be used in all dialects. Their names follow the
33/// convention of `$_<name>`. The rationale of the leading underscore is to
34/// avoid confusion and name collision: op arguments/attributes/results are
35/// named as $<name>, and we can potentially support referencing those entities
36/// directly in the format template in the future.
37//
38/// Custom ones are registered by dialect-specific TableGen backends and use the
39/// same unified setter.
41public:
42 // Placeholder kinds
43 enum class PHKind : char {
45 Custom, // For custom placeholders
46 Builder, // For the $_builder placeholder
47 Self, // For the $_self placeholder
48 };
49
50 FmtContext() = default;
51
52 // Create a format context with a list of substitutions.
53 FmtContext(ArrayRef<std::pair<StringRef, StringRef>> subs);
54
55 // Setter for custom placeholders
56 FmtContext &addSubst(StringRef placeholder, const Twine &subst);
57
58 // Setters for builtin placeholders
59 FmtContext &withBuilder(Twine subst);
60 FmtContext &withSelf(Twine subst);
61
62 std::optional<StringRef> getSubstFor(PHKind placeholder) const;
63 std::optional<StringRef> getSubstFor(StringRef placeholder) const;
64
65 static PHKind getPlaceHolderKind(StringRef str);
66
67private:
68 struct PHKindInfo : DenseMapInfo<PHKind> {
69 using CharInfo = DenseMapInfo<char>;
70
71 static inline PHKind getEmptyKey() {
72 return static_cast<PHKind>(CharInfo::getEmptyKey());
73 }
74 static unsigned getHashValue(const PHKind &val) {
75 return CharInfo::getHashValue(static_cast<char>(val));
76 }
77
78 static bool isEqual(const PHKind &lhs, const PHKind &rhs) {
79 return lhs == rhs;
80 }
81 };
82
83 llvm::SmallDenseMap<PHKind, std::string, 4, PHKindInfo> builtinSubstMap;
84 llvm::StringMap<std::string> customSubstMap;
85};
86
87/// Struct representing a replacement segment for the formatted string. It can
88/// be a segment of the formatting template (for `Literal`) or a replacement
89/// parameter (for `PositionalPH`, `PositionalRangePH` and `SpecialPH`).
98
99 FmtReplacement() = default;
100 explicit FmtReplacement(StringRef literal)
101 : type(Type::Literal), spec(literal) {}
102 FmtReplacement(StringRef spec, size_t index)
104 FmtReplacement(StringRef spec, size_t index, size_t end)
108
110 StringRef spec;
111 size_t index = 0;
112 size_t end = kUnset;
114
115 static constexpr size_t kUnset = -1;
116};
117
119private:
120 static std::pair<FmtReplacement, StringRef> splitFmtSegment(StringRef fmt);
121 static std::vector<FmtReplacement> parseFormatString(StringRef fmt);
122
123protected:
124 // The parameters are stored in a std::tuple, which does not provide runtime
125 // indexing capabilities. In order to enable runtime indexing, we use this
126 // structure to put the parameters into a std::vector. Since the parameters
127 // are not all the same type, we use some type-erasure by wrapping the
128 // parameters in a template class that derives from a non-template superclass.
129 // Essentially, we are converting a std::tuple<Derived<Ts...>> to a
130 // std::vector<Base*>.
132 template <typename... Ts>
133 std::vector<llvm::support::detail::format_adapter *>
134 operator()(Ts &...items) {
135 return std::vector<llvm::support::detail::format_adapter *>{&items...};
136 }
137 };
138
139 StringRef fmt;
141 std::vector<llvm::support::detail::format_adapter *> adapters;
142 std::vector<FmtReplacement> replacements;
143
144public:
145 FmtObjectBase(StringRef fmt, const FmtContext *ctx, size_t numParams)
146 : fmt(fmt), context(ctx), replacements(parseFormatString(fmt)) {}
147
148 FmtObjectBase(const FmtObjectBase &that) = delete;
149
151 : fmt(that.fmt), context(that.context),
152 adapters(), // adapters are initialized by FmtObject
153 replacements(std::move(that.replacements)) {}
154
155 void format(llvm::raw_ostream &s) const;
156
157 std::string str() const {
158 std::string result;
159 llvm::raw_string_ostream s(result);
160 format(s);
161 return s.str();
162 }
163
164 template <unsigned N>
167 llvm::raw_svector_ostream s(result);
168 format(s);
169 return result;
170 }
171
172 template <unsigned N>
173 operator SmallString<N>() const {
174 return sstr<N>();
175 }
176
177 operator std::string() const { return str(); }
178};
179
180template <typename Tuple>
181class FmtObject : public FmtObjectBase {
182 // Storage for the parameter adapters. Since the base class erases the type
183 // of the parameters, we have to own the storage for the parameters here, and
184 // have the base class store type-erased pointers into this tuple.
185 Tuple parameters;
186
187public:
188 FmtObject(StringRef fmt, const FmtContext *ctx, Tuple &&params)
189 : FmtObjectBase(fmt, ctx, std::tuple_size<Tuple>::value),
190 parameters(std::move(params)) {
191 adapters.reserve(std::tuple_size<Tuple>::value);
192 adapters = std::apply(CreateAdapters(), parameters);
193 }
194
195 FmtObject(FmtObject const &that) = delete;
196
198 : FmtObjectBase(std::move(that)), parameters(std::move(that.parameters)) {
199 adapters.reserve(that.adapters.size());
200 adapters = std::apply(CreateAdapters(), parameters);
201 }
202};
203
205public:
206 using StrFormatAdapter = decltype(llvm::support::detail::build_format_adapter(
207 std::declval<std::string>()));
208
209 FmtStrVecObject(StringRef fmt, const FmtContext *ctx,
210 ArrayRef<std::string> params);
211 FmtStrVecObject(FmtStrVecObject const &that) = delete;
213
214private:
216};
217
218/// Formats text by substituting placeholders in format string with replacement
219/// parameters.
220///
221/// There are two categories of placeholders accepted, both led by a '$' sign:
222///
223/// 1.a Positional placeholder: $[0-9]+
224/// 1.b Positional range placeholder: $[0-9]+...
225/// 2. Special placeholder: $[a-zA-Z_][a-zA-Z0-9_]*
226///
227/// Replacement parameters for positional placeholders are supplied as the
228/// `vals` parameter pack with 1:1 mapping. That is, $0 will be replaced by the
229/// first parameter in `vals`, $1 by the second one, and so on. Note that you
230/// can use the positional placeholders in any order and repeat any times, for
231/// example, "$2 $1 $1 $0" is accepted.
232///
233/// Replace parameters for positional range placeholders are supplied as if
234/// positional placeholders were specified with commas separating them.
235///
236/// Replacement parameters for special placeholders are supplied using the `ctx`
237/// format context.
238///
239/// The `fmt` is recorded as a `StringRef` inside the returned `FmtObject`.
240/// The caller needs to make sure the underlying data is available when the
241/// `FmtObject` is used.
242///
243/// `ctx` accepts a nullptr if there is no special placeholder is used.
244///
245/// If no substitution is provided for a placeholder or any error happens during
246/// format string parsing or replacement, the placeholder will be outputted
247/// as-is with an additional marker '<no-subst-found>', to aid debugging.
248///
249/// To print a '$' literally, escape it with '$$'.
250///
251/// This utility function is inspired by LLVM formatv(), with modifications
252/// specially tailored for TableGen C++ generation usage:
253///
254/// 1. This utility use '$' instead of '{' and '}' for denoting the placeholder
255/// because '{' and '}' are frequently used in C++ code.
256/// 2. This utility does not support format layout because it is rarely needed
257/// in C++ code generation.
258template <typename... Ts>
259inline auto tgfmt(StringRef fmt, const FmtContext *ctx, Ts &&...vals)
260 -> FmtObject<
261 decltype(std::make_tuple(llvm::support::detail::build_format_adapter(
262 std::forward<Ts>(vals))...))> {
263 using ParamTuple = decltype(std::make_tuple(
264 llvm::support::detail::build_format_adapter(std::forward<Ts>(vals))...));
266 fmt, ctx,
267 std::make_tuple(llvm::support::detail::build_format_adapter(
268 std::forward<Ts>(vals))...));
269}
270
271inline FmtStrVecObject tgfmt(StringRef fmt, const FmtContext *ctx,
272 ArrayRef<std::string> params) {
273 return FmtStrVecObject(fmt, ctx, params);
274}
275
276} // namespace tblgen
277} // namespace mlir
278
279#endif // MLIR_TABLEGEN_FORMAT_H_
lhs
Format context containing substitutions for special placeholders.
Definition Format.h:40
FmtContext & withBuilder(Twine subst)
Definition Format.cpp:36
static PHKind getPlaceHolderKind(StringRef str)
Definition Format.cpp:64
std::optional< StringRef > getSubstFor(PHKind placeholder) const
Definition Format.cpp:47
FmtContext & withSelf(Twine subst)
Definition Format.cpp:41
FmtContext & addSubst(StringRef placeholder, const Twine &subst)
Definition Format.cpp:31
std::string str() const
Definition Format.h:157
std::vector< llvm::support::detail::format_adapter * > adapters
Definition Format.h:141
FmtObjectBase(StringRef fmt, const FmtContext *ctx, size_t numParams)
Definition Format.h:145
void format(llvm::raw_ostream &s) const
Definition Format.cpp:145
const FmtContext * context
Definition Format.h:140
FmtObjectBase(FmtObjectBase &&that)
Definition Format.h:150
std::vector< FmtReplacement > replacements
Definition Format.h:142
FmtObjectBase(const FmtObjectBase &that)=delete
SmallString< N > sstr() const
Definition Format.h:165
FmtObject(FmtObject const &that)=delete
FmtObject(StringRef fmt, const FmtContext *ctx, Tuple &&params)
Definition Format.h:188
FmtObject(FmtObject &&that)
Definition Format.h:197
FmtStrVecObject(StringRef fmt, const FmtContext *ctx, ArrayRef< std::string > params)
Definition Format.cpp:201
decltype(llvm::support::detail::build_format_adapter( std::declval< std::string >())) StrFormatAdapter
Definition Format.h:206
FmtStrVecObject(FmtStrVecObject const &that)=delete
auto tgfmt(StringRef fmt, const FmtContext *ctx, Ts &&...vals) -> FmtObject< decltype(std::make_tuple(llvm::support::detail::build_format_adapter(std::forward< Ts >(vals))...))>
Formats text by substituting placeholders in format string with replacement parameters.
Definition Format.h:259
Include the generated interface declarations.
llvm::DenseMapInfo< T, Enable > DenseMapInfo
Definition LLVM.h:116
std::vector< llvm::support::detail::format_adapter * > operator()(Ts &...items)
Definition Format.h:134
FmtReplacement(StringRef literal)
Definition Format.h:100
FmtReplacement(StringRef spec, size_t index)
Definition Format.h:102
static constexpr size_t kUnset
Definition Format.h:115
FmtContext::PHKind placeholder
Definition Format.h:113
FmtReplacement(StringRef spec, FmtContext::PHKind placeholder)
Definition Format.h:106
FmtReplacement(StringRef spec, size_t index, size_t end)
Definition Format.h:104