https://github.com/pvelesko updated https://github.com/llvm/llvm-project/pull/218642
>From e4b628f940f99ba78aeecc77ae4b9bf7a83aafa7 Mon Sep 17 00:00:00 2001 From: Paulius Velesko <[email protected]> Date: Fri, 20 Nov 2026 13:50:03 +0200 Subject: [PATCH] [clang][Sema] Let builtins opt out of the address space rewrite rewriteBuiltinFunctionDecl retypes unqualified pointer parameters of a builtin to the call argument's address space. That is required by OpenCL, where no pointer is in the default address space, so it stays the default. Some builtins cannot take a non-default address space, because codegen passes the pointer to an intrinsic or library call with a fixed signature. A builtin had no way to opt out, since "*0" names address_space(0), not the default address space. __builtin___clear_cache and the ARM/AArch64 __clear_cache called with address_space(1) pointers reached llvm.clear_cache with mismatched pointer types and asserted in CallInst::init. Add the NoAddressSpaceRewrite attribute ('k') and set it on those builtins. Their arguments now go through the normal argument conversion: a pointer in another address space gets the "changes address space of pointer" error, unless the language converts it implicitly to the default address space, as HIP does. --- clang/include/clang/Basic/Builtins.def | 1 + clang/include/clang/Basic/Builtins.h | 6 ++++++ clang/include/clang/Basic/Builtins.td | 2 +- clang/include/clang/Basic/BuiltinsAArch64.td | 2 +- clang/include/clang/Basic/BuiltinsARM.td | 2 +- clang/include/clang/Basic/BuiltinsBase.td | 4 ++++ clang/lib/Sema/SemaExpr.cpp | 5 +++-- .../Sema/builtin-clear-cache-address-space.c | 21 +++++++++++++++++++ 8 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 clang/test/Sema/builtin-clear-cache-address-space.c diff --git a/clang/include/clang/Basic/Builtins.def b/clang/include/clang/Basic/Builtins.def index dca024ebd2804a..d14b01d6b16f0f 100644 --- a/clang/include/clang/Basic/Builtins.def +++ b/clang/include/clang/Basic/Builtins.def @@ -101,6 +101,7 @@ // g -> const when FP exceptions are ignored // j -> returns_twice (like setjmp) // u -> arguments are not evaluated for their side-effects +// k -> pointer parameters are not rewritten to the argument's address space // V:{num}: -> requires vectors of at least {num} bits to be legal // C<{num},M_0,...,M_k> -> callback behavior: argument {num} is called with // argument M_0, ..., M_k as payload diff --git a/clang/include/clang/Basic/Builtins.h b/clang/include/clang/Basic/Builtins.h index 0420b5547a5682..26a99c7e418d91 100644 --- a/clang/include/clang/Basic/Builtins.h +++ b/clang/include/clang/Basic/Builtins.h @@ -373,6 +373,12 @@ class Context { return strchr(getTypeString(ID), '*') != nullptr; } + /// Determines whether Sema must not retype this builtin's pointer + /// parameters to the address space of the call arguments. + bool hasNoAddressSpaceRewrite(unsigned ID) const { + return strchr(getAttributesString(ID), 'k') != nullptr; + } + /// Return true if this builtin has a result or any arguments which are /// reference types. bool hasReferenceArgsOrResult(unsigned ID) const { diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td index c9318d965317be..b8b54ca5a5065b 100644 --- a/clang/include/clang/Basic/Builtins.td +++ b/clang/include/clang/Basic/Builtins.td @@ -1201,7 +1201,7 @@ def StackAddress : Builtin { def ClearCache : Builtin { let Spellings = ["__builtin___clear_cache"]; - let Attributes = [NoThrow]; + let Attributes = [NoThrow, NoAddressSpaceRewrite]; let Prototype = "void(void*, void*)"; } diff --git a/clang/include/clang/Basic/BuiltinsAArch64.td b/clang/include/clang/Basic/BuiltinsAArch64.td index 84ad46119682a6..3ab238fe55a3e6 100644 --- a/clang/include/clang/Basic/BuiltinsAArch64.td +++ b/clang/include/clang/Basic/BuiltinsAArch64.td @@ -16,7 +16,7 @@ include "clang/Basic/BuiltinsAArch64Base.td" // In libgcc -let Attributes = [RuntimeLibraryFunction] in { +let Attributes = [RuntimeLibraryFunction, NoAddressSpaceRewrite] in { def __clear_cache : AArch64NoPrefixBuiltin<"void (void *, void *)">; } diff --git a/clang/include/clang/Basic/BuiltinsARM.td b/clang/include/clang/Basic/BuiltinsARM.td index 95ec8b18a8c8e3..79f236a21bd41a 100644 --- a/clang/include/clang/Basic/BuiltinsARM.td +++ b/clang/include/clang/Basic/BuiltinsARM.td @@ -15,7 +15,7 @@ include "clang/Basic/BuiltinsARMBase.td" // In libgcc -let Attributes = [RuntimeLibraryFunction] in { +let Attributes = [RuntimeLibraryFunction, NoAddressSpaceRewrite] in { def __clear_cache : ARMNoPrefixBuiltin<"void (void *, void *)">; } diff --git a/clang/include/clang/Basic/BuiltinsBase.td b/clang/include/clang/Basic/BuiltinsBase.td index 86ead09e99aba1..a208bbad19df78 100644 --- a/clang/include/clang/Basic/BuiltinsBase.td +++ b/clang/include/clang/Basic/BuiltinsBase.td @@ -79,6 +79,10 @@ def RequireDeclaration : Attribute<"h">; // '__builtin_' prefix. It will be implemented in compiler-rt or libgcc. def RuntimeLibraryFunction : Attribute<"i">; +// Do not retype unqualified pointer parameters to the call argument's address +// space; the builtin only accepts the default address space. +def NoAddressSpaceRewrite : Attribute<"k">; + // FIXME: Why is this not simply the min_vector_width attribute? // Vector has to be at least N bits wide. class RequiredVectorWidth<int N> : IndexedAttribute<"V", N>; diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 571ef6ea6b19eb..474a257568d5ae 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -6795,8 +6795,9 @@ static FunctionDecl *rewriteBuiltinFunctionDecl(Sema *Sema, ASTContext &Context, QualType DeclType = FDecl->getType(); const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(DeclType); - if (!Context.BuiltinInfo.hasPtrArgsOrResult(FDecl->getBuiltinID()) || !FT || - ArgExprs.size() < FT->getNumParams()) + if (!Context.BuiltinInfo.hasPtrArgsOrResult(FDecl->getBuiltinID()) || + Context.BuiltinInfo.hasNoAddressSpaceRewrite(FDecl->getBuiltinID()) || + !FT || ArgExprs.size() < FT->getNumParams()) return nullptr; bool NeedsNewDecl = false; diff --git a/clang/test/Sema/builtin-clear-cache-address-space.c b/clang/test/Sema/builtin-clear-cache-address-space.c new file mode 100644 index 00000000000000..da8f3041e18f3f --- /dev/null +++ b/clang/test/Sema/builtin-clear-cache-address-space.c @@ -0,0 +1,21 @@ +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fsyntax-only -verify %s +// RUN: %clang_cc1 -triple thumbv7-eabi -fsyntax-only -verify -DARM %s +// RUN: %clang_cc1 -triple arm64-unknown-linux -fsyntax-only -verify -DARM %s + +#define AS1 __attribute__((address_space(1))) + +void default_as(char *a, char *b) { +#ifdef ARM + __clear_cache(a, b); +#else + __builtin___clear_cache(a, b); +#endif +} + +void non_default_as(AS1 char *a, AS1 char *b) { +#ifdef ARM + __clear_cache(a, b); // expected-error {{changes address space of pointer}} +#else + __builtin___clear_cache(a, b); // expected-error {{changes address space of pointer}} +#endif +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
