gchatelet updated this revision to Diff 224286. gchatelet added a comment. - Fix missing rename
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D68028/new/ https://reviews.llvm.org/D68028 Files: clang/include/clang/Basic/Attr.td clang/include/clang/Basic/AttrDocs.td clang/include/clang/Basic/DiagnosticSemaKinds.td clang/include/clang/Sema/Sema.h clang/lib/CodeGen/CGCall.cpp clang/lib/Sema/SemaDecl.cpp clang/lib/Sema/SemaDeclAttr.cpp clang/test/CodeGen/no-builtin.c clang/test/Misc/pragma-attribute-supported-attributes-list.test clang/test/Sema/no-builtin.c
Index: clang/test/Sema/no-builtin.c =================================================================== --- /dev/null +++ clang/test/Sema/no-builtin.c @@ -0,0 +1,26 @@ +// RUN: %clang_cc1 -triple x86_64-unknown-unknown -S -emit-llvm -o - -fsyntax-only -verify %s + +void valid_attribute_wildcard() __attribute__((no_builtin("*"))) {} +void valid_attribute_function() __attribute__((no_builtin("memcpy"))) {} +void valid_attribute_functions() __attribute__((no_builtin("memcpy"))) __attribute__((no_builtin("memcmp"))) {} + +void no_builtin_no_argument() __attribute__((no_builtin)) {} +// expected-error@-1 {{'no_builtin' attribute takes at least 1 argument}} + +void no_builtin_no_argument2() __attribute__((no_builtin())) {} +// expected-error@-1 {{'no_builtin' attribute takes at least 1 argument}} + +void invalid_builtin() __attribute__((no_builtin("not_a_builtin"))) {} +// expected-error@-1 {{'not_a_builtin' is not a valid builtin name for no_builtin}} + +void wildcard_and_functionname() __attribute__((no_builtin("*", "memcpy"))) {} +// expected-error@-1 {{no_builtin wildcard (*) cannot be composed with other builtin names}} + +void wildcard_and_functionname2() __attribute__((no_builtin("*"))) __attribute__((no_builtin("memcpy"))) {} +// expected-error@-1 {{no_builtin wildcard (*) cannot be composed with other builtin names}} + +void nobuiltin_on_declaration() __attribute__((no_builtin("memcpy"))); +// expected-error@-1 {{no_builtin attribute is permitted on definitions only}} + +int __attribute__((no_builtin("*"))) variable; +// expected-warning@-1 {{'no_builtin' attribute only applies to functions}} Index: clang/test/Misc/pragma-attribute-supported-attributes-list.test =================================================================== --- clang/test/Misc/pragma-attribute-supported-attributes-list.test +++ 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) Index: clang/test/CodeGen/no-builtin.c =================================================================== --- /dev/null +++ clang/test/CodeGen/no-builtin.c @@ -0,0 +1,23 @@ +// 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_mempcy_twice() #0 +void foo_no_mempcy_twice() __attribute__((no_builtin("memcpy"))) __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 @separate_attrs_ordering() #2 +void separate_attrs_ordering() __attribute__((no_builtin("memcpy"))) __attribute__((no_builtin("memset"))) {} + +// CHECK: attributes #0 = {{{.*}}"no-builtin-memcpy"{{.*}}} +// CHECK: attributes #1 = {{{.*}}"no-builtins"{{.*}}} +// CHECK: attributes #2 = {{{.*}}"no-builtin-memcpy"{{.*}}"no-builtin-memset"{{.*}}} Index: clang/lib/Sema/SemaDeclAttr.cpp =================================================================== --- clang/lib/Sema/SemaDeclAttr.cpp +++ clang/lib/Sema/SemaDeclAttr.cpp @@ -1068,6 +1068,60 @@ S.Context, AL, Cond, Msg, DiagType, ArgDependent, cast<NamedDecl>(D))); } +NoBuiltinAttr * +Sema::mergeNoBuiltinAttr(Decl *D, const AttributeCommonInfo &CI, + llvm::ArrayRef<StringRef> BuiltinNames) { + llvm::SmallVector<StringRef, 16> Names; + bool HasWildcard = false; + + const auto TestAndPushBack = [&Names, &HasWildcard](StringRef Name) { + HasWildcard |= (Name == "*"); + Names.push_back(Name); + }; + + if (const auto *NBA = D->getAttr<NoBuiltinAttr>()) + for (StringRef BuiltinName : NBA->builtinNames()) + TestAndPushBack(BuiltinName); + + for (StringRef BuiltinName : BuiltinNames) + TestAndPushBack(BuiltinName); + + if (HasWildcard && Names.size() > 1) + Diag(D->getLocation(), + diag::err_attribute_no_builtin_wildcard_or_builtin_name) + << CI.getAttrName()->getName(); + + llvm::sort(Names); + Names.erase(std::unique(Names.begin(), Names.end()), Names.end()); + + if (D->hasAttr<NoBuiltinAttr>()) + D->dropAttr<NoBuiltinAttr>(); + + return ::new (Context) NoBuiltinAttr(Context, CI, Names.data(), Names.size()); +} + +static void handleNoBuiltin(Sema &S, Decl *D, const ParsedAttr &AL) { + if (!checkAttributeAtLeastNumArgs(S, AL, 1)) + return; + + llvm::SmallVector<StringRef, 8> BuiltinNames; + for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) { + StringRef BuiltinName; + SourceLocation LiteralLoc; + if (!S.checkStringLiteralArgumentAttr(AL, I, BuiltinName, &LiteralLoc)) + return; + + bool IsValidBuiltin = Builtin::Context::isBuiltinFunc(BuiltinName.data()); + bool IsValidBuiltinName = BuiltinName == "*" || IsValidBuiltin; + if (!IsValidBuiltinName) + S.Diag(LiteralLoc, diag::err_attribute_no_builtin_invalid_builtin_name) + << BuiltinName << AL.getAttrName()->getName(); + + BuiltinNames.push_back(BuiltinName); + } + D->addAttr(S.mergeNoBuiltinAttr(D, AL, BuiltinNames)); +} + static void handlePassObjectSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) { if (D->hasAttr<PassObjectSizeAttr>()) { S.Diag(D->getBeginLoc(), diag::err_attribute_only_once_per_parameter) << AL; @@ -6578,6 +6632,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; Index: clang/lib/Sema/SemaDecl.cpp =================================================================== --- clang/lib/Sema/SemaDecl.cpp +++ clang/lib/Sema/SemaDecl.cpp @@ -2531,6 +2531,10 @@ NewAttr = S.mergeNoSpeculativeLoadHardeningAttr(D, *SLHA); else if (Attr->shouldInheritEvenIfAlreadyPresent() || !DeclHasAttr(D, Attr)) NewAttr = cast<InheritableAttr>(Attr->clone(S.Context)); + else if (const auto *NBA = dyn_cast<NoBuiltinAttr>(Attr)) + NewAttr = S.mergeNoBuiltinAttr( + D, *NBA, + llvm::makeArrayRef(NBA->builtinNames_begin(), NBA->builtinNames_end())); if (NewAttr) { NewAttr->setInherited(true); @@ -9499,6 +9503,16 @@ } } + // Diagnose no_builtin attribute on function declaration that are not a + // definition. + // FIXME: We should really be doing this in SemaDeclAttr.cpp::handleNoBuiltin + // but there is a bug with FunctionDecl::isThisDeclarationADefinition() which + // always returns false before Sema::ActOnStartOfFunctionDef is called. + if (!D.isFunctionDefinition()) + if (const auto *NBA = NewFD->getAttr<NoBuiltinAttr>()) + Diag(NBA->getLocation(), diag::err_attribute_no_builtin_on_non_definition) + << NBA->getSpelling(); + return NewFD; } Index: clang/lib/CodeGen/CGCall.cpp =================================================================== --- clang/lib/CodeGen/CGCall.cpp +++ clang/lib/CodeGen/CGCall.cpp @@ -1849,6 +1849,18 @@ FuncAttrs.addAttribute(llvm::Attribute::NoDuplicate); if (TargetDecl->hasAttr<ConvergentAttr>()) FuncAttrs.addAttribute(llvm::Attribute::Convergent); + if (const auto *Attr = TargetDecl->getAttr<NoBuiltinAttr>()) { + bool HasWildcard = llvm::is_contained(Attr->builtinNames(), "*"); + if (HasWildcard) + FuncAttrs.addAttribute("no-builtins"); + else + for (StringRef BuiltinName : Attr->builtinNames()) { + SmallString<32> AttributeName; + AttributeName += "no-builtin-"; + AttributeName += BuiltinName; + FuncAttrs.addAttribute(AttributeName); + } + } if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) { AddAttributesFromFunctionProtoType( Index: clang/include/clang/Sema/Sema.h =================================================================== --- clang/include/clang/Sema/Sema.h +++ clang/include/clang/Sema/Sema.h @@ -2677,6 +2677,8 @@ MSInheritanceAttr * mergeMSInheritanceAttr(Decl *D, const AttributeCommonInfo &CI, bool BestCase, MSInheritanceAttr::Spelling SemanticSpelling); + NoBuiltinAttr *mergeNoBuiltinAttr(Decl *D, const AttributeCommonInfo &CI, + llvm::ArrayRef<StringRef> BuiltinNames); FormatAttr *mergeFormatAttr(Decl *D, const AttributeCommonInfo &CI, IdentifierInfo *Format, int FormatIdx, int FirstArg); Index: clang/include/clang/Basic/DiagnosticSemaKinds.td =================================================================== --- clang/include/clang/Basic/DiagnosticSemaKinds.td +++ clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -3597,6 +3597,12 @@ def err_attribute_overloadable_multiple_unmarked_overloads : Error< "at most one overload for a given name may lack the 'overloadable' " "attribute">; +def err_attribute_no_builtin_invalid_builtin_name : Error< + "'%0' is not a valid builtin name for %1">; +def err_attribute_no_builtin_wildcard_or_builtin_name : Error< + "%0 wildcard (*) cannot be composed with other builtin names">; +def err_attribute_no_builtin_on_non_definition : Error< + "%0 attribute is permitted on definitions only">; def warn_ns_attribute_wrong_return_type : Warning< "%0 attribute only applies to %select{functions|methods|properties}1 that " "return %select{an Objective-C object|a pointer|a non-retainable pointer}2">, Index: clang/include/clang/Basic/AttrDocs.td =================================================================== --- clang/include/clang/Basic/AttrDocs.td +++ clang/include/clang/Basic/AttrDocs.td @@ -4391,3 +4391,36 @@ }]; } + +def NoBuiltinDocs : Documentation { + let Category = DocCatFunction; + let Content = [{ +.. Note:: This attribute is not yet fully implemented, it is validated but has +no effect on the generated code. + +The ``__attribute__((no_builtin))`` is similar to the ``-fno-builtin`` flag +except it is specific to the body of a function. + +It accepts one or more strings corresponding to the name of the builtin +(e.g. "memcpy", "memset") or "*" which disables all builtins at once. + +.. code-block:: c++ + + // The compiler is not allowed to add any builtin to foo's body. + 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 add the `memcpy` builtin to bar's body. + 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; + } + }]; +} Index: clang/include/clang/Basic/Attr.td =================================================================== --- clang/include/clang/Basic/Attr.td +++ clang/include/clang/Basic/Attr.td @@ -3412,3 +3412,10 @@ let Subjects = SubjectList<[NonParmVar, Function, Block, ObjCMethod]>; let Documentation = [ObjCExternallyRetainedDocs]; } + +def NoBuiltin : InheritableAttr { + let Spellings = [Clang<"no_builtin">]; + let Args = [VariadicStringArgument<"BuiltinNames">]; + let Subjects = SubjectList<[Function]>; + let Documentation = [NoBuiltinDocs]; +}
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits