Skip to content

[MLIR][ODS] Make dialect attribute helper member functions const (NFC)#181111

Merged
Dinistro merged 1 commit into
mainfrom
users/dinistro/add-constness-to-attr-helpers
Feb 13, 2026
Merged

[MLIR][ODS] Make dialect attribute helper member functions const (NFC)#181111
Dinistro merged 1 commit into
mainfrom
users/dinistro/add-constness-to-attr-helpers

Conversation

@Dinistro
Copy link
Copy Markdown
Contributor

This commit marks member functions of dialect attribute helpers as constant. This ensures that these helpers can be used as members of rewrite patterns, whose matchAndRewrite functions are marked as const as well.

This commit marks member functions of dialect attribute helpers as
constant. This ensures that these helpers can be added as members of
rewrite patterns, whose `matchAndRewrite` functions are marked as const
as well.
@Dinistro Dinistro requested a review from joker-eph February 12, 2026 10:01
@llvmbot llvmbot added mlir:core MLIR Core Infrastructure mlir labels Feb 12, 2026
@llvmbot llvmbot temporarily deployed to main-branch-only February 12, 2026 10:01 — with GitHub Actions Inactive
@llvmbot llvmbot temporarily deployed to main-branch-only February 12, 2026 10:01 — with GitHub Actions Inactive
@llvmbot
Copy link
Copy Markdown
Member

llvmbot commented Feb 12, 2026

@llvm/pr-subscribers-mlir

Author: Christian Ulmann (Dinistro)

Changes

This commit marks member functions of dialect attribute helpers as constant. This ensures that these helpers can be used as members of rewrite patterns, whose matchAndRewrite functions are marked as const as well.


Full diff: https://github.com/llvm/llvm-project/pull/181111.diff

1 Files Affected:

  • (modified) mlir/tools/mlir-tblgen/DialectGen.cpp (+6-6)
diff --git a/mlir/tools/mlir-tblgen/DialectGen.cpp b/mlir/tools/mlir-tblgen/DialectGen.cpp
index c2c0c1f415254..8eecad39f49f3 100644
--- a/mlir/tools/mlir-tblgen/DialectGen.cpp
+++ b/mlir/tools/mlir-tblgen/DialectGen.cpp
@@ -207,23 +207,23 @@ 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)
+      explicit {0}AttrHelper(::mlir::MLIRContext *ctx)
         : name(::mlir::StringAttr::get(ctx, getNameStr())) {{}
 
-     {2} getAttr(::mlir::Operation *op) {{
+     {2} getAttr(::mlir::Operation *op) const {{
        return op->getAttrOfType<{2}>(name);
      }
-     void setAttr(::mlir::Operation *op, {2} val) {{
+     void setAttr(::mlir::Operation *op, {2} val) const {{
        op->setAttr(name, val);
      }
-     bool isAttrPresent(::mlir::Operation *op) {{
+     bool isAttrPresent(::mlir::Operation *op) const {{
        return op->hasAttrOfType<{2}>(name);
      }
-     void removeAttr(::mlir::Operation *op) {{
+     void removeAttr(::mlir::Operation *op) const {{
        assert(op->hasAttrOfType<{2}>(name));
        op->removeAttr(name);
      }

@llvmbot
Copy link
Copy Markdown
Member

llvmbot commented Feb 12, 2026

@llvm/pr-subscribers-mlir-core

Author: Christian Ulmann (Dinistro)

Changes

This commit marks member functions of dialect attribute helpers as constant. This ensures that these helpers can be used as members of rewrite patterns, whose matchAndRewrite functions are marked as const as well.


Full diff: https://github.com/llvm/llvm-project/pull/181111.diff

1 Files Affected:

  • (modified) mlir/tools/mlir-tblgen/DialectGen.cpp (+6-6)
diff --git a/mlir/tools/mlir-tblgen/DialectGen.cpp b/mlir/tools/mlir-tblgen/DialectGen.cpp
index c2c0c1f415254..8eecad39f49f3 100644
--- a/mlir/tools/mlir-tblgen/DialectGen.cpp
+++ b/mlir/tools/mlir-tblgen/DialectGen.cpp
@@ -207,23 +207,23 @@ 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)
+      explicit {0}AttrHelper(::mlir::MLIRContext *ctx)
         : name(::mlir::StringAttr::get(ctx, getNameStr())) {{}
 
-     {2} getAttr(::mlir::Operation *op) {{
+     {2} getAttr(::mlir::Operation *op) const {{
        return op->getAttrOfType<{2}>(name);
      }
-     void setAttr(::mlir::Operation *op, {2} val) {{
+     void setAttr(::mlir::Operation *op, {2} val) const {{
        op->setAttr(name, val);
      }
-     bool isAttrPresent(::mlir::Operation *op) {{
+     bool isAttrPresent(::mlir::Operation *op) const {{
        return op->hasAttrOfType<{2}>(name);
      }
-     void removeAttr(::mlir::Operation *op) {{
+     void removeAttr(::mlir::Operation *op) const {{
        assert(op->hasAttrOfType<{2}>(name));
        op->removeAttr(name);
      }

@matthias-springer
Copy link
Copy Markdown
Member

these helpers can be used as members of rewrite patterns

What does that mean? Can you post a small example of what does not compile?

@Dinistro
Copy link
Copy Markdown
Contributor Author

these helpers can be used as members of rewrite patterns

What does that mean? Can you post a small example of what does not compile?

Sure, sorry for not adding it right away. Something like this does not work:

struct ExamplePattern : public OpConversionPattern<MyDialect::MyOp> {
  using Base::OpConversionPattern;

  ExamplePattern(MLIRContext *context) : Base(context), helper(context) {}

  LogicalResult
  matchAndRewrite(MyDialect::MyOpop, OpAdaptor adaptor,
                  ConversionPatternRewriter &rewriter) const override {
    // Does not compile, because `setAttr` is not const.              
    helper.setAttr(op, rewriter.getUnitAttr());
    return success();
  }
  
  MyDialect::MyAttrHelper helper;
};

@Dinistro Dinistro merged commit 54c080d into main Feb 13, 2026
13 checks passed
@Dinistro Dinistro deleted the users/dinistro/add-constness-to-attr-helpers branch February 13, 2026 06:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mlir:core MLIR Core Infrastructure mlir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants