[llvm] IR: Add !nofpclass metadata (PR #177140)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 21 03:17:25 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-ir
Author: Matt Arsenault (arsenm)
<details>
<summary>Changes</summary>
This adds the analagous metadata to the nofpclass attribute
to assert values are not a certain set of floating-point classes.
This allows the same information to be expressed if a function
argument is passed indirectly. This matches the bitmask encoding
of nofpclass.
I also think this should be allowed for stores to symmetrically handle
sret, but leave that for later.
Alternatively we could add a more expressive !fprange metadata,
but that would be much more complex. It's useful to match the attribute,
and more annotations can always be added.
Fixes #<!-- -->133560
---
Full diff: https://github.com/llvm/llvm-project/pull/177140.diff
6 Files Affected:
- (modified) llvm/docs/LangRef.rst (+31)
- (modified) llvm/include/llvm/IR/FixedMetadataKinds.def (+1)
- (modified) llvm/lib/Analysis/ValueTracking.cpp (+11)
- (modified) llvm/lib/IR/Verifier.cpp (+25)
- (modified) llvm/test/Transforms/Attributor/nofpclass.ll (+58)
- (added) llvm/test/Verifier/nofpclass-metadata.ll (+101)
``````````diff
diff --git a/llvm/docs/LangRef.rst b/llvm/docs/LangRef.rst
index 7c212097286ac..48d6ede26c682 100644
--- a/llvm/docs/LangRef.rst
+++ b/llvm/docs/LangRef.rst
@@ -7448,6 +7448,37 @@ Examples:
!2 = !{ i8 0, i8 2, i8 3, i8 6 }
!3 = !{ i8 -2, i8 0, i8 3, i8 6 }
+
+.. _nofpclass-metadata:
+
+'``nofpclass``' Metadata
+^^^^^^^^^^^^^^^^^^^^^^^^
+
+``nofpclass`` metadata may be attached only to ``load``
+instructions. It asserts floating-point value classes which will not
+be loaded. The representation is an i32 constant integer encoding a
+10-bit bitmask, with the same meaning and format as the
+:ref:`nofpclass <nofpclass>` attribute. If the loaded value is one of
+the specified floating-point classes, a poison value is returned. The
+intrepretation does not depend on the floating-point environment. A
+zero value would be meaningless and is invalid.
+
+Examples:
+
+.. code-block:: llvm
+ %not.nan = load float, ptr %p, align 4, !nofpclass !0
+ %not.inf = load <2 x float>, ptr %p, align 4, !nofpclass !1
+ %only.normal = load double, ptr %p, align 8, !nofpclass !2
+ %always.poison = load double, ptr %p, align 8, !nofpclass !3
+ %not.nan.array = load [2 x <2 x float>], ptr %p, !nofpclass !1
+ %not.nan.struct = load { 2 x float }, ptr %p, align 8, !nofpclass !1
+
+ !0 = !{ i32 3 }
+ !1 = !{ i32 516 }
+ !2 = !{ i32 759 }
+ !3 = !{ i32 1023 }
+
+
'``absolute_symbol``' Metadata
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/llvm/include/llvm/IR/FixedMetadataKinds.def b/llvm/include/llvm/IR/FixedMetadataKinds.def
index 01d22bdd7c996..98129985714b2 100644
--- a/llvm/include/llvm/IR/FixedMetadataKinds.def
+++ b/llvm/include/llvm/IR/FixedMetadataKinds.def
@@ -58,3 +58,4 @@ LLVM_FIXED_MD_KIND(MD_nofree, "nofree", 43)
LLVM_FIXED_MD_KIND(MD_captures, "captures", 44)
LLVM_FIXED_MD_KIND(MD_alloc_token, "alloc_token", 45)
LLVM_FIXED_MD_KIND(MD_implicit_ref, "implicit.ref", 46)
+LLVM_FIXED_MD_KIND(MD_nofpclass, "nofpclass", 47)
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 171952120fc40..d6fc0889c3efd 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -5098,6 +5098,17 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
.intersectWith(ComputeForArm(Op->getOperand(2), /*Invert=*/true));
break;
}
+ case Instruction::Load: {
+ const MDNode *NoFPClass =
+ cast<LoadInst>(Op)->getMetadata(LLVMContext::MD_nofpclass);
+ if (!NoFPClass)
+ break;
+
+ ConstantInt *MaskVal =
+ mdconst::extract<ConstantInt>(NoFPClass->getOperand(0));
+ Known.knownNot(static_cast<FPClassTest>(MaskVal->getZExtValue()));
+ break;
+ }
case Instruction::Call: {
const CallInst *II = cast<CallInst>(Op);
const Intrinsic::ID IID = II->getIntrinsicID();
diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp
index 055b5852dcce7..c098e9e0f200d 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -529,6 +529,7 @@ class Verifier : public InstVisitor<Verifier>, VerifierSupport {
void verifyRangeLikeMetadata(const Value &V, const MDNode *Range, Type *Ty,
RangeLikeMetadataKind Kind);
void visitRangeMetadata(Instruction &I, MDNode *Range, Type *Ty);
+ void visitNoFPClassMetadata(Instruction &I, MDNode *Range, Type *Ty);
void visitNoaliasAddrspaceMetadata(Instruction &I, MDNode *Range, Type *Ty);
void visitDereferenceableMetadata(Instruction &I, MDNode *MD);
void visitNofreeMetadata(Instruction &I, MDNode *MD);
@@ -4570,6 +4571,25 @@ void Verifier::visitRangeMetadata(Instruction &I, MDNode *Range, Type *Ty) {
verifyRangeLikeMetadata(I, Range, Ty, RangeLikeMetadataKind::Range);
}
+void Verifier::visitNoFPClassMetadata(Instruction &I, MDNode *NoFPClass,
+ Type *Ty) {
+ Check(AttributeFuncs::isNoFPClassCompatibleType(Ty),
+ "nofpclass only applies to floating-point typed loads", I);
+
+ Check(NoFPClass->getNumOperands() == 1,
+ "nofpclass must have exactly one entry", NoFPClass);
+ ConstantInt *MaskVal =
+ mdconst::dyn_extract<ConstantInt>(NoFPClass->getOperand(0));
+ Check(MaskVal && MaskVal->getType()->isIntegerTy(32),
+ "nofpclass entry must be a constant i32", NoFPClass);
+ uint32_t Val = MaskVal->getZExtValue();
+ Check(Val != 0, "'nofpclass' must have at least one test bit set", NoFPClass,
+ I);
+
+ Check((Val & ~static_cast<unsigned>(fcAllFlags)) == 0,
+ "Invalid value for 'nofpclass' test mask", NoFPClass, I);
+}
+
void Verifier::visitNoaliasAddrspaceMetadata(Instruction &I, MDNode *Range,
Type *Ty) {
assert(Range && Range == I.getMetadata(LLVMContext::MD_noalias_addrspace) &&
@@ -5692,6 +5712,11 @@ void Verifier::visitInstruction(Instruction &I) {
visitRangeMetadata(I, Range, I.getType());
}
+ if (MDNode *MD = I.getMetadata(LLVMContext::MD_nofpclass)) {
+ Check(isa<LoadInst>(I), "nofpclass is only for loads", &I);
+ visitNoFPClassMetadata(I, MD, I.getType());
+ }
+
if (MDNode *Range = I.getMetadata(LLVMContext::MD_noalias_addrspace)) {
Check(isa<LoadInst>(I) || isa<StoreInst>(I) || isa<AtomicRMWInst>(I) ||
isa<AtomicCmpXchgInst>(I) || isa<CallInst>(I),
diff --git a/llvm/test/Transforms/Attributor/nofpclass.ll b/llvm/test/Transforms/Attributor/nofpclass.ll
index d325ba001e65c..68b5e80f5419d 100644
--- a/llvm/test/Transforms/Attributor/nofpclass.ll
+++ b/llvm/test/Transforms/Attributor/nofpclass.ll
@@ -3734,6 +3734,62 @@ define float @fadd_double_no_zero_or_sub__output_only_is_ftpz(float noundef nofp
ret float %add
}
+define float @infer_return_from_load_nofpclass_md_f32_0(ptr %ptr) {
+; CHECK: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read)
+; CHECK-LABEL: define nofpclass(nan) float @infer_return_from_load_nofpclass_md_f32_0
+; CHECK-SAME: (ptr nofree noundef nonnull readonly align 4 captures(none) dereferenceable(4) [[PTR:%.*]]) #[[ATTR5]] {
+; CHECK-NEXT: [[LOAD:%.*]] = load float, ptr [[PTR]], align 4, !nofpclass [[META1:![0-9]+]]
+; CHECK-NEXT: ret float [[LOAD]]
+;
+ %load = load float, ptr %ptr, !nofpclass !1
+ ret float %load
+}
+
+define float @infer_return_from_load_nofpclass_md_f32_1(ptr %ptr) {
+; CHECK: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read)
+; CHECK-LABEL: define nofpclass(pinf) float @infer_return_from_load_nofpclass_md_f32_1
+; CHECK-SAME: (ptr nofree noundef nonnull readonly align 4 captures(none) dereferenceable(4) [[PTR:%.*]]) #[[ATTR5]] {
+; CHECK-NEXT: [[LOAD:%.*]] = load float, ptr [[PTR]], align 4, !nofpclass [[META2:![0-9]+]]
+; CHECK-NEXT: ret float [[LOAD]]
+;
+ %load = load float, ptr %ptr, !nofpclass !2
+ ret float %load
+}
+
+define <2 x float> @infer_return_from_load_nofpclass_md_v2f32(ptr %ptr) {
+; CHECK: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read)
+; CHECK-LABEL: define nofpclass(nan) <2 x float> @infer_return_from_load_nofpclass_md_v2f32
+; CHECK-SAME: (ptr nofree noundef nonnull readonly align 8 captures(none) dereferenceable(8) [[PTR:%.*]]) #[[ATTR5]] {
+; CHECK-NEXT: [[LOAD:%.*]] = load <2 x float>, ptr [[PTR]], align 8, !nofpclass [[META1]]
+; CHECK-NEXT: ret <2 x float> [[LOAD]]
+;
+ %load = load <2 x float>, ptr %ptr, !nofpclass !1
+ ret <2 x float> %load
+}
+
+; TODO: Misses this case
+define { float, float } @infer_return_from_load_nofpclass_md_struct(ptr %ptr) {
+; CHECK: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read)
+; CHECK-LABEL: define { float, float } @infer_return_from_load_nofpclass_md_struct
+; CHECK-SAME: (ptr nofree noundef nonnull readonly align 4 captures(none) dereferenceable(8) [[PTR:%.*]]) #[[ATTR5]] {
+; CHECK-NEXT: [[LOAD:%.*]] = load { float, float }, ptr [[PTR]], align 4, !nofpclass [[META1]]
+; CHECK-NEXT: ret { float, float } [[LOAD]]
+;
+ %load = load { float, float }, ptr %ptr, !nofpclass !1
+ ret { float, float } %load
+}
+
+define [4 x float] @infer_return_from_load_nofpclass_md_array(ptr %ptr) {
+; CHECK: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read)
+; CHECK-LABEL: define nofpclass(nan) [4 x float] @infer_return_from_load_nofpclass_md_array
+; CHECK-SAME: (ptr nofree noundef nonnull readonly align 4 captures(none) dereferenceable(16) [[PTR:%.*]]) #[[ATTR5]] {
+; CHECK-NEXT: [[LOAD:%.*]] = load [4 x float], ptr [[PTR]], align 4, !nofpclass [[META1]]
+; CHECK-NEXT: ret [4 x float] [[LOAD]]
+;
+ %load = load [4 x float], ptr %ptr, !nofpclass !1
+ ret [4 x float] %load
+}
+
declare i64 @_Z13get_global_idj(i32 noundef)
attributes #0 = { "denormal-fp-math"="preserve-sign,preserve-sign" }
@@ -3748,6 +3804,8 @@ attributes #8 = { "denormal-fp-math"="dynamic,ieee" }
attributes #9 = { "denormal-fp-math"="ieee,dynamic" }
!0 = !{}
+!1 = !{i32 3}
+!2 = !{i32 512}
;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
; CGSCC-CI: {{.*}}
diff --git a/llvm/test/Verifier/nofpclass-metadata.ll b/llvm/test/Verifier/nofpclass-metadata.ll
new file mode 100644
index 0000000000000..f4ef482ff31bc
--- /dev/null
+++ b/llvm/test/Verifier/nofpclass-metadata.ll
@@ -0,0 +1,101 @@
+; RUN: not llvm-as -disable-output %s 2>&1 | FileCheck %s
+
+; CHECK: nofpclass must have exactly one entry
+; CHECK-NEXT: !0 = !{}
+define float @md_missing_value(ptr %ptr) {
+ %load = load float, ptr %ptr, align 4, !nofpclass !0
+ ret float %load
+}
+
+; CHECK: nofpclass must have exactly one entry
+; CHECK-NEXT: !1 = !{i32 1, i32 2}
+define float @md_too_many_entries(ptr %ptr) {
+ %load = load float, ptr %ptr, align 4, !nofpclass !1
+ ret float %load
+}
+
+; CHECK: nofpclass entry must be a constant i32
+; CHECK: !2 = !{i64 1}
+define float @md_is_i64(ptr %ptr) {
+ %load = load float, ptr %ptr, align 4, !nofpclass !2
+ ret float %load
+}
+
+; CHECK: nofpclass entry must be a constant i32
+; CHECK-NEXT: !3 = !{float 1.000000e+00}
+define float @md_is_float(ptr %ptr) {
+ %load = load float, ptr %ptr, align 4, !nofpclass !3
+ ret float %load
+}
+
+; CHECK: nofpclass entry must be a constant i32
+; CHECK-NEXT: !4 = !{!"foo"}
+define float @md_is_string(ptr %ptr) {
+ %load = load float, ptr %ptr, align 4, !nofpclass !4
+ ret float %load
+}
+
+; CHECK: nofpclass entry must be a constant i32
+; CHECK-NEXT: !5 = !{ptr @md_is_ptr}
+define float @md_is_ptr(ptr %ptr) {
+ %load = load float, ptr %ptr, align 4, !nofpclass !5
+ ret float %load
+}
+
+; CHECK: 'nofpclass' must have at least one test bit set
+; CHECK-NEXT: !6 = !{i32 0}
+; CHECK-NEXT: %load = load float, ptr %ptr, align 4, !nofpclass !6
+ define float @md_is_zero_val(ptr %ptr) {
+ %load = load float, ptr %ptr, align 4, !nofpclass !6
+ ret float %load
+}
+
+; CHECK: Invalid value for 'nofpclass' test mask
+; CHECK-NEXT: !7 = !{i32 1024}
+; CHECK-NEXT: %load = load float, ptr %ptr, align 4, !nofpclass !7
+define float @md_is_out_of_bounds(ptr %ptr) {
+ %load = load float, ptr %ptr, align 4, !nofpclass !7
+ ret float %load
+}
+
+declare float @func()
+
+; CHECK: nofpclass is only for loads
+; CHECK-NEXT: %result = call float @func(), !nofpclass !8
+define float @not_load(ptr %ptr) {
+ %result = call float @func(), !nofpclass !8
+ ret float %result
+}
+
+; CHECK: nofpclass only applies to floating-point typed loads
+; CHECK-NEXT: %load = load i32, ptr %ptr, align 4, !nofpclass !8
+define i32 @load_int(ptr %ptr) {
+ %load = load i32, ptr %ptr, align 4, !nofpclass !8
+ ret i32 %load
+}
+
+; CHECK: nofpclass only applies to floating-point typed loads
+; CHECK-NEXT: %load = load <2 x i32>, ptr %ptr, align 8, !nofpclass !8
+define <2 x i32> @load_int_vec(ptr %ptr) {
+ %load = load <2 x i32>, ptr %ptr, align 8, !nofpclass !8
+ ret <2 x i32> %load
+}
+
+%struct = type { i32, float }
+
+; CHECK: nofpclass only applies to floating-point typed loads
+; CHECK-NEXT: %load = load %struct, ptr %ptr, align 4, !nofpclass !8
+define %struct @load_hetero_struct(ptr %ptr) {
+ %load = load %struct, ptr %ptr, align 4, !nofpclass !8
+ ret %struct %load
+}
+
+!0 = !{}
+!1 = !{i32 1, i32 2}
+!2 = !{i64 1}
+!3 = !{float 1.000000e+00}
+!4 = !{!"foo"}
+!5 = !{ptr @md_is_ptr}
+!6 = !{i32 0}
+!7 = !{i32 1024}
+!8 = !{i32 3}
``````````
</details>
https://github.com/llvm/llvm-project/pull/177140
More information about the llvm-commits
mailing list