Author: Kenzo Tjandra Date: 2026-08-16T16:16:01Z New Revision: 6a2d309c7d204730a9a35f3261f8448efeb6c5e3
URL: https://github.com/llvm/llvm-project/commit/6a2d309c7d204730a9a35f3261f8448efeb6c5e3 DIFF: https://github.com/llvm/llvm-project/commit/6a2d309c7d204730a9a35f3261f8448efeb6c5e3.diff LOG: [Clang] Diagnose conflict between always_inline/noinline attributes (#215173) This PR aims to diagnose `always_inline` and `noinline` conflicts which currently could pass silently through `Sema` or cause a crash (with assertions enabled). Changes include: - Added `MutualExclusions<[AlwaysInline, NoInline]>` in `Attr.td` - Prevent `always_inline` and `noinline` from being propagated from generic to specialized templates in `SemaDecl.cpp` - Prevent inner attributes from clobbering unrelated outer attributes (e.g. `noinline` and `nomerge`) in both `CGStmt.cpp` and `CIRGenStmt.cpp` Something to consider though: Should we prevent propagating conflicting attributes when doing explicit template specializations in general, not just `inline`, e.g. `hot`/`cold`, `convergent`/`noconvergent`, etc. Fixes #214764 --------- Co-authored-by: Erich Keane <[email protected]> Added: clang/test/CodeGen/attr-noinline-always-inline.cpp Modified: clang/include/clang/Basic/Attr.td clang/lib/CIR/CodeGen/CIRGenStmt.cpp clang/lib/CodeGen/CGStmt.cpp clang/lib/Sema/SemaDecl.cpp clang/test/CIR/CodeGen/callsite-inline-attributes.cpp clang/test/CodeGenHLSL/HLSLControlFlowHint.hlsl clang/test/Sema/attr-noinline.cpp Removed: ################################################################################ diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index f599e6fecc8ff..252b53e25e5c8 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -2420,6 +2420,8 @@ def NoInline : DeclOrStmtAttr { let SimpleHandler = 1; } +def : MutualExclusions<[AlwaysInline, NoInline]>; + def NoOutline : DeclOrStmtAttr { let Spellings = [Clang<"no_outline">]; let Subjects = SubjectList<[Function, ObjCMethod, Block], ErrorDiag>; diff --git a/clang/lib/CIR/CodeGen/CIRGenStmt.cpp b/clang/lib/CIR/CodeGen/CIRGenStmt.cpp index 39919cc28981a..28c82794614d5 100644 --- a/clang/lib/CIR/CodeGen/CIRGenStmt.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenStmt.cpp @@ -90,9 +90,9 @@ mlir::LogicalResult CIRGenFunction::emitCompoundStmtWithoutScope( mlir::LogicalResult CIRGenFunction::emitAttributedStmt(const AttributedStmt &s) { - bool noinline = false; - bool alwaysinline = false; - const CallExpr *musttail = nullptr; + bool noinline = inNoInlineAttributedStmt; + bool alwaysinline = inAlwaysInlineAttributedStmt; + const CallExpr *musttail = mustTailCall; for (const Attr *attr : s.getAttrs()) { switch (attr->getKind()) { @@ -108,9 +108,11 @@ CIRGenFunction::emitAttributedStmt(const AttributedStmt &s) { break; case attr::NoInline: noinline = true; + alwaysinline = false; break; case attr::AlwaysInline: alwaysinline = true; + noinline = false; break; case attr::MustTail: { const Stmt *sub = s.getSubStmt(); @@ -131,6 +133,9 @@ CIRGenFunction::emitAttributedStmt(const AttributedStmt &s) { } } + assert(!(alwaysinline && noinline) && + "alwaysinline and noinline are mutually exclusive"); + SaveAndRestore save_noinline(inNoInlineAttributedStmt, noinline); SaveAndRestore save_alwaysinline(inAlwaysInlineAttributedStmt, alwaysinline); diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp index 18f1e87d3245d..bf6e6eb50f555 100644 --- a/clang/lib/CodeGen/CGStmt.cpp +++ b/clang/lib/CodeGen/CGStmt.cpp @@ -782,14 +782,13 @@ void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) { } void CodeGenFunction::EmitAttributedStmt(const AttributedStmt &S) { - bool nomerge = false; - bool noinline = false; - bool alwaysinline = false; - bool noconvergent = false; - StringRef amdgpuAVMode; - HLSLControlFlowHintAttr::Spelling flattenOrBranch = - HLSLControlFlowHintAttr::SpellingNotCalculated; - const CallExpr *musttail = nullptr; + bool nomerge = InNoMergeAttributedStmt; + bool noinline = InNoInlineAttributedStmt; + bool alwaysinline = InAlwaysInlineAttributedStmt; + bool noconvergent = InNoConvergentAttributedStmt; + StringRef amdgpuAVMode = AMDGPUAvailableVisibleMode; + HLSLControlFlowHintAttr::Spelling flattenOrBranch = HLSLControlFlowAttr; + const CallExpr *musttail = MustTailCall; const AtomicAttr *AA = nullptr; for (const auto *A : S.getAttrs()) { @@ -801,9 +800,11 @@ void CodeGenFunction::EmitAttributedStmt(const AttributedStmt &S) { break; case attr::NoInline: noinline = true; + alwaysinline = false; break; case attr::AlwaysInline: alwaysinline = true; + noinline = false; break; case attr::NoConvergent: noconvergent = true; @@ -832,6 +833,10 @@ void CodeGenFunction::EmitAttributedStmt(const AttributedStmt &S) { } break; } } + + assert(!(alwaysinline && noinline) && + "alwaysinline and noinline are mutually exclusive"); + SaveAndRestore save_nomerge(InNoMergeAttributedStmt, nomerge); SaveAndRestore save_noinline(InNoInlineAttributedStmt, noinline); SaveAndRestore save_alwaysinline(InAlwaysInlineAttributedStmt, alwaysinline); diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index d87710d3cf140..a65596d5bbeeb 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -3389,11 +3389,14 @@ void Sema::mergeDeclAttributes(NamedDecl *New, Decl *Old, if (isa<UsedAttr>(I) || isa<RetainAttr>(I)) continue; - if (isa<InferredNoReturnAttr>(I)) { + // Don't propagate inferred noreturn or conflicting inline attributes to + // explicit specializations. + if (isa<InferredNoReturnAttr>(I) || isa<AlwaysInlineAttr>(I) || + isa<NoInlineAttr>(I)) { if (auto *FD = dyn_cast<FunctionDecl>(New); FD && FD->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) - continue; // Don't propagate inferred noreturn attributes to explicit + continue; } if (mergeDeclAttribute(*this, New, I, LocalAMK)) diff --git a/clang/test/CIR/CodeGen/callsite-inline-attributes.cpp b/clang/test/CIR/CodeGen/callsite-inline-attributes.cpp index 13a4fcfbb690c..d9916e725ca50 100644 --- a/clang/test/CIR/CodeGen/callsite-inline-attributes.cpp +++ b/clang/test/CIR/CodeGen/callsite-inline-attributes.cpp @@ -49,6 +49,14 @@ void caller() { // CIR: cir.call %{{.*}}() {inline_kind = #cir.inline_kind<no_inline>} // LLVM: call void %{{.*}}() #[[NOINLINE]] } + + [[clang::noinline]] + { + [[clang::always_inline]] + callee(); + // CIR: cir.call @_Z6calleev() {inline_kind = #cir.inline_kind<always_inline>} + // LLVM: call void @_Z6calleev() #[[ALWAYSINLINE]] + } } // LLVM: attributes #[[ALWAYSINLINE]] = { alwaysinline } diff --git a/clang/test/CodeGen/attr-noinline-always-inline.cpp b/clang/test/CodeGen/attr-noinline-always-inline.cpp new file mode 100644 index 0000000000000..ee7fa86a85dd7 --- /dev/null +++ b/clang/test/CodeGen/attr-noinline-always-inline.cpp @@ -0,0 +1,92 @@ +// RUN: %clang_cc1 -O1 -disable-llvm-passes -triple x86_64-unknown-linux-gnu -emit-llvm -o - %s | FileCheck %s + +template <typename T> +[[clang::noinline]] void foo(T) {} + +// Explicit specializations should not inherit noinline +template <> +[[clang::always_inline]] void foo<int>(int) {} + +template <> +void foo<long>(long) {} + +void caller() { + foo<float>(4.2f); // expect noinline on function + foo<int>(42); // expect alwaysinline on function + foo<long>(42L); // expect no noinline attribute +} +// CHECK: define {{.*}}void @_Z3fooIiEvT_({{.*}}) #[[ALWAYSINLINE:[0-9]+]] +// CHECK: define {{.*}}void @_Z3fooIlEvT_({{.*}}) #[[BARE:[0-9]+]] +// CHECK: define {{.*}}void @_Z3fooIfEvT_({{.*}}) #[[NOINLINE:[0-9]+]] + +// Inner function should not clobber non-conflicting attributes +void inner_fn(); + +void outer_fn() { + [[clang::noinline]] + { + [[clang::nomerge]] // unrelated to inlining + inner_fn(); + } +} +// CHECK: call void @_Z8inner_fnv() #[[NOINLINE_NOMERGE:[0-9]+]] + +void inner_fn2(); + +void outer_fn2() { + [[clang::nomerge]] + { + [[clang::noinline]] // unrelated to nomerge + inner_fn2(); + } +} +// CHECK: call void @_Z9inner_fn2v() #[[NOINLINE_NOMERGE]] + +[[clang::convergent]] +void inner_fn3(); + +void outer_fn3() { + [[clang::always_inline]] + { + [[clang::noconvergent]] // unrelated to inlining + inner_fn3(); + } +} +// CHECK: call void @_Z9inner_fn3v() #[[ALWAYSINLINE_ONLY:[0-9]+]] + +[[clang::convergent]] +void inner_fn4(); + +void outer_fn4() { + [[clang::noconvergent]] + { + [[clang::always_inline]] // unrelated to noconvergent + inner_fn4(); + } +} +// CHECK: call void @_Z9inner_fn4v() #[[ALWAYSINLINE_ONLY]] + +// Inner function should clobber a conflicting attribute +void inner_fn5(); + +void outer_fn5() { + [[clang::noinline]] + { + [[clang::always_inline]] + inner_fn5(); + } +} +// CHECK: call void @_Z9inner_fn5v() #[[ALWAYSINLINE_ONLY]] + +// CHECK: attributes #[[ALWAYSINLINE]] = { +// CHECK-SAME: alwaysinline +// CHECK-NOT: noinline +// CHECK: attributes #[[BARE]] = { +// CHECK-NOT: alwaysinline +// CHECK-NOT: noinline +// CHECK: attributes #[[NOINLINE]] = { +// CHECK-SAME: noinline +// CHECK-NOT: alwaysinline + +// CHECK: attributes #[[NOINLINE_NOMERGE]] = { noinline nomerge } +// CHECK: attributes #[[ALWAYSINLINE_ONLY]] = { alwaysinline } diff --git a/clang/test/CodeGenHLSL/HLSLControlFlowHint.hlsl b/clang/test/CodeGenHLSL/HLSLControlFlowHint.hlsl index 6737cd3ee78ba..09ae698d3d8c0 100644 --- a/clang/test/CodeGenHLSL/HLSLControlFlowHint.hlsl +++ b/clang/test/CodeGenHLSL/HLSLControlFlowHint.hlsl @@ -44,5 +44,27 @@ export int test_no_attr(int X){ return resp; } +// CHECK-LABEL: test_nested_branch_preserved +// CHECK: br i1 %{{.*}}, label %if.then, label %if.end{{.*}}, !hlsl.controlflow.hint [[HINT_BRANCH]] +// CHECK: br i1 %{{.*}}, label %if.then{{.*}}, label %if.end{{.*}}, !hlsl.controlflow.hint [[HINT_BRANCH]] +void foo(int); +export void test_nested_branch_preserved(int X, int Y) { + [branch] if (X) { + [[clang::noinline]] // unrelated to branch + if (Y) foo(Y); + } +} + +// CHECK-LABEL: test_nested_flatten_preserved +// CHECK: br i1 %{{.*}}, label %if.then, label %if.end{{.*}}, !hlsl.controlflow.hint [[HINT_FLATTEN]] +// CHECK: br i1 %{{.*}}, label %if.then{{.*}}, label %if.end{{.*}}, !hlsl.controlflow.hint [[HINT_FLATTEN]] +void bar(int); +export void test_nested_flatten_preserved(int X, int Y) { + [flatten] if (X) { + [[clang::noinline]] // unrelated to flatten + if (Y) bar(Y); + } +} + //CHECK: [[HINT_BRANCH]] = !{!"hlsl.controlflow.hint", i32 1} //CHECK: [[HINT_FLATTEN]] = !{!"hlsl.controlflow.hint", i32 2} diff --git a/clang/test/Sema/attr-noinline.cpp b/clang/test/Sema/attr-noinline.cpp index d6d48321d6604..b16ee660602d0 100644 --- a/clang/test/Sema/attr-noinline.cpp +++ b/clang/test/Sema/attr-noinline.cpp @@ -119,3 +119,37 @@ void use() { qux<3>(0); // #QUX_INST variadic_qux<0, 1, 2>(0); // #QUX_VARIADIC_INST } + +// Statement attributes are mutually exclusive +void caller() { + [[clang::noinline, clang::always_inline]] bar(); // expected-error {{'clang::always_inline' and 'clang::noinline' attributes are not compatible}} \ + // expected-note {{conflicting attribute is here}} + + [[clang::always_inline, clang::noinline]] bar(); // expected-error {{'clang::noinline' and 'clang::always_inline' attributes are not compatible}} \ + // expected-note {{conflicting attribute is here}} +} + +// Attributes on redeclared functions are mutually exclusive +[[clang::noinline]] void redecl_fn(); // expected-note {{conflicting attribute is here}} +[[clang::always_inline]] void redecl_fn() {} // expected-error {{'clang::always_inline' and 'clang::noinline' attributes are not compatible}} + +[[clang::always_inline]] void redecl_fn2(); // expected-note {{conflicting attribute is here}} +[[clang::noinline]] void redecl_fn2() {} // expected-error {{'clang::noinline' and 'clang::always_inline' attributes are not compatible}} + +// Attributes on the same declaration are mutually exclusive +[[clang::noinline, clang::always_inline]] void decl_fn(); // expected-error {{'clang::always_inline' and 'clang::noinline' attributes are not compatible}} \ + // expected-note {{conflicting attribute is here}} + +// Explicit specialization should not inherit inline attributes +template <typename T> +[[clang::noinline]] void tmpl_fn(T); + +template <> +[[clang::always_inline]] void tmpl_fn(int); // no error expected + +// Check diff erent spellings +[[gnu::noinline]] void spelling_fn(); // expected-note {{conflicting attribute is here}} +[[gnu::always_inline]] void spelling_fn() {} // expected-error {{'gnu::always_inline' and 'gnu::noinline' attributes are not compatible}} + +__attribute__((noinline)) void spelling_fn2(); // expected-note {{conflicting attribute is here}} +__attribute__((always_inline)) void spelling_fn2() {} // expected-error {{'always_inline' and 'noinline' attributes are not compatible}} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
