[Mlir-commits] [mlir] [mlir][arith] Add result pretty printing for constant vscale values (PR #83565)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Fri Mar 1 04:32:27 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir-arith

Author: Benjamin Maxwell (MacDue)

<details>
<summary>Changes</summary>

In scalable code it is very common to have constant multiples of vscale, e.g. `4 * vscale`. This updates `arith.muli` to pretty print the result name in cases like this, so `4 * vscale` would be `%c4_vscale`.

This makes reading IR dumps of scalable code a little nicer.

---
Full diff: https://github.com/llvm/llvm-project/pull/83565.diff


3 Files Affected:

- (modified) mlir/include/mlir/Dialect/Arith/IR/ArithOps.td (+3-1) 
- (modified) mlir/lib/Dialect/Arith/IR/ArithOps.cpp (+29) 
- (added) mlir/test/Dialect/Arith/vscale_constants.mlir (+14) 


``````````diff
diff --git a/mlir/include/mlir/Dialect/Arith/IR/ArithOps.td b/mlir/include/mlir/Dialect/Arith/IR/ArithOps.td
index c9df50d0395d1f..fe9f9539cdb5da 100644
--- a/mlir/include/mlir/Dialect/Arith/IR/ArithOps.td
+++ b/mlir/include/mlir/Dialect/Arith/IR/ArithOps.td
@@ -343,7 +343,9 @@ def Arith_SubIOp : Arith_IntBinaryOpWithOverflowFlags<"subi"> {
 // MulIOp
 //===----------------------------------------------------------------------===//
 
-def Arith_MulIOp : Arith_IntBinaryOpWithOverflowFlags<"muli", [Commutative]> {
+def Arith_MulIOp : Arith_IntBinaryOpWithOverflowFlags<"muli",
+  [Commutative, DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>]
+> {
   let summary = [{
     Integer multiplication operation.
   }];
diff --git a/mlir/lib/Dialect/Arith/IR/ArithOps.cpp b/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
index 0f71c19c23b654..4d39aa7efb91f7 100644
--- a/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
+++ b/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
@@ -400,6 +400,35 @@ OpFoldResult arith::MulIOp::fold(FoldAdaptor adaptor) {
       [](const APInt &a, const APInt &b) { return a * b; });
 }
 
+void arith::MulIOp::getAsmResultNames(
+    function_ref<void(Value, StringRef)> setNameFn) {
+  if (!isa<IndexType>(getType()))
+    return;
+
+  // Match vector.vscale by name to avoid depending on the vector dialect (which
+  // is a circular dependency).
+  auto isVscale = [](Operation *op) {
+    return op && op->getName().getStringRef() == "vector.vscale";
+  };
+
+  // Name `base * vscale` or `vscale * base` as `c<base_value>_vscale`.
+  IntegerAttr baseValue;
+  if (matchPattern(getLhs(), m_Constant(&baseValue)) &&
+      isVscale(getRhs().getDefiningOp())) {
+    // base * vscale
+  } else if (isVscale(getLhs().getDefiningOp()) &&
+             matchPattern(getRhs(), m_Constant(&baseValue))) {
+    // vscale * base
+  } else {
+    return;
+  }
+
+  SmallString<32> specialNameBuffer;
+  llvm::raw_svector_ostream specialName(specialNameBuffer);
+  specialName << 'c' << baseValue.getInt() << "_vscale";
+  setNameFn(getResult(), specialName.str());
+}
+
 void arith::MulIOp::getCanonicalizationPatterns(RewritePatternSet &patterns,
                                                 MLIRContext *context) {
   patterns.add<MulIMulIConstant>(context);
diff --git a/mlir/test/Dialect/Arith/vscale_constants.mlir b/mlir/test/Dialect/Arith/vscale_constants.mlir
new file mode 100644
index 00000000000000..568e20ae668455
--- /dev/null
+++ b/mlir/test/Dialect/Arith/vscale_constants.mlir
@@ -0,0 +1,14 @@
+// RUN: mlir-opt %s | FileCheck %s
+
+// Note: This test is checking value names (so deliberately is not using a regex match).
+
+func.func @test_vscale_constant_names() {
+  %0 = vector.vscale
+  %1 = arith.constant 8 : index
+  %2 = arith.constant 10 : index
+  // CHECK: %c8_vscale = arith.muli %c8, %vscale : index
+  %3 = arith.muli %1, %0 : index
+  // CHECK: %c10_vscale = arith.muli %vscale, %c10 : index
+  %4 = arith.muli %0, %2 : index
+  return
+}

``````````

</details>


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


More information about the Mlir-commits mailing list