Skip to content

Commit

Permalink
[IR] Increase max alignment to 4GB
Browse files Browse the repository at this point in the history
Currently the max alignment representable is 1GB, see D108661.
Setting the align of an object to 4GB is desirable in some cases to make sure the lower 32 bits are clear which can be used for some optimizations, e.g. https://crbug.com/1016945.

This uses an extra bit in instructions that carry an alignment. We can store 15 bits of "free" information, and with this change some instructions (e.g. AtomicCmpXchgInst) use 14 bits.
We can increase the max alignment representable above 4GB (up to 2^62) since we're only using 33 of the 64 values, but I've just limited it to 4GB for now.

The one place we have to update the bitcode format is for the alloca instruction. It stores its alignment into 5 bits of a 32 bit bitfield. I've added another field which is 8 bits and should be future proof for a while. For backward compatibility, we check if the old field has a value and use that, otherwise use the new field.

Updating clang's max allowed alignment will come in a future patch.

Reviewed By: hans

Differential Revision: https://reviews.llvm.org/D110451
  • Loading branch information
aeubanks committed Oct 6, 2021
1 parent afdac5f commit df84c1f
Show file tree
Hide file tree
Showing 48 changed files with 161 additions and 152 deletions.
4 changes: 2 additions & 2 deletions clang/lib/CodeGen/CGBlocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2695,8 +2695,8 @@ const BlockByrefInfo &CodeGenFunction::getBlockByrefInfo(const VarDecl *D) {
size = varOffset;

// Conversely, we might have to prevent LLVM from inserting padding.
} else if (CGM.getDataLayout().getABITypeAlignment(varTy)
> varAlign.getQuantity()) {
} else if (CGM.getDataLayout().getABITypeAlignment(varTy) >
uint64_t(varAlign.getQuantity())) {
packed = true;
}
types.push_back(varTy);
Expand Down
12 changes: 6 additions & 6 deletions clang/lib/CodeGen/CGCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5021,12 +5021,12 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
auto scalarAlign = CGM.getDataLayout().getPrefTypeAlignment(scalarType);

// Materialize to a temporary.
addr = CreateTempAlloca(
RV.getScalarVal()->getType(),
CharUnits::fromQuantity(std::max(
(unsigned)layout->getAlignment().value(), scalarAlign)),
"tmp",
/*ArraySize=*/nullptr, &AllocaAddr);
addr =
CreateTempAlloca(RV.getScalarVal()->getType(),
CharUnits::fromQuantity(std::max(
layout->getAlignment().value(), scalarAlign)),
"tmp",
/*ArraySize=*/nullptr, &AllocaAddr);
tempSize = EmitLifetimeStart(scalarSize, AllocaAddr.getPointer());

Builder.CreateStore(RV.getScalarVal(), addr);
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CGDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1142,7 +1142,7 @@ Address CodeGenModule::createUnnamedGlobalFrom(const VarDecl &D,
GV->setAlignment(Align.getAsAlign());
GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
CacheEntry = GV;
} else if (CacheEntry->getAlignment() < Align.getQuantity()) {
} else if (CacheEntry->getAlignment() < uint64_t(Align.getQuantity())) {
CacheEntry->setAlignment(Align.getAsAlign());
}

Expand Down
4 changes: 2 additions & 2 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5386,7 +5386,7 @@ CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S,
if (!LangOpts.WritableStrings) {
Entry = &ConstantStringMap[C];
if (auto GV = *Entry) {
if (Alignment.getQuantity() > GV->getAlignment())
if (uint64_t(Alignment.getQuantity()) > GV->getAlignment())
GV->setAlignment(Alignment.getAsAlign());
return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV),
Alignment);
Expand Down Expand Up @@ -5449,7 +5449,7 @@ ConstantAddress CodeGenModule::GetAddrOfConstantCString(
if (!LangOpts.WritableStrings) {
Entry = &ConstantStringMap[C];
if (auto GV = *Entry) {
if (Alignment.getQuantity() > GV->getAlignment())
if (uint64_t(Alignment.getQuantity()) > GV->getAlignment())
GV->setAlignment(Alignment.getAsAlign());
return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV),
Alignment);
Expand Down
8 changes: 6 additions & 2 deletions llvm/include/llvm/Bitcode/BitcodeCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@
namespace llvm {

struct AllocaPackedValues {
using Align = Bitfield::Element<unsigned, 0, 5>;
using UsedWithInAlloca = Bitfield::Element<bool, Align::NextBit, 1>;
// We increased the number of bits needed to represent alignment to be more
// than 5, but to preserve backward compatibility we store the upper bits
// separately.
using AlignLower = Bitfield::Element<unsigned, 0, 5>;
using UsedWithInAlloca = Bitfield::Element<bool, AlignLower::NextBit, 1>;
using ExplicitType = Bitfield::Element<bool, UsedWithInAlloca::NextBit, 1>;
using SwiftError = Bitfield::Element<bool, ExplicitType::NextBit, 1>;
using AlignUpper = Bitfield::Element<unsigned, SwiftError::NextBit, 3>;
};

} // namespace llvm
Expand Down
4 changes: 2 additions & 2 deletions llvm/include/llvm/IR/DataLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ class DataLayout {

/// Returns the minimum ABI-required alignment for the specified type.
/// FIXME: Deprecate this function once migration to Align is over.
unsigned getABITypeAlignment(Type *Ty) const;
uint64_t getABITypeAlignment(Type *Ty) const;

/// Returns the minimum ABI-required alignment for the specified type.
Align getABITypeAlign(Type *Ty) const;
Expand All @@ -541,7 +541,7 @@ class DataLayout {
///
/// This is always at least as good as the ABI alignment.
/// FIXME: Deprecate this function once migration to Align is over.
unsigned getPrefTypeAlignment(Type *Ty) const;
uint64_t getPrefTypeAlignment(Type *Ty) const;

/// Returns the preferred stack/global alignment for the specified
/// type.
Expand Down
4 changes: 2 additions & 2 deletions llvm/include/llvm/IR/GlobalObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class GlobalObject : public GlobalValue {

Comdat *ObjComdat;
enum {
LastAlignmentBit = 4,
LastAlignmentBit = 5,
HasSectionHashEntryBit,

GlobalObjectBits,
Expand All @@ -68,7 +68,7 @@ class GlobalObject : public GlobalValue {
GlobalObject(const GlobalObject &) = delete;

/// FIXME: Remove this function once transition to Align is over.
unsigned getAlignment() const {
uint64_t getAlignment() const {
MaybeAlign Align = getAlign();
return Align ? Align->value() : 0;
}
Expand Down
4 changes: 2 additions & 2 deletions llvm/include/llvm/IR/Instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ class Instruction : public User,
// Template alias so that all Instruction storing alignment use the same
// definiton.
// Valid alignments are powers of two from 2^0 to 2^MaxAlignmentExponent =
// 2^30. We store them as Log2(Alignment), so we need 5 bits to encode the 31
// 2^32. We store them as Log2(Alignment), so we need 6 bits to encode the 33
// possible values.
template <unsigned Offset>
using AlignmentBitfieldElementT =
typename Bitfield::Element<unsigned, Offset, 5,
typename Bitfield::Element<unsigned, Offset, 6,
Value::MaxAlignmentExponent>;

template <unsigned Offset>
Expand Down
6 changes: 3 additions & 3 deletions llvm/include/llvm/IR/Instructions.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class AllocaInst : public UnaryInstruction {
}

// FIXME: Remove this one transition to Align is over.
unsigned getAlignment() const { return getAlign().value(); }
uint64_t getAlignment() const { return getAlign().value(); }

/// Return true if this alloca is in the entry block of the function and is a
/// constant size. If so, the code generator will fold it into the
Expand Down Expand Up @@ -217,7 +217,7 @@ class LoadInst : public UnaryInstruction {
/// Return the alignment of the access that is being performed.
/// FIXME: Remove this function once transition to Align is over.
/// Use getAlign() instead.
unsigned getAlignment() const { return getAlign().value(); }
uint64_t getAlignment() const { return getAlign().value(); }

/// Return the alignment of the access that is being performed.
Align getAlign() const {
Expand Down Expand Up @@ -348,7 +348,7 @@ class StoreInst : public Instruction {
/// Return the alignment of the access that is being performed
/// FIXME: Remove this function once transition to Align is over.
/// Use getAlign() instead.
unsigned getAlignment() const { return getAlign().value(); }
uint64_t getAlignment() const { return getAlign().value(); }

Align getAlign() const {
return Align(1ULL << (getSubclassData<AlignmentField>()));
Expand Down
4 changes: 2 additions & 2 deletions llvm/include/llvm/IR/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -785,8 +785,8 @@ class Value {
///
/// This is the greatest alignment value supported by load, store, and alloca
/// instructions, and global values.
static constexpr unsigned MaxAlignmentExponent = 30;
static constexpr unsigned MaximumAlignment = 1u << MaxAlignmentExponent;
static constexpr unsigned MaxAlignmentExponent = 32;
static constexpr uint64_t MaximumAlignment = 1ULL << MaxAlignmentExponent;

/// Mutate the type of this Value to be of the specified type.
///
Expand Down
6 changes: 3 additions & 3 deletions llvm/include/llvm/Transforms/IPO/Attributor.h
Original file line number Diff line number Diff line change
Expand Up @@ -3413,18 +3413,18 @@ struct AADereferenceable
};

using AAAlignmentStateType =
IncIntegerState<uint32_t, Value::MaximumAlignment, 1>;
IncIntegerState<uint64_t, Value::MaximumAlignment, 1>;
/// An abstract interface for all align attributes.
struct AAAlign : public IRAttribute<
Attribute::Alignment,
StateWrapper<AAAlignmentStateType, AbstractAttribute>> {
AAAlign(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}

/// Return assumed alignment.
unsigned getAssumedAlign() const { return getAssumed(); }
uint64_t getAssumedAlign() const { return getAssumed(); }

/// Return known alignment.
unsigned getKnownAlign() const { return getKnown(); }
uint64_t getKnownAlign() const { return getKnown(); }

/// See AbstractAttribute::getName()
const std::string getName() const override { return "AAAlign"; }
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/AsmParser/LLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1927,7 +1927,7 @@ bool LLParser::parseOptionalAlignment(MaybeAlign &Alignment, bool AllowParens) {
if (!EatIfPresent(lltok::kw_align))
return false;
LocTy AlignLoc = Lex.getLoc();
uint32_t Value = 0;
uint64_t Value = 0;

LocTy ParenLoc = Lex.getLoc();
bool HaveParens = false;
Expand All @@ -1936,13 +1936,13 @@ bool LLParser::parseOptionalAlignment(MaybeAlign &Alignment, bool AllowParens) {
HaveParens = true;
}

if (parseUInt32(Value))
if (parseUInt64(Value))
return true;

if (HaveParens && !EatIfPresent(lltok::rparen))
return error(ParenLoc, "expected ')'");

if (!isPowerOf2_32(Value))
if (!isPowerOf2_64(Value))
return error(AlignLoc, "alignment is not a power of two");
if (Value > Value::MaximumAlignment)
return error(AlignLoc, "huge alignments are not supported yet");
Expand Down
6 changes: 4 additions & 2 deletions llvm/lib/Bitcode/Reader/BitcodeReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4930,8 +4930,10 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
Type *OpTy = getTypeByID(Record[1]);
Value *Size = getFnValueByID(Record[2], OpTy);
MaybeAlign Align;
if (Error Err =
parseAlignmentValue(Bitfield::get<APV::Align>(Rec), Align)) {
uint64_t AlignExp =
Bitfield::get<APV::AlignLower>(Rec) |
(Bitfield::get<APV::AlignUpper>(Rec) << APV::AlignLower::Bits);
if (Error Err = parseAlignmentValue(AlignExp, Align)) {
return Err;
}
if (!Ty || !Size)
Expand Down
6 changes: 5 additions & 1 deletion llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3055,7 +3055,11 @@ void ModuleBitcodeWriter::writeInstruction(const Instruction &I,
Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
using APV = AllocaPackedValues;
unsigned Record = 0;
Bitfield::set<APV::Align>(Record, getEncodedAlign(AI.getAlign()));
unsigned EncodedAlign = getEncodedAlign(AI.getAlign());
Bitfield::set<APV::AlignLower>(
Record, EncodedAlign & ((1 << APV::AlignLower::Bits) - 1));
Bitfield::set<APV::AlignUpper>(Record,
EncodedAlign >> APV::AlignLower::Bits);
Bitfield::set<APV::UsedWithInAlloca>(Record, AI.isUsedWithInAlloca());
Bitfield::set<APV::ExplicitType>(Record, true);
Bitfield::set<APV::SwiftError>(Record, AI.isSwiftError());
Expand Down
12 changes: 6 additions & 6 deletions llvm/lib/CodeGen/MIRParser/MIParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ class MIParser {
MachineOperand &Dest,
Optional<unsigned> &TiedDefIdx);
bool parseOffset(int64_t &Offset);
bool parseAlignment(unsigned &Alignment);
bool parseAlignment(uint64_t &Alignment);
bool parseAddrspace(unsigned &Addrspace);
bool parseSectionID(Optional<MBBSectionID> &SID);
bool parseOperandsOffset(MachineOperand &Op);
Expand Down Expand Up @@ -676,7 +676,7 @@ bool MIParser::parseBasicBlockDefinition(
bool IsLandingPad = false;
bool IsEHFuncletEntry = false;
Optional<MBBSectionID> SectionID;
unsigned Alignment = 0;
uint64_t Alignment = 0;
BasicBlock *BB = nullptr;
if (consumeIfPresent(MIToken::lparen)) {
do {
Expand Down Expand Up @@ -2898,16 +2898,16 @@ bool MIParser::parseOffset(int64_t &Offset) {
return false;
}

bool MIParser::parseAlignment(unsigned &Alignment) {
bool MIParser::parseAlignment(uint64_t &Alignment) {
assert(Token.is(MIToken::kw_align) || Token.is(MIToken::kw_basealign));
lex();
if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
return error("expected an integer literal after 'align'");
if (getUnsigned(Alignment))
if (getUint64(Alignment))
return true;
lex();

if (!isPowerOf2_32(Alignment))
if (!isPowerOf2_64(Alignment))
return error("expected a power-of-2 literal after 'align'");

return false;
Expand Down Expand Up @@ -3261,7 +3261,7 @@ bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
if (parseMachinePointerInfo(Ptr))
return true;
}
unsigned BaseAlignment =
uint64_t BaseAlignment =
(Size != MemoryLocation::UnknownSize ? PowerOf2Ceil(Size) : 1);
AAMDNodes AAInfo;
MDNode *Range = nullptr;
Expand Down
11 changes: 5 additions & 6 deletions llvm/lib/CodeGen/SafeStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class SafeStack {
///
/// 16 seems like a reasonable upper bound on the alignment of objects that we
/// might expect to appear on the stack on most common targets.
enum { StackAlignment = 16 };
static constexpr uint64_t StackAlignment = 16;

/// Return the value of the stack canary.
Value *getStackGuard(IRBuilder<> &IRB, Function &F);
Expand Down Expand Up @@ -544,8 +544,7 @@ Value *SafeStack::moveStaticAllocasToUnsafeStack(
Size = 1; // Don't create zero-sized stack objects.

// Ensure the object is properly aligned.
unsigned Align =
std::max((unsigned)DL.getPrefTypeAlignment(Ty), AI->getAlignment());
uint64_t Align = std::max(DL.getPrefTypeAlignment(Ty), AI->getAlignment());

SSL.addObject(AI, Size, Align,
ClColoring ? SSC.getLiveRange(AI) : NoColoringRange);
Expand Down Expand Up @@ -676,9 +675,9 @@ void SafeStack::moveDynamicAllocasToUnsafeStack(
SP = IRB.CreateSub(SP, Size);

// Align the SP value to satisfy the AllocaInst, type and stack alignments.
unsigned Align = std::max(
std::max((unsigned)DL.getPrefTypeAlignment(Ty), AI->getAlignment()),
(unsigned)StackAlignment);
uint64_t Align =
std::max(std::max(DL.getPrefTypeAlignment(Ty), AI->getAlignment()),
StackAlignment);

assert(isPowerOf2_32(Align));
Value *NewTop = IRB.CreateIntToPtr(
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/IR/DataLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ Align DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const {
}

/// TODO: Remove this function once the transition to Align is over.
unsigned DataLayout::getABITypeAlignment(Type *Ty) const {
uint64_t DataLayout::getABITypeAlignment(Type *Ty) const {
return getABITypeAlign(Ty).value();
}

Expand All @@ -828,7 +828,7 @@ Align DataLayout::getABITypeAlign(Type *Ty) const {
}

/// TODO: Remove this function once the transition to Align is over.
unsigned DataLayout::getPrefTypeAlignment(Type *Ty) const {
uint64_t DataLayout::getPrefTypeAlignment(Type *Ty) const {
return getPrefTypeAlign(Ty).value();
}

Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ const char kAMDGPUAddressPrivateName[] = "llvm.amdgcn.is.private";
// Accesses sizes are powers of two: 1, 2, 4, 8, 16.
static const size_t kNumberOfAccessSizes = 5;

static const unsigned kAllocaRzSize = 32;
static const uint64_t kAllocaRzSize = 32;

// ASanAccessInfo implementation constants.
constexpr size_t kCompileKernelShift = 0;
Expand Down Expand Up @@ -3578,7 +3578,7 @@ void FunctionStackPoisoner::poisonAlloca(Value *V, uint64_t Size,
void FunctionStackPoisoner::handleDynamicAllocaCall(AllocaInst *AI) {
IRBuilder<> IRB(AI);

const unsigned Alignment = std::max(kAllocaRzSize, AI->getAlignment());
const uint64_t Alignment = std::max(kAllocaRzSize, AI->getAlignment());
const uint64_t AllocaRedzoneMask = kAllocaRzSize - 1;

Value *Zero = Constant::getNullValue(IntptrTy);
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ class HWAddressSanitizer {
bool WithFrameRecord;

void init(Triple &TargetTriple, bool InstrumentWithCalls);
unsigned getObjectAlignment() const { return 1U << Scale; }
uint64_t getObjectAlignment() const { return 1ULL << Scale; }
};

ShadowMapping Mapping;
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Assembler/align-inst-alloca.ll
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
; RUN: not llvm-as %s -o /dev/null 2>/dev/null

define void @foo() {
%p = alloca i1, align 2147483648
%p = alloca i1, align 8589934592
ret void
}
2 changes: 1 addition & 1 deletion llvm/test/Assembler/align-inst-load.ll
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
; RUN: not llvm-as %s -o /dev/null 2>/dev/null

define void @foo(i1* %p) {
load i1, i1* %p, align 2147483648
load i1, i1* %p, align 8589934592
ret void
}
2 changes: 1 addition & 1 deletion llvm/test/Assembler/align-inst-store.ll
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
; RUN: not llvm-as %s -o /dev/null 2>/dev/null

define void @foo(i1* %p) {
store i1 false, i1* %p, align 2147483648
store i1 false, i1* %p, align 8589934592
ret void
}
8 changes: 4 additions & 4 deletions llvm/test/Assembler/align-inst.ll
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
; RUN: llvm-as %s -o /dev/null
; RUN: verify-uselistorder %s

@A = global i1 0, align 1073741824
@A = global i1 0, align 4294967296

define void @foo() {
%p = alloca i1, align 1073741824
load i1, i1* %p, align 1073741824
store i1 false, i1* %p, align 1073741824
%p = alloca i1, align 4294967296
load i1, i1* %p, align 4294967296
store i1 false, i1* %p, align 4294967296
ret void
}
Binary file modified llvm/test/Bitcode/Inputs/invalid-align.bc
Binary file not shown.
Loading

0 comments on commit df84c1f

Please sign in to comment.