llvm-project/clang/test/Parser/pragma-unroll.cpp
Mark Heffernan 397a98d86d Add new llvm.loop.unroll.enable metadata for use with "#pragma unroll".
This change adds the new unroll metadata "llvm.loop.unroll.enable" which directs
the optimizer to unroll a loop fully if the trip count is known at compile time, and
unroll partially if the trip count is not known at compile time. This differs from
"llvm.loop.unroll.full" which explicitly does not unroll a loop if the trip count is not
known at compile time

With this change "#pragma unroll" generates "llvm.loop.unroll.enable" rather than
"llvm.loop.unroll.full" metadata. This changes the semantics of "#pragma unroll" slightly
to mean "unroll aggressively (fully or partially)" rather than "unroll fully or not at all".

The motivating example for this change was some internal code with a loop marked
with "#pragma unroll" which only sometimes had a compile-time trip count depending
on template magic. When the trip count was a compile-time constant, everything works
as expected and the loop is fully unrolled. However, when the trip count was not a
compile-time constant the "#pragma unroll" explicitly disabled unrolling of the loop(!).
Removing "#pragma unroll" caused the loop to be unrolled partially which was desirable
from a performance perspective.

llvm-svn: 244467
2015-08-10 17:29:39 +00:00

114 lines
3.3 KiB
C++

// RUN: %clang_cc1 -std=c++11 -verify %s
// Note that this puts the expected lines before the directives to work around
// limitations in the -verify mode.
void test(int *List, int Length) {
int i = 0;
#pragma unroll
while (i + 1 < Length) {
List[i] = i;
}
#pragma nounroll
while (i < Length) {
List[i] = i;
}
#pragma unroll 4
while (i - 1 < Length) {
List[i] = i;
}
#pragma unroll(8)
while (i - 2 < Length) {
List[i] = i;
}
/* expected-error {{expected ')'}} */ #pragma unroll(4
/* expected-error {{missing argument; expected an integer value}} */ #pragma unroll()
/* expected-warning {{extra tokens at end of '#pragma unroll'}} */ #pragma unroll 1 2
while (i-6 < Length) {
List[i] = i;
}
/* expected-warning {{extra tokens at end of '#pragma nounroll'}} */ #pragma nounroll 1
while (i-7 < Length) {
List[i] = i;
}
/* expected-error {{expected ')'}} */ #pragma unroll(()
/* expected-error {{expected expression}} */ #pragma unroll -
/* expected-error {{invalid value '0'; must be positive}} */ #pragma unroll(0)
/* expected-error {{invalid value '0'; must be positive}} */ #pragma unroll 0
/* expected-error {{value '3000000000' is too large}} */ #pragma unroll(3000000000)
/* expected-error {{value '3000000000' is too large}} */ #pragma unroll 3000000000
while (i-8 < Length) {
List[i] = i;
}
#pragma unroll
/* expected-error {{expected a for, while, or do-while loop to follow '#pragma unroll'}} */ int j = Length;
#pragma unroll 4
/* expected-error {{expected a for, while, or do-while loop to follow '#pragma unroll'}} */ int k = Length;
#pragma nounroll
/* expected-error {{expected a for, while, or do-while loop to follow '#pragma nounroll'}} */ int l = Length;
/* expected-error {{incompatible directives 'unroll(disable)' and '#pragma unroll(4)'}} */ #pragma unroll 4
#pragma clang loop unroll(disable)
while (i-10 < Length) {
List[i] = i;
}
/* expected-error {{incompatible directives 'unroll(full)' and '#pragma unroll(4)'}} */ #pragma unroll(4)
#pragma clang loop unroll(full)
while (i-11 < Length) {
List[i] = i;
}
/* expected-error {{incompatible directives 'unroll(enable)' and '#pragma unroll(4)'}} */ #pragma unroll(4)
#pragma clang loop unroll(enable)
while (i-11 < Length) {
List[i] = i;
}
/* expected-error {{incompatible directives '#pragma unroll' and '#pragma unroll(4)'}} */ #pragma unroll(4)
#pragma unroll
while (i-11 < Length) {
List[i] = i;
}
/* expected-error {{incompatible directives '#pragma nounroll' and 'unroll_count(4)'}} */ #pragma clang loop unroll_count(4)
#pragma nounroll
while (i-12 < Length) {
List[i] = i;
}
/* expected-error {{duplicate directives '#pragma nounroll' and '#pragma nounroll'}} */ #pragma nounroll
#pragma nounroll
while (i-13 < Length) {
List[i] = i;
}
/* expected-error {{duplicate directives '#pragma unroll' and '#pragma unroll'}} */ #pragma unroll
#pragma unroll
while (i-14 < Length) {
List[i] = i;
}
/* expected-error {{duplicate directives 'unroll(full)' and '#pragma unroll'}} */ #pragma unroll
#pragma clang loop unroll(full)
while (i-15 < Length) {
List[i] = i;
}
/* expected-error {{duplicate directives '#pragma unroll(4)' and '#pragma unroll(4)'}} */ #pragma unroll 4
#pragma unroll(4)
while (i-16 < Length) {
List[i] = i;
}
#pragma unroll
/* expected-error {{expected statement}} */ }