Skip to content

Commit e722976

Browse files
committed
I give up on trying to use reader/writer locks for recursive type refinement. Use a recursive mutex instead, which will (in theory) generate more contention, but is really
a much more natural fit for what's going on during recursive type refinement. llvm-svn: 74618
1 parent 503c370 commit e722976

File tree

1 file changed

+27
-71
lines changed

1 file changed

+27
-71
lines changed

llvm/lib/VMCore/Type.cpp

Lines changed: 27 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ AbstractTypeUser::~AbstractTypeUser() {}
4343
// Type Class Implementation
4444
//===----------------------------------------------------------------------===//
4545

46-
// Reader/writer lock used for guarding access to the type maps.
47-
static ManagedStatic<sys::SmartRWMutex<true> > TypeMapLock;
46+
// Lock used for guarding access to the type maps.
47+
static ManagedStatic<sys::SmartMutex<true> > TypeMapLock;
4848

4949
// Recursive lock used for guarding access to AbstractTypeUsers.
5050
// NOTE: The true template parameter means this will no-op when we're not in
@@ -1006,23 +1006,13 @@ const IntegerType *IntegerType::get(unsigned NumBits) {
10061006

10071007
// First, see if the type is already in the table, for which
10081008
// a reader lock suffices.
1009-
TypeMapLock->reader_acquire();
1009+
sys::SmartScopedLock<true> L(&*TypeMapLock);
10101010
ITy = IntegerTypes->get(IVT);
1011-
TypeMapLock->reader_release();
10121011

10131012
if (!ITy) {
1014-
// OK, not in the table, get a writer lock.
1015-
sys::SmartScopedWriter<true> Writer(&*TypeMapLock);
1016-
ITy = IntegerTypes->get(IVT);
1017-
1018-
// We need to _recheck_ the table in case someone
1019-
// put it in between when we released the reader lock
1020-
// and when we gained the writer lock!
1021-
if (!ITy) {
1022-
// Value not found. Derive a new type!
1023-
ITy = new IntegerType(NumBits);
1024-
IntegerTypes->add(IVT, ITy);
1025-
}
1013+
// Value not found. Derive a new type!
1014+
ITy = new IntegerType(NumBits);
1015+
IntegerTypes->add(IVT, ITy);
10261016
}
10271017
#ifdef DEBUG_MERGE_TYPES
10281018
DOUT << "Derived new type: " << *ITy << "\n";
@@ -1089,23 +1079,14 @@ FunctionType *FunctionType::get(const Type *ReturnType,
10891079
FunctionValType VT(ReturnType, Params, isVarArg);
10901080
FunctionType *FT = 0;
10911081

1092-
TypeMapLock->reader_acquire();
1082+
sys::SmartScopedLock<true> L(&*TypeMapLock);
10931083
FT = FunctionTypes->get(VT);
1094-
TypeMapLock->reader_release();
10951084

10961085
if (!FT) {
1097-
sys::SmartScopedWriter<true> Writer(&*TypeMapLock);
1098-
1099-
// Have to check again here, because it might have
1100-
// been inserted between when we release the reader
1101-
// lock and when we acquired the writer lock.
1102-
FT = FunctionTypes->get(VT);
1103-
if (!FT) {
1104-
FT = (FunctionType*) operator new(sizeof(FunctionType) +
1105-
sizeof(PATypeHandle)*(Params.size()+1));
1106-
new (FT) FunctionType(ReturnType, Params, isVarArg);
1107-
FunctionTypes->add(VT, FT);
1108-
}
1086+
FT = (FunctionType*) operator new(sizeof(FunctionType) +
1087+
sizeof(PATypeHandle)*(Params.size()+1));
1088+
new (FT) FunctionType(ReturnType, Params, isVarArg);
1089+
FunctionTypes->add(VT, FT);
11091090
}
11101091

11111092
#ifdef DEBUG_MERGE_TYPES
@@ -1148,19 +1129,12 @@ ArrayType *ArrayType::get(const Type *ElementType, uint64_t NumElements) {
11481129
ArrayValType AVT(ElementType, NumElements);
11491130
ArrayType *AT = 0;
11501131

1151-
TypeMapLock->reader_acquire();
1132+
sys::SmartScopedLock<true> L(&*TypeMapLock);
11521133
AT = ArrayTypes->get(AVT);
1153-
TypeMapLock->reader_release();
1154-
1134+
11551135
if (!AT) {
1156-
sys::SmartScopedWriter<true> Writer(&*TypeMapLock);
1157-
1158-
// Recheck. Might have changed between release and acquire.
1159-
AT = ArrayTypes->get(AVT);
1160-
if (!AT) {
1161-
// Value not found. Derive a new type!
1162-
ArrayTypes->add(AVT, AT = new ArrayType(ElementType, NumElements));
1163-
}
1136+
// Value not found. Derive a new type!
1137+
ArrayTypes->add(AVT, AT = new ArrayType(ElementType, NumElements));
11641138
}
11651139
#ifdef DEBUG_MERGE_TYPES
11661140
DOUT << "Derived new type: " << *AT << "\n";
@@ -1214,17 +1188,11 @@ VectorType *VectorType::get(const Type *ElementType, unsigned NumElements) {
12141188
VectorValType PVT(ElementType, NumElements);
12151189
VectorType *PT = 0;
12161190

1217-
TypeMapLock->reader_acquire();
1191+
sys::SmartScopedLock<true> L(&*TypeMapLock);
12181192
PT = VectorTypes->get(PVT);
1219-
TypeMapLock->reader_release();
12201193

12211194
if (!PT) {
1222-
sys::SmartScopedWriter<true> Writer(&*TypeMapLock);
1223-
PT = VectorTypes->get(PVT);
1224-
// Recheck. Might have changed between release and acquire.
1225-
if (!PT) {
1226-
VectorTypes->add(PVT, PT = new VectorType(ElementType, NumElements));
1227-
}
1195+
VectorTypes->add(PVT, PT = new VectorType(ElementType, NumElements));
12281196
}
12291197
#ifdef DEBUG_MERGE_TYPES
12301198
DOUT << "Derived new type: " << *PT << "\n";
@@ -1282,21 +1250,15 @@ StructType *StructType::get(const std::vector<const Type*> &ETypes,
12821250
StructValType STV(ETypes, isPacked);
12831251
StructType *ST = 0;
12841252

1285-
TypeMapLock->reader_acquire();
1253+
sys::SmartScopedLock<true> L(&*TypeMapLock);
12861254
ST = StructTypes->get(STV);
1287-
TypeMapLock->reader_release();
12881255

12891256
if (!ST) {
1290-
sys::SmartScopedWriter<true> Writer(&*TypeMapLock);
1291-
ST = StructTypes->get(STV);
1292-
// Recheck. Might have changed between release and acquire.
1293-
if (!ST) {
1294-
// Value not found. Derive a new type!
1295-
ST = (StructType*) operator new(sizeof(StructType) +
1296-
sizeof(PATypeHandle) * ETypes.size());
1297-
new (ST) StructType(ETypes, isPacked);
1298-
StructTypes->add(STV, ST);
1299-
}
1257+
// Value not found. Derive a new type!
1258+
ST = (StructType*) operator new(sizeof(StructType) +
1259+
sizeof(PATypeHandle) * ETypes.size());
1260+
new (ST) StructType(ETypes, isPacked);
1261+
StructTypes->add(STV, ST);
13001262
}
13011263
#ifdef DEBUG_MERGE_TYPES
13021264
DOUT << "Derived new type: " << *ST << "\n";
@@ -1367,18 +1329,12 @@ PointerType *PointerType::get(const Type *ValueType, unsigned AddressSpace) {
13671329

13681330
PointerType *PT = 0;
13691331

1370-
TypeMapLock->reader_acquire();
1332+
sys::SmartScopedLock<true> L(&*TypeMapLock);
13711333
PT = PointerTypes->get(PVT);
1372-
TypeMapLock->reader_release();
13731334

13741335
if (!PT) {
1375-
sys::SmartScopedWriter<true> Writer(&*TypeMapLock);
1376-
PT = PointerTypes->get(PVT);
1377-
// Recheck. Might have changed between release and acquire.
1378-
if (!PT) {
1379-
// Value not found. Derive a new type!
1380-
PointerTypes->add(PVT, PT = new PointerType(ValueType, AddressSpace));
1381-
}
1336+
// Value not found. Derive a new type!
1337+
PointerTypes->add(PVT, PT = new PointerType(ValueType, AddressSpace));
13821338
}
13831339
#ifdef DEBUG_MERGE_TYPES
13841340
DOUT << "Derived new type: " << *PT << "\n";
@@ -1532,7 +1488,7 @@ void DerivedType::unlockedRefineAbstractTypeTo(const Type *NewType) {
15321488
void DerivedType::refineAbstractTypeTo(const Type *NewType) {
15331489
// All recursive calls will go through unlockedRefineAbstractTypeTo,
15341490
// to avoid deadlock problems.
1535-
sys::SmartScopedWriter<true> Writer(&*TypeMapLock);
1491+
sys::SmartScopedLock<true> L(&*TypeMapLock);
15361492
unlockedRefineAbstractTypeTo(NewType);
15371493
}
15381494

0 commit comments

Comments
 (0)