[Mlir-commits] [mlir] 005b720 - [NFC][mlir] Adding some helpful EDSC intrinsics

Alex Zinenko llvmlistbot at llvm.org
Thu Feb 13 00:21:46 PST 2020


Author: Kern Handa
Date: 2020-02-13T09:21:17+01:00
New Revision: 005b720373f163b153957d35db24722b8f72ad9b

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

LOG: [NFC][mlir] Adding some helpful EDSC intrinsics

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

Added: 
    

Modified: 
    mlir/include/mlir/Dialect/StandardOps/EDSC/Intrinsics.h
    mlir/include/mlir/EDSC/Builders.h
    mlir/include/mlir/EDSC/Intrinsics.h
    mlir/lib/Dialect/StandardOps/EDSC/Intrinsics.cpp
    mlir/test/EDSC/builder-api-test.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/StandardOps/EDSC/Intrinsics.h b/mlir/include/mlir/Dialect/StandardOps/EDSC/Intrinsics.h
index 1c73cb5aa628..6735cf7b5fcc 100644
--- a/mlir/include/mlir/Dialect/StandardOps/EDSC/Intrinsics.h
+++ b/mlir/include/mlir/Dialect/StandardOps/EDSC/Intrinsics.h
@@ -52,11 +52,14 @@ struct ValueBuilder : public ValueHandle {
 using std_addf = ValueBuilder<AddFOp>;
 using std_alloc = ValueBuilder<AllocOp>;
 using std_call = OperationBuilder<CallOp>;
+using std_constant = ValueBuilder<ConstantOp>;
 using std_constant_float = ValueBuilder<ConstantFloatOp>;
 using std_constant_index = ValueBuilder<ConstantIndexOp>;
 using std_constant_int = ValueBuilder<ConstantIntOp>;
 using std_dealloc = OperationBuilder<DeallocOp>;
 using std_dim = ValueBuilder<DimOp>;
+using std_extract_element = ValueBuilder<ExtractElementOp>;
+using std_index_cast = ValueBuilder<IndexCastOp>;
 using std_muli = ValueBuilder<MulIOp>;
 using std_mulf = ValueBuilder<MulFOp>;
 using std_memref_cast = ValueBuilder<MemRefCastOp>;
@@ -65,7 +68,10 @@ using std_select = ValueBuilder<SelectOp>;
 using std_load = ValueBuilder<LoadOp>;
 using std_store = OperationBuilder<StoreOp>;
 using std_subi = ValueBuilder<SubIOp>;
+using std_sub_view = ValueBuilder<SubViewOp>;
 using std_tanh = ValueBuilder<TanhOp>;
+using std_tensor_load = ValueBuilder<TensorLoadOp>;
+using std_tensor_store = OperationBuilder<TensorStoreOp>;
 using std_view = ValueBuilder<ViewOp>;
 using std_zero_extendi = ValueBuilder<ZeroExtendIOp>;
 using std_sign_extendi = ValueBuilder<SignExtendIOp>;
@@ -74,7 +80,7 @@ using std_sign_extendi = ValueBuilder<SignExtendIOp>;
 ///
 /// Prerequisites:
 ///   All Handles have already captured previously constructed IR objects.
-OperationHandle br(BlockHandle bh, ArrayRef<ValueHandle> operands);
+OperationHandle std_br(BlockHandle bh, ArrayRef<ValueHandle> operands);
 
 /// Creates a new mlir::Block* and branches to it from the current block.
 /// Argument types are specified by `operands`.
@@ -89,8 +95,8 @@ OperationHandle br(BlockHandle bh, ArrayRef<ValueHandle> operands);
 ///   All `operands` have already captured an mlir::Value
 ///   captures.size() == operands.size()
 ///   captures and operands are pairwise of the same type.
-OperationHandle br(BlockHandle *bh, ArrayRef<ValueHandle *> captures,
-                   ArrayRef<ValueHandle> operands);
+OperationHandle std_br(BlockHandle *bh, ArrayRef<ValueHandle *> captures,
+                       ArrayRef<ValueHandle> operands);
 
 /// Branches into the mlir::Block* captured by BlockHandle `trueBranch` with
 /// `trueOperands` if `cond` evaluates to `true` (resp. `falseBranch` and
@@ -98,10 +104,10 @@ OperationHandle br(BlockHandle *bh, ArrayRef<ValueHandle *> captures,
 ///
 /// Prerequisites:
 ///   All Handles have captured previously constructed IR objects.
-OperationHandle cond_br(ValueHandle cond, BlockHandle trueBranch,
-                        ArrayRef<ValueHandle> trueOperands,
-                        BlockHandle falseBranch,
-                        ArrayRef<ValueHandle> falseOperands);
+OperationHandle std_cond_br(ValueHandle cond, BlockHandle trueBranch,
+                            ArrayRef<ValueHandle> trueOperands,
+                            BlockHandle falseBranch,
+                            ArrayRef<ValueHandle> falseOperands);
 
 /// Eagerly creates new mlir::Block* with argument types specified by
 /// `trueOperands`/`falseOperands`.
@@ -119,12 +125,12 @@ OperationHandle cond_br(ValueHandle cond, BlockHandle trueBranch,
 ///   `falseCaptures`.size() == `falseOperands`.size()
 ///   `trueCaptures` and `trueOperands` are pairwise of the same type
 ///   `falseCaptures` and `falseOperands` are pairwise of the same type.
-OperationHandle cond_br(ValueHandle cond, BlockHandle *trueBranch,
-                        ArrayRef<ValueHandle *> trueCaptures,
-                        ArrayRef<ValueHandle> trueOperands,
-                        BlockHandle *falseBranch,
-                        ArrayRef<ValueHandle *> falseCaptures,
-                        ArrayRef<ValueHandle> falseOperands);
+OperationHandle std_cond_br(ValueHandle cond, BlockHandle *trueBranch,
+                            ArrayRef<ValueHandle *> trueCaptures,
+                            ArrayRef<ValueHandle> trueOperands,
+                            BlockHandle *falseBranch,
+                            ArrayRef<ValueHandle *> falseCaptures,
+                            ArrayRef<ValueHandle> falseOperands);
 
 /// Provide an index notation around sdt_load and std_store.
 using StdIndexedValue =

diff  --git a/mlir/include/mlir/EDSC/Builders.h b/mlir/include/mlir/EDSC/Builders.h
index 955b6c81cc66..5309187d1714 100644
--- a/mlir/include/mlir/EDSC/Builders.h
+++ b/mlir/include/mlir/EDSC/Builders.h
@@ -340,7 +340,8 @@ struct OperationHandle : public CapturableHandle {
   /// of MLIR without duplicating the type system or the op definitions.
   template <typename Op, typename... Args>
   static OperationHandle create(Args... args);
-  template <typename Op, typename... Args> static Op createOp(Args... args);
+  template <typename Op, typename... Args>
+  static Op createOp(Args... args);
 
   /// Generic create for a named operation.
   static OperationHandle create(StringRef name, ArrayRef<ValueHandle> operands,
@@ -355,7 +356,8 @@ struct OperationHandle : public CapturableHandle {
 };
 
 /// Simple wrapper to build a generic operation without successor blocks.
-template <typename HandleType> struct CustomOperation {
+template <typename HandleType>
+struct CustomOperation {
   CustomOperation(StringRef name) : name(name) {
     static_assert(std::is_same<HandleType, ValueHandle>() ||
                       std::is_same<HandleType, OperationHandle>(),
@@ -490,7 +492,8 @@ inline SmallVector<ValueHandle, 8> makeValueHandles(Container values) {
 /// Store parameters. Assigning to an IndexedValue emits an actual `Store`
 /// operation, while converting an IndexedValue to a ValueHandle emits an actual
 /// `Load` operation.
-template <typename Load, typename Store> class TemplatedIndexedValue {
+template <typename Load, typename Store>
+class TemplatedIndexedValue {
 public:
   explicit TemplatedIndexedValue(Type t) : base(t) {}
   explicit TemplatedIndexedValue(Value v)
@@ -529,7 +532,7 @@ template <typename Load, typename Store> class TemplatedIndexedValue {
   }
 
   /// Emits a `load` when converting to a Value.
-  Value operator*(void) const {
+  Value operator*(void)const {
     return Load(getBase(), {indices.begin(), indices.end()}).getValue();
   }
 

diff  --git a/mlir/include/mlir/EDSC/Intrinsics.h b/mlir/include/mlir/EDSC/Intrinsics.h
index 3a8fc6d53468..3e82eddc39c1 100644
--- a/mlir/include/mlir/EDSC/Intrinsics.h
+++ b/mlir/include/mlir/EDSC/Intrinsics.h
@@ -55,7 +55,10 @@ class ValueHandleArray {
   SmallVector<Value, 8> values;
 };
 
-template <typename T> inline T unpack(T value) { return value; }
+template <typename T>
+inline T unpack(T value) {
+  return value;
+}
 
 inline detail::ValueHandleArray unpack(ArrayRef<ValueHandle> values) {
   return detail::ValueHandleArray(values);
@@ -70,7 +73,8 @@ inline detail::ValueHandleArray unpack(ArrayRef<ValueHandle> values) {
 /// Implementing it as a subclass allows it to compose all the way to Value.
 /// Without subclassing, implicit conversion to Value would fail when composing
 /// in patterns such as: `select(a, b, select(c, d, e))`.
-template <typename Op> struct ValueBuilder : public ValueHandle {
+template <typename Op>
+struct ValueBuilder : public ValueHandle {
   // Builder-based
   template <typename... Args>
   ValueBuilder(Args... args)
@@ -94,7 +98,8 @@ template <typename Op> struct ValueBuilder : public ValueHandle {
   ValueBuilder() : ValueHandle(ValueHandle::create<Op>()) {}
 };
 
-template <typename Op> struct OperationBuilder : public OperationHandle {
+template <typename Op>
+struct OperationBuilder : public OperationHandle {
   template <typename... Args>
   OperationBuilder(Args... args)
       : OperationHandle(OperationHandle::create<Op>(detail::unpack(args)...)) {}

diff  --git a/mlir/lib/Dialect/StandardOps/EDSC/Intrinsics.cpp b/mlir/lib/Dialect/StandardOps/EDSC/Intrinsics.cpp
index 466ee049515f..f2e2974d76dd 100644
--- a/mlir/lib/Dialect/StandardOps/EDSC/Intrinsics.cpp
+++ b/mlir/lib/Dialect/StandardOps/EDSC/Intrinsics.cpp
@@ -12,8 +12,8 @@
 using namespace mlir;
 using namespace mlir::edsc;
 
-OperationHandle mlir::edsc::intrinsics::br(BlockHandle bh,
-                                           ArrayRef<ValueHandle> operands) {
+OperationHandle mlir::edsc::intrinsics::std_br(BlockHandle bh,
+                                               ArrayRef<ValueHandle> operands) {
   assert(bh && "Expected already captured BlockHandle");
   for (auto &o : operands) {
     (void)o;
@@ -22,6 +22,7 @@ OperationHandle mlir::edsc::intrinsics::br(BlockHandle bh,
   SmallVector<Value, 4> ops(operands.begin(), operands.end());
   return OperationHandle::create<BranchOp>(bh.getBlock(), ops);
 }
+
 static void enforceEmptyCapturesMatchOperands(ArrayRef<ValueHandle *> captures,
                                               ArrayRef<ValueHandle> operands) {
   assert(captures.size() == operands.size() &&
@@ -36,9 +37,9 @@ static void enforceEmptyCapturesMatchOperands(ArrayRef<ValueHandle *> captures,
   }
 }
 
-OperationHandle mlir::edsc::intrinsics::br(BlockHandle *bh,
-                                           ArrayRef<ValueHandle *> captures,
-                                           ArrayRef<ValueHandle> operands) {
+OperationHandle mlir::edsc::intrinsics::std_br(BlockHandle *bh,
+                                               ArrayRef<ValueHandle *> captures,
+                                               ArrayRef<ValueHandle> operands) {
   assert(!*bh && "Unexpected already captured BlockHandle");
   enforceEmptyCapturesMatchOperands(captures, operands);
   BlockBuilder(bh, captures)(/* no body */);
@@ -47,17 +48,17 @@ OperationHandle mlir::edsc::intrinsics::br(BlockHandle *bh,
 }
 
 OperationHandle
-mlir::edsc::intrinsics::cond_br(ValueHandle cond, BlockHandle trueBranch,
-                                ArrayRef<ValueHandle> trueOperands,
-                                BlockHandle falseBranch,
-                                ArrayRef<ValueHandle> falseOperands) {
+mlir::edsc::intrinsics::std_cond_br(ValueHandle cond, BlockHandle trueBranch,
+                                    ArrayRef<ValueHandle> trueOperands,
+                                    BlockHandle falseBranch,
+                                    ArrayRef<ValueHandle> falseOperands) {
   SmallVector<Value, 4> trueOps(trueOperands.begin(), trueOperands.end());
   SmallVector<Value, 4> falseOps(falseOperands.begin(), falseOperands.end());
   return OperationHandle::create<CondBranchOp>(
       cond, trueBranch.getBlock(), trueOps, falseBranch.getBlock(), falseOps);
 }
 
-OperationHandle mlir::edsc::intrinsics::cond_br(
+OperationHandle mlir::edsc::intrinsics::std_cond_br(
     ValueHandle cond, BlockHandle *trueBranch,
     ArrayRef<ValueHandle *> trueCaptures, ArrayRef<ValueHandle> trueOperands,
     BlockHandle *falseBranch, ArrayRef<ValueHandle *> falseCaptures,

diff  --git a/mlir/test/EDSC/builder-api-test.cpp b/mlir/test/EDSC/builder-api-test.cpp
index ea9dde9f8906..ed42278c6410 100644
--- a/mlir/test/EDSC/builder-api-test.cpp
+++ b/mlir/test/EDSC/builder-api-test.cpp
@@ -192,16 +192,16 @@ TEST_FUNC(builder_blocks) {
       // b2 has not yet been constructed, need to come back later.
       // This is a byproduct of non-structured control-flow.
   );
-  BlockBuilder(&b2, {&arg3, &arg4})([&] { br(b1, {arg3, arg4}); });
+  BlockBuilder(&b2, {&arg3, &arg4})([&] { std_br(b1, {arg3, arg4}); });
   // The insertion point within the toplevel function is now past b2, we will
   // need to get back the entry block.
   // This is what happens with unstructured control-flow..
   BlockBuilder(b1, Append())([&] {
     r = arg1 + arg2;
-    br(b2, {arg1, r});
+    std_br(b2, {arg1, r});
   });
   // Get back to entry block and add a branch into b1
-  BlockBuilder(functionBlock, Append())([&] { br(b1, {c1, c2}); });
+  BlockBuilder(functionBlock, Append())([&] { std_br(b1, {c1, c2}); });
 
   // clang-format off
   // CHECK-LABEL: @builder_blocks
@@ -234,15 +234,15 @@ TEST_FUNC(builder_blocks_eager) {
   BlockHandle b1, b2;
   { // Toplevel function scope.
     // Build a new block for b1 eagerly.
-    br(&b1, {&arg1, &arg2}, {c1, c2});
+    std_br(&b1, {&arg1, &arg2}, {c1, c2});
     // Construct a new block b2 explicitly with a branch into b1.
     BlockBuilder(&b2, {&arg3, &arg4})([&]{
-        br(b1, {arg3, arg4});
+        std_br(b1, {arg3, arg4});
     });
     /// And come back to append into b1 once b2 exists.
     BlockBuilder(b1, Append())([&]{
         r = arg1 + arg2;
-        br(b2, {arg1, r});
+        std_br(b2, {arg1, r});
     });
   }
 
@@ -278,7 +278,7 @@ TEST_FUNC(builder_cond_branch) {
   BlockBuilder(&b2, {&arg2, &arg3})([&] { std_ret(); });
   // Get back to entry block and add a conditional branch
   BlockBuilder(functionBlock, Append())([&] {
-    cond_br(funcArg, b1, {c32}, b2, {c64, c42});
+    std_cond_br(funcArg, b1, {c32}, b2, {c64, c42});
   });
 
   // clang-format off
@@ -311,7 +311,7 @@ TEST_FUNC(builder_cond_branch_eager) {
 
   // clang-format off
   BlockHandle b1, b2;
-  cond_br(funcArg, &b1, {&arg1}, {c32}, &b2, {&arg2, &arg3}, {c64, c42});
+  std_cond_br(funcArg, &b1, {&arg1}, {c32}, &b2, {&arg2, &arg3}, {c64, c42});
   BlockBuilder(b1, Append())([]{
       std_ret();
   });


        


More information about the Mlir-commits mailing list