Skip to content

Commit 35f55f5

Browse files
[mlir][ODS] Consistent cppType / cppClassName usage (llvm#102657)
Make sure that the usage of `cppType` and `cppClassName` of type and attribute definitions/constraints is consistent in TableGen. - `cppClassName`: The C++ class name of the type or attribute. - `cppType`: The fully qualified C++ class name: C++ namespace and C++ class name. Basically, we should always use the fully qualified C++ class name for parameter types, return types or template arguments. Also some minor cleanups. Fixes llvm#57279.
1 parent 101cf54 commit 35f55f5

File tree

9 files changed

+44
-69
lines changed

9 files changed

+44
-69
lines changed

mlir/include/mlir/IR/AttrTypeBase.td

+9-9
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ class AttrDef<Dialect dialect, string name, list<Trait> traits = [],
256256
AttrOrTypeDef<"Attr", name, traits, baseCppClass> {
257257
// The name of the C++ Attribute class.
258258
string cppClassName = name # "Attr";
259-
let storageType = dialect.cppNamespace # "::" # name # "Attr";
259+
let storageType = dialect.cppNamespace # "::" # cppClassName;
260260

261261
// The underlying C++ value type
262262
let returnType = dialect.cppNamespace # "::" # cppClassName;
@@ -275,12 +275,10 @@ class AttrDef<Dialect dialect, string name, list<Trait> traits = [],
275275
//
276276
// For example, `$_self.getValue().getSExtValue()` for `IntegerAttr val` will
277277
// expand to `getAttrOfType<IntegerAttr>("val").getValue().getSExtValue()`.
278-
let convertFromStorage = "::llvm::cast<" # dialect.cppNamespace #
279-
"::" # cppClassName # ">($_self)";
278+
let convertFromStorage = "::llvm::cast<" # cppType # ">($_self)";
280279

281280
// The predicate for when this def is used as a constraint.
282-
let predicate = CPred<"::llvm::isa<" # dialect.cppNamespace #
283-
"::" # cppClassName # ">($_self)">;
281+
let predicate = CPred<"::llvm::isa<" # cppType # ">($_self)">;
284282
}
285283

286284
// Define a new type, named `name`, belonging to `dialect` that inherits from
@@ -289,6 +287,9 @@ class TypeDef<Dialect dialect, string name, list<Trait> traits = [],
289287
string baseCppClass = "::mlir::Type">
290288
: DialectType<dialect, CPred<"">, /*descr*/"", name # "Type">,
291289
AttrOrTypeDef<"Type", name, traits, baseCppClass> {
290+
// The name of the C++ Type class.
291+
string cppClassName = name # "Type";
292+
292293
// Make it possible to use such type as parameters for other types.
293294
string cppType = dialect.cppNamespace # "::" # cppClassName;
294295

@@ -297,12 +298,11 @@ class TypeDef<Dialect dialect, string name, list<Trait> traits = [],
297298

298299
// A constant builder provided when the type has no parameters.
299300
let builderCall = !if(!empty(parameters),
300-
"$_builder.getType<" # dialect.cppNamespace #
301-
"::" # cppClassName # ">()",
301+
"$_builder.getType<" # cppType # ">()",
302302
"");
303+
303304
// The predicate for when this def is used as a constraint.
304-
let predicate = CPred<"::llvm::isa<" # dialect.cppNamespace #
305-
"::" # cppClassName # ">($_self)">;
305+
let predicate = CPred<"::llvm::isa<" # cppType # ">($_self)">;
306306
}
307307

308308
//===----------------------------------------------------------------------===//

mlir/include/mlir/IR/CommonAttrConstraints.td

+3-3
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,14 @@ def AnyAttr : Attr<CPred<"true">, "any attribute"> {
169169

170170
// Any attribute from the given list
171171
class AnyAttrOf<list<Attr> allowedAttrs, string summary = "",
172-
string cppClassName = "::mlir::Attribute",
172+
string cppType = "::mlir::Attribute",
173173
string fromStorage = "$_self"> : Attr<
174174
// Satisfy any of the allowed attribute's condition
175175
Or<!foreach(allowedattr, allowedAttrs, allowedattr.predicate)>,
176176
!if(!eq(summary, ""),
177177
!interleave(!foreach(t, allowedAttrs, t.summary), " or "),
178178
summary)> {
179-
let returnType = cppClassName;
179+
let returnType = cppType;
180180
let convertFromStorage = fromStorage;
181181
}
182182

@@ -369,7 +369,7 @@ def TypeAttr : TypeAttrBase<"::mlir::Type", "any type attribute"> {
369369
}
370370

371371
class TypeAttrOf<Type ty>
372-
: TypeAttrBase<ty.cppClassName, "type attribute of " # ty.summary,
372+
: TypeAttrBase<ty.cppType, "type attribute of " # ty.summary,
373373
ty.predicate> {
374374
let constBuilderCall = "::mlir::TypeAttr::get($0)";
375375
}

mlir/include/mlir/IR/CommonTypeConstraints.td

+17-17
Original file line numberDiff line numberDiff line change
@@ -98,31 +98,31 @@ def HasValueSemanticsPred : CPred<"$_self.hasTrait<::mlir::ValueSemantics>()">;
9898

9999
// A type, carries type constraints.
100100
class Type<Pred condition, string descr = "",
101-
string cppClassName = "::mlir::Type"> :
102-
TypeConstraint<condition, descr, cppClassName> {
101+
string cppType = "::mlir::Type"> :
102+
TypeConstraint<condition, descr, cppType> {
103103
string description = "";
104104
string builderCall = "";
105105
}
106106

107107
// Allows providing an alternative name and summary to an existing type def.
108108
class TypeAlias<Type t, string summary = t.summary> :
109-
Type<t.predicate, summary, t.cppClassName> {
109+
Type<t.predicate, summary, t.cppType> {
110110
let description = t.description;
111111
let builderCall = t.builderCall;
112112
}
113113

114114
// A type of a specific dialect.
115115
class DialectType<Dialect d, Pred condition, string descr = "",
116-
string cppClassName = "::mlir::Type"> :
117-
Type<condition, descr, cppClassName> {
116+
string cppType = "::mlir::Type"> :
117+
Type<condition, descr, cppType> {
118118
Dialect dialect = d;
119119
}
120120

121121
// A variadic type constraint. It expands to zero or more of the base type. This
122122
// class is used for supporting variadic operands/results.
123123
class Variadic<Type type> : TypeConstraint<type.predicate,
124124
"variadic of " # type.summary,
125-
type.cppClassName> {
125+
type.cppType> {
126126
Type baseType = type;
127127
int minSize = 0;
128128
}
@@ -140,7 +140,7 @@ class VariadicOfVariadic<Type type, string variadicSegmentAttrName>
140140
// An optional type constraint. It expands to either zero or one of the base
141141
// type. This class is used for supporting optional operands/results.
142142
class Optional<Type type> : TypeConstraint<type.predicate, type.summary,
143-
type.cppClassName> {
143+
type.cppType> {
144144
Type baseType = type;
145145
}
146146

@@ -172,33 +172,33 @@ def NoneType : Type<CPred<"::llvm::isa<::mlir::NoneType>($_self)">, "none type",
172172

173173
// Any type from the given list
174174
class AnyTypeOf<list<Type> allowedTypeList, string summary = "",
175-
string cppClassName = "::mlir::Type"> : Type<
175+
string cppType = "::mlir::Type"> : Type<
176176
// Satisfy any of the allowed types' conditions.
177177
Or<!foreach(allowedtype, allowedTypeList, allowedtype.predicate)>,
178178
!if(!eq(summary, ""),
179179
!interleave(!foreach(t, allowedTypeList, t.summary), " or "),
180180
summary),
181-
cppClassName> {
181+
cppType> {
182182
list<Type> allowedTypes = allowedTypeList;
183183
}
184184

185185
// A type that satisfies the constraints of all given types.
186186
class AllOfType<list<Type> allowedTypeList, string summary = "",
187-
string cppClassName = "::mlir::Type"> : Type<
187+
string cppType = "::mlir::Type"> : Type<
188188
// Satisfy all of the allowed types' conditions.
189189
And<!foreach(allowedType, allowedTypeList, allowedType.predicate)>,
190190
!if(!eq(summary, ""),
191191
!interleave(!foreach(t, allowedTypeList, t.summary), " and "),
192192
summary),
193-
cppClassName> {
193+
cppType> {
194194
list<Type> allowedTypes = allowedTypeList;
195195
}
196196

197197
// A type that satisfies additional predicates.
198198
class ConfinedType<Type type, list<Pred> predicates, string summary = "",
199-
string cppClassName = type.cppClassName> : Type<
199+
string cppType = type.cppType> : Type<
200200
And<!listconcat([type.predicate], !foreach(pred, predicates, pred))>,
201-
summary, cppClassName>;
201+
summary, cppType>;
202202

203203
// Integer types.
204204

@@ -375,23 +375,23 @@ def FunctionType : Type<CPred<"::llvm::isa<::mlir::FunctionType>($_self)">,
375375

376376
// A container type is a type that has another type embedded within it.
377377
class ContainerType<Type etype, Pred containerPred, code elementTypeCall,
378-
string descr, string cppClassName = "::mlir::Type"> :
378+
string descr, string cppType = "::mlir::Type"> :
379379
// First, check the container predicate. Then, substitute the extracted
380380
// element into the element type checker.
381381
Type<And<[containerPred,
382382
SubstLeaves<"$_self", !cast<string>(elementTypeCall),
383383
etype.predicate>]>,
384-
descr # " of " # etype.summary # " values", cppClassName>;
384+
descr # " of " # etype.summary # " values", cppType>;
385385

386386
class ShapedContainerType<list<Type> allowedTypes,
387387
Pred containerPred, string descr,
388-
string cppClassName = "::mlir::Type"> :
388+
string cppType = "::mlir::Type"> :
389389
Type<And<[containerPred,
390390
Concat<"[](::mlir::Type elementType) { return ",
391391
SubstLeaves<"$_self", "elementType",
392392
AnyTypeOf<allowedTypes>.predicate>,
393393
"; }(::llvm::cast<::mlir::ShapedType>($_self).getElementType())">]>,
394-
descr # " of " # AnyTypeOf<allowedTypes>.summary # " values", cppClassName>;
394+
descr # " of " # AnyTypeOf<allowedTypes>.summary # " values", cppType>;
395395

396396
// Whether a shaped type is ranked.
397397
def HasRankPred : CPred<"::llvm::cast<::mlir::ShapedType>($_self).hasRank()">;

mlir/include/mlir/IR/Constraints.td

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,10 @@ class Constraint<Pred pred, string desc = ""> {
149149

150150
// Subclass for constraints on a type.
151151
class TypeConstraint<Pred predicate, string summary = "",
152-
string cppClassNameParam = "::mlir::Type"> :
152+
string cppTypeParam = "::mlir::Type"> :
153153
Constraint<predicate, summary> {
154154
// The name of the C++ Type class if known, or Type if not.
155-
string cppClassName = cppClassNameParam;
155+
string cppType = cppTypeParam;
156156
}
157157

158158
// Subclass for constraints on an attribute.

mlir/include/mlir/TableGen/Type.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ class TypeConstraint : public Constraint {
5656
// returns std::nullopt otherwise.
5757
std::optional<StringRef> getBuilderCall() const;
5858

59-
// Return the C++ class name for this type (which may just be ::mlir::Type).
60-
std::string getCPPClassName() const;
59+
// Return the C++ type for this type (which may just be ::mlir::Type).
60+
StringRef getCppType() const;
6161
};
6262

6363
// Wrapper class with helper methods for accessing Types defined in TableGen.

mlir/lib/TableGen/Type.cpp

+3-14
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,9 @@ std::optional<StringRef> TypeConstraint::getBuilderCall() const {
5959
.Default([](auto *) { return std::nullopt; });
6060
}
6161

62-
// Return the C++ class name for this type (which may just be ::mlir::Type).
63-
std::string TypeConstraint::getCPPClassName() const {
64-
StringRef className = def->getValueAsString("cppClassName");
65-
66-
// If the class name is already namespace resolved, use it.
67-
if (className.contains("::"))
68-
return className.str();
69-
70-
// Otherwise, check to see if there is a namespace from a dialect to prepend.
71-
if (const llvm::RecordVal *value = def->getValue("dialect")) {
72-
Dialect dialect(cast<const llvm::DefInit>(value->getValue())->getDef());
73-
return (dialect.getCppNamespace() + "::" + className).str();
74-
}
75-
return className.str();
62+
// Return the C++ type for this type (which may just be ::mlir::Type).
63+
StringRef TypeConstraint::getCppType() const {
64+
return def->getValueAsString("cppType");
7665
}
7766

7867
Type::Type(const llvm::Record *record) : TypeConstraint(record) {}

mlir/lib/Tools/PDLL/Parser/Parser.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -879,8 +879,7 @@ void Parser::processTdIncludeRecords(llvm::RecordKeeper &tdRecords,
879879
-> const ods::TypeConstraint & {
880880
return odsContext.insertTypeConstraint(
881881
cst.constraint.getUniqueDefName(),
882-
processDoc(cst.constraint.getSummary()),
883-
cst.constraint.getCPPClassName());
882+
processDoc(cst.constraint.getSummary()), cst.constraint.getCppType());
884883
};
885884
auto convertLocToRange = [&](llvm::SMLoc loc) -> llvm::SMRange {
886885
return {loc, llvm::SMLoc::getFromPointer(loc.getPointer() + 1)};
@@ -944,7 +943,7 @@ void Parser::processTdIncludeRecords(llvm::RecordKeeper &tdRecords,
944943
tblgen::TypeConstraint constraint(def);
945944
decls.push_back(createODSNativePDLLConstraintDecl<ast::TypeConstraintDecl>(
946945
constraint, convertLocToRange(def->getLoc().front()), typeTy,
947-
constraint.getCPPClassName()));
946+
constraint.getCppType()));
948947
}
949948
/// OpInterfaces.
950949
ast::Type opTy = ast::OperationType::get(ctx);

mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp

+3-16
Original file line numberDiff line numberDiff line change
@@ -2085,21 +2085,8 @@ static void generateValueRangeStartAndEnd(
20852085
}
20862086

20872087
static std::string generateTypeForGetter(const NamedTypeConstraint &value) {
2088-
std::string str = "::mlir::Value";
2089-
/// If the CPPClassName is not a fully qualified type. Uses of types
2090-
/// across Dialect fail because they are not in the correct namespace. So we
2091-
/// dont generate TypedValue unless the type is fully qualified.
2092-
/// getCPPClassName doesn't return the fully qualified path for
2093-
/// `mlir::pdl::OperationType` see
2094-
/// https://github.com/llvm/llvm-project/issues/57279.
2095-
/// Adaptor will have values that are not from the type of their operation and
2096-
/// this is expected, so we dont generate TypedValue for Adaptor
2097-
if (value.constraint.getCPPClassName() != "::mlir::Type" &&
2098-
StringRef(value.constraint.getCPPClassName()).starts_with("::"))
2099-
str = llvm::formatv("::mlir::TypedValue<{0}>",
2100-
value.constraint.getCPPClassName())
2101-
.str();
2102-
return str;
2088+
return llvm::formatv("::mlir::TypedValue<{0}>", value.constraint.getCppType())
2089+
.str();
21032090
}
21042091

21052092
// Generates the named operand getter methods for the given Operator `op` and
@@ -3944,7 +3931,7 @@ void OpEmitter::genTraits() {
39443931
// For single result ops with a known specific type, generate a OneTypedResult
39453932
// trait.
39463933
if (numResults == 1 && numVariadicResults == 0) {
3947-
auto cppName = op.getResults().begin()->constraint.getCPPClassName();
3934+
auto cppName = op.getResults().begin()->constraint.getCppType();
39483935
opClass.addTrait("::mlir::OpTrait::OneTypedResult<" + cppName + ">::Impl");
39493936
}
39503937

mlir/tools/mlir-tblgen/OpFormatGen.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1657,7 +1657,7 @@ void OperationFormat::genElementParser(FormatElement *element, MethodBody &body,
16571657
TypeSwitch<FormatElement *>(dir->getArg())
16581658
.Case<OperandVariable, ResultVariable>([&](auto operand) {
16591659
body << formatv(parserCode,
1660-
operand->getVar()->constraint.getCPPClassName(),
1660+
operand->getVar()->constraint.getCppType(),
16611661
listName);
16621662
})
16631663
.Default([&](auto operand) {
@@ -2603,7 +2603,7 @@ void OperationFormat::genElementPrinter(FormatElement *element,
26032603
}
26042604
if (var && !var->isVariadicOfVariadic() && !var->isVariadic() &&
26052605
!var->isOptional()) {
2606-
std::string cppClass = var->constraint.getCPPClassName();
2606+
StringRef cppType = var->constraint.getCppType();
26072607
if (dir->shouldBeQualified()) {
26082608
body << " _odsPrinter << " << op.getGetterName(var->name)
26092609
<< "().getType();\n";
@@ -2612,7 +2612,7 @@ void OperationFormat::genElementPrinter(FormatElement *element,
26122612
body << " {\n"
26132613
<< " auto type = " << op.getGetterName(var->name)
26142614
<< "().getType();\n"
2615-
<< " if (auto validType = ::llvm::dyn_cast<" << cppClass
2615+
<< " if (auto validType = ::llvm::dyn_cast<" << cppType
26162616
<< ">(type))\n"
26172617
<< " _odsPrinter.printStrippedAttrOrType(validType);\n"
26182618
<< " else\n"

0 commit comments

Comments
 (0)