Non-throwing allocators currently will always get null-check code. However, if the non-throwing allocator is explicitly annotated with returns_nonnull the null check should be elided.
Testing:
ninja check-all
added test case correctly elides
Differential D102820
[Clang] Check for returns_nonnull when deciding to add allocation null checks modimo on May 19 2021, 6:26 PM. Authored by
Details Non-throwing allocators currently will always get null-check code. However, if the non-throwing allocator is explicitly annotated with returns_nonnull the null check should be elided. Testing:
Diff Detail
Event TimelineComment Actions Go back to single statement return, fix up ATTR_BUILTIN_NOTHROW_NEW test label that pointed to nothing to correct ATTR_NOBUILTIN_NOUNWIND_ALLOCSIZE label. Comment Actions Discussing with @urnathan this makes more sense for the BE to handle when optimizing by eliding the generated null check. Confirmed this is indeed the case so removing the generation in the FE isn't really needed. Comment Actions Sounds reasonable to me! Can you double check whether this attribute gets correctly serialized/deserialized in face of CXXNewExpr? An example of how to test that would be in clang/test/PCH/cxx-method.cpp. Comment Actions Piggybacking on that test case: Inputs/cxx-method.h: typedef __typeof__(sizeof(0)) size_t; void *operator new(size_t size) { return ::operator new(size); } cxx-method.cpp: int * foo() { return new int; } Testing ~/llvm-project/clang/test/PCH# ~/llvm-project/build-rel/bin/clang++ -cc1 -x c++ -emit-pch Inputs/cxx-method.h -o test.pch ~/llvm-project/clang/test/PCH# ~/llvm-project/build-rel/bin/clang++ -cc1 -x c++ cxx-method.cpp -include-pch test.pch -emit-llvm ~/llvm-project/clang/test/PCH# grep _Znwm cxx-method.ll define dso_local nonnull i8* @_Znwm(i64 %size) #0 { %call = call noalias nonnull i8* @_Znwm(i64 %0) #2 %call = call noalias nonnull i8* @_Znwm(i64 4) #3 Assuming I'm answering the correct question that the returns_nonnull is preserved through a PCH, the answer is yes. Comment Actions @rsmith @lebedev.ri thoughts on adding this directly to FE generation? As mentioned this isn't strictly needed and the BE can elide the check but we can also not emit it to save on AST/IR processing cost. Comment Actions
Thanks!
It's a good way for clang based tooling to get the expected result if relying on shouldNullCheckAllocation for an accurate answer. Maybe wait a few more days to see if anyone else has any comment, otherwise LGTM. |