-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[NFC] Improve readability of AttrHelper usage #135873
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -110,15 +110,23 @@ tblgen::findDialectToGenerate(ArrayRef<Dialect> dialects) { | |
/// {2}: The dialect parent class. | ||
static const char *const dialectDeclBeginStr = R"( | ||
class {0} : public ::mlir::{2} { | ||
typedef {0} DialectType; | ||
explicit {0}(::mlir::MLIRContext *context); | ||
|
||
void initialize(); | ||
friend class ::mlir::MLIRContext; | ||
public: | ||
~{0}() override; | ||
static constexpr ::llvm::StringLiteral getDialectNamespace() { | ||
static constexpr ::llvm::StringLiteral getDialectNamespace() {{ | ||
return ::llvm::StringLiteral("{1}"); | ||
} | ||
static const DialectType *getLoaded(::mlir::MLIRContext &context) {{ | ||
return context.getLoadedDialect<DialectType>(); | ||
} | ||
static const DialectType *getLoaded(::mlir::MLIRContext *context) {{ | ||
return getLoaded(*context); | ||
} | ||
static const DialectType *getLoaded(::mlir::Operation *operation); | ||
)"; | ||
|
||
/// Registration for a single dependent dialect: to be inserted in the ctor | ||
|
@@ -206,28 +214,28 @@ static const char *const discardableAttrHelperDecl = R"( | |
static constexpr ::llvm::StringLiteral getNameStr() {{ | ||
return "{4}.{1}"; | ||
} | ||
constexpr ::mlir::StringAttr getName() {{ | ||
constexpr ::mlir::StringAttr getName() const {{ | ||
return name; | ||
} | ||
|
||
{0}AttrHelper(::mlir::MLIRContext *ctx) | ||
: name(::mlir::StringAttr::get(ctx, getNameStr())) {{} | ||
|
||
{2} getAttr(::mlir::Operation *op) {{ | ||
return op->getAttrOfType<{2}>(name); | ||
} | ||
void setAttr(::mlir::Operation *op, {2} val) {{ | ||
op->setAttr(name, val); | ||
} | ||
bool isAttrPresent(::mlir::Operation *op) {{ | ||
return op->hasAttrOfType<{2}>(name); | ||
} | ||
void removeAttr(::mlir::Operation *op) {{ | ||
assert(op->hasAttrOfType<{2}>(name)); | ||
op->removeAttr(name); | ||
} | ||
{2} getAttr(::mlir::Operation *op) const {{ | ||
return op->getAttrOfType<{2}>(name); | ||
} | ||
void setAttr(::mlir::Operation *op, {2} val) const {{ | ||
op->setAttr(name, val); | ||
} | ||
bool isAttrPresent(::mlir::Operation *op) const {{ | ||
return op->hasAttrOfType<{2}>(name); | ||
} | ||
void removeAttr(::mlir::Operation *op) const {{ | ||
assert(op->hasAttrOfType<{2}>(name)); | ||
op->removeAttr(name); | ||
} | ||
}; | ||
{0}AttrHelper get{0}AttrHelper() { | ||
const {0}AttrHelper get{0}AttrHelper() const { | ||
return {3}AttrName; | ||
} | ||
private: | ||
|
@@ -342,6 +350,16 @@ static const char *const dialectDestructorStr = R"( | |
|
||
)"; | ||
|
||
/// The code block to generate a member funcs. | ||
/// | ||
/// {0}: The name of the dialect class. | ||
static const char *const dialectStaticMemberDefs = R"( | ||
const {0} *{0}::getLoaded(::mlir::Operation *operation) {{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not convinced of the utility of this method over just casting from the operations dialect. This method is going to be significantly slower than calling getDialect on the operation itself. If we wanted to make this easier, we should rather consider auto-generating a templated version of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This also seems like an unrelated change compared to the description of this commit. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The dialect of the operation may not be the same dialect that owns the attribute though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method seems ad-hoc to me and does not provide much convenience compared to the current code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nothing really, just adding for convenience. I was asked on a dialect in a separate project to make usage of AttrHelpers cleaner. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This particular method was added in the cpp file gen so Operation.h would not require inclusion by all Dialects. But it is just an extra convenience, happy to remove. |
||
return getLoaded(*operation->getContext()); | ||
} | ||
|
||
)"; | ||
|
||
static void emitDialectDef(Dialect &dialect, const RecordKeeper &records, | ||
raw_ostream &os) { | ||
std::string cppClassName = dialect.getCppClassName(); | ||
|
@@ -388,6 +406,9 @@ static void emitDialectDef(Dialect &dialect, const RecordKeeper &records, | |
discardableAttributesInit); | ||
if (!dialect.hasNonDefaultDestructor()) | ||
os << llvm::formatv(dialectDestructorStr, cppClassName); | ||
|
||
// Emit member function definitions. | ||
os << llvm::formatv(dialectStaticMemberDefs, cppClassName); | ||
} | ||
|
||
static bool emitDialectDefs(const RecordKeeper &records, raw_ostream &os) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not understanding the reasoning behind these changes. When sending a commit for review, please add context as to "why" a change is being proposed.