Skip to content

Commit 6f5d913

Browse files
committed
OpaquePtr: Don't check pointee type for byval/preallocated
Since none of these users really care about the actual type, hide the type under a new size-getting attribute to go along with hasPassPointeeByValueAttr. This will work better for the future byref attribute, which may end up only tracking the byte size and not the IR type. We currently have 3 parameter attributes that should carry the type (technically inalloca does not yet). The APIs are somewhat awkward since preallocated/inalloca piggyback on byval in some places, but in others are treated as distinct attributes. Since these are all mutually exclusive, we should probably just merge all the attribute infrastructure treating these as totally distinct attributes.
1 parent d12d0b7 commit 6f5d913

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

llvm/include/llvm/IR/Argument.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ class Argument final : public Value {
7575
/// attribute. These attributes represent arguments being passed by value.
7676
bool hasPassPointeeByValueAttr() const;
7777

78+
/// If this argument satisfies has hasPassPointeeByValueAttr, return the
79+
/// in-memory ABI size copied to the stack for the call. Otherwise, return 0.
80+
uint64_t getPassPointeeByValueCopySize(const DataLayout &DL) const;
81+
7882
/// If this is a byval or inalloca argument, return its alignment.
7983
/// FIXME: Remove this function once transition to Align is over.
8084
/// Use getParamAlign() instead.

llvm/lib/IR/Function.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,27 @@ bool Argument::hasPassPointeeByValueAttr() const {
128128
Attrs.hasParamAttribute(getArgNo(), Attribute::Preallocated);
129129
}
130130

131+
uint64_t Argument::getPassPointeeByValueCopySize(const DataLayout &DL) const {
132+
AttributeSet ParamAttrs
133+
= getParent()->getAttributes().getParamAttributes(getArgNo());
134+
135+
// FIXME: All the type carrying attributes are mutually exclusive, so there
136+
// should be a single query to get the stored type that handles any of them.
137+
if (Type *ByValTy = ParamAttrs.getByValType())
138+
return DL.getTypeAllocSize(ByValTy);
139+
if (Type *PreAllocTy = ParamAttrs.getPreallocatedType())
140+
return DL.getTypeAllocSize(PreAllocTy);
141+
142+
// FIXME: inalloca always depends on pointee element type. It's also possible
143+
// for byval to miss it.
144+
if (ParamAttrs.hasAttribute(Attribute::InAlloca) ||
145+
ParamAttrs.hasAttribute(Attribute::ByVal) ||
146+
ParamAttrs.hasAttribute(Attribute::Preallocated))
147+
return DL.getTypeAllocSize(cast<PointerType>(getType())->getElementType());
148+
149+
return 0;
150+
}
151+
131152
unsigned Argument::getParamAlignment() const {
132153
assert(getType()->isPointerTy() && "Only pointers have alignments");
133154
return getParent()->getParamAlignment(getArgNo());

llvm/lib/IR/Mangler.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,18 @@ static void addByteCountSuffix(raw_ostream &OS, const Function *F,
9494
const DataLayout &DL) {
9595
// Calculate arguments size total.
9696
unsigned ArgWords = 0;
97+
98+
const unsigned PtrSize = DL.getPointerSize();
99+
97100
for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
98101
AI != AE; ++AI) {
99-
Type *Ty = AI->getType();
100102
// 'Dereference' type in case of byval or inalloca parameter attribute.
101-
if (AI->hasPassPointeeByValueAttr())
102-
Ty = cast<PointerType>(Ty)->getElementType();
103+
uint64_t AllocSize = AI->hasPassPointeeByValueAttr() ?
104+
AI->getPassPointeeByValueCopySize(DL) :
105+
DL.getTypeAllocSize(AI->getType());
106+
103107
// Size should be aligned to pointer size.
104-
unsigned PtrSize = DL.getPointerSize();
105-
ArgWords += alignTo(DL.getTypeAllocSize(Ty), PtrSize);
108+
ArgWords += alignTo(AllocSize, PtrSize);
106109
}
107110

108111
OS << '@' << ArgWords;

0 commit comments

Comments
 (0)