Skip to content

Commit a6cb7fb

Browse files
committed
Fix cyclic dependency issue through a coarse type
1 parent 1f29675 commit a6cb7fb

File tree

10 files changed

+120
-165
lines changed

10 files changed

+120
-165
lines changed

mlir/include/mlir/Dialect/LLVMIR/CMakeLists.txt

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,12 @@ mlir_tablegen(LLVMOpsDialect.h.inc -gen-dialect-decls)
77
mlir_tablegen(LLVMOpsDialect.cpp.inc -gen-dialect-defs)
88
mlir_tablegen(LLVMOpsEnums.h.inc -gen-enum-decls)
99
mlir_tablegen(LLVMOpsEnums.cpp.inc -gen-enum-defs)
10-
#For LLVMOpsAttrDefs.h.inc, see below.
10+
mlir_tablegen(LLVMOpsAttrDefs.h.inc -gen-attrdef-decls
11+
-attrdefs-dialect=llvm)
1112
mlir_tablegen(LLVMOpsAttrDefs.cpp.inc -gen-attrdef-defs
1213
-attrdefs-dialect=llvm)
1314
add_public_tablegen_target(MLIRLLVMOpsIncGen)
1415

15-
# NB: Separate out LLVMOpsAttrDefs.h.inc generation as generating it
16-
# through LLVMOps.td ends up defining LLVMTargetFeaturesAttr even
17-
# though LLVMTargetFeaturesAttrDefs.* is responsible for that.
18-
set(LLVM_TARGET_DEFINITIONS LLVMAttrAndEnumDefs.td)
19-
mlir_tablegen(LLVMOpsAttrDefs.h.inc -gen-attrdef-decls -attrdefs-dialect=llvm)
20-
add_public_tablegen_target(MLIRLLVMAttrsIncGen)
21-
22-
# NB: LLVMTargetFeaturesAttr is split out into its own file
23-
# to break a recursive dependency: LLVMInterfaces depends
24-
# on it, and other LLVMAttrs depending on LLVMInterfaces.
25-
set(LLVM_TARGET_DEFINITIONS LLVMTargetFeaturesAttrDefs.td)
26-
mlir_tablegen(LLVMTargetFeaturesAttrDefs.h.inc -gen-attrdef-decls)
27-
mlir_tablegen(LLVMTargetFeaturesAttrDefs.cpp.inc -gen-attrdef-defs)
28-
add_public_tablegen_target(MLIRLLVMTargetFeaturesAttrsIncGen)
29-
3016
set(LLVM_TARGET_DEFINITIONS LLVMTypes.td)
3117
mlir_tablegen(LLVMTypes.h.inc -gen-typedef-decls -typedefs-dialect=llvm)
3218
mlir_tablegen(LLVMTypes.cpp.inc -gen-typedef-defs -typedefs-dialect=llvm)

mlir/include/mlir/Dialect/LLVMIR/LLVMAttrAndEnumDefs.td

Lines changed: 0 additions & 10 deletions
This file was deleted.

mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ include "mlir/IR/AttrTypeBase.td"
1515
include "mlir/IR/CommonAttrConstraints.td"
1616
include "mlir/Interfaces/DataLayoutInterfaces.td"
1717

18+
// All of the attributes will extend this class.
19+
class LLVM_Attr<string name, string attrMnemonic,
20+
list<Trait> traits = [],
21+
string baseCppClass = "::mlir::Attribute">
22+
: AttrDef<LLVM_Dialect, name, traits, baseCppClass> {
23+
let mnemonic = attrMnemonic;
24+
}
25+
1826
//===----------------------------------------------------------------------===//
1927
// CConvAttr
2028
//===----------------------------------------------------------------------===//
@@ -1234,6 +1242,73 @@ def LLVM_VScaleRangeAttr : LLVM_Attr<"VScaleRange", "vscale_range"> {
12341242
let assemblyFormat = "`<` struct(params) `>`";
12351243
}
12361244

1245+
//===----------------------------------------------------------------------===//
1246+
// TargetFeaturesAttr
1247+
//===----------------------------------------------------------------------===//
1248+
1249+
def LLVM_TargetFeaturesAttr : LLVM_Attr<"TargetFeatures", "target_features",
1250+
[DLTIQueryInterface]>
1251+
{
1252+
let summary = "LLVM target features attribute";
1253+
1254+
let description = [{
1255+
Represents the LLVM target features as a list that can be checked within
1256+
passes/rewrites.
1257+
1258+
Example:
1259+
```mlir
1260+
#llvm.target_features<["+sme", "+sve", "+sme-f64f64"]>
1261+
```
1262+
1263+
Then within a pass or rewrite the features active at an op can be queried:
1264+
1265+
```c++
1266+
auto targetFeatures = LLVM::TargetFeaturesAttr::featuresAt(op);
1267+
1268+
if (!targetFeatures.contains("+sme-f64f64"))
1269+
return failure();
1270+
```
1271+
}];
1272+
1273+
let parameters = (ins OptionalArrayRefParameter<"StringAttr">:$features);
1274+
1275+
let builders = [
1276+
TypeBuilder<(ins "::llvm::StringRef":$features)>,
1277+
TypeBuilder<(ins "::llvm::ArrayRef<::llvm::StringRef>":$features)>
1278+
];
1279+
1280+
let extraClassDeclaration = [{
1281+
/// Checks if a feature is contained within the features list.
1282+
/// Note: Using a StringAttr allows doing pointer-comparisons.
1283+
bool contains(::mlir::StringAttr feature) const;
1284+
bool contains(::llvm::StringRef feature) const;
1285+
1286+
bool nullOrEmpty() const {
1287+
// Checks if this attribute is null, or the features are empty.
1288+
return !bool(*this) || getFeatures().empty();
1289+
}
1290+
1291+
/// Returns the list of features as an LLVM-compatible string.
1292+
std::string getFeaturesString() const;
1293+
1294+
/// Finds the target features on the parent FunctionOpInterface.
1295+
/// Note: This assumes the attribute name matches the return value of
1296+
/// `getAttributeName()`.
1297+
static TargetFeaturesAttr featuresAt(Operation* op);
1298+
1299+
/// Canonical name for this attribute within MLIR.
1300+
static constexpr StringLiteral getAttributeName() {
1301+
return StringLiteral("target_features");
1302+
}
1303+
1304+
/// Returns the attribute associated with the key.
1305+
FailureOr<Attribute> query(DataLayoutEntryKey key);
1306+
}];
1307+
1308+
let assemblyFormat = "`<` `[` (`]`) : ($features^ `]`)? `>`";
1309+
let genVerifyDecl = 1;
1310+
}
1311+
12371312
//===----------------------------------------------------------------------===//
12381313
// TargetAttr
12391314
//===----------------------------------------------------------------------===//

mlir/include/mlir/Dialect/LLVMIR/LLVMAttrs.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,9 @@ class TBAANodeAttr : public Attribute {
9292
using cconv::CConv;
9393
using linkage::Linkage;
9494
using tailcallkind::TailCallKind;
95-
96-
class TargetFeaturesAttr;
9795
} // namespace LLVM
9896
} // namespace mlir
9997

100-
#define GET_ATTRDEF_CLASSES
101-
#include "mlir/Dialect/LLVMIR/LLVMTargetFeaturesAttrDefs.h.inc"
102-
10398
#include "mlir/Dialect/LLVMIR/LLVMAttrInterfaces.h.inc"
10499

105100
#define GET_ATTRDEF_CLASSES

mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#define LLVMIR_DIALECT
1111

1212
include "mlir/IR/DialectBase.td"
13-
include "mlir/IR/AttrTypeBase.td"
1413

1514
def LLVM_Dialect : Dialect {
1615
let name = "llvm";
@@ -128,12 +127,4 @@ def LLVM_Dialect : Dialect {
128127
}];
129128
}
130129

131-
// All of the attributes will extend this class.
132-
class LLVM_Attr<string name, string attrMnemonic,
133-
list<Trait> traits = [],
134-
string baseCppClass = "::mlir::Attribute">
135-
: AttrDef<LLVM_Dialect, name, traits, baseCppClass> {
136-
let mnemonic = attrMnemonic;
137-
}
138-
139130
#endif // LLVMIR_DIALECT

mlir/include/mlir/Dialect/LLVMIR/LLVMInterfaces.td

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,10 @@ def LLVM_TargetAttrInterface
560560
/*args=*/(ins)
561561
>,
562562
InterfaceMethod<
563-
/*description=*/"Returns the target features as a string.",
564-
/*retTy=*/"LLVM::TargetFeaturesAttr",
563+
/*description=*/"Returns the target features as a TargetFeaturesAttr.",
564+
/*retTy=*/"Attribute", // NB: will be a LLVM::TargetFeaturesAttr, though
565+
// need to work around a cyclic dependency on
566+
// LLVMInterfaces.td and LLVMAttrDefs.td.
565567
/*methodName=*/"getFeatures",
566568
/*args=*/(ins)
567569
>

mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#define LLVMIR_OPS
1515

1616
include "mlir/Dialect/LLVMIR/LLVMAttrDefs.td"
17-
include "mlir/Dialect/LLVMIR/LLVMTargetFeaturesAttrDefs.td"
1817
include "mlir/Dialect/LLVMIR/LLVMEnums.td"
1918
include "mlir/Dialect/LLVMIR/LLVMOpBase.td"
2019
include "mlir/IR/EnumAttr.td"

mlir/include/mlir/Dialect/LLVMIR/LLVMTargetFeaturesAttrDefs.td

Lines changed: 0 additions & 89 deletions
This file was deleted.

mlir/lib/Dialect/LLVMIR/IR/LLVMAttrs.cpp

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -374,35 +374,6 @@ TargetFeaturesAttr TargetFeaturesAttr::featuresAt(Operation *op) {
374374
getAttributeName());
375375
}
376376

377-
LogicalResult
378-
ModuleFlagAttr::verify(function_ref<InFlightDiagnostic()> emitError,
379-
LLVM::ModFlagBehavior flagBehavior, StringAttr key,
380-
Attribute value) {
381-
if (key == LLVMDialect::getModuleFlagKeyCGProfileName()) {
382-
auto arrayAttr = dyn_cast<ArrayAttr>(value);
383-
if ((!arrayAttr) || (!llvm::all_of(arrayAttr, [](Attribute attr) {
384-
return isa<ModuleFlagCGProfileEntryAttr>(attr);
385-
})))
386-
return emitError()
387-
<< "'CG Profile' key expects an array of '#llvm.cgprofile_entry'";
388-
return success();
389-
}
390-
391-
if (key == LLVMDialect::getModuleFlagKeyProfileSummaryName()) {
392-
if (!isa<ModuleFlagProfileSummaryAttr>(value))
393-
return emitError() << "'ProfileSummary' key expects a "
394-
"'#llvm.profile_summary' attribute";
395-
return success();
396-
}
397-
398-
if (isa<IntegerAttr, StringAttr>(value))
399-
return success();
400-
401-
return emitError() << "only integer and string values are currently "
402-
"supported for unknown key '"
403-
<< key << "'";
404-
}
405-
406377
FailureOr<Attribute> TargetFeaturesAttr::query(DataLayoutEntryKey key) {
407378
if (auto stringKey = dyn_cast<StringAttr>(key)) {
408379
if (contains(stringKey))
@@ -418,7 +389,7 @@ FailureOr<Attribute> TargetFeaturesAttr::query(DataLayoutEntryKey key) {
418389
}
419390

420391
//===----------------------------------------------------------------------===//
421-
// LLVM_TargetAttr
392+
// TargetAttr
422393
//===----------------------------------------------------------------------===//
423394

424395
FailureOr<::mlir::Attribute> TargetAttr::query(DataLayoutEntryKey key) {
@@ -430,6 +401,38 @@ FailureOr<::mlir::Attribute> TargetAttr::query(DataLayoutEntryKey key) {
430401
if (stringAttrKey.getValue() == "features" && getFeatures())
431402
return getFeatures();
432403
}
433-
434404
return failure();
435405
}
406+
407+
//===----------------------------------------------------------------------===//
408+
// ModuleFlagAttr
409+
//===----------------------------------------------------------------------===//
410+
411+
LogicalResult
412+
ModuleFlagAttr::verify(function_ref<InFlightDiagnostic()> emitError,
413+
LLVM::ModFlagBehavior flagBehavior, StringAttr key,
414+
Attribute value) {
415+
if (key == LLVMDialect::getModuleFlagKeyCGProfileName()) {
416+
auto arrayAttr = dyn_cast<ArrayAttr>(value);
417+
if ((!arrayAttr) || (!llvm::all_of(arrayAttr, [](Attribute attr) {
418+
return isa<ModuleFlagCGProfileEntryAttr>(attr);
419+
})))
420+
return emitError()
421+
<< "'CG Profile' key expects an array of '#llvm.cgprofile_entry'";
422+
return success();
423+
}
424+
425+
if (key == LLVMDialect::getModuleFlagKeyProfileSummaryName()) {
426+
if (!isa<ModuleFlagProfileSummaryAttr>(value))
427+
return emitError() << "'ProfileSummary' key expects a "
428+
"'#llvm.profile_summary' attribute";
429+
return success();
430+
}
431+
432+
if (isa<IntegerAttr, StringAttr>(value))
433+
return success();
434+
435+
return emitError() << "only integer and string values are currently "
436+
"supported for unknown key '"
437+
<< key << "'";
438+
}

mlir/lib/Target/LLVMIR/Transforms/TargetUtils.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@ FailureOr<std::unique_ptr<llvm::TargetMachine>>
3838
getTargetMachine(mlir::LLVM::TargetAttrInterface attr) {
3939
StringRef triple = attr.getTriple();
4040
StringRef chipAKAcpu = attr.getChip();
41-
std::string features =
42-
attr.getFeatures() ? attr.getFeatures().getFeaturesString() : "";
41+
// NB: `TargetAttrInterface::getFeatures()` is coarsely typed to work around
42+
// cyclic dependency issue in tablegen files.
43+
auto featuresAttr =
44+
llvm::cast_or_null<LLVM::TargetFeaturesAttr>(attr.getFeatures());
45+
std::string features = featuresAttr ? featuresAttr.getFeaturesString() : "";
4346

4447
std::string error;
4548
const llvm::Target *target =

0 commit comments

Comments
 (0)