[Mlir-commits] [mlir] ac9f88e - [mlir][Index] Add pretty result names for ConstantOp and BoolConstantOp

River Riddle llvmlistbot at llvm.org
Fri Dec 2 01:34:48 PST 2022


Author: River Riddle
Date: 2022-12-02T01:34:29-08:00
New Revision: ac9f88e8622629c43c8f0538d03ae93cd25b3f34

URL: https://github.com/llvm/llvm-project/commit/ac9f88e8622629c43c8f0538d03ae93cd25b3f34
DIFF: https://github.com/llvm/llvm-project/commit/ac9f88e8622629c43c8f0538d03ae93cd25b3f34.diff

LOG: [mlir][Index] Add pretty result names for ConstantOp and BoolConstantOp

ConstantOp uses `%idx<value>` and BoolConstantOp uses true/false, which
is similar to the printing for arith::ConstantOp.

Differential Revision: https://reviews.llvm.org/D139175

Added: 
    

Modified: 
    mlir/include/mlir/Dialect/Index/IR/IndexOps.h
    mlir/include/mlir/Dialect/Index/IR/IndexOps.td
    mlir/lib/Dialect/Index/IR/IndexOps.cpp
    mlir/test/Dialect/Index/index-ops.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/Index/IR/IndexOps.h b/mlir/include/mlir/Dialect/Index/IR/IndexOps.h
index 461b63ed0e56f..85a0549edd4dd 100644
--- a/mlir/include/mlir/Dialect/Index/IR/IndexOps.h
+++ b/mlir/include/mlir/Dialect/Index/IR/IndexOps.h
@@ -11,6 +11,7 @@
 
 #include "mlir/IR/BuiltinTypes.h"
 #include "mlir/IR/OpDefinition.h"
+#include "mlir/IR/OpImplementation.h"
 #include "mlir/Interfaces/CastInterfaces.h"
 #include "mlir/Interfaces/InferTypeOpInterface.h"
 #include "mlir/Interfaces/SideEffectInterfaces.h"

diff  --git a/mlir/include/mlir/Dialect/Index/IR/IndexOps.td b/mlir/include/mlir/Dialect/Index/IR/IndexOps.td
index 233b7521ca179..9bed038a56c17 100644
--- a/mlir/include/mlir/Dialect/Index/IR/IndexOps.td
+++ b/mlir/include/mlir/Dialect/Index/IR/IndexOps.td
@@ -14,6 +14,7 @@ include "mlir/Dialect/Index/IR/IndexEnums.td"
 include "mlir/Interfaces/CastInterfaces.td"
 include "mlir/Interfaces/InferTypeOpInterface.td"
 include "mlir/Interfaces/SideEffectInterfaces.td"
+include "mlir/IR/OpAsmInterface.td"
 include "mlir/IR/OpBase.td"
 
 //===----------------------------------------------------------------------===//
@@ -528,7 +529,10 @@ def Index_SizeOfOp : IndexOp<"sizeof"> {
 // ConstantOp
 //===----------------------------------------------------------------------===//
 
-def Index_ConstantOp : IndexOp<"constant", [ConstantLike]> {
+def Index_ConstantOp : IndexOp<"constant", [
+    ConstantLike,
+    DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>
+  ]> {
   let summary = "index constant";
   let description = [{
     The `index.constant` operation produces an index-typed SSA value equal to
@@ -553,7 +557,10 @@ def Index_ConstantOp : IndexOp<"constant", [ConstantLike]> {
 // BoolConstantOp
 //===----------------------------------------------------------------------===//
 
-def Index_BoolConstantOp : IndexOp<"bool.constant", [ConstantLike]> {
+def Index_BoolConstantOp : IndexOp<"bool.constant", [
+    ConstantLike,
+    DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>
+  ]> {
   let summary = "boolean constant";
   let description = [{
     The `index.bool.constant` operation produces an bool-typed SSA value equal

diff  --git a/mlir/lib/Dialect/Index/IR/IndexOps.cpp b/mlir/lib/Dialect/Index/IR/IndexOps.cpp
index 00e31bcb591f5..95148ca829678 100644
--- a/mlir/lib/Dialect/Index/IR/IndexOps.cpp
+++ b/mlir/lib/Dialect/Index/IR/IndexOps.cpp
@@ -11,6 +11,7 @@
 #include "mlir/Dialect/Index/IR/IndexDialect.h"
 #include "mlir/IR/Builders.h"
 #include "mlir/IR/OpImplementation.h"
+#include "llvm/ADT/SmallString.h"
 
 using namespace mlir;
 using namespace mlir::index;
@@ -424,6 +425,14 @@ OpFoldResult CmpOp::fold(ArrayRef<Attribute> operands) {
 // ConstantOp
 //===----------------------------------------------------------------------===//
 
+void ConstantOp::getAsmResultNames(
+    function_ref<void(Value, StringRef)> setNameFn) {
+  SmallString<32> specialNameBuffer;
+  llvm::raw_svector_ostream specialName(specialNameBuffer);
+  specialName << "idx" << getValueAttr().getValue();
+  setNameFn(getResult(), specialName.str());
+}
+
 OpFoldResult ConstantOp::fold(ArrayRef<Attribute> operands) {
   return getValueAttr();
 }
@@ -440,6 +449,11 @@ OpFoldResult BoolConstantOp::fold(ArrayRef<Attribute> operands) {
   return getValueAttr();
 }
 
+void BoolConstantOp::getAsmResultNames(
+    function_ref<void(Value, StringRef)> setNameFn) {
+  setNameFn(getResult(), getValue() ? "true" : "false");
+}
+
 //===----------------------------------------------------------------------===//
 // ODS-Generated Definitions
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/test/Dialect/Index/index-ops.mlir b/mlir/test/Dialect/Index/index-ops.mlir
index 5fa04983c8c8d..e79686b8cfe11 100644
--- a/mlir/test/Dialect/Index/index-ops.mlir
+++ b/mlir/test/Dialect/Index/index-ops.mlir
@@ -77,20 +77,20 @@ func.func @sizeof_op() {
 
 // CHECK-LABEL: @constant_op
 func.func @constant_op() {
-  // CHECK-NEXT: index.constant 0
+  // CHECK-NEXT: %idx0 = index.constant 0
   %0 = index.constant 0
-  // CHECK-NEXT: index.constant 1
+  // CHECK-NEXT: %idx1 = index.constant 1
   %1 = index.constant 1
-  // CHECK-NEXT: index.constant 42
+  // CHECK-NEXT: %idx42 = index.constant 42
   %2 = index.constant 42
   return
 }
 
 // CHECK-LABEL: @bool_constant_op
 func.func @bool_constant_op() {
-  // CHECK-NEXT: index.bool.constant true
+  // CHECK-NEXT: %true = index.bool.constant true
   %0 = index.bool.constant true
-  // CHECK-NEXT: index.bool.constant false
+  // CHECK-NEXT: %false = index.bool.constant false
   %1 = index.bool.constant false
   return
 }


        


More information about the Mlir-commits mailing list