Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
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
158 changes: 151 additions & 7 deletions mlir/include/mlir/TableGen/AttrOrTypeDef.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,13 @@

#include "mlir/Support/LLVM.h"
#include "mlir/TableGen/Builder.h"
#include "mlir/TableGen/Class.h"
#include "mlir/TableGen/Constraint.h"
#include "mlir/TableGen/Trait.h"

namespace llvm {
class DagInit;
class Record;
class SMLoc;
} // namespace llvm

namespace mlir {
namespace tblgen {
class Dialect;
class InterfaceMethod;

//===----------------------------------------------------------------------===//
// AttrOrTypeBuilder
Expand Down Expand Up @@ -285,6 +280,155 @@ class TypeDef : public AttrOrTypeDef {
StringRef getTypeName() const;
};

class DefGen {
public:
/// Create the attribute or type class.
DefGen(const AttrOrTypeDef &def, bool formatErrorIsFatal);

void emitDecl(raw_ostream &os) const;
void emitDef(raw_ostream &os) const;

private:
/// Add traits from the TableGen definition to the class.
void createParentWithTraits();
/// Emit top-level declarations: using declarations and any extra class
/// declarations.
void emitTopLevelDeclarations();
/// Emit the function that returns the type or attribute name.
void emitName();
/// Emit the dialect name as a static member variable.
void emitDialectName();
/// Emit attribute or type builders.
void emitBuilders();
/// Emit a verifier declaration for custom verification (impl. provided by
/// the users).
void emitVerifierDecl();
/// Emit a verifier that checks type constraints.
void emitInvariantsVerifierImpl();
/// Emit an entry poiunt for verification that calls the invariants and
/// custom verifier.
void emitInvariantsVerifier(bool hasImpl, bool hasCustomVerifier);
/// Emit parsers and printers.
void emitParserPrinter(bool formatErrorIsFatal);
/// Emit parameter accessors, if required.
void emitAccessors();
/// Emit interface methods.
void emitInterfaceMethods();

//===--------------------------------------------------------------------===//
// Builder Emission

/// Emit the default builder `Attribute::get`
void emitDefaultBuilder();
/// Emit the checked builder `Attribute::getChecked`
void emitCheckedBuilder();
/// Emit a custom builder.
void emitCustomBuilder(const AttrOrTypeBuilder &builder);
/// Emit a checked custom builder.
void emitCheckedCustomBuilder(const AttrOrTypeBuilder &builder);

//===--------------------------------------------------------------------===//
// Interface Method Emission

/// Emit methods for a trait.
void emitTraitMethods(const InterfaceTrait &trait);
/// Emit a trait method.
void emitTraitMethod(const InterfaceMethod &method);

//===--------------------------------------------------------------------===//
// Storage Class Emission
void emitStorageClass();
/// Generate the storage class constructor.
void emitStorageConstructor();
/// Emit the key type `KeyTy`.
void emitKeyType();
/// Emit the equality comparison operator.
void emitEquals();
/// Emit the key hash function.
void emitHashKey();
/// Emit the function to construct the storage class.
void emitConstruct();

//===--------------------------------------------------------------------===//
// Utility Function Declarations

/// Get the method parameters for a def builder, where the first several
/// parameters may be different.
SmallVector<MethodParameter>
getBuilderParams(std::initializer_list<MethodParameter> prefix) const;

//===--------------------------------------------------------------------===//
// Class fields

/// The attribute or type definition.
const AttrOrTypeDef &def;
/// The list of attribute or type parameters.
ArrayRef<AttrOrTypeParameter> params;
/// The attribute or type class.
Class defCls;
/// An optional attribute or type storage class. The storage class will
/// exist if and only if the def has more than zero parameters.
std::optional<Class> storageCls;

/// The C++ base value of the def, either "Attribute" or "Type".
StringRef valueType;
/// The prefix/suffix of the TableGen def name, either "Attr" or "Type".
StringRef defType;
};

class DefGenerator {
public:
bool emitDecls(StringRef selectedDialect);
bool emitDefs(StringRef selectedDialect);

protected:
DefGenerator(ArrayRef<const llvm::Record *> defs, raw_ostream &os,
StringRef defType, StringRef valueType, bool isAttrGenerator,
bool formatErrorIsFatal);

/// Emit the list of def type names.
void emitTypeDefList(ArrayRef<AttrOrTypeDef> defs);
/// Emit the code to dispatch between different defs during parsing/printing.
void emitParsePrintDispatch(ArrayRef<AttrOrTypeDef> defs);

/// The set of def records to emit.
std::vector<const llvm::Record *> defRecords;
/// The attribute or type class to emit.
/// The stream to emit to.
raw_ostream &os;
/// The prefix of the tablegen def name, e.g. Attr or Type.
StringRef defType;
/// The C++ base value type of the def, e.g. Attribute or Type.
StringRef valueType;
/// Flag indicating if this generator is for Attributes. False if the
/// generator is for types.
bool isAttrGenerator;
/// Whether a failure in parsing the assembly format should be a fatal error.
bool formatErrorIsFatal;
};

/// A specialized generator for AttrDefs.
struct AttrDefGenerator : public DefGenerator {
AttrDefGenerator(const llvm::RecordKeeper &records, raw_ostream &os,
bool formatErrorIsFatal)
: DefGenerator(records.getAllDerivedDefinitionsIfDefined("AttrDef"), os,
"Attr", "Attribute", /*isAttrGenerator=*/true,
formatErrorIsFatal) {}
};
/// A specialized generator for TypeDefs.
struct TypeDefGenerator : public DefGenerator {
TypeDefGenerator(const llvm::RecordKeeper &records, raw_ostream &os,
bool formatErrorIsFatal)
: DefGenerator(records.getAllDerivedDefinitionsIfDefined("TypeDef"), os,
"Type", "Type", /*isAttrGenerator=*/false,
formatErrorIsFatal) {}
};

void emitTypeConstraintDecls(const llvm::RecordKeeper &records,
raw_ostream &os);

void emitTypeConstraintDefs(const llvm::RecordKeeper &records, raw_ostream &os);

} // namespace tblgen
} // namespace mlir

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
//
//===----------------------------------------------------------------------===//

#ifndef MLIR_TOOLS_MLIRTBLGEN_ATTRORTYPEFORMATGEN_H_
#define MLIR_TOOLS_MLIRTBLGEN_ATTRORTYPEFORMATGEN_H_
#ifndef MLIR_MLIRTBLGEN_ATTRORTYPEFORMATGEN_H_
#define MLIR_MLIRTBLGEN_ATTRORTYPEFORMATGEN_H_

#include "mlir/TableGen/Class.h"

Expand All @@ -18,9 +18,9 @@ class AttrOrTypeDef;
/// Generate a parser and printer based on a custom assembly format for an
/// attribute or type.
void generateAttrOrTypeFormat(const AttrOrTypeDef &def, MethodBody &parser,
MethodBody &printer);
MethodBody &printer, bool formatErrorIsFatal);

} // namespace tblgen
} // namespace mlir

#endif // MLIR_TOOLS_MLIRTBLGEN_ATTRORTYPEFORMATGEN_H_
#endif // MLIR_MLIRTBLGEN_ATTRORTYPEFORMATGEN_H_
4 changes: 4 additions & 0 deletions mlir/include/mlir/TableGen/Attribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "llvm/ADT/StringRef.h"

namespace llvm {
class RecordKeeper;
class DefInit;
class Record;
} // namespace llvm
Expand Down Expand Up @@ -212,6 +213,9 @@ class EnumAttr : public Attribute {
// Name of infer type op interface.
extern const char *inferTypeOpInterface;

bool emitEnumDecls(const llvm::RecordKeeper &records, raw_ostream &os);
bool emitEnumDefs(const llvm::RecordKeeper &records, raw_ostream &os);

} // namespace tblgen
} // namespace mlir

Expand Down
19 changes: 19 additions & 0 deletions mlir/include/mlir/TableGen/Bytecode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//===-- Bytecode.h - Bytecode definitions -*- C++ -----------------------*-===//
//
// 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

#ifndef MLIR_TABLEGEN_BYTECODE_H
#define MLIR_TABLEGEN_BYTECODE_H

namespace llvm {
class RecordKeeper;
} // namespace llvm

namespace mlir::tblgen {
bool emitBCRW(const llvm::RecordKeeper &records, raw_ostream &os,
const std::string &selectedBcDialect);
} // namespace mlir::tblgen

#endif // MLIR_TABLEGEN_BYTECODE_H
23 changes: 23 additions & 0 deletions mlir/include/mlir/TableGen/CAPI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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

#ifndef MLIR_TABLEGEN_CAPI_H_
#define MLIR_TABLEGEN_CAPI_H_

#include "mlir/Support/LLVM.h"
#include "llvm/TableGen/Record.h"

namespace mlir {
namespace tblgen {

bool emitPassCAPIImpl(const llvm::RecordKeeper &records, raw_ostream &os,
const std::string &groupName);

bool emitPasssCAPIHeader(const llvm::RecordKeeper &records, raw_ostream &os,
const std::string &groupName);

} // namespace tblgen
} // namespace mlir

#endif // MLIR_TABLEGEN_CAPI_H_
8 changes: 8 additions & 0 deletions mlir/include/mlir/TableGen/Dialect.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#define MLIR_TABLEGEN_DIALECT_H_

#include "mlir/Support/LLVM.h"

#include "llvm/Support/CommandLine.h"
#include "llvm/TableGen/Record.h"

#include <string>
Expand Down Expand Up @@ -112,6 +114,12 @@ class Dialect {
const llvm::Record *def;
std::vector<StringRef> dependentDialects;
};

bool emitDialectDecls(const llvm::RecordKeeper &records, raw_ostream &os,
const llvm::cl::opt<std::string> &selectedDialect);
bool emitDialectDefs(const llvm::RecordKeeper &records, raw_ostream &os,
const llvm::cl::opt<std::string> &selectedDialect);

} // namespace tblgen
} // namespace mlir

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@
//
//===----------------------------------------------------------------------===//

#ifndef MLIR_TOOLS_MLIRTBLGEN_DIALECTGENUTILITIES_H_
#define MLIR_TOOLS_MLIRTBLGEN_DIALECTGENUTILITIES_H_
#ifndef MLIR_MLIRTBLGEN_DIALECTGENUTILITIES_H_
#define MLIR_MLIRTBLGEN_DIALECTGENUTILITIES_H_

#include "mlir/Support/LLVM.h"

#include "llvm/Support/CommandLine.h"

namespace mlir {
namespace tblgen {
class Dialect;

/// Find the dialect selected by the user to generate for. Returns std::nullopt
/// if no dialect was found, or if more than one potential dialect was found.
std::optional<Dialect> findDialectToGenerate(ArrayRef<Dialect> dialects);
std::optional<Dialect>
findDialectToGenerate(ArrayRef<Dialect> dialects,
const llvm::cl::opt<std::string> &selectedDialect);
} // namespace tblgen
} // namespace mlir

#endif // MLIR_TOOLS_MLIRTBLGEN_DIALECTGENUTILITIES_H_
#endif // MLIR_MLIRTBLGEN_DIALECTGENUTILITIES_H_
24 changes: 24 additions & 0 deletions mlir/include/mlir/TableGen/Directive.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===- Directive.h - Directive class -----------------------------*- C++-*-===//
//
// 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

#ifndef MLIR_TABLEGEN_DIRECTIVE_H_
#define MLIR_TABLEGEN_DIRECTIVE_H_

#include "mlir/Support/LLVM.h"
#include "llvm/TableGen/Record.h"

#include <string>
#include <vector>

namespace mlir {
namespace tblgen {
bool emitDirectiveDecls(const llvm::RecordKeeper &records,
llvm::StringRef dialect, raw_ostream &os);

} // namespace tblgen
} // namespace mlir

#endif // MLIR_TABLEGEN_DIRECTIVE_H_
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@
//
//===----------------------------------------------------------------------===//

#ifndef MLIR_TOOLS_MLIRTBLGEN_DOCGENUTILITIES_H_
#define MLIR_TOOLS_MLIRTBLGEN_DOCGENUTILITIES_H_
#ifndef MLIR_MLIRTBLGEN_DOCGENUTILITIES_H_
#define MLIR_MLIRTBLGEN_DOCGENUTILITIES_H_

#include "llvm/ADT/StringRef.h"
#include "llvm/Support/CommandLine.h"

namespace llvm {
class raw_ostream;
class RecordKeeper;
} // namespace llvm

namespace mlir {
namespace tblgen {
class AttrOrTypeDef;

// Emit the summary. To avoid confusion, the summary is styled differently from
// the description.
Expand All @@ -39,7 +42,28 @@ void emitDescription(llvm::StringRef description, llvm::raw_ostream &os);
void emitDescriptionComment(llvm::StringRef description, llvm::raw_ostream &os,
llvm::StringRef prefix = "");

void emitAttrOrTypeDefDoc(const AttrOrTypeDef &def, llvm::raw_ostream &os);

void emitAttrOrTypeDefDoc(const llvm::RecordKeeper &records,
llvm::raw_ostream &os,
llvm::StringRef recordTypeName);

bool emitDialectDoc(const llvm::RecordKeeper &records, llvm::raw_ostream &os,
const llvm::cl::opt<std::string> &selectedDialect,
const std::string &stripPrefix,
bool allowHugoSpecificFeatures,
const std::string &opIncFilter,
const std::string &opExcFilter);

void emitOpDoc(const llvm::RecordKeeper &records, llvm::raw_ostream &os,
const std::string &stripPrefix, bool allowHugoSpecificFeatures,
const std::string &opIncFilter, const std::string &opExcFilter);

void emitEnumDoc(const llvm::RecordKeeper &records, llvm::raw_ostream &os);

void emitPassDocs(const llvm::RecordKeeper &records, llvm::raw_ostream &os);

} // namespace tblgen
} // namespace mlir

#endif // MLIR_TOOLS_MLIRTBLGEN_DOCGENUTILITIES_H_
#endif // MLIR_MLIRTBLGEN_DOCGENUTILITIES_H_
Loading