This is an archive of the discontinued LLVM Phabricator instance.

[MLIR] Added llvm.fence
ClosedPublic

Authored by Sagar on Mar 4 2020, 2:09 PM.

Details

Summary

This patch adds llvm.fence. I tried not to change the syntax much.

syntax:

LLVM IR
fence [syncscope("<target-scope>")] <ordering>

MLIR LLVM Dialect

llvm.fence [syncscope("<target-scope>")] <ordering>

example:
LLVM IR: fence syncscope("agent") seq_cst
MLIR: llvm.fence syncscope("agent") seq_cst

Diff Detail

Event Timeline

Sagar created this revision.Mar 4 2020, 2:09 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 4 2020, 2:09 PM
Sagar updated this revision to Diff 248388.Mar 4 2020, 8:52 PM

local was not up to date.

Sagar added a comment.Mar 5 2020, 12:06 AM

Can anyone let me know why the build is failing? There are no errors/warnings on my local build.

Can anyone let me know why the build is failing? There are no errors/warnings on my local build.

It tells you which test is failing. You can also click on the summary links to get more information.
In this case it seems like a clang test is broken, which is unrelated. There is also a clang-tidy fixit suggestion but it is just a warning and I'd ignore it here.

ftynse retitled this revision from {MLIR} Added llvm.fence to [MLIR] Added llvm.fence.Mar 5 2020, 1:21 AM
ftynse added a reviewer: ftynse.
ftynse requested changes to this revision.Mar 5 2020, 1:34 AM
ftynse added inline comments.
mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
947

Could you keep arguments in the same order as LLVM's builder (ordering, then scope).

951

Please keep C++ blobs inside Tablegen formatted as per regular C++ coding style.

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

Can't this be implemented with declarative assembly format?

1574

This doesn't correspond to the code below: you have a string-attr inside syncscope, not a keyword

1578

Let's have a named constant for "syncscope" instead of repeating the string every time.

1593

Why this condition here? Do you assign special meaning to empty scope identifier? This effectively makes a fence op with empty scope identifier and one without scope identifier print identically...

Also, dyn_cast may return null, use cast to assert instead.
Also, use .empty() for emptiness check.

1600

I don't see the point of converting an enum to string and then comparing strings, instead of comparing enum values.

mlir/test/Dialect/LLVMIR/roundtrip.mlir
295

Please add tests for all error messages in the custom verifier.

This revision now requires changes to proceed.Mar 5 2020, 1:34 AM
Sagar marked 6 inline comments as done.Mar 5 2020, 2:15 AM
Sagar added inline comments.
mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
1571

I did try that, but optional groups are only supported for variadic arguments, afaik. Essentially, I was not able to do something like, (syncscope("$syncscope"))?.

1574

Will correct that.

1593

The condition was to avoid printing something like syncscope(""), which afaik does not hold any special meaning and so must not be printed.

1600

Will try to do the same with enums itself.

ftynse added inline comments.Mar 5 2020, 3:01 AM
mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
1593

"Does not hold special meaning" does not mean it should not be printed in the custom form. Quite the inverse. If you print it in the generic form, it _will_ have the attribute with an empty string as value. This is _different_ from not having an attribute at all, and we should be able to roundtrip this properly, which your code does not.

Sagar marked an inline comment as done.Mar 5 2020, 6:17 AM
Sagar added inline comments.
mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
1593

I get what you mean, but I tried to replicate what opt does.
Example:
given: fence syncscope("") seq_cst
opt returns: fence seq_cst

ftynse added inline comments.Mar 5 2020, 6:41 AM
mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
1593

I don't think you chose the right way of doing so. You only changed the way an operation is printed, without fundamentally changing anything about the operation itself. In the proposed version, there are two states of the op (no-attribute, and empty-attribute), which seem equivalent in LLVM IR. Instead, I would suggest making the attribute mandatory, and changing the parser and the builder to create the attribute and assign an empty string value to it if it is not present or provided in the builder.

Sagar updated this revision to Diff 248503.Mar 5 2020, 8:59 AM
Sagar marked 9 inline comments as done.
ftynse requested changes to this revision.Mar 6 2020, 1:12 AM
ftynse added inline comments.
mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
1593

The dyn_cast comment is still not addressed.

Normally, you don't need to go op.getAttr().cast().getValue(), you have an auto-generated accessor .syncscope for that.

1603

Still not there. Please use named enum values instead of magic numbers. Previous version was more readable, but used expensive string comparisons for no reason. You need something like op.ordering() != AtomicOrdering::not_atomic or op.ordering < AtomicOrderig::acquire.

This revision now requires changes to proceed.Mar 6 2020, 1:12 AM
Sagar updated this revision to Diff 248747.Mar 6 2020, 8:44 AM
Sagar marked 2 inline comments as done.

Addressed comments.

Sagar updated this revision to Diff 248811.Mar 6 2020, 1:00 PM

Resolved clang-tidy warning.

ftynse accepted this revision.Mar 16 2020, 7:14 AM
This revision is now accepted and ready to land.Mar 16 2020, 7:14 AM
rriddle added inline comments.Mar 16 2020, 9:54 AM
mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp
772

nit: Please cache the end of the range.

for (unsigned i = 0, e = ...; i != e;

Sagar updated this revision to Diff 250635.Mar 16 2020, 2:54 PM
Sagar marked an inline comment as done.

Addressed nits.

I do not have write access to the repository. If would be great if someone could commit for me.

The patch does not apply anymore, please rebase and upload a new version. Thanks!

Sagar updated this revision to Diff 250799.Mar 17 2020, 9:35 AM

Rebased.

This revision was automatically updated to reflect the committed changes.