This is an archive of the discontinued LLVM Phabricator instance.

[mlir][spirv] Add support to import/export Functions and Global variables
ClosedPublic

Authored by mshahneo on Apr 19 2023, 3:11 PM.

Details

Summary

"LinkageAttributes" decoration allow a SPIR-V module to import
external functions and global variables, or export functions or
global variables for other SPIR-V modules to link against and use.

Import/export capability is extremely important when using outside
libraries (e.g., intrinsic libraries).

Added decorations:

  • LinkageAttributes

Diff Detail

Event Timeline

mshahneo created this revision.Apr 19 2023, 3:11 PM
Herald added a project: Restricted Project. · View Herald Transcript
mshahneo requested review of this revision.Apr 19 2023, 3:11 PM
mshahneo updated this revision to Diff 515107.Apr 19 2023, 3:19 PM

Add Linkage capabilities to the test cases

mshahneo updated this revision to Diff 515109.Apr 19 2023, 3:25 PM

Remove an unnecessary test

Cool, thanks for the contribution! Adding the support for this is certainly fine; but I think we should make it more integrated in the implementation. Two major issues to address before going into details. :)

mlir/lib/Dialect/SPIRV/IR/SPIRVOps.cpp
3528

Instead of just attaching this to the function and global variable as a normal attribute, I'd think we want to modify the definition of spirv.func and spirv.GlobalVariable to include it as a native (optional) attribute. That is, update those two ops' ODS to includ this. It would be better for printing/verification and to avoid accidentally dropping this.

mlir/lib/Target/SPIRV/Deserialization/Deserializer.cpp
289

Can we actually have a proper MLIR attribute definition of LinkageAttributes? You can see SPIRVAttribute.td/h/cpp where we have a few examples there. A proper MLIR attribute, like #spirv.linkage<Import, "ExternalName"> would make it easier to read/print/verify.

antiagainst requested changes to this revision.Apr 20 2023, 11:53 AM
This revision now requires changes to proceed.Apr 20 2023, 11:53 AM

Cool, thanks for the contribution! Adding the support for this is certainly fine; but I think we should make it more integrated in the implementation. Two major issues to address before going into details. :)

Absolutely, can do. Yep, that would be nice, I wasn't sure of that would be too invasive. Will update the PR. Thank you so much :)

mshahneo updated this revision to Diff 522221.May 15 2023, 9:03 AM

Define LinkageAttributes as a prpoper MLIR attribute
Update the implementation accordingly
Update the test cases

mshahneo marked 2 inline comments as done.May 15 2023, 9:07 AM
mshahneo added inline comments.
mlir/lib/Dialect/SPIRV/IR/SPIRVOps.cpp
3528

Updated the definition of spirv.func and spirv.GlobalVariable

mlir/lib/Target/SPIRV/Deserialization/Deserializer.cpp
289

Created a new SPIR-V attribute.

antiagainst requested changes to this revision.May 18 2023, 4:08 PM

Nice! Just a few more nits.

mlir/lib/Target/SPIRV/Deserialization/Deserializer.cpp
280

Strictly this should be 3 + ceildiv(strlen(name), 4) + 1?

281

For unused code please just remove it instead of commenting out.

462

Nit: use . to end the sentence to be consistent with other comments.

Applies to all comments in other places.

mlir/lib/Target/SPIRV/Serialization/SerializeOps.cpp
243

Flip the condition check and do early returns for this case so we can avoid one level of indent?

298

Can we use a local variable for this? Multi-line if conditions is not very nice to readability.

mlir/test/Dialect/SPIRV/IR/function-decorations.mlir
13

I think we can improve the assembly by customizing it to spirv.func @... "Pure" Import["outside.func"], which reads nicer. I won't block it on this though; given this adds the functionality. Would you be interested to handle it later?

15

Nit: indent this to show that linkage_name and linkage_type is nested in linkage_attributes?

Applies to other places too.

This revision now requires changes to proceed.May 18 2023, 4:08 PM
antiagainst retitled this revision from Add support to import/export Functions and Global variables to [mlir][spirv] Add support to import/export Functions and Global variables.May 18 2023, 4:10 PM
mshahneo updated this revision to Diff 524534.May 22 2023, 4:44 PM
mshahneo marked 2 inline comments as done.

Added some stylistic change.

mshahneo marked 5 inline comments as done.May 22 2023, 5:13 PM

Thank you so much for pointing out some nit issues :). I tried to address them.

mlir/lib/Target/SPIRV/Deserialization/Deserializer.cpp
280

I think it should be

3 + strlen(name)/4 + 1

or

3 + ceildiv(strlen(name), 4)

, right?

mlir/test/Dialect/SPIRV/IR/function-decorations.mlir
13

I think we can do it later, it does read nice, but do we actually want to do this? It kind of goes away from the normal rule of passing attributes.

mshahneo updated this revision to Diff 524544.May 22 2023, 5:34 PM

A 'space' was added on an error message.

antiagainst accepted this revision.May 23 2023, 8:00 AM
antiagainst added inline comments.
mlir/lib/Target/SPIRV/Deserialization/Deserializer.cpp
280

Yes actually. (I was thinking the trailing 1 is for "Linkage Type". Sorry for the confusion.)

mlir/test/Dialect/SPIRV/IR/function-decorations.mlir
13

I'm not sure what "goes away from the normal rule of passing attributes" means? It's common to define custom assembly to avoid boilerplate in parsing/printing. But anyway, we can do it later. :)

This revision is now accepted and ready to land.May 23 2023, 8:00 AM

Hi @antiagainst ,
Could you please help me commit the change, I do not have commit access.
Thank you so much in advance :).

mlir/lib/Target/SPIRV/Deserialization/Deserializer.cpp
280

No problem at all :). Thank you so much for looking into it :).

mlir/test/Dialect/SPIRV/IR/function-decorations.mlir
13

Ah, by normal rule I mean, nested way of writing, attribute_name=value. But yes, LinakgeType["LinkageName"] would read nice :).