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 @@ -1646,6 +1646,13 @@ let SimpleHandler = 1; } +def DisableMerge : InheritableAttr { + let Spellings = [Clang<"disable_merge">]; + let Subjects = SubjectList<[Function]>; + let Documentation = [DisableMergeDocs]; + let SimpleHandler = 1; +} + def NoMips16 : InheritableAttr, TargetSpecificAttr { let Spellings = [GCC<"nomips16">]; let Subjects = SubjectList<[Function], 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 @@ -4123,6 +4123,14 @@ }]; } +def DisableMergeDocs : Documentation { + let Category = DocCatFunction; + let Content = [{ +Use this attribute to prevent multiple calls to the specified function from +merging during optimization. + }]; +} + def AnyX86NoCallerSavedRegistersDocs : 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 @@ -1946,6 +1946,8 @@ FuncAttrs.addAttribute(llvm::Attribute::NoDuplicate); if (TargetDecl->hasAttr()) FuncAttrs.addAttribute(llvm::Attribute::Convergent); + if (TargetDecl->hasAttr()) + FuncAttrs.addAttribute(llvm::Attribute::NoMerge); 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 @@ -7743,6 +7743,9 @@ handleSimpleAttributeWithExclusions(S, D, AL); break; + case ParsedAttr::AT_DisableMerge: + handleSimpleAttribute(S, D, AL); + break; case ParsedAttr::AT_Visibility: handleVisibilityAttr(S, D, AL, false); break; diff --git a/clang/test/CodeGen/attr-disable-merge.cpp b/clang/test/CodeGen/attr-disable-merge.cpp new file mode 100644 --- /dev/null +++ b/clang/test/CodeGen/attr-disable-merge.cpp @@ -0,0 +1,12 @@ +// RUN: %clang_cc1 -S -emit-llvm %s -triple x86_64-unknown-linux-gnu -o - | FileCheck %s + +[[clang::disable_merge]] void bar(); + +void foo(int i) { + bar(); +} + +// CHECK: call void @_Z3barv() #[[NOMERGEATTR2:[0-9]+]] +// CHECK: declare void @_Z3barv() #[[NOMERGEATTR:[0-9]+]] +// CHECK: attributes #[[NOMERGEATTR]] = { nomerge {{.*}}} +// CHECK: attributes #[[NOMERGEATTR2]] = { nomerge } 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 @@ -54,6 +54,7 @@ // CHECK-NEXT: DLLExport (SubjectMatchRule_function, SubjectMatchRule_variable, SubjectMatchRule_record, SubjectMatchRule_objc_interface) // CHECK-NEXT: DLLImport (SubjectMatchRule_function, SubjectMatchRule_variable, SubjectMatchRule_record, SubjectMatchRule_objc_interface) // CHECK-NEXT: Destructor (SubjectMatchRule_function) +// CHECK-NEXT: DisableMerge (SubjectMatchRule_function) // CHECK-NEXT: DisableTailCalls (SubjectMatchRule_function, SubjectMatchRule_objc_method) // CHECK-NEXT: EnableIf (SubjectMatchRule_function) // CHECK-NEXT: EnumExtensibility (SubjectMatchRule_enum) diff --git a/clang/test/Sema/attr-disable-merge.c b/clang/test/Sema/attr-disable-merge.c new file mode 100644 --- /dev/null +++ b/clang/test/Sema/attr-disable-merge.c @@ -0,0 +1,5 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +int foo __attribute__((disable_merge)); // expected-warning {{'disable_merge' attribute only applies to functions}} + +int bar(int a) __attribute__((disable_merge("abc"))); // expected-error {{'disable_merge' attribute takes no arguments}} diff --git a/clang/test/SemaCXX/attr-disable-merge.cpp b/clang/test/SemaCXX/attr-disable-merge.cpp new file mode 100644 --- /dev/null +++ b/clang/test/SemaCXX/attr-disable-merge.cpp @@ -0,0 +1,8 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s +// expected-no-diagnostics + +class A { +public: + [[clang::disable_merge]] virtual int foo1() { return 1; } + [[clang::disable_merge]] int foo2() { return 2; } +}; \ No newline at end of file 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 @@ -121,7 +121,7 @@ /// Function is called early and/or often, so lazy binding isn't worthwhile. def NonLazyBind : EnumAttr<"nonlazybind">; -/// Disable merging for call sites +/// Disable merging for specified function or call sites. def NoMerge : EnumAttr<"nomerge">; /// Pointer is known to be not null.