Skip to content

[Clang] Fix incorrect handling of #pragma {GCC} unroll N in dependent context #90240

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

Merged
merged 6 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 0 additions & 2 deletions clang/lib/CodeGen/CGLoopInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -673,8 +673,6 @@ void LoopInfoStack::push(BasicBlock *Header, clang::ASTContext &Ctx,
setPipelineDisabled(true);
break;
case LoopHintAttr::UnrollCount:
setUnrollState(LoopAttributes::Disable);
break;
case LoopHintAttr::UnrollAndJamCount:
case LoopHintAttr::VectorizeWidth:
case LoopHintAttr::InterleaveCount:
Expand Down
18 changes: 8 additions & 10 deletions clang/lib/Sema/SemaStmtAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,14 @@ static Attr *handleLoopHintAttr(Sema &S, Stmt *St, const ParsedAttr &A,
SetHints(LoopHintAttr::Unroll, LoopHintAttr::Disable);
} else if (PragmaName == "unroll") {
// #pragma unroll N
if (ValueExpr && !ValueExpr->isValueDependent()) {
llvm::APSInt ValueAPS;
ExprResult R = S.VerifyIntegerConstantExpression(ValueExpr, &ValueAPS);
assert(!R.isInvalid() && "unroll count value must be a valid value, it's "
"should be checked in Sema::CheckLoopHintExpr");
(void)R;
// The values of 0 and 1 block any unrolling of the loop.
if (ValueAPS.isZero() || ValueAPS.isOne())
SetHints(LoopHintAttr::UnrollCount, LoopHintAttr::Disable);
else
if (ValueExpr) {
if (!ValueExpr->isValueDependent()) {
auto Value = ValueExpr->EvaluateKnownConstInt(S.getASTContext());
if (Value.isZero() || Value.isOne())
SetHints(LoopHintAttr::Unroll, LoopHintAttr::Disable);
else
SetHints(LoopHintAttr::UnrollCount, LoopHintAttr::Numeric);
} else
SetHints(LoopHintAttr::UnrollCount, LoopHintAttr::Numeric);
} else
SetHints(LoopHintAttr::Unroll, LoopHintAttr::Enable);
Expand Down
18 changes: 15 additions & 3 deletions clang/lib/Sema/SemaTemplateInstantiate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2151,13 +2151,25 @@ TemplateInstantiator::TransformLoopHintAttr(const LoopHintAttr *LH) {

// Generate error if there is a problem with the value.
if (getSema().CheckLoopHintExpr(TransformedExpr, LH->getLocation(),
LH->getOption() == LoopHintAttr::UnrollCount))
LH->getSemanticSpelling() ==
LoopHintAttr::Pragma_unroll))
return LH;

LoopHintAttr::OptionType Option = LH->getOption();
LoopHintAttr::LoopHintState State = LH->getState();

llvm::APSInt ValueAPS =
TransformedExpr->EvaluateKnownConstInt(getSema().getASTContext());
// The values of 0 and 1 block any unrolling of the loop.
if (ValueAPS.isZero() || ValueAPS.isOne()) {
Option = LoopHintAttr::Unroll;
State = LoopHintAttr::Disable;
}

// Create new LoopHintValueAttr with integral expression in place of the
// non-type template parameter.
return LoopHintAttr::CreateImplicit(getSema().Context, LH->getOption(),
LH->getState(), TransformedExpr, *LH);
return LoopHintAttr::CreateImplicit(getSema().Context, Option, State,
TransformedExpr, *LH);
}
const NoInlineAttr *TemplateInstantiator::TransformStmtNoInlineAttr(
const Stmt *OrigS, const Stmt *InstS, const NoInlineAttr *A) {
Expand Down
31 changes: 31 additions & 0 deletions clang/test/AST/ast-dump-pragma-unroll.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ast-dump %s | FileCheck %s

using size_t = unsigned long long;

// CHECK: LoopHintAttr {{.*}} Implicit unroll UnrollCount Numeric
// CHECK: LoopHintAttr {{.*}} Implicit unroll UnrollCount Numeric
// CHECK: LoopHintAttr {{.*}} Implicit unroll Unroll Disable
// CHECK: LoopHintAttr {{.*}} Implicit unroll Unroll Disable
template <bool Flag>
int value_dependent(int n) {
constexpr int N = 100;
auto init = [=]() { return Flag ? n : 0UL; };
auto cond = [=](size_t ix) { return Flag ? ix != 0 : ix < 10; };
auto iter = [=](size_t ix) {
return Flag ? ix & ~(1ULL << __builtin_clzll(ix)) : ix + 1;
};

#pragma unroll Flag ? 1 : N
for (size_t ix = init(); cond(ix); ix = iter(ix)) {
n *= n;
}
#pragma unroll Flag ? 0 : N
for (size_t ix = init(); cond(ix); ix = iter(ix)) {
n *= n;
}
return n;
}

void test_value_dependent(int n) {
value_dependent<true>(n);
}
30 changes: 30 additions & 0 deletions clang/test/CodeGenCXX/pragma-gcc-unroll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,34 @@ void while_unroll_zero_test(int *List, int Length) {
}
}

using size_t = unsigned long long;

template <bool Flag>
int value_dependent(int n) {
// CHECK: define {{.*}} @_Z15value_dependentILb1EEii
constexpr int N = 100;
auto init = [=]() { return Flag ? n : 0UL; };
auto cond = [=](size_t ix) { return Flag ? ix != 0 : ix < 10; };
auto iter = [=](size_t ix) {
return Flag ? ix & ~(1ULL << __builtin_clzll(ix)) : ix + 1;
};
#pragma GCC unroll Flag ? 1 : N
for (size_t ix = init(); cond(ix); ix = iter(ix)) {
// CHECK: br label {{.*}}, !llvm.loop ![[LOOP_16:.*]]
n *= n;
}
#pragma GCC unroll Flag ? 0 : N
for (size_t ix = init(); cond(ix); ix = iter(ix)) {
// CHECK: br label {{.*}}, !llvm.loop ![[LOOP_17:.*]]
n *= n;
}
return n;
}

void test_value_dependent(int n) {
value_dependent<true>(n);
}

// CHECK: ![[LOOP_1]] = distinct !{![[LOOP_1]], [[MP:![0-9]+]], ![[UNROLL_ENABLE:.*]]}
// CHECK: ![[UNROLL_ENABLE]] = !{!"llvm.loop.unroll.enable"}
// CHECK: ![[LOOP_2]] = distinct !{![[LOOP_2:.*]], ![[UNROLL_DISABLE:.*]]}
Expand All @@ -129,3 +157,5 @@ void while_unroll_zero_test(int *List, int Length) {
// CHECK: ![[LOOP_7]] = distinct !{![[LOOP_7]], ![[UNROLL_8:.*]]}
// CHECK: ![[LOOP_14]] = distinct !{![[LOOP_14]], [[MP]], ![[UNROLL_DISABLE:.*]]}
// CHECK: ![[LOOP_15]] = distinct !{![[LOOP_15]], [[MP]], ![[UNROLL_DISABLE:.*]]}
// CHECK: ![[LOOP_16]] = distinct !{![[LOOP_16]], [[MP]], ![[UNROLL_DISABLE:.*]]}
// CHECK: ![[LOOP_17]] = distinct !{![[LOOP_17]], [[MP]], ![[UNROLL_DISABLE:.*]]}
52 changes: 52 additions & 0 deletions clang/test/CodeGenCXX/pragma-unroll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,54 @@ void template_test(double *List, int Length) {
for_template_define_test<double>(List, Length, Value);
}

void for_unroll_zero_test(int *List, int Length) {
// CHECK: define {{.*}} @_Z20for_unroll_zero_testPii
#pragma unroll 0
for (int i = 0; i < Length; i++) {
// CHECK: br label {{.*}}, !llvm.loop ![[LOOP_14:.*]]
List[i] = i * 2;
}
}

void while_unroll_zero_test(int *List, int Length) {
// CHECK: define {{.*}} @_Z22while_unroll_zero_testPii
int i = 0;
#pragma unroll(0)
while (i < Length) {
// CHECK: br label {{.*}}, !llvm.loop ![[LOOP_15:.*]]
List[i] = i * 2;
i++;
}
}

using size_t = unsigned long long;

template <bool Flag>
int value_dependent(int n) {
// CHECK: define {{.*}} @_Z15value_dependentILb1EEii
constexpr int N = 100;
auto init = [=]() { return Flag ? n : 0UL; };
auto cond = [=](size_t ix) { return Flag ? ix != 0 : ix < 10; };
auto iter = [=](size_t ix) {
return Flag ? ix & ~(1ULL << __builtin_clzll(ix)) : ix + 1;
};
#pragma unroll Flag ? 1 : N
for (size_t ix = init(); cond(ix); ix = iter(ix)) {
// CHECK: br label {{.*}}, !llvm.loop ![[LOOP_16:.*]]
n *= n;
}
#pragma unroll Flag ? 0 : N
for (size_t ix = init(); cond(ix); ix = iter(ix)) {
// CHECK: br label {{.*}}, !llvm.loop ![[LOOP_17:.*]]
n *= n;
}
return n;
}

void test_value_dependent(int n) {
value_dependent<true>(n);
}

// CHECK: ![[LOOP_1]] = distinct !{![[LOOP_1]], [[MP:![0-9]+]], ![[UNROLL_ENABLE:.*]]}
// CHECK: ![[UNROLL_ENABLE]] = !{!"llvm.loop.unroll.enable"}
// CHECK: ![[LOOP_2]] = distinct !{![[LOOP_2:.*]], ![[UNROLL_DISABLE:.*]]}
Expand All @@ -107,3 +155,7 @@ void template_test(double *List, int Length) {
// CHECK: ![[LOOP_5]] = distinct !{![[LOOP_5]], ![[UNROLL_8:.*]]}
// CHECK: ![[LOOP_6]] = distinct !{![[LOOP_6]], ![[UNROLL_8:.*]]}
// CHECK: ![[LOOP_7]] = distinct !{![[LOOP_7]], ![[UNROLL_8:.*]]}
// CHECK: ![[LOOP_14]] = distinct !{![[LOOP_14]], [[MP]], ![[UNROLL_DISABLE:.*]]}
// CHECK: ![[LOOP_15]] = distinct !{![[LOOP_15]], [[MP]], ![[UNROLL_DISABLE:.*]]}
// CHECK: ![[LOOP_16]] = distinct !{![[LOOP_16]], [[MP]], ![[UNROLL_DISABLE:.*]]}
// CHECK: ![[LOOP_17]] = distinct !{![[LOOP_17]], [[MP]], ![[UNROLL_DISABLE:.*]]}
29 changes: 29 additions & 0 deletions clang/test/Parser/pragma-unroll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,32 @@ void test(int *List, int Length) {

#pragma unroll
/* expected-error {{expected statement}} */ }

using size_t = unsigned long long;

template <bool Flag>
int FailToBuild(int n) {
constexpr int N = 100;
auto init = [=]() { return Flag ? n : 0UL; };
auto cond = [=](size_t ix) { return Flag ? ix != 0 : ix < 10; };
auto iter = [=](size_t ix) {
return Flag ? ix & ~(1ULL << __builtin_clzll(ix)) : ix + 1;
};
#pragma unroll Flag ? 0 : N // Ok, allow 0.
for (size_t ix = init(); cond(ix); ix = iter(ix)) {
n *= n;
}
#pragma GCC unroll Flag ? 0 : N // Ok, allow 0.
for (size_t ix = init(); cond(ix); ix = iter(ix)) {
n *= n;
}
return n;
}

int foo(int n) {
return FailToBuild<true>(n);
}

int bar(int n) {
return FailToBuild<false>(n);
}