| 1 | //===- AttributeTableEmitter.h ----------------------------------*- 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 | // Helpers for emitting compact tables of IR attributes. AttributeSetTable |
| 10 | // uniques attribute sets and emits the cases of a switch constructing each one. |
| 11 | // AttributeSequenceTable uniques per-entity sequences of argument attribute |
| 12 | // entries and emits them as one table, sharing storage between sequences. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #ifndef LLVM_UTILS_TABLEGEN_BASIC_ATTRIBUTETABLEEMITTER_H |
| 17 | #define LLVM_UTILS_TABLEGEN_BASIC_ATTRIBUTETABLEEMITTER_H |
| 18 | |
| 19 | #include "SequenceToOffsetTable.h" |
| 20 | #include "llvm/ADT/STLExtras.h" |
| 21 | #include "llvm/ADT/STLFunctionalExtras.h" |
| 22 | #include "llvm/ADT/SmallVector.h" |
| 23 | #include "llvm/ADT/StringRef.h" |
| 24 | #include "llvm/ADT/UniqueVector.h" |
| 25 | #include "llvm/Support/FormatVariadic.h" |
| 26 | #include "llvm/Support/raw_ostream.h" |
| 27 | |
| 28 | namespace llvm { |
| 29 | |
| 30 | /// Uniques attribute sets, keyed by \p KeyT, and assigns them dense IDs in |
| 31 | /// insertion order. |
| 32 | template <typename KeyT> class AttributeSetTable { |
| 33 | UniqueVector<KeyT> Sets; |
| 34 | |
| 35 | public: |
| 36 | /// Returns the ID of the set \p Attrs. |
| 37 | unsigned add(const KeyT &Attrs) { return Sets.insert(Attrs) - 1; } |
| 38 | |
| 39 | unsigned size() const { return static_cast<unsigned>(Sets.size()); } |
| 40 | |
| 41 | /// Emits a switch case returning each set, in ID order. \p Print emits the |
| 42 | /// attributes of a non-empty set. If provided, \p WriteComment writes a |
| 43 | /// comment for the case of each ID. |
| 44 | void emitCases( |
| 45 | raw_ostream &OS, function_ref<void(raw_ostream &, const KeyT &)> Print, |
| 46 | function_ref<void(raw_ostream &, unsigned)> = {}) const { |
| 47 | for (const auto &[ID, Attrs] : enumerate(Sets)) { |
| 48 | OS << "\n case " << ID << ':'; |
| 49 | if (WriteComment) { |
| 50 | OS << " // " ; |
| 51 | WriteComment(OS, ID); |
| 52 | } |
| 53 | |
| 54 | if (Attrs.empty()) { |
| 55 | OS << "\n return AttributeSet();" ; |
| 56 | continue; |
| 57 | } |
| 58 | |
| 59 | OS << "\n return AttributeSet::get(C, {\n" ; |
| 60 | Print(OS, Attrs); |
| 61 | OS << " });" ; |
| 62 | } |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | /// Uniques sequences of argument attribute entries, assigning them dense IDs in |
| 67 | /// insertion order, and lays them out in a single table where a sequence that |
| 68 | /// is a suffix of another shares its storage. |
| 69 | template <typename EntryT> class AttributeSequenceTable { |
| 70 | public: |
| 71 | using Sequence = SmallVector<EntryT, 0>; |
| 72 | |
| 73 | private: |
| 74 | SequenceToOffsetTable<Sequence> Table{std::nullopt}; |
| 75 | UniqueVector<Sequence> Seqs; |
| 76 | |
| 77 | public: |
| 78 | /// Returns the ID of \p Seq. Must be called before the table is laid out. |
| 79 | unsigned add(const Sequence &Seq) { |
| 80 | size_t NumSeqs = Seqs.size(); |
| 81 | unsigned ID = Seqs.insert(Seq) - 1; |
| 82 | if (Seqs.size() != NumSeqs && !Seq.empty()) |
| 83 | Table.add(Seq); |
| 84 | return ID; |
| 85 | } |
| 86 | |
| 87 | void layout() { Table.layout(); } |
| 88 | |
| 89 | /// Number of sequences. |
| 90 | unsigned numSequences() const { return static_cast<unsigned>(Seqs.size()); } |
| 91 | |
| 92 | /// Number of entries in the laid out table. |
| 93 | unsigned size() const { return Table.size(); } |
| 94 | |
| 95 | /// Emits the entries of the laid out table. |
| 96 | void emitEntries(raw_ostream &OS, |
| 97 | function_ref<void(raw_ostream &, EntryT)> Print) const { |
| 98 | Table.emit(OS, Print); |
| 99 | } |
| 100 | |
| 101 | /// Emits the start index and length of each sequence, in ID order. |
| 102 | void emitInfo(raw_ostream &OS) const { |
| 103 | for (const Sequence &Seq : Seqs) { |
| 104 | unsigned Start = Seq.empty() ? 0 : Table.get(Seq); |
| 105 | OS << formatv(" {{{}, {}},\n" , Start, Seq.size()); |
| 106 | } |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | } // namespace llvm |
| 111 | |
| 112 | #endif // LLVM_UTILS_TABLEGEN_BASIC_ATTRIBUTETABLEEMITTER_H |
| 113 | |