-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[mlir][interfaces] Add the TargetInfo
attribute interface
#78073
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
fabianmcg
wants to merge
2
commits into
llvm:main
Choose a base branch
from
fabianmcg:target-info
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
set(LLVM_TARGET_DEFINITIONS Passes.td) | ||
mlir_tablegen(Passes.h.inc -gen-pass-decls -name DLTI) | ||
mlir_tablegen(Passes.capi.h.inc -gen-pass-capi-header --prefix DLTI) | ||
mlir_tablegen(Passes.capi.cpp.inc -gen-pass-capi-impl --prefix DLTI) | ||
add_public_tablegen_target(MLIRDLTIPassIncGen) | ||
|
||
add_mlir_doc(Passes DLTIPasses ./ -gen-pass-doc) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//===- Passes.h - Pass Entrypoints ------------------------------*- C++ -*-===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This header file defines prototypes that expose pass constructors. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef MLIR_DIALECT_DLTI_TRANSFORMS_PASSES_H | ||
#define MLIR_DIALECT_DLTI_TRANSFORMS_PASSES_H | ||
|
||
#include "mlir/Pass/Pass.h" | ||
|
||
namespace mlir { | ||
#define GEN_PASS_DECL | ||
#include "mlir/Dialect/DLTI/Transforms/Passes.h.inc" | ||
|
||
/// Generate the code for registering passes. | ||
#define GEN_PASS_REGISTRATION | ||
#include "mlir/Dialect/DLTI/Transforms/Passes.h.inc" | ||
|
||
/// Sets the target specs using the target attached to the module. | ||
LogicalResult setTargetSpecsFromTarget(Operation *op); | ||
} // namespace mlir | ||
|
||
#endif // MLIR_DIALECT_DLTI_TRANSFORMS_PASSES_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//===-- Passes.td - DLTI 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_DIALECT_DLTI_PASSES | ||
#define MLIR_DIALECT_DLTI_PASSES | ||
|
||
include "mlir/Pass/PassBase.td" | ||
|
||
def DltiSetTargetSpecsFromTarget: Pass<"dlti-set-target-specs", ""> { | ||
let summary = "Sets DLTI target specs using a target."; | ||
let description = [{ | ||
This pass potentially sets the following DLTI target specs in the current | ||
operation: | ||
- The data layout. | ||
- The target system spec. | ||
|
||
Example: | ||
|
||
```mlir | ||
// Given the following input: | ||
builtin.module @module_1 attributes {dlti.target = #my.target} {...} | ||
// After applying the pass: | ||
builtin.module @module_1 attributes { | ||
dlti.target = #my.target, | ||
dlti.target_system_spec = #my.system_spec, | ||
dlti.dl_spec = #my.dl_spec | ||
} {...} | ||
``` | ||
}]; | ||
let dependentDialects = [ | ||
"::mlir::DLTIDialect" | ||
]; | ||
} | ||
|
||
#endif // MLIR_DIALECT_DLTI_PASSES |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -354,6 +354,46 @@ def TargetSystemSpecInterface : AttrInterface<"TargetSystemSpecInterface", [DLTI | |||||||||||||
}]; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
def TargetAttrInterface : AttrInterface<"TargetAttrInterface"> { | ||||||||||||||
let cppNamespace = "::mlir"; | ||||||||||||||
|
||||||||||||||
let description = [{ | ||||||||||||||
Attribute interface describing target information. | ||||||||||||||
|
||||||||||||||
Target information attributes provide essential information on a | ||||||||||||||
compilation target. This information includes the target triple identifier, | ||||||||||||||
the target chip identifier, a string representation of the target features, | ||||||||||||||
and the target spec data layout. | ||||||||||||||
}]; | ||||||||||||||
|
||||||||||||||
let methods = [ | ||||||||||||||
InterfaceMethod< | ||||||||||||||
/*description=*/"Returns the target triple identifier.", | ||||||||||||||
/*retTy=*/"::llvm::StringRef", | ||||||||||||||
/*methodName=*/"getTargetTriple", | ||||||||||||||
/*args=*/(ins) | ||||||||||||||
>, | ||||||||||||||
InterfaceMethod< | ||||||||||||||
/*description=*/"Returns the target chip identifier.", | ||||||||||||||
/*retTy=*/"::llvm::StringRef", | ||||||||||||||
/*methodName=*/"getTargetChip", | ||||||||||||||
/*args=*/(ins) | ||||||||||||||
>, | ||||||||||||||
InterfaceMethod< | ||||||||||||||
/*description=*/"Returns the target features as a string.", | ||||||||||||||
/*retTy=*/"std::string", | ||||||||||||||
/*methodName=*/"getTargetFeatures", | ||||||||||||||
/*args=*/(ins) | ||||||||||||||
>, | ||||||||||||||
Comment on lines
+370
to
+387
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this entirely "LLVM centric"? |
||||||||||||||
InterfaceMethod< | ||||||||||||||
/*description=*/"Sets the target spec. Returns failure if there was a problem.", | ||||||||||||||
/*retTy=*/"::llvm::LogicalResult", | ||||||||||||||
/*methodName=*/"setTargetSpec", | ||||||||||||||
/*args=*/(ins "::mlir::TargetSpec&":$spec) | ||||||||||||||
Comment on lines
+390
to
+392
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
> | ||||||||||||||
]; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
//===----------------------------------------------------------------------===// | ||||||||||||||
// Operation interface | ||||||||||||||
//===----------------------------------------------------------------------===// | ||||||||||||||
|
@@ -387,13 +427,39 @@ def DataLayoutOpInterface : OpInterface<"DataLayoutOpInterface"> { | |||||||||||||
/*methodName=*/"getDataLayoutSpec", | ||||||||||||||
/*args=*/(ins) | ||||||||||||||
>, | ||||||||||||||
InterfaceMethod< | ||||||||||||||
/*description=*/"Sets the data layout specification for this op.", | ||||||||||||||
/*retTy=*/"void", | ||||||||||||||
/*methodName=*/"setDataLayoutSpec", | ||||||||||||||
/*args=*/(ins "::mlir::DataLayoutSpecInterface":$spec) | ||||||||||||||
>, | ||||||||||||||
InterfaceMethod< | ||||||||||||||
/*description=*/"Returns the target system desc specification for this " | ||||||||||||||
"op, or null if it does not exist.", | ||||||||||||||
/*retTy=*/"::mlir::TargetSystemSpecInterface", | ||||||||||||||
/*methodName=*/"getTargetSystemSpec", | ||||||||||||||
/*args=*/(ins) | ||||||||||||||
>, | ||||||||||||||
InterfaceMethod< | ||||||||||||||
/*description=*/"Sets the target system desc specification for this " | ||||||||||||||
"op.", | ||||||||||||||
/*retTy=*/"void", | ||||||||||||||
/*methodName=*/"setTargetSystemSpec", | ||||||||||||||
/*args=*/(ins "::mlir::TargetSystemSpecInterface":$spec) | ||||||||||||||
>, | ||||||||||||||
InterfaceMethod< | ||||||||||||||
/*description=*/"Returns the target attr for this op, or null if it " | ||||||||||||||
"does not exist.", | ||||||||||||||
/*retTy=*/"::mlir::TargetAttrInterface", | ||||||||||||||
/*methodName=*/"getTargetAttr", | ||||||||||||||
/*args=*/(ins) | ||||||||||||||
>, | ||||||||||||||
InterfaceMethod< | ||||||||||||||
/*description=*/"Sets the target attr for this", | ||||||||||||||
/*retTy=*/"void", | ||||||||||||||
/*methodName=*/"setTargetAttr", | ||||||||||||||
/*args=*/(ins "::mlir::TargetAttrInterface":$target) | ||||||||||||||
>, | ||||||||||||||
StaticInterfaceMethod< | ||||||||||||||
/*description=*/"Returns the size of the given type computed using the " | ||||||||||||||
"relevant entries. The data layout object can be used " | ||||||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
//===- Target.h - Target information ----------------------------*- C++ -*-===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file declare utilities to interact with LLVM targets by querying an MLIR | ||
// target. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef MLIR_TARGET_LLVM_TARGET_H | ||
#define MLIR_TARGET_LLVM_TARGET_H | ||
|
||
#include "mlir/Interfaces/DataLayoutInterfaces.h" | ||
#include "llvm/IR/DataLayout.h" | ||
#include "llvm/TargetParser/Triple.h" | ||
|
||
namespace llvm { | ||
class Triple; | ||
class Target; | ||
class TargetMachine; | ||
} // namespace llvm | ||
|
||
namespace mlir { | ||
/// Given a target triple. chip and features returns the LLVM data layout. | ||
FailureOr<const llvm::DataLayout> | ||
getLLVMDataLayout(StringRef triple, StringRef chip, StringRef features); | ||
|
||
/// Returns the LLVM target triple held by `target`. | ||
llvm::Triple getTargetTriple(TargetAttrInterface target); | ||
|
||
/// Returns the LLVM target held by `target`. | ||
FailureOr<const llvm::Target *> getLLVMTarget(TargetAttrInterface target); | ||
|
||
/// Helper class for holding LLVM target information. Note: This class requires | ||
/// that the corresponding LLVM target has ben initialized. | ||
class TargetInfo { | ||
public: | ||
TargetInfo(TargetInfo &&) = default; | ||
TargetInfo(const TargetInfo &) = delete; | ||
~TargetInfo(); | ||
TargetInfo &operator=(TargetInfo &&) = default; | ||
TargetInfo &operator=(const TargetInfo &) = delete; | ||
/// Constructs the target info from `target`. | ||
static FailureOr<TargetInfo> getTargetInfo(StringRef triple, StringRef chip, | ||
StringRef features); | ||
|
||
/// Constructs the target info from `target`. | ||
static FailureOr<TargetInfo> getTargetInfo(TargetAttrInterface target) { | ||
return getTargetInfo(target.getTargetTriple(), target.getTargetChip(), | ||
target.getTargetFeatures()); | ||
} | ||
|
||
/// Returns the target chip. | ||
StringRef getTargetChip() const; | ||
|
||
/// Returns the target features. | ||
StringRef getTargetFeatures() const; | ||
|
||
/// Returns the target triple. | ||
const llvm::Triple &getTriple() const; | ||
|
||
/// Returns the target. | ||
const llvm::Target &getTarget() const; | ||
|
||
/// Returns the target machine. | ||
const llvm::TargetMachine *getTargetMachine() const { | ||
return targetMachine.get(); | ||
} | ||
|
||
/// Returns the LLVM data layout for the corresponding target. | ||
const llvm::DataLayout getDataLayout() const; | ||
|
||
private: | ||
TargetInfo(llvm::TargetMachine *targetMachine); | ||
/// The LLVM target machine. | ||
mutable std::unique_ptr<llvm::TargetMachine> targetMachine; | ||
}; | ||
} // namespace mlir | ||
|
||
#endif // MLIR_TARGET_LLVM_TARGET_H |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.