-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Re apply 130577 narrow math for and operand #133896
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
Shoreshen
merged 26 commits into
llvm:main
from
Shoreshen:re-apply-130577-narrow-math-for-and-operand
Apr 17, 2025
Merged
Changes from 4 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
10bb8da
Revert "Revert "[AMDGPU][CodeGenPrepare] Narrow 64 bit math to 32 bit…
Shoreshen fd6b530
Merge branch 'main' into revert-133880-revert-130577-narrow-math-for-…
Shoreshen 69271c0
fix address sanitizer failure
Shoreshen f831e51
Merge branch 'main' into re-apply-130577-narrow-math-for-and-operand
Shoreshen a937f5b
fix comments
Shoreshen 9fd971f
Merge branch 'main' into re-apply-130577-narrow-math-for-and-operand
Shoreshen 990da17
Merge branch 'main' into re-apply-130577-narrow-math-for-and-operand
Shoreshen 80695a0
remove isd related type check
Shoreshen 37dc751
Merge branch 'main' into re-apply-130577-narrow-math-for-and-operand
Shoreshen 72560d7
Merge branch 'main' into re-apply-130577-narrow-math-for-and-operand
Shoreshen 8d87d7c
fix comments
Shoreshen 92442bf
Merge branch 'main' into re-apply-130577-narrow-math-for-and-operand
Shoreshen 7db8bae
Merge branch 'main' into re-apply-130577-narrow-math-for-and-operand
Shoreshen 96a3cd9
Merge branch 'main' into re-apply-130577-narrow-math-for-and-operand
Shoreshen f2db6a2
Merge branch 'main' into re-apply-130577-narrow-math-for-and-operand
Shoreshen ef91659
fix comments
Shoreshen 11a745c
fix comment
Shoreshen c84f1f3
Merge branch 'main' into re-apply-130577-narrow-math-for-and-operand
Shoreshen 468eec7
fix comment, consider one constant situation
Shoreshen b185ed9
Merge branch 'main' into re-apply-130577-narrow-math-for-and-operand
Shoreshen 7332e7d
Merge branch 'main' into re-apply-130577-narrow-math-for-and-operand
Shoreshen 3e06714
Merge branch 'main' into re-apply-130577-narrow-math-for-and-operand
Shoreshen 90bad11
Merge branch 'main' into re-apply-130577-narrow-math-for-and-operand
Shoreshen 6d34b08
Merge branch 'main' into re-apply-130577-narrow-math-for-and-operand
Shoreshen 8add585
Merge branch 'main' into re-apply-130577-narrow-math-for-and-operand
Shoreshen a5837c4
Merge branch 'main' into re-apply-130577-narrow-math-for-and-operand
Shoreshen 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1561,6 +1561,87 @@ void AMDGPUCodeGenPrepareImpl::expandDivRem64(BinaryOperator &I) const { | |
llvm_unreachable("not a division"); | ||
} | ||
|
||
Type *findSmallestLegalBits(Instruction *I, int OrigBit, int MaxBitsNeeded, | ||
const TargetLowering *TLI, const DataLayout &DL) { | ||
Shoreshen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (MaxBitsNeeded >= OrigBit) | ||
return nullptr; | ||
|
||
Type *NewType = I->getType()->getWithNewBitWidth(MaxBitsNeeded); | ||
while (OrigBit > MaxBitsNeeded) { | ||
if (TLI->isOperationLegalOrCustom( | ||
TLI->InstructionOpcodeToISD(I->getOpcode()), | ||
Shoreshen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
TLI->getValueType(DL, NewType, true))) | ||
return NewType; | ||
|
||
MaxBitsNeeded *= 2; | ||
NewType = I->getType()->getWithNewBitWidth(MaxBitsNeeded); | ||
} | ||
return nullptr; | ||
} | ||
|
||
static bool tryNarrowMathIfNoOverflow(Instruction *I, const TargetLowering *TLI, | ||
const TargetTransformInfo &TTI, | ||
Shoreshen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const DataLayout &DL) { | ||
unsigned Opc = I->getOpcode(); | ||
Type *OldType = I->getType(); | ||
|
||
if (Opc != Instruction::Add && Opc != Instruction::Mul) | ||
return false; | ||
|
||
unsigned OrigBit = OldType->getScalarSizeInBits(); | ||
unsigned MaxBitsNeeded = OrigBit; | ||
|
||
switch (Opc) { | ||
case Instruction::Add: | ||
MaxBitsNeeded = KnownBits::add(computeKnownBits(I->getOperand(0), DL), | ||
computeKnownBits(I->getOperand(1), DL)) | ||
.countMaxActiveBits(); | ||
break; | ||
case Instruction::Mul: | ||
MaxBitsNeeded = KnownBits::mul(computeKnownBits(I->getOperand(0), DL), | ||
computeKnownBits(I->getOperand(1), DL)) | ||
.countMaxActiveBits(); | ||
break; | ||
default: | ||
llvm_unreachable("Unexpected opcode, only valid for Instruction::Add and " | ||
"Instruction::Mul."); | ||
} | ||
Shoreshen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
MaxBitsNeeded = std::max<unsigned>(bit_ceil(MaxBitsNeeded), 8); | ||
Type *NewType = findSmallestLegalBits(I, OrigBit, MaxBitsNeeded, TLI, DL); | ||
|
||
if (!NewType) | ||
return false; | ||
|
||
// Old cost | ||
InstructionCost OldCost = | ||
TTI.getArithmeticInstrCost(Opc, OldType, TTI::TCK_RecipThroughput); | ||
// New cost of new op | ||
InstructionCost NewCost = | ||
TTI.getArithmeticInstrCost(Opc, NewType, TTI::TCK_RecipThroughput); | ||
// New cost of narrowing 2 operands (use trunc) | ||
NewCost += 2 * TTI.getCastInstrCost(Instruction::Trunc, NewType, OldType, | ||
Shoreshen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
TTI.getCastContextHint(I), | ||
TTI::TCK_RecipThroughput); | ||
// New cost of zext narrowed result to original type | ||
NewCost += | ||
TTI.getCastInstrCost(Instruction::ZExt, OldType, NewType, | ||
TTI.getCastContextHint(I), TTI::TCK_RecipThroughput); | ||
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. Can we move this whole back to the generic code? 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. |
||
if (NewCost >= OldCost) | ||
return false; | ||
|
||
IRBuilder<> Builder(I); | ||
Value *Trunc0 = Builder.CreateTrunc(I->getOperand(0), NewType); | ||
Value *Trunc1 = Builder.CreateTrunc(I->getOperand(1), NewType); | ||
Value *Arith = | ||
Builder.CreateBinOp((Instruction::BinaryOps)Opc, Trunc0, Trunc1); | ||
|
||
Value *Zext = Builder.CreateZExt(Arith, OldType); | ||
I->replaceAllUsesWith(Zext); | ||
I->eraseFromParent(); | ||
return true; | ||
} | ||
|
||
bool AMDGPUCodeGenPrepareImpl::visitBinaryOperator(BinaryOperator &I) { | ||
if (foldBinOpIntoSelect(I)) | ||
return true; | ||
|
@@ -1571,6 +1652,9 @@ bool AMDGPUCodeGenPrepareImpl::visitBinaryOperator(BinaryOperator &I) { | |
|
||
if (UseMul24Intrin && replaceMulWithMul24(I)) | ||
return true; | ||
if (tryNarrowMathIfNoOverflow(&I, ST.getTargetLowering(), | ||
TM.getTargetTransformInfo(F), DL)) | ||
return true; | ||
|
||
bool Changed = false; | ||
Instruction::BinaryOps Opc = I.getOpcode(); | ||
|
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.
This seems to be reinventing TLI::getTypeToPromoteTo
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.
Or potentially integrate the logic there.
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.
Hi @arsenm @shiltian , I've removed the function by avoiding using DAG selection related functionalities...
As discussed with @arsenm I'll use DataLayout::getSmallestLegalIntType instead~ Thanks