Skip to content

Commit 7aafd7b

Browse files
committed
Address @Dinistro and @ftynse's comments and fix flang DLTI tests
1 parent 4c2f6d4 commit 7aafd7b

File tree

6 files changed

+34
-22
lines changed

6 files changed

+34
-22
lines changed

flang/test/Fir/tco-default-datalayout.fir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ module {
77
// CHECK: module attributes {
88
// CHECK-SAME: dlti.dl_spec = #dlti.dl_spec<
99
// ...
10-
// CHECK-SAME: #dlti.dl_entry<i64, dense<[32, 64]> : vector<2xi64>>,
10+
// CHECK-SAME: i64 = dense<[32, 64]> : vector<2xi64>,
1111
// ...
1212
// CHECK-SAME: llvm.data_layout = ""

flang/test/Fir/tco-explicit-datalayout.fir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ module attributes {llvm.data_layout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i6
88
// CHECK: module attributes {
99
// CHECK-SAME: dlti.dl_spec = #dlti.dl_spec<
1010
// ...
11-
// CHECK-SAME: #dlti.dl_entry<i64, dense<128> : vector<2xi64>>,
11+
// CHECK-SAME: i64 = dense<128> : vector<2xi64>,
1212
// ...
1313
// CHECK-SAME: llvm.data_layout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:128-i128:128-f80:128-n8:16:32:64-S128"

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def DLTI_DataLayoutSpecAttr :
8888

8989
/// Returns the attribute associated with the key.
9090
FailureOr<Attribute> query(DataLayoutEntryKey key) {
91-
return llvm::cast<mlir::DataLayoutSpecInterface>(*this).queryHelper(key);
91+
return ::llvm::cast<mlir::DataLayoutSpecInterface>(*this).queryHelper(key);
9292
}
9393
}];
9494
}
@@ -194,7 +194,7 @@ def DLTI_TargetSystemSpecAttr :
194194

195195
/// Returns the attribute associated with the key.
196196
FailureOr<Attribute> query(DataLayoutEntryKey key) const {
197-
return llvm::cast<mlir::TargetSystemSpecInterface>(*this).queryHelper(key);
197+
return ::llvm::cast<mlir::TargetSystemSpecInterface>(*this).queryHelper(key);
198198
}
199199
}];
200200
let extraClassDefinition = [{
@@ -204,7 +204,7 @@ def DLTI_TargetSystemSpecAttr :
204204
for (const auto& entry : getEntries()) {
205205
if (entry.getKey() == DataLayoutEntryKey(deviceID))
206206
if (auto deviceSpec =
207-
llvm::dyn_cast<TargetDeviceSpecInterface>(entry.getValue()))
207+
::llvm::dyn_cast<TargetDeviceSpecInterface>(entry.getValue()))
208208
return deviceSpec;
209209
}
210210
return std::nullopt;
@@ -239,7 +239,7 @@ def DLTI_TargetDeviceSpecAttr :
239239
let extraClassDeclaration = [{
240240
/// Returns the attribute associated with the key.
241241
FailureOr<Attribute> query(DataLayoutEntryKey key) const {
242-
return llvm::cast<mlir::TargetDeviceSpecInterface>(*this).queryHelper(key);
242+
return ::llvm::cast<mlir::TargetDeviceSpecInterface>(*this).queryHelper(key);
243243
}
244244
}];
245245
}

mlir/include/mlir/Interfaces/DataLayoutInterfaces.td

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ def TargetDeviceSpecInterface : AttrInterface<"TargetDeviceSpecInterface", [DLTI
276276
/// Helper for default implementation of `DLTIQueryInterface`'s `query`.
277277
::mlir::FailureOr<::mlir::Attribute>
278278
queryHelper(::mlir::DataLayoutEntryKey key) const {
279-
if (auto strKey = llvm::dyn_cast<StringAttr>(key))
279+
if (auto strKey = ::llvm::dyn_cast<StringAttr>(key))
280280
if (DataLayoutEntryInterface spec = getSpecForIdentifier(strKey))
281281
return spec.getValue();
282282
return ::mlir::failure();
@@ -304,7 +304,7 @@ def TargetSystemSpecInterface : AttrInterface<"TargetSystemSpecInterface", [DLTI
304304
let methods = [
305305
InterfaceMethod<
306306
/*description=*/"Returns the list of layout entries.",
307-
/*retTy=*/"llvm::ArrayRef<DataLayoutEntryInterface>",
307+
/*retTy=*/"::llvm::ArrayRef<DataLayoutEntryInterface>",
308308
/*methodName=*/"getEntries",
309309
/*args=*/(ins)
310310
>,
@@ -334,7 +334,7 @@ def TargetSystemSpecInterface : AttrInterface<"TargetSystemSpecInterface", [DLTI
334334
/// Helper for default implementation of `DLTIQueryInterface`'s `query`.
335335
::mlir::FailureOr<::mlir::Attribute>
336336
queryHelper(::mlir::DataLayoutEntryKey key) const {
337-
if (auto strKey = llvm::dyn_cast<::mlir::StringAttr>(key))
337+
if (auto strKey = ::llvm::dyn_cast<::mlir::StringAttr>(key))
338338
if (auto deviceSpec = getDeviceSpecForDeviceID(strKey))
339339
return *deviceSpec;
340340
return ::mlir::failure();

mlir/lib/Dialect/DLTI/DLTI.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@ using namespace mlir;
3030
#define DEBUG_TYPE "dlti"
3131

3232
//===----------------------------------------------------------------------===//
33-
// parsing
33+
// Common parsing utility functions.
3434
//===----------------------------------------------------------------------===//
3535

36+
/// Parse an entry which can either be of the form `key = value` or a
37+
/// #dlti.dl_entry attribute. When `tryType=true` the key can be a type,
38+
/// otherwise only quoted strings are allowed. The grammar is as follows:
39+
/// entry ::= ((type | quoted-string) `=` attr) | dl-entry-attr
3640
static ParseResult parseKeyValuePair(AsmParser &parser,
3741
DataLayoutEntryInterface &entry,
3842
bool tryType = false) {
@@ -79,6 +83,12 @@ static ParseResult parseKeyValuePair(AsmParser &parser,
7983
<< "failed to parse DLTI entry";
8084
}
8185

86+
/// Construct a requested attribute by parsing list of entries occurring within
87+
/// a pair of `<` and `>`, optionally allow types as keys and an empty list.
88+
/// The grammar is as follows:
89+
/// bracketed-entry-list ::=`<` entry-list `>`
90+
/// entry-list ::= | entry | entry `,` entry-list
91+
/// entry ::= ((type | quoted-string) `=` attr) | dl-entry-attr
8292
template <class Attr>
8393
static Attribute parseAngleBracketedEntries(AsmParser &parser, Type ty,
8494
bool tryType = false,
@@ -100,18 +110,19 @@ static Attribute parseAngleBracketedEntries(AsmParser &parser, Type ty,
100110
}
101111

102112
//===----------------------------------------------------------------------===//
103-
// printing
113+
// Common printing utility functions.
104114
//===----------------------------------------------------------------------===//
105115

116+
/// Convert pointer-union keys to strings.
106117
static inline std::string keyToStr(DataLayoutEntryKey key) {
107118
std::string buf;
108-
llvm::TypeSwitch<DataLayoutEntryKey>(key)
119+
TypeSwitch<DataLayoutEntryKey>(key)
109120
.Case<StringAttr, Type>( // The only two kinds of key we know of.
110-
[&](auto key) { llvm::raw_string_ostream(buf) << key; })
111-
.Default([](auto) { llvm_unreachable("unexpected entry key kind"); });
121+
[&](auto key) { llvm::raw_string_ostream(buf) << key; });
112122
return buf;
113123
}
114124

125+
/// Pretty-print entries, each in `key = value` format, separated by commas.
115126
template <class T>
116127
static void printAngleBracketedEntries(AsmPrinter &os, T &&entries) {
117128
os << "<";
@@ -122,9 +133,10 @@ static void printAngleBracketedEntries(AsmPrinter &os, T &&entries) {
122133
}
123134

124135
//===----------------------------------------------------------------------===//
125-
// verifying
136+
// Common verifying utility functions.
126137
//===----------------------------------------------------------------------===//
127138

139+
/// Verify entries, with the option to disallow types as keys.
128140
static LogicalResult verifyEntries(function_ref<InFlightDiagnostic()> emitError,
129141
ArrayRef<DataLayoutEntryInterface> entries,
130142
bool allowTypes = true) {
@@ -135,7 +147,7 @@ static LogicalResult verifyEntries(function_ref<InFlightDiagnostic()> emitError,
135147
DataLayoutEntryKey key = entry.getKey();
136148
if (key.isNull())
137149
return emitError() << "contained invalid DLTI key";
138-
if (!allowTypes && llvm::dyn_cast<Type>(key))
150+
if (!allowTypes && dyn_cast<Type>(key))
139151
return emitError() << "type as DLIT key is not allowed";
140152
if (!keys.insert(key).second)
141153
return emitError() << "repeated DLTI key: " << keyToStr(key);
@@ -306,7 +318,7 @@ combineOneSpec(DataLayoutSpecInterface spec,
306318
typeSample.getContext()->getLoadedDialect<BuiltinDialect>() &&
307319
"unexpected data layout entry for built-in type");
308320

309-
auto interface = llvm::cast<DataLayoutTypeInterface>(typeSample);
321+
auto interface = cast<DataLayoutTypeInterface>(typeSample);
310322
if (!interface.areCompatible(entriesForType.lookup(kvp.first), kvp.second))
311323
return failure();
312324

@@ -340,7 +352,7 @@ DataLayoutSpecAttr
340352
DataLayoutSpecAttr::combineWith(ArrayRef<DataLayoutSpecInterface> specs) const {
341353
// Only combine with attributes of the same kind.
342354
// TODO: reconsider this when the need arises.
343-
if (llvm::any_of(specs, [](DataLayoutSpecInterface spec) {
355+
if (any_of(specs, [](DataLayoutSpecInterface spec) {
344356
return !llvm::isa<DataLayoutSpecAttr>(spec);
345357
}))
346358
return {};
@@ -488,7 +500,7 @@ getClosestQueryable(Operation *op) {
488500
// Search op and its ancestors for the first attached DLTIQueryInterface attr.
489501
do {
490502
for (NamedAttribute attr : op->getAttrs())
491-
if ((queryable = llvm::dyn_cast<DLTIQueryInterface>(attr.getValue())))
503+
if ((queryable = dyn_cast<DLTIQueryInterface>(attr.getValue())))
492504
break;
493505
} while (!queryable && (op = op->getParentOp()));
494506

@@ -519,7 +531,7 @@ dlti::query(Operation *op, ArrayRef<DataLayoutEntryKey> keys, bool emitError) {
519531

520532
Attribute currentAttr = queryable;
521533
for (auto &&[idx, key] : llvm::enumerate(keys)) {
522-
if (auto map = llvm::dyn_cast<DLTIQueryInterface>(currentAttr)) {
534+
if (auto map = dyn_cast<DLTIQueryInterface>(currentAttr)) {
523535
auto maybeAttr = map.query(key);
524536
if (failed(maybeAttr)) {
525537
if (emitError) {
@@ -565,7 +577,7 @@ class TargetDataLayoutInterface : public DataLayoutDialectInterface {
565577
Location loc) const final {
566578
StringRef entryName = entry.getKey().get<StringAttr>().strref();
567579
if (entryName == DLTIDialect::kDataLayoutEndiannessKey) {
568-
auto value = llvm::dyn_cast<StringAttr>(entry.getValue());
580+
auto value = dyn_cast<StringAttr>(entry.getValue());
569581
if (value &&
570582
(value.getValue() == DLTIDialect::kDataLayoutEndiannessBig ||
571583
value.getValue() == DLTIDialect::kDataLayoutEndiannessLittle))

mlir/lib/Interfaces/DataLayoutInterfaces.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ mlir::detail::verifyTargetSystemSpec(TargetSystemSpecInterface spec,
791791
DenseSet<TargetSystemSpecInterface::DeviceID> deviceIDs;
792792
for (const auto &entry : spec.getEntries()) {
793793
auto targetDeviceSpec =
794-
llvm::dyn_cast<TargetDeviceSpecInterface>(entry.getValue());
794+
dyn_cast<TargetDeviceSpecInterface>(entry.getValue());
795795

796796
if (!targetDeviceSpec)
797797
return failure();

0 commit comments

Comments
 (0)