Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions llvm/include/llvm/TableGen/CodeGenHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <string>

namespace llvm {

// Simple RAII helper for emitting ifdef-undef-endif scope.
class IfDefEmitter {
public:
Expand Down Expand Up @@ -57,15 +58,15 @@ class NamespaceEmitter {
NamespaceEmitter(raw_ostream &OS, StringRef NameUntrimmed)
: Name(trim(NameUntrimmed).str()), OS(OS) {
if (!Name.empty())
OS << "namespace " << Name << " {\n";
OS << "namespace " << Name << " {\n\n";
}

~NamespaceEmitter() { close(); }

// Explicit function to close the namespace scopes.
void close() {
if (!Closed && !Name.empty())
OS << "} // namespace " << Name << "\n";
OS << "\n} // namespace " << Name << "\n";
Closed = true;
}

Expand Down
2 changes: 2 additions & 0 deletions llvm/test/TableGen/directive1.td
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
// CHECK-NEXT: #include <utility>
// CHECK-EMPTY:
// CHECK-NEXT: namespace llvm {
// CHECK-EMPTY:
// CHECK-NEXT: namespace tdl {
// CHECK-EMPTY:
// CHECK-NEXT: LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
Expand Down Expand Up @@ -176,6 +177,7 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
// CHECK-NEXT: template <> struct enum_iteration_traits<tdl::Clause> {
// CHECK-NEXT: static constexpr bool is_iterable = true;
// CHECK-NEXT: };
// CHECK-EMPTY:
// CHECK-NEXT: } // namespace llvm
// CHECK-EMPTY:
// CHECK-NEXT: #endif // LLVM_Tdl_INC
Expand Down
3 changes: 3 additions & 0 deletions llvm/test/TableGen/directive2.td
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
// CHECK-NEXT: #include <utility>
// CHECK-EMPTY:
// CHECK-NEXT: namespace llvm {
// CHECK-EMPTY:
// CHECK-NEXT: namespace tdl {
// CHECK-EMPTY:
// CHECK-NEXT: enum class Association {
Expand Down Expand Up @@ -132,6 +133,7 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
// CHECK-NEXT: LLVM_ABI Association getDirectiveAssociation(Directive D);
// CHECK-NEXT: LLVM_ABI Category getDirectiveCategory(Directive D);
// CHECK-NEXT: LLVM_ABI SourceLanguage getDirectiveLanguages(Directive D);
// CHECK-EMPTY:
// CHECK-NEXT: } // namespace tdl
// CHECK-EMPTY:
// CHECK-NEXT: template <> struct enum_iteration_traits<tdl::Association> {
Expand All @@ -149,6 +151,7 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
// CHECK-NEXT: template <> struct enum_iteration_traits<tdl::Clause> {
// CHECK-NEXT: static constexpr bool is_iterable = true;
// CHECK-NEXT: };
// CHECK-EMPTY:
// CHECK-NEXT: } // namespace llvm
// CHECK-EMPTY:
// CHECK-NEXT: #endif // LLVM_Tdl_INC
Expand Down
25 changes: 8 additions & 17 deletions llvm/utils/TableGen/Basic/DirectiveEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ static void generateEnumExports(ArrayRef<const Record *> Records,
std::string N = getIdentifierName(R, Prefix);
OS << "constexpr auto " << N << " = " << Enum << "::" << N << ";\n";
}
OS << "\n";
}

// Generate enum class. Entries are emitted in the order in which they appear
// in the `Records` vector.
static void generateEnumClass(ArrayRef<const Record *> Records, raw_ostream &OS,
StringRef Enum, StringRef Prefix,
bool ExportEnums) {
OS << "\n";
OS << "enum class " << Enum << " {\n";
if (!Records.empty()) {
std::string N;
Expand All @@ -104,17 +104,15 @@ static void generateEnumClass(ArrayRef<const Record *> Records, raw_ostream &OS,
OS << "};\n";
OS << "\n";
OS << "static constexpr std::size_t " << Enum
<< "_enumSize = " << Records.size() << ";\n";
<< "_enumSize = " << Records.size() << ";\n\n";

// Make the enum values available in the defined namespace. This allows us to
// write something like Enum_X if we have a `using namespace <CppNamespace>`.
// At the same time we do not loose the strong type guarantees of the enum
// class, that is we cannot pass an unsigned as Directive without an explicit
// cast.
if (ExportEnums) {
OS << "\n";
if (ExportEnums)
generateEnumExports(Records, OS, Enum, Prefix);
}
}

// Generate enum class with values corresponding to different bit positions.
Expand All @@ -127,7 +125,6 @@ static void generateEnumBitmask(ArrayRef<const Record *> Records,
StringRef Type = Records.size() <= 32 ? "uint32_t" : "uint64_t";
StringRef TypeSuffix = Records.size() <= 32 ? "U" : "ULL";

OS << "\n";
OS << "enum class " << Enum << " : " << Type << " {\n";
std::string LastName;
for (auto [I, R] : llvm::enumerate(Records)) {
Expand All @@ -138,17 +135,15 @@ static void generateEnumBitmask(ArrayRef<const Record *> Records,
OS << "};\n";
OS << "\n";
OS << "static constexpr std::size_t " << Enum
<< "_enumSize = " << Records.size() << ";\n";
<< "_enumSize = " << Records.size() << ";\n\n";

// Make the enum values available in the defined namespace. This allows us to
// write something like Enum_X if we have a `using namespace <CppNamespace>`.
// At the same time we do not loose the strong type guarantees of the enum
// class, that is we cannot pass an unsigned as Directive without an explicit
// cast.
if (ExportEnums) {
OS << "\n";
if (ExportEnums)
generateEnumExports(Records, OS, Enum, Prefix);
}
}

// Generate enums for values that clauses can take.
Expand All @@ -170,7 +165,6 @@ static void generateClauseEnumVal(ArrayRef<const Record *> Records,
return;
}

OS << "\n";
OS << "enum class " << Enum << " {\n";
for (const EnumVal Val : ClauseVals)
OS << " " << Val.getRecordName() << "=" << Val.getValue() << ",\n";
Expand All @@ -182,6 +176,7 @@ static void generateClauseEnumVal(ArrayRef<const Record *> Records,
OS << "constexpr auto " << CV->getName() << " = " << Enum
<< "::" << CV->getName() << ";\n";
}
OS << "\n";
EnumHelperFuncs += (Twine("LLVM_ABI ") + Twine(Enum) + Twine(" get") +
Twine(Enum) + Twine("(StringRef Str);\n"))
.str();
Expand Down Expand Up @@ -284,7 +279,7 @@ static void emitDirectivesDecl(const RecordKeeper &Records, raw_ostream &OS) {
NamespaceEmitter DirLangNS(OS, DirLang.getCppNamespace());

if (DirLang.hasEnableBitmaskEnumInNamespace())
OS << "\nLLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();\n";
OS << "LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();\n\n";

// Emit Directive associations
std::vector<const Record *> Associations;
Expand Down Expand Up @@ -315,7 +310,6 @@ static void emitDirectivesDecl(const RecordKeeper &Records, raw_ostream &OS) {
generateClauseEnumVal(DirLang.getClauses(), OS, DirLang, EnumHelperFuncs);

// Generic function signatures
OS << "\n";
OS << "// Enumeration helper functions\n";

OS << "LLVM_ABI std::pair<Directive, directive::VersionRange> get" << Lang
Expand Down Expand Up @@ -353,10 +347,7 @@ static void emitDirectivesDecl(const RecordKeeper &Records, raw_ostream &OS) {
OS << "LLVM_ABI Association getDirectiveAssociation(Directive D);\n";
OS << "LLVM_ABI Category getDirectiveCategory(Directive D);\n";
OS << "LLVM_ABI SourceLanguage getDirectiveLanguages(Directive D);\n";
if (EnumHelperFuncs.length() > 0) {
OS << EnumHelperFuncs;
OS << "\n";
}
OS << EnumHelperFuncs;

DirLangNS.close();

Expand Down
4 changes: 4 additions & 0 deletions mlir/test/mlir-tblgen/cpp-class-comments.td
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def A_SomeOp1 : Op<A_Dialect, "some_op1", []>{

let cppNamespace = "OP1";
// OP: namespace OP1
// OP-EMPTY:
// OP-NEXT: /// Some Op1 summary line1
// OP-NEXT: /// summary line2
// OP-NEXT: /// Some Op1 description
Expand Down Expand Up @@ -97,13 +98,15 @@ def EncodingTrait : AttrInterface<"EncodingTrait"> {
let methods = [
];
// ATTR-INTERFACE: namespace mlir::a::traits {
// ATTR-INTERFACE-EMPTY:
// ATTR-INTERFACE-NEXT: /// Common trait for all layouts.
// ATTR-INTERFACE-NEXT: class EncodingTrait;
}

def SimpleEncodingTrait : AttrInterface<"SimpleEncodingTrait"> {
let cppNamespace = "a::traits";
// ATTR-INTERFACE: namespace a::traits {
// ATTR-INTERFACE-EMPTY:
// ATTR-INTERFACE-NEXT: class SimpleEncodingTrait;
}

Expand All @@ -114,6 +117,7 @@ def SimpleOpInterface : OpInterface<"SimpleOpInterface"> {
Simple Op Interface description
}];
// OP-INTERFACE: namespace a::traits {
// OP-INTERFACE-EMPTY:
// OP-INTERFACE-NEXT: /// Simple Op Interface description
// OP-INTERFACE-NEXT: class SimpleOpInterface;
}
Expand Down
Loading