This is an archive of the discontinued LLVM Phabricator instance.

[mlir] [EDSC] Add interface for yield-for loops.
ClosedPublic

Authored by poechsel on Apr 14 2020, 3:15 AM.

Details

Summary

ModelBuilder was missing an api to easily generate yield-for-loops.
This diffs implements an interface allowing to write:

%2:2 = loop.for %i = %start to %end step %step iter_args(%arg0 = %init0, %arg1 = %init1) -> (f32, f32) {
  %sum = addf %arg0, %arg1 : f32
  loop.yield %arg1, %sum : f32, f32
}
%3 = addf %2#0, %2#1 : f32

as

auto results =
    LoopNestBuilder(&i, start, end, step, {&arg0, &arg1},  {init0, init1})([&] {
      auto sum = arg0 + arg1;
      loop_yield(ArrayRef<ValueHandle>{arg1, sum});
    });

// Add the two values accumulated by the yield-for-loop:
ValueHandle(results[0]) + ValueHandle(results[1]);

Diff Detail

Event Timeline

poechsel created this revision.Apr 14 2020, 3:15 AM
poechsel retitled this revision from [mlir] [EDSC] Add interface for yield-for loops. to [mlir] [EDSC] Add interface for yield-for loops..Apr 14 2020, 3:20 AM
poechsel edited the summary of this revision. (Show Details)
poechsel updated this revision to Diff 257266.Apr 14 2020, 3:35 AM

Renames 'arguments' to 'iter_args_init_values' and 'argument_handles' to 'iter_args_handles'.
Swapped position of the arguments 'arguments' and 'argument_handles' to mimic the position in mlir.

poechsel edited the summary of this revision. (Show Details)Apr 14 2020, 3:36 AM
ftynse requested changes to this revision.Apr 14 2020, 4:14 AM
ftynse added a subscriber: ftynse.

I have two concerns:

  • this does not seem to work with builders that have the insertion point in the middle of a block;
  • it is unclear if this is intended to work for loop _nests_.
mlir/include/mlir/Dialect/LoopOps/EDSC/Builders.h
38

Nit: ValueRange instead of ArrayRef<Value> unless you have a strong reason to do otherwise.

59

Nit: no need to prefix with edsc:: if you are already inside the edsc namespace

mlir/include/mlir/Dialect/LoopOps/EDSC/Intrinsics.h
2

Spurious leading // makes you have ////

mlir/lib/Dialect/LoopOps/EDSC/Builders.cpp
63

I don't understand this assert, and the message does not really help. Do you only support building one loop with iter args? If so, have you considered modifying LoopBuilder instead of LoopNestBuilder ?

77

While it was the last to be inserted, nothing guarantees that it's the last operation in the block. The insertion point may have been pointing in the middle of the block. I would suggest to just store the results somewhere when you create the loop operation and return them here....

78

Nit: let's add some camelBack here: forLoopOp

111

Nit: size_t i = 0, e = iter_args_handles.size(); i < e; ++i to avoid calling size on every iteration. This idiom is extremely common in LLVM.

This revision now requires changes to proceed.Apr 14 2020, 4:14 AM
mlir/lib/Dialect/LoopOps/EDSC/Builders.cpp
63

+1, you already modify the static makeLoopBuilder method inside LoopBuilder.
It seems LoopBuilder is the natural thing to extend and we can revisit LoopNestBuilder later if/when necessary.

77

Does this just go away if we focus on just LoopBuilder?

poechsel updated this revision to Diff 257363.Apr 14 2020, 8:56 AM

Rebase, address comments.

poechsel updated this revision to Diff 257368.Apr 14 2020, 9:07 AM
poechsel marked 5 inline comments as done.

Address the for loop comment.

ftynse added inline comments.Apr 14 2020, 10:02 AM
mlir/include/mlir/Dialect/LoopOps/EDSC/Intrinsics.h
2

Ehm, we need the inverse of the change you did. We only use two slashes //, not four ///.

poechsel updated this revision to Diff 257719.Apr 15 2020, 8:00 AM

Simplified the implementation of LoopNestBuilder::operator().

poechsel marked 2 inline comments as done.Apr 15 2020, 8:06 AM
poechsel updated this revision to Diff 257723.Apr 15 2020, 8:06 AM

// -> in intrinsics.h

nicolasvasilache accepted this revision.Apr 15 2020, 8:15 AM

Looks good, thanks Pierre!

ftynse accepted this revision.Apr 15 2020, 9:30 AM
This revision is now accepted and ready to land.Apr 15 2020, 9:30 AM
This revision was automatically updated to reflect the committed changes.