MLIR  21.0.0git
Attributes.cpp
Go to the documentation of this file.
1 //===- Attributes.cpp - MLIR Affine Expr Classes --------------------------===//
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 #include "mlir/IR/Attributes.h"
10 #include "mlir/IR/Dialect.h"
11 
12 using namespace mlir;
13 using namespace mlir::detail;
14 
15 //===----------------------------------------------------------------------===//
16 // AbstractAttribute
17 //===----------------------------------------------------------------------===//
18 
20  Attribute attr, function_ref<void(Attribute)> walkAttrsFn,
21  function_ref<void(Type)> walkTypesFn) const {
22  walkImmediateSubElementsFn(attr, walkAttrsFn, walkTypesFn);
23 }
24 
27  ArrayRef<Attribute> replAttrs,
28  ArrayRef<Type> replTypes) const {
29  return replaceImmediateSubElementsFn(attr, replAttrs, replTypes);
30 }
31 
32 //===----------------------------------------------------------------------===//
33 // Attribute
34 //===----------------------------------------------------------------------===//
35 
36 /// Return the context this attribute belongs to.
37 MLIRContext *Attribute::getContext() const { return getDialect().getContext(); }
38 
39 //===----------------------------------------------------------------------===//
40 // NamedAttribute
41 //===----------------------------------------------------------------------===//
42 
44  : name(name), value(value) {
45  assert(name && value && "expected valid attribute name and value");
46  assert(!name.empty() && "expected valid attribute name");
47 }
48 
49 NamedAttribute::NamedAttribute(StringRef name, Attribute value) : value(value) {
50  assert(value && "expected valid attribute value");
51  assert(!name.empty() && "expected valid attribute name");
52  this->name = StringAttr::get(value.getContext(), name);
53 }
54 
55 StringAttr NamedAttribute::getName() const {
56  return llvm::cast<StringAttr>(name);
57 }
58 
60  return getName().getReferencedDialect();
61 }
62 
63 void NamedAttribute::setName(StringAttr newName) {
64  assert(name && "expected valid attribute name");
65  name = newName;
66 }
67 
68 bool NamedAttribute::operator<(const NamedAttribute &rhs) const {
69  return getName().compare(rhs.getName()) < 0;
70 }
71 
72 bool NamedAttribute::operator<(StringRef rhs) const {
73  return getName().getValue().compare(rhs) < 0;
74 }
void walkImmediateSubElements(Attribute attr, function_ref< void(Attribute)> walkAttrsFn, function_ref< void(Type)> walkTypesFn) const
Walk the immediate sub-elements of this attribute.
Definition: Attributes.cpp:19
Attribute replaceImmediateSubElements(Attribute attr, ArrayRef< Attribute > replAttrs, ArrayRef< Type > replTypes) const
Replace the immediate sub-elements of this attribute.
Definition: Attributes.cpp:26
Attributes are known-constant values of operations.
Definition: Attributes.h:25
MLIRContext * getContext() const
Return the context this attribute belongs to.
Definition: Attributes.cpp:37
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
Definition: Dialect.h:38
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
NamedAttribute represents a combination of a name and an Attribute value.
Definition: Attributes.h:207
StringAttr getName() const
Return the name of the attribute.
Definition: Attributes.cpp:55
void setName(StringAttr newName)
Set the name of this attribute.
Definition: Attributes.cpp:63
bool operator<(const NamedAttribute &rhs) const
Compare this attribute to the provided attribute, ordering by name.
Definition: Attributes.cpp:68
NamedAttribute(StringAttr name, Attribute value)
Definition: Attributes.cpp:43
Dialect * getNameDialect() const
Return the dialect of the name of this attribute, if the name is prefixed by a dialect namespace.
Definition: Attributes.cpp:59
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
AttrTypeReplacer.
Include the generated interface declarations.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...