This is an archive of the discontinued LLVM Phabricator instance.

[mlir][Func] Extract datalayout string attribute setting as a separate module pass
ClosedPublic

Authored by nicolasvasilache on Aug 10 2023, 4:50 AM.

Details

Summary

FuncToLLVM uses the data layout string attribute in 3 different ways:

  1. LowerToLLVMOptions options(&getContext(), getAnalysis<DataLayoutAnalysis>().getAtOrAbove(m));
  2. options.dataLayout = llvm::DataLayout(this->dataLayout);
  3. m->setAttr(..., this->dataLayout));

The 3rd way is unrelated to the other 2 and occurs after conversion, making it confusing.
This revision separates this post-hoc module annotation functionality into its own pass.
The convert-func-to-llvm pass loses its data-layout option and instead recovers it from
the llvm.data_layout attribute attached to the module, when present.

In the future, LowerToLLVMOptions options(&getContext(), getAnalysis<DataLayoutAnalysis>().getAtOrAbove(m)) and
options.dataLayout = llvm::DataLayout(dataLayout); should be unified.

Diff Detail

Event Timeline

Herald added a reviewer: dcaballe. · View Herald Transcript
Herald added a project: Restricted Project. · View Herald Transcript
nicolasvasilache requested review of this revision.Aug 10 2023, 4:50 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 10 2023, 4:50 AM
mehdi_amini added inline comments.Aug 10 2023, 12:50 PM
mlir/include/mlir/Conversion/Passes.td
349

Mention override existing one?

mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp
789

That's changing the behavior of the pass ConvertFuncToLLVM pass, might be desirable, but how is it OK to change the datalayout after the fact?

Address comment

nicolasvasilache marked an inline comment as done.Aug 11 2023, 8:18 AM
nicolasvasilache added inline comments.
mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp
789

The "change the datalayout after the fact" behavior is already present before this revision.
I would certainly want that cleaned up.

For now I am just trying to separate pieces, there is another cleanup of "untested therefore unused" datalayout string usage coming up.

mehdi_amini added inline comments.Aug 29 2023, 3:28 PM
mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp
789

One different I see is that before, the ConvertFuncToLLVM would use a data layout for performing the conversion, and then set it on the Module, while now the ConvertFuncToLLVM still takes a data layout option but it is lost in the IR.

mehdi_amini added inline comments.Aug 29 2023, 8:26 PM
mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp
789

(I guess I'm still trying to figure out the direction here)

nicolasvasilache retitled this revision from [mlir][Func] Extract the datalayout string functionality as a separate pass to [mlir][Func] Extract datalayout string attribute setting as a separate module pass.
nicolasvasilache edited the summary of this revision. (Show Details)

Update

mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp
789

Good point, I reshuffled a bit so that the setting of the llvm.datalayout string attribute on the module is explicit.
convert-func-to-llvm can then lose its data-layout string option and recover it from the module, when present, to build LowerToLLVMOptions::dataLayout and at least have some consistency there.

This way, we at least won't need to make all xxx-to-llvm passes grow a data-layout string option to be consistent with convert-func-to-llvm.

Note however that consistency is a big word here:

  1. no other LowerToLLVMOptions::dataLayout object instance is set up today outside of llvm.datalayout: the "data layout from string" is fundamentally inconsistent across xxx-to-llvm passes today.
  2. the "MLIR DataLayout mechanism" is currently only used to set up LowerToLLVMOptions::indexBitwidth (but not LowerToLLVMOptions::dataLayout) (see here ). Index bitwidth at the module level is not very relevant as we already experience with GPU global / shared address spaces.
  3. TypeConverter is the common place from which conversion patterns extract the llvm.datalayout (via an LowerToLLVMOptions::dataLayout); it also has a dataLayoutAnalysis so it combines both ways of specifying data layouts (LLVM's and MLIR's). Unfortunately, we know the TypeConverter across passes is very inconsistent already as per this discussion.

I believe this is a step towards cleaning some of this up.

ftynse added inline comments.Aug 30 2023, 9:23 AM
mlir/include/mlir/Conversion/Passes.td
344

Nit: I'd prefer explicitly mentioning llvm in the name here, e.g., set-module-llvm-datalayout.

mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp
753

Nit: we shouldn't need llvm:: here.

789

IIRC, the direction was to eventually stop using the LLVM string entirely in the conversions and have it constructed from the MLIR data layout. It can be either at conversion time or even at translation time to avoid mismatch. Don't remember why this has never happened, probably the lack of support for some of the non-size parts of the data layout that are available on GPU.

mehdi_amini accepted this revision.Aug 30 2023, 10:22 PM

Works for me, if @ftynse is OK :)

This revision is now accepted and ready to land.Aug 30 2023, 10:22 PM
nicolasvasilache marked 3 inline comments as done.Aug 31 2023, 2:20 AM
nicolasvasilache added inline comments.
mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp
789

Yes and https://reviews.llvm.org/D157726 actually shows that the data layout itself is currently not used (or at least not tested) beyond being forwarded to LLVM.

ftynse accepted this revision.Sep 4 2023, 5:07 AM

Okay for me.

nicolasvasilache marked an inline comment as done.

Address comments

nicolasvasilache marked 3 inline comments as done.Sep 4 2023, 8:04 AM
This revision was landed with ongoing or failed builds.Sep 4 2023, 8:05 AM
This revision was automatically updated to reflect the committed changes.