rikhuijzer 4acbd4446d [MLIR][Doc] Also print summarys for passes on a newline
This patch is improves upon https://reviews.llvm.org/D152621. There, I pointed out some issues with D152621, which I'll repeat here.

> Passes use a different logic for generating the documentation; which I didn't update to be in-line with this change.

Fixed by defining and using `mlir::tblgen::emitSummary`. This is now used in `OpDocGen.cpp` and `PassDocGen.cpp`.

Note that the passes documentation currently prints the summary behind the pass argument. For example:

```
#### -arm-neon-2d-to-intr: Convert Arm NEON structured ops to intrinsics
```
at https://mlir.llvm.org/docs/Passes/#-promote-buffers-to-stack-promotes-heap-based-allocations-to-automatically-managed-stack-based-allocations.

This currently differs from how the summary is printed for Ops. For example:

```
#### amdgpu.lds_barrier (::mlir::amdgpu::LDSBarrierOp) ¶

**Summary:** _Barrier that includes a wait for LDS memory operations._
```

at https://mlir.llvm.org/docs/Dialects/AMDGPU/#amdgpulds_barrier-mliramdgpuldsbarrierop.

The changes in this patch ensure that:

1. The summary is always printed on a new line.
2. The summary is always printed in italic.
3. The summary always starts with a capital letter.

I've dropped the `**Summary:**`, which was introduced in D152621, because only italicization should be already clear enough.

> `amx.tdpbssd` shows **Summary:** __ meaning that apparently hasSummary does not guarantee a non-empty summary.

This is fixed by double-checking `!summary.empty()`, because the following code

```cpp
void mlir::tblgen::emitSummary(StringRef summary, raw_ostream &os) {
  if (!summary.empty()) {
    char first = std::toupper(summary.front());
    llvm::StringRef rest = summary.drop_front();
    os << "\n_" << first << rest << "_\n\n";
  } else {
    os << "\n_" << "foo" << "_\n\n";
  }
}
```
generates the following Markdown:
```
### `amx.tdpbssd` (::mlir::amx::x86_amx_tdpbssd)

_foo_
```
in `tools/mlir/docs/Dialects/AMX.md`.

> Summary fields containing * cancel the italicization, so the * should probably be escaped to solve this. EDIT: Nope. This is because mlir-www runs Hugo 0.80 whereas 0.111 correctly parses _Raw Buffer Floating-point Atomic Add (MI-* only)_ as an italicized string.

This will be fixed by https://github.com/llvm/mlir-www/pull/152.

Reviewed By: jpienaar

Differential Revision: https://reviews.llvm.org/D152648
2023-06-13 09:26:11 +02:00

81 lines
3.0 KiB
C++

//===- PassDocGen.cpp - MLIR pass documentation generator -----------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// PassDocGen uses the description of passes to generate documentation.
//
//===----------------------------------------------------------------------===//
#include "DocGenUtilities.h"
#include "mlir/TableGen/GenInfo.h"
#include "mlir/TableGen/Pass.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/TableGen/Record.h"
using namespace mlir;
using namespace mlir::tblgen;
/// Emit the documentation for the given pass.
static void emitDoc(const Pass &pass, raw_ostream &os) {
os << llvm::formatv("### `-{0}`\n", pass.getArgument());
emitSummary(pass.getSummary(), os);
emitDescription(pass.getDescription(), os);
// Handle the options of the pass.
ArrayRef<PassOption> options = pass.getOptions();
if (!options.empty()) {
os << "\n#### Options\n```\n";
size_t longestOption = 0;
for (const PassOption &option : options)
longestOption = std::max(option.getArgument().size(), longestOption);
for (const PassOption &option : options) {
os << "-" << option.getArgument();
os.indent(longestOption - option.getArgument().size())
<< " : " << option.getDescription() << "\n";
}
os << "```\n";
}
// Handle the statistics of the pass.
ArrayRef<PassStatistic> stats = pass.getStatistics();
if (!stats.empty()) {
os << "\n#### Statistics\n```\n";
size_t longestStat = 0;
for (const PassStatistic &stat : stats)
longestStat = std::max(stat.getName().size(), longestStat);
for (const PassStatistic &stat : stats) {
os << stat.getName();
os.indent(longestStat - stat.getName().size())
<< " : " << stat.getDescription() << "\n";
}
os << "```\n";
}
}
static void emitDocs(const llvm::RecordKeeper &recordKeeper, raw_ostream &os) {
os << "<!-- Autogenerated by mlir-tblgen; don't manually edit -->\n";
auto passDefs = recordKeeper.getAllDerivedDefinitions("PassBase");
// Collect the registered passes, sorted by argument name.
SmallVector<Pass, 16> passes(passDefs.begin(), passDefs.end());
SmallVector<Pass *, 16> sortedPasses(llvm::make_pointer_range(passes));
llvm::array_pod_sort(sortedPasses.begin(), sortedPasses.end(),
[](Pass *const *lhs, Pass *const *rhs) {
return (*lhs)->getArgument().compare(
(*rhs)->getArgument());
});
for (Pass *pass : sortedPasses)
emitDoc(*pass, os);
}
static mlir::GenRegistration
genRegister("gen-pass-doc", "Generate pass documentation",
[](const llvm::RecordKeeper &records, raw_ostream &os) {
emitDocs(records, os);
return false;
});