Skip to content

Commit 4d7a0ab

Browse files
authored
[DataLayout] Change return type of getStackAlignment to MaybeAlign (#105478)
Currently, `getStackAlignment` asserts if the stack alignment wasn't specified. This makes it inconvenient to use and complicates testing. This change also makes `exceedsNaturalStackAlignment` method redundant.
1 parent 29bb523 commit 4d7a0ab

File tree

10 files changed

+51
-34
lines changed

10 files changed

+51
-34
lines changed

llvm/include/llvm/IR/DataLayout.h

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,9 @@ class DataLayout {
220220

221221
bool isIllegalInteger(uint64_t Width) const { return !isLegalInteger(Width); }
222222

223-
/// Returns true if the given alignment exceeds the natural stack alignment.
224-
bool exceedsNaturalStackAlignment(Align Alignment) const {
225-
return StackNaturalAlign && (Alignment > *StackNaturalAlign);
226-
}
227-
228-
Align getStackAlignment() const {
229-
assert(StackNaturalAlign && "StackNaturalAlign must be defined");
230-
return *StackNaturalAlign;
231-
}
223+
/// Returns the natural stack alignment, or MaybeAlign() if one wasn't
224+
/// specified.
225+
MaybeAlign getStackAlignment() const { return StackNaturalAlign; }
232226

233227
unsigned getAllocaAddrSpace() const { return AllocaAddrSpace; }
234228

llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9136,8 +9136,8 @@ LegalizerHelper::lowerMemcpy(MachineInstr &MI, Register Dst, Register Src,
91369136
// realignment.
91379137
const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
91389138
if (!TRI->hasStackRealignment(MF))
9139-
while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign))
9140-
NewAlign = NewAlign.previous();
9139+
if (MaybeAlign StackAlign = DL.getStackAlignment())
9140+
NewAlign = std::min(NewAlign, *StackAlign);
91419141

91429142
if (NewAlign > Alignment) {
91439143
Alignment = NewAlign;
@@ -9244,8 +9244,8 @@ LegalizerHelper::lowerMemmove(MachineInstr &MI, Register Dst, Register Src,
92449244
// realignment.
92459245
const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
92469246
if (!TRI->hasStackRealignment(MF))
9247-
while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign))
9248-
NewAlign = NewAlign.previous();
9247+
if (MaybeAlign StackAlign = DL.getStackAlignment())
9248+
NewAlign = std::min(NewAlign, *StackAlign);
92499249

92509250
if (NewAlign > Alignment) {
92519251
Alignment = NewAlign;

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7896,8 +7896,8 @@ static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl,
78967896
// optimization.
78977897
const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
78987898
if (!TRI->hasStackRealignment(MF))
7899-
while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign))
7900-
NewAlign = NewAlign.previous();
7899+
if (MaybeAlign StackAlign = DL.getStackAlignment())
7900+
NewAlign = std::min(NewAlign, *StackAlign);
79017901

79027902
if (NewAlign > Alignment) {
79037903
// Give the stack frame object a larger alignment if needed.
@@ -8091,8 +8091,8 @@ static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl,
80918091
// optimization.
80928092
const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
80938093
if (!TRI->hasStackRealignment(MF))
8094-
while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign))
8095-
NewAlign = NewAlign.previous();
8094+
if (MaybeAlign StackAlign = DL.getStackAlignment())
8095+
NewAlign = std::min(NewAlign, *StackAlign);
80968096

80978097
if (NewAlign > Alignment) {
80988098
// Give the stack frame object a larger alignment if needed.
@@ -8209,8 +8209,8 @@ static SDValue getMemsetStores(SelectionDAG &DAG, const SDLoc &dl,
82098209
// optimization.
82108210
const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
82118211
if (!TRI->hasStackRealignment(MF))
8212-
while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign))
8213-
NewAlign = NewAlign.previous();
8212+
if (MaybeAlign StackAlign = DL.getStackAlignment())
8213+
NewAlign = std::min(NewAlign, *StackAlign);
82148214

82158215
if (NewAlign > Alignment) {
82168216
// Give the stack frame object a larger alignment if needed.

llvm/lib/Target/AArch64/AArch64CallingConvention.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,11 @@ static bool CC_AArch64_Custom_Block(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
209209
State.AllocateReg(Reg);
210210
}
211211

212-
const Align StackAlign =
212+
const MaybeAlign StackAlign =
213213
State.getMachineFunction().getDataLayout().getStackAlignment();
214+
assert(StackAlign && "data layout string is missing stack alignment");
214215
const Align MemAlign = ArgFlags.getNonZeroMemAlign();
215-
Align SlotAlign = std::min(MemAlign, StackAlign);
216+
Align SlotAlign = std::min(MemAlign, *StackAlign);
216217
if (!Subtarget.isTargetDarwin())
217218
SlotAlign = std::max(SlotAlign, Align(8));
218219

llvm/lib/Target/ARM/ARMCallingConv.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,10 @@ static bool CC_ARM_AAPCS_Custom_Aggregate(unsigned ValNo, MVT ValVT,
190190
// Try to allocate a contiguous block of registers, each of the correct
191191
// size to hold one member.
192192
auto &DL = State.getMachineFunction().getDataLayout();
193-
const Align StackAlign = DL.getStackAlignment();
193+
const MaybeAlign StackAlign = DL.getStackAlignment();
194+
assert(StackAlign && "data layout string is missing stack alignment");
194195
const Align FirstMemberAlign(PendingMembers[0].getExtraInfo());
195-
Align Alignment = std::min(FirstMemberAlign, StackAlign);
196+
Align Alignment = std::min(FirstMemberAlign, *StackAlign);
196197

197198
ArrayRef<MCPhysReg> RegList;
198199
switch (LocVT.SimpleTy) {

llvm/lib/Target/ARM/ARMISelLowering.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2461,8 +2461,9 @@ ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
24612461

24622462
// Since callee will pop argument stack as a tail call, we must keep the
24632463
// popped size 16-byte aligned.
2464-
Align StackAlign = DAG.getDataLayout().getStackAlignment();
2465-
NumBytes = alignTo(NumBytes, StackAlign);
2464+
MaybeAlign StackAlign = DAG.getDataLayout().getStackAlignment();
2465+
assert(StackAlign && "data layout string is missing stack alignment");
2466+
NumBytes = alignTo(NumBytes, *StackAlign);
24662467

24672468
// SPDiff will be negative if this tail call requires more space than we
24682469
// would automatically have in our incoming argument space. Positive if we
@@ -4711,8 +4712,9 @@ SDValue ARMTargetLowering::LowerFormalArguments(
47114712
if (canGuaranteeTCO(CallConv, TailCallOpt)) {
47124713
// The only way to guarantee a tail call is if the callee restores its
47134714
// argument area, but it must also keep the stack aligned when doing so.
4714-
const DataLayout &DL = DAG.getDataLayout();
4715-
StackArgSize = alignTo(StackArgSize, DL.getStackAlignment());
4715+
MaybeAlign StackAlign = DAG.getDataLayout().getStackAlignment();
4716+
assert(StackAlign && "data layout string is missing stack alignment");
4717+
StackArgSize = alignTo(StackArgSize, *StackAlign);
47164718

47174719
AFI->setArgumentStackToRestore(StackArgSize);
47184720
}
@@ -22033,7 +22035,9 @@ Align ARMTargetLowering::getABIAlignmentForCallingConv(
2203322035

2203422036
// Avoid over-aligning vector parameters. It would require realigning the
2203522037
// stack and waste space for no real benefit.
22036-
return std::min(ABITypeAlign, DL.getStackAlignment());
22038+
MaybeAlign StackAlign = DL.getStackAlignment();
22039+
assert(StackAlign && "data layout string is missing stack alignment");
22040+
return std::min(ABITypeAlign, *StackAlign);
2203722041
}
2203822042

2203922043
/// Return true if a type is an AAPCS-VFP homogeneous aggregate or one of

llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,8 +1189,9 @@ WebAssemblyTargetLowering::LowerCall(CallLoweringInfo &CLI,
11891189
if (IsVarArg && NumBytes) {
11901190
// For non-fixed arguments, next emit stores to store the argument values
11911191
// to the stack buffer at the offsets computed above.
1192-
int FI = MF.getFrameInfo().CreateStackObject(NumBytes,
1193-
Layout.getStackAlignment(),
1192+
MaybeAlign StackAlign = Layout.getStackAlignment();
1193+
assert(StackAlign && "data layout string is missing stack alignment");
1194+
int FI = MF.getFrameInfo().CreateStackObject(NumBytes, *StackAlign,
11941195
/*isSS=*/false);
11951196
unsigned ValNo = 0;
11961197
SmallVector<SDValue, 8> Chains;

llvm/lib/Transforms/IPO/ExpandVariadics.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -748,10 +748,10 @@ bool ExpandVariadics::expandCall(Module &M, IRBuilder<> &Builder, CallBase *CB,
748748
// This is an awkward way to guess whether there is a known stack alignment
749749
// without hitting an assert in DL.getStackAlignment, 1024 is an arbitrary
750750
// number likely to be greater than the natural stack alignment.
751-
// TODO: DL.getStackAlignment could return a MaybeAlign instead of assert
752751
Align AllocaAlign = MaxFieldAlign;
753-
if (DL.exceedsNaturalStackAlignment(Align(1024)))
754-
AllocaAlign = std::max(AllocaAlign, DL.getStackAlignment());
752+
if (MaybeAlign StackAlign = DL.getStackAlignment();
753+
StackAlign && *StackAlign > AllocaAlign)
754+
AllocaAlign = *StackAlign;
755755

756756
// Put the alloca to hold the variadic args in the entry basic block.
757757
Builder.SetInsertPointPastAllocas(CBF);

llvm/lib/Transforms/Utils/Local.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1506,7 +1506,8 @@ Align llvm::tryEnforceAlignment(Value *V, Align PrefAlign,
15061506

15071507
// If the preferred alignment is greater than the natural stack alignment
15081508
// then don't round up. This avoids dynamic stack realignment.
1509-
if (DL.exceedsNaturalStackAlignment(PrefAlign))
1509+
MaybeAlign StackAlign = DL.getStackAlignment();
1510+
if (StackAlign && PrefAlign > *StackAlign)
15101511
return CurrentAlign;
15111512
AI->setAlignment(PrefAlign);
15121513
return PrefAlign;

llvm/unittests/IR/DataLayoutTest.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,21 @@ TEST(DataLayout, ParseNonIntegralAddrSpace) {
444444
FailedWithMessage("address space 0 cannot be non-integral"));
445445
}
446446

447+
TEST(DataLayout, GetStackAlignment) {
448+
DataLayout Default;
449+
EXPECT_FALSE(Default.getStackAlignment().has_value());
450+
451+
std::pair<StringRef, Align> Cases[] = {
452+
{"S8", Align(1)},
453+
{"S64", Align(8)},
454+
{"S32768", Align(4096)},
455+
};
456+
for (auto [Layout, Val] : Cases) {
457+
DataLayout DL = cantFail(DataLayout::parse(Layout));
458+
EXPECT_EQ(DL.getStackAlignment(), Val) << Layout;
459+
}
460+
}
461+
447462
TEST(DataLayout, GetPointerSizeInBits) {
448463
std::tuple<StringRef, unsigned, unsigned, unsigned> Cases[] = {
449464
{"", 64, 64, 64},

0 commit comments

Comments
 (0)