This is an archive of the discontinued LLVM Phabricator instance.

[mlir] LLVMFuncOp: provide a capability to pass attributes through to LLVM IR
ClosedPublic

Authored by ftynse on Mar 30 2020, 9:50 AM.

Details

Summary

LLVM IR functions can have arbitrary attributes attached to them, some of which
affect may affect code transformations. Until we can model all attributes
consistently, provide a pass-through mechanism that forwards attributes from
the LLVMFuncOp in MLIR to LLVM IR functions during translation. This mechanism
relies on LLVM IR being able to recognize string representations of the
attributes and performs some additional checking to avoid hitting assertions
within LLVM code.

Diff Detail

Event Timeline

ftynse created this revision.Mar 30 2020, 9:50 AM
nicolasvasilache added a subscriber: fhahn.

Thanks Alex, I can confirm that with this, the expected behavior is reached on the LLVM matrix front.
Also + @fhahn

This revision is now accepted and ready to land.Mar 30 2020, 10:39 AM
mehdi_amini added inline comments.Mar 30 2020, 10:57 AM
mlir/docs/Dialects/LLVM.md
107

That seems a bit like a hack to me: when importing from LLVM are we gonna encode attributes this way? Are we gonna honor them?
We're getting into the "json of compiler" here.

mlir/docs/Dialects/LLVM.md
107

Isn't the LLVM string = string attribute the issue here?
Would an alternative require giving more semantics than LLVM has?

In other words, until/unless LLVM force more semantics on those attributes, isn't it a raising problem to import from LLVM and guesstimate types?

mehdi_amini added inline comments.Mar 30 2020, 12:10 PM
mlir/docs/Dialects/LLVM.md
107

LLVM has string attributes only for target specific I believe, otherwise there are rules for "IR-level" attributes: https://github.com/llvm/llvm-project/blob/master/llvm/include/llvm/IR/Attributes.td
(I am not sure I totally understood what you meant though, so my answer may be off here)

ftynse marked an inline comment as done.Mar 31 2020, 2:02 AM
ftynse added inline comments.
mlir/docs/Dialects/LLVM.md
107

I agree that it feels hacky, but so does LLVM's approach. You can add arbitrary quoted strings as function attributes, which some passes of various levels of experimental-ness rely on, and we got to support that. We have a need for that today to support some forms of vectorization. While we could restrict this feature to only support string attributes, we would need to encode a list of allowed attributes somewhere, which sounds equivalent to just building support for them, and we don't have time to invest in that right now.

I expect that we will eventually have first-class modeling for the equivalents of common attributes that may differ from what LLVM does with attributes (e.g., for inlining or FP math options). Until we do, we need to have a way to attach these attributes in the generated code for the purposes of code generation.

https://github.com/llvm/llvm-project/blob/master/llvm/include/llvm/IR/Attributes.td is of little help because it does not say which attributes are allowed on functions (as opposed to, e.g., function attributes) and which of them are allowed to have values. Furthermore, there are attributes allowed on functions listed in https://llvm.org/docs/LangRef.html#function-attributes that are not listed in Attributes.td.

ftynse marked an inline comment as done.Mar 31 2020, 2:13 AM
ftynse added inline comments.
mlir/docs/Dialects/LLVM.md
107

(as opposed to, e.g., function attributes)

I meant function _argument_ attributes

This revision was automatically updated to reflect the committed changes.
mehdi_amini added inline comments.Apr 2 2020, 12:06 PM
mlir/docs/Dialects/LLVM.md
107

I agree that LLVM is not perfect, but this is much uglier and I'm gonna be concerned by any code that start to manipulate or emit this. It'll have to be very contained.

ftynse marked an inline comment as done.Apr 3 2020, 1:06 AM
ftynse added inline comments.
mlir/docs/Dialects/LLVM.md
107

The code Nicolas has uses platform-specific attributes, which are anyway strings that are not registered anyway. I don't see how to "fix" that without imposing our choices further down the pipeline. If someone suddenly starts to use this for an actual function attribute listed in LangRef, we will be able to model that attribute specifically, without having to prematurely overdesign all of them.