[Clang] Add __builtin_launder
Needs ReviewPublic

Authored by EricWF on Nov 18 2017, 3:03 PM.

Details

Summary

This patch adds __builtin_launder, which is required to implement std::launder. Additionally GCC provides __builtin_launder, so thing brings Clang in-line with GCC.

I'm not exactly sure what magic __builtin_launder requires, but based on previous discussions this patch applies a @llvm.invariant.group.barrier. As noted in previous discussions, this may not be enough to correctly handle vtables.

Diff Detail

EricWF created this revision.Nov 18 2017, 3:03 PM
EricWF updated this revision to Diff 123484.Nov 18 2017, 3:05 PM
  • Remove incorrect FIXME comment.

A test with restrict and __restrict might be interesting.

jroelofs added inline comments.
include/clang/Basic/Builtins.def
486

GCC's is type-generic:

#include <type_traits>

bool is_type_generic() {
  int v;
  return std::is_same<decltype(__builtin_launder(&v)),
                      decltype(&v)>::value;
}

However this BUILTIN line says that this one is not (yeah, I see it's being fixed up in Sema later in this patch). I think you need to use custom type checking here via t.

EricWF added inline comments.Nov 19 2017, 2:12 PM
include/clang/Basic/Builtins.def
486

The t is specified. It's in the third argument.

jroelofs added inline comments.Nov 19 2017, 2:13 PM
include/clang/Basic/Builtins.def
486

ohhh. duh. sorry.

EricWF updated this revision to Diff 123516.Nov 19 2017, 2:23 PM
  • Fix argument initialization.
  • Make constexpr.
EricWF updated this revision to Diff 123627.Nov 20 2017, 11:20 AM
  • Improve quality of tests.
  • Format code.
rsmith added inline comments.Dec 7 2017, 3:02 PM
lib/CodeGen/CGBuiltin.cpp
1674

It would be nice to avoid this for types that contain no const subobjects / reference subobjects / vptrs. I think we can also omit this entirely if -fstrict-vtable-ptrs is disabled, since in that case we don't generate any invariant.group metadata.

I'd be OK with the former being left to a future change, but the latter should be part of this change so we don't generate unnecessarily-inefficient code in the default mode for uses of std::launder.

lib/Sema/SemaChecking.cpp
860–864

Please also check that the pointee type is an object type -- per [ptr.launder]p3, "the program is ill-formed if T is a function type or cv void", and we don't want our builtin to need to deal with such cases.

EricWF marked 2 inline comments as done.Fri, Jan 12, 5:23 PM
EricWF added inline comments.
lib/CodeGen/CGBuiltin.cpp
1674

I'll put it in this change set.

EricWF updated this revision to Diff 129740.Fri, Jan 12, 5:25 PM
EricWF marked an inline comment as done.
  • Address inline comments about missing diagnostics for void pointers and function pointers.
  • Address inline comments about only enabling when -fstrict-vtable-pointers is specified, and only on types with vtables.
EricWF updated this revision to Diff 129742.Fri, Jan 12, 5:49 PM
  • Improve diagnostic handling.