Skip to content

Commit 34e880a

Browse files
committed
[clang] Add value_type attr, use it to add noalias when pass-by-value.
This adds an attribute version of -fpass-by-value-is-noalias (added in a874d63) Still needs proper docs.
1 parent e7d569a commit 34e880a

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed

clang/include/clang/Basic/Attr.td

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,6 +1811,16 @@ def TrivialABI : InheritableAttr {
18111811
let SimpleHandler = 1;
18121812
}
18131813

1814+
def ValueType : InheritableAttr {
1815+
// This attribute does not have a C [[]] spelling because it requires the
1816+
// CPlusPlus language option.
1817+
let Spellings = [Clang<"value_type", 0>];
1818+
let Subjects = SubjectList<[CXXRecord]>;
1819+
let Documentation = [TrivialABIDocs];
1820+
let LangOpts = [CPlusPlus];
1821+
let SimpleHandler = 1;
1822+
}
1823+
18141824
def MaxFieldAlignment : InheritableAttr {
18151825
// This attribute has no spellings as it is only ever created implicitly.
18161826
let Spellings = [];

clang/lib/CodeGen/CGCall.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2744,9 +2744,10 @@ void CodeGenModule::ConstructAttributeList(StringRef Name,
27442744
Attrs.addByValAttr(getTypes().ConvertTypeForMem(ParamType));
27452745

27462746
auto *Decl = ParamType->getAsRecordDecl();
2747-
if (CodeGenOpts.PassByValueIsNoAlias && Decl &&
2748-
Decl->getArgPassingRestrictions() ==
2749-
RecordArgPassingKind::CanPassInRegs)
2747+
if (Decl && ((CodeGenOpts.PassByValueIsNoAlias &&
2748+
Decl->getArgPassingRestrictions() ==
2749+
RecordArgPassingKind::CanPassInRegs) ||
2750+
Decl->hasAttr<ValueTypeAttr>()))
27502751
// When calling the function, the pointer passed in will be the only
27512752
// reference to the underlying object. Mark it accordingly.
27522753
Attrs.addAttribute(llvm::Attribute::NoAlias);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 5
2+
// RUN: %clang_cc1 -triple arm64-apple-ios11 -std=c++11 -fcxx-exceptions -fexceptions -emit-llvm -o - %s | FileCheck %s
3+
4+
struct Small {
5+
int *p;
6+
Small();
7+
~Small();
8+
Small(const Small &) noexcept;
9+
Small &operator=(const Small &);
10+
};
11+
12+
13+
struct __attribute__((value_type)) SmallVT {
14+
int *p;
15+
SmallVT();
16+
~SmallVT();
17+
SmallVT(const SmallVT &) noexcept;
18+
SmallVT &operator=(const SmallVT &);
19+
};
20+
21+
struct Large {
22+
int *p;
23+
int a[128];
24+
Large();
25+
~Large();
26+
Large(const Large &) noexcept;
27+
Large &operator=(const Large &);
28+
};
29+
30+
struct __attribute__((value_type)) LargeVT {
31+
int *p;
32+
int a[128];
33+
LargeVT();
34+
~LargeVT();
35+
LargeVT(const LargeVT &) noexcept;
36+
LargeVT &operator=(const LargeVT &);
37+
};
38+
39+
void foo(Small a, SmallVT b, Large c, LargeVT d) {}
40+
// CHECK: define void @_Z3foo5Small7SmallVT5Large7LargeVT(ptr noundef %a, ptr noalias noundef %b, ptr noundef %c, ptr noalias noundef %d)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@
201201
// CHECK-NEXT: Uninitialized (SubjectMatchRule_variable_is_local)
202202
// CHECK-NEXT: UnsafeBufferUsage (SubjectMatchRule_function)
203203
// CHECK-NEXT: UseHandle (SubjectMatchRule_variable_is_parameter)
204+
// CHECK-NEXT: ValueType (SubjectMatchRule_record)
204205
// CHECK-NEXT: VecReturn (SubjectMatchRule_record)
205206
// CHECK-NEXT: VecTypeHint (SubjectMatchRule_function)
206207
// CHECK-NEXT: WarnUnused (SubjectMatchRule_record)

0 commit comments

Comments
 (0)