diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/lib/Analysis/ConstantFolding.cpp @@ -1460,6 +1460,7 @@ case Intrinsic::convert_to_fp16: case Intrinsic::bitreverse: case Intrinsic::amdgcn_fmul_legacy: + case Intrinsic::amdgcn_fract: case Intrinsic::x86_sse_cvtss2si: case Intrinsic::x86_sse_cvtss2si64: case Intrinsic::x86_sse_cvttss2si: @@ -1789,9 +1790,20 @@ /// We only fold functions with finite arguments. Folding NaN and inf is /// likely to be aborted with an exception anyway, and some host libms /// have known errors raising exceptions. - if (Op->getValueAPF().isNaN() || Op->getValueAPF().isInfinity()) + if (!U.isFinite()) return nullptr; + if (IntrinsicID == Intrinsic::amdgcn_fract) { + // TODO what should amdgcn_fract return for tiny negative arguments? + // GLSL defines fract(x) as x - floor(x). + // OpenCL defines fract(x) as fmin(x - floor(x), 0x1.fffffep-1f): "The + // min() operator is there to prevent fract(-small) from returning 1.0. + // It returns the largest positive floating-point number less than 1.0." + APFloat FloorU(U); + FloorU.roundToIntegral(APFloat::rmTowardNegative); + return ConstantFP::get(Ty->getContext(), U - FloorU); + } + /// Currently APFloat versions of these functions do not exist, so we use /// the host native double versions. Float versions are not called /// directly but for all these it is true (float)(f((double)arg)) == diff --git a/llvm/test/Analysis/ConstantFolding/AMDGPU/fract.ll b/llvm/test/Analysis/ConstantFolding/AMDGPU/fract.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Analysis/ConstantFolding/AMDGPU/fract.ll @@ -0,0 +1,99 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt < %s -instsimplify -S | FileCheck %s + +declare half @llvm.amdgcn.fract.f16(half) +declare float @llvm.amdgcn.fract.f32(float) +declare double @llvm.amdgcn.fract.f64(double) + +define void @test_f16(half* %p) { +; CHECK-LABEL: @test_f16( +; CHECK-NEXT: store volatile half 0xH0000, half* [[P:%.*]] +; CHECK-NEXT: store volatile half 0xH0000, half* [[P]] +; CHECK-NEXT: store volatile half 0xH3400, half* [[P]] +; CHECK-NEXT: store volatile half 0xH3B00, half* [[P]] +; CHECK-NEXT: [[E:%.*]] = call half @llvm.amdgcn.fract.f16(half 0xH7C00) +; CHECK-NEXT: store volatile half [[E]], half* [[P]] +; CHECK-NEXT: [[F:%.*]] = call half @llvm.amdgcn.fract.f16(half 0xHFC00) +; CHECK-NEXT: store volatile half [[F]], half* [[P]] +; CHECK-NEXT: [[G:%.*]] = call half @llvm.amdgcn.fract.f16(half 0xH7E00) +; CHECK-NEXT: store volatile half [[G]], half* [[P]] +; CHECK-NEXT: ret void +; + %a = call half @llvm.amdgcn.fract.f16(half +0.0) + store volatile half %a, half* %p + %b = call half @llvm.amdgcn.fract.f16(half +1.0) + store volatile half %b, half* %p + %c = call half @llvm.amdgcn.fract.f16(half +2.25) + store volatile half %c, half* %p + %d = call half @llvm.amdgcn.fract.f16(half -6.125) + store volatile half %d, half* %p + %e = call half @llvm.amdgcn.fract.f16(half 0x7ff0000000000000) ; +inf + store volatile half %e, half* %p + %f = call half @llvm.amdgcn.fract.f16(half 0xfff0000000000000) ; -inf + store volatile half %f, half* %p + %g = call half @llvm.amdgcn.fract.f16(half 0x7ff8000000000000) ; nan + store volatile half %g, half* %p + ret void +} + +define void @test_f32(float* %p) { +; CHECK-LABEL: @test_f32( +; CHECK-NEXT: store volatile float 0.000000e+00, float* [[P:%.*]] +; CHECK-NEXT: store volatile float 0.000000e+00, float* [[P]] +; CHECK-NEXT: store volatile float 2.500000e-01, float* [[P]] +; CHECK-NEXT: store volatile float 8.750000e-01, float* [[P]] +; CHECK-NEXT: [[E:%.*]] = call float @llvm.amdgcn.fract.f32(float 0x7FF0000000000000) +; CHECK-NEXT: store volatile float [[E]], float* [[P]] +; CHECK-NEXT: [[F:%.*]] = call float @llvm.amdgcn.fract.f32(float 0xFFF0000000000000) +; CHECK-NEXT: store volatile float [[F]], float* [[P]] +; CHECK-NEXT: [[G:%.*]] = call float @llvm.amdgcn.fract.f32(float 0x7FF8000000000000) +; CHECK-NEXT: store volatile float [[G]], float* [[P]] +; CHECK-NEXT: ret void +; + %a = call float @llvm.amdgcn.fract.f32(float +0.0) + store volatile float %a, float* %p + %b = call float @llvm.amdgcn.fract.f32(float +1.0) + store volatile float %b, float* %p + %c = call float @llvm.amdgcn.fract.f32(float +2.25) + store volatile float %c, float* %p + %d = call float @llvm.amdgcn.fract.f32(float -6.125) + store volatile float %d, float* %p + %e = call float @llvm.amdgcn.fract.f32(float 0x7ff0000000000000) ; +inf + store volatile float %e, float* %p + %f = call float @llvm.amdgcn.fract.f32(float 0xfff0000000000000) ; -inf + store volatile float %f, float* %p + %g = call float @llvm.amdgcn.fract.f32(float 0x7ff8000000000000) ; nan + store volatile float %g, float* %p + ret void +} + +define void @test_f64(double* %p) { +; CHECK-LABEL: @test_f64( +; CHECK-NEXT: store volatile double 0.000000e+00, double* [[P:%.*]] +; CHECK-NEXT: store volatile double 0.000000e+00, double* [[P]] +; CHECK-NEXT: store volatile double 2.500000e-01, double* [[P]] +; CHECK-NEXT: store volatile double 8.750000e-01, double* [[P]] +; CHECK-NEXT: [[E:%.*]] = call double @llvm.amdgcn.fract.f64(double 0x7FF0000000000000) +; CHECK-NEXT: store volatile double [[E]], double* [[P]] +; CHECK-NEXT: [[F:%.*]] = call double @llvm.amdgcn.fract.f64(double 0xFFF0000000000000) +; CHECK-NEXT: store volatile double [[F]], double* [[P]] +; CHECK-NEXT: [[G:%.*]] = call double @llvm.amdgcn.fract.f64(double 0x7FF8000000000000) +; CHECK-NEXT: store volatile double [[G]], double* [[P]] +; CHECK-NEXT: ret void +; + %a = call double @llvm.amdgcn.fract.f64(double +0.0) + store volatile double %a, double* %p + %b = call double @llvm.amdgcn.fract.f64(double +1.0) + store volatile double %b, double* %p + %c = call double @llvm.amdgcn.fract.f64(double +2.25) + store volatile double %c, double* %p + %d = call double @llvm.amdgcn.fract.f64(double -6.125) + store volatile double %d, double* %p + %e = call double @llvm.amdgcn.fract.f64(double 0x7ff0000000000000) ; +inf + store volatile double %e, double* %p + %f = call double @llvm.amdgcn.fract.f64(double 0xfff0000000000000) ; -inf + store volatile double %f, double* %p + %g = call double @llvm.amdgcn.fract.f64(double 0x7ff8000000000000) ; nan + store volatile double %g, double* %p + ret void +}