Skip to content

Commit d95c6ed

Browse files
committed
[MLIR][LLVMIR][DLTI] Pass for updating target features per TargetMachine
1 parent 60ee056 commit d95c6ed

File tree

11 files changed

+68
-72
lines changed

11 files changed

+68
-72
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,26 @@ 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-
mlir_tablegen(LLVMOpsAttrDefs.h.inc -gen-attrdef-decls
11-
-attrdefs-dialect=llvm)
10+
#For LLVMOpsAttrDefs.h.inc, see below.
1211
mlir_tablegen(LLVMOpsAttrDefs.cpp.inc -gen-attrdef-defs
1312
-attrdefs-dialect=llvm)
1413
add_public_tablegen_target(MLIRLLVMOpsIncGen)
1514

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+
1630
set(LLVM_TARGET_DEFINITIONS LLVMTypes.td)
1731
mlir_tablegen(LLVMTypes.h.inc -gen-typedef-decls -typedefs-dialect=llvm)
1832
mlir_tablegen(LLVMTypes.cpp.inc -gen-typedef-defs -typedefs-dialect=llvm)

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

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,70 +1243,7 @@ def LLVM_VScaleRangeAttr : LLVM_Attr<"VScaleRange", "vscale_range"> {
12431243
}
12441244

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

13121249
def LLVM_TargetAttr : LLVM_Attr<"Target", "target",
@@ -1324,7 +1261,7 @@ def LLVM_TargetAttr : LLVM_Attr<"Target", "target",
13241261
}];
13251262
let parameters = (ins "StringAttr":$triple,
13261263
"StringAttr":$chip,
1327-
OptionalParameter<"StringAttr", "">:$features);
1264+
OptionalParameter<"TargetFeaturesAttr", "">:$features);
13281265

13291266
let assemblyFormat = [{`<` struct($triple, $chip, $features) `>`}];
13301267

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

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

100+
#define GET_ATTRDEF_CLASSES
101+
#include "mlir/Dialect/LLVMIR/LLVMTargetFeaturesAttrDefs.h.inc"
102+
98103
#include "mlir/Dialect/LLVMIR/LLVMAttrInterfaces.h.inc"
99104

100105
#define GET_ATTRDEF_CLASSES

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

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

1212
include "mlir/IR/DialectBase.td"
13+
include "mlir/IR/AttrTypeBase.td"
1314

1415
def LLVM_Dialect : Dialect {
1516
let name = "llvm";
@@ -127,4 +128,12 @@ def LLVM_Dialect : Dialect {
127128
}];
128129
}
129130

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+
130139
#endif // LLVMIR_DIALECT

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ def LLVM_TargetAttrInterface
561561
>,
562562
InterfaceMethod<
563563
/*description=*/"Returns the target features as a string.",
564-
/*retTy=*/"StringAttr",
564+
/*retTy=*/"LLVM::TargetFeaturesAttr",
565565
/*methodName=*/"getFeatures",
566566
/*args=*/(ins)
567567
>

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

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

1616
include "mlir/Dialect/LLVMIR/LLVMAttrDefs.td"
17+
include "mlir/Dialect/LLVMIR/LLVMTargetFeaturesAttrDefs.td"
1718
include "mlir/Dialect/LLVMIR/LLVMEnums.td"
1819
include "mlir/Dialect/LLVMIR/LLVMOpBase.td"
1920
include "mlir/IR/EnumAttr.td"

mlir/include/mlir/Target/LLVMIR/Transforms/Passes.td

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,18 @@ def LLVMTargetToDataLayout : Pass<"llvm-target-to-data-layout"> {
2727
];
2828
}
2929

30+
def LLVMFeaturesFromTarget : Pass<"llvm-features-from-target"> {
31+
let summary = "TODO";
32+
let dependentDialects = ["mlir::DLTIDialect"];
33+
let description = [{
34+
TODO
35+
}];
36+
let options = [
37+
Option<"initializeLLVMTargets", "initialize-llvm-targets", "bool",
38+
/*default=*/"true",
39+
"Whether to pre-load all available target machines, that LLVM is "
40+
"configured to support, into the TargetRegistry.">
41+
];
42+
}
43+
3044
#endif // MLIR_TARGET_LLVMIR_TRANSFORMS_PASSES

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,20 @@ ModuleFlagAttr::verify(function_ref<InFlightDiagnostic()> emitError,
403403
<< key << "'";
404404
}
405405

406+
FailureOr<Attribute> TargetFeaturesAttr::query(DataLayoutEntryKey key) {
407+
if (auto stringKey = dyn_cast<StringAttr>(key)) {
408+
if (contains(stringKey))
409+
return UnitAttr::get(getContext());
410+
411+
if (contains((std::string("+") + stringKey.strref()).str()))
412+
return BoolAttr::get(getContext(), true);
413+
414+
if (contains((std::string("-") + stringKey.strref()).str()))
415+
return BoolAttr::get(getContext(), false);
416+
}
417+
return failure();
418+
}
419+
406420
//===----------------------------------------------------------------------===//
407421
// LLVM_TargetAttr
408422
//===----------------------------------------------------------------------===//

mlir/lib/Target/LLVMIR/Transforms/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
add_mlir_dialect_library(MLIRTargetLLVMIRTransforms
22
TargetToDataLayout.cpp
3+
FeaturesFromTarget.cpp
34

45
DEPENDS
56
MLIRTargetLLVMIRTransformsIncGen

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ static FailureOr<std::unique_ptr<llvm::TargetMachine>>
3333
getTargetMachine(LLVM::TargetAttrInterface attr) {
3434
StringRef triple = attr.getTriple();
3535
StringRef chipAKAcpu = attr.getChip();
36-
StringRef features = attr.getFeatures() ? attr.getFeatures().getValue() : "";
36+
std::string features =
37+
attr.getFeatures() ? attr.getFeatures().getFeaturesString() : "";
3738

3839
std::string error;
3940
const llvm::Target *target =

0 commit comments

Comments
 (0)