Skip to content

Commit 475ceca

Browse files
authored
[clang] Increase VecLib bitfield size to 4 bits in CodeGenOptions.def (#108804)
Summary: This PR fixes the issue where the VecLib bitfield in CodeGenOptions.def is too small to accommodate the increasing number of vector libraries. Specifically, the bitfield size was previously set to 3, but with the introduction of more vector libraries (currently 9), the bitfield needed to be expanded to avoid potential issues in vectorization. In this PR, I have increased the size of the VecLib bitfield from 3 to 4 to account for the additional libraries. This ensures that all 9 vector libraries are correctly encoded and available for use without errors. Changes Made: Modified: Increased the VecLib bitfield size from 3 to 4 in clang/include/clang/Basic/CodeGenOptions.def. Motivation: This change is necessary to ensure that all vector libraries are properly represented and selectable. The current limitation of the VecLib bitfield size was causing some vectorization opportunities to be lost when more than 3 bits were needed to represent the library options. Closes: Fixes #108704
1 parent 67518a4 commit 475ceca

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

clang/include/clang/Basic/CodeGenOptions.def

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,18 @@ ENUM_CODEGENOPT(Inlining, InliningMethod, 2, NormalInlining)
375375
/// The maximum stack size a function can have to be considered for inlining.
376376
VALUE_CODEGENOPT(InlineMaxStackSize, 32, UINT_MAX)
377377

378-
// Vector functions library to use.
379-
ENUM_CODEGENOPT(VecLib, llvm::driver::VectorLibrary, 3, llvm::driver::VectorLibrary::NoLibrary)
378+
// Define the number of bits required for the VecLib enum
379+
#define VECLIB_BIT_COUNT (llvm::countPopulation(llvm::driver::VectorLibrary::MaxLibrary))
380+
381+
// Ensure the VecLib bitfield has enough space for future vector libraries.
382+
// The number of bits is determined automatically based on the number of enum values.
383+
static_assert(static_cast<size_t>(llvm::driver::VectorLibrary::MaxLibrary) <= (1 << VECLIB_BIT_COUNT),
384+
"VecLib bitfield size is too small to accommodate all vector libraries.");
385+
386+
// VecLib definition in CodeGenOptions.def
387+
ENUM_CODEGENOPT(VecLib, llvm::driver::VectorLibrary, VECLIB_BIT_COUNT, llvm::driver::VectorLibrary::NoLibrary)
388+
389+
#undef VECLIB_BIT_COUNT
380390

381391
/// The default TLS model to use.
382392
ENUM_CODEGENOPT(DefaultTLSModel, TLSModel, 2, GeneralDynamicTLSModel)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "clang/Basic/CodeGenOptions.h"
2+
#include "llvm/Driver/Options.h"
3+
#include "gtest/gtest.h"
4+
5+
TEST(VecLibBitfieldTest, AllLibrariesFit) {
6+
// We expect that all vector libraries fit in the bitfield size
7+
EXPECT_LE(static_cast<size_t>(llvm::driver::VectorLibrary::MaxLibrary),
8+
(1 << VECLIB_BIT_COUNT))
9+
<< "VecLib bitfield size is too small!";
10+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
TEST(VecLibBitfieldTest, EncodingDecodingTest) {
2+
clang::CodeGenOptions Opts;
3+
4+
// Test encoding and decoding for each vector library
5+
for (int i = static_cast<int>(llvm::driver::VectorLibrary::Accelerate);
6+
i <= static_cast<int>(llvm::driver::VectorLibrary::MaxLibrary); ++i) {
7+
8+
Opts.VecLib = static_cast<llvm::driver::VectorLibrary>(i);
9+
10+
// Encode and then decode
11+
llvm::driver::VectorLibrary decodedValue =
12+
static_cast<llvm::driver::VectorLibrary>(Opts.VecLib);
13+
14+
EXPECT_EQ(decodedValue, Opts.VecLib)
15+
<< "Encoding/Decoding failed for vector library " << i;
16+
}
17+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Simulate the addition of a new library without increasing the bitfield size
2+
enum class SimulatedVectorLibrary {
3+
Accelerate = 0,
4+
LIBMVEC,
5+
MASSV,
6+
SVML,
7+
SLEEF,
8+
Darwin_libsystem_m,
9+
ArmPL,
10+
AMDLIBM,
11+
NoLibrary,
12+
// Simulate new addition
13+
NewLibrary,
14+
MaxLibrary
15+
};
16+
17+
#define SIMULATED_VECLIB_BIT_COUNT \
18+
4 // The current bitfield size (should be 4 for 9 options)
19+
20+
TEST(VecLibBitfieldTest, SimulatedOverflowTest) {
21+
// Simulate the addition of a new library and check if the bitfield size is
22+
// sufficient
23+
EXPECT_LE(static_cast<size_t>(SimulatedVectorLibrary::MaxLibrary),
24+
(1 << SIMULATED_VECLIB_BIT_COUNT))
25+
<< "Simulated VecLib bitfield size overflow!";
26+
}

0 commit comments

Comments
 (0)