This is an archive of the discontinued LLVM Phabricator instance.

[CodeGen] Support allocating of arguments by decreasing offsets
ClosedPublic

Authored by barannikov88 on May 1 2023, 2:00 AM.

Details

Summary

Previously, CCState::AllocateStack always allocated stack space by increasing
offsets. For targets with stack growing up (away from zero) it is more
convenient to allocate arguments by decreasing offsets, so that the first
argument is at the top of the stack. This is important when calling a function
with variable number of arguments: the callee does not know the size of the
stack, but must be able to access "fixed" arguments. For that to work, the
"fixed" arguments should have fixed offsets relative to the stack top, i.e. the
variadic arguments area should be at the stack bottom (at lowest addresses).

The in-tree target with stack growing up is AMDGPU, but it allocates
arguments by increasing addresses. It does not support variadic arguments.

A drive-by change is to promote stack size/offset to 64-bit integer.
This is what MachineFrameInfo expects.

Diff Detail

Event Timeline

barannikov88 created this revision.May 1 2023, 2:00 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 1 2023, 2:00 AM
barannikov88 retitled this revision from [CodeGen] Support allocating of arguments by decreasing addresses to [CodeGen] Support allocating of arguments by decreasing offsets.May 1 2023, 3:10 AM
barannikov88 edited the summary of this revision. (Show Details)
barannikov88 edited the summary of this revision. (Show Details)May 1 2023, 3:59 AM
barannikov88 edited the summary of this revision. (Show Details)May 1 2023, 4:01 AM
barannikov88 published this revision for review.May 1 2023, 4:06 AM
barannikov88 added reviewers: arsenm, yusra.syeda.
Herald added a project: Restricted Project. · View Herald TranscriptMay 1 2023, 4:07 AM
arsenm added inline comments.May 9 2023, 9:18 AM
llvm/include/llvm/CodeGen/CallingConvLower.h
177–178

Is this really any different from StackGrowsUp/Down?

barannikov88 edited the summary of this revision. (Show Details)May 9 2023, 9:49 AM
barannikov88 added inline comments.May 9 2023, 9:51 AM
llvm/include/llvm/CodeGen/CallingConvLower.h
177–178

NegativeOffsets == true should be equivalent to StackGrowsUp,
but this is not the case for AMDGPU, which allocates objects at positive offsets.
I remember you saying it may change in the future. If that happens, this flag can be removed.

(I've update the description, it used the wrong terms.)

arsenm accepted this revision.May 9 2023, 10:44 AM
arsenm added inline comments.
llvm/unittests/CodeGen/CCStateTest.cpp
38

These should use EXPECT_EQ with swapped arguments

This revision is now accepted and ready to land.May 9 2023, 10:44 AM
barannikov88 added inline comments.May 18 2023, 8:58 AM
llvm/unittests/CodeGen/CCStateTest.cpp
38

@arsenm
I used ASSERT_EQ because if one of the checks fails, the rest will most likely fail too. That is, there is little point in continuing.
About swapping the operands -- I vaguely remember it was the preferred style, but I think it is no longer relevant (https://groups.google.com/a/chromium.org/g/chromium-dev/c/3aQU5iM5Ov0).
There seems to be no consistency in LLVM unit tests regarding the order of operands.

I was planning to apply the suggestion when pushing anyway, but forgot. Sorry about that.