This is an archive of the discontinued LLVM Phabricator instance.

[mlir][gpu] Migrate hard-coded address space integers to an enum attribute (gpu::AddressSpaceAttr)
ClosedPublic

Authored by christopherbate on Dec 23 2022, 4:44 PM.

Details

Summary

This is a purely mechanical change that introduces an enum attribute in the GPU
dialect to represent the various memref memory spaces as opposed to the
hard-coded integer attributes that are currently used.

The following steps were taken to make the transition across the codebase:

  1. Introduce a pass "gpu-lower-memory-space-attributes":

The pass updates all memref types that have a memory space attribute that is a
gpu::AddressSpaceAttr. These attributes are changed to IntegerAttr's using a
mapping that is given by the caller. This pass is based on the
"map-memref-spirv-storage-class" pass and the common functions can probably
be refactored into a set of utilities under the MemRef dialect.

  1. Update the verifiers of GPU/NVGPU dialect operations.

If a verifier currently checks the address space of an operand using
e.g.getWorkspaceAddressSpace, then it can continue to do so. However, the
checks are changed to only fail if the memory space is either missing or a wrong
value of type gpu::AddressSpaceAttr. Otherwise, it just assumes the address
space is correct because it was specifically lowered to something other than a
gpu::AddressSpaceAttr.

  1. Update existing gpu-to-llvm conversion infrastructure.

In the existing gpu-to-X passes, we add a full conversion equivalent to
gpu-lower-memory-space-attributes just before doing the conversion to the
LLVMDialect. This is done because currently both the gpu-to-llvm passes
(rocdl,nvvm) run gpu-to-gpu rewrites within the pass, which introduce
AddressSpaceAttr memory space annotations. Therefore, I inserted the
memory space conversion between the gpu-to-gpu rewrites and the LLVM
conversion.

For more context see the below discourse discussion:
https://discourse.llvm.org/t/gpu-workgroup-shared-memory-address-space-is-hard-coded/

Diff Detail

Event Timeline

christopherbate requested review of this revision.Dec 23 2022, 4:44 PM

The code for the pass that is introduced here is being exercised in the gpu-to-llvm conversions, but I still need to add tests for the pass in isolation.

ftynse requested changes to this revision.Dec 28 2022, 12:44 AM
ftynse added a subscriber: ftynse.

The approach looks good overall. Please address a bunch of cosmetic changes.

mlir/include/mlir/Dialect/GPU/IR/GPUBase.td
76–77

Any particular reason to start from 3 here? I'd rather use the number that wouldn't work transparently for CUDA so we shake off related issues early. I understand reserving 0 for the default space though.

76–77

Also, please add the non-capitalized version of the string as the third argument to I32EnumAttrCase, we don't want capitalized names in the assembly syntax.

mlir/lib/Conversion/GPUToNVVM/LowerGpuOpsToNVVMOps.cpp
184

We'd better not ignore pattern application failures. Even if the patterns _currently_ cannot fail, they may be eventually changed and we will puzzled as to why the pass succeeds while doing nothing.

mlir/lib/Dialect/GPU/Transforms/LowerMemorySpaceAttributes.cpp
33–34

Copy-pasta? This file is not related to SPIR-V conversion.

43

Ditto.

51

Same here and probably below.

99–108

I see that some of this code is carried over, but we should modernize it. There is no reason to privilege builtin function types in this conversion. Use the SubelementTypeInterface to access nested types and convert them. This will support, e.g., memref of memref, which is currently ignored.

122

Nit: no need to prefix SmallVector with llvm::. And also no need to specify the explicit number of stack elements. Here and below.

183

Nit: please add a newline.

mlir/lib/Dialect/NVGPU/IR/NVGPUDialect.cpp
42

Can we factor out the magic 3 into a named constant, something like NVGPUDialect::kSharedMemoryAddressSpace.

272

Nit: let's not hardcode 3 here either, use the named constant instead.

mlir/test/Dialect/GPU/invalid.mlir
353

Please drop trailing spaces here and below.

This revision now requires changes to proceed.Dec 28 2022, 12:44 AM
bondhugula added inline comments.Dec 28 2022, 9:38 AM
mlir/lib/Conversion/GPUToNVVM/LowerGpuOpsToNVVMOps.cpp
188–190

Nit: These three lines can go below to the relevant. case statement.

mlir/lib/Dialect/NVGPU/IR/NVGPUDialect.cpp
42

+1 to kSharedMemoryAddressSpace.

mlir/lib/Dialect/NVGPU/Transforms/OptimizeSharedMemory.cpp
261

allocOp.getType().

mlir/test/Dialect/GPU/invalid.mlir
364

Drop trailing whitespace.

git diff --check HEAD~

antiagainst added inline comments.Dec 29 2022, 3:43 PM
mlir/include/mlir/Dialect/GPU/Transforms/Passes.h
67

Populates type conversion rules for ...

mlir/lib/Dialect/GPU/Transforms/LowerMemorySpaceAttributes.cpp
99–108

+1! It would be nice to update the SPIR-V side too. :)

I've got a few notes

mlir/include/mlir/Dialect/GPU/IR/GPUBase.td
76

Because AMD wants it, please include global : 1

80
mlir/lib/Dialect/NVGPU/IR/NVGPUDialect.cpp
42

+1 and do it for ROCDL too

christopherbate marked 20 inline comments as done.

Address all comments

christopherbate added inline comments.Jan 5 2023, 9:00 AM
mlir/lib/Dialect/GPU/Transforms/LowerMemorySpaceAttributes.cpp
99–108

I updated it to to SubElementTypeInterface, thanks for the pointer! That appears to handle all cases in this function.

99–108

For the SPIRV dialect code, I'm not very familiar with it, so I didn't modify it as it's not absolutely required for this patch.

ftynse accepted this revision.Jan 5 2023, 9:07 AM

Thanks!

mlir/lib/Conversion/GPUToROCDL/LowerGpuOpsToROCDLOps.cpp
141

Nit: can these be turned into named constants, similarly to NVVM?

This revision is now accepted and ready to land.Jan 5 2023, 9:07 AM
mlir/lib/Conversion/GPUToROCDL/LowerGpuOpsToROCDLOps.cpp
141

Yeah I missed that one, will fix before landing.

Add some tests for the standalone "gpu-lower-memory-space-attributes" pass.

In gpu.func verifier, don't emit an error if block args for workgroup/private address space don't contain an attribute at all. This could be purposeful.
Only emit an error if the memref type contains the wrong #gpu.address_space attribute.

Fix trailing whitespace.

This appears to have broken the Windows buildbot. https://lab.llvm.org/buildbot/#/builders/13/builds/30769/steps/6/logs/stdio

C:\buildbot\mlir-x64-windows-ninja\llvm-project\mlir\lib\Conversion\GPUToNVVM\LowerGpuOpsToNVVMOps.cpp(209) : error C2220: the following warning is treated as an error
C:\buildbot\mlir-x64-windows-ninja\llvm-project\mlir\lib\Conversion\GPUToNVVM\LowerGpuOpsToNVVMOps.cpp(209) : warning C4715: '<lambda_7426c7b91625e1020521dab1f464b370>::operator()': not all control paths return a value

C:\buildbot\mlir-x64-windows-ninja\llvm-project\mlir\lib\Conversion\GPUToROCDL\LowerGpuOpsToROCDLOps.cpp(149) : error C2220: the following warning is treated as an error
C:\buildbot\mlir-x64-windows-ninja\llvm-project\mlir\lib\Conversion\GPUToROCDL\LowerGpuOpsToROCDLOps.cpp(149) : warning C4715: '<lambda_fea91019496d34fd405e9fcb5563e11d>::operator()': not all control paths return a value

This appears to have broken the Windows buildbot. https://lab.llvm.org/buildbot/#/builders/13/builds/30769/steps/6/logs/stdio

C:\buildbot\mlir-x64-windows-ninja\llvm-project\mlir\lib\Conversion\GPUToNVVM\LowerGpuOpsToNVVMOps.cpp(209) : error C2220: the following warning is treated as an error
C:\buildbot\mlir-x64-windows-ninja\llvm-project\mlir\lib\Conversion\GPUToNVVM\LowerGpuOpsToNVVMOps.cpp(209) : warning C4715: '<lambda_7426c7b91625e1020521dab1f464b370>::operator()': not all control paths return a value

C:\buildbot\mlir-x64-windows-ninja\llvm-project\mlir\lib\Conversion\GPUToROCDL\LowerGpuOpsToROCDLOps.cpp(149) : error C2220: the following warning is treated as an error
C:\buildbot\mlir-x64-windows-ninja\llvm-project\mlir\lib\Conversion\GPUToROCDL\LowerGpuOpsToROCDLOps.cpp(149) : warning C4715: '<lambda_fea91019496d34fd405e9fcb5563e11d>::operator()': not all control paths return a value

Thanks, will fix now.