-
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 all 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 |
---|---|---|
|
@@ -1559,6 +1559,80 @@ void AMDGPUCodeGenPrepareImpl::expandDivRem64(BinaryOperator &I) const { | |
llvm_unreachable("not a division"); | ||
} | ||
|
||
/* | ||
This will cause non-byte load in consistency, for example: | ||
``` | ||
%load = load i1, ptr addrspace(4) %arg, align 4 | ||
%zext = zext i1 %load to | ||
i64 %add = add i64 %zext | ||
``` | ||
Instead of creating `s_and_b32 s0, s0, 1`, | ||
it will create `s_and_b32 s0, s0, 0xff`. | ||
We accept this change since the non-byte load assumes the upper bits | ||
within the byte are all 0. | ||
*/ | ||
static bool tryNarrowMathIfNoOverflow(Instruction *I, | ||
const SITargetLowering *TLI, | ||
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 use references |
||
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(); | ||
|
||
if (Opc != Instruction::Add && Opc != Instruction::Mul) | ||
llvm_unreachable("Unexpected opcode, only valid for Instruction::Add and " | ||
"Instruction::Mul."); | ||
|
||
unsigned MaxBitsNeeded = computeKnownBits(I, DL).countMaxActiveBits(); | ||
|
||
MaxBitsNeeded = std::max<unsigned>(bit_ceil(MaxBitsNeeded), 8); | ||
Type *NewType = DL.getSmallestLegalIntType(I->getContext(), MaxBitsNeeded); | ||
if (!NewType) | ||
return false; | ||
unsigned NewBit = NewType->getIntegerBitWidth(); | ||
if (NewBit >= OrigBit) | ||
return false; | ||
NewType = I->getType()->getWithNewBitWidth(NewBit); | ||
jmmartinez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// 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) | ||
int NumOfNonConstOps = 2; | ||
if (isa<Constant>(I->getOperand(0)) || isa<Constant>(I->getOperand(1))) { | ||
// Cannot be both constant, should be propagated | ||
NumOfNonConstOps = 1; | ||
} | ||
NewCost += NumOfNonConstOps * TTI.getCastInstrCost(Instruction::Trunc, | ||
NewType, OldType, | ||
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); | ||
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; | ||
|
@@ -1569,6 +1643,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
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.
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.
Comment is off topic for what this actually does (should also prefer c++ style comments)