This is an archive of the discontinued LLVM Phabricator instance.

[mlir] Fix --convert-func-to-llvm=emit-c-wrappers argument and result attribute handling
ClosedPublic

Authored by laszlo-luminous on Feb 15 2022, 2:47 PM.

Details

Summary

When using --convert-func-to-llvm=emit-c-wrappers the attribute arguments of the wrapper would not be created correctly in some cases.
This patch fixes that and introduces a set of tests for (hopefully) all corner cases.

See https://github.com/llvm/llvm-project/issues/53503

Author: Sam Carroll <sam.carroll@lmns.com>
Co-Author: Laszlo Kindrat <laszlo.kindrat@lmns.com>

Diff Detail

Event Timeline

laszlo-luminous requested review of this revision.Feb 15 2022, 2:47 PM

Updated patch with context, and removed some trailing whitespaces

Sorry I missed this revision when it was sent out and I won't have time to get to it before next week.

@ftynse can you take care of it?

LGTM in general, suggesting to add more verification.

mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp
69 ↗(On Diff #409083)

I would rather put this into the LLVM dialect itself and have a verifier that checks if the attribute is well-formed (used in an op with FunctionOpInterface, the number of elements matches the number of struct elements).

86 ↗(On Diff #409083)

Nit: drop the explicit number of SmallVector elements unless there is a strong reason to use a specific number.

@ftynse I'm happy to add more verification and tests. Do you have any suggestions what they should cover?

mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp
86 ↗(On Diff #409083)

I'll do that. I see that other functions in this file are also specifying the elements for SmallVector, but I can't immediately see why. Should I modify those as well?

Do you have any suggestions what they should cover?

See my comment on line 69. MLIR has a mechanism to ensure that attributes with a name starting with the dialect name satisfy certain conditions -- dialect attribute verifiers. LLVM dialect already implements some here https://github.com/llvm/llvm-project/blob/da047445f77bfd74b04c36169e104f35dbfff84e/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp#L2536. What I suggest is to add your struct_atttribute in the list and check its well-formedness with respect to the operation it is attached to.

mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp
86 ↗(On Diff #409083)

In most cases, they predate the default value for the number of stack elements in vectors. It would be helpful to clean them up in separate patches, thanks in advance!

Ah okay, I misunderstood and thought you meant more unit tests by verification...

Anyway, I am almost done with the verifier for this attribute. However, should this verification happen in verifyRegionArgAttribute and verifyRegionResultAttribute, rather than verifyOperationAttribute? I'm not sure what the semantics are if it appears just as an op attribute (and if we should even allow that).

The standard practice is to have a test for each user-visible error message, so adding a verifier mechanically requires adding more tests.

RegionArg/ResultVerifier are probably a better place for this if you are able to put the check there.

Moved the llvm.struct_attrs attribute to the dialect, and added verifiers with tests.

laszlo-luminous marked 3 inline comments as done.Feb 25 2022, 11:56 AM

I think I addressed all points, see my inline comment about checking for FuncOpInterface.

mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
2665–2667

I'm not sure how I could test this. I found similar logic in https://github.com/llvm/llvm-project/blob/main/mlir/lib/Dialect/Linalg/IR/LinalgDialect.cpp#L132-L133 but I can't find tests for that.

ftynse accepted this revision.Feb 25 2022, 12:30 PM

Thanks!

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

Nit: start with the small case here like everywhere else.

This revision is now accepted and ready to land.Feb 25 2022, 12:30 PM
ftynse added inline comments.Feb 25 2022, 12:31 PM
mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
2665–2667

Just attach the attribute to any operation that doesn't implement the functionOpInterface, e.g. "addi".

mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
2665–2667

I have tried this:

func @invalid(%arg0 : i32, %arg1 : i32) {
  %0 = "arith.addi"(%arg0, %arg1) {llvm.struct_attrs} : (i32, i32) -> i32 
  return
}

which emits a different error, that I already have tests for:
error: 'scf.parallel' op 'llvm.struct_attrs' is permitted only in argument or result attributes

It seems that the only place where verifyRegionResultAttribute or verifyRegionArgAttribute is called is in the FunctionOpInterface verifier: https://github.com/llvm/llvm-project/blob/main/mlir/include/mlir/IR/FunctionInterfaces.h#L207
Which in turn means that this helper only ever gets called when a FunctionOpInterface is verified, so this error is never emitted. Or I'm misunderstanding something completely.

fixed error message to start with lowercase

laszlo-luminous marked an inline comment as done and 2 inline comments as not done.Feb 25 2022, 3:52 PM

Accidentally removed some tests, now they are back.

Is there anything else I should fix on this?

Herald added a project: Restricted Project. · View Herald TranscriptMar 2 2022, 8:27 AM
ftynse added a comment.Mar 2 2022, 8:49 AM

Please rebase on main and let me know if you need help landing this.

Rebased on main.

@ftynse Thanks for the review! I don't think I have permissions to commit, could you please help with that?

laszlo-luminous retitled this revision from [mlir] Fix --convert-std-to-llvm=emit-c-wrappers argument and result attribute handling to [mlir] Fix --convert-func-to-llvm=emit-c-wrappers argument and result attribute handling.
laszlo-luminous edited the summary of this revision. (Show Details)

Update with changes due to new func dialect

laszlo-luminous added a comment.EditedMar 15 2022, 5:47 AM

@mehdi_amini @ftynse Could you please help with committing this? Thanks!