Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dc152f0

Browse files
authored
[IR] Add fpmath to keep list of dropUBImplyingAttrsAndMetadata (llvm#179019)
`fpmath` is precision metadata rather than UB-implying metadata. This avoids `fpmath` from being dropped in InstCombine FoldOpIntoSelect.
1 parent cf60af8 commit dc152f0

3 files changed

Lines changed: 19 additions & 8 deletions

File tree

clang/test/Headers/__clang_hip_math.hip

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5633,7 +5633,7 @@ extern "C" __device__ double test_normcdfinv(double x) {
56335633
// NCRDIV-NEXT: [[TOBOOL_NOT_I:%.*]] = icmp eq i32 [[DEC_I]], 0
56345634
// NCRDIV-NEXT: br i1 [[TOBOOL_NOT_I]], label %[[_ZL5NORMFIPKF_EXIT_LOOPEXIT:.*]], label %[[WHILE_BODY_I]], !llvm.loop [[LOOP21:![0-9]+]]
56355635
// NCRDIV: [[_ZL5NORMFIPKF_EXIT_LOOPEXIT]]:
5636-
// NCRDIV-NEXT: [[TMP1:%.*]] = tail call contract float @llvm.sqrt.f32(float [[ADD_I]])
5636+
// NCRDIV-NEXT: [[TMP1:%.*]] = tail call contract float @llvm.sqrt.f32(float [[ADD_I]]), !fpmath [[META22:![0-9]+]]
56375637
// NCRDIV-NEXT: br label %[[_ZL5NORMFIPKF_EXIT]]
56385638
// NCRDIV: [[_ZL5NORMFIPKF_EXIT]]:
56395639
// NCRDIV-NEXT: [[__R_0_I_LCSSA:%.*]] = phi float [ 0.000000e+00, %[[ENTRY]] ], [ [[TMP1]], %[[_ZL5NORMFIPKF_EXIT_LOOPEXIT]] ]
@@ -5750,7 +5750,7 @@ extern "C" __device__ float test_normf(int x, const float *y) {
57505750
// NCRDIV-NEXT: [[ADD_I]] = fadd contract double [[__R_0_I4]], [[MUL_I]]
57515751
// NCRDIV-NEXT: [[INCDEC_PTR_I]] = getelementptr inbounds nuw i8, ptr [[__A_ADDR_0_I3]], i64 8
57525752
// NCRDIV-NEXT: [[TOBOOL_NOT_I:%.*]] = icmp eq i32 [[DEC_I]], 0
5753-
// NCRDIV-NEXT: br i1 [[TOBOOL_NOT_I]], label %[[_ZL4NORMIPKD_EXIT_LOOPEXIT:.*]], label %[[WHILE_BODY_I]], !llvm.loop [[LOOP22:![0-9]+]]
5753+
// NCRDIV-NEXT: br i1 [[TOBOOL_NOT_I]], label %[[_ZL4NORMIPKD_EXIT_LOOPEXIT:.*]], label %[[WHILE_BODY_I]], !llvm.loop [[LOOP23:![0-9]+]]
57545754
// NCRDIV: [[_ZL4NORMIPKD_EXIT_LOOPEXIT]]:
57555755
// NCRDIV-NEXT: [[TMP1:%.*]] = tail call contract double @llvm.sqrt.f64(double [[ADD_I]])
57565756
// NCRDIV-NEXT: br label %[[_ZL4NORMIPKD_EXIT]]
@@ -6391,7 +6391,7 @@ extern "C" __device__ double test_rint(double x) {
63916391
// NCRDIV-NEXT: [[ADD_I]] = fadd contract float [[__R_0_I4]], [[MUL_I]]
63926392
// NCRDIV-NEXT: [[INCDEC_PTR_I]] = getelementptr inbounds nuw i8, ptr [[__A_ADDR_0_I3]], i64 4
63936393
// NCRDIV-NEXT: [[TOBOOL_NOT_I:%.*]] = icmp eq i32 [[DEC_I]], 0
6394-
// NCRDIV-NEXT: br i1 [[TOBOOL_NOT_I]], label %[[_ZL6RNORMFIPKF_EXIT]], label %[[WHILE_BODY_I]], !llvm.loop [[LOOP23:![0-9]+]]
6394+
// NCRDIV-NEXT: br i1 [[TOBOOL_NOT_I]], label %[[_ZL6RNORMFIPKF_EXIT]], label %[[WHILE_BODY_I]], !llvm.loop [[LOOP24:![0-9]+]]
63956395
// NCRDIV: [[_ZL6RNORMFIPKF_EXIT]]:
63966396
// NCRDIV-NEXT: [[__R_0_I_LCSSA:%.*]] = phi float [ 0.000000e+00, %[[ENTRY]] ], [ [[ADD_I]], %[[WHILE_BODY_I]] ]
63976397
// NCRDIV-NEXT: [[CALL_I:%.*]] = tail call contract noundef float @__ocml_rsqrt_f32(float noundef [[__R_0_I_LCSSA]]) #[[ATTR14]]
@@ -6500,7 +6500,7 @@ extern "C" __device__ float test_rnormf(int x, const float* y) {
65006500
// NCRDIV-NEXT: [[ADD_I]] = fadd contract double [[__R_0_I4]], [[MUL_I]]
65016501
// NCRDIV-NEXT: [[INCDEC_PTR_I]] = getelementptr inbounds nuw i8, ptr [[__A_ADDR_0_I3]], i64 8
65026502
// NCRDIV-NEXT: [[TOBOOL_NOT_I:%.*]] = icmp eq i32 [[DEC_I]], 0
6503-
// NCRDIV-NEXT: br i1 [[TOBOOL_NOT_I]], label %[[_ZL5RNORMIPKD_EXIT]], label %[[WHILE_BODY_I]], !llvm.loop [[LOOP24:![0-9]+]]
6503+
// NCRDIV-NEXT: br i1 [[TOBOOL_NOT_I]], label %[[_ZL5RNORMIPKD_EXIT]], label %[[WHILE_BODY_I]], !llvm.loop [[LOOP25:![0-9]+]]
65046504
// NCRDIV: [[_ZL5RNORMIPKD_EXIT]]:
65056505
// NCRDIV-NEXT: [[__R_0_I_LCSSA:%.*]] = phi double [ 0.000000e+00, %[[ENTRY]] ], [ [[ADD_I]], %[[WHILE_BODY_I]] ]
65066506
// NCRDIV-NEXT: [[CALL_I:%.*]] = tail call contract noundef double @__ocml_rsqrt_f64(double noundef [[__R_0_I_LCSSA]]) #[[ATTR14]]
@@ -7459,7 +7459,7 @@ extern "C" __device__ double test_sinpi(double x) {
74597459
// NCRDIV-LABEL: define dso_local noundef float @test_sqrtf(
74607460
// NCRDIV-SAME: float noundef [[X:%.*]]) local_unnamed_addr #[[ATTR3]] {
74617461
// NCRDIV-NEXT: [[ENTRY:.*:]]
7462-
// NCRDIV-NEXT: [[TMP0:%.*]] = tail call contract noundef float @llvm.sqrt.f32(float [[X]]), !fpmath [[META25:![0-9]+]]
7462+
// NCRDIV-NEXT: [[TMP0:%.*]] = tail call contract noundef float @llvm.sqrt.f32(float [[X]]), !fpmath [[META22]]
74637463
// NCRDIV-NEXT: ret float [[TMP0]]
74647464
//
74657465
// AMDGCNSPIRV-LABEL: define spir_func noundef float @test_sqrtf(
@@ -9421,10 +9421,10 @@ extern "C" __device__ int test_int_max(int x, int y) {
94219421
// NCRDIV: [[DOUBLE_TBAA19]] = !{[[META20:![0-9]+]], [[META20]], i64 0}
94229422
// NCRDIV: [[META20]] = !{!"double", [[META6]], i64 0}
94239423
// NCRDIV: [[LOOP21]] = distinct !{[[LOOP21]], [[META10]], [[META11]]}
9424-
// NCRDIV: [[LOOP22]] = distinct !{[[LOOP22]], [[META10]], [[META11]]}
9424+
// NCRDIV: [[META22]] = !{float 3.000000e+00}
94259425
// NCRDIV: [[LOOP23]] = distinct !{[[LOOP23]], [[META10]], [[META11]]}
94269426
// NCRDIV: [[LOOP24]] = distinct !{[[LOOP24]], [[META10]], [[META11]]}
9427-
// NCRDIV: [[META25]] = !{float 3.000000e+00}
9427+
// NCRDIV: [[LOOP25]] = distinct !{[[LOOP25]], [[META10]], [[META11]]}
94289428
// NCRDIV: [[LOOP26]] = distinct !{[[LOOP26]], [[META10]], [[META11]]}
94299429
// NCRDIV: [[LOOP27]] = distinct !{[[LOOP27]], [[META10]], [[META11]]}
94309430
//.

llvm/lib/IR/Instruction.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,11 +572,13 @@ void Instruction::dropUBImplyingAttrsAndUnknownMetadata(
572572
void Instruction::dropUBImplyingAttrsAndMetadata(ArrayRef<unsigned> Keep) {
573573
// !annotation and !prof metadata does not impact semantics.
574574
// !range, !nonnull and !align produce poison, so they are safe to speculate.
575+
// !fpmath specifies floating-point precision and does not imply UB.
575576
// !noundef and various AA metadata must be dropped, as it generally produces
576577
// immediate undefined behavior.
577578
static const unsigned KnownIDs[] = {
578579
LLVMContext::MD_annotation, LLVMContext::MD_range,
579-
LLVMContext::MD_nonnull, LLVMContext::MD_align, LLVMContext::MD_prof};
580+
LLVMContext::MD_nonnull, LLVMContext::MD_align,
581+
LLVMContext::MD_fpmath, LLVMContext::MD_prof};
580582
SmallVector<unsigned> KeepIDs;
581583
KeepIDs.reserve(Keep.size() + std::size(KnownIDs));
582584
append_range(KeepIDs, (!ProfcheckDisableMetadataFixes ? KnownIDs

llvm/test/Transforms/InstCombine/fold-fops-into-selects.ll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,12 @@ EntryBlock:
6969
; CHECK: select i1 %A, float 0x3FD5555560000000, float [[OP]]
7070
}
7171

72+
define float @test8(i1 %A, float %B) {
73+
EntryBlock:
74+
%cf = select i1 %A, float 1.000000e+00, float %B
75+
%op = fdiv float 3.000000e+00, %cf, !fpmath !{float 2.5}
76+
ret float %op
77+
; CHECK-LABEL: @test8(
78+
; CHECK: [[OP:%.*]] = fdiv float 3.000000e+00, %B, !fpmath
79+
; CHECK: select i1 %A, float 3.000000e+00, float [[OP]]
80+
}

0 commit comments

Comments
 (0)