Skip to content

Remove GlobalObject::getAlign/setAlignment #143188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions llvm/include/llvm/IR/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,21 @@ class LLVM_ABI Function : public GlobalObject, public ilist_node<Function> {
/// Return value: true => null pointer dereference is not undefined.
bool nullPointerIsDefined() const;

/// Returns the alignment of the given function.
///
/// Note that this is the alignment of the code, not the alignment of a
/// function pointer.
MaybeAlign getAlign() const { return GlobalObject::getAlign(); }

/// Sets the alignment attribute of the Function.
void setAlignment(Align Align) { GlobalObject::setAlignment(Align); }

/// Sets the alignment attribute of the Function.
///
/// This method will be deprecated as the alignment property should always be
/// defined.
void setAlignment(MaybeAlign Align) { GlobalObject::setAlignment(Align); }

private:
void allocHungoffUselist();
template<int Idx> void setHungoffOperand(Constant *C);
Expand Down
8 changes: 2 additions & 6 deletions llvm/include/llvm/IR/GlobalObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,7 @@ class GlobalObject : public GlobalValue {
public:
GlobalObject(const GlobalObject &) = delete;

/// FIXME: Remove this function once transition to Align is over.
uint64_t getAlignment() const {
MaybeAlign Align = getAlign();
return Align ? Align->value() : 0;
}

protected:
/// Returns the alignment of the given variable or function.
///
/// Note that for functions this is the alignment of the code, not the
Expand Down Expand Up @@ -103,6 +98,7 @@ class GlobalObject : public GlobalValue {
assert(getGlobalObjectSubClassData() == Val && "representation error");
}

public:
/// Check if this global has a custom object file section.
///
/// This is more efficient than calling getSection() and checking for an empty
Expand Down
17 changes: 17 additions & 0 deletions llvm/include/llvm/IR/GlobalVariable.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,23 @@ class GlobalVariable : public GlobalObject, public ilist_node<GlobalVariable> {
///
LLVM_ABI void clearCodeModel();

/// FIXME: Remove this function once transition to Align is over.
uint64_t getAlignment() const {
MaybeAlign Align = getAlign();
return Align ? Align->value() : 0;
}

/// Returns the alignment of the given variable.
MaybeAlign getAlign() const { return GlobalObject::getAlign(); }

/// Sets the alignment attribute of the GlobalVariable.
void setAlignment(Align Align) { GlobalObject::setAlignment(Align); }

/// Sets the alignment attribute of the GlobalVariable.
/// This method will be deprecated as the alignment property should always be
/// defined.
void setAlignment(MaybeAlign Align) { GlobalObject::setAlignment(Align); }

// Methods for support type inquiry through isa, cast, and dyn_cast:
static bool classof(const Value *V) {
return V->getValueID() == Value::GlobalVariableVal;
Expand Down
38 changes: 12 additions & 26 deletions llvm/include/llvm/SandboxIR/Constant.h
Original file line number Diff line number Diff line change
Expand Up @@ -976,32 +976,6 @@ class GlobalObject : public GlobalValue {
}
}

/// FIXME: Remove this function once transition to Align is over.
uint64_t getAlignment() const {
return cast<llvm::GlobalObject>(Val)->getAlignment();
}

/// Returns the alignment of the given variable or function.
///
/// Note that for functions this is the alignment of the code, not the
/// alignment of a function pointer.
MaybeAlign getAlign() const {
return cast<llvm::GlobalObject>(Val)->getAlign();
}

// TODO: Add missing: setAlignment(Align)

/// Sets the alignment attribute of the GlobalObject.
/// This method will be deprecated as the alignment property should always be
/// defined.
void setAlignment(MaybeAlign Align);

unsigned getGlobalObjectSubClassData() const {
return cast<llvm::GlobalObject>(Val)->getGlobalObjectSubClassData();
}

void setGlobalObjectSubClassData(unsigned V);

/// Check if this global has a custom object file section.
///
/// This is more efficient than calling getSection() and checking for an empty
Expand Down Expand Up @@ -1294,6 +1268,18 @@ class GlobalVariable final
return cast<llvm::GlobalVariable>(Val)->getCodeModel();
}

/// Returns the alignment of the given variable.
MaybeAlign getAlign() const {
return cast<llvm::GlobalVariable>(Val)->getAlign();
}

// TODO: Add missing: setAligment(Align)

/// Sets the alignment attribute of the GlobalVariable.
/// This method will be deprecated as the alignment property should always be
/// defined.
void setAlignment(MaybeAlign Align);

// TODO: Missing setCodeModel(). Requires custom tracker.

#ifndef NDEBUG
Expand Down
10 changes: 10 additions & 0 deletions llvm/include/llvm/SandboxIR/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ class Function : public GlobalWithNodeAPI<Function, llvm::Function,
}
FunctionType *getFunctionType() const;

/// Returns the alignment of the given function.
MaybeAlign getAlign() const { return cast<llvm::Function>(Val)->getAlign(); }

// TODO: Add missing: setAligment(Align)

/// Sets the alignment attribute of the Function.
/// This method will be deprecated as the alignment property should always be
/// defined.
void setAlignment(MaybeAlign Align);

#ifndef NDEBUG
void verify() const final {
assert(isa<llvm::Function>(Val) && "Expected Function!");
Expand Down
6 changes: 5 additions & 1 deletion llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,11 @@ Align AsmPrinter::getGVAlignment(const GlobalObject *GV, const DataLayout &DL,
Alignment = InAlign;

// If the GV has a specified alignment, take it into account.
const MaybeAlign GVAlign(GV->getAlign());
MaybeAlign GVAlign;
if (auto *GVar = dyn_cast<GlobalVariable>(GV))
GVAlign = GVar->getAlign();
else if (auto *F = dyn_cast<Function>(GV))
GVAlign = F->getAlign();
if (!GVAlign)
return Alignment;

Expand Down
8 changes: 6 additions & 2 deletions llvm/lib/IR/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2108,8 +2108,10 @@ LLVMTypeRef LLVMGlobalGetValueType(LLVMValueRef Global) {

unsigned LLVMGetAlignment(LLVMValueRef V) {
Value *P = unwrap(V);
if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(P))
return GV->getAlign() ? GV->getAlign()->value() : 0;
if (Function *F = dyn_cast<Function>(P))
return F->getAlign() ? F->getAlign()->value() : 0;
if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
return AI->getAlign().value();
if (LoadInst *LI = dyn_cast<LoadInst>(P))
Expand All @@ -2128,8 +2130,10 @@ unsigned LLVMGetAlignment(LLVMValueRef V) {

void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
Value *P = unwrap(V);
if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(P))
GV->setAlignment(MaybeAlign(Bytes));
else if (Function *F = dyn_cast<Function>(P))
F->setAlignment(MaybeAlign(Bytes));
else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
AI->setAlignment(Align(Bytes));
else if (LoadInst *LI = dyn_cast<LoadInst>(P))
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/IR/IRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,10 +457,10 @@ CallInst *IRBuilderBase::CreateInvariantStart(Value *Ptr, ConstantInt *Size) {
}

static MaybeAlign getAlign(Value *Ptr) {
if (auto *O = dyn_cast<GlobalObject>(Ptr))
return O->getAlign();
if (auto *V = dyn_cast<GlobalVariable>(Ptr))
return V->getAlign();
if (auto *A = dyn_cast<GlobalAlias>(Ptr))
return A->getAliaseeObject()->getAlign();
return getAlign(A->getAliaseeObject());
return {};
}

Expand Down
41 changes: 19 additions & 22 deletions llvm/lib/IR/Value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -957,30 +957,27 @@ uint64_t Value::getPointerDereferenceableBytes(const DataLayout &DL,

Align Value::getPointerAlignment(const DataLayout &DL) const {
assert(getType()->isPointerTy() && "must be pointer");
if (auto *GO = dyn_cast<GlobalObject>(this)) {
if (isa<Function>(GO)) {
Align FunctionPtrAlign = DL.getFunctionPtrAlign().valueOrOne();
switch (DL.getFunctionPtrAlignType()) {
case DataLayout::FunctionPtrAlignType::Independent:
return FunctionPtrAlign;
case DataLayout::FunctionPtrAlignType::MultipleOfFunctionAlign:
return std::max(FunctionPtrAlign, GO->getAlign().valueOrOne());
}
llvm_unreachable("Unhandled FunctionPtrAlignType");
if (const Function *F = dyn_cast<Function>(this)) {
Align FunctionPtrAlign = DL.getFunctionPtrAlign().valueOrOne();
switch (DL.getFunctionPtrAlignType()) {
case DataLayout::FunctionPtrAlignType::Independent:
return FunctionPtrAlign;
case DataLayout::FunctionPtrAlignType::MultipleOfFunctionAlign:
return std::max(FunctionPtrAlign, F->getAlign().valueOrOne());
}
const MaybeAlign Alignment(GO->getAlign());
llvm_unreachable("Unhandled FunctionPtrAlignType");
} else if (auto *GVar = dyn_cast<GlobalVariable>(this)) {
const MaybeAlign Alignment(GVar->getAlign());
if (!Alignment) {
if (auto *GVar = dyn_cast<GlobalVariable>(GO)) {
Type *ObjectType = GVar->getValueType();
if (ObjectType->isSized()) {
// If the object is defined in the current Module, we'll be giving
// it the preferred alignment. Otherwise, we have to assume that it
// may only have the minimum ABI alignment.
if (GVar->isStrongDefinitionForLinker())
return DL.getPreferredAlign(GVar);
else
return DL.getABITypeAlign(ObjectType);
}
Type *ObjectType = GVar->getValueType();
if (ObjectType->isSized()) {
// If the object is defined in the current Module, we'll be giving
// it the preferred alignment. Otherwise, we have to assume that it
// may only have the minimum ABI alignment.
if (GVar->isStrongDefinitionForLinker())
return DL.getPreferredAlign(GVar);
else
return DL.getABITypeAlign(ObjectType);
}
}
return Alignment.valueOrOne();
Expand Down
16 changes: 10 additions & 6 deletions llvm/lib/IR/Verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -735,12 +735,6 @@ void Verifier::visitGlobalValue(const GlobalValue &GV) {
"Global is external, but doesn't have external or weak linkage!", &GV);

if (const GlobalObject *GO = dyn_cast<GlobalObject>(&GV)) {

if (MaybeAlign A = GO->getAlign()) {
Check(A->value() <= Value::MaximumAlignment,
"huge alignment values are unsupported", GO);
}

if (const MDNode *Associated =
GO->getMetadata(LLVMContext::MD_associated)) {
Check(Associated->getNumOperands() == 1,
Expand Down Expand Up @@ -830,6 +824,11 @@ void Verifier::visitGlobalValue(const GlobalValue &GV) {
void Verifier::visitGlobalVariable(const GlobalVariable &GV) {
Type *GVType = GV.getValueType();

if (MaybeAlign A = GV.getAlign()) {
Check(A->value() <= Value::MaximumAlignment,
"huge alignment values are unsupported", &GV);
}

if (GV.hasInitializer()) {
Check(GV.getInitializer()->getType() == GVType,
"Global variable initializer type does not match global "
Expand Down Expand Up @@ -2869,6 +2868,11 @@ void Verifier::visitFunction(const Function &F) {
Check(!F.hasStructRetAttr() || F.getReturnType()->isVoidTy(),
"Invalid struct return type!", &F);

if (MaybeAlign A = F.getAlign()) {
Check(A->value() <= Value::MaximumAlignment,
"huge alignment values are unsupported", &F);
}

AttributeList Attrs = F.getAttributes();

Check(verifyAttributeCount(Attrs, FT->getNumParams()),
Expand Down
7 changes: 5 additions & 2 deletions llvm/lib/LTO/LTOModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,11 @@ void LTOModule::addDefinedFunctionSymbol(StringRef Name, const GlobalValue *F) {

void LTOModule::addDefinedSymbol(StringRef Name, const GlobalValue *def,
bool isFunction) {
const GlobalObject *go = dyn_cast<GlobalObject>(def);
uint32_t attr = go ? Log2(go->getAlign().valueOrOne()) : 0;
uint32_t attr = 0;
if (auto *gv = dyn_cast<GlobalVariable>(def))
attr = Log2(gv->getAlign().valueOrOne());
else if (auto *f = dyn_cast<Function>(def))
attr = Log2(f->getAlign().valueOrOne());

// set permissions part
if (isFunction) {
Expand Down
17 changes: 4 additions & 13 deletions llvm/lib/SandboxIR/Constant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,20 +282,11 @@ PoisonValue *PoisonValue::getElementValue(unsigned Idx) const {
cast<llvm::PoisonValue>(Val)->getElementValue(Idx)));
}

void GlobalObject::setAlignment(MaybeAlign Align) {
void GlobalVariable::setAlignment(MaybeAlign Align) {
Ctx.getTracker()
.emplaceIfTracking<
GenericSetter<&GlobalObject::getAlign, &GlobalObject::setAlignment>>(
this);
cast<llvm::GlobalObject>(Val)->setAlignment(Align);
}

void GlobalObject::setGlobalObjectSubClassData(unsigned V) {
Ctx.getTracker()
.emplaceIfTracking<
GenericSetter<&GlobalObject::getGlobalObjectSubClassData,
&GlobalObject::setGlobalObjectSubClassData>>(this);
cast<llvm::GlobalObject>(Val)->setGlobalObjectSubClassData(V);
.emplaceIfTracking<GenericSetter<&GlobalVariable::getAlign,
&GlobalVariable::setAlignment>>(this);
cast<llvm::GlobalVariable>(Val)->setAlignment(Align);
}

void GlobalObject::setSection(StringRef S) {
Expand Down
7 changes: 7 additions & 0 deletions llvm/lib/SandboxIR/Function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ FunctionType *Function::getFunctionType() const {
Ctx.getType(cast<llvm::Function>(Val)->getFunctionType()));
}

void Function::setAlignment(MaybeAlign Align) {
Ctx.getTracker()
.emplaceIfTracking<
GenericSetter<&Function::getAlign, &Function::setAlignment>>(this);
cast<llvm::Function>(Val)->setAlignment(Align);
}

#ifndef NDEBUG
void Function::dumpNameAndArgs(raw_ostream &OS) const {
auto *F = cast<llvm::Function>(Val);
Expand Down
7 changes: 4 additions & 3 deletions llvm/lib/Target/PowerPC/PPCMIPeephole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachinePostDominators.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/DebugCounter.h"

Expand Down Expand Up @@ -997,9 +998,9 @@ bool PPCMIPeephole::simplifyCode() {
// the transformation.
bool IsWordAligned = false;
if (SrcMI->getOperand(1).isGlobal()) {
const GlobalObject *GO =
dyn_cast<GlobalObject>(SrcMI->getOperand(1).getGlobal());
if (GO && GO->getAlign() && *GO->getAlign() >= 4 &&
const GlobalVariable *GV =
dyn_cast<GlobalVariable>(SrcMI->getOperand(1).getGlobal());
if (GV && GV->getAlign() && *GV->getAlign() >= 4 &&
(SrcMI->getOperand(1).getOffset() % 4 == 0))
IsWordAligned = true;
} else if (SrcMI->getOperand(1).isImm()) {
Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/Target/SystemZ/SystemZSubtarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "SystemZSubtarget.h"
#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/Target/TargetMachine.h"

using namespace llvm;
Expand Down Expand Up @@ -83,9 +83,9 @@ bool SystemZSubtarget::isAddressedViaADA(const GlobalValue *GV) const {
// least two byte alignment, then generated code can use relative
// instructions to address the variable. Otherwise, use the ADA to address
// the variable.
if (GO->getAlignment() & 0x1) {
return true;
}
if (auto *GV = dyn_cast<GlobalVariable>(GO))
if (GV->getAlign() && (*GV->getAlign()).value() & 0x1)
return true;

// getKindForGlobal only works with definitions
if (GO->isDeclaration()) {
Expand Down
Loading