diff --git a/llvm/docs/LangRef.rst b/llvm/docs/LangRef.rst --- a/llvm/docs/LangRef.rst +++ b/llvm/docs/LangRef.rst @@ -2754,9 +2754,11 @@ This specifies the *size* of a pointer and its ```` and ````\erred alignments for address space ``n``. ```` is optional and defaults to ````. The fourth parameter ```` is the size of the - index that used for address calculation. If not - specified, the default index size is equal to the pointer size. All sizes - are in bits. The address space, ``n``, is optional, and if not specified, + index that used for address calculation. For targets that include additional + non-integral bits in the pointer representation (e.g. fat pointer metadata), + ```` also specifies the number of integral bits, i.e. the address space + range. If not specified, the default index size is equal to the pointer + size. The address space, ``n``, is optional, and if not specified, denotes the default address space 0. The value of ``n`` must be in the range [1,2^23). ``i:[:]`` diff --git a/llvm/include/llvm/IR/DataLayout.h b/llvm/include/llvm/IR/DataLayout.h --- a/llvm/include/llvm/IR/DataLayout.h +++ b/llvm/include/llvm/IR/DataLayout.h @@ -373,12 +373,26 @@ /// the backends/clients are updated. Align getPointerPrefAlignment(unsigned AS = 0) const; - /// Layout pointer size in bytes, rounded up to a whole - /// number of bytes. + /// Layout pointer size in bytes, rounded up to a whole number of bytes. The + /// difference between this function and getPointerIntegralSize() is this one + /// returns the size of the entire pointer type (this includes metadata bits + /// for fat pointers) and the latter only returns the number of address bits. + /// \sa DataLayout::getPointerIntegralSize /// FIXME: The defaults need to be removed once all of /// the backends/clients are updated. unsigned getPointerSize(unsigned AS = 0) const; + /// Returns the integral size of a pointer in a given address space in bytes. + /// For targets that store bits in pointers that are not part of the address, + /// this returns the number of bits that can be manipulated using operations + /// that change the address (e.g. addition/subtraction). + /// For example, a 64-bit CHERI-enabled target has 128-bit pointers of which + /// only 64 are used to represent the address and the remaining ones are used + /// for metadata such as bounds and access permissions. In this case + /// getPointerSize() returns 16, but getPointerIntegralSize() returns 8. + /// \sa DataLayout::getPointerSize + unsigned getPointerIntegralSize(unsigned AS) const; + /// Returns the maximum index size over all address spaces. unsigned getMaxIndexSize() const; @@ -413,6 +427,14 @@ return getPointerAlignElem(AS).TypeBitWidth; } + unsigned getPointerIntegralSizeInBits(unsigned AS) const { + // Currently, this returns the same value as getIndexSizeInBits() as this + // is correct for all currently known LLVM targets. If another target is + // added that has pointer size != pointer range != GEP index width, we can + // add a new datalayout field for pointer integral range. + return getPointerAlignElem(AS).IndexBitWidth; + } + /// Returns the maximum index size over all address spaces. unsigned getMaxIndexSizeInBits() const { return getMaxIndexSize() * 8; diff --git a/llvm/lib/IR/DataLayout.cpp b/llvm/lib/IR/DataLayout.cpp --- a/llvm/lib/IR/DataLayout.cpp +++ b/llvm/lib/IR/DataLayout.cpp @@ -358,6 +358,8 @@ return Err; if (!IndexSize) return reportError("Invalid index size of 0 bytes"); + if (IndexSize > PointerMemSize) + return reportError("Index size cannot be larger than pointer size"); } } if (Error Err = setPointerAlignmentInBits( @@ -709,6 +711,10 @@ return divideCeil(getPointerAlignElem(AS).TypeBitWidth, 8); } +unsigned DataLayout::getPointerIntegralSize(unsigned AS) const { + return divideCeil(getPointerIntegralSizeInBits(AS), 8); +} + unsigned DataLayout::getMaxIndexSize() const { unsigned MaxIndexSize = 0; for (auto &P : Pointers) diff --git a/llvm/test/Assembler/invalid-datalayout25.ll b/llvm/test/Assembler/invalid-datalayout25.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Assembler/invalid-datalayout25.ll @@ -0,0 +1,6 @@ +; RUN: not llvm-as < %s 2>&1 | FileCheck %s + +target datalayout = "p0:32:32:32:64" + +; CHECK: error: index size cannot be larger than pointer size + diff --git a/llvm/test/Assembler/invalid-datalayout26.ll b/llvm/test/Assembler/invalid-datalayout26.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Assembler/invalid-datalayout26.ll @@ -0,0 +1,6 @@ +; RUN: not llvm-as < %s 2>&1 | FileCheck %s + +target datalayout = "p0:32:32:32:0" + +; CHECK: error: Invalid index size of 0 bytes +