[LLVM][IR] Add support for address space names in DataLayout by jurahul · Pull Request #170559 · llvm/llvm-project (original) (raw)

@llvm/pr-subscribers-llvm-ir

Author: Rahul Joshi (jurahul)

Changes

Add support for specifying the names of address spaces when specifying pointer properties for an address space. Update LLVM's AsmParser and LLParser to print and read these symbolic address space name.


Patch is 26.06 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/170559.diff

6 Files Affected:

diff --git a/llvm/include/llvm/IR/DataLayout.h b/llvm/include/llvm/IR/DataLayout.h index 54458201af0b3..9e67f726a92b3 100644 --- a/llvm/include/llvm/IR/DataLayout.h +++ b/llvm/include/llvm/IR/DataLayout.h @@ -21,6 +21,7 @@

#include "llvm/ADT/APInt.h" #include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/DenseSet.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" @@ -92,7 +93,15 @@ class DataLayout { /// of this would be CHERI capabilities where the validity bit is stored /// separately from the pointer address+bounds information. bool HasExternalState;

};

enum class FunctionPtrAlignType { @@ -158,7 +167,8 @@ class DataLayout { /// Sets or updates the specification for pointer in the given address space. void setPointerSpec(uint32_t AddrSpace, uint32_t BitWidth, Align ABIAlign, Align PrefAlign, uint32_t IndexBitWidth,

@@ -173,11 +183,13 @@ class DataLayout { Error parseAggregateSpec(StringRef Spec);

/// Attempts to parse pointer specification ('p').

@@ -324,9 +336,13 @@ class DataLayout { return false; }

diff --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp index 7932765db8359..10577fceee239 100644 --- a/llvm/lib/IR/AsmWriter.cpp +++ b/llvm/lib/IR/AsmWriter.cpp @@ -543,7 +543,8 @@ namespace {

class TypePrinting { public:

@@ -563,8 +564,9 @@ class TypePrinting { private: void incorporateTypes();

@@ -605,11 +607,11 @@ bool TypePrinting::empty() { }

void TypePrinting::incorporateTypes() {

@@ -630,6 +632,20 @@ void TypePrinting::incorporateTypes() { NamedTypes.erase(NextToUse, NamedTypes.end()); }

+static void printAddressSpace(const Module *M, unsigned AS, raw_ostream &OS,

static void maybePrintCallAddrSpace(const Value *Operand, const Instruction *I, raw_ostream &Out) {

}

// This member is called for each Instruction in a function.. @@ -4735,9 +4749,8 @@ void AssemblyWriter::printInstruction(const Instruction &I) { Out << ", align " << A->value(); }

diff --git a/llvm/lib/IR/DataLayout.cpp b/llvm/lib/IR/DataLayout.cpp index 49e1f898ca594..52b77e39ffe5b 100644 --- a/llvm/lib/IR/DataLayout.cpp +++ b/llvm/lib/IR/DataLayout.cpp @@ -147,12 +147,10 @@ bool DataLayout::PrimitiveSpec::operator==(const PrimitiveSpec &Other) const { PrefAlign == Other.PrefAlign; }

-bool DataLayout::PointerSpec::operator==(const PointerSpec &Other) const {

+StringRef +DataLayout::PointerSpec::getAddrSpaceName(const DataLayout &DL) const {

}

namespace { @@ -195,7 +193,7 @@ constexpr DataLayout::PrimitiveSpec DefaultVectorSpecs[] = { // Default pointer type specifications. constexpr DataLayout::PointerSpec DefaultPointerSpecs[] = { // p0:64:64:64:64

};

DataLayout::DataLayout() @@ -233,6 +231,15 @@ DataLayout &DataLayout::operator=(const DataLayout &Other) {

bool DataLayout::operator==(const DataLayout &Other) const { // NOTE: StringRepresentation might differ, it is not canonicalized.

@@ -243,9 +250,9 @@ bool DataLayout::operator==(const DataLayout &Other) const { ManglingMode == Other.ManglingMode && LegalIntWidths == Other.LegalIntWidths && IntSpecs == Other.IntSpecs && FloatSpecs == Other.FloatSpecs && VectorSpecs == Other.VectorSpecs &&

}

Expected DataLayout::parse(StringRef LayoutString) { @@ -271,6 +278,48 @@ static Error parseAddrSpace(StringRef Str, unsigned &AddrSpace) { return Error::success(); }

+/// Attempts to parse an address space component of a specification allowing +/// name to be specified as well. The input is expected to be of the form +/// '(' name ' )', with the name otional and the number is optional as +/// well. +static Error parseAddrSpaceAndName(StringRef Str, unsigned &AddrSpace,

@@ -395,7 +444,8 @@ Error DataLayout::parseAggregateSpec(StringRef Spec) { return Error::success(); }

-Error DataLayout::parsePointerSpec(StringRef Spec) { +Error DataLayout::parsePointerSpec(

// p[]::[:[:]] SmallVector<StringRef, 5> Components; assert(Spec.front() == 'p'); @@ -408,6 +458,7 @@ Error DataLayout::parsePointerSpec(StringRef Spec) { unsigned AddrSpace = 0; bool ExternalState = false; bool UnstableRepr = false;

Error DataLayout::parseSpecification(

// The "ni" specifier is the only two-character specifier. Handle it first. if (Spec.starts_with("ni")) { // ni:

[:
]... @@ -499,7 +557,7 @@ Error DataLayout::parseSpecification( return parseAggregateSpec(Spec);

if (Specifier == 'p')

  • return parsePointerSpec(Spec);
  • return parsePointerSpec(Spec, AddrSpaceNames);

StringRef Rest = Spec.drop_front(); switch (Specifier) { @@ -616,7 +674,7 @@ Error DataLayout::parseSpecification( }

Error DataLayout::parseLayoutString(StringRef LayoutString) {

  • StringRepresentation = std::string(LayoutString);
  • StringRepresentation = LayoutString.str();

    if (LayoutString.empty()) return Error::success();

@@ -624,10 +682,12 @@ Error DataLayout::parseLayoutString(StringRef LayoutString) { // Split the data layout string into specifications separated by '-' and // parse each specification individually, updating internal data structures. SmallVector<unsigned, 8> NonIntegralAddressSpaces;

  • for (StringRef Spec : split(LayoutString, '-')) {
  • SmallDenseSet<StringRef, 8> AddessSpaceNames;
  • for (StringRef Spec : split(StringRepresentation, '-')) { if (Spec.empty()) return createStringError("empty specification is not allowed");
  • if (Error Err = parseSpecification(Spec, NonIntegralAddressSpaces))
  • if (Error Err = parseSpecification(Spec, NonIntegralAddressSpaces,
  •                                   AddessSpaceNames))
     return Err;
    } // Mark all address spaces that were qualified as non-integral now. This has @@ -638,7 +698,8 @@ Error DataLayout::parseLayoutString(StringRef LayoutString) { // the spec for AS0, and we then update that to mark it non-integral. const PointerSpec &PS = getPointerSpec(AS); setPointerSpec(AS, PS.BitWidth, PS.ABIAlign, PS.PrefAlign, PS.IndexBitWidth,
  •               /*HasUnstableRepr=*/true, /*HasExternalState=*/false);
  •               /*HasUnstableRepr=*/true, /*HasExternalState=*/false,
  •               getAddressSpaceName(AS));

    }

    return Error::success();

@@ -687,12 +748,28 @@ DataLayout::getPointerSpec(uint32_t AddrSpace) const { void DataLayout::setPointerSpec(uint32_t AddrSpace, uint32_t BitWidth, Align ABIAlign, Align PrefAlign, uint32_t IndexBitWidth, bool HasUnstableRepr,

  •                            bool HasExternalState) {
  •                            bool HasExternalState,
  •                            StringRef AddrSpaceName) {
    auto I = lower_bound(PointerSpecs, AddrSpace, LessPointerAddrSpace());
  • size_t AddrSpaceNameOffset = 0, AddrSpaceNameSize = AddrSpaceName.size();
  • if (!AddrSpaceName.empty()) {
  • // Validate that AddrSpaceName points to data within the
  • // StringRepresentation.
  • const char *RepStart = StringRepresentation.data();
  • const char *ASStart = AddrSpaceName.data();
  • [[maybe_unused]] const char *RepEnd =
  •    RepStart + StringRepresentation.size();
  • [[maybe_unused]] const char *ASEnd = ASStart + AddrSpaceNameSize;
  • assert(RepStart <= ASStart && ASStart < RepEnd && Rep...

[truncated]