-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[AMDGPU][NFC] Refactor D16 folding for image samples with multiple ExtractElement+FPTrunc chains #145312
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
Open
harrisonGPU
wants to merge
1
commit into
llvm:main
Choose a base branch
from
harrisonGPU:amdgpu/refine-image-extract
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.
+69
−59
Conversation
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
…tractElement+FPTrunc chains
Because I want to support D16 folding for image instructions involving multiple extractelement and pkrtz operations, I need to refactor this function. After the QA team finishes testing, I will push a PR to enable D16 folding support for such cases. |
@llvm/pr-subscribers-backend-amdgpu Author: Harrison Hao (harrisonGPU) ChangesFull diff: https://github.com/llvm/llvm-project/pull/145312.diff 1 Files Affected:
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp b/llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp
index 5477c5eae9392..171d44b5ec329 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp
@@ -34,6 +34,12 @@ struct AMDGPUImageDMaskIntrinsic {
unsigned Intr;
};
+struct D16Candidate {
+ SmallVector<Instruction *, 4> InstsToErase;
+ Instruction *Replacee = nullptr;
+ Value *Index = nullptr;
+};
+
#define GET_AMDGPUImageDMaskIntrinsicTable_IMPL
#include "InstCombineTables.inc"
@@ -150,6 +156,67 @@ static std::optional<Instruction *> modifyIntrinsicCall(
return RetValue;
}
+/// Attempts to fold an image sample whose users are ExtractElement + FPTrunc
+/// chains into a D16-returning version.
+static std::optional<Instruction *>
+modifyImageIntrinsicForD16(IntrinsicInst &II,
+ const AMDGPU::ImageDimIntrinsicInfo *ImageDimIntr,
+ InstCombiner &IC) {
+ SmallVector<D16Candidate, 4> Candidates;
+
+ // Collect all (ExtractElement, FPTrunc) pairs; abort on the first mismatch
+ for (User *U : II.users()) {
+ auto *Ext = dyn_cast<ExtractElementInst>(U);
+ if (!Ext || !Ext->hasOneUse())
+ return std::nullopt;
+
+ auto *Tr = dyn_cast<FPTruncInst>(*Ext->user_begin());
+ if (!Tr || !Tr->getType()->getScalarType()->isHalfTy())
+ return std::nullopt;
+
+ auto &Cand = Candidates.emplace_back();
+ Cand.InstsToErase = {Tr, Ext};
+ Cand.Replacee = Tr;
+ Cand.Index = Ext->getIndexOperand();
+ }
+
+ if (Candidates.empty())
+ return std::nullopt;
+
+ // Build the new half-vector return type
+ auto *VecTy = cast<VectorType>(II.getType());
+ Type *HalfVecTy = VecTy->getWithNewType(Type::getHalfTy(II.getContext()));
+
+ // Obtain the original image sample intrinsic's signature
+ // and replace its return type with the half-vector for D16 folding
+ SmallVector<Type *, 8> SigTys;
+ Intrinsic::getIntrinsicSignature(II.getCalledFunction(), SigTys);
+ SigTys[0] = HalfVecTy;
+
+ Function *HalfDecl = Intrinsic::getOrInsertDeclaration(
+ II.getModule(), ImageDimIntr->Intr, SigTys);
+
+ II.mutateType(HalfVecTy);
+ II.setCalledFunction(HalfDecl);
+
+ // Replace each chain with a single ExtractElement from the new D16 image
+ IRBuilder<> B(II.getContext());
+ for (auto &[Insts, Replacee, Idx] : Candidates) {
+ B.SetInsertPoint(Replacee);
+ auto *HalfExtract = B.CreateExtractElement(&II, Idx);
+ HalfExtract->takeName(Replacee);
+ Replacee->replaceAllUsesWith(HalfExtract);
+ }
+
+ // Erase the old instructions
+ for (auto &[Insts, Replacee, Idx] : Candidates) {
+ for (auto *I : Insts)
+ IC.eraseInstFromFunction(*I);
+ }
+
+ return &II;
+}
+
static std::optional<Instruction *>
simplifyAMDGCNImageIntrinsic(const GCNSubtarget *ST,
const AMDGPU::ImageDimIntrinsicInfo *ImageDimIntr,
@@ -249,65 +316,8 @@ simplifyAMDGCNImageIntrinsic(const GCNSubtarget *ST,
}
}
- // Only perform D16 folding if every user of the image sample is
- // an ExtractElementInst immediately followed by an FPTrunc to half.
- SmallVector<std::pair<ExtractElementInst *, FPTruncInst *>, 4>
- ExtractTruncPairs;
- bool AllHalfExtracts = true;
-
- for (User *U : II.users()) {
- auto *Ext = dyn_cast<ExtractElementInst>(U);
- if (!Ext || !Ext->hasOneUse()) {
- AllHalfExtracts = false;
- break;
- }
-
- auto *Tr = dyn_cast<FPTruncInst>(*Ext->user_begin());
- if (!Tr || !Tr->getType()->isHalfTy()) {
- AllHalfExtracts = false;
- break;
- }
-
- ExtractTruncPairs.emplace_back(Ext, Tr);
- }
-
- if (!ExtractTruncPairs.empty() && AllHalfExtracts) {
- auto *VecTy = cast<VectorType>(II.getType());
- Type *HalfVecTy =
- VecTy->getWithNewType(Type::getHalfTy(II.getContext()));
-
- // Obtain the original image sample intrinsic's signature
- // and replace its return type with the half-vector for D16 folding
- SmallVector<Type *, 8> SigTys;
- Intrinsic::getIntrinsicSignature(II.getCalledFunction(), SigTys);
- SigTys[0] = HalfVecTy;
-
- Module *M = II.getModule();
- Function *HalfDecl =
- Intrinsic::getOrInsertDeclaration(M, ImageDimIntr->Intr, SigTys);
-
- II.mutateType(HalfVecTy);
- II.setCalledFunction(HalfDecl);
-
- IRBuilder<> Builder(II.getContext());
- for (auto &[Ext, Tr] : ExtractTruncPairs) {
- Value *Idx = Ext->getIndexOperand();
-
- Builder.SetInsertPoint(Tr);
-
- Value *HalfExtract = Builder.CreateExtractElement(&II, Idx);
- HalfExtract->takeName(Tr);
-
- Tr->replaceAllUsesWith(HalfExtract);
- }
-
- for (auto &[Ext, Tr] : ExtractTruncPairs) {
- IC.eraseInstFromFunction(*Tr);
- IC.eraseInstFromFunction(*Ext);
- }
-
- return &II;
- }
+ if (auto FoldedII = modifyImageIntrinsicForD16(II, ImageDimIntr, IC))
+ return *FoldedII;
}
}
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
No description provided.