diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -3343,3 +3343,10 @@ let Subjects = SubjectList<[NonParmVar, Function, Block, ObjCMethod]>; let Documentation = [ObjCExternallyRetainedDocs]; } + +def NoBuiltin : InheritableAttr { + let Spellings = [Clang<"no_builtin">]; + let Args = [VariadicStringArgument<"FunctionNames">]; + let Subjects = SubjectList<[Function]>; + let Documentation = [NoBuiltinDocs]; +} diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -4367,3 +4367,33 @@ }]; } + +def NoBuiltinDocs : Documentation { + let Category = DocCatFunction; + let Content = [{ +.. Note:: This attribute is not yet fully implemented, in particular it will + not report an error if the function name is illegal or not supported. + +The ``__attribute__((no_builtin))`` is similar to the ``-fno-builtin`` flag +except it is specific to the body of a function. + +.. code-block:: c++ + + // The compiler is not allowed to replace parts of foo's body with builtins. + void foo(char* data, size_t count) __attribute__((no_builtin())) { + // The compiler is not allowed to convert the loop into + // `__builtin_memset(data, 0xFE, count);`. + for (size_t i = 0; i < count; ++i) + data[i] = 0xFE; + } + + // The compiler is not allowed to replace parts of bar's body with builtins. + void bar(char* data, size_t count) __attribute__((no_builtin("memcpy"))) { + // The compiler is allowed to convert the loop into + // `__builtin_memset(data, 0xFE, count);` but cannot generate any + // `__builtin_memcpy` + for (size_t i = 0; i < count; ++i) + data[i] = 0xFE; + } + }]; +} diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp --- a/clang/lib/CodeGen/CGCall.cpp +++ b/clang/lib/CodeGen/CGCall.cpp @@ -1849,6 +1849,22 @@ FuncAttrs.addAttribute(llvm::Attribute::NoDuplicate); if (TargetDecl->hasAttr()) FuncAttrs.addAttribute(llvm::Attribute::Convergent); + if (const auto *Attr = TargetDecl->getAttr()) { + const auto HasWildcard = llvm::is_contained(Attr->functionNames(), "*"); + assert(!HasWildcard || + Attr->functionNames_size() == 1 && "Wildcard must be on its own"); + if (HasWildcard) + FuncAttrs.addAttribute("no-builtins"); + else + for (const auto &FunctionName : Attr->functionNames()) { + SmallString<32> AttributeName; + // TODO: check that function names are valid for the + // TargetLibraryInfo. + AttributeName += "no-builtin-"; + AttributeName += FunctionName; + FuncAttrs.addAttribute(AttributeName); + } + } if (const FunctionDecl *Fn = dyn_cast(TargetDecl)) { AddAttributesFromFunctionProtoType( diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -1068,6 +1068,42 @@ S.Context, AL, Cond, Msg, DiagType, ArgDependent, cast(D))); } +static void handleNoBuiltin(Sema &S, Decl *D, const ParsedAttr &AL) { + const StringRef Wildcard = "*"; + llvm::SmallSetVector FunctionNames; + + // Insert previous NoBuiltin attributes. + if (D->hasAttr()) + for (StringRef FunctionName : D->getAttr()->functionNames()) + FunctionNames.insert(FunctionName); + + if (AL.getNumArgs() == 0) + FunctionNames.insert(Wildcard); + else + for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) { + StringRef FunctionName; + SourceLocation LiteralLoc; + if (!S.checkStringLiteralArgumentAttr(AL, I, FunctionName, &LiteralLoc)) + return; + FunctionNames.insert(FunctionName); + } + + // Wildcard is a super set of all builtins, we keep only this one. + if (FunctionNames.count(Wildcard) > 0) { + FunctionNames.clear(); + FunctionNames.insert(Wildcard); + } + + auto UniqueFunctionNames = FunctionNames.takeVector(); + llvm::sort(UniqueFunctionNames); + + if (D->hasAttr()) + D->dropAttr(); + + D->addAttr(::new (S.Context) NoBuiltinAttr( + S.Context, AL, UniqueFunctionNames.data(), UniqueFunctionNames.size())); +} + static void handlePassObjectSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { if (D->hasAttr()) { S.Diag(D->getBeginLoc(), diag::err_attribute_only_once_per_parameter) << AL; @@ -6573,6 +6609,9 @@ case ParsedAttr::AT_DiagnoseIf: handleDiagnoseIfAttr(S, D, AL); break; + case ParsedAttr::AT_NoBuiltin: + handleNoBuiltin(S, D, AL); + break; case ParsedAttr::AT_ExtVectorType: handleExtVectorTypeAttr(S, D, AL); break; diff --git a/clang/test/CodeGen/no-builtin.c b/clang/test/CodeGen/no-builtin.c new file mode 100644 --- /dev/null +++ b/clang/test/CodeGen/no-builtin.c @@ -0,0 +1,20 @@ +// RUN: %clang_cc1 -triple x86_64-unknown-unknown -S -emit-llvm -o - %s | FileCheck %s + +// CHECK-LABEL: define void @foo_no_mempcy() #0 +void foo_no_mempcy() __attribute__((no_builtin("memcpy"))) {} + +// CHECK-LABEL: define void @foo_no_builtins() #1 +void foo_no_builtins() __attribute__((no_builtin)) {} + +// CHECK-LABEL: define void @foo_no_mempcy_memset() #2 +void foo_no_mempcy_memset() __attribute__((no_builtin("memset", "memcpy"))) {} + +// CHECK-LABEL: define void @separate_attrs() #2 +void separate_attrs() __attribute__((no_builtin("memset"))) __attribute__((no_builtin("memcpy"))) {} + +// CHECK-LABEL: define void @wildcard_wins() #1 +void wildcard_wins() __attribute__((no_builtin("memset"))) __attribute__((no_builtin)) __attribute__((no_builtin("memcpy"))) {} + +// CHECK: attributes #0 = {{{.*}}"no-builtin-memcpy"{{.*}}} +// CHECK: attributes #1 = {{{.*}}"no-builtins"{{.*}}} +// CHECK: attributes #2 = {{{.*}}"no-builtin-memcpy"{{.*}}"no-builtin-memset"{{.*}}} diff --git a/clang/test/Misc/pragma-attribute-supported-attributes-list.test b/clang/test/Misc/pragma-attribute-supported-attributes-list.test --- a/clang/test/Misc/pragma-attribute-supported-attributes-list.test +++ b/clang/test/Misc/pragma-attribute-supported-attributes-list.test @@ -74,6 +74,7 @@ // CHECK-NEXT: NSConsumed (SubjectMatchRule_variable_is_parameter) // CHECK-NEXT: NSConsumesSelf (SubjectMatchRule_objc_method) // CHECK-NEXT: Naked (SubjectMatchRule_function) +// CHECK-NEXT: NoBuiltin (SubjectMatchRule_function) // CHECK-NEXT: NoCommon (SubjectMatchRule_variable) // CHECK-NEXT: NoDebug (SubjectMatchRule_type_alias, SubjectMatchRule_hasType_functionType, SubjectMatchRule_objc_method, SubjectMatchRule_variable_not_is_parameter) // CHECK-NEXT: NoDestroy (SubjectMatchRule_variable)