Skip to content

Commit

Permalink
Increase __cilkrts_stack_frame alignment to ABI alignment of stack po…
Browse files Browse the repository at this point in the history
…inter.

All the __cilkrts_stack_frame objects are aligned on the stack.
There is no cost to increasing their alignment and the compiler
might generate better code.  This alignment is only used within
a module.  The runtime will use whatever value is declared in C code.
  • Loading branch information
VoxSciurorum committed Aug 11, 2024
1 parent 21d8fe4 commit 3497c51
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
7 changes: 6 additions & 1 deletion llvm/include/llvm/Transforms/Tapir/OpenCilkABI.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ class OpenCilkABI final : public TapirTarget {
FunctionCallee CilkRTSCilkForGrainsize32 = nullptr;
FunctionCallee CilkRTSCilkForGrainsize64 = nullptr;

MaybeAlign StackFrameAlign{8};
// The alignment of __cilkrts_stack_frame. This value will be increased
// to the ABI-prescribed stack pointer alignment of the module. This has
// no runtime cost. The bitcode file may request even greater alignment
// by defining a variable __cilkrts_stack_frame_align. This may have a
// runtime cost by forcing overalignment of stack pointers.
MaybeAlign StackFrameAlign;

// Accessors for CilkRTS ABI functions. When a bitcode file is loaded, these
// functions should return the function defined in the bitcode file.
Expand Down
13 changes: 10 additions & 3 deletions llvm/lib/Transforms/Tapir/OpenCilkABI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ void OpenCilkABI::prepareModule() {
}
if (GlobalVariable *AlignVar =
M.getGlobalVariable("__cilkrts_stack_frame_align", true)) {
// StackFrameAlign is undefined here.
StackFrameAlign = AlignVar->getAlign();
// Mark this variable with private linkage, to avoid linker failures when
// compiling with no optimizations.
Expand All @@ -328,6 +329,12 @@ void OpenCilkABI::prepareModule() {
if (StackFrameTy->isOpaque()) {
// Create a dummy __cilkrts_stack_frame structure
StackFrameTy->setBody(Int64Ty);
} else {
// Promote the stack frame structure alignment to the largest convenient
// value given the ABI.
Align ABIStackAlign = M.getDataLayout().getStackAlignment();
if (ABIStackAlign > StackFrameAlign.valueOrOne())
StackFrameAlign = ABIStackAlign;
}
// Create declarations of all CilkRTS functions, and add basic attributes to
// those declarations.
Expand Down Expand Up @@ -875,9 +882,9 @@ void OpenCilkABI::processSubTaskCall(TaskOutlineInfo &TOI, DominatorTree &DT) {
ConstantPointerNull::get(PointerType::getUnqual(C)));
ReplCall->setOperand(ParentSFArgNum, SF);
Argument *ParentSFArg = TOI.Outline->getArg(ParentSFArgNum);
ParentSFArg->addAttr(
Attribute::getWithAlignment(C, StackFrameAlign.valueOrOne()));

if (StackFrameAlign)
ParentSFArg->addAttr(
Attribute::getWithAlignment(C, StackFrameAlign.value()));
// Split the basic block containing the detach replacement just before the
// start of the detach-replacement instructions.
BasicBlock *DetBlock = ReplStart->getParent();
Expand Down

0 comments on commit 3497c51

Please sign in to comment.