[llvm] [mlir] [MLIR][IR] Add ConstantLikeInterface for extensible constant creation (PR #177740)
Matthias Springer via llvm-commits
llvm-commits at lists.llvm.org
Sat Jan 24 04:11: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)
+ >,
+ InterfaceMethod<
+ /*desc=*/[{
+ Creates a constant attribute for this type from the given APInt values.
+ The APInt should be compatible with this type's bit width and semantics.
+ Returns null if the type does not support this operation.
+ }],
+ /*retTy=*/"::mlir::TypedAttr",
+ /*methodName=*/"createConstantAttrFromValues",
+ /*args=*/(ins "::llvm::ArrayRef<APInt> ":$values)
+ >,
+ InterfaceMethod<
+ /*desc=*/[{
+ Creates a constant operation for this type with the given attribute.
+ The builder's insertion point should be set before calling this method.
+ Returns null if the type does not support constant operation creation.
+ }],
+ /*retTy=*/"::mlir::Operation *",
+ /*methodName=*/"createConstantOp",
----------------
matthias-springer wrote:
Let me make sure that I understand. The problem with the current implementation is that an `arith.constant` is created, while you want a `field.constant`?
We may be able to use the dialect materializer in `FoldScalarOrSplatConstant`: instead of hard-coding `arith.constant`, the implementation could delegate to the constant materializer of the incoming (to-be-fused) constant:
```
Value scalarConstant = def->getDialect()->materializeConstant(rewriter, constantAttr, constantAttr.getType(), def.getLoc());
```
With this implementation approach, `arith.constant` will stay `arith.constant`. `field.constant` will stay `field.constant`.
https://github.com/llvm/llvm-project/pull/177740
More information about the llvm-commits
mailing list