Rolf Morel cbfa265e98
[MLIR][LLVMIR][DLTI] Add LLVM::TargetAttrInterface and #llvm.target attr (#145899)
Adds the `#llvm.target<triple = $TRIPLE, chip = $CHIP, features =
$FEATURES>` attribute and along with a `-llvm-target-to-data-layout`
pass to derive a MLIR data layout from the LLVM data layout string
(using the existing `DataLayoutImporter`). The attribute implements the
relevant DLTI-interfaces, to expose the `triple`, `chip` (AKA `cpu`) and
`features` on `#llvm.target` and the full `DataLayoutSpecInterface`. The
pass combines the generated `#dlti.dl_spec` with an existing `dl_spec`
in case one is already present, e.g. a `dl_spec` which is there to
specify size of the `index` type.

Adds a `TargetAttrInterface` which can be implemented by all attributes
representing LLVM targets.

Similar to the Draft PR https://github.com/llvm/llvm-project/pull/78073.

RFC on which this PR is based:
https://discourse.llvm.org/t/mandatory-data-layout-in-the-llvm-dialect/85875
2025-08-20 22:00:30 +01:00

35 lines
1.3 KiB
C++

//===- Traits.cpp - Traits for MLIR DLTI dialect --------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/DLTI/Traits.h"
#include "mlir/Dialect/DLTI/DLTI.h"
#include "mlir/Interfaces/DataLayoutInterfaces.h"
using namespace mlir;
LogicalResult mlir::impl::verifyHasDefaultDLTIDataLayoutTrait(Operation *op) {
// TODO: consider having trait inheritance so that HasDefaultDLTIDataLayout
// trait can inherit DataLayoutOpInterface::Trait and enforce the validity of
// the assertion below.
assert(
isa<DataLayoutOpInterface>(op) &&
"HasDefaultDLTIDataLayout trait unexpectedly attached to an op that does "
"not implement DataLayoutOpInterface");
return success();
}
DataLayoutSpecInterface mlir::impl::getDataLayoutSpec(Operation *op) {
return op->getAttrOfType<DataLayoutSpecInterface>(
DLTIDialect::kDataLayoutAttrName);
}
TargetSystemSpecInterface mlir::impl::getTargetSystemSpec(Operation *op) {
return op->getAttrOfType<TargetSystemSpecAttr>(
DLTIDialect::kTargetSystemDescAttrName);
}