This is an archive of the discontinued LLVM Phabricator instance.

[flang] Add support for dense complex constants
ClosedPublic

Authored by luporl on Jul 21 2023, 6:29 AM.

Details

Summary

Add support for representing complex array constants with MLIR
dense attribute. This improves compile time and greatly reduces
memory usage of programs with large complex array constants.

Fixes https://github.com/llvm/llvm-project/issues/63610

Diff Detail

Event Timeline

luporl created this revision.Jul 21 2023, 6:29 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptJul 21 2023, 6:29 AM
luporl published this revision for review.Jul 21 2023, 6:38 AM
vzakhari added inline comments.Jul 31 2023, 3:37 PM
flang/test/Lower/array.f90
133

I do not think it is a good idea to mix mlir.complex and fir.complex types here. fir.global might be allowing this right now, but there is no guarantee that mlir.complex and fir.complex will be converted to the same type during fir.global to llvm.global codegen.

Can we at least add an assertion in fir.global codegen that the init attribute data type and the operation's result data type match?

luporl updated this revision to Diff 546224.Aug 1 2023, 2:23 PM

Assert that operation's result type matches init attribute data type.

luporl marked an inline comment as done.Aug 1 2023, 2:37 PM
luporl added inline comments.
flang/test/Lower/array.f90
133

Assert added.
There is already a mix between fir.logical and integer types.
For instance, an array constructor with 3 logical elements becomes something like:
fir.global internal @_QQro.3xl4.1(dense<[1, 0, 1]> : tensor<3xi32>) constant : !fir.array<3x!fir.logical<4>>.

luporl marked an inline comment as done.Aug 1 2023, 2:41 PM
vzakhari added inline comments.Aug 4 2023, 1:34 PM
flang/lib/Optimizer/CodeGen/CodeGen.cpp
2950

I think this assert is a no-op, because tyAttr is the created op's explicit result type. What I suggested to assert is that initAttr's type and tyAttr comply.

luporl added inline comments.Aug 18 2023, 8:45 AM
flang/lib/Optimizer/CodeGen/CodeGen.cpp
2950

Sorry for the delayed response, I've been away for a few days.

initAttr's type and tyAttr doesn't match.

Considering a complex array of 10 elements, they will be:

tyAttr:   !llvm.array<10 x struct<(f32, f32)>>
initAttr's type: tensor<10xcomplex<f32>>
global: llvm.mlir.global internal @_QFEc1(dense<[(3.000000e+00,4.000000e+00), ...]> : tensor<10xcomplex<f32>>) {addr_space = 0 : i32} : !llvm.array<10 x struct<(f32, f32)>>

It seems the global's dense attribute and its tensor<10xcomplex<f32>> type are lowered to LLVM on a later pass.

It's easy to extract the element type from both (struct<(f32, f32) and complex<f32>), but is there a function that can be called from codegen to convert from MLIR to LLVM type?

luporl updated this revision to Diff 554462.Aug 29 2023, 12:41 PM

Assert that initAttr's type and tyAttr comply.

luporl marked an inline comment as done.Aug 29 2023, 12:59 PM
luporl added inline comments.
flang/lib/Optimizer/CodeGen/CodeGen.cpp
2950

A new function was added to compare initAttr's type and tyAttr.

It extracts the element type of both and convert initAttr's element type to its corresponding LLVM type, to make it possible to compare it with tyAttr's type.

The new function is only called by the assert.

flang/test/Lower/array.f90
133

Just expanding a bit more on this, I don't see any good way to avoid mixing mlir.complex and fir.complex types here.

fir.complex can't be changed to mlir.complex without adding casts to the code that manipulates the global's elements, which seems worse. Or that code would need to be changed to operate with mlir.complex values, which would probably require a lot of work.

mlir.complex can't be changed to fir.complex, as it is not a supported attribute type. Maybe it could be wrapped inside an OpaqueAttr, but I don't know if it would work and it doesn't seem a good idea either.

vzakhari accepted this revision.Aug 29 2023, 1:10 PM

Thank you for the extra steps to improve the verification and for the explanations! I think it is as good as it can get currently.

This revision is now accepted and ready to land.Aug 29 2023, 1:10 PM
This revision was landed with ongoing or failed builds.Aug 30 2023, 6:51 AM
This revision was automatically updated to reflect the committed changes.

Thank you for the review!