Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang] Add support for new loop attribute [[clang::code_align()]] #70762

Merged
merged 43 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
93d46d4
[clang] Add support for new loop attribute [[clang::code_align()]]
smanna12 Oct 31, 2023
d049dc9
Address review comments
smanna12 Nov 2, 2023
6c44cc1
Fix clang format errors
smanna12 Nov 2, 2023
f35c4be
Fix Lit test failure
smanna12 Nov 2, 2023
4c40ce2
Address review comments
smanna12 Nov 6, 2023
9144e44
Fix clang format errors
smanna12 Nov 6, 2023
22f66bd
Address review comments
smanna12 Nov 6, 2023
66f6269
Fix lit test failure
smanna12 Nov 7, 2023
3ac064b
Fix DuplicateCodeAlignAttrs check
smanna12 Nov 7, 2023
172e238
Fix Clang format errors
smanna12 Nov 7, 2023
c8d5270
Remove wrong function
smanna12 Nov 7, 2023
d16a0f9
Merge remote-tracking branch 'my_remote/main' into AddCodeAlignAttr
smanna12 Nov 7, 2023
b4b3c10
Merge remote-tracking branch 'my_remote/main' into AddCodeAlignAttr
smanna12 Nov 10, 2023
2e6ee33
Update diagnostic message and test
smanna12 Nov 13, 2023
82c2e70
Merge remote-tracking branch 'my_remote/main' into AddCodeAlignAttr
smanna12 Nov 13, 2023
8474860
Update min and max aligment check
smanna12 Nov 13, 2023
aa85ed1
Add additional members as static-constexpr
smanna12 Nov 13, 2023
0887674
Address review comments
smanna12 Nov 14, 2023
48e00f1
Fix test
smanna12 Nov 14, 2023
8fb7d22
Fix lit test failures
smanna12 Nov 14, 2023
8bec3c4
Fix bug for alignment value with ((__int128_t)0x1234567890abcde0ULL <…
smanna12 Nov 15, 2023
5c4ce9e
Fix clang format errors
smanna12 Nov 15, 2023
974ebf2
update patch
smanna12 Nov 15, 2023
06eadd9
Fix return nullptr check
smanna12 Nov 15, 2023
5ebb42f
Update patch with trySExtValue() and add test case for big negative
smanna12 Nov 15, 2023
ebcefed
Check an expression resulting in a negative that takes more than 64 bits
smanna12 Nov 15, 2023
72a1a69
Fix lit test
smanna12 Nov 15, 2023
845b687
Fix assertion and remove duplicate codes
smanna12 Nov 15, 2023
fca4436
Fix Clang format errors
smanna12 Nov 15, 2023
a8b13eb
Use Simple algorithm to handle duplicate attribute values.
smanna12 Nov 16, 2023
488b810
Fix clang format errors
smanna12 Nov 16, 2023
09b00c7
Fix Duplicate Attribute Values diagnostic using find_if()
smanna12 Nov 16, 2023
5fadd6a
Apply review comments
smanna12 Nov 16, 2023
87bb3f2
Fix clang format errors
smanna12 Nov 16, 2023
8ad4686
Diagnose non-identical duplicates as a 'conflicting' loop attr and
smanna12 Nov 17, 2023
e8ce33e
Merge remote-tracking branch 'my_remote/main' into AddCodeAlignAttr
smanna12 Nov 17, 2023
ac63c62
Address review comments
smanna12 Nov 17, 2023
8fd7bc1
Fix clang format errors
smanna12 Nov 17, 2023
6a986a1
Fix crash and add dependence tests
smanna12 Nov 17, 2023
f32df3f
Address review comments
smanna12 Nov 17, 2023
d5ee5c1
Fix clang format errors
smanna12 Nov 17, 2023
6778154
Fix diagnostic issues with conflicting attributes for template cases
smanna12 Nov 20, 2023
886c03a
Fix clang format errors
smanna12 Nov 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Address review comments
  • Loading branch information
smanna12 committed Nov 2, 2023
commit d049dc9997bdb78ff7e7cbf9b04aa42b9274cfd9
13 changes: 10 additions & 3 deletions clang/include/clang/Basic/Attr.td
Original file line number Diff line number Diff line change
Expand Up @@ -4282,10 +4282,17 @@ def PreferredType: InheritableAttr {
}

def CodeAlign: StmtAttr {
let Spellings = [CXX11<"clang", "code_align">,
C23<"clang", "code_align">];
let Spellings = [Clang<"code_align">];
let Subjects = SubjectList<[ForStmt, CXXForRangeStmt, WhileStmt, DoStmt],
ErrorDiag, "'for', 'while', and 'do' statements">;
let Args = [ExprArgument<"NExpr">];
let Args = [ExprArgument<"Alignment">];
let Documentation = [CodeAlignAttrDocs];
let AdditionalMembers = [{
smanna12 marked this conversation as resolved.
Show resolved Hide resolved
static int getMinValue() {
return 1;
}
static int getMaxValue() {
return 4096;
}
}];
}
9 changes: 4 additions & 5 deletions clang/include/clang/Basic/AttrDocs.td
Original file line number Diff line number Diff line change
Expand Up @@ -7421,12 +7421,11 @@ def CodeAlignAttrDocs : Documentation {
let Category = DocCatVariable;
let Heading = "clang::code_align";
let Content = [{
The ``clang::code_align(N)`` attribute applies to a loop and it specifies the
byte alignment for a loop. The attribute accepts a positive integer constant
The ``clang::code_align(N)`` attribute applies to a loop and specifies the byte
alignment for a loop. The attribute accepts a positive integer constant
initialization expression indicating the number of bytes for the minimum
alignment boundary. Its value must be a power of 2, between 1 and 4096, such as
1, 2, 4, 8, and so on. This attribute sets ``llvm.loop.align`` loop metadata
when it applies on a loop statement.
alignment boundary. Its value must be a power of 2, between 1 and 4096
(inclusive).

.. code-block:: c++

Expand Down
4 changes: 2 additions & 2 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -10028,8 +10028,8 @@ def warn_missing_case_for_condition :
Warning<"no case matching constant switch condition '%0'">;
def err_loop_attr_duplication : Error<
"duplicate loop attribute %0">;
def err_attribute_argument_not_power_of_two : Error<
"%0 attribute argument must be a constant power of two greater than zero">;
def err_attribute_power_of_two_in_range : Error<
"%0 attribute must be a constant power of two between %1 and %2 inclusive">;
smanna12 marked this conversation as resolved.
Show resolved Hide resolved

def warn_def_missing_case : Warning<"%plural{"
"1:enumeration value %1 not explicitly handled in switch|"
Expand Down
7 changes: 2 additions & 5 deletions clang/lib/CodeGen/CGLoopInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -797,15 +797,12 @@ void LoopInfoStack::push(BasicBlock *Header, clang::ASTContext &Ctx,
}
}

// Translate 'loop attributes' arguments to equivalent Attr enums.
// It's being handled separately from LoopHintAttrs not to support
// legacy GNU attributes and pragma styles.
//
// Identify loop attribute 'code_align' from Attrs.
// For attribute code_align:
// n - 'llvm.loop.align i32 n' metadata will be emitted.
for (const auto *A : Attrs) {
smanna12 marked this conversation as resolved.
Show resolved Hide resolved
if (const auto *CodeAlign = dyn_cast<CodeAlignAttr>(A)) {
const auto *CE = cast<ConstantExpr>(CodeAlign->getNExpr());
const auto *CE = cast<ConstantExpr>(CodeAlign->getAlignment());
llvm::APSInt ArgVal = CE->getResultAsAPSInt();
setCodeAlign(ArgVal.getSExtValue());
}
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/CodeGen/CGLoopInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ struct LoopAttributes {
/// Value for llvm.loop.pipeline.iicount metadata.
unsigned PipelineInitiationInterval;

/// Value for 'llvm.loop.align' loop metadata.
/// Value for 'llvm.loop.align' metadata.
unsigned CodeAlign;

/// Value for whether the loop is required to make progress.
Expand Down Expand Up @@ -285,7 +285,7 @@ class LoopInfoStack {
StagedAttrs.PipelineInitiationInterval = C;
}

/// Set the CodeAlign for the next loop pushed.
/// Set value of code align for the next loop pushed.
void setCodeAlign(unsigned C) { StagedAttrs.CodeAlign = C; }

/// Set no progress for the next loop pushed.
Expand Down
32 changes: 14 additions & 18 deletions clang/lib/Sema/SemaStmtAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,17 +331,13 @@ CodeAlignAttr *Sema::BuildCodeAlignAttr(const AttributeCommonInfo &CI,
return nullptr;
E = Res.get();

// This attribute requires a strictly positive value.
if (ArgVal <= 0) {
Diag(E->getExprLoc(), diag::err_attribute_requires_positive_integer)
<< CI << /*positive*/ 0;
return nullptr;
}

// This attribute requires a single constant power of two greater than zero.
if (!ArgVal.isPowerOf2()) {
Diag(E->getExprLoc(), diag::err_attribute_argument_not_power_of_two)
<< CI;
int align_value = ArgVal.getSExtValue();
if (align_value < CodeAlignAttr::getMinValue() ||
align_value > CodeAlignAttr::getMaxValue() ||
!ArgVal.isPowerOf2()) {
Diag(CI.getLoc(), diag:: err_attribute_power_of_two_in_range)
<< CI << CodeAlignAttr::getMinValue()
<< CodeAlignAttr::getMaxValue();
return nullptr;
}
}
Expand All @@ -360,15 +356,15 @@ template <typename LoopAttrT>
static void
CheckForDuplicateLoopAttribute(Sema &S,
const SmallVectorImpl<const Attr *> &Attrs) {
const LoopAttrT *LoopAttr = nullptr;

const Attr *A = nullptr;
for (const auto *I : Attrs) {
if (LoopAttr && isa<LoopAttrT>(I)) {
// Cannot specify same type of attribute twice.
S.Diag(I->getLocation(), diag::err_loop_attr_duplication) << LoopAttr;
if (isa<LoopAttrT>(I)) {
if (A) {
// Cannot specify same type of attribute twice.
S.Diag(I->getLocation(), diag::err_loop_attr_duplication) << A;
}
A = I;
}
if (isa<LoopAttrT>(I))
LoopAttr = cast<LoopAttrT>(I);
}
}

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Sema/SemaTemplateInstantiate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1907,7 +1907,7 @@ const AlwaysInlineAttr *TemplateInstantiator::TransformStmtAlwaysInlineAttr(

const CodeAlignAttr *
TemplateInstantiator::TransformCodeAlignAttr(const CodeAlignAttr *CA) {
Expr *TransformedExpr = getDerived().TransformExpr(CA->getNExpr()).get();
Expr *TransformedExpr = getDerived().TransformExpr(CA->getAlignment()).get();
return getSema().BuildCodeAlignAttr(*CA, TransformedExpr);
}

Expand Down
6 changes: 2 additions & 4 deletions clang/test/CodeGen/code_align.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o - | FileCheck -check-prefix=CHECK-C %s
// RUN: %clang_cc1 -x c++ -std=c++11 -fsyntax-only -emit-llvm %s -o - | FileCheck %s --check-prefixes CHECK-C,CHECK-CPP

// Add CodeGen tests for Loop attribute: [[clang::code_align)]].
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -x c %s %s -o - | FileCheck -check-prefix=CHECK-C %s
// RUN: %clang_cc1 -fsyntax-only -emit-llvm -x c++ -std=c++11 %s -o - | FileCheck %s --check-prefixes CHECK-C,CHECK-CPP

// CHECK-C: br label %for.cond, !llvm.loop ![[MD_FP:[0-9]+]]
// CHECK-C: br label %while.cond, !llvm.loop ![[MD_FP_1:[0-9]+]]
Expand Down
21 changes: 12 additions & 9 deletions clang/test/Sema/code_align.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// RUN: %clang_cc1 -fsyntax-only -verify=expected,c-local %s
// RUN: %clang_cc1 -fsyntax-only -verify=expected,cpp-local -pedantic -x c++ -std=c++11 %s

// Add diagnostics tests for Loop attribute: [[clang::code_align()]].
// RUN: %clang_cc1 -fsyntax-only -verify=expected,c-local -x c %s
// RUN: %clang_cc1 -fsyntax-only -verify=expected,cpp-local -pedantic -x c++ -std=c++11 %s

void foo() {
int i;
Expand All @@ -28,11 +26,11 @@ void bar(int);
#endif
void foo1(int A)
{
// expected-error@+1 {{'code_align' attribute requires a positive integral compile time constant expression}}
// expected-error@+1 {{'code_align' attribute must be a constant power of two between 1 and 4096 inclusive}}
[[clang::code_align(0)]]
for(int I=0; I<128; ++I) { bar(I); }

// expected-error@+1{{'code_align' attribute requires a positive integral compile time constant expression}}
// expected-error@+1{{'code_align' attribute must be a constant power of two between 1 and 4096 inclusive}}
[[clang::code_align(-4)]]
for(int I=0; I<128; ++I) { bar(I); }

Expand Down Expand Up @@ -69,10 +67,14 @@ void foo1(int A)
[[clang::code_align(64)]]
for(int I=0; I<128; ++I) { bar(I); }

// expected-error@+1 {{'code_align' attribute argument must be a constant power of two greater than zero}}
// expected-error@+1 {{'code_align' attribute must be a constant power of two between 1 and 4096 inclusive}}
[[clang::code_align(7)]]
for(int I=0; I<128; ++I) { bar(I); }

// expected-error@+1 {{'code_align' attribute must be a constant power of two between 1 and 4096 inclusive}}
[[clang::code_align(5000)]]
for(int I=0; I<128; ++I) { bar(I); }

#if __cplusplus >= 201103L
// cpp-local-error@+5 {{expression is not an integral constant expression}}
// cpp-local-note@+4 {{function parameter 'A' with unknown value cannot be used in a constant expression}}
Expand Down Expand Up @@ -112,13 +114,14 @@ void code_align_dependent() {
[[clang::code_align(B)]]
for(int I=0; I<128; ++I) { bar(I); }

// cpp-local-error@+1{{'code_align' attribute requires a positive integral compile time constant expression}}
// cpp-local-error@+2{{'code_align' attribute must be a constant power of two between 1 and 4096 inclusive}}
// cpp-local-note@#neg-instantiation {{in instantiation of function template specialization}}
[[clang::code_align(D)]]
for(int I=0; I<128; ++I) { bar(I); }
}

int main() {
code_align_dependent<8, 16, 32, -10>(); // cpp-local-note{{in instantiation of function template specialization 'code_align_dependent<8, 16, 32, -10>' requested here}}
code_align_dependent<8, 16, 32, -10>(); // #neg-instantiation
return 0;
}
#endif
8 changes: 2 additions & 6 deletions clang/test/Sema/code_align_ast.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
// RUN: %clang_cc1 -fsyntax-only -ast-dump -verify %s | FileCheck -check-prefix=CHECK-C %s
// RUN: %clang_cc1 -x c++ -std=c++11 -fsyntax-only -ast-dump %s | FileCheck %s --check-prefixes CHECK-C,CHECK-CPP

// expected-no-diagnostics

// Add AST tests for Loop attribute: [[clang::code_align()]].
// RUN: %clang_cc1 -fsyntax-only -ast-dump -verify -x c %s | FileCheck -check-prefix=CHECK-C %s
// RUN: %clang_cc1 -fsyntax-only -ast-dump -x c++ -std=c++11 %s | FileCheck %s --check-prefixes CHECK-C,CHECK-CPP

void bar(int);
// CHECK-C: FunctionDecl{{.*}}code_align 'void ()'
Expand Down
Loading