Skip to content

Commit aad16bf

Browse files
LewisCrawfordmahesh-attarde
authored andcommitted
[ConstantFolding] Fix nvvm_round folding on PPC (llvm#149837)
Fix a failing test for constant-folding the nvvm_round intrinsic. The original implementation added in llvm#141233 used a native libm call to the "round" function, but on PPC this produces +0.0 if the input is -0.0, which caused a test failure. This patch updates it to use APFloat functions instead of native libm calls to ensure cross-platform consistency.
1 parent 6a9715c commit aad16bf

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2677,11 +2677,14 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
26772677

26782678
case Intrinsic::nvvm_round_ftz_f:
26792679
case Intrinsic::nvvm_round_f:
2680-
case Intrinsic::nvvm_round_d:
2681-
return ConstantFoldFP(
2682-
round, APF, Ty,
2683-
nvvm::GetNVVMDenromMode(
2684-
nvvm::UnaryMathIntrinsicShouldFTZ(IntrinsicID)));
2680+
case Intrinsic::nvvm_round_d: {
2681+
// Use APFloat implementation instead of native libm call, as some
2682+
// implementations (e.g. on PPC) do not preserve the sign of negative 0.
2683+
bool IsFTZ = nvvm::UnaryMathIntrinsicShouldFTZ(IntrinsicID);
2684+
auto V = IsFTZ ? FTZPreserveSign(APF) : APF;
2685+
V.roundToIntegral(APFloat::rmNearestTiesToAway);
2686+
return ConstantFP::get(Ty->getContext(), V);
2687+
}
26852688

26862689
case Intrinsic::nvvm_saturate_ftz_f:
26872690
case Intrinsic::nvvm_saturate_d:

0 commit comments

Comments
 (0)