-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[MLIR][LLVMIR][DLTI] Pass to update #llvm.target's features per relevant backend #154938
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
Merged
Merged
Changes from 14 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d95c6ed
[MLIR][LLVMIR][DLTI] Pass for updating target features per TargetMachine
rolfmorel fbcd09d
Sharing is caring
rolfmorel ec15717
Get feature flags from proper source
rolfmorel 32c9cea
Add test cases
rolfmorel facc907
Add comment explaining file structuring
rolfmorel 36c3ba4
Add tests exercising DLTI
rolfmorel b3e91bf
Update DLTI tests
rolfmorel 27a9749
Address Mehdi's comments
rolfmorel 1f29675
Add missing file
rolfmorel a6cb7fb
Fix cyclic dependency issue through a coarse type
rolfmorel 3bc26b1
Better init
rolfmorel 79e99b9
Remove extraneaous dialect dependency
rolfmorel b7e5765
Update cast
rolfmorel d49b0db
Obsolete
rolfmorel 6149b17
Address Medhi's and Tobias' comments
rolfmorel 2a2ce61
Merge remote-tracking branch 'origin/main' into dlti-target-features
rolfmorel 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,6 @@ | |
| #include "mlir/Pass/Pass.h" | ||
|
|
||
| namespace mlir { | ||
|
|
||
| namespace LLVM { | ||
|
|
||
| #define GEN_PASS_DECL | ||
|
|
||
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,35 @@ | ||
| //===- TargetUtils.h - Utils to obtain LLVM's TargetMachine and DataLayout ===// | ||
| // | ||
| // 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_TARGETUTILS_H | ||
| #define MLIR_TARGET_LLVMIR_TRANSFORMS_TARGETUTILS_H | ||
|
|
||
| #include "mlir/Dialect/LLVMIR/LLVMInterfaces.h" | ||
| #include "llvm/Support/Threading.h" | ||
| #include "llvm/Target/TargetMachine.h" | ||
|
|
||
| namespace mlir { | ||
| namespace LLVM { | ||
| namespace detail { | ||
| /// Idempotent helper to register/initialize all backends that LLVM has been | ||
| /// configured to support. Only runs the first time it is called. | ||
| void initializeBackendsOnce(); | ||
|
|
||
| /// Helper to obtain the TargetMachine specified by the properties of the | ||
| /// TargetAttrInterface-implementing attribute. | ||
| FailureOr<std::unique_ptr<llvm::TargetMachine>> | ||
| getTargetMachine(mlir::LLVM::TargetAttrInterface attr); | ||
|
|
||
| /// Helper to obtain the DataLayout of the target specified by the properties of | ||
| /// the TargetAttrInterface-implementing attribute. | ||
| FailureOr<llvm::DataLayout> getDataLayout(mlir::LLVM::TargetAttrInterface attr); | ||
| } // namespace detail | ||
| } // namespace LLVM | ||
| } // namespace mlir | ||
|
|
||
| #endif // MLIR_TARGET_LLVMIR_TRANSFORMS_TARGETUTILS_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
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
78 changes: 78 additions & 0 deletions
78
mlir/lib/Target/LLVMIR/Transforms/TargetToTargetFeatures.cpp
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,78 @@ | ||
| //===- TargetToTargetFeatures.cpp - extract features from TargetMachine ---===// | ||
| // | ||
| // 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/Target/LLVMIR/Transforms/Passes.h" | ||
| #include "mlir/Target/LLVMIR/Transforms/TargetUtils.h" | ||
|
|
||
| #include "mlir/Dialect/DLTI/DLTI.h" | ||
| #include "mlir/Dialect/LLVMIR/LLVMDialect.h" | ||
| #include "mlir/Target/LLVMIR/Import.h" | ||
|
|
||
| #include "llvm/MC/MCSubtargetInfo.h" | ||
|
|
||
| namespace mlir { | ||
| namespace LLVM { | ||
| #define GEN_PASS_DEF_LLVMTARGETTOTARGETFEATURES | ||
| #include "mlir/Target/LLVMIR/Transforms/Passes.h.inc" | ||
| } // namespace LLVM | ||
| } // namespace mlir | ||
|
|
||
| using namespace mlir; | ||
|
|
||
| struct TargetToTargetFeaturesPass | ||
| : public LLVM::impl::LLVMTargetToTargetFeaturesBase< | ||
| TargetToTargetFeaturesPass> { | ||
| using LLVM::impl::LLVMTargetToTargetFeaturesBase< | ||
| TargetToTargetFeaturesPass>::LLVMTargetToTargetFeaturesBase; | ||
|
|
||
| void runOnOperation() override { | ||
| Operation *op = getOperation(); | ||
|
|
||
| if (initializeLLVMTargets) | ||
| LLVM::detail::initializeBackendsOnce(); | ||
|
|
||
| auto targetAttr = op->getAttrOfType<LLVM::TargetAttr>( | ||
| LLVM::LLVMDialect::getTargetAttrName()); | ||
| if (!targetAttr) { | ||
| op->emitError() << "no LLVM::TargetAttr attribute at key \"" | ||
| << LLVM::LLVMDialect::getTargetAttrName() << "\""; | ||
| return signalPassFailure(); | ||
| } | ||
|
|
||
| FailureOr<std::unique_ptr<llvm::TargetMachine>> targetMachine = | ||
| LLVM::detail::getTargetMachine(targetAttr); | ||
| if (failed(targetMachine)) { | ||
| op->emitError() << "failed to obtain llvm::TargetMachine for " | ||
| << targetAttr; | ||
| return signalPassFailure(); | ||
| } | ||
|
|
||
| llvm::MCSubtargetInfo const *subTargetInfo = | ||
| (*targetMachine)->getMCSubtargetInfo(); | ||
|
|
||
| const std::vector<llvm::SubtargetFeatureKV> enabledFeatures = | ||
| subTargetInfo->getEnabledProcessorFeatures(); | ||
|
|
||
| auto plussedFeatures = llvm::to_vector( | ||
| llvm::map_range(enabledFeatures, [](llvm::SubtargetFeatureKV feature) { | ||
| return std::string("+") + feature.Key; | ||
| })); | ||
|
|
||
| auto plussedFeaturesRefs = llvm::to_vector(llvm::map_range( | ||
| plussedFeatures, [](auto &it) { return StringRef(it.c_str()); })); | ||
|
|
||
| auto fullTargetFeaturesAttr = | ||
| LLVM::TargetFeaturesAttr::get(&getContext(), plussedFeaturesRefs); | ||
|
|
||
| auto updatedTargetAttr = | ||
| LLVM::TargetAttr::get(&getContext(), targetAttr.getTriple(), | ||
| targetAttr.getChip(), fullTargetFeaturesAttr); | ||
|
|
||
| op->setAttr(LLVM::LLVMDialect::getTargetAttrName(), updatedTargetAttr); | ||
| } | ||
| }; |
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.
Uh oh!
There was an error while loading. Please reload this page.