30 Commits

Author SHA1 Message Date
Paul Kirth
2b60b6d963
[llvm][mustache] Avoid extra allocations in parseSection (#159199)
We don't need to have extra allocations when concatenating raw bodies.
2025-11-05 00:46:09 +00:00
Paul Kirth
dc3b5141cd
[llvm][mustache] Optimize accessor splitting with a single pass (#159198)
The splitMustacheString function previously used a loop of
StringRef::split and StringRef::trim. This was inefficient as
it scanned each segment of the accessor string multiple times.

This change introduces a custom splitAndTrim function that
performs both operations in a single pass over the string,
reducing redundant work and improving performance, most notably
in the number of CPU cycles executed.

| Metric | Baseline | Optimized | Change |
| --- | --- | --- | --- |
| Time (ms) | 35\.57 | 35\.36 | \-0.59% |
| Cycles | 34\.91M | 34\.26M | \-1.86% |
| Instructions | 85\.54M | 85\.24M | \-0.35% |
| Branch Misses | 111\.9K | 112\.2K | \+0.27% |
| Cache Misses | 242\.1K | 239\.9K | \-0.91% |
2025-11-04 23:56:00 +00:00
Paul Kirth
1c85981e37
[llvm][mustache] Avoid redundant saves in accessor splitting (#159197)
The splitMustacheString function was saving StringRefs that
were already backed by an arena-allocated string. This was
unnecessary work. This change removes the redundant
Ctx.Saver.save() call.

This optimization provides a small but measurable performance
improvement on top of the single-pass tokenizer, most notably
reducing branch misses.

  Metric         | Baseline | Optimized | Change
  -------------- | -------- | --------- | -------
  Time (ms)      | 35.77    | 35.57     | -0.56%
  Cycles         | 35.16M   | 34.91M    | -0.71%
  Instructions   | 85.77M   | 85.54M    | -0.27%
  Branch Misses  | 113.9K   | 111.9K    | -1.76%
  Cache Misses   | 237.7K   | 242.1K    | +1.85%
2025-11-04 13:21:04 -08:00
Paul Kirth
67ce4aba26
[llvm][mustache] Use single pass when tokenizing (#159196)
The old implementation used many string searches over the same portions
of the strings. This version sacrifices some API niceness for perf wins.

| Metric | Baseline | Single-Pass | Change |
| --- | --- | --- | --- |
| Time (ms) | 36\.09 | 35\.78 | \-0.86% |
| Cycles | 35\.3M | 35\.0M | \-0.79% |
| Instructions | 86\.7M | 85\.8M | \-1.03% |
| Branch Misses | 116K | 114K | \-1.91% |
| Cache Misses | 244K | 232K | \-4.98% |
2025-11-04 17:24:27 +00:00
Paul Kirth
b0d252da3c
[llvm][mustache] Avoid extra copy for json strings (#159195) 2025-10-09 18:27:11 +00:00
Paul Kirth
5cef6f38c0
[llvm][mustache] Use BumpPtrAllocator to save ASTNodes (#159194)
We make the Mustache ASTNodes usable in the arena by first removing all
of the memory owning data structures, like std::vector, std::unique_ptr,
and SmallVector. We use standard LLVM list types to hold this data
instead, and make use of a UniqueStringSaver to hold the various
templates strings.

Additionally, update clang-doc APIs to use the new interfaces.

Future work can make better use of Twine interfaces to help avoid any
intermediate copies or allocations.
2025-10-09 10:46:39 -07:00
Kazu Hirata
a414c22f32 [Support] Fix warnings
This patch fixes:

  llvm/lib/Support/Mustache.cpp:332:20: error: unused function
  'tagKindToString' [-Werror,-Wunused-function]

  llvm/lib/Support/Mustache.cpp:344:20: error: unused function
  'jsonKindToString' [-Werror,-Wunused-function]
2025-09-30 21:44:08 -07:00
Paul Kirth
9ce0dae54e
[llvm][mustache] Simplify debug logging (#159193)
The existing logging was inconsistent, and we logged too many things.
This PR introduces a more principled schema, and eliminates many,
redundant log lines.
2025-10-01 00:47:06 +00:00
Paul Kirth
0aa7da089a
[llvm][mustache] Fix failing StandaloneIndentation test (#159192)
When rendering partials, we need to use an indentation stream,
but when part of the partial is a unescaped sequence, we cannot
indent those. To address this, we build a common MustacheStream
interface for all the output streams to use. This allows us to
further customize the AddIndentationStream implementation
and opt it out of indenting the UnescapeSequence.
2025-09-30 17:02:49 -07:00
Paul Kirth
aa42b6455a
[llvm][mustache] Introduce MustacheContext to simplify mustache APIs (#159191) 2025-09-29 20:40:11 -07:00
Paul Kirth
978644c29f
[llvm][mustache] Remove out parameters from processTags() (#159190)
We can construct the return values directly and simplify the interface.
2025-09-30 02:37:33 +00:00
Paul Kirth
781baf76fb
[llvm][mustache] Refactor template rendering (#159189)
Move the rendering logic into the ASTNode, and break the logic down into
individual methods.
2025-09-30 01:48:33 +00:00
Paul Kirth
6ffacae996
[llvm][mustache] Refactor tokenizer for clarity (#159188)
This patch refactors the Mustache tokenizer by breaking the logic up
with helper functions to improve clarity and simplify the code.
2025-09-29 17:55:08 -07:00
Paul Kirth
1fcf481631
[llvm][mustache] Support setting delimiters in templates (#159187)
The base mustache spec allows setting custom delimiters, which slightly
change parsing of partials. This patch implements that feature by adding
a new token type, and changing the tokenizer's behavior to allow setting
custom delimiters.
2025-09-29 17:00:00 -07:00
Paul Kirth
7b96dfbb7d
[llvm][mustache] Align standalone partial indentation with spec (#159185)
The current implementaion did not correctly handle indentation for
standalone partial tags. It was only applied to lines following a
newline, instead of the first line of a partial's content. This was
fixed by updating the AddIndentation implementaion to prepend the
indentation to the first line of the partial.
2025-09-29 11:00:04 -07:00
Paul Kirth
f9065fce8f
[llvm][mustache] Avoid excessive hash lookups in EscapeStringStream (#160166)
The naive char-by-char lookup performed OK, but we can skip ahead to the
next match, avoiding all the extra hash lookups in the key map. Likely
there is a faster method than this, but its already a 42% win in the
BM_Mustache_StringRendering/Escaped benchmark, and an order of magnitude
improvement for BM_Mustache_LargeOutputString.

| Benchmark | Before (ns) | After (ns) | Speedup |
| :--- | ---: | ---: | ---: |
| `StringRendering/Escaped` | 29,440,922 | 16,583,603 | ~44% |
| `LargeOutputString` | 15,139,251 | 929,891 | ~94% |
| `HugeArrayIteration` | 102,148,245 | 95,943,960 | ~6% |
| `PartialsRendering` | 308,330,014 | 303,556,563 | ~1.6% |

Unreported benchmarks, like those for parsing, had no significant
change.
2025-09-26 15:46:57 -07:00
Paul Kirth
1c6e896c71
[llvm][mustache] Add support for Triple Mustache (#159183)
We extend the logic in tokenize() to treat the `{{{}}}` delimiters
to treat it like other unescaped HTML. We do this by updating the
tokenizer to treat the new tokes the same way we do for the `{{&variable}}`
syntax, which avoid the need to change the parser.

We also update the llvm-test-mustache-spec tool to no longer mark Triple
Mustache as XFAIL.
2025-09-24 13:44:26 -07:00
Paul Kirth
8e77263ad0
[llvm][mustache] Fix UB in ASTNode::render() (#142249)
The current implementation set a reference to a nullptr, leading to all
kinds of problems. Instead, we can check the various uses to ensure we
don't deref invalid memory, and improve the logic for how contexts are
passed to children, since that was also subtly wrong in some cases.
2025-06-04 09:46:14 -07:00
Rahul Joshi
a76bf4da53
[NFC][ADT/Support] Add {} for else when if body has {} (#140758) 2025-05-21 13:19:09 -07:00
Paul Kirth
6c764a6dda
[llvm][mustache][NFC] Use type alias for escape symbols (#138050)
This data structure's type and/or representation is likely to change.
Using an alias, makes it easy to refactor.
2025-05-06 15:54:32 -07:00
Kazu Hirata
2d287f51ef
[llvm] Use *(Set|Map)::contains (NFC) (#138431) 2025-05-03 21:55:36 -07:00
Kazu Hirata
aa613777af
[llvm] Remove redundant control flow (NFC) (#138304) 2025-05-02 10:34:25 -07:00
Paul Kirth
ece59a8cb9
Reland Support for mustache templating language (#132467)
The last version of this patch had memory leaks due to using the
BumpPtrAllocator for data types that required destructors to run to
release heap memory (e.g. via std::vector and std::string). This version
avoids that by using smart pointers, and dropping support for
BumpPtrAllocator.

We should refactor this code to use the BumpPtrAllocator again, but that
can be addressed in future patches, since those are more invasive
changes that need to refactor many of the core data types to avoid
owning allocations.

Adds Support for the Mustache Templating Language. See specs here:
https://mustache.github.io/mustache.5.html This patch implements
support+tests for majority of the features of the language including:

    - Variables
    - Comments
    - Lambdas
    - Sections

This meant as a library to support places where we have to generate
HTML, such as in clang-doc.

Co-authored-by: Peter Chou <peter.chou@mail.utoronto.ca>
2025-03-24 17:23:25 -07:00
PeterChou1
08d15e3f64
Revert "reapply [llvm] add support for mustache templating language" (#131228)
This broke:

https://lab.llvm.org/buildbot/#/builders/64/builds/2486
https://lab.llvm.org/buildbot/#/builders/146/builds/2476
2025-03-13 18:08:58 -04:00
PeterChou1
6bf0c4648e
reapply [llvm] add support for mustache templating language (#130876)
Reapply https://github.com/llvm/llvm-project/pull/130732

Fixes errors which broke build bot that uses GCC as a compiler
https://lab.llvm.org/buildbot/#/builders/66/builds/11049

GCC threw an warning due to an issue std::move with a temporary object
which prevents copy elision. Fixes the issue by removing the std::move

Adds Support for the Mustache Templating Language. See specs here:
https://mustache.github.io/mustache.5.html
This patch implements support+tests for majority of the features of the
language including:

- Variables
- Comments
- Lambdas
- Sections

This meant as a library to support places where we have to generate
HTML, such as in clang-doc.
2025-03-12 07:59:28 -04:00
PeterChou1
554347ba45
Revert "[llvm] add support for mustache templating language (#105893)" (#130873)
This patch caused certain GCC buildbots to failed
errors: https://lab.llvm.org/buildbot/#/builders/66/builds/11049
2025-03-11 21:27:43 -04:00
Jie Fu
9415b7d97f [Support] Fix -Wpessimizing-move in Mustache.cpp (NFC)
/llvm-project/llvm/lib/Support/Mustache.cpp:299:27:
error: moving a temporary object prevents copy elision [-Werror,-Wpessimizing-move]
    PrevToken.TokenBody = std::move(Unindented.str());
                          ^
/llvm-project/llvm/lib/Support/Mustache.cpp:299:27: note: remove std::move call here
    PrevToken.TokenBody = std::move(Unindented.str());
                          ^~~~~~~~~~                ~
1 error generated.
2025-03-12 08:58:17 +08:00
PeterChou1
8f05f25360
reapply [llvm] add support for mustache templating language (#130732)
Reapply https://github.com/llvm/llvm-project/pull/105893

Fixes errors which broke build bot that uses GCC as a compiler
https://lab.llvm.org/buildbot/#/builders/136/builds/3100

The issue here was that using Accessor defined in the anonymous
namespace introduces Accessor as a type alias. Which is, later redeclare
as members in classes Token and ASTNode with the same name which causes
error in GCC. The patch fixes it by renaming the Accesor to
AccessorValue. It also fixes warnings caused by the compile due to
initialization


Adds Support for the Mustache Templating Language. See specs here:
https://mustache.github.io/mustache.5.html
This patch implements support+tests for majority of the features of the
language including:

   - Variables
   - Comments
   - Lambdas
   - Sections

This meant as a library to support places where we have to generate
HTML, such as in clang-doc.
2025-03-11 20:46:11 -04:00
PeterChou1
5c055cc4eb
Revert "[llvm] add support for mustache templating language (#105893)" (#130676)
This broke some build bot warnings


https://lab.llvm.org/buildbot/#/builders/160/builds/14381/steps/5/logs/stdio

reverts: https://github.com/llvm/llvm-project/pull/105893
2025-03-10 18:09:26 -04:00
PeterChou1
92c8dd6fc9
[llvm] add support for mustache templating language (#105893)
Adds Support for the Mustache Templating Language. See specs here:
https://mustache.github.io/mustache.5.html
This patch implements support+tests for majority of the features of the
language including:
- Variables
- Comments
- Lambdas
- Sections

This meant as a library to support places where we have to generate
HTML, such as in clang-doc.
2025-03-10 17:51:17 -04:00