Skip to content

Conversation

@easyonaadit
Copy link
Contributor

@easyonaadit easyonaadit commented Jan 13, 2025

Currently, the AMDGPU backend sets up FP and then
increments SP by fixed size, from FP, in the prolog
and decrements it by the same amount in the epilog.
Prolog:
tmp = sp + (alignment - 1)
fp &= -alignment
sp += frameSize + alignment
Epilog:
sp -= (frameSize + alignment)

In the presence of dynamic alloca, this leads to incorrect restoration of SP.
This patch enforces the presence of a base pointer for all functions with dynamic allocas,
and SP is restored from the saved BP in the epilog.
Prolog:
tmp = sp + (alignment - 1)
fp &= -alignment
bp = sp
sp += frameSize + alignment
Epilog:
sp += bp + frameSize + alignment
sp -= (frameSize + alignment)

(Note: for dynamic allocas with default alignment, SP can be restored with saved FP as well.
However, for the sake of uniformity, presence of BP is enforced)

@easyonaadit easyonaadit changed the title [Draft] Restore SP in functions with dynamic allocas [Wip][Draft] Restore SP in functions with dynamic allocas Jan 13, 2025
@easyonaadit easyonaadit changed the title [Wip][Draft] Restore SP in functions with dynamic allocas [Wip][Draft] Restore SP correctly in functions with dynamic allocas Jan 13, 2025
Currently, the AMDGPU backend sets up FP and then
increments SP by fixed size, from FP, in the prolog
and decrements it by the same amount in the epilog.
Prolog:
tmp = sp + (alignment - 1)
fp &= -alignment
sp += frameSize + alignment
Epilog:
sp -= (frameSize + alignment)

In the presence of dynamic alloca, this leads to incorrect restoration of SP.
This patch enforces the presence of a base pointer for all functions with dynamic allocas,
and SP is restored from the saved BP in the epilog.
Prolog:
tmp = sp + (alignment - 1)
fp &= -alignment
bp = sp
sp += frameSize + alignment
Epilog:
sp += bp + frameSize + alignment
sp -= (frameSize + alignment)

(Note: for dynamic allocas with default alignment, SP can be restored with saved FP as well.
However, for the sake of uniformity, presence of BP is enforced)

Fixes: SWDEV-408164
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.

1 participant