Skip to content

[AMD] Introduce specialized Allocation pass #7328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

alefimov-amd
Copy link

This PR introduces AMD specific allocation pass and new attribute that defines conversion method: padded or swizzled.
For now OptimizeLDSUsage pass sets all convert layout operations in padded mode.

This PR introduces AMD specific allocation pass and new attribute that controlls method scratch pad conversion method: padded or swizzled.
For now OptimizeLDSUsage pass sets all convert_layout operations in padded mode.
@alefimov-amd
Copy link
Author

alefimov-amd commented Jun 26, 2025

General swizzling conversion consumes a lot more shared memory, which is a problem on mi30x and older architectures.

Idea is to support both variants in AMD backend:

  • By default use swizzling pattern
  • OptimizeLDSUsage pass analyzes lds consumption and can add special operand attribute denoting operations should be padded instead of swizzled
  • Implement special AMD Allocation pass and convert patterns, which will be applied on operations with given attribute, otherwise fallback to common implementation.

This PR adds only part related to allocation analysis. Conversion pattern implementation is in progress.

Copy link
Collaborator

@ThomasRaoux ThomasRaoux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this is something we should make common to all backends but I have to admit I don't understand how this controls code generation right now

}

unsigned AMDAllocationAnalysisScratchSizeFn(Operation *op) {
if (op->hasAttr(AttrSharedMemPadded)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't that affect the codegen? I don't see any changes there?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For AMD we still use old pattern, which is using padded memory, so this should be safe.

@alefimov-amd
Copy link
Author

@ThomasRaoux hi

I wonder if this is something we should make common to all backends but I have to admit I don't understand how this controls code generation right now

This particular PR do not affect codegen, it affects only allocation analysis.
The problem I want to solve in this PR is to align analysis and codegen for AMD.

Analysis pessimistically allocates maximum memory from swizzled and padded memory: https://github.com/triton-lang/triton/blob/main/lib/Analysis/Allocation.cpp#L206

Codegen use old padded pattern for AMD: https://github.com/triton-lang/triton/blob/main/lib/Conversion/TritonGPUToLLVM/ConvertLayoutOpToLLVM.cpp#L287

@alefimov-amd
Copy link
Author

I wonder if this is something we should make common

I am not sure this should be in common code. It seems NVidia backend is mostly using swizzling for everything and gets rid of old pattern, that do not fit linear layout.

+cc @antiagainst

@ThomasRaoux
Copy link
Collaborator

@ThomasRaoux hi

I wonder if this is something we should make common to all backends but I have to admit I don't understand how this controls code generation right now

This particular PR do not affect codegen, it affects only allocation analysis. The problem I want to solve in this PR is to align analysis and codegen for AMD.

Analysis pessimistically allocates maximum memory from swizzled and padded memory: https://github.com/triton-lang/triton/blob/main/lib/Analysis/Allocation.cpp#L206

Codegen use old padded pattern for AMD: https://github.com/triton-lang/triton/blob/main/lib/Conversion/TritonGPUToLLVM/ConvertLayoutOpToLLVM.cpp#L287

ah I see, so the control should ensure that we use the padded pass in the lowering though? Otherwise we just rely on implicit assumptions.
No need to interrupt your work but maybe @lezcano has some ideas to generalize that a bit. I think it would be good to make it more explicit how the temp allocation is going to be made to avoid relying on implicit handshake.

@lezcano
Copy link
Contributor

lezcano commented Jun 27, 2025

Yes, in the nvidia backend we are never using padding. We always use swizzling or the stmatrix pass (which I'm working to integrate in the swizzling pass).

I would suggest AMD also uses swizzling if possible given that it's more efficient in general and it will allow to generate swizzled layouts that can be lowered with special instructions like ldmatrix/stmatrix (not sure if AMD has special instructions like these).

If this is not possible, AMD might want to end up moving the padding codegen to the AMD folder as this will not be needed for nvida.

auto scratchConfig = getScratchConfigForCvt(srcTy, dstTy);
elems = getNumScratchElements(scratchConfig.paddedRepShape);
} else {
// TODO use swizzling
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert that this path is not taken?

@alefimov-amd
Copy link
Author

If this is not possible, AMD might want to end up moving the padding codegen to the AMD folder as this will not be needed for nvida.

Right, this is what I want to implement for now. Keep padding related code in AMD backend, at least until we manage to lower memory consumption of swizzling pattern.


namespace mlir::triton::gpu {

void fillAllocationInfo(ModuleOp mod, ModuleAllocation &allocation);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe name it as attachAllocationSizeAndOffsetAttr is clearer. Also can you add documentation to this function?

#blocked1 = #ttg.blocked<{sizePerThread = [8, 4], threadsPerWarp = [8, 8], warpsPerCTA = [4, 1], order = [1, 0]}>
#blocked2 = #ttg.blocked<{sizePerThread = [1, 1], threadsPerWarp = [8, 8], warpsPerCTA = [4, 1], order = [1, 0]}>

// CHECK: ttg.shared = 36864 : i32
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain in comments how this number is computed so easier to understand/update later?

#blocked1 = #ttg.blocked<{sizePerThread = [8, 4], threadsPerWarp = [8, 8], warpsPerCTA = [4, 1], order = [1, 0]}>
#blocked2 = #ttg.blocked<{sizePerThread = [1, 1], threadsPerWarp = [8, 8], warpsPerCTA = [4, 1], order = [1, 0]}>

// CHECK: ttg.shared = 131072 : i32
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similary here.

Comment on lines +8 to +9

constexpr char AttrSharedMemPadded[] = "amdgpu.shared_mem_padded";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kPaddedScratchShmemAttrName[] = amdgpu.use_padded_scratch_shmem to be precise?

@antiagainst antiagainst marked this pull request as ready for review June 27, 2025 23:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants