Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mlir/include/mlir/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ add_subdirectory(Dialect)
add_subdirectory(IR)
add_subdirectory(Interfaces)
add_subdirectory(Reducer)
add_subdirectory(Target)
add_subdirectory(Transforms)
29 changes: 29 additions & 0 deletions mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ include "mlir/Dialect/LLVMIR/LLVMDialect.td"
include "mlir/Dialect/LLVMIR/LLVMInterfaces.td"
include "mlir/IR/AttrTypeBase.td"
include "mlir/IR/CommonAttrConstraints.td"
include "mlir/Interfaces/DataLayoutInterfaces.td"

// All of the attributes will extend this class.
class LLVM_Attr<string name, string attrMnemonic,
Expand Down Expand Up @@ -1304,6 +1305,34 @@ def LLVM_TargetFeaturesAttr : LLVM_Attr<"TargetFeatures", "target_features">
let genVerifyDecl = 1;
}

//===----------------------------------------------------------------------===//
// LLVM_TargetAttr
//===----------------------------------------------------------------------===//

def LLVM_TargetAttr : LLVM_Attr<"Target", "target",
[LLVM_TargetAttrInterface]> {
let summary = "LLVM target info: triple, chip, features";
let description = [{
An attribute to hold LLVM target information, specifying LLVM's target
`triple` string, the target `chip` string (i.e. the `cpu` string), and
target `features` string as an attribute. The latter is optional.

Responds to DLTI-queries on the keys:
* A query for `"triple"` returns the `StringAttr` for the `triple`.
* A query for `"chip"` returns the `StringAttr` for the `chip`/`cpu`.
* A query for `"features"` returns the `StringAttr`, if provided.
}];
let parameters = (ins "StringAttr":$triple,
"StringAttr":$chip,
OptionalParameter<"StringAttr", "">:$features);

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

let extraClassDeclaration = [{
FailureOr<Attribute> query(DataLayoutEntryKey key);
}];
}

//===----------------------------------------------------------------------===//
// UndefAttr
//===----------------------------------------------------------------------===//
Expand Down
3 changes: 2 additions & 1 deletion mlir/include/mlir/Dialect/LLVMIR/LLVMAttrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
#include "mlir/IR/OpImplementation.h"
#include "mlir/Interfaces/DataLayoutInterfaces.h"
#include <optional>

#include "mlir/Dialect/LLVMIR/LLVMOpsEnums.h.inc"
Expand Down Expand Up @@ -89,8 +90,8 @@ class TBAANodeAttr : public Attribute {
// TODO: this shouldn't be needed after we unify the attribute generation, i.e.
// --gen-attr-* and --gen-attrdef-*.
using cconv::CConv;
using tailcallkind::TailCallKind;
using linkage::Linkage;
using tailcallkind::TailCallKind;
} // namespace LLVM
} // namespace mlir

Expand Down
1 change: 1 addition & 0 deletions mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def LLVM_Dialect : Dialect {
);

let extraClassDeclaration = [{
static StringRef getTargetAttrName() { return "llvm.target"; }
/// Name of the data layout attributes.
static StringRef getDataLayoutAttrName() { return "llvm.data_layout"; }
static StringRef getNoAliasScopesAttrName() { return "noalias_scopes"; }
Expand Down
36 changes: 36 additions & 0 deletions mlir/include/mlir/Dialect/LLVMIR/LLVMInterfaces.td
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define LLVMIR_INTERFACES

include "mlir/IR/OpBase.td"
include "mlir/Interfaces/DataLayoutInterfaces.td"

def FastmathFlagsInterface : OpInterface<"FastmathFlagsInterface"> {
let description = [{
Expand Down Expand Up @@ -532,4 +533,39 @@ def LLVM_DIRecursiveTypeAttrInterface
];
}

def LLVM_TargetAttrInterface
: AttrInterface<"TargetAttrInterface", [DLTIQueryInterface]> {
let description = [{
Interface for attributes that describe LLVM targets.

These attributes should be able to return the specified target `triple`,
`chip` and `features`.

Implementing attributes should provide a `DLTIQueryInterface::query()`
implementation which responds to keys `"triple"`, `"chip"` and `"features"`
by returning appropriate `StringAttr`s.
}];
let cppNamespace = "::mlir::LLVM";
let methods = [
InterfaceMethod<
/*description=*/"Returns the target triple identifier.",
/*retTy=*/"StringAttr",
/*methodName=*/"getTriple",
/*args=*/(ins)
>,
InterfaceMethod<
/*description=*/"Returns the target chip (i.e. \"cpu\") identifier.",
/*retTy=*/"StringAttr",
/*methodName=*/"getChip",
/*args=*/(ins)
>,
InterfaceMethod<
/*description=*/"Returns the target features as a string.",
/*retTy=*/"StringAttr",
/*methodName=*/"getFeatures",
/*args=*/(ins)
>
];
}

#endif // LLVMIR_INTERFACES
1 change: 1 addition & 0 deletions mlir/include/mlir/Target/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(LLVMIR)
1 change: 1 addition & 0 deletions mlir/include/mlir/Target/LLVMIR/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(Transforms)
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
//
//===----------------------------------------------------------------------===//

#ifndef MLIR_LIB_TARGET_LLVMIR_DATALAYOUTIMPORTER_H_
#define MLIR_LIB_TARGET_LLVMIR_DATALAYOUTIMPORTER_H_
#ifndef MLIR_TARGET_LLVMIR_DATALAYOUTIMPORTER_H
#define MLIR_TARGET_LLVMIR_DATALAYOUTIMPORTER_H

#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
#include "mlir/IR/BuiltinAttributes.h"
Expand All @@ -38,23 +38,31 @@ namespace detail {
/// null if the bit width is not supported.
FloatType getFloatType(MLIRContext *context, unsigned width);

/// Helper class that translates an LLVM data layout to an MLIR data layout
/// specification. Only integer, float, pointer, alloca memory space, stack
/// alignment, and endianness entries are translated. The class also returns all
/// entries from the default data layout specification found in the language
/// reference (https://llvm.org/docs/LangRef.html#data-layout) if they are not
/// overwritten by the provided data layout.
/// Helper class that translates an LLVM data layout string to an MLIR data
/// layout specification. Only integer, float, pointer, alloca memory space,
/// stack alignment, and endianness entries are translated. The class also
/// returns all entries from the default data layout specification found in the
/// language reference (https://llvm.org/docs/LangRef.html#data-layout) if they
/// are not overwritten by the provided data layout.
class DataLayoutImporter {
public:
DataLayoutImporter(MLIRContext *context,
const llvm::DataLayout &llvmDataLayout)
: context(context) {
translateDataLayout(llvmDataLayout);
DataLayoutImporter(MLIRContext *context, StringRef dataLayoutStr)
: dataLayoutStr(dataLayoutStr), context(context) {
// Translate the `dataLayoutStr`. First, append the default data layout
// string specified in the language reference
// (https://llvm.org/docs/LangRef.html#data-layout) to the supplied string.
// The translation then parses the string and ignores the default value if a
// specific kind occurs in both strings. Additionally, the following default
// values exist:
// - non-default address space pointer specifications default to the default
// address space pointer specification
// - the alloca address space defaults to the default address space.
dataLayoutSpec = dataLayoutSpecFromDataLayoutStr();
}

/// Returns the MLIR data layout specification translated from the LLVM
/// data layout.
DataLayoutSpecInterface getDataLayout() const { return dataLayout; }
DataLayoutSpecInterface getDataLayoutSpec() const { return dataLayoutSpec; }

/// Returns the last data layout token that has been processed before
/// the data layout translation failed.
Expand All @@ -65,8 +73,9 @@ class DataLayoutImporter {
ArrayRef<StringRef> getUnhandledTokens() const { return unhandledTokens; }

private:
/// Translates the LLVM `dataLayout` to an MLIR data layout specification.
void translateDataLayout(const llvm::DataLayout &llvmDataLayout);
/// Translate the LLVM data layout string to an MLIR data layout
/// specification.
DataLayoutSpecInterface dataLayoutSpecFromDataLayoutStr();

/// Tries to parse the letter only prefix that identifies the specification
/// and removes the consumed characters from the beginning of the string.
Expand Down Expand Up @@ -116,17 +125,18 @@ class DataLayoutImporter {
/// Adds legal int widths entry if there is none yet.
LogicalResult tryToEmplaceLegalIntWidthsEntry(StringRef token);

std::string layoutStr = {};
std::string dataLayoutStr = {};
DataLayoutSpecInterface dataLayoutSpec;

StringRef lastToken = {};
SmallVector<StringRef> unhandledTokens;
llvm::MapVector<StringAttr, DataLayoutEntryInterface> keyEntries;
llvm::MapVector<TypeAttr, DataLayoutEntryInterface> typeEntries;
MLIRContext *context;
DataLayoutSpecInterface dataLayout;
};

} // namespace detail
} // namespace LLVM
} // namespace mlir

#endif // MLIR_LIB_TARGET_LLVMIR_DATALAYOUTIMPORTER_H_
#endif // MLIR_TARGET_LLVMIR_DATALAYOUTIMPORTER_H
5 changes: 5 additions & 0 deletions mlir/include/mlir/Target/LLVMIR/Transforms/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
set(LLVM_TARGET_DEFINITIONS Passes.td)
mlir_tablegen(Passes.h.inc -gen-pass-decls -name TargetLLVMIRTransforms)
add_public_tablegen_target(MLIRTargetLLVMIRTransformsIncGen)

add_mlir_doc(Passes TargetLLVMIRTransforms ./ -gen-pass-doc)
27 changes: 27 additions & 0 deletions mlir/include/mlir/Target/LLVMIR/Transforms/Passes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===- Passes.h - LLVM Target Pass Construction and Registration ----------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#ifndef MLIR_TARGET_LLVMIR_TRANSFORMS_PASSES_H
#define MLIR_TARGET_LLVMIR_TRANSFORMS_PASSES_H

#include "mlir/Pass/Pass.h"

namespace mlir {

namespace LLVM {

#define GEN_PASS_DECL
#define GEN_PASS_REGISTRATION
#include "mlir/Target/LLVMIR/Transforms/Passes.h.inc"

void registerTargetLLVMPasses();

} // namespace LLVM
} // namespace mlir

#endif // MLIR_TARGET_LLVMIR_TRANSFORMS_PASSES_H
30 changes: 30 additions & 0 deletions mlir/include/mlir/Target/LLVMIR/Transforms/Passes.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//===-- Passes.td - LLVM Target pass definition file -------*- tablegen -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//

#ifndef MLIR_TARGET_LLVMIR_TRANSFORMS_PASSES
#define MLIR_TARGET_LLVMIR_TRANSFORMS_PASSES

include "mlir/Pass/PassBase.td"

def LLVMTargetToDataLayout : Pass<"llvm-target-to-data-layout"> {
let summary = "Derive data layout attributes from LLVM target attributes";
let dependentDialects = ["mlir::DLTIDialect"];
let description = [{
Derive a `DataLayoutSpecInterface`-implementing data layout attribute from
the LLVM-backend target specified by the `TargetAttrInterface`-implementing
attribute attached to the target op at the name `llvm.target`.
}];
let options = [
Option<"initializeLLVMTargets", "initialize-llvm-targets", "bool",
/*default=*/"true",
"Whether to pre-load all available target machines, that LLVM is "
"configured to support, into the TargetRegistry.">
];
}

#endif // MLIR_TARGET_LLVMIR_TRANSFORMS_PASSES
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/DLTI/Traits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ LogicalResult mlir::impl::verifyHasDefaultDLTIDataLayoutTrait(Operation *op) {
}

DataLayoutSpecInterface mlir::impl::getDataLayoutSpec(Operation *op) {
return op->getAttrOfType<DataLayoutSpecAttr>(
return op->getAttrOfType<DataLayoutSpecInterface>(
DLTIDialect::kDataLayoutAttrName);
}

Expand Down
17 changes: 17 additions & 0 deletions mlir/lib/Dialect/LLVMIR/IR/LLVMAttrs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,3 +402,20 @@ ModuleFlagAttr::verify(function_ref<InFlightDiagnostic()> emitError,
"supported for unknown key '"
<< key << "'";
}

//===----------------------------------------------------------------------===//
// LLVM_TargetAttr
//===----------------------------------------------------------------------===//

FailureOr<::mlir::Attribute> TargetAttr::query(DataLayoutEntryKey key) {
if (auto stringAttrKey = dyn_cast<StringAttr>(key)) {
if (stringAttrKey.getValue() == "triple")
return getTriple();
if (stringAttrKey.getValue() == "chip")
return getChip();
if (stringAttrKey.getValue() == "features" && getFeatures())
return getFeatures();
}

return failure();
}
2 changes: 2 additions & 0 deletions mlir/lib/RegisterAllPasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "mlir/Dialect/Transform/Transforms/Passes.h"
#include "mlir/Dialect/Vector/Transforms/Passes.h"
#include "mlir/Dialect/XeGPU/Transforms/Passes.h"
#include "mlir/Target/LLVMIR/Transforms/Passes.h"
#include "mlir/Transforms/Passes.h"

// This function may be called to register the MLIR passes with the
Expand Down Expand Up @@ -74,6 +75,7 @@ void mlir::registerAllPasses() {
registerNVGPUPasses();
registerSparseTensorPasses();
LLVM::registerLLVMPasses();
LLVM::registerTargetLLVMIRTransformsPasses();
math::registerMathPasses();
memref::registerMemRefPasses();
shard::registerShardPasses();
Expand Down
1 change: 1 addition & 0 deletions mlir/lib/Target/LLVMIR/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
add_subdirectory(Dialect)
add_subdirectory(Transforms)

set(LLVM_OPTIONAL_SOURCES
ConvertFromLLVMIR.cpp
Expand Down
Loading