[Mlir-commits] [mlir] 8bef354 - [mlir][index] Add boilerplate for the `index` dialect

Jeff Niu llvmlistbot at llvm.org
Fri Oct 21 09:46:24 PDT 2022


Author: Jeff Niu
Date: 2022-10-21T09:45:59-07:00
New Revision: 8bef3541fabf01fc1f11452d45737b15109b8bd6

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

LOG: [mlir][index] Add boilerplate for the `index` dialect

This patch introduces the `index` dialect and associated boilerplate for
adding ops and enums (comparison predicates).

Reviewed By: rriddle, jpienaar, nicolasvasilache

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

Added: 
    mlir/include/mlir/Dialect/Index/CMakeLists.txt
    mlir/include/mlir/Dialect/Index/IR/CMakeLists.txt
    mlir/include/mlir/Dialect/Index/IR/IndexAttrs.h
    mlir/include/mlir/Dialect/Index/IR/IndexDialect.h
    mlir/include/mlir/Dialect/Index/IR/IndexDialect.td
    mlir/include/mlir/Dialect/Index/IR/IndexEnums.td
    mlir/include/mlir/Dialect/Index/IR/IndexOps.h
    mlir/include/mlir/Dialect/Index/IR/IndexOps.td
    mlir/lib/Dialect/Index/CMakeLists.txt
    mlir/lib/Dialect/Index/IR/CMakeLists.txt
    mlir/lib/Dialect/Index/IR/IndexAttrs.cpp
    mlir/lib/Dialect/Index/IR/IndexDialect.cpp
    mlir/lib/Dialect/Index/IR/IndexOps.cpp

Modified: 
    mlir/include/mlir/Dialect/CMakeLists.txt
    mlir/include/mlir/InitAllDialects.h
    mlir/lib/Dialect/CMakeLists.txt
    mlir/test/mlir-opt/commandline.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/CMakeLists.txt b/mlir/include/mlir/Dialect/CMakeLists.txt
index 4ebdb54fcf1c3..a9843d1a1249c 100644
--- a/mlir/include/mlir/Dialect/CMakeLists.txt
+++ b/mlir/include/mlir/Dialect/CMakeLists.txt
@@ -12,6 +12,7 @@ add_subdirectory(DLTI)
 add_subdirectory(EmitC)
 add_subdirectory(Func)
 add_subdirectory(GPU)
+add_subdirectory(Index)
 add_subdirectory(LLVMIR)
 add_subdirectory(Linalg)
 add_subdirectory(MLProgram)

diff  --git a/mlir/include/mlir/Dialect/Index/CMakeLists.txt b/mlir/include/mlir/Dialect/Index/CMakeLists.txt
new file mode 100644
index 0000000000000..f33061b2d87cf
--- /dev/null
+++ b/mlir/include/mlir/Dialect/Index/CMakeLists.txt
@@ -0,0 +1 @@
+add_subdirectory(IR)

diff  --git a/mlir/include/mlir/Dialect/Index/IR/CMakeLists.txt b/mlir/include/mlir/Dialect/Index/IR/CMakeLists.txt
new file mode 100644
index 0000000000000..0337b3cd10871
--- /dev/null
+++ b/mlir/include/mlir/Dialect/Index/IR/CMakeLists.txt
@@ -0,0 +1,7 @@
+set(LLVM_TARGET_DEFINITIONS IndexEnums.td)
+mlir_tablegen(IndexEnums.h.inc -gen-enum-decls)
+mlir_tablegen(IndexEnums.cpp.inc -gen-enum-defs)
+mlir_tablegen(IndexAttrs.h.inc -gen-attrdef-decls -attrdefs-dialect=index)
+mlir_tablegen(IndexAttrs.cpp.inc -gen-attrdef-defs -attrdefs-dialect=index)
+add_mlir_dialect(IndexOps index)
+add_mlir_doc(IndexOps IndexOps Dialects/ -gen-dialect-doc)

diff  --git a/mlir/include/mlir/Dialect/Index/IR/IndexAttrs.h b/mlir/include/mlir/Dialect/Index/IR/IndexAttrs.h
new file mode 100644
index 0000000000000..25ee50dc191e5
--- /dev/null
+++ b/mlir/include/mlir/Dialect/Index/IR/IndexAttrs.h
@@ -0,0 +1,23 @@
+//===- IndexAttrs.h - Index attribute declarations ----------------*- C++-*-==//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_DIALECT_INDEX_IR_INDEXATTRS_H
+#define MLIR_DIALECT_INDEX_IR_INDEXATTRS_H
+
+#include "mlir/IR/Attributes.h"
+
+//===----------------------------------------------------------------------===//
+// ODS-Generated Declarations
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Dialect/Index/IR/IndexEnums.h.inc"
+
+#define GET_ATTRDEF_CLASSES
+#include "mlir/Dialect/Index/IR/IndexAttrs.h.inc"
+
+#endif // MLIR_DIALECT_INDEX_IR_INDEXATTRS_H

diff  --git a/mlir/include/mlir/Dialect/Index/IR/IndexDialect.h b/mlir/include/mlir/Dialect/Index/IR/IndexDialect.h
new file mode 100644
index 0000000000000..fb8aebfb240b2
--- /dev/null
+++ b/mlir/include/mlir/Dialect/Index/IR/IndexDialect.h
@@ -0,0 +1,20 @@
+//===- IndexDialect.h - Index dialect declaration -----------------*- C++-*-==//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_DIALECT_INDEX_IR_INDEXDIALECT_H
+#define MLIR_DIALECT_INDEX_IR_INDEXDIALECT_H
+
+#include "mlir/IR/Dialect.h"
+
+//===----------------------------------------------------------------------===//
+// ODS-Generated Declarations
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Dialect/Index/IR/IndexOpsDialect.h.inc"
+
+#endif // MLIR_DIALECT_INDEX_IR_INDEXDIALECT_H

diff  --git a/mlir/include/mlir/Dialect/Index/IR/IndexDialect.td b/mlir/include/mlir/Dialect/Index/IR/IndexDialect.td
new file mode 100644
index 0000000000000..0b91341eba8a2
--- /dev/null
+++ b/mlir/include/mlir/Dialect/Index/IR/IndexDialect.td
@@ -0,0 +1,85 @@
+//===- IndexDialect.td - Index dialect definition ----------*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef INDEX_DIALECT
+#define INDEX_DIALECT
+
+include "mlir/IR/DialectBase.td"
+
+//===----------------------------------------------------------------------===//
+// IndexDialect
+//===----------------------------------------------------------------------===//
+
+def IndexDialect : Dialect {
+  let name = "index";
+
+  let summary = "The Index dialect";
+  let description = [{
+    The Index dialect contains operations for manipulating values of the builtin
+    `index` type. The index type models target-specific values of pointer width,
+    like `intptr_t`. Index values are typically used as loop bounds, array
+    subscripts, tensor dimensions, etc.
+
+    The operations in this dialect operate exclusively on scalar index types.
+    The dialect and its operations treat the index type as signless and contains
+    signed and unsigned versions of certain operations where the distinction is
+    meaningful. In particular, the operations and transformations are careful to
+    be aware of the target-independent-ness of the index type, such as when
+    folding.
+
+    The folding semantics of the Index dialect operations ensure that folding
+    produces the same results irrespective of the eventual target pointer width.
+    All index constants are stored in `APInt`s of maximum index bitwidth: 64.
+    Operations are folded using 64-bit integer arithmetic.
+
+    For operations where the values of the upper 32 bits don't impact the values
+    of the lower 32 bits, no additional handling is required because if the
+    target is 32-bit, the truncated folded result will be the same as if the
+    operation were computed with 32-bit arithmetic, and if the target is 64-bit,
+    the fold result is valid by default.
+
+    Consider addition: an overflow in 32-bit is the same as truncating the
+    result computed in 64-bit. For example, `add(0x800000008, 0x800000008)` is
+    `0x1000000010` in 64-bit, which truncates to `0x10`, the same result as
+    truncating the operands first: `add(0x08, 0x08)`. Specifically, an operation
+    `f` can always be folded if it satisfies the following for all 64-bit values
+    of `a` and `b`:
+
+    ```
+    trunc(f(a, b)) = f(trunc(a), trunc(b))
+    ```
+
+    When materializing target-specific code, constants just need to be truncated
+    as appropriate.
+
+    Operations where the values of the upper 32 bits do impact the values of the
+    lower 32 bits are not folded if the results would be 
diff erent in 32-bit.
+    These are operations that right shift -- division, remainder, etc. These
+    operations are only folded for subsets of `a` and `b` for which the above
+    property is satisfied. This is checked per fold attempt.
+
+    Consider division: the 32-bit computation will 
diff er from 64-bit if the
+    latter results in a high bit shifted into the lower 32 bits. For example,
+    `div(0x100000002, 2)` is `0x80000001` in 64-bit but `0x01` in 32-bit; it
+    cannot be folded. However, `div(0x200000002, 2)` can be folded. The 64-bit
+    result is `0x100000001`, which truncated to 32 bits is `0x01`. The 32-bit
+    result of the operation with truncated operands `div(0x02, 2)` which is
+    `0x01`, the same as truncating the 64-bit result.
+  }];
+
+  let cppNamespace = "::mlir::index";
+
+  let extraClassDeclaration = [{
+    /// Register all dialect attributes.
+    void registerAttributes();
+    /// Register all dialect operations.
+    void registerOperations();
+  }];
+}
+
+#endif // INDEX_DIALECT

diff  --git a/mlir/include/mlir/Dialect/Index/IR/IndexEnums.td b/mlir/include/mlir/Dialect/Index/IR/IndexEnums.td
new file mode 100644
index 0000000000000..7f7d6cefc1db9
--- /dev/null
+++ b/mlir/include/mlir/Dialect/Index/IR/IndexEnums.td
@@ -0,0 +1,14 @@
+//===- IndexEnums.td - Index enum definitions --------------*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef INDEX_ENUMS
+#define INDEX_ENUMS
+
+include "mlir/IR/EnumAttr.td"
+
+#endif // INDEX_ENUMS

diff  --git a/mlir/include/mlir/Dialect/Index/IR/IndexOps.h b/mlir/include/mlir/Dialect/Index/IR/IndexOps.h
new file mode 100644
index 0000000000000..8c42472866e83
--- /dev/null
+++ b/mlir/include/mlir/Dialect/Index/IR/IndexOps.h
@@ -0,0 +1,19 @@
+//===- IndexOps.h - Index operation declarations ------------------*- C++-*-==//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_DIALECT_INDEX_IR_INDEXOPS_H
+#define MLIR_DIALECT_INDEX_IR_INDEXOPS_H
+
+//===----------------------------------------------------------------------===//
+// ODS-Generated Declarations
+//===----------------------------------------------------------------------===//
+
+#define GET_OP_CLASSES
+#include "mlir/Dialect/Index/IR/IndexOps.h.inc"
+
+#endif // MLIR_DIALECT_INDEX_IR_INDEXOPS_H

diff  --git a/mlir/include/mlir/Dialect/Index/IR/IndexOps.td b/mlir/include/mlir/Dialect/Index/IR/IndexOps.td
new file mode 100644
index 0000000000000..5d5066208cece
--- /dev/null
+++ b/mlir/include/mlir/Dialect/Index/IR/IndexOps.td
@@ -0,0 +1,24 @@
+//===- IndexOps.td - Index operation definitions -----------*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef INDEX_OPS
+#define INDEX_OPS
+
+include "mlir/Dialect/Index/IR/IndexDialect.td"
+include "mlir/Interfaces/SideEffectInterfaces.td"
+include "mlir/IR/OpBase.td"
+
+//===----------------------------------------------------------------------===//
+// IndexOp
+//===----------------------------------------------------------------------===//
+
+/// Base class for Index dialect operations.
+class IndexOp<string mnemonic, list<Trait> traits = []>
+    : Op<IndexDialect, mnemonic, [NoSideEffect] # traits>;
+
+#endif // INDEX_OPS

diff  --git a/mlir/include/mlir/InitAllDialects.h b/mlir/include/mlir/InitAllDialects.h
index 52b9ba4507158..c82a6ac2f3f75 100644
--- a/mlir/include/mlir/InitAllDialects.h
+++ b/mlir/include/mlir/InitAllDialects.h
@@ -32,6 +32,7 @@
 #include "mlir/Dialect/Func/IR/FuncOps.h"
 #include "mlir/Dialect/GPU/IR/GPUDialect.h"
 #include "mlir/Dialect/GPU/TransformOps/GPUTransformOps.h"
+#include "mlir/Dialect/Index/IR/IndexDialect.h"
 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
 #include "mlir/Dialect/LLVMIR/NVVMDialect.h"
 #include "mlir/Dialect/LLVMIR/ROCDLDialect.h"
@@ -87,6 +88,7 @@ inline void registerAllDialects(DialectRegistry &registry) {
                   emitc::EmitCDialect,
                   func::FuncDialect,
                   gpu::GPUDialect,
+                  index::IndexDialect,
                   LLVM::LLVMDialect,
                   linalg::LinalgDialect,
                   math::MathDialect,

diff  --git a/mlir/lib/Dialect/CMakeLists.txt b/mlir/lib/Dialect/CMakeLists.txt
index 9c8dccd8bcf40..5cf5c125a7cb4 100644
--- a/mlir/lib/Dialect/CMakeLists.txt
+++ b/mlir/lib/Dialect/CMakeLists.txt
@@ -12,6 +12,7 @@ add_subdirectory(DLTI)
 add_subdirectory(EmitC)
 add_subdirectory(Func)
 add_subdirectory(GPU)
+add_subdirectory(Index)
 add_subdirectory(Linalg)
 add_subdirectory(LLVMIR)
 add_subdirectory(Math)

diff  --git a/mlir/lib/Dialect/Index/CMakeLists.txt b/mlir/lib/Dialect/Index/CMakeLists.txt
new file mode 100644
index 0000000000000..f33061b2d87cf
--- /dev/null
+++ b/mlir/lib/Dialect/Index/CMakeLists.txt
@@ -0,0 +1 @@
+add_subdirectory(IR)

diff  --git a/mlir/lib/Dialect/Index/IR/CMakeLists.txt b/mlir/lib/Dialect/Index/IR/CMakeLists.txt
new file mode 100644
index 0000000000000..8b5b26028a898
--- /dev/null
+++ b/mlir/lib/Dialect/Index/IR/CMakeLists.txt
@@ -0,0 +1,12 @@
+add_mlir_dialect_library(MLIRIndexDialect
+  IndexAttrs.cpp
+  IndexDialect.cpp
+  IndexOps.cpp
+
+  DEPENDS
+  MLIRIndexOpsIncGen
+
+  LINK_LIBS PUBLIC
+  MLIRDialect
+  MLIRIR
+  )

diff  --git a/mlir/lib/Dialect/Index/IR/IndexAttrs.cpp b/mlir/lib/Dialect/Index/IR/IndexAttrs.cpp
new file mode 100644
index 0000000000000..9b6dfef11bdef
--- /dev/null
+++ b/mlir/lib/Dialect/Index/IR/IndexAttrs.cpp
@@ -0,0 +1,33 @@
+//===- IndexAttrs.cpp - Index attribute definitions ------------------------==//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Dialect/Index/IR/IndexAttrs.h"
+#include "mlir/Dialect/Index/IR/IndexDialect.h"
+
+using namespace mlir;
+using namespace mlir::index;
+
+//===----------------------------------------------------------------------===//
+// IndexDialect
+//===----------------------------------------------------------------------===//
+
+void IndexDialect::registerAttributes() {
+  addAttributes<
+#define GET_ATTRDEF_LIST
+#include "mlir/Dialect/Index/IR/IndexAttrs.cpp.inc"
+      >();
+}
+
+//===----------------------------------------------------------------------===//
+// ODS-Generated Declarations
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Dialect/Index/IR/IndexEnums.cpp.inc"
+
+#define GET_ATTRDEF_CLASSES
+#include "mlir/Dialect/Index/IR/IndexAttrs.cpp.inc"

diff  --git a/mlir/lib/Dialect/Index/IR/IndexDialect.cpp b/mlir/lib/Dialect/Index/IR/IndexDialect.cpp
new file mode 100644
index 0000000000000..130157f357818
--- /dev/null
+++ b/mlir/lib/Dialect/Index/IR/IndexDialect.cpp
@@ -0,0 +1,27 @@
+//===- IndexDialect.cpp - Index dialect definition -------------------------==//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Dialect/Index/IR/IndexDialect.h"
+
+using namespace mlir;
+using namespace mlir::index;
+
+//===----------------------------------------------------------------------===//
+// IndexDialect
+//===----------------------------------------------------------------------===//
+
+void IndexDialect::initialize() {
+  registerAttributes();
+  registerOperations();
+}
+
+//===----------------------------------------------------------------------===//
+// ODS-Generated Definitions
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Dialect/Index/IR/IndexOpsDialect.cpp.inc"

diff  --git a/mlir/lib/Dialect/Index/IR/IndexOps.cpp b/mlir/lib/Dialect/Index/IR/IndexOps.cpp
new file mode 100644
index 0000000000000..d4683910bed10
--- /dev/null
+++ b/mlir/lib/Dialect/Index/IR/IndexOps.cpp
@@ -0,0 +1,31 @@
+//===- IndexOps.cpp - Index operation definitions --------------------------==//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Dialect/Index/IR/IndexOps.h"
+#include "mlir/Dialect/Index/IR/IndexDialect.h"
+
+using namespace mlir;
+using namespace mlir::index;
+
+//===----------------------------------------------------------------------===//
+// IndexDialect
+//===----------------------------------------------------------------------===//
+
+void IndexDialect::registerOperations() {
+  addOperations<
+#define GET_OP_LIST
+#include "mlir/Dialect/Index/IR/IndexOps.cpp.inc"
+      >();
+}
+
+//===----------------------------------------------------------------------===//
+// ODS-Generated Definitions
+//===----------------------------------------------------------------------===//
+
+#define GET_OP_CLASSES
+#include "mlir/Dialect/Index/IR/IndexOps.cpp.inc"

diff  --git a/mlir/test/mlir-opt/commandline.mlir b/mlir/test/mlir-opt/commandline.mlir
index 536e7e9f97837..1badf905d28de 100644
--- a/mlir/test/mlir-opt/commandline.mlir
+++ b/mlir/test/mlir-opt/commandline.mlir
@@ -16,6 +16,7 @@
 // CHECK-NEXT: emitc
 // CHECK-NEXT: func
 // CHECK-NEXT: gpu
+// CHECK-NEXT: index
 // CHECK-NEXT: linalg
 // CHECK-NEXT: llvm
 // CHECK-NEXT: math


        


More information about the Mlir-commits mailing list