This is an archive of the discontinued LLVM Phabricator instance.

[fir] Add fir.insert_on_range conversion
ClosedPublic

Authored by clementval on Oct 31 2021, 2:04 PM.

Details

Summary

Convert fir.insert_on_range operation to corresponding
llvm.insertvalue operations.

This patch is part of the upstreaming effort from fir-dev branch.

Diff Detail

Event Timeline

clementval created this revision.Oct 31 2021, 2:04 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 31 2021, 2:04 PM
Herald added a subscriber: mehdi_amini. · View Herald Transcript
clementval requested review of this revision.Oct 31 2021, 2:04 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 31 2021, 2:04 PM

Looks good in general, just some minor comments inline.

flang/lib/Optimizer/CodeGen/CodeGen.cpp
34

Nit: seems like it could be using llvm::all_of:

return llvm::all_of(operands, [] (Value opnd) { 
  auto *defop = opnd.getDefiningOp();
  return defop && isa<mlir::LLVM::ConstantOp, mlir::arith::ConstantOp>(defop);
});

Should this use isConstantLike? https://github.com/llvm/llvm-project/blob/main/mlir/include/mlir/IR/Matchers.h#L53-L56

return llvm::all_of(operands, [] (Value opnd) { 
  auto *defop = opnd.getDefiningOp();
  return defop && isConstantLike(defop);
});
67

If you make it a pure virtual = 0 it'll be a compile time failure.
(Could also use CRTP here)

200

The usual pattern would look like: https://github.com/llvm/llvm-project/blob/main/mlir/lib/Dialect/Linalg/Transforms/ElementwiseOpFusion.cpp#L1182-L1204

But also this function isn't used here, so I rather remove this until it's needed.

205

Same as above, unused.

227

ditto

259

Actually the entire class seems unused?

275

It is worth documenting that if the subscript is full of zeros then it means we reached the end of the array.
This "wrap around" could also be signaled by a returned LogicalResult (or a bool maybe).

281

May be worth a message && "..." in the assert on the broken invariant.

Also the InsertOnRangeOp op is defined with only two operands, how we can get more?

301

Nit: std:: is spurious here (you don't use it line 295)
(same below)

308

Please fuse this loop with the previous one and eliminate the temporary storage lowerBound / upperBound.
Unless I'm mistaken this is just an extra indirection that doesn't seem useful here.
(You can iterate on the reverse iterators)

310

auto really doesn't help here.
Which reminds me: did you follow up on https://reviews.llvm.org/D111288#inline-1072217 ?

Also this seems like another spurious copy here?
If this is just for the sake of the naming, a lighter way to do this may be auto &subscripts = lBounds;

320

rewriter.getI64Type isn't necessarily "cheap", it is quite common to get it into a variable outside of the enclosing loop and reuse it.

321

Do you need this? (In general SmallVector implicitly convert to ArrayRef)

mehdi_amini added inline comments.Oct 31 2021, 10:45 PM
flang/lib/Optimizer/CodeGen/CodeGen.cpp
59

Seem like this line is the entire justification for this class? Is this enough for it be pulling its weight?

Previously added pattern (GlobalOpConversion) started with auto tyAttr = convertType(global.getType()); ; should it inherit from this class as well or do I miss something?

clementval updated this revision to Diff 383785.Nov 1 2021, 5:44 AM
clementval marked 12 inline comments as done and an inline comment as not done.

Address review comments

flang/lib/Optimizer/CodeGen/CodeGen.cpp
34

We could use the proposed code. Just removed it now since it is not used currently after removing the assert.

59

Yeah GlobalOpConversion might inherit from this. I'll explore that and open a separate revision if needed. I would prefer to differ any refactoring (keeping or removing this class) after we have the full picture on this pass.

200

No needed now so will bring it back later.

205

Removed for now

227

Removed for now

259

Removed for now

275

When subscripts is equal to ubounds we reach the end of the array and end of the insertion.

281

Removed it since this assert does not really makes sense anymore. Looks like it was useful previously when we had more operands.

310

Somehow I missed your last comment on D111288. I'll check with the others what we will do. It seems a bit overkill to go through a RFC for this.

mehdi_amini added inline comments.Nov 1 2021, 9:37 AM
flang/lib/Optimizer/CodeGen/CodeGen.cpp
275

Right, but I think you're describing a behavior of the caller, while I'm referring to the contract of this function alone.
If we don't expect to wrap-around, then we should just add an assert (and document it).
(I shouldn't have to analyze call-site(s) to figure out the behavior of a function in isolation).

clementval added inline comments.Nov 1 2021, 9:59 AM
flang/lib/Optimizer/CodeGen/CodeGen.cpp
275

Right my bad. That's true the function can have some more description without looking how it is used. I'll update that.

clementval updated this revision to Diff 384002.Nov 2 2021, 2:03 AM

Add comment about the wrap around in incrementSubscripts.

clementval updated this revision to Diff 384005.Nov 2 2021, 2:06 AM
clementval marked 2 inline comments as done.

Replace auto for subscripts ref

clementval marked an inline comment as done.Nov 2 2021, 2:06 AM
mehdi_amini added inline comments.Nov 2 2021, 4:23 PM
flang/lib/Optimizer/CodeGen/CodeGen.cpp
216

How do we guarantee that we have a static array type? This will crash with an IR with dynamic shape.

Right now the InsertOnRangeOp is defined as taking a fir_SequenceType which isn't restricted to static shapes.

clementval marked an inline comment as done.Nov 3 2021, 3:14 AM
clementval added inline comments.
flang/lib/Optimizer/CodeGen/CodeGen.cpp
216

This is actually smth that was missing in the verifier. D113092 adds the check directly in the verifier.

mehdi_amini accepted this revision.Nov 3 2021, 11:22 AM
This revision is now accepted and ready to land.Nov 3 2021, 11:22 AM
clementval marked an inline comment as done.Nov 3 2021, 12:22 PM

Thanks for the review @mehdi_amini. I'll wait until the verifier is updated to land this.

clementval updated this revision to Diff 384685.Nov 4 2021, 2:21 AM

Fix rebase

This revision was automatically updated to reflect the committed changes.