yonghong-song created this revision. yonghong-song added a reviewer: ast. Herald added subscribers: cfe-commits, arphaman. Herald added a project: clang.
The clang intrinsic __builtin_preserve_access_index() currently has signature: const void * __builtin_preserve_access_index(const void * ptr) This may cause compiler warning when: - parameter type is "volatile void *" or "const volatile void *", or - the assign-to type of the intrinsic does not have "const" qualifier. These warnings can be suppressed by type casting in the user code. But let's design the __builtin_preserve_access_index() to be more user friendly to avoid user casting. The new signature looks like: void * __builtin_preserve_access_index(const volatile void * ptr) A type casting is generated to "void *" at the end of builtin codegen. This type casting is typically combined with other subsequent user casts or optimized away completely and has little impact on optimized IR. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D67734 Files: clang/docs/LanguageExtensions.rst clang/include/clang/Basic/Builtins.def clang/lib/CodeGen/CGBuiltin.cpp clang/lib/Sema/SemaChecking.cpp clang/test/Sema/builtin-preserve-access-index.c Index: clang/test/Sema/builtin-preserve-access-index.c =================================================================== --- clang/test/Sema/builtin-preserve-access-index.c +++ clang/test/Sema/builtin-preserve-access-index.c @@ -4,10 +4,10 @@ return __builtin_preserve_access_index(&arg[1], 1); // expected-error {{too many arguments to function call, expected 1, have 2}} } -void *invalid2(const int *arg) { - return __builtin_preserve_access_index(&arg[1]); // expected-warning {{returning 'const void *' from a function with result type 'void *' discards qualifiers}} +void *valid2(const int *arg) { + return __builtin_preserve_access_index(&arg[1]); } const void *invalid3(const int *arg) { - return __builtin_preserve_access_index(1); // expected-warning {{incompatible integer to pointer conversion passing 'int' to parameter of type 'const void *'}} + return __builtin_preserve_access_index(1); // expected-warning {{incompatible integer to pointer conversion passing 'int' to parameter of type 'const volatile void *'}} } Index: clang/lib/Sema/SemaChecking.cpp =================================================================== --- clang/lib/Sema/SemaChecking.cpp +++ clang/lib/Sema/SemaChecking.cpp @@ -197,7 +197,6 @@ if (checkArgCount(S, TheCall, 1)) return true; - TheCall->setType(TheCall->getArg(0)->getType()); return false; } Index: clang/lib/CodeGen/CGBuiltin.cpp =================================================================== --- clang/lib/CodeGen/CGBuiltin.cpp +++ clang/lib/CodeGen/CGBuiltin.cpp @@ -1879,6 +1879,8 @@ IsInPreservedAIRegion = true; Value *Res = EmitScalarExpr(E->getArg(0)); + unsigned AddrSpace = Res->getType()->getPointerAddressSpace(); + Res = Builder.CreateBitCast(Res, Int8Ty->getPointerTo(AddrSpace)); IsInPreservedAIRegion = false; return RValue::get(Res); } Index: clang/include/clang/Basic/Builtins.def =================================================================== --- clang/include/clang/Basic/Builtins.def +++ clang/include/clang/Basic/Builtins.def @@ -1470,7 +1470,7 @@ BUILTIN(__builtin_operator_delete, "vv*", "tn") BUILTIN(__builtin_char_memchr, "c*cC*iz", "n") BUILTIN(__builtin_dump_struct, "ivC*v*", "tn") -BUILTIN(__builtin_preserve_access_index, "vC*vC*", "nU") +BUILTIN(__builtin_preserve_access_index, "v*vCD*", "nU") // Safestack builtins BUILTIN(__builtin___get_unsafe_stack_start, "v*", "Fn") Index: clang/docs/LanguageExtensions.rst =================================================================== --- clang/docs/LanguageExtensions.rst +++ clang/docs/LanguageExtensions.rst @@ -2358,7 +2358,7 @@ .. code-block:: c - const void * __builtin_preserve_access_index(const void * ptr) + void * __builtin_preserve_access_index(const volatile void * ptr) **Example of Use**:
Index: clang/test/Sema/builtin-preserve-access-index.c =================================================================== --- clang/test/Sema/builtin-preserve-access-index.c +++ clang/test/Sema/builtin-preserve-access-index.c @@ -4,10 +4,10 @@ return __builtin_preserve_access_index(&arg[1], 1); // expected-error {{too many arguments to function call, expected 1, have 2}} } -void *invalid2(const int *arg) { - return __builtin_preserve_access_index(&arg[1]); // expected-warning {{returning 'const void *' from a function with result type 'void *' discards qualifiers}} +void *valid2(const int *arg) { + return __builtin_preserve_access_index(&arg[1]); } const void *invalid3(const int *arg) { - return __builtin_preserve_access_index(1); // expected-warning {{incompatible integer to pointer conversion passing 'int' to parameter of type 'const void *'}} + return __builtin_preserve_access_index(1); // expected-warning {{incompatible integer to pointer conversion passing 'int' to parameter of type 'const volatile void *'}} } Index: clang/lib/Sema/SemaChecking.cpp =================================================================== --- clang/lib/Sema/SemaChecking.cpp +++ clang/lib/Sema/SemaChecking.cpp @@ -197,7 +197,6 @@ if (checkArgCount(S, TheCall, 1)) return true; - TheCall->setType(TheCall->getArg(0)->getType()); return false; } Index: clang/lib/CodeGen/CGBuiltin.cpp =================================================================== --- clang/lib/CodeGen/CGBuiltin.cpp +++ clang/lib/CodeGen/CGBuiltin.cpp @@ -1879,6 +1879,8 @@ IsInPreservedAIRegion = true; Value *Res = EmitScalarExpr(E->getArg(0)); + unsigned AddrSpace = Res->getType()->getPointerAddressSpace(); + Res = Builder.CreateBitCast(Res, Int8Ty->getPointerTo(AddrSpace)); IsInPreservedAIRegion = false; return RValue::get(Res); } Index: clang/include/clang/Basic/Builtins.def =================================================================== --- clang/include/clang/Basic/Builtins.def +++ clang/include/clang/Basic/Builtins.def @@ -1470,7 +1470,7 @@ BUILTIN(__builtin_operator_delete, "vv*", "tn") BUILTIN(__builtin_char_memchr, "c*cC*iz", "n") BUILTIN(__builtin_dump_struct, "ivC*v*", "tn") -BUILTIN(__builtin_preserve_access_index, "vC*vC*", "nU") +BUILTIN(__builtin_preserve_access_index, "v*vCD*", "nU") // Safestack builtins BUILTIN(__builtin___get_unsafe_stack_start, "v*", "Fn") Index: clang/docs/LanguageExtensions.rst =================================================================== --- clang/docs/LanguageExtensions.rst +++ clang/docs/LanguageExtensions.rst @@ -2358,7 +2358,7 @@ .. code-block:: c - const void * __builtin_preserve_access_index(const void * ptr) + void * __builtin_preserve_access_index(const volatile void * ptr) **Example of Use**:
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits