-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[SandboxVec][BottomUpVec] Add cost estimation and tr-accept-or-revert pass #126325
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 all commits
Commits
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
30 changes: 30 additions & 0 deletions
30
llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Passes/TransactionAcceptOrRevert.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,30 @@ | ||
| //===- TransactionAcceptOrRevert.h ------------------------------*- 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 is a region pass that checks the region cost before/after vectorization | ||
| // and accepts the state of Sandbox IR if the cost is better, or otherwise | ||
| // reverts it. | ||
| // | ||
|
|
||
| #ifndef LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_TRANSACTIONACCEPTORREVERT_H | ||
| #define LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_TRANSACTIONACCEPTORREVERT_H | ||
|
|
||
| #include "llvm/SandboxIR/Pass.h" | ||
| #include "llvm/SandboxIR/Region.h" | ||
|
|
||
| namespace llvm::sandboxir { | ||
|
|
||
| class TransactionAcceptOrRevert : public RegionPass { | ||
| public: | ||
| TransactionAcceptOrRevert() : RegionPass("tr-accept-or-revert") {} | ||
| bool runOnRegion(Region &Rgn, const Analyses &A) final; | ||
| }; | ||
|
|
||
| } // namespace llvm::sandboxir | ||
|
|
||
| #endif // LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_TRANSACTIONACCEPTORREVERT_H |
34 changes: 34 additions & 0 deletions
34
llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Passes/TransactionAlwaysAccept.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,34 @@ | ||
| //===- TransactionAlwaysAccept.h --------------------------------*- 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 is a region pass that always accepts the transaction without checking | ||
| // its cost. This is mainly used as a final pass in lit tests. | ||
| // | ||
|
|
||
| #ifndef LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_TRANSACTIONALWAYSACCEPT_H | ||
| #define LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_TRANSACTIONALWAYSACCEPT_H | ||
|
|
||
| #include "llvm/SandboxIR/Pass.h" | ||
| #include "llvm/SandboxIR/Region.h" | ||
|
|
||
| namespace llvm::sandboxir { | ||
|
|
||
| class TransactionAlwaysAccept : public RegionPass { | ||
| public: | ||
| TransactionAlwaysAccept() : RegionPass("tr-accept") {} | ||
| bool runOnRegion(Region &Rgn, const Analyses &A) final { | ||
| auto &Tracker = Rgn.getContext().getTracker(); | ||
| bool HasChanges = !Tracker.empty(); | ||
| Tracker.accept(); | ||
| return HasChanges; | ||
| } | ||
| }; | ||
|
|
||
| } // namespace llvm::sandboxir | ||
|
|
||
| #endif // LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_PASSES_TRANSACTIONALWAYSACCEPT_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
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
37 changes: 37 additions & 0 deletions
37
llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/TransactionAcceptOrRevert.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,37 @@ | ||
| //===- TransactionAcceptOrRevert.cpp - Check cost and accept/revert region ===// | ||
| // | ||
| // 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 "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/TransactionAcceptOrRevert.h" | ||
| #include "llvm/Support/CommandLine.h" | ||
| #include "llvm/Support/InstructionCost.h" | ||
|
|
||
| namespace llvm { | ||
|
|
||
| static cl::opt<int> CostThreshold("sbvec-cost-threshold", cl::init(0), | ||
| cl::Hidden, | ||
| cl::desc("Vectorization cost threshold.")); | ||
|
|
||
| namespace sandboxir { | ||
|
|
||
| bool TransactionAcceptOrRevert::runOnRegion(Region &Rgn, const Analyses &A) { | ||
| const auto &SB = Rgn.getScoreboard(); | ||
| InstructionCost CostAfterMinusBefore = SB.getAfterCost() - SB.getBeforeCost(); | ||
| // TODO: Print costs / write to remarks. | ||
| auto &Tracker = Rgn.getContext().getTracker(); | ||
| if (CostAfterMinusBefore < -CostThreshold) { | ||
| bool HasChanges = !Tracker.empty(); | ||
| Tracker.accept(); | ||
| return HasChanges; | ||
| } | ||
| // Revert the IR. | ||
| Rgn.getContext().getTracker().revert(); | ||
| return false; | ||
| } | ||
|
|
||
| } // namespace sandboxir | ||
| } // namespace llvm |
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
91 changes: 91 additions & 0 deletions
91
llvm/test/Transforms/SandboxVectorizer/X86/simple_cost_test.ll
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,91 @@ | ||
| ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 | ||
|
Collaborator
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. Do we have a test case where vectorization is rejected due to the cost threshold?
Contributor
Author
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. Good point, let me add one. |
||
| ; RUN: opt -passes=sandbox-vectorizer -mtriple=x86_64-- -mattr=+sse4.1 %s -S -sbvec-cost-threshold=0 | FileCheck %s --check-prefix=THRESHOLD_0 | ||
| ; RUN: opt -passes=sandbox-vectorizer -mtriple=x86_64-- -mattr=+sse4.1 %s -S -sbvec-cost-threshold=99 | FileCheck %s --check-prefix=THRESHOLD_99 | ||
|
|
||
| define void @simple_cost_test(ptr %ptr) { | ||
| ; THRESHOLD_0-LABEL: define void @simple_cost_test( | ||
| ; THRESHOLD_0-SAME: ptr [[PTR:%.*]]) #[[ATTR0:[0-9]+]] { | ||
| ; THRESHOLD_0-NEXT: [[PTR0:%.*]] = getelementptr double, ptr [[PTR]], i32 0 | ||
| ; THRESHOLD_0-NEXT: [[VECL:%.*]] = load <2 x double>, ptr [[PTR0]], align 8, !sandboxvec [[META0:![0-9]+]] | ||
| ; THRESHOLD_0-NEXT: store <2 x double> [[VECL]], ptr [[PTR0]], align 8, !sandboxvec [[META0]] | ||
| ; THRESHOLD_0-NEXT: ret void | ||
| ; | ||
| ; THRESHOLD_99-LABEL: define void @simple_cost_test( | ||
| ; THRESHOLD_99-SAME: ptr [[PTR:%.*]]) #[[ATTR0:[0-9]+]] { | ||
| ; THRESHOLD_99-NEXT: [[PTR0:%.*]] = getelementptr double, ptr [[PTR]], i32 0 | ||
| ; THRESHOLD_99-NEXT: [[PTR1:%.*]] = getelementptr double, ptr [[PTR]], i32 1, !sandboxvec [[META0:![0-9]+]] | ||
| ; THRESHOLD_99-NEXT: [[LD0:%.*]] = load double, ptr [[PTR0]], align 8, !sandboxvec [[META0]] | ||
| ; THRESHOLD_99-NEXT: [[LD1:%.*]] = load double, ptr [[PTR1]], align 8, !sandboxvec [[META0]] | ||
| ; THRESHOLD_99-NEXT: store double [[LD0]], ptr [[PTR0]], align 8, !sandboxvec [[META0]] | ||
| ; THRESHOLD_99-NEXT: store double [[LD1]], ptr [[PTR1]], align 8, !sandboxvec [[META0]] | ||
| ; THRESHOLD_99-NEXT: ret void | ||
| ; | ||
| %ptr0 = getelementptr double, ptr %ptr, i32 0 | ||
| %ptr1 = getelementptr double, ptr %ptr, i32 1 | ||
| %ld0 = load double, ptr %ptr0 | ||
| %ld1 = load double, ptr %ptr1 | ||
| store double %ld0, ptr %ptr0 | ||
| store double %ld1, ptr %ptr1 | ||
| ret void | ||
| } | ||
|
|
||
| define void @pack_cost_test_(ptr %ptr) { | ||
| ; THRESHOLD_0-LABEL: define void @pack_cost_test_( | ||
| ; THRESHOLD_0-SAME: ptr [[PTR:%.*]]) #[[ATTR0]] { | ||
| ; THRESHOLD_0-NEXT: [[PTR0:%.*]] = getelementptr float, ptr [[PTR]], i32 0 | ||
| ; THRESHOLD_0-NEXT: [[PTR1:%.*]] = getelementptr float, ptr [[PTR]], i32 1 | ||
| ; THRESHOLD_0-NEXT: [[LD0:%.*]] = load float, ptr [[PTR0]], align 4 | ||
| ; THRESHOLD_0-NEXT: [[LD1:%.*]] = load float, ptr [[PTR1]], align 4 | ||
| ; THRESHOLD_0-NEXT: [[PACK4:%.*]] = insertelement <4 x float> poison, float [[LD0]], i32 0, !sandboxvec [[META1:![0-9]+]] | ||
| ; THRESHOLD_0-NEXT: [[PACK5:%.*]] = insertelement <4 x float> [[PACK4]], float [[LD1]], i32 1, !sandboxvec [[META1]] | ||
| ; THRESHOLD_0-NEXT: [[PACK6:%.*]] = insertelement <4 x float> [[PACK5]], float [[LD0]], i32 2, !sandboxvec [[META1]] | ||
| ; THRESHOLD_0-NEXT: [[PACK7:%.*]] = insertelement <4 x float> [[PACK6]], float [[LD1]], i32 3, !sandboxvec [[META1]] | ||
| ; THRESHOLD_0-NEXT: [[PACK:%.*]] = insertelement <4 x float> poison, float [[LD0]], i32 0, !sandboxvec [[META1]] | ||
| ; THRESHOLD_0-NEXT: [[PACK1:%.*]] = insertelement <4 x float> [[PACK]], float [[LD1]], i32 1, !sandboxvec [[META1]] | ||
| ; THRESHOLD_0-NEXT: [[PACK2:%.*]] = insertelement <4 x float> [[PACK1]], float [[LD0]], i32 2, !sandboxvec [[META1]] | ||
| ; THRESHOLD_0-NEXT: [[PACK3:%.*]] = insertelement <4 x float> [[PACK2]], float [[LD1]], i32 3, !sandboxvec [[META1]] | ||
| ; THRESHOLD_0-NEXT: [[VEC:%.*]] = fmul <4 x float> [[PACK3]], [[PACK7]], !sandboxvec [[META1]] | ||
| ; THRESHOLD_0-NEXT: store <4 x float> [[VEC]], ptr [[PTR0]], align 4, !sandboxvec [[META1]] | ||
| ; THRESHOLD_0-NEXT: ret void | ||
| ; | ||
| ; THRESHOLD_99-LABEL: define void @pack_cost_test_( | ||
| ; THRESHOLD_99-SAME: ptr [[PTR:%.*]]) #[[ATTR0]] { | ||
| ; THRESHOLD_99-NEXT: [[PTR0:%.*]] = getelementptr float, ptr [[PTR]], i32 0 | ||
| ; THRESHOLD_99-NEXT: [[PTR1:%.*]] = getelementptr float, ptr [[PTR]], i32 1 | ||
| ; THRESHOLD_99-NEXT: [[PTR2:%.*]] = getelementptr float, ptr [[PTR]], i32 2, !sandboxvec [[META1:![0-9]+]] | ||
| ; THRESHOLD_99-NEXT: [[PTR3:%.*]] = getelementptr float, ptr [[PTR]], i32 3, !sandboxvec [[META1]] | ||
| ; THRESHOLD_99-NEXT: [[LD0:%.*]] = load float, ptr [[PTR0]], align 4 | ||
| ; THRESHOLD_99-NEXT: [[LD1:%.*]] = load float, ptr [[PTR1]], align 4 | ||
| ; THRESHOLD_99-NEXT: [[MUL0:%.*]] = fmul float [[LD0]], [[LD0]], !sandboxvec [[META1]] | ||
| ; THRESHOLD_99-NEXT: [[MUL1:%.*]] = fmul float [[LD1]], [[LD1]], !sandboxvec [[META1]] | ||
| ; THRESHOLD_99-NEXT: [[MUL2:%.*]] = fmul float [[LD0]], [[LD0]], !sandboxvec [[META1]] | ||
| ; THRESHOLD_99-NEXT: [[MUL3:%.*]] = fmul float [[LD1]], [[LD1]], !sandboxvec [[META1]] | ||
| ; THRESHOLD_99-NEXT: store float [[MUL0]], ptr [[PTR0]], align 4, !sandboxvec [[META1]] | ||
| ; THRESHOLD_99-NEXT: store float [[MUL1]], ptr [[PTR1]], align 4, !sandboxvec [[META1]] | ||
| ; THRESHOLD_99-NEXT: store float [[MUL2]], ptr [[PTR2]], align 4, !sandboxvec [[META1]] | ||
| ; THRESHOLD_99-NEXT: store float [[MUL3]], ptr [[PTR3]], align 4, !sandboxvec [[META1]] | ||
| ; THRESHOLD_99-NEXT: ret void | ||
| ; | ||
| %ptr0 = getelementptr float, ptr %ptr, i32 0 | ||
| %ptr1 = getelementptr float, ptr %ptr, i32 1 | ||
| %ptr2 = getelementptr float, ptr %ptr, i32 2 | ||
| %ptr3 = getelementptr float, ptr %ptr, i32 3 | ||
| %ld0 = load float, ptr %ptr0 | ||
| %ld1 = load float, ptr %ptr1 | ||
| %mul0 = fmul float %ld0, %ld0 | ||
| %mul1 = fmul float %ld1, %ld1 | ||
| %mul2 = fmul float %ld0, %ld0 | ||
| %mul3 = fmul float %ld1, %ld1 | ||
| store float %mul0, ptr %ptr0 | ||
| store float %mul1, ptr %ptr1 | ||
| store float %mul2, ptr %ptr2 | ||
| store float %mul3, ptr %ptr3 | ||
| ret void | ||
| } | ||
| ;. | ||
| ; THRESHOLD_0: [[META0]] = distinct !{!"sandboxregion"} | ||
| ; THRESHOLD_0: [[META1]] = distinct !{!"sandboxregion"} | ||
| ;. | ||
| ; THRESHOLD_99: [[META0]] = distinct !{!"sandboxregion"} | ||
| ; THRESHOLD_99: [[META1]] = distinct !{!"sandboxregion"} | ||
| ;. | ||
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
4 changes: 2 additions & 2 deletions
4
llvm/test/Transforms/SandboxVectorizer/bottomup_seed_slice_pow2.ll
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
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.
Note that this is replaced by a
tr-savepass further in the patch chain. I would usually complain about the asymmetry of having a hardcodedsavebut havingtr-acceptin a pass, but I think it's okay as an intermediate step.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.
Yes, this asymmetry will go away in a follow-up patch.