https://github.com/yxsamliu created https://github.com/llvm/llvm-project/pull/244343
Inlining can lose a call's nofpclass(inf) return constraint when the callee returns a select. Carry the constraint over as ninf on eligible scalar selects whose only use is the return. This lets later passes simplify overflow paths that would return infinity. >From 6f7faa4f3c70903dd8eb658fdcbe5c8b7d93fcd2 Mon Sep 17 00:00:00 2001 From: "Yaxun (Sam) Liu" <[email protected]> Date: Thu, 19 Nov 2026 18:43:13 +0000 Subject: [PATCH] [Inline] Preserve nofpclass(inf) on returned selects Inlining can lose a call's nofpclass(inf) return constraint when the callee returns a select. Carry the constraint over as ninf on eligible scalar selects whose only use is the return. This lets later passes simplify overflow paths that would return infinity. AI assisted. --- llvm/lib/Transforms/Utils/InlineFunction.cpp | 14 +++++ .../Inline/ret_attr_nofpclass-select.ll | 58 +++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 llvm/test/Transforms/Inline/ret_attr_nofpclass-select.ll diff --git a/llvm/lib/Transforms/Utils/InlineFunction.cpp b/llvm/lib/Transforms/Utils/InlineFunction.cpp index 918c8c02493bd5..72e6ba43c4d463 100644 --- a/llvm/lib/Transforms/Utils/InlineFunction.cpp +++ b/llvm/lib/Transforms/Utils/InlineFunction.cpp @@ -1655,6 +1655,19 @@ static AttrBuilder IdentifyValidPoisonGeneratingAttributes(CallBase &CB) { return Valid; } +// Preserve a no-infinity return constraint when the original call disappears. +static void preserveNoInfReturn(CallBase &CB, ArrayRef<ReturnInst *> Returns) { + if ((CB.getRetNoFPClass() & fcInf) != fcInf) + return; + + for (ReturnInst *RI : Returns) { + auto *SI = dyn_cast_if_present<SelectInst>(RI->getReturnValue()); + if (SI && SI->getType()->isFloatingPointTy() && SI->hasOneUse() && + SI->getParent() == RI->getParent()) + SI->setHasNoInfs(true); + } +} + static void AddReturnAttributes(CallBase &CB, ValueToValueMapTy &VMap, ClonedCodeInfo &InlinedFunctionInfo) { AttrBuilder CallSiteValidUB = IdentifyValidUBGeneratingAttributes(CB); @@ -2951,6 +2964,7 @@ void llvm::InlineFunctionImpl(CallBase &CB, InlineFunctionInfo &IFI, // Clone return attributes on the callsite into the calls within the inlined // function which feed into its return value. AddReturnAttributes(CB, VMap, InlinedFunctionInfo); + preserveNoInfReturn(CB, Returns); // Clone attributes on the params of the callsite to calls within the // inlined function which use the same param. diff --git a/llvm/test/Transforms/Inline/ret_attr_nofpclass-select.ll b/llvm/test/Transforms/Inline/ret_attr_nofpclass-select.ll new file mode 100644 index 00000000000000..df226d3e96ce0e --- /dev/null +++ b/llvm/test/Transforms/Inline/ret_attr_nofpclass-select.ll @@ -0,0 +1,58 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6 +; RUN: opt -S -passes=inline %s | FileCheck %s + +define internal float @callee_select(i1 %overflow, i1 %lower, float %normal) alwaysinline { + %upper = select i1 %overflow, float +inf, float %normal + %result = select i1 %lower, float -1.0, float %upper + ret float %result +} + +define float @caller_select_noinf(i1 %overflow, i1 %lower, float %normal) { +; CHECK-LABEL: define float @caller_select_noinf( +; CHECK-SAME: i1 [[OVERFLOW:%.*]], i1 [[LOWER:%.*]], float [[NORMAL:%.*]]) { +; CHECK-NEXT: [[UPPER_I:%.*]] = select i1 [[OVERFLOW]], float +inf, float [[NORMAL]] +; CHECK-NEXT: [[RESULT_I:%.*]] = select ninf i1 [[LOWER]], float -1.000000e+00, float [[UPPER_I]] +; CHECK-NEXT: ret float [[RESULT_I]] +; + %value = call nofpclass(inf) float @callee_select(i1 %overflow, i1 %lower, float %normal) + ret float %value +} + +define float @caller_select_unrestricted(i1 %overflow, i1 %lower, float %normal) { +; CHECK-LABEL: define float @caller_select_unrestricted( +; CHECK-SAME: i1 [[OVERFLOW:%.*]], i1 [[LOWER:%.*]], float [[NORMAL:%.*]]) { +; CHECK-NEXT: [[UPPER_I:%.*]] = select i1 [[OVERFLOW]], float +inf, float [[NORMAL]] +; CHECK-NEXT: [[RESULT_I:%.*]] = select i1 [[LOWER]], float -1.000000e+00, float [[UPPER_I]] +; CHECK-NEXT: ret float [[RESULT_I]] +; + %value = call float @callee_select(i1 %overflow, i1 %lower, float %normal) + ret float %value +} + +define float @caller_select_one_inf_class(i1 %overflow, i1 %lower, float %normal) { +; CHECK-LABEL: define float @caller_select_one_inf_class( +; CHECK-SAME: i1 [[OVERFLOW:%.*]], i1 [[LOWER:%.*]], float [[NORMAL:%.*]]) { +; CHECK-NEXT: [[UPPER_I:%.*]] = select i1 [[OVERFLOW]], float +inf, float [[NORMAL]] +; CHECK-NEXT: [[RESULT_I:%.*]] = select i1 [[LOWER]], float -1.000000e+00, float [[UPPER_I]] +; CHECK-NEXT: ret float [[RESULT_I]] +; + %value = call nofpclass(pinf) float @callee_select(i1 %overflow, i1 %lower, float %normal) + ret float %value +} + +define internal float @callee_select_with_store(i1 %overflow, float %normal, ptr %out) alwaysinline { + %result = select i1 %overflow, float +inf, float %normal + store float %result, ptr %out + ret float %result +} + +define float @caller_select_shared_result(i1 %overflow, float %normal, ptr %out) { +; CHECK-LABEL: define float @caller_select_shared_result( +; CHECK-SAME: i1 [[OVERFLOW:%.*]], float [[NORMAL:%.*]], ptr [[OUT:%.*]]) { +; CHECK-NEXT: [[RESULT_I:%.*]] = select i1 [[OVERFLOW]], float +inf, float [[NORMAL]] +; CHECK-NEXT: store float [[RESULT_I]], ptr [[OUT]], align 4 +; CHECK-NEXT: ret float [[RESULT_I]] +; + %value = call nofpclass(inf) float @callee_select_with_store(i1 %overflow, float %normal, ptr %out) + ret float %value +} _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
