Skip to content

Commit 31c8669

Browse files
committed
[clang] Add clang::nooutline Attribute
This PR: - Adds a `[[clang::nooutline]]` function attribute for C and C++. There is no equivalent GNU syntax for this attribute, so no `__attribute__` syntax. - Uses the presence of `[[clang::nooutline]]` to add the `nooutline` attribute to IR function definitions. - Turns the `"nooutline"` attribute into an enum attribute (without quotes), and adds an auto-upgrader for bitcode to make that same change to existing IR. - Adds test for the above. The attribute is capable of disabling both the Machine Outliner (enabled at Oz for some targets), and the IR Outliner (disabled by default).
1 parent 53575e7 commit 31c8669

File tree

18 files changed

+78
-6
lines changed

18 files changed

+78
-6
lines changed

clang/include/clang/Basic/Attr.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2355,6 +2355,13 @@ def NoInline : DeclOrStmtAttr {
23552355
let SimpleHandler = 1;
23562356
}
23572357

2358+
def NoOutline : DeclOrStmtAttr {
2359+
let Spellings = [CXX11<"clang", "nooutline">, C23<"clang", "nooutline">];
2360+
let Subjects = SubjectList<[Function], ErrorDiag>;
2361+
let Documentation = [Undocumented];
2362+
let SimpleHandler = 1;
2363+
}
2364+
23582365
def NoMips16 : InheritableAttr, TargetSpecificAttr<TargetMips32> {
23592366
let Spellings = [GCC<"nomips16">];
23602367
let Subjects = SubjectList<[Function], ErrorDiag>;

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2820,6 +2820,9 @@ void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
28202820
B.addAttribute(llvm::Attribute::MinSize);
28212821
}
28222822

2823+
if (D->hasAttr<NoOutlineAttr>())
2824+
B.addAttribute(llvm::Attribute::NoOutline);
2825+
28232826
F->addFnAttrs(B);
28242827

28252828
unsigned alignment = D->getMaxAlignment() / Context.getCharWidth();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --check-attributes --version 6
2+
// RUN: %clang_cc1 -emit-llvm %s -triple x86_64-unknown-linux-gnu -disable-O0-optnone -o - | FileCheck %s
3+
4+
5+
// CHECK: Function Attrs: noinline nooutline nounwind
6+
// CHECK-LABEL: define dso_local i32 @t1(
7+
// CHECK-SAME: i32 noundef [[X:%.*]]) #[[ATTR0:[0-9]+]] {
8+
// CHECK-NEXT: [[ENTRY:.*:]]
9+
// CHECK-NEXT: [[X_ADDR:%.*]] = alloca i32, align 4
10+
// CHECK-NEXT: store i32 [[X]], ptr [[X_ADDR]], align 4
11+
// CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[X_ADDR]], align 4
12+
// CHECK-NEXT: ret i32 [[TMP0]]
13+
//
14+
[[clang::nooutline]] int t1(int x) {
15+
return x;
16+
}

clang/test/Misc/pragma-attribute-supported-attributes-list.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
// CHECK-NEXT: NoMerge (SubjectMatchRule_function, SubjectMatchRule_variable)
124124
// CHECK-NEXT: NoMicroMips (SubjectMatchRule_function)
125125
// CHECK-NEXT: NoMips16 (SubjectMatchRule_function)
126+
// CHECK-NEXT: NoOutline (SubjectMatchRule_function)
126127
// CHECK-NEXT: NoProfileFunction (SubjectMatchRule_function)
127128
// CHECK-NEXT: NoRandomizeLayout (SubjectMatchRule_record)
128129
// CHECK-NEXT: NoSanitize (SubjectMatchRule_function, SubjectMatchRule_objc_method, SubjectMatchRule_variable_is_global)

clang/test/Sema/attr-nooutline.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %clang_cc1 %s -verify -fsyntax-only
2+
3+
[[clang::nooutline]] int a; // expected-error {{'clang::nooutline' attribute only applies to functions}}
4+
5+
[[clang::nooutline]] void t1(void);
6+
7+
[[clang::nooutline(2)]] void t2(void); // expected-error {{'clang::nooutline' attribute takes no arguments}}
8+

clang/test/Sema/attr-nooutline.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %clang_cc1 -verify -fsyntax-only %s -Wno-c++17-extensions
2+
3+
[[clang::nooutline]] int a; // expected-error {{'clang::nooutline' attribute only applies to functions}}
4+
5+
[[clang::nooutline]] void t1(void);
6+
7+
[[clang::nooutline(2)]] void t2(void); // expected-error {{'clang::nooutline' attribute takes no arguments}}

llvm/docs/LangRef.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2738,7 +2738,7 @@ For example:
27382738
to signify an unbounded maximum. The syntax `vscale_range(<val>)` can be
27392739
used to set both `min` and `max` to the same value. Functions that don't
27402740
include this attribute make no assumptions about the value of `vscale`.
2741-
``"nooutline"``
2741+
``nooutline``
27422742
This attribute indicates that outlining passes should not modify the
27432743
function.
27442744

llvm/include/llvm/Bitcode/LLVMBitCodes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,7 @@ enum AttributeKindCodes {
801801
ATTR_KIND_CAPTURES = 102,
802802
ATTR_KIND_DEAD_ON_RETURN = 103,
803803
ATTR_KIND_SANITIZE_ALLOC_TOKEN = 104,
804+
ATTR_KIND_NOOUTLINE = 105,
804805
};
805806

806807
enum ComdatSelectionKindCodes {

llvm/include/llvm/IR/Attributes.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ def NoImplicitFloat : EnumAttr<"noimplicitfloat", IntersectPreserve, [FnAttr]>;
207207
/// inline=never.
208208
def NoInline : EnumAttr<"noinline", IntersectPreserve, [FnAttr]>;
209209

210+
/// nooutline
211+
def NoOutline : EnumAttr<"nooutline", IntersectPreserve, [FnAttr]>;
212+
210213
/// Function is called early and/or often, so lazy binding isn't worthwhile.
211214
def NonLazyBind : EnumAttr<"nonlazybind", IntersectPreserve, [FnAttr]>;
212215

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,6 +2257,8 @@ static Attribute::AttrKind getAttrFromCode(uint64_t Code) {
22572257
return Attribute::Captures;
22582258
case bitc::ATTR_KIND_DEAD_ON_RETURN:
22592259
return Attribute::DeadOnReturn;
2260+
case bitc::ATTR_KIND_NOOUTLINE:
2261+
return Attribute::NoOutline;
22602262
}
22612263
}
22622264

0 commit comments

Comments
 (0)