Skip to content

[DataLayout] Move '*AlignElem' structs and enum inside DataLayout (NFC) #103723

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 3 commits into from
Aug 15, 2024
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
107 changes: 48 additions & 59 deletions llvm/include/llvm/IR/DataLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,51 +49,11 @@ class StructLayout;
class Triple;
class Value;

/// Enum used to categorize the alignment types stored by LayoutAlignElem
enum AlignTypeEnum {
INTEGER_ALIGN = 'i',
VECTOR_ALIGN = 'v',
FLOAT_ALIGN = 'f',
AGGREGATE_ALIGN = 'a'
};

// FIXME: Currently the DataLayout string carries a "preferred alignment"
// for types. As the DataLayout is module/global, this should likely be
// sunk down to an FTTI element that is queried rather than a global
// preference.

/// Layout alignment element.
///
/// Stores the alignment data associated with a given type bit width.
struct LayoutAlignElem {
uint32_t TypeBitWidth;
Align ABIAlign;
Align PrefAlign;

static LayoutAlignElem get(Align ABIAlign, Align PrefAlign,
uint32_t BitWidth);

bool operator==(const LayoutAlignElem &rhs) const;
};

/// Layout pointer alignment element.
///
/// Stores the alignment data associated with a given pointer and address space.
struct PointerAlignElem {
uint32_t AddressSpace;
uint32_t TypeBitWidth;
Align ABIAlign;
Align PrefAlign;
uint32_t IndexBitWidth;

/// Initializer
static PointerAlignElem getInBits(uint32_t AddressSpace, Align ABIAlign,
Align PrefAlign, uint32_t TypeBitWidth,
uint32_t IndexBitWidth);

bool operator==(const PointerAlignElem &rhs) const;
};

/// A parsed version of the target data layout string in and methods for
/// querying it.
///
Expand All @@ -102,6 +62,26 @@ struct PointerAlignElem {
/// target being codegen'd to.
class DataLayout {
public:
/// Primitive type specification.
struct PrimitiveSpec {
uint32_t BitWidth;
Align ABIAlign;
Align PrefAlign;

bool operator==(const PrimitiveSpec &Other) const;
};

/// Pointer type specification.
struct PointerSpec {
uint32_t AddrSpace;
uint32_t BitWidth;
Align ABIAlign;
Align PrefAlign;
uint32_t IndexBitWidth;

bool operator==(const PointerSpec &Other) const;
};

enum class FunctionPtrAlignType {
/// The function pointer alignment is independent of the function alignment.
Independent,
Expand Down Expand Up @@ -135,20 +115,26 @@ class DataLayout {
// FIXME: `unsigned char` truncates the value parsed by `parseSpecifier`.
SmallVector<unsigned char, 8> LegalIntWidths;

// Primitive type specifications. Sorted and uniqued by type bit width.
SmallVector<LayoutAlignElem, 6> IntAlignments;
SmallVector<LayoutAlignElem, 4> FloatAlignments;
SmallVector<LayoutAlignElem, 10> VectorAlignments;
/// Type specifier used by some internal functions.
enum class TypeSpecifier {
Integer = 'i',
Float = 'f',
Vector = 'v',
Aggregate = 'a'
};

// Pointer type specifications. Sorted and uniqued by address space number.
SmallVector<PointerAlignElem, 8> Pointers;
/// Primitive type specifications. Sorted and uniqued by type bit width.
SmallVector<PrimitiveSpec, 6> IntSpecs;
SmallVector<PrimitiveSpec, 4> FloatSpecs;
SmallVector<PrimitiveSpec, 10> VectorSpecs;

/// Pointer type specifications. Sorted and uniqued by address space number.
SmallVector<PointerSpec, 8> PointerSpecs;

/// The string representation used to create this DataLayout
std::string StringRepresentation;

const PointerAlignElem &getPointerAlignElem(uint32_t AddressSpace) const;

// Struct type ABI and preferred alignments. The default spec is "a:8:64".
/// Struct type ABI and preferred alignments. The default spec is "a:8:64".
Align StructABIAlignment = Align::Constant<1>();
Align StructPrefAlignment = Align::Constant<8>();

Expand All @@ -159,16 +145,19 @@ class DataLayout {
/// well-defined bitwise representation.
SmallVector<unsigned, 8> NonIntegralAddressSpaces;

/// Attempts to set the alignment of the given type. Returns an error
/// description on failure.
Error setAlignment(AlignTypeEnum AlignType, Align ABIAlign, Align PrefAlign,
uint32_t BitWidth);
/// Attempts to set the specification for the given type.
/// Returns an error description on failure.
Error setPrimitiveSpec(TypeSpecifier Specifier, uint32_t BitWidth,
Align ABIAlign, Align PrefAlign);

/// Searches for a pointer specification that matches the given address space.
/// Returns the default address space specification if not found.
const PointerSpec &getPointerSpec(uint32_t AddrSpace) const;

/// Attempts to set the alignment of a pointer in the given address space.
/// Attempts to set the specification for pointer in the given address space.
/// Returns an error description on failure.
Error setPointerAlignmentInBits(uint32_t AddrSpace, Align ABIAlign,
Align PrefAlign, uint32_t TypeBitWidth,
uint32_t IndexBitWidth);
Error setPointerSpec(uint32_t AddrSpace, uint32_t BitWidth, Align ABIAlign,
Align PrefAlign, uint32_t IndexBitWidth);

/// Internal helper to get alignment for integer of given bitwidth.
Align getIntegerAlignment(uint32_t BitWidth, bool abi_or_pref) const;
Expand Down Expand Up @@ -375,7 +364,7 @@ class DataLayout {
/// FIXME: The defaults need to be removed once all of
/// the backends/clients are updated.
unsigned getPointerSizeInBits(unsigned AS = 0) const {
return getPointerAlignElem(AS).TypeBitWidth;
return getPointerSpec(AS).BitWidth;
}

/// Returns the maximum index size over all address spaces.
Expand All @@ -385,7 +374,7 @@ class DataLayout {

/// Size in bits of index used for address calculation in getelementptr.
unsigned getIndexSizeInBits(unsigned AS) const {
return getPointerAlignElem(AS).IndexBitWidth;
return getPointerSpec(AS).IndexBitWidth;
}

/// Layout pointer size, in bits, based on the type. If this function is
Expand Down
Loading
Loading