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 @@ -1448,6 +1448,13 @@ let Documentation = [LayoutVersionDocs]; } +def Leaf : InheritableAttr { + let Spellings = [GCC<"leaf">]; + let Subjects = SubjectList<[Function]>; + let Documentation = [LeafDocs]; + let SimpleHandler = 1; +} + def LifetimeBound : DeclOrTypeAttr { let Spellings = [Clang<"lifetimebound", 0>]; let Subjects = SubjectList<[ParmVar, ImplicitObjectParameter], ErrorDiag>; 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 @@ -3976,6 +3976,22 @@ }]; } +def LeafDocs : Documentation { + let Category = DocCatVariable; + let Content = [{ + +The ``leaf`` attribute is used as a compiler hint to improve dataflow analysis +in library functions. Functions marked with the ``leaf`` attribute are not allowed +to jump back into the caller's translation unit, whether through invoking a +callback function, an external function call, use of ``longjmp``, or other means. +Therefore, they cannot use or modify any data that does not escape the caller function's +compilation unit. + +For more information see +`gcc documentation ` +}]; +} + def NoStackProtectorDocs : Documentation { let Category = DocCatFunction; let Content = [{ 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 @@ -1989,6 +1989,8 @@ FuncAttrs.addAttribute("no_caller_saved_registers"); if (TargetDecl->hasAttr()) FuncAttrs.addAttribute(llvm::Attribute::NoCfCheck); + if (TargetDecl->hasAttr()) + FuncAttrs.addAttribute(llvm::Attribute::NoCallback); HasOptnone = TargetDecl->hasAttr(); if (auto *AllocSize = TargetDecl->getAttr()) { 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 @@ -7715,6 +7715,9 @@ case ParsedAttr::AT_AnyX86NoCfCheck: handleNoCfCheckAttr(S, D, AL); break; + case ParsedAttr::AT_Leaf: + handleSimpleAttribute(S, D, AL); + break; case ParsedAttr::AT_NoThrow: if (!AL.isUsedAsTypeAttr()) handleSimpleAttribute(S, D, AL); diff --git a/clang/test/CodeGen/attr-leaf.c b/clang/test/CodeGen/attr-leaf.c new file mode 100644 --- /dev/null +++ b/clang/test/CodeGen/attr-leaf.c @@ -0,0 +1,10 @@ +// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -Oz -o - %s | FileCheck %s + +// CHECK: define void @f() local_unnamed_addr [[ATTRS:#[0-9]+]] { +void f() __attribute__((leaf)); + +void f() +{ +} + +// CHECK: attributes [[ATTRS]] = { {{.*}}nocallback{{.*}} } 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 @@ -68,6 +68,7 @@ // CHECK-NEXT: InitPriority (SubjectMatchRule_variable) // CHECK-NEXT: InternalLinkage (SubjectMatchRule_variable, SubjectMatchRule_function, SubjectMatchRule_record) // CHECK-NEXT: LTOVisibilityPublic (SubjectMatchRule_record) +// CHECK-NEXT: Leaf (SubjectMatchRule_function) // CHECK-NEXT: LoaderUninitialized (SubjectMatchRule_variable_is_global) // CHECK-NEXT: Lockable (SubjectMatchRule_record) // CHECK-NEXT: MIGServerRoutine (SubjectMatchRule_function, SubjectMatchRule_objc_method, SubjectMatchRule_block) diff --git a/clang/test/Sema/attr-leaf.c b/clang/test/Sema/attr-leaf.c new file mode 100644 --- /dev/null +++ b/clang/test/Sema/attr-leaf.c @@ -0,0 +1,12 @@ +// RUN: %clang_cc1 -verify -fsyntax-only %s + +void f1() __attribute__((leaf)); + +void f2() __attribute__((leaf("abc"))); // expected-error {{'leaf' attribute takes no argument}} + +int var __attribute__ ((leaf())); // expected-warning {{'leaf' attribute only applies to functions}} + +// FIXME: Might diagnose a warning if leaf attribute is used in function definition +// The leaf attribute has no effect on functions defined within the current compilation unit +__attribute__((leaf)) void f3() { +} diff --git a/llvm/include/llvm/Bitcode/LLVMBitCodes.h b/llvm/include/llvm/Bitcode/LLVMBitCodes.h --- a/llvm/include/llvm/Bitcode/LLVMBitCodes.h +++ b/llvm/include/llvm/Bitcode/LLVMBitCodes.h @@ -653,6 +653,7 @@ ATTR_KIND_NOUNDEF = 68, ATTR_KIND_BYREF = 69, ATTR_KIND_MUSTPROGRESS = 70, + ATTR_KIND_NO_CALLBACK = 71, }; enum ComdatSelectionKindCodes { diff --git a/llvm/include/llvm/IR/Attributes.td b/llvm/include/llvm/IR/Attributes.td --- a/llvm/include/llvm/IR/Attributes.td +++ b/llvm/include/llvm/IR/Attributes.td @@ -103,6 +103,9 @@ /// Callee isn't recognized as a builtin. def NoBuiltin : EnumAttr<"nobuiltin">; +/// Function cannot enter into caller's translation unit. +def NoCallback : EnumAttr<"nocallback">; + /// Function creates no aliases of pointer. def NoCapture : EnumAttr<"nocapture">; diff --git a/llvm/lib/AsmParser/LLLexer.cpp b/llvm/lib/AsmParser/LLLexer.cpp --- a/llvm/lib/AsmParser/LLLexer.cpp +++ b/llvm/lib/AsmParser/LLLexer.cpp @@ -653,6 +653,7 @@ KEYWORD(nest); KEYWORD(noalias); KEYWORD(nobuiltin); + KEYWORD(nocallback); KEYWORD(nocapture); KEYWORD(noduplicate); KEYWORD(nofree); diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp --- a/llvm/lib/AsmParser/LLParser.cpp +++ b/llvm/lib/AsmParser/LLParser.cpp @@ -1353,6 +1353,9 @@ break; case lltok::kw_naked: B.addAttribute(Attribute::Naked); break; case lltok::kw_nobuiltin: B.addAttribute(Attribute::NoBuiltin); break; + case lltok::kw_nocallback: + B.addAttribute(Attribute::NoCallback); + break; case lltok::kw_noduplicate: B.addAttribute(Attribute::NoDuplicate); break; case lltok::kw_nofree: B.addAttribute(Attribute::NoFree); break; case lltok::kw_noimplicitfloat: diff --git a/llvm/lib/AsmParser/LLToken.h b/llvm/lib/AsmParser/LLToken.h --- a/llvm/lib/AsmParser/LLToken.h +++ b/llvm/lib/AsmParser/LLToken.h @@ -200,6 +200,7 @@ kw_noalias, kw_noundef, kw_nobuiltin, + kw_nocallback, kw_nocapture, kw_noduplicate, kw_nofree, diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -1433,6 +1433,8 @@ return Attribute::NoAlias; case bitc::ATTR_KIND_NO_BUILTIN: return Attribute::NoBuiltin; + case bitc::ATTR_KIND_NO_CALLBACK: + return Attribute::NoCallback; case bitc::ATTR_KIND_NO_CAPTURE: return Attribute::NoCapture; case bitc::ATTR_KIND_NO_DUPLICATE: diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp --- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -646,6 +646,8 @@ return bitc::ATTR_KIND_NO_ALIAS; case Attribute::NoBuiltin: return bitc::ATTR_KIND_NO_BUILTIN; + case Attribute::NoCallback: + return bitc::ATTR_KIND_NO_CALLBACK; case Attribute::NoCapture: return bitc::ATTR_KIND_NO_CAPTURE; case Attribute::NoDuplicate: diff --git a/llvm/lib/IR/Attributes.cpp b/llvm/lib/IR/Attributes.cpp --- a/llvm/lib/IR/Attributes.cpp +++ b/llvm/lib/IR/Attributes.cpp @@ -371,6 +371,8 @@ return "noalias"; if (hasAttribute(Attribute::NoBuiltin)) return "nobuiltin"; + if (hasAttribute(Attribute::NoCallback)) + return "nocallback"; if (hasAttribute(Attribute::NoCapture)) return "nocapture"; if (hasAttribute(Attribute::NoDuplicate)) diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp --- a/llvm/lib/IR/Verifier.cpp +++ b/llvm/lib/IR/Verifier.cpp @@ -1597,6 +1597,7 @@ case Attribute::NoReturn: case Attribute::NoSync: case Attribute::WillReturn: + case Attribute::NoCallback: case Attribute::NoCfCheck: case Attribute::NoUnwind: case Attribute::NoInline: diff --git a/llvm/lib/Transforms/Utils/CodeExtractor.cpp b/llvm/lib/Transforms/Utils/CodeExtractor.cpp --- a/llvm/lib/Transforms/Utils/CodeExtractor.cpp +++ b/llvm/lib/Transforms/Utils/CodeExtractor.cpp @@ -946,6 +946,7 @@ case Attribute::NoRecurse: case Attribute::InlineHint: case Attribute::MinSize: + case Attribute::NoCallback: case Attribute::NoDuplicate: case Attribute::NoFree: case Attribute::NoImplicitFloat: diff --git a/llvm/test/Bitcode/attributes.ll b/llvm/test/Bitcode/attributes.ll --- a/llvm/test/Bitcode/attributes.ll +++ b/llvm/test/Bitcode/attributes.ll @@ -404,6 +404,12 @@ ret void } +; CHECK; define void @f69() #43 +define void @f70() nocallback +{ + ret void +} + ; CHECK: attributes #0 = { noreturn } ; CHECK: attributes #1 = { nounwind } ; CHECK: attributes #2 = { readnone } @@ -446,4 +452,5 @@ ; CHECK: attributes #39 = { sanitize_memtag } ; CHECK: attributes #40 = { null_pointer_is_valid } ; CHECK: attributes #41 = { mustprogress } +; CHECK: attributes #42 = { nocallback } ; CHECK: attributes #[[NOBUILTIN]] = { nobuiltin }