[RFC] Add llvm.target_features attribute


The motivation for this comes from the Arm SME dialect where we would like a convenient way to check what variants of an operation are available based on the CPU features.

The LLVM dialect seems like the clear place for this attribute, as it models the LLVM feature flags, and can directly map to the target-features attribue.

Intended usage:

The target_features attribute is populated manually or by a pass:

func.func @example() 
  attributes { target_features = #llvm.target_features<["+sme", "+sve", "+sme-f64f64"]> }
// ...

Then within a later rewrite the attribute can be checked, and used to make lowering decisions.

// Finds the "target_features" attribute on the parent
// FunctionOpInterface via a static helper on TargetFeaturesAttr.
auto targetFeatures = LLVM::TargetFeaturesAttr::featuresAt(op);

// Check a feature.
// Returns false if targetFeatures is null or the feature is not in
// the list.
if (!targetFeatures.contains("+sme-f64f64"))
  return failure();

For now, this could be rather simple and just check if the exact feature is in the list, though it could be possible to extend with implied features using information from LLVM.


PR: [mlir][llvm] Add llvm.target_features features attribute by MacDue · Pull Request #71510 · llvm/llvm-project · GitHub


Would it make sense to add this to the DLTI (Data Layout and Target Information) system instead? That’s ostensibly what it’s there for

1 Like

This is good to have. Indeed, it would be nice to think about integrating this into the DLTI mechanism. It is not limited to modules and can be attached to other ops. I can live with it being a separate attribute while we find a way to integrate.