-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[AMDGPU] Try to reuse register with the constant from compare in v_cndmask #148740
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4842,11 +4842,94 @@ AMDGPUTargetLowering::foldFreeOpFromSelect(TargetLowering::DAGCombinerInfo &DCI, | |||||||||||||||||||||||||||||
| return SDValue(); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // Detect when CMP and SELECT use the same constant and fold them to avoid | ||||||||||||||||||||||||||||||
| // loading the constant twice. Specifically handles patterns like: | ||||||||||||||||||||||||||||||
| // %cmp = icmp eq i32 %val, 4242 | ||||||||||||||||||||||||||||||
| // %sel = select i1 %cmp, i32 4242, i32 %other | ||||||||||||||||||||||||||||||
| // It can be optimized to reuse %val instead of 4242 in select. | ||||||||||||||||||||||||||||||
| static SDValue | ||||||||||||||||||||||||||||||
| foldCmpSelectWithSharedConstant(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, | ||||||||||||||||||||||||||||||
| const AMDGPUSubtarget *ST) { | ||||||||||||||||||||||||||||||
| SDValue Cond = N->getOperand(0); | ||||||||||||||||||||||||||||||
| SDValue TrueVal = N->getOperand(1); | ||||||||||||||||||||||||||||||
| SDValue FalseVal = N->getOperand(2); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // Check if condition is a comparison. | ||||||||||||||||||||||||||||||
| if (Cond.getOpcode() != ISD::SETCC) | ||||||||||||||||||||||||||||||
| return SDValue(); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| SDValue LHS = Cond.getOperand(0); | ||||||||||||||||||||||||||||||
| SDValue RHS = Cond.getOperand(1); | ||||||||||||||||||||||||||||||
| ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get(); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| bool isFloatingPoint = LHS.getValueType().isFloatingPoint(); | ||||||||||||||||||||||||||||||
| bool isInteger = LHS.getValueType().isInteger(); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // Handle simple floating-point and integer types only. | ||||||||||||||||||||||||||||||
| if (!isFloatingPoint && !isInteger) | ||||||||||||||||||||||||||||||
| return SDValue(); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| bool isEquality = CC == (isFloatingPoint ? ISD::SETOEQ : ISD::SETEQ); | ||||||||||||||||||||||||||||||
| bool isNonEquality = CC == (isFloatingPoint ? ISD::SETONE : ISD::SETNE); | ||||||||||||||||||||||||||||||
| if (!isEquality && !isNonEquality) | ||||||||||||||||||||||||||||||
| return SDValue(); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| SDValue ArgVal, ConstVal; | ||||||||||||||||||||||||||||||
| if ((isFloatingPoint && isa<ConstantFPSDNode>(RHS)) || | ||||||||||||||||||||||||||||||
| (isInteger && isa<ConstantSDNode>(RHS))) { | ||||||||||||||||||||||||||||||
| ConstVal = RHS; | ||||||||||||||||||||||||||||||
| ArgVal = LHS; | ||||||||||||||||||||||||||||||
| } else if ((isFloatingPoint && isa<ConstantFPSDNode>(LHS)) || | ||||||||||||||||||||||||||||||
| (isInteger && isa<ConstantSDNode>(LHS))) { | ||||||||||||||||||||||||||||||
| ConstVal = LHS; | ||||||||||||||||||||||||||||||
| ArgVal = RHS; | ||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||
| return SDValue(); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
+4878
to
+4888
|
||||||||||||||||||||||||||||||
| if ((isFloatingPoint && isa<ConstantFPSDNode>(RHS)) || | |
| (isInteger && isa<ConstantSDNode>(RHS))) { | |
| ConstVal = RHS; | |
| ArgVal = LHS; | |
| } else if ((isFloatingPoint && isa<ConstantFPSDNode>(LHS)) || | |
| (isInteger && isa<ConstantSDNode>(LHS))) { | |
| ConstVal = LHS; | |
| ArgVal = RHS; | |
| } else { | |
| return SDValue(); | |
| } | |
| std::tie(ConstVal, ArgVal) = getConstAndArgValues(LHS, RHS, isFloatingPoint, isInteger); | |
| if (!ConstVal || !ArgVal) | |
| return SDValue(); |
Copilot
AI
Jul 14, 2025
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.
Consider using dynamic_cast instead of static_cast for safer type conversion, or add an assertion to verify the cast is valid.
| const GCNSubtarget *GCNST = static_cast<const GCNSubtarget *>(ST); | |
| const GCNSubtarget *GCNST = dynamic_cast<const GCNSubtarget *>(ST); | |
| if (!GCNST) { | |
| // Return early if the cast is invalid | |
| return SDValue(); | |
| } |
Uh oh!
There was an error while loading. Please reload this page.