Skip to content

Commit b9c7754

Browse files
committed
[Clang][Attr] Introduce the assume function attribute
The `assume` attribute is a way to provide additional, arbitrary information to the optimizer. For now, assumptions are restricted to strings which will be accumulated for a function and emitted as comma separated string function attribute. The key of the LLVM-IR function attribute is `llvm.assume`. Similar to `llvm.assume` and `__builtin_assume`, the `assume` attribute provides a user defined assumption to the compiler. A follow up patch will introduce an LLVM-core API to query the assumptions attached to a function. We also expect to add more options, e.g., expression arguments, to the `assume` attribute later on. The `omp [begin] asssumes` pragma will leverage this attribute and expose the functionality in the absence of OpenMP. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D91979
1 parent d08d490 commit b9c7754

File tree

16 files changed

+390
-1
lines changed

16 files changed

+390
-1
lines changed

clang/docs/LanguageExtensions.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,6 +1740,8 @@ implemented directly in terms of :ref:`extended vector support
17401740
<langext-vectors>` instead of builtins, in order to reduce the number of
17411741
builtins that we need to implement.
17421742
1743+
.. _langext-__builtin_assume:
1744+
17431745
``__builtin_assume``
17441746
------------------------------
17451747

clang/include/clang/Basic/Attr.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3527,6 +3527,14 @@ def OMPDeclareVariant : InheritableAttr {
35273527
}];
35283528
}
35293529

3530+
def Assumption : InheritableAttr {
3531+
let Spellings = [Clang<"assume">];
3532+
let Subjects = SubjectList<[Function, ObjCMethod]>;
3533+
let InheritEvenIfAlreadyPresent = 1;
3534+
let Documentation = [AssumptionDocs];
3535+
let Args = [StringArgument<"Assumption">];
3536+
}
3537+
35303538
def InternalLinkage : InheritableAttr {
35313539
let Spellings = [Clang<"internal_linkage">];
35323540
let Subjects = SubjectList<[Var, Function, CXXRecord]>;

clang/include/clang/Basic/AttrDocs.td

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3992,6 +3992,41 @@ For more information see
39923992
}];
39933993
}
39943994

3995+
def AssumptionDocs : Documentation {
3996+
let Category = DocCatFunction;
3997+
let Heading = "assume";
3998+
let Content = [{
3999+
Clang supports the ``__attribute__((assume("assumption")))`` attribute to
4000+
provide additional information to the optimizer. The string-literal, here
4001+
"assumption", will be attached to the function declaration such that later
4002+
analysis and optimization passes can assume the "assumption" to hold.
4003+
This is similar to :ref:`__builtin_assume <langext-__builtin_assume>` but
4004+
instead of an expression that can be assumed to be non-zero, the assumption is
4005+
expressed as a string and it holds for the entire function.
4006+
4007+
A function can have multiple assume attributes and they propagate from prior
4008+
declarations to later definitions. Multiple assumptions are aggregated into a
4009+
single comma separated string. Thus, one can provide multiple assumptions via
4010+
a comma separated string, i.a.,
4011+
``__attribute__((assume("assumption1,assumption2")))``.
4012+
4013+
While LLVM plugins might provide more assumption strings, the default LLVM
4014+
optimization passes are aware of the following assumptions:
4015+
4016+
.. code-block:: none
4017+
4018+
"omp_no_openmp"
4019+
"omp_no_openmp_routines"
4020+
"omp_no_parallelism"
4021+
4022+
The OpenMP standard defines the meaning of OpenMP assumptions ("omp_XYZ" is
4023+
spelled "XYZ" in the `OpenMP 5.1 Standard`_).
4024+
4025+
.. _`OpenMP 5.1 Standard`: https://www.openmp.org/spec-html/5.1/openmpsu37.html#x56-560002.5.2
4026+
4027+
}];
4028+
}
4029+
39954030
def NoStackProtectorDocs : Documentation {
39964031
let Category = DocCatFunction;
39974032
let Content = [{

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ def Implicit : DiagGroup<"implicit", [
1919
def ODR : DiagGroup<"odr">;
2020
def : DiagGroup<"abi">;
2121
def AbsoluteValue : DiagGroup<"absolute-value">;
22+
def MisspelledAssumption : DiagGroup<"misspelled-assumption">;
23+
def UnknownAssumption : DiagGroup<"unknown-assumption">;
2224
def AddressOfTemporary : DiagGroup<"address-of-temporary">;
2325
def : DiagGroup<"aggregate-return">;
2426
def GNUAlignofExpression : DiagGroup<"gnu-alignof-expression">;

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,13 @@ def note_strncat_wrong_size : Note<
740740
def warn_assume_side_effects : Warning<
741741
"the argument to %0 has side effects that will be discarded">,
742742
InGroup<DiagGroup<"assume">>;
743+
def warn_assume_attribute_string_unknown : Warning<
744+
"unknown assumption string '%0'; attribute is potentially ignored">,
745+
InGroup<UnknownAssumption>;
746+
def warn_assume_attribute_string_unknown_suggested : Warning<
747+
"unknown assumption string '%0' may be misspelled; attribute is potentially "
748+
"ignored, did you mean '%1'?">,
749+
InGroup<MisspelledAssumption>;
743750

744751
def warn_builtin_chk_overflow : Warning<
745752
"'%0' will always overflow; destination buffer has size %1,"

clang/include/clang/Sema/Sema.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@
5858
#include "llvm/ADT/Optional.h"
5959
#include "llvm/ADT/SetVector.h"
6060
#include "llvm/ADT/SmallBitVector.h"
61-
#include "llvm/ADT/SmallSet.h"
6261
#include "llvm/ADT/SmallPtrSet.h"
62+
#include "llvm/ADT/SmallSet.h"
6363
#include "llvm/ADT/SmallVector.h"
6464
#include "llvm/ADT/TinyPtrVector.h"
6565
#include "llvm/Frontend/OpenMP/OMPConstants.h"

clang/lib/CodeGen/CGCall.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "clang/CodeGen/SwiftCallingConv.h"
3232
#include "llvm/ADT/StringExtras.h"
3333
#include "llvm/Analysis/ValueTracking.h"
34+
#include "llvm/IR/Assumptions.h"
3435
#include "llvm/IR/Attributes.h"
3536
#include "llvm/IR/CallingConv.h"
3637
#include "llvm/IR/DataLayout.h"
@@ -2015,6 +2016,18 @@ void CodeGenModule::ConstructAttributeList(
20152016
llvm::toStringRef(CodeGenOpts.UniformWGSize));
20162017
}
20172018
}
2019+
2020+
std::string AssumptionValueStr;
2021+
for (AssumptionAttr *AssumptionA :
2022+
TargetDecl->specific_attrs<AssumptionAttr>()) {
2023+
std::string AS = AssumptionA->getAssumption().str();
2024+
if (!AS.empty() && !AssumptionValueStr.empty())
2025+
AssumptionValueStr += ",";
2026+
AssumptionValueStr += AS;
2027+
}
2028+
2029+
if (!AssumptionValueStr.empty())
2030+
FuncAttrs.addAttribute(llvm::AssumptionAttrKey, AssumptionValueStr);
20182031
}
20192032

20202033
// Attach "no-builtins" attributes to:

clang/lib/Sema/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
set(LLVM_LINK_COMPONENTS
2+
Core
23
FrontendOpenMP
34
Support
45
)

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "clang/AST/RecursiveASTVisitor.h"
2424
#include "clang/AST/Type.h"
2525
#include "clang/Basic/CharInfo.h"
26+
#include "clang/Basic/SourceLocation.h"
2627
#include "clang/Basic/SourceManager.h"
2728
#include "clang/Basic/TargetBuiltins.h"
2829
#include "clang/Basic/TargetInfo.h"
@@ -38,6 +39,7 @@
3839
#include "llvm/ADT/Optional.h"
3940
#include "llvm/ADT/STLExtras.h"
4041
#include "llvm/ADT/StringExtras.h"
42+
#include "llvm/IR/Assumptions.h"
4143
#include "llvm/Support/MathExtras.h"
4244
#include "llvm/Support/raw_ostream.h"
4345

@@ -1712,6 +1714,42 @@ void Sema::AddAllocAlignAttr(Decl *D, const AttributeCommonInfo &CI,
17121714
D->addAttr(::new (Context) AllocAlignAttr(Context, CI, Idx));
17131715
}
17141716

1717+
/// Check if \p AssumptionStr is a known assumption and warn if not.
1718+
static void checkAssumptionAttr(Sema &S, SourceLocation Loc,
1719+
StringRef AssumptionStr) {
1720+
if (llvm::KnownAssumptionStrings.count(AssumptionStr))
1721+
return;
1722+
1723+
unsigned BestEditDistance = 3;
1724+
StringRef Suggestion;
1725+
for (const auto &KnownAssumptionIt : llvm::KnownAssumptionStrings) {
1726+
unsigned EditDistance =
1727+
AssumptionStr.edit_distance(KnownAssumptionIt.getKey());
1728+
if (EditDistance < BestEditDistance) {
1729+
Suggestion = KnownAssumptionIt.getKey();
1730+
BestEditDistance = EditDistance;
1731+
}
1732+
}
1733+
1734+
if (!Suggestion.empty())
1735+
S.Diag(Loc, diag::warn_assume_attribute_string_unknown_suggested)
1736+
<< AssumptionStr << Suggestion;
1737+
else
1738+
S.Diag(Loc, diag::warn_assume_attribute_string_unknown) << AssumptionStr;
1739+
}
1740+
1741+
static void handleAssumumptionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1742+
// Handle the case where the attribute has a text message.
1743+
StringRef Str;
1744+
SourceLocation AttrStrLoc;
1745+
if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &AttrStrLoc))
1746+
return;
1747+
1748+
checkAssumptionAttr(S, AttrStrLoc, Str);
1749+
1750+
D->addAttr(::new (S.Context) AssumptionAttr(S.Context, AL, Str));
1751+
}
1752+
17151753
/// Normalize the attribute, __foo__ becomes foo.
17161754
/// Returns true if normalization was applied.
17171755
static bool normalizeName(StringRef &AttrName) {
@@ -7845,6 +7883,9 @@ static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
78457883
case ParsedAttr::AT_Unavailable:
78467884
handleAttrWithMessage<UnavailableAttr>(S, D, AL);
78477885
break;
7886+
case ParsedAttr::AT_Assumption:
7887+
handleAssumumptionAttr(S, D, AL);
7888+
break;
78487889
case ParsedAttr::AT_ObjCDirect:
78497890
handleObjCDirectAttr(S, D, AL);
78507891
break;

clang/test/CodeGen/assume_attr.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// RUN: %clang_cc1 -emit-llvm -triple i386-linux-gnu %s -o - | FileCheck %s
2+
// RUN: %clang_cc1 -x c -emit-pch -o %t %s
3+
// RUN: %clang_cc1 -include-pch %t %s -emit-llvm -o - | FileCheck %s
4+
5+
// TODO: for "foo" and "bar", "after" is not added as it appears "after" the first use or definition respectively. There might be a way to allow that.
6+
7+
// CHECK: define{{.*}} void @bar() #0
8+
// CHECK: define{{.*}} void @baz() #1
9+
// CHECK: declare{{.*}} void @foo() #2
10+
// CHECK: attributes #0
11+
// CHECK-SAME: "llvm.assume"="bar:before1,bar:before2,bar:before3,bar:def1,bar:def2"
12+
// CHECK: attributes #1
13+
// CHECK-SAME: "llvm.assume"="baz:before1,baz:before2,baz:before3,baz:def1,baz:def2,baz:after"
14+
// CHECK: attributes #2
15+
// CHECK-SAME: "llvm.assume"="foo:before1,foo:before2,foo:before3"
16+
17+
#ifndef HEADER
18+
#define HEADER
19+
20+
/// foo: declarations only
21+
22+
__attribute__((assume("foo:before1"))) void foo(void);
23+
24+
__attribute__((assume("foo:before2")))
25+
__attribute__((assume("foo:before3"))) void
26+
foo(void);
27+
28+
/// baz: static function declarations and a definition
29+
30+
__attribute__((assume("baz:before1"))) static void baz(void);
31+
32+
__attribute__((assume("baz:before2")))
33+
__attribute__((assume("baz:before3"))) static void
34+
baz(void);
35+
36+
// Definition
37+
__attribute__((assume("baz:def1,baz:def2"))) static void baz(void) { foo(); }
38+
39+
__attribute__((assume("baz:after"))) static void baz(void);
40+
41+
/// bar: external function declarations and a definition
42+
43+
__attribute__((assume("bar:before1"))) void bar(void);
44+
45+
__attribute__((assume("bar:before2")))
46+
__attribute__((assume("bar:before3"))) void
47+
bar(void);
48+
49+
// Definition
50+
__attribute__((assume("bar:def1,bar:def2"))) void bar(void) { baz(); }
51+
52+
__attribute__((assume("bar:after"))) void bar(void);
53+
54+
/// back to foo
55+
56+
__attribute__((assume("foo:after"))) void foo(void);
57+
58+
#endif

clang/test/CodeGenCXX/assume_attr.cpp

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// RUN: %clang_cc1 -emit-llvm -triple i386-linux-gnu %s -o - | FileCheck %s
2+
// RUN: %clang_cc1 -x c++ -emit-pch -triple i386-linux-gnu -o %t %s
3+
// RUN: %clang_cc1 -include-pch %t %s -triple i386-linux-gnu -emit-llvm -o - | FileCheck %s
4+
// expected-no-diagnostics
5+
6+
#ifndef HEADER
7+
#define HEADER
8+
9+
/// foo: declarations only
10+
11+
__attribute__((assume("foo:before1"))) void foo();
12+
13+
__attribute__((assume("foo:before2")))
14+
__attribute__((assume("foo:before3"))) void
15+
foo();
16+
17+
/// baz: static function declarations and a definition
18+
19+
__attribute__((assume("baz:before1"))) static void baz();
20+
21+
__attribute__((assume("baz:before2")))
22+
__attribute__((assume("baz:before3"))) static void
23+
baz();
24+
25+
// Definition
26+
__attribute__((assume("baz:def1,baz:def2"))) static void baz() { foo(); }
27+
28+
__attribute__((assume("baz:after"))) static void baz();
29+
30+
/// bar: external function declarations and a definition
31+
32+
__attribute__((assume("bar:before1"))) void bar();
33+
34+
__attribute__((assume("bar:before2")))
35+
__attribute__((assume("bar:before3"))) void
36+
bar();
37+
38+
// Definition
39+
__attribute__((assume("bar:def1,bar:def2"))) void bar() { baz(); }
40+
41+
__attribute__((assume("bar:after"))) void bar();
42+
43+
/// back to foo
44+
45+
__attribute__((assume("foo:after"))) void foo();
46+
47+
/// class tests
48+
class C {
49+
__attribute__((assume("C:private_method"))) void private_method();
50+
__attribute__((assume("C:private_static"))) static void private_static();
51+
52+
public:
53+
__attribute__((assume("C:public_method1"))) void public_method();
54+
__attribute__((assume("C:public_static1"))) static void public_static();
55+
};
56+
57+
__attribute__((assume("C:public_method2"))) void C::public_method() {
58+
private_method();
59+
}
60+
61+
__attribute__((assume("C:public_static2"))) void C::public_static() {
62+
private_static();
63+
}
64+
65+
/// template tests
66+
template <typename T>
67+
__attribute__((assume("template_func<T>"))) void template_func() {}
68+
69+
template <>
70+
__attribute__((assume("template_func<float>"))) void template_func<float>() {}
71+
72+
template <>
73+
void template_func<int>() {}
74+
75+
template <typename T>
76+
struct S {
77+
__attribute__((assume("S<T>::method"))) void method();
78+
};
79+
80+
template <>
81+
__attribute__((assume("S<float>::method"))) void S<float>::method() {}
82+
83+
template <>
84+
void S<int>::method() {}
85+
86+
// CHECK: define{{.*}} void @_Z3barv() #0
87+
// CHECK: define{{.*}} void @_ZL3bazv() #1
88+
// CHECK: define{{.*}} void @_ZN1C13public_methodEv({{.*}}) #2
89+
// CHECK: declare{{.*}} void @_ZN1C14private_methodEv({{.*}}) #3
90+
// CHECK: define{{.*}} void @_ZN1C13public_staticEv() #4
91+
// CHECK: declare{{.*}} void @_ZN1C14private_staticEv() #5
92+
// CHECK: define{{.*}} void @_Z13template_funcIfEvv() #6
93+
// CHECK: define{{.*}} void @_Z13template_funcIiEvv() #7
94+
// CHECK: define{{.*}} void @_ZN1SIfE6methodEv({{.*}}) #8
95+
// CHECK: define{{.*}} void @_ZN1SIiE6methodEv({{.*}}) #9
96+
// CHECK: declare{{.*}} void @_Z3foov() #10
97+
// CHECK: attributes #0
98+
// CHECK-SAME: "llvm.assume"="bar:before1,bar:before2,bar:before3,bar:def1,bar:def2"
99+
// CHECK: attributes #1
100+
// CHECK-SAME: "llvm.assume"="baz:before1,baz:before2,baz:before3,baz:def1,baz:def2,baz:after"
101+
// CHECK: attributes #2
102+
// CHECK-SAME: "llvm.assume"="C:public_method1,C:public_method2"
103+
// CHECK: attributes #3
104+
// CHECK-SAME: "llvm.assume"="C:private_method"
105+
// CHECK: attributes #4
106+
// CHECK-SAME: "llvm.assume"="C:public_static1,C:public_static2"
107+
// CHECK: attributes #5
108+
// CHECK-SAME: "llvm.assume"="C:private_static"
109+
// CHECK: attributes #6
110+
// CHECK-SAME: "llvm.assume"="template_func<T>,template_func<float>"
111+
// CHECK: attributes #7
112+
// CHECK-SAME: "llvm.assume"="template_func<T>"
113+
// CHECK: attributes #8
114+
// CHECK-SAME: "llvm.assume"="S<T>::method,S<float>::method"
115+
// CHECK: attributes #9
116+
// CHECK-SAME: "llvm.assume"="S<T>::method"
117+
// CHECK: attributes #10
118+
// CHECK-SAME: "llvm.assume"="foo:before1,foo:before2,foo:before3"
119+
120+
#endif

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
// CHECK-NEXT: ArcWeakrefUnavailable (SubjectMatchRule_objc_interface)
2121
// CHECK-NEXT: ArmBuiltinAlias (SubjectMatchRule_function)
2222
// CHECK-NEXT: AssumeAligned (SubjectMatchRule_objc_method, SubjectMatchRule_function)
23+
// CHECK-NEXT: Assumption (SubjectMatchRule_function SubjectMatchRule_objc_method)
2324
// CHECK-NEXT: Availability ((SubjectMatchRule_record, SubjectMatchRule_enum, SubjectMatchRule_enum_constant, SubjectMatchRule_field, SubjectMatchRule_function, SubjectMatchRule_namespace, SubjectMatchRule_objc_category, SubjectMatchRule_objc_implementation, SubjectMatchRule_objc_interface, SubjectMatchRule_objc_method, SubjectMatchRule_objc_property, SubjectMatchRule_objc_protocol, SubjectMatchRule_record, SubjectMatchRule_type_alias, SubjectMatchRule_variable))
2425
// CHECK-NEXT: BPFPreserveAccessIndex (SubjectMatchRule_record)
2526
// CHECK-NEXT: CFAuditedTransfer (SubjectMatchRule_function)

0 commit comments

Comments
 (0)