[Mlir-commits] [llvm] [mlir] [MLIR][IR] Add ConstantLikeInterface for extensible constant creation (PR #177740)

Matthias Springer llvmlistbot at llvm.org
Sat Jan 24 04:21:54 PST 2026


================
@@ -0,0 +1,82 @@
+//===- ConstantLikeInterface.td - Constant creation interfaces -*- tablegen -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains definitions for type interfaces that allow types to
+// define how to create constant attributes and operations for themselves.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_IR_CONSTANTLIKEINTERFACE_TD_
+#define MLIR_IR_CONSTANTLIKEINTERFACE_TD_
+
+include "mlir/IR/OpBase.td"
+
+//===----------------------------------------------------------------------===//
+// ConstantLikeInterface
+//===----------------------------------------------------------------------===//
+
+def ConstantLikeInterface : TypeInterface<"ConstantLikeInterface"> {
+  let cppNamespace = "::mlir";
+  let description = [{
+    This interface allows types to define how to create constant attributes
+    and operations for their values. This decouples generic MLIR code from
+    specific constant operation types, enabling better layering and extensibility.
+
+    Types implementing this interface can provide custom constant creation logic,
+    which is particularly useful for domain-specific types (e.g., field elements,
+    custom numeric types) that need special constant handling.
+  }];
+
+  let methods = [
+    InterfaceMethod<
+      /*desc=*/[{
+        Creates a constant attribute for this type from the given int64 value.
+        Returns null if the type does not support this operation.
+      }],
+      /*retTy=*/"::mlir::TypedAttr",
+      /*methodName=*/"createConstantAttr",
+      /*args=*/(ins "int64_t ":$value)
----------------
matthias-springer wrote:

How about adding `getZeroAttr` / `getOneAttr` helper functions in your dialect? You could check the type there and then either create an attr from your dialect or call the `Builder::getZeroAttr`/`getOneAttr` implementation. Is there any downside to that approach?

Given that this works only with integers, I feel like this interface is not quite ready to be placed in `mlir/IR` (or the name is too general). (The fact that it cannot be in `mlir/Interfaces` is another code smell.)


https://github.com/llvm/llvm-project/pull/177740


More information about the Mlir-commits mailing list