Skip to content

Commit 970094d

Browse files
authored
[llvm-opt-report] Show scalable vectorization factors (#123367)
Scalable vectorization factors are printed as "vscale x VF" where VF is the known minimum number of elements, a integer. Currently, llvm-opt-report always expects a integer (like for vectorization with fixed-sized vectors), and does not display any vectorization factor in the output (just 'V', but without a number). This patch adds support for scalable vectorization factors and prints them as "VNx<VF>", so for example "VNx4". The "Nx" is used to differentiate between fixed-sized and scalable factors, and is consistent with the way LLVM mangles scalable vectors in other places.
1 parent 11b0401 commit 970094d

File tree

4 files changed

+65
-12
lines changed

4 files changed

+65
-12
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <stddef.h>
2+
3+
void foo(size_t N, float A[restrict N], float B[N]) {
4+
#pragma clang loop vectorize_width(4, scalable)
5+
for (size_t i = 0; i < N; i++) {
6+
A[i] = B[i] * 42.f;
7+
}
8+
}
9+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--- !Passed
2+
Pass: loop-vectorize
3+
Name: Vectorized
4+
DebugLoc: { File: './Inputs/scalable.c', Line: 5, Column: 3 }
5+
Function: foo
6+
Args:
7+
- String: 'vectorized loop (vectorization width: '
8+
- VectorizationFactor: vscale x 4
9+
- String: ', interleaved count: '
10+
- InterleaveCount: '2'
11+
- String: ')'
12+
...
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
RUN: llvm-opt-report -r %p %p/Inputs/scalable.yaml | FileCheck -strict-whitespace %s
2+
3+
; CHECK: < {{.*[/\]}}scalable.c
4+
; CHECK-NEXT: 1 | #include <stddef.h>
5+
; CHECK-NEXT: 2 |
6+
; CHECK-NEXT: 3 | void foo(size_t N, float A[restrict N], float B[N]) {
7+
; CHECK-NEXT: 4 | #pragma clang loop vectorize_width(4, scalable)
8+
; CHECK-NEXT: 5 VNx4,2 | for (size_t i = 0; i < N; i++) {
9+
; CHECK-NEXT: 6 | A[i] = B[i] * 42.f;
10+
; CHECK-NEXT: 7 | }
11+
; CHECK-NEXT: 8 | }
12+
; CHECK-NEXT: 9 |

llvm/tools/llvm-opt-report/OptReport.cpp

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "llvm/Support/MemoryBuffer.h"
3030
#include "llvm/Support/Path.h"
3131
#include "llvm/Support/Program.h"
32+
#include "llvm/Support/TypeSize.h"
3233
#include "llvm/Support/WithColor.h"
3334
#include "llvm/Support/raw_ostream.h"
3435
#include <cstdlib>
@@ -100,7 +101,7 @@ struct OptReportLocationInfo {
100101
OptReportLocationItemInfo Unrolled;
101102
OptReportLocationItemInfo Vectorized;
102103

103-
int VectorizationFactor = 1;
104+
ElementCount VectorizationFactor = ElementCount::getFixed(1);
104105
int InterleaveCount = 1;
105106
int UnrollCount = 1;
106107

@@ -109,8 +110,9 @@ struct OptReportLocationInfo {
109110
Unrolled |= RHS.Unrolled;
110111
Vectorized |= RHS.Vectorized;
111112

112-
VectorizationFactor =
113-
std::max(VectorizationFactor, RHS.VectorizationFactor);
113+
if (ElementCount::isKnownLT(VectorizationFactor, RHS.VectorizationFactor))
114+
VectorizationFactor = RHS.VectorizationFactor;
115+
114116
InterleaveCount = std::max(InterleaveCount, RHS.InterleaveCount);
115117
UnrollCount = std::max(UnrollCount, RHS.UnrollCount);
116118

@@ -130,9 +132,11 @@ struct OptReportLocationInfo {
130132
return true;
131133
else if (RHS.Vectorized < Vectorized || Succinct)
132134
return false;
133-
else if (VectorizationFactor < RHS.VectorizationFactor)
135+
else if (ElementCount::isKnownLT(VectorizationFactor,
136+
RHS.VectorizationFactor))
134137
return true;
135-
else if (VectorizationFactor > RHS.VectorizationFactor)
138+
else if (ElementCount::isKnownGT(VectorizationFactor,
139+
RHS.VectorizationFactor))
136140
return false;
137141
else if (InterleaveCount < RHS.InterleaveCount)
138142
return true;
@@ -197,17 +201,26 @@ static bool readLocationInfo(LocationInfoTy &LocationInfo) {
197201

198202
bool Transformed = Remark.RemarkType == remarks::Type::Passed;
199203

200-
int VectorizationFactor = 1;
204+
ElementCount VectorizationFactor = ElementCount::getFixed(1);
201205
int InterleaveCount = 1;
202206
int UnrollCount = 1;
203207

204208
for (const remarks::Argument &Arg : Remark.Args) {
205-
if (Arg.Key == "VectorizationFactor")
206-
Arg.Val.getAsInteger(10, VectorizationFactor);
207-
else if (Arg.Key == "InterleaveCount")
209+
if (Arg.Key == "VectorizationFactor") {
210+
int MinValue = 1;
211+
bool IsScalable = false;
212+
if (Arg.Val.starts_with("vscale x ")) {
213+
Arg.Val.drop_front(9).getAsInteger(10, MinValue);
214+
IsScalable = true;
215+
} else {
216+
Arg.Val.getAsInteger(10, MinValue);
217+
}
218+
VectorizationFactor = ElementCount::get(MinValue, IsScalable);
219+
} else if (Arg.Key == "InterleaveCount") {
208220
Arg.Val.getAsInteger(10, InterleaveCount);
209-
else if (Arg.Key == "UnrollCount")
221+
} else if (Arg.Key == "UnrollCount") {
210222
Arg.Val.getAsInteger(10, UnrollCount);
223+
}
211224
}
212225

213226
const std::optional<remarks::RemarkLocation> &Loc = Remark.Loc;
@@ -292,7 +305,11 @@ static bool writeReport(LocationInfoTy &LocationInfo) {
292305
bool NothingUnrolled = !MaxLI.Unrolled.Transformed;
293306
bool NothingVectorized = !MaxLI.Vectorized.Transformed;
294307

295-
unsigned VFDigits = llvm::utostr(MaxLI.VectorizationFactor).size();
308+
unsigned VFDigits =
309+
llvm::utostr(MaxLI.VectorizationFactor.getKnownMinValue()).size();
310+
if (MaxLI.VectorizationFactor.isScalable())
311+
VFDigits += 2; // For "Nx..."
312+
296313
unsigned ICDigits = llvm::utostr(MaxLI.InterleaveCount).size();
297314
unsigned UCDigits = llvm::utostr(MaxLI.UnrollCount).size();
298315

@@ -382,7 +399,10 @@ static bool writeReport(LocationInfoTy &LocationInfo) {
382399
raw_string_ostream RS(R);
383400

384401
if (!Succinct) {
385-
RS << LLI.VectorizationFactor << "," << LLI.InterleaveCount;
402+
if (LLI.VectorizationFactor.isScalable())
403+
RS << "Nx";
404+
RS << LLI.VectorizationFactor.getKnownMinValue() << ","
405+
<< LLI.InterleaveCount;
386406
RS << std::string(VFDigits + ICDigits + 1 - R.size(), ' ');
387407
}
388408

0 commit comments

Comments
 (0)