Skip to content

[MLIR] Add endianness accessors to the data layout #87347

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 2 commits into from
Apr 3, 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
3 changes: 3 additions & 0 deletions mlir/include/mlir/Dialect/DLTI/DLTI.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ class DataLayoutSpecAttr
/// Returns the list of entries.
DataLayoutEntryListRef getEntries() const;

/// Returns the endiannes identifier.
StringAttr getEndiannessIdentifier(MLIRContext *context) const;

/// Returns the alloca memory space identifier.
StringAttr getAllocaMemorySpaceIdentifier(MLIRContext *context) const;

Expand Down
9 changes: 9 additions & 0 deletions mlir/include/mlir/Interfaces/DataLayoutInterfaces.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ std::optional<uint64_t>
getDefaultIndexBitwidth(Type type, const DataLayout &dataLayout,
ArrayRef<DataLayoutEntryInterface> params);

/// Default handler for endianness request. Dispatches to the
/// DataLayoutInterface if specified, otherwise returns the default.
Attribute getDefaultEndianness(DataLayoutEntryInterface entry);

/// Default handler for alloca memory space request. Dispatches to the
/// DataLayoutInterface if specified, otherwise returns the default.
Attribute getDefaultAllocaMemorySpace(DataLayoutEntryInterface entry);
Expand Down Expand Up @@ -192,6 +196,9 @@ class DataLayout {
/// type is not a pointer-like type, it returns std::nullopt.
std::optional<uint64_t> getTypeIndexBitwidth(Type t) const;

/// Returns the specified endianness.
Attribute getEndianness() const;

/// Returns the memory space used for AllocaOps.
Attribute getAllocaMemorySpace() const;

Expand Down Expand Up @@ -230,6 +237,8 @@ class DataLayout {
mutable DenseMap<Type, uint64_t> preferredAlignments;
mutable DenseMap<Type, std::optional<uint64_t>> indexBitwidths;

/// Cache for the endianness.
mutable std::optional<Attribute> endianness;
/// Cache for alloca, global, and program memory spaces.
mutable std::optional<Attribute> allocaMemorySpace;
mutable std::optional<Attribute> programMemorySpace;
Expand Down
18 changes: 18 additions & 0 deletions mlir/include/mlir/Interfaces/DataLayoutInterfaces.td
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ def DataLayoutSpecInterface : AttrInterface<"DataLayoutSpecInterface"> {
/*methodName=*/"getEntries",
/*args=*/(ins)
>,
InterfaceMethod<
/*description=*/"Returns the endianness identifier.",
/*retTy=*/"::mlir::StringAttr",
/*methodName=*/"getEndiannessIdentifier",
/*args=*/(ins "::mlir::MLIRContext *":$context)
>,
InterfaceMethod<
/*description=*/"Returns the alloca memory space identifier.",
/*retTy=*/"::mlir::StringAttr",
Expand Down Expand Up @@ -296,6 +302,18 @@ def DataLayoutOpInterface : OpInterface<"DataLayoutOpInterface"> {
params);
}]
>,
StaticInterfaceMethod<
/*description=*/"Returns the endianness used by the ABI computed "
"using the relevant entries. The data layout object "
"can be used for recursive queries.",
/*retTy=*/"::mlir::Attribute",
/*methodName=*/"getEndianness",
/*args=*/(ins "::mlir::DataLayoutEntryInterface":$entry),
/*methodBody=*/"",
/*defaultImplementation=*/[{
return ::mlir::detail::getDefaultEndianness(entry);
}]
>,
StaticInterfaceMethod<
/*description=*/"Returns the memory space used by the ABI computed "
"using the relevant entries. The data layout object "
Expand Down
5 changes: 5 additions & 0 deletions mlir/lib/Dialect/DLTI/DLTI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,11 @@ DataLayoutEntryListRef DataLayoutSpecAttr::getEntries() const {
return getImpl()->entries;
}

StringAttr
DataLayoutSpecAttr::getEndiannessIdentifier(MLIRContext *context) const {
return Builder(context).getStringAttr(DLTIDialect::kDataLayoutEndiannessKey);
}

StringAttr
DataLayoutSpecAttr::getAllocaMemorySpaceIdentifier(MLIRContext *context) const {
return Builder(context).getStringAttr(
Expand Down
25 changes: 25 additions & 0 deletions mlir/lib/Interfaces/DataLayoutInterfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,15 @@ std::optional<uint64_t> mlir::detail::getDefaultIndexBitwidth(
return std::nullopt;
}

// Returns the endianness if specified in the given entry. If the entry is empty
// the default endianness represented by an empty attribute is returned.
Attribute mlir::detail::getDefaultEndianness(DataLayoutEntryInterface entry) {
if (entry == DataLayoutEntryInterface())
return Attribute();

return entry.getValue();
}

// Returns the memory space used for alloca operations if specified in the
// given entry. If the entry is empty the default memory space represented by
// an empty attribute is returned.
Expand Down Expand Up @@ -548,6 +557,22 @@ std::optional<uint64_t> mlir::DataLayout::getTypeIndexBitwidth(Type t) const {
});
}

mlir::Attribute mlir::DataLayout::getEndianness() const {
checkValid();
if (endianness)
return *endianness;
DataLayoutEntryInterface entry;
if (originalLayout)
entry = originalLayout.getSpecForIdentifier(
originalLayout.getEndiannessIdentifier(originalLayout.getContext()));

if (auto iface = dyn_cast_or_null<DataLayoutOpInterface>(scope))
endianness = iface.getEndianness(entry);
else
endianness = detail::getDefaultEndianness(entry);
return *endianness;
}

mlir::Attribute mlir::DataLayout::getAllocaMemorySpace() const {
checkValid();
if (allocaMemorySpace)
Expand Down
8 changes: 8 additions & 0 deletions mlir/test/Dialect/LLVMIR/layout.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module {
// CHECK: alignment = 8
// CHECK: alloca_memory_space = 0
// CHECK: bitsize = 64
// CHECK: endianness = ""
// CHECK: global_memory_space = 0
// CHECK: index = 64
// CHECK: preferred = 8
Expand All @@ -16,6 +17,7 @@ module {
// CHECK: alignment = 8
// CHECK: alloca_memory_space = 0
// CHECK: bitsize = 64
// CHECK: endianness = ""
// CHECK: global_memory_space = 0
// CHECK: index = 64
// CHECK: preferred = 8
Expand All @@ -26,6 +28,7 @@ module {
// CHECK: alignment = 8
// CHECK: alloca_memory_space = 0
// CHECK: bitsize = 64
// CHECK: endianness = ""
// CHECK: global_memory_space = 0
// CHECK: index = 64
// CHECK: preferred = 8
Expand All @@ -43,6 +46,7 @@ module attributes { dlti.dl_spec = #dlti.dl_spec<
#dlti.dl_entry<!llvm.ptr, dense<[32, 32, 64]> : vector<3xi64>>,
#dlti.dl_entry<!llvm.ptr<5>, dense<[64, 64, 64]> : vector<3xi64>>,
#dlti.dl_entry<!llvm.ptr<4>, dense<[32, 64, 64, 24]> : vector<4xi64>>,
#dlti.dl_entry<"dlti.endianness", "little">,
#dlti.dl_entry<"dlti.alloca_memory_space", 5 : ui64>,
#dlti.dl_entry<"dlti.global_memory_space", 2 : ui64>,
#dlti.dl_entry<"dlti.program_memory_space", 3 : ui64>,
Expand All @@ -53,6 +57,7 @@ module attributes { dlti.dl_spec = #dlti.dl_spec<
// CHECK: alignment = 4
// CHECK: alloca_memory_space = 5
// CHECK: bitsize = 32
// CHECK: endianness = "little"
// CHECK: global_memory_space = 2
// CHECK: index = 32
// CHECK: preferred = 8
Expand All @@ -63,6 +68,7 @@ module attributes { dlti.dl_spec = #dlti.dl_spec<
// CHECK: alignment = 4
// CHECK: alloca_memory_space = 5
// CHECK: bitsize = 32
// CHECK: endianness = "little"
// CHECK: global_memory_space = 2
// CHECK: index = 32
// CHECK: preferred = 8
Expand All @@ -73,6 +79,7 @@ module attributes { dlti.dl_spec = #dlti.dl_spec<
// CHECK: alignment = 8
// CHECK: alloca_memory_space = 5
// CHECK: bitsize = 64
// CHECK: endianness = "little"
// CHECK: global_memory_space = 2
// CHECK: index = 64
// CHECK: preferred = 8
Expand All @@ -83,6 +90,7 @@ module attributes { dlti.dl_spec = #dlti.dl_spec<
// CHECK: alignment = 8
// CHECK: alloca_memory_space = 5
// CHECK: bitsize = 32
// CHECK: endianness = "little"
// CHECK: global_memory_space = 2
// CHECK: index = 24
// CHECK: preferred = 8
Expand Down
4 changes: 4 additions & 0 deletions mlir/test/lib/Dialect/DLTI/TestDataLayoutQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ struct TestDataLayoutQuery
uint64_t alignment = layout.getTypeABIAlignment(op.getType());
uint64_t preferred = layout.getTypePreferredAlignment(op.getType());
uint64_t index = layout.getTypeIndexBitwidth(op.getType()).value_or(0);
Attribute endianness = layout.getEndianness();
Attribute allocaMemorySpace = layout.getAllocaMemorySpace();
Attribute programMemorySpace = layout.getProgramMemorySpace();
Attribute globalMemorySpace = layout.getGlobalMemorySpace();
Expand All @@ -51,6 +52,9 @@ struct TestDataLayoutQuery
builder.getNamedAttr("alignment", builder.getIndexAttr(alignment)),
builder.getNamedAttr("preferred", builder.getIndexAttr(preferred)),
builder.getNamedAttr("index", builder.getIndexAttr(index)),
builder.getNamedAttr("endianness", endianness == Attribute()
? builder.getStringAttr("")
: endianness),
builder.getNamedAttr("alloca_memory_space",
allocaMemorySpace == Attribute()
? builder.getUI32IntegerAttr(0)
Expand Down
18 changes: 18 additions & 0 deletions mlir/unittests/Interfaces/DataLayoutInterfacesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ using namespace mlir;

namespace {
constexpr static llvm::StringLiteral kAttrName = "dltest.layout";
constexpr static llvm::StringLiteral kEndiannesKeyName = "dltest.endianness";
constexpr static llvm::StringLiteral kAllocaKeyName =
"dltest.alloca_memory_space";
constexpr static llvm::StringLiteral kProgramKeyName =
Expand Down Expand Up @@ -73,6 +74,9 @@ struct CustomDataLayoutSpec
}
DataLayoutEntryListRef getEntries() const { return getImpl()->entries; }
LogicalResult verifySpec(Location loc) { return success(); }
StringAttr getEndiannessIdentifier(MLIRContext *context) const {
return Builder(context).getStringAttr(kEndiannesKeyName);
}
StringAttr getAllocaMemorySpaceIdentifier(MLIRContext *context) const {
return Builder(context).getStringAttr(kAllocaKeyName);
}
Expand Down Expand Up @@ -130,6 +134,15 @@ struct SingleQueryType
return 4;
}

Attribute getEndianness(DataLayoutEntryInterface entry) {
static bool executed = false;
if (executed)
llvm::report_fatal_error("repeated call");

executed = true;
return Attribute();
}

Attribute getAllocaMemorySpace(DataLayoutEntryInterface entry) {
static bool executed = false;
if (executed)
Expand Down Expand Up @@ -317,6 +330,7 @@ module {}
EXPECT_EQ(layout.getTypePreferredAlignment(IntegerType::get(&ctx, 42)), 8u);
EXPECT_EQ(layout.getTypePreferredAlignment(Float16Type::get(&ctx)), 2u);

EXPECT_EQ(layout.getEndianness(), Attribute());
EXPECT_EQ(layout.getAllocaMemorySpace(), Attribute());
EXPECT_EQ(layout.getProgramMemorySpace(), Attribute());
EXPECT_EQ(layout.getGlobalMemorySpace(), Attribute());
Expand Down Expand Up @@ -348,6 +362,7 @@ TEST(DataLayout, NullSpec) {
EXPECT_EQ(layout.getTypeIndexBitwidth(Float16Type::get(&ctx)), std::nullopt);
EXPECT_EQ(layout.getTypeIndexBitwidth(IndexType::get(&ctx)), 64u);

EXPECT_EQ(layout.getEndianness(), Attribute());
EXPECT_EQ(layout.getAllocaMemorySpace(), Attribute());
EXPECT_EQ(layout.getProgramMemorySpace(), Attribute());
EXPECT_EQ(layout.getGlobalMemorySpace(), Attribute());
Expand Down Expand Up @@ -378,6 +393,7 @@ TEST(DataLayout, EmptySpec) {
EXPECT_EQ(layout.getTypeIndexBitwidth(Float16Type::get(&ctx)), std::nullopt);
EXPECT_EQ(layout.getTypeIndexBitwidth(IndexType::get(&ctx)), 64u);

EXPECT_EQ(layout.getEndianness(), Attribute());
EXPECT_EQ(layout.getAllocaMemorySpace(), Attribute());
EXPECT_EQ(layout.getProgramMemorySpace(), Attribute());
EXPECT_EQ(layout.getGlobalMemorySpace(), Attribute());
Expand All @@ -390,6 +406,7 @@ TEST(DataLayout, SpecWithEntries) {
#dlti.dl_entry<i42, 5>,
#dlti.dl_entry<i16, 6>,
#dlti.dl_entry<index, 42>,
#dlti.dl_entry<"dltest.endianness", "little">,
#dlti.dl_entry<"dltest.alloca_memory_space", 5 : i32>,
#dlti.dl_entry<"dltest.program_memory_space", 3 : i32>,
#dlti.dl_entry<"dltest.global_memory_space", 2 : i32>,
Expand Down Expand Up @@ -425,6 +442,7 @@ TEST(DataLayout, SpecWithEntries) {
EXPECT_EQ(layout.getTypePreferredAlignment(IntegerType::get(&ctx, 32)), 64u);
EXPECT_EQ(layout.getTypePreferredAlignment(Float32Type::get(&ctx)), 64u);

EXPECT_EQ(layout.getEndianness(), Builder(&ctx).getStringAttr("little"));
EXPECT_EQ(layout.getAllocaMemorySpace(), Builder(&ctx).getI32IntegerAttr(5));
EXPECT_EQ(layout.getProgramMemorySpace(), Builder(&ctx).getI32IntegerAttr(3));
EXPECT_EQ(layout.getGlobalMemorySpace(), Builder(&ctx).getI32IntegerAttr(2));
Expand Down