Skip to content

Commit 8c0e9ad

Browse files
authored
[MLIR][DLTI] Add mangling style (#125875)
Add mangling style as a spec entry to datalayout, and implemented importing and exporting LLVM IR to MLIR (LLVM dialect). Its represented as string as the scope of this PR is to preserve info from LLVM IR, so client in MLIR still need to map deduce the meaning of the string, like "e" means ELF, "o" for Mach-O, etc. it addresses one of issues mentioned in this [issue](#126046)
1 parent dd662d8 commit 8c0e9ad

File tree

15 files changed

+130
-3
lines changed

15 files changed

+130
-3
lines changed

mlir/include/mlir/Dialect/DLTI/DLTIAttrs.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ def DLTI_DataLayoutSpecAttr :
7777
/// Returns the alloca memory space identifier.
7878
StringAttr getAllocaMemorySpaceIdentifier(MLIRContext *context) const;
7979

80+
/// Returns the mangling mode identifier.
81+
StringAttr getManglingModeIdentifier(MLIRContext *context) const;
82+
8083
/// Returns the program memory space identifier.
8184
StringAttr getProgramMemorySpaceIdentifier(MLIRContext *context) const;
8285

mlir/include/mlir/Dialect/DLTI/DLTIBase.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ def DLTI_Dialect : Dialect {
4949
constexpr const static ::llvm::StringLiteral
5050
kDataLayoutEndiannessLittle = "little";
5151

52+
// Mangling mode, a.k.a mangling style used to mangle llvm names.
53+
constexpr const static ::llvm::StringLiteral
54+
kDataLayoutManglingModeKey = "dlti.mangling_mode";
55+
5256
constexpr const static ::llvm::StringLiteral
5357
kDataLayoutAllocaMemorySpaceKey = "dlti.alloca_memory_space";
5458

mlir/include/mlir/Interfaces/DataLayoutInterfaces.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ Attribute getDefaultEndianness(DataLayoutEntryInterface entry);
7878
/// DataLayoutInterface if specified, otherwise returns the default.
7979
Attribute getDefaultAllocaMemorySpace(DataLayoutEntryInterface entry);
8080

81+
/// Default handler for mangling mode request. Dispatches to the
82+
/// DataLayoutInterface if specified, otherwise returns the default.
83+
Attribute getDefaultManglingMode(DataLayoutEntryInterface entry);
84+
8185
/// Default handler for program memory space request. Dispatches to the
8286
/// DataLayoutInterface if specified, otherwise returns the default.
8387
Attribute getDefaultProgramMemorySpace(DataLayoutEntryInterface entry);
@@ -230,6 +234,9 @@ class DataLayout {
230234
/// Returns the memory space used for AllocaOps.
231235
Attribute getAllocaMemorySpace() const;
232236

237+
/// Returns the mangling mode.
238+
Attribute getManglingMode() const;
239+
233240
/// Returns the memory space used for program memory operations.
234241
Attribute getProgramMemorySpace() const;
235242

@@ -276,6 +283,8 @@ class DataLayout {
276283

277284
/// Cache for the endianness.
278285
mutable std::optional<Attribute> endianness;
286+
/// Cache for the mangling mode.
287+
mutable std::optional<Attribute> manglingMode;
279288
/// Cache for alloca, global, and program memory spaces.
280289
mutable std::optional<Attribute> allocaMemorySpace;
281290
mutable std::optional<Attribute> programMemorySpace;

mlir/include/mlir/Interfaces/DataLayoutInterfaces.td

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ def DataLayoutSpecInterface : AttrInterface<"DataLayoutSpecInterface", [DLTIQuer
153153
/*methodName=*/"getGlobalMemorySpaceIdentifier",
154154
/*args=*/(ins "::mlir::MLIRContext *":$context)
155155
>,
156+
InterfaceMethod<
157+
/*description=*/"Returns the mangling mode identifier.",
158+
/*retTy=*/"::mlir::StringAttr",
159+
/*methodName=*/"getManglingModeIdentifier",
160+
/*args=*/(ins "::mlir::MLIRContext *":$context)
161+
>,
156162
InterfaceMethod<
157163
/*description=*/"Returns the stack alignment identifier.",
158164
/*retTy=*/"::mlir::StringAttr",
@@ -481,6 +487,18 @@ def DataLayoutOpInterface : OpInterface<"DataLayoutOpInterface"> {
481487
return ::mlir::detail::getDefaultAllocaMemorySpace(entry);
482488
}]
483489
>,
490+
StaticInterfaceMethod<
491+
/*description=*/"Returns the mangling mode computed "
492+
"using the relevant entries. The data layout object "
493+
"can be used for recursive queries.",
494+
/*retTy=*/"::mlir::Attribute",
495+
/*methodName=*/"getManglingMode",
496+
/*args=*/(ins "::mlir::DataLayoutEntryInterface":$entry),
497+
/*methodBody=*/"",
498+
/*defaultImplementation=*/[{
499+
return ::mlir::detail::getDefaultManglingMode(entry);
500+
}]
501+
>,
484502
StaticInterfaceMethod<
485503
/*description=*/"Returns the memory space used by the ABI computed "
486504
"using the relevant entries. The data layout object "

mlir/lib/Dialect/DLTI/DLTI.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,12 @@ DataLayoutSpecAttr::getGlobalMemorySpaceIdentifier(MLIRContext *context) const {
397397
DLTIDialect::kDataLayoutGlobalMemorySpaceKey);
398398
}
399399

400+
StringAttr
401+
DataLayoutSpecAttr::getManglingModeIdentifier(MLIRContext *context) const {
402+
return Builder(context).getStringAttr(
403+
DLTIDialect::kDataLayoutManglingModeKey);
404+
}
405+
400406
StringAttr
401407
DataLayoutSpecAttr::getStackAlignmentIdentifier(MLIRContext *context) const {
402408
return Builder(context).getStringAttr(
@@ -606,7 +612,8 @@ class TargetDataLayoutInterface : public DataLayoutDialectInterface {
606612
if (entryName == DLTIDialect::kDataLayoutAllocaMemorySpaceKey ||
607613
entryName == DLTIDialect::kDataLayoutProgramMemorySpaceKey ||
608614
entryName == DLTIDialect::kDataLayoutGlobalMemorySpaceKey ||
609-
entryName == DLTIDialect::kDataLayoutStackAlignmentKey)
615+
entryName == DLTIDialect::kDataLayoutStackAlignmentKey ||
616+
entryName == DLTIDialect::kDataLayoutManglingModeKey)
610617
return success();
611618
return emitError(loc) << "unknown data layout entry name: " << entryName;
612619
}

mlir/lib/Interfaces/DataLayoutInterfaces.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,15 @@ mlir::detail::getDefaultAllocaMemorySpace(DataLayoutEntryInterface entry) {
258258
return entry.getValue();
259259
}
260260

261+
// Returns the mangling mode if specified in the given entry.
262+
// If the entry is empty, an empty attribute is returned.
263+
Attribute mlir::detail::getDefaultManglingMode(DataLayoutEntryInterface entry) {
264+
if (entry == DataLayoutEntryInterface())
265+
return Attribute();
266+
267+
return entry.getValue();
268+
}
269+
261270
// Returns the memory space used for the program memory space. if
262271
// specified in the given entry. If the entry is empty the default
263272
// memory space represented by an empty attribute is returned.
@@ -612,6 +621,22 @@ mlir::Attribute mlir::DataLayout::getAllocaMemorySpace() const {
612621
return *allocaMemorySpace;
613622
}
614623

624+
mlir::Attribute mlir::DataLayout::getManglingMode() const {
625+
checkValid();
626+
if (manglingMode)
627+
return *manglingMode;
628+
DataLayoutEntryInterface entry;
629+
if (originalLayout)
630+
entry = originalLayout.getSpecForIdentifier(
631+
originalLayout.getManglingModeIdentifier(originalLayout.getContext()));
632+
633+
if (auto iface = dyn_cast_or_null<DataLayoutOpInterface>(scope))
634+
manglingMode = iface.getManglingMode(entry);
635+
else
636+
manglingMode = detail::getDefaultManglingMode(entry);
637+
return *manglingMode;
638+
}
639+
615640
mlir::Attribute mlir::DataLayout::getProgramMemorySpace() const {
616641
checkValid();
617642
if (programMemorySpace)

mlir/lib/Target/LLVMIR/DataLayoutImporter.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,21 @@ DataLayoutImporter::tryToEmplaceEndiannessEntry(StringRef endianness,
163163
return success();
164164
}
165165

166+
LogicalResult DataLayoutImporter::tryToEmplaceManglingModeEntry(
167+
StringRef token, llvm::StringLiteral manglingKey) {
168+
auto key = StringAttr::get(context, manglingKey);
169+
if (keyEntries.count(key))
170+
return success();
171+
172+
token.consume_front(":");
173+
if (token.empty())
174+
return failure();
175+
176+
keyEntries.try_emplace(
177+
key, DataLayoutEntryAttr::get(key, StringAttr::get(context, token)));
178+
return success();
179+
}
180+
166181
LogicalResult
167182
DataLayoutImporter::tryToEmplaceAddrSpaceEntry(StringRef token,
168183
llvm::StringLiteral spaceKey) {
@@ -254,6 +269,13 @@ void DataLayoutImporter::translateDataLayout(
254269
return;
255270
continue;
256271
}
272+
// Parse the mangling mode.
273+
if (*prefix == "m") {
274+
if (failed(tryToEmplaceManglingModeEntry(
275+
token, DLTIDialect::kDataLayoutManglingModeKey)))
276+
return;
277+
continue;
278+
}
257279
// Parse the global address space.
258280
if (*prefix == "G") {
259281
if (failed(tryToEmplaceAddrSpaceEntry(

mlir/lib/Target/LLVMIR/DataLayoutImporter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ class DataLayoutImporter {
100100
LogicalResult tryToEmplaceAddrSpaceEntry(StringRef token,
101101
llvm::StringLiteral spaceKey);
102102

103+
/// Adds an mangling mode entry if there is none yet.
104+
LogicalResult tryToEmplaceManglingModeEntry(StringRef token,
105+
llvm::StringLiteral manglingKey);
106+
103107
/// Adds a stack alignment entry if there is none yet.
104108
LogicalResult tryToEmplaceStackAlignmentEntry(StringRef token);
105109

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ translateDataLayout(DataLayoutSpecInterface attribute,
199199
layoutStream << "-" << (isLittleEndian ? "e" : "E");
200200
continue;
201201
}
202+
if (key.getValue() == DLTIDialect::kDataLayoutManglingModeKey) {
203+
auto value = cast<StringAttr>(entry.getValue());
204+
layoutStream << "-m:" << value.getValue();
205+
continue;
206+
}
202207
if (key.getValue() == DLTIDialect::kDataLayoutProgramMemorySpaceKey) {
203208
auto value = cast<IntegerAttr>(entry.getValue());
204209
uint64_t space = value.getValue().getZExtValue();

mlir/test/Dialect/LLVMIR/layout.mlir

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module {
99
// CHECK: endianness = ""
1010
// CHECK: global_memory_space = 0
1111
// CHECK: index = 64
12+
// CHECK: mangling_mode = ""
1213
// CHECK: preferred = 8
1314
// CHECK: program_memory_space = 0
1415
// CHECK: size = 8
@@ -20,6 +21,7 @@ module {
2021
// CHECK: endianness = ""
2122
// CHECK: global_memory_space = 0
2223
// CHECK: index = 64
24+
// CHECK: mangling_mode = ""
2325
// CHECK: preferred = 8
2426
// CHECK: program_memory_space = 0
2527
// CHECK: size = 8
@@ -31,6 +33,7 @@ module {
3133
// CHECK: endianness = ""
3234
// CHECK: global_memory_space = 0
3335
// CHECK: index = 64
36+
// CHECK: mangling_mode = ""
3437
// CHECK: preferred = 8
3538
// CHECK: program_memory_space = 0
3639
// CHECK: size = 8
@@ -50,7 +53,8 @@ module attributes { dlti.dl_spec = #dlti.dl_spec<
5053
#dlti.dl_entry<"dlti.alloca_memory_space", 5 : ui64>,
5154
#dlti.dl_entry<"dlti.global_memory_space", 2 : ui64>,
5255
#dlti.dl_entry<"dlti.program_memory_space", 3 : ui64>,
53-
#dlti.dl_entry<"dlti.stack_alignment", 128 : i64>
56+
#dlti.dl_entry<"dlti.stack_alignment", 128 : i64>,
57+
#dlti.dl_entry<"dlti.mangling_mode", "e">
5458
>} {
5559
// CHECK: @spec
5660
func.func @spec() {
@@ -60,6 +64,7 @@ module attributes { dlti.dl_spec = #dlti.dl_spec<
6064
// CHECK: endianness = "little"
6165
// CHECK: global_memory_space = 2
6266
// CHECK: index = 32
67+
// CHECK: mangling_mode = "e"
6368
// CHECK: preferred = 8
6469
// CHECK: program_memory_space = 3
6570
// CHECK: size = 4
@@ -82,6 +87,7 @@ module attributes { dlti.dl_spec = #dlti.dl_spec<
8287
// CHECK: endianness = "little"
8388
// CHECK: global_memory_space = 2
8489
// CHECK: index = 64
90+
// CHECK: mangling_mode = "e"
8591
// CHECK: preferred = 8
8692
// CHECK: program_memory_space = 3
8793
// CHECK: size = 8
@@ -93,6 +99,7 @@ module attributes { dlti.dl_spec = #dlti.dl_spec<
9399
// CHECK: endianness = "little"
94100
// CHECK: global_memory_space = 2
95101
// CHECK: index = 24
102+
// CHECK: mangling_mode = "e"
96103
// CHECK: preferred = 8
97104
// CHECK: program_memory_space = 3
98105
// CHECK: size = 4

mlir/test/Target/LLVMIR/Import/data-layout.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ target datalayout = ""
2929
; CHECK-DAG: !llvm.ptr<271> = dense<32> : vector<4xi64>
3030
; CHECK-DAG: !llvm.ptr<272> = dense<64> : vector<4xi64>
3131
; CHECK-DAG: "dlti.stack_alignment" = 128 : i64
32+
; CHECK-DAG: "dlti.mangling_mode" = "e"
3233
target datalayout = "e-m:e-p270:32:64-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
3334

3435
; // -----

mlir/test/Target/LLVMIR/Import/import-failure.ll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,12 @@ target datalayout = "e-ni:42-i64:64"
344344

345345
; // -----
346346

347+
; CHECK: import-failure.ll
348+
; CHECK-SAME: malformed specification, must be of the form "m:<mangling>"
349+
target datalayout = "e-m-i64:64"
350+
351+
; // -----
352+
347353
; CHECK: <unknown>
348354
; CHECK-SAME: incompatible call and callee types: '!llvm.func<void (i64)>' and '!llvm.func<void (ptr)>'
349355
define void @incompatible_call_and_callee_types() {

mlir/test/Target/LLVMIR/data-layout.mlir

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// CHECK: E-
55
// CHECK: A4-
66
// CHECK: S128-
7+
// CHECK: m:e-
78
// CHECK: i64:64:128
89
// CHECK: f80:128:256
910
// CHECK: p0:32:64:128:32
@@ -12,6 +13,7 @@ module attributes {dlti.dl_spec = #dlti.dl_spec<
1213
#dlti.dl_entry<"dlti.endianness", "big">,
1314
#dlti.dl_entry<"dlti.alloca_memory_space", 4 : ui32>,
1415
#dlti.dl_entry<"dlti.stack_alignment", 128 : i32>,
16+
#dlti.dl_entry<"dlti.mangling_mode", "e">,
1517
#dlti.dl_entry<index, 64>,
1618
#dlti.dl_entry<i64, dense<[64,128]> : vector<2xi64>>,
1719
#dlti.dl_entry<f80, dense<[128,256]> : vector<2xi64>>,

mlir/test/lib/Dialect/DLTI/TestDataLayoutQuery.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ struct TestDataLayoutQuery
4343
uint64_t index = layout.getTypeIndexBitwidth(op.getType()).value_or(0);
4444
Attribute endianness = layout.getEndianness();
4545
Attribute allocaMemorySpace = layout.getAllocaMemorySpace();
46+
Attribute manglingMode = layout.getManglingMode();
4647
Attribute programMemorySpace = layout.getProgramMemorySpace();
4748
Attribute globalMemorySpace = layout.getGlobalMemorySpace();
4849
uint64_t stackAlignment = layout.getStackAlignment();
@@ -72,6 +73,9 @@ struct TestDataLayoutQuery
7273
allocaMemorySpace == Attribute()
7374
? builder.getUI32IntegerAttr(0)
7475
: allocaMemorySpace),
76+
builder.getNamedAttr("mangling_mode", manglingMode == Attribute()
77+
? builder.getStringAttr("")
78+
: manglingMode),
7579
builder.getNamedAttr("program_memory_space",
7680
programMemorySpace == Attribute()
7781
? builder.getUI32IntegerAttr(0)

mlir/unittests/Interfaces/DataLayoutInterfacesTest.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ constexpr static llvm::StringLiteral kAttrName = "dltest.layout";
2525
constexpr static llvm::StringLiteral kEndiannesKeyName = "dltest.endianness";
2626
constexpr static llvm::StringLiteral kAllocaKeyName =
2727
"dltest.alloca_memory_space";
28+
constexpr static llvm::StringLiteral kManglingModeKeyName =
29+
"dltest.mangling_mode";
2830
constexpr static llvm::StringLiteral kProgramKeyName =
2931
"dltest.program_memory_space";
3032
constexpr static llvm::StringLiteral kGlobalKeyName =
@@ -83,6 +85,9 @@ struct CustomDataLayoutSpec
8385
StringAttr getAllocaMemorySpaceIdentifier(MLIRContext *context) const {
8486
return Builder(context).getStringAttr(kAllocaKeyName);
8587
}
88+
StringAttr getManglingModeIdentifier(MLIRContext *context) const {
89+
return Builder(context).getStringAttr(kManglingModeKeyName);
90+
}
8691
StringAttr getProgramMemorySpaceIdentifier(MLIRContext *context) const {
8792
return Builder(context).getStringAttr(kProgramKeyName);
8893
}
@@ -474,6 +479,7 @@ module {}
474479
EXPECT_EQ(layout.getProgramMemorySpace(), Attribute());
475480
EXPECT_EQ(layout.getGlobalMemorySpace(), Attribute());
476481
EXPECT_EQ(layout.getStackAlignment(), 0u);
482+
EXPECT_EQ(layout.getManglingMode(), Attribute());
477483
}
478484

479485
TEST(DataLayout, NullSpec) {
@@ -506,6 +512,7 @@ TEST(DataLayout, NullSpec) {
506512
EXPECT_EQ(layout.getProgramMemorySpace(), Attribute());
507513
EXPECT_EQ(layout.getGlobalMemorySpace(), Attribute());
508514
EXPECT_EQ(layout.getStackAlignment(), 0u);
515+
EXPECT_EQ(layout.getManglingMode(), Attribute());
509516

510517
EXPECT_EQ(layout.getDevicePropertyValue(
511518
Builder(&ctx).getStringAttr("CPU" /* device ID*/),
@@ -546,6 +553,7 @@ TEST(DataLayout, EmptySpec) {
546553
EXPECT_EQ(layout.getProgramMemorySpace(), Attribute());
547554
EXPECT_EQ(layout.getGlobalMemorySpace(), Attribute());
548555
EXPECT_EQ(layout.getStackAlignment(), 0u);
556+
EXPECT_EQ(layout.getManglingMode(), Attribute());
549557

550558
EXPECT_EQ(layout.getDevicePropertyValue(
551559
Builder(&ctx).getStringAttr("CPU" /* device ID*/),
@@ -567,7 +575,8 @@ TEST(DataLayout, SpecWithEntries) {
567575
#dlti.dl_entry<"dltest.alloca_memory_space", 5 : i32>,
568576
#dlti.dl_entry<"dltest.program_memory_space", 3 : i32>,
569577
#dlti.dl_entry<"dltest.global_memory_space", 2 : i32>,
570-
#dlti.dl_entry<"dltest.stack_alignment", 128 : i32>
578+
#dlti.dl_entry<"dltest.stack_alignment", 128 : i32>,
579+
#dlti.dl_entry<"dltest.mangling_mode", "o">
571580
> } : () -> ()
572581
)MLIR";
573582

@@ -604,6 +613,7 @@ TEST(DataLayout, SpecWithEntries) {
604613
EXPECT_EQ(layout.getProgramMemorySpace(), Builder(&ctx).getI32IntegerAttr(3));
605614
EXPECT_EQ(layout.getGlobalMemorySpace(), Builder(&ctx).getI32IntegerAttr(2));
606615
EXPECT_EQ(layout.getStackAlignment(), 128u);
616+
EXPECT_EQ(layout.getManglingMode(), Builder(&ctx).getStringAttr("o"));
607617
}
608618

609619
TEST(DataLayout, SpecWithTargetSystemDescEntries) {

0 commit comments

Comments
 (0)