Add initial support for NonNull attribute.
(https://github.com/llvm/llvm-project/issues/57113)
Test plan:
verify that for
__thread int x;
int main() {
int* y = &x;
return *y;
}
(with this patch) clang -O -fsanitize=null -S -emit-llvm -o -
doesn't emit a null-pointer check
Differential revision: https://reviews.llvm.org/D131872
// CHECK-NEXT: %[[RET:.+]] = load i32, ptr %[[IA2]], align 4
// CHECK-NEXT: ret i32 %[[RET]]
//
-// CHECK: declare ptr @llvm.threadlocal.address.p0(ptr) #[[ATTR_NUM:.+]]
+// CHECK: declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #[[ATTR_NUM:.+]]
//
// CHECK-O1-LABEL: @_Z1gv
// CHECK-O1-NEXT: entry:
-// CHECK-O1-NEXT: %[[I_ADDR:.+]] = {{.*}}call ptr @llvm.threadlocal.address.p0(ptr nonnull @i)
+// CHECK-O1-NEXT: %[[I_ADDR:.+]] = {{.*}}call ptr @llvm.threadlocal.address.p0(ptr @i)
// CHECK-O1-NEXT: %[[VAL:.+]] = load i32, ptr %[[I_ADDR]]
// CHECK-O1-NEXT: %[[INC:.+]] = add nsw i32 %[[VAL]], 1
// CHECK-O1-NEXT: store i32 %[[INC]], ptr %[[I_ADDR]]
//
// CHECK-O1-LABEL: @_Z1fv
// CHECK-O1-NEXT: entry:
-// CHECK-O1-NEXT: %[[J_ADDR:.+]] = {{.*}}call ptr @llvm.threadlocal.address.p0(ptr nonnull @_ZZ1fvE1j)
+// CHECK-O1-NEXT: %[[J_ADDR:.+]] = {{.*}}call ptr @llvm.threadlocal.address.p0(ptr @_ZZ1fvE1j)
// CHECK-O1-NEXT: %[[VAL:.+]] = load i32, ptr %[[J_ADDR]]
// CHECK-O1-NEXT: %[[INC:.+]] = add nsw i32 %[[VAL]], 1
// CHECK-O1-NEXT: store i32 %[[INC]], ptr %[[J_ADDR]]
int ArgNo = idx.Value;
}
+// NonNull - The specified argument is not null.
+class NonNull<AttrIndex idx> : IntrinsicProperty {
+ int ArgNo = idx.Value;
+}
+
class Align<AttrIndex idx, int align> : IntrinsicProperty {
int ArgNo = idx.Value;
int Align = align;
// Intrinsic to wrap a thread local variable.
def int_threadlocal_address : DefaultAttrsIntrinsic<[llvm_anyptr_ty], [LLVMMatchType<0>],
- [IntrNoMem, IntrSpeculatable, IntrWillReturn]>;
+ [NonNull<RetIndex>, NonNull<ArgIndex<0>>,
+ IntrNoMem, IntrSpeculatable, IntrWillReturn]>;
def int_experimental_stepvector : DefaultAttrsIntrinsic<[llvm_anyvector_ty],
[], [IntrNoMem]>;
}
; Function Attrs: nocallback nofree nosync nounwind readnone speculatable willreturn
- declare ptr @llvm.threadlocal.address.p0(ptr) #0
+ declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #0
attributes #0 = { nocallback nofree nosync nounwind readnone speculatable willreturn }
}
; Function Attrs: nocallback nofree nosync nounwind readnone speculatable willreturn
- declare ptr @llvm.threadlocal.address.p0(ptr) #0
+ declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #0
attributes #0 = { nocallback nofree nosync nounwind readnone speculatable willreturn }
ret i32 %3
}
-declare ptr @llvm.threadlocal.address(ptr) nounwind readnone willreturn
-declare ptr addrspace(1) @llvm.threadlocal.address.p1(ptr addrspace(1)) nounwind readnone willreturn
+declare nonnull ptr @llvm.threadlocal.address(ptr nonnull) nounwind readnone willreturn
+declare nonnull ptr addrspace(1) @llvm.threadlocal.address.p1(ptr addrspace(1) nonnull) nounwind readnone willreturn
NoCapture,
NoAlias,
NoUndef,
+ NonNull,
Returned,
ReadOnly,
WriteOnly,
} else if (R->isSubClassOf("NoUndef")) {
unsigned ArgNo = R->getValueAsInt("ArgNo");
ArgumentAttributes.emplace_back(ArgNo, NoUndef, 0);
+ } else if (R->isSubClassOf("NonNull")) {
+ unsigned ArgNo = R->getValueAsInt("ArgNo");
+ ArgumentAttributes.emplace_back(ArgNo, NonNull, 0);
} else if (R->isSubClassOf("Returned")) {
unsigned ArgNo = R->getValueAsInt("ArgNo");
ArgumentAttributes.emplace_back(ArgNo, Returned, 0);
unsigned numAttrs = 0;
// The argument attributes are alreadys sorted by argument index.
+ assert(is_sorted(Intrinsic.ArgumentAttributes) &&
+ "Argument attributes are not sorted");
+
unsigned Ai = 0, Ae = Intrinsic.ArgumentAttributes.size();
if (Ae) {
while (Ai != Ae) {
unsigned AttrIdx = Intrinsic.ArgumentAttributes[Ai].Index;
-
OS << " const Attribute::AttrKind AttrParam" << AttrIdx << "[]= {";
ListSeparator LS(",");
case CodeGenIntrinsic::NoUndef:
OS << LS << "Attribute::NoUndef";
break;
+ case CodeGenIntrinsic::NonNull:
+ OS << LS << "Attribute::NonNull";
+ break;
case CodeGenIntrinsic::Returned:
OS << LS << "Attribute::Returned";
break;
OS << LSV << V;
OS << "};\n";
}
-
+ // AttributeList::ReturnIndex = 0, AttrParam0 corresponds to return
+ // value.
OS << " AS[" << numAttrs++ << "] = AttributeList::get(C, "
<< AttrIdx << ", AttrParam" << AttrIdx;
if (!AllValuesAreZero)