[Mlir-commits] [llvm] [mlir] [MLIR][IR] Add ConstantLikeInterface for extensible constant creation (PR #177740)
Ryan Kim
llvmlistbot at llvm.org
Sat Jan 24 03:35:24 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)
----------------
chokobole wrote:
The main target for this is `getZeroAttr()` and `getOneAttr()` in `Builder`
My background is cryptography, so there we play around with integer, but this integer is a modular integer. So we want to treat it as normal integer, but without this every time we have code that uses `getZeroAttr()` and `getOneAttr()`, we need to add a patch kinda.
https://github.com/llvm/llvm-project/pull/177740
More information about the Mlir-commits
mailing list