This is an archive of the discontinued LLVM Phabricator instance.

[Flang][MLIR] Alter Fir.GlobalOp to print and lower external attributes
ClosedPublic

Authored by agozillon on Apr 14 2023, 9:54 AM.

Details

Summary

Fir.GlobalOp's currently do not respect attributes that
are applied to them, this change will do two things:

  1. Allow lowering of arbitrary attributes applied to Fir.GlobalOp's to LLVMGlobalOp's during CodeGen
  2. Allow printing and parsing of arbitrarily applied attributes

This allows applying other dialects attributes (or other
fir attributes) to fir.GlobalOps on the fly and have them
exist in the resulting LLVM dialect IR or FIR IR.

Diff Detail

Event Timeline

agozillon created this revision.Apr 14 2023, 9:54 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptApr 14 2023, 9:54 AM
agozillon requested review of this revision.Apr 14 2023, 9:54 AM
agozillon updated this revision to Diff 513655.Apr 14 2023, 9:59 AM

Add missing newline

Not sure who the best reviewer(s) is for this, unfortunately! So please do add reviewers if I am missing any.

This is something I came across while utilizing an OpenMP dialect attribute that I'd like to be able to apply to fir.GlobalOp's. I am unsure if ignoring externally applied attributes is the desired behavior, however. Although aligning it a little more with the LLVM Dialects GlobalOp's in this respect seemed reasonable.

Makes sense to me.
I think there is a way to get all the names of the attributes explicitly defined for an operation using getAttributeNames(). It is generated by MLIR in FIROps.h.inc and looks like:

`
class GlobalOp  /* ....*/ {
  // ....
  static ::llvm::ArrayRef<::llvm::StringRef> getAttributeNames() {
    static ::llvm::StringRef attrNames[] = {::llvm::StringRef("constant"), ::llvm::StringRef("initVal"), ::llvm::StringRef("linkName"), ::llvm::StringRef("sym_name"), ::llvm::StringRef("symref"), ::llvm::StringRef("target"), ::llvm::StringRef("type")};
    return ::llvm::ArrayRef(attrNames);
  }
`

Can you try using it?
This would avoid having to manually list of attributes explicitly handled by the GlobalOp in codgen and in the printer.

agozillon updated this revision to Diff 514951.Apr 19 2023, 7:31 AM
  • [Flang][MLIR] Apply use of getAttributeNames to simplify and make changes more robust

Makes sense to me.
I think there is a way to get all the names of the attributes explicitly defined for an operation using getAttributeNames(). It is generated by MLIR in FIROps.h.inc and looks like:

`
class GlobalOp  /* ....*/ {
  // ....
  static ::llvm::ArrayRef<::llvm::StringRef> getAttributeNames() {
    static ::llvm::StringRef attrNames[] = {::llvm::StringRef("constant"), ::llvm::StringRef("initVal"), ::llvm::StringRef("linkName"), ::llvm::StringRef("sym_name"), ::llvm::StringRef("symref"), ::llvm::StringRef("target"), ::llvm::StringRef("type")};
    return ::llvm::ArrayRef(attrNames);
  }
`

Can you try using it?
This would avoid having to manually list of attributes explicitly handled by the GlobalOp in codgen and in the printer.

Thank you for pointing this out, I was unaware that it only gave the explicit attributes! I've applied the changes now and they work nicely.

jeanPerier accepted this revision.Apr 20 2023, 2:18 AM

LGTM, thanks

This revision is now accepted and ready to land.Apr 20 2023, 2:18 AM

LGTM, thanks

Thank you very much for the review!