This is an archive of the discontinued LLVM Phabricator instance.

Rework LLVM Dialect LoopOptions attribute
ClosedPublic

Authored by mehdi_amini on Mar 5 2021, 10:39 PM.

Details

Summary

Instead of storing an array of LoopOpt attributes, which were just
wrapping std::pair<enum, int> anyway, we can have an attribute storing
a sorted ArrayRef<std::pair<enum, int>> as a single unit. This improves
here the textual format and the general API. Note that we're limiting
the options to fit into an int64_t by design, but this isn't a new
constraint.

Building the LoopOptions attribute is likely worth a specific builder
for efficient reason, that'll be the subject of a future patch.

Diff Detail

Event Timeline

mehdi_amini created this revision.Mar 5 2021, 10:39 PM
mehdi_amini requested review of this revision.Mar 5 2021, 10:39 PM
ftynse added inline comments.Mar 8 2021, 2:01 AM
mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
29

Could we have a better comment?

mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
2424

This assumption doesn't seem documented anywhere.

More generally, I am not convinced the additional complexity and use awkwardness that comes with requiring the user to maintain the list of options sorted is worth the savings. We may have maybe a dozen attributes tops stored consecutively in memory, linear scan should be just fine here.

mlir/test/Dialect/LLVMIR/invalid.mlir
793–794

I find this double error message counterintuitive. It's even worse because the second message claims the attribute type is unknown instead of saying it failed to parse.

mehdi_amini added inline comments.Mar 8 2021, 11:17 AM
mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
29

Ah right, that was a bad copy/paste :)

mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
2424

As I mentioned in the description, I don't expect anyone to use this API directly, we should offer a builder API with convenient access. Sketch would be:

LoopOptionsAttrBuilder b; // can be constructed from a LoopOptionsAttr
b.setDisableLICM(true) 
  .setInterleaveCount(12)
  .setDisableUnroll(false);
LoopOptionsAttr opts = b.create();

So you can even write this:

br_op.setLoopOpts(LoopOptionsAttBuilder(br_op.loopOpts()).setDisableLICM(true).setInterleaveCount(12).setDisableUnroll(false))
mlir/test/Dialect/LLVMIR/invalid.mlir
793–794

Yes I noticed, and actually sent a fix here yesterday: https://reviews.llvm.org/D98162

rriddle added inline comments.Mar 8 2021, 11:28 AM
mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
2402

The predicate of lower_bound should return if the value is less(ordered before), not equal. Use less_first here?

2453

I know this isn't new code, but we don't really use failed for these in ifs(because of chaining).

2491

while (succeeded(parser.parseOptionalComma()))?

2519–2522
2519–2522

If the attribute has a mnemonic it should be included in the generatedAttributePrinter. Is that not happening?

mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp
227

I wonder if we should just add a templated get to DictionaryAttr.

mehdi_amini marked 8 inline comments as done.Mar 8 2021, 1:47 PM
mehdi_amini added inline comments.
mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
2402

Oh right! I don't think I can use less_first because only the first argument is a pair.

2424

@rriddle an opinion here?

(right now I add an API doc and renamed the parameter to sortedOptions)

2491

Thanks, much better :)

mehdi_amini marked 2 inline comments as done.

Address comments

rriddle added inline comments.Mar 8 2021, 1:57 PM
mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
29

Can you move this into the description field instead?

mlir/include/mlir/IR/BuiltinAttributes.td
206 ↗(On Diff #329130)

nit: Drop the extra newline.

mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
2424

I don't really see a reason *not* to have the list be sorted. It seems like a simple invariant to impose on the user, especially if the current "raw" API is not something most users should have to interface with. We also have to provide the values in a deterministic way that allows for attributes with the same elements to be uniqued, gives deterministic iteration to users, etc.

@mehdi_amini Can you just add the Builder in this revision or is this something you were going to add in a followup?

mehdi_amini marked 2 inline comments as done.Mar 8 2021, 2:30 PM
mehdi_amini added inline comments.
mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
2424

I can add the Builder now, I didn't because there are no users right now and so the "testing" aspect isn't straightforward: any suggestion on this?

Add LoopOptionsAttrBuilder

ftynse accepted this revision.Mar 9 2021, 12:54 AM
ftynse added inline comments.
mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
2424

I am fine just having a sketch of the builder in a comment with TODO, so however hits the need sees what was planned.

This revision is now accepted and ready to land.Mar 9 2021, 12:54 AM
arpith-jacob accepted this revision.Mar 9 2021, 11:50 AM
This revision was landed with ongoing or failed builds.Mar 9 2021, 11:50 AM
This revision was automatically updated to reflect the committed changes.