This is an archive of the discontinued LLVM Phabricator instance.

[MLIR][SPIRVToLLVM] Conversion for global and addressof
ClosedPublic

Authored by georgemitenkov on Jul 27 2020, 2:52 AM.

Details

Summary

Inital conversion of spv._address_of and spv.globalVariable.
In SPIR-V, the global returns a pointer, whereas in LLVM dialect the global
holds an actual value. This difference is handled by spv._address_of and
llvm.mlir.addressofops that both return a pointer. Moreover, only current
invocation is in conversion's scope.

Diff Detail

Event Timeline

georgemitenkov created this revision.Jul 27 2020, 2:52 AM
antiagainst requested changes to this revision.Jul 28 2020, 6:38 PM
antiagainst added inline comments.
mlir/lib/Conversion/SPIRVToLLVM/ConvertSPIRVToLLVM.cpp
519

considered as

540

I think External is not correct for Private storage class. Private storage class can only be accessed by the current module so it should map to LLVM private linkage?

This revision now requires changes to proceed.Jul 28 2020, 6:38 PM
georgemitenkov marked an inline comment as done.Jul 29 2020, 6:28 AM
georgemitenkov added inline comments.
mlir/lib/Conversion/SPIRVToLLVM/ConvertSPIRVToLLVM.cpp
540

@antiagainst That's right! What about other storage classes?
Based on SPIR-V spec:

By default, functions and global variables are private to a module and cannot be accessed by other modules. However, a module may be written to export or import functions and global (module scope) variables. Imported functions and global variable definitions are resolved at linkage time. A module is considered to be partially linked if it depends on imported values. Within a module, imported or exported values are decorated using the Linkage Attributes Decoration.

This means that Private linkage should be the default for all, and it is the linkage decoration that specifies if the variable is Import/Export?

georgemitenkov marked an inline comment as done.

Fixed a typo and handled linkage properly.

mravishankar added inline comments.Aug 3 2020, 11:36 PM
mlir/lib/Conversion/SPIRVToLLVM/ConvertSPIRVToLLVM.cpp
507

Comment is misleading. The SPIR-V spec returns an SSA value. In SPIR-V dialect GlobalVariable is a Symbol. Not sure what LLVM dialect holds an actual value means though. AFAIK, the LLVM global variable and SPIR-V global variable are the same and the address of act the same way?

539

I am confused why input is chosen as a constant. I might be missing something here (or maybe just the naming is throwing me off), but Input seems to be more like a Global Variable that is externally defined and used internally (I dont know off the top of my head if there is a further qualifier that makes it externally defined but not modified within an LLVM module). SPIR-V constants map more directly to LLVM constants.

mlir/test/Conversion/SPIRVToLLVM/memory-ops-to-llvm.mlir
13

Please group all the CHECK comments together. Its easier to read that way.

mravishankar requested changes to this revision.Aug 3 2020, 11:37 PM
This revision now requires changes to proceed.Aug 3 2020, 11:37 PM
georgemitenkov marked an inline comment as done.Aug 4 2020, 2:42 AM
georgemitenkov added inline comments.
mlir/lib/Conversion/SPIRVToLLVM/ConvertSPIRVToLLVM.cpp
507

spv._address_of acts the same way indeed. What I meant here is that spv.globalVariable's type is a pointer, whereas llvm.mlir.global op's type is the pointee's type.
Hence we need to do this before type conversion

globalOp.type().cast<spirv::PointerType>().getPointeeType()

to later build llvm.mlir.global.

539

It is still a LLVM global but just with a constant flag. Based on LLVM dialect spec, if the global value is a constant, storing into it is not allowed. I think that by "is a constant" it means that it has a constant attribute.
Since Input variables are

read-only, and cannot have initializers

per SPIR-V spec, we add constant attribute to llvm.mlir.global?

Put CHECK statements in one block.

mravishankar accepted this revision.Aug 4 2020, 10:09 AM
mravishankar added inline comments.
mlir/lib/Conversion/SPIRVToLLVM/ConvertSPIRVToLLVM.cpp
539

THanks for the clarification! That makes sense to me.

mlir/test/Conversion/SPIRVToLLVM/memory-ops-to-llvm.mlir
13

THanks. Super Nit, typically I have seen

CHECK-LABEL:
    CHECK:

instead. But this is as good.

georgemitenkov marked 2 inline comments as done.Aug 6 2020, 7:27 AM
georgemitenkov marked an inline comment as done.Aug 6 2020, 7:30 AM
antiagainst accepted this revision.Aug 11 2020, 8:58 AM

Sorry for the delay! For future patches, please feel free to land once you've gotten Mahesh's approval; no need to wait for mine.

This revision is now accepted and ready to land.Aug 11 2020, 8:58 AM