This is an archive of the discontinued LLVM Phabricator instance.

[mlir][llvmir] Translate function result attributes to LLVM IR.
ClosedPublic

Authored by vzakhari on Oct 30 2022, 2:08 PM.

Details

Summary

Translate align, noalias, noundef, signext and zeroext result
attributes from llvm.func to LLVM IR.

This is needed for https://github.com/llvm/llvm-project/issues/58579

Depends on D137048

Diff Detail

Event Timeline

vzakhari created this revision.Oct 30 2022, 2:08 PM
vzakhari requested review of this revision.Oct 30 2022, 2:08 PM
ftynse accepted this revision.Nov 2 2022, 6:44 AM

LGTM with comments addressed.

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
894–924

Could we move verification to LLVMDialect::verifyRegionResultAttribute?

mlir/test/Target/LLVMIR/llvmir-invalid.mlir
108 ↗(On Diff #471871)

Nit: prefer @below to @+1.

This revision is now accepted and ready to land.Nov 2 2022, 6:44 AM
vzakhari added inline comments.Nov 2 2022, 3:31 PM
mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
894–924

Yes, we should move it there. After looking through the verification code I think code here and in D137050 is not quite correct. These attributes should be return attributes, not the operation attributes. I will have to redo this and I will move verification along with this rewrite.

vzakhari planned changes to this revision.Nov 2 2022, 3:33 PM
vzakhari updated this revision to Diff 472814.Nov 2 2022, 5:41 PM
This revision is now accepted and ready to land.Nov 2 2022, 5:41 PM
vzakhari requested review of this revision.Nov 2 2022, 5:41 PM
vzakhari marked an inline comment as done.Nov 2 2022, 7:39 PM

Hi @ftynse, there is some code duplication between verifyRegionArgAttribute and verifyRegionResultAttribute right now. I am going to get rid of it when addressing https://reviews.llvm.org/D137048#inline-1324059 in a separate commit.

ftynse added inline comments.Nov 3 2022, 1:03 AM
mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
894–924

Ultimately, argument/result attributes are operation attributes with a special name and pretty printing...

vzakhari added inline comments.Nov 3 2022, 9:28 AM
mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
894–924

I agree, in general, but at least the setters/getters are different for argument/result/operation attributes :)
Can you please take another look and see if everything looks okay to you?

vzakhari added inline comments.Nov 6 2022, 2:19 PM
mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
2795

It looks like I cannot verify the argument/result type in the verification hooks. For example, there are cases where llvm.align is attached to memref<...> arguments, so it is not clear to me how to verify the value type here without converting the value type to LLVM dialect type. I suggest keeping the attribute's type (UnitAttr, IntegerAttr, etc.) verification here, but keep the value type verification inside LLVM dialect to LLVM IR translator (i.e. ModuleTranslation.cpp). @ftynse, does it sound appropriate to you?

vzakhari updated this revision to Diff 473800.Nov 7 2022, 3:13 PM
vzakhari retitled this revision from [mlir][llvmir] Translate function return attributes to LLVM IR. to [mlir][llvmir] Translate function result attributes to LLVM IR..
vzakhari edited the summary of this revision. (Show Details)

Added LLVM::isCompatibleType() check to narrow the set of types that we can reliably diagnose in the verification code.

Hi @ftynse, does it look okay to you? Thank you!

ftynse accepted this revision.Nov 18 2022, 4:25 AM
ftynse added inline comments.
mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
2795

This may be slightly better than before, so let's go with it, though the "verification" is now split between two places. I think attaching llvm.align to memref is plain wrong because the default convention converts memref to a struct with two pointers that have different alignment and _both_ will have the attribute in the LLVM representation. This is partly my fault for originally introducing llvm.noalias and not cleaning it up when the two-pointer approach was added (so now llvm.noalias on a memref is a blatant lie), but we should really model these things properly at higher levels or have interfaces that ensure they are interpreted correctly at lowering time.

This revision is now accepted and ready to land.Nov 18 2022, 4:25 AM
ftynse added inline comments.
mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
2795

Let me also tag @ezhulenev to whom I made a similar request in another patch.

vzakhari added inline comments.Nov 18 2022, 12:05 PM
mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
2795

Thank you, @ftynse! Yes, I believe I saw that the integer members of the converted memref were also getting llvm.align, which was completely wrong.