[Mlir-commits] [llvm] [mlir] [MLIR][IR] Add ConstantLikeInterface for extensible constant creation (PR #177740)
Ryan Kim
llvmlistbot at llvm.org
Sat Jan 24 04:48:15 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",
+ /*args=*/(ins "::mlir::OpBuilder &":$builder,
+ "::mlir::Location":$loc,
+ "::mlir::TypedAttr":$attr)
+ >,
+ InterfaceMethod<
+ /*desc=*/[{
+ Allows custom types to override the shaped type (tensor/vector) used for
+ constant creation. This is useful when a custom element type needs to
+ substitute the shaped type with a different representation (e.g., using
+ a storage type instead of the semantic type).
+ Returns the potentially modified shaped type.
+ }],
+ /*retTy=*/"::mlir::ShapedType",
+ /*methodName=*/"overrideShapedType",
----------------
chokobole wrote:
Because we also have a performance critical usecase that want the tensor operations to fold. For example, `tensor.extract` on a constant tensor of prime field kinda. In cryptography, for example, we need to hash 2^{20} ~ 2^{27} times over prime field depending on applications to compute something called merkle root. We have many such things. Here this involves 2D matrix that includes constants. We want to extract it at compile time.
But unfortunately if we hold the constants with `DenseIntElementsAttr`, then it sometimes not to be folded. So we needed to add a patch.
Yeah I also tried to create a `DenseMyIntElementsAttr` and bring a lot of code from LLVM in our codebase, but it's also not a good idea, we needed to bring more that we expected, and we came up with this idea...
https://github.com/llvm/llvm-project/pull/177740
More information about the Mlir-commits
mailing list