Skip to content

Commit bdf147f

Browse files
committed
Merge 81316 from mainline.
Make TypeBuilder's result depend on the LLVMContext it's passed. TypeBuilder was using a local static variable to cache its result. This made it ignore changes in its LLVMContext argument and always return a type constructed from the argument to the first call. llvm-svn: 81694
1 parent 4414586 commit bdf147f

File tree

2 files changed

+24
-97
lines changed

2 files changed

+24
-97
lines changed

llvm/include/llvm/Support/TypeBuilder.h

Lines changed: 12 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,14 @@ namespace llvm {
5050
/// namespace llvm {
5151
/// template<bool xcompile> class TypeBuilder<MyType, xcompile> {
5252
/// public:
53-
/// static const StructType *get() {
54-
/// // Using the static result variable ensures that the type is
55-
/// // only looked up once.
56-
/// static const StructType *const result = StructType::get(
57-
/// TypeBuilder<types::i<32>, xcompile>::get(),
58-
/// TypeBuilder<types::i<32>*, xcompile>::get(),
59-
/// TypeBuilder<types::i<8>*[], xcompile>::get(),
53+
/// static const StructType *get(LLVMContext &Context) {
54+
/// // If you cache this result, be sure to cache it separately
55+
/// // for each LLVMContext.
56+
/// return StructType::get(
57+
/// TypeBuilder<types::i<32>, xcompile>::get(Context),
58+
/// TypeBuilder<types::i<32>*, xcompile>::get(Context),
59+
/// TypeBuilder<types::i<8>*[], xcompile>::get(Context),
6060
/// NULL);
61-
/// return result;
6261
/// }
6362
///
6463
/// // You may find this a convenient place to put some constants
@@ -72,9 +71,6 @@ namespace llvm {
7271
/// }
7372
/// } // namespace llvm
7473
///
75-
/// Using the static result variable ensures that the type is only looked up
76-
/// once.
77-
///
7874
/// TypeBuilder cannot handle recursive types or types you only know at runtime.
7975
/// If you try to give it a recursive type, it will deadlock, infinitely
8076
/// recurse, or throw a recursive_init exception.
@@ -106,9 +102,7 @@ template<typename T, bool cross> class TypeBuilder<const volatile T, cross>
106102
template<typename T, bool cross> class TypeBuilder<T*, cross> {
107103
public:
108104
static const PointerType *get(LLVMContext &Context) {
109-
static const PointerType *const result =
110-
PointerType::getUnqual(TypeBuilder<T,cross>::get(Context));
111-
return result;
105+
return PointerType::getUnqual(TypeBuilder<T,cross>::get(Context));
112106
}
113107
};
114108

@@ -119,18 +113,14 @@ template<typename T, bool cross> class TypeBuilder<T&, cross> {};
119113
template<typename T, size_t N, bool cross> class TypeBuilder<T[N], cross> {
120114
public:
121115
static const ArrayType *get(LLVMContext &Context) {
122-
static const ArrayType *const result =
123-
ArrayType::get(TypeBuilder<T, cross>::get(Context), N);
124-
return result;
116+
return ArrayType::get(TypeBuilder<T, cross>::get(Context), N);
125117
}
126118
};
127119
/// LLVM uses an array of length 0 to represent an unknown-length array.
128120
template<typename T, bool cross> class TypeBuilder<T[], cross> {
129121
public:
130122
static const ArrayType *get(LLVMContext &Context) {
131-
static const ArrayType *const result =
132-
ArrayType::get(TypeBuilder<T, cross>::get(Context), 0);
133-
return result;
123+
return ArrayType::get(TypeBuilder<T, cross>::get(Context), 0);
134124
}
135125
};
136126

@@ -160,9 +150,7 @@ template<typename T, bool cross> class TypeBuilder<T[], cross> {
160150
template<> class TypeBuilder<T, false> { \
161151
public: \
162152
static const IntegerType *get(LLVMContext &Context) { \
163-
static const IntegerType *const result = \
164-
IntegerType::get(Context, sizeof(T) * CHAR_BIT); \
165-
return result; \
153+
return IntegerType::get(Context, sizeof(T) * CHAR_BIT); \
166154
} \
167155
}; \
168156
template<> class TypeBuilder<T, true> { \
@@ -191,8 +179,7 @@ template<uint32_t num_bits, bool cross>
191179
class TypeBuilder<types::i<num_bits>, cross> {
192180
public:
193181
static const IntegerType *get(LLVMContext &C) {
194-
static const IntegerType *const result = IntegerType::get(C, num_bits);
195-
return result;
182+
return IntegerType::get(C, num_bits);
196183
}
197184
};
198185

@@ -248,24 +235,12 @@ template<> class TypeBuilder<void*, false>
248235
template<typename R, bool cross> class TypeBuilder<R(), cross> {
249236
public:
250237
static const FunctionType *get(LLVMContext &Context) {
251-
static const FunctionType *const result = create(Context);
252-
return result;
253-
}
254-
255-
private:
256-
static const FunctionType *create(LLVMContext &Context) {
257238
return FunctionType::get(TypeBuilder<R, cross>::get(Context), false);
258239
}
259240
};
260241
template<typename R, typename A1, bool cross> class TypeBuilder<R(A1), cross> {
261242
public:
262243
static const FunctionType *get(LLVMContext &Context) {
263-
static const FunctionType *const result = create(Context);
264-
return result;
265-
}
266-
267-
private:
268-
static const FunctionType *create(LLVMContext &Context) {
269244
std::vector<const Type*> params;
270245
params.reserve(1);
271246
params.push_back(TypeBuilder<A1, cross>::get(Context));
@@ -277,12 +252,6 @@ template<typename R, typename A1, typename A2, bool cross>
277252
class TypeBuilder<R(A1, A2), cross> {
278253
public:
279254
static const FunctionType *get(LLVMContext &Context) {
280-
static const FunctionType *const result = create(Context);
281-
return result;
282-
}
283-
284-
private:
285-
static const FunctionType *create(LLVMContext &Context) {
286255
std::vector<const Type*> params;
287256
params.reserve(2);
288257
params.push_back(TypeBuilder<A1, cross>::get(Context));
@@ -295,12 +264,6 @@ template<typename R, typename A1, typename A2, typename A3, bool cross>
295264
class TypeBuilder<R(A1, A2, A3), cross> {
296265
public:
297266
static const FunctionType *get(LLVMContext &Context) {
298-
static const FunctionType *const result = create(Context);
299-
return result;
300-
}
301-
302-
private:
303-
static const FunctionType *create(LLVMContext &Context) {
304267
std::vector<const Type*> params;
305268
params.reserve(3);
306269
params.push_back(TypeBuilder<A1, cross>::get(Context));
@@ -316,12 +279,6 @@ template<typename R, typename A1, typename A2, typename A3, typename A4,
316279
class TypeBuilder<R(A1, A2, A3, A4), cross> {
317280
public:
318281
static const FunctionType *get(LLVMContext &Context) {
319-
static const FunctionType *const result = create(Context);
320-
return result;
321-
}
322-
323-
private:
324-
static const FunctionType *create(LLVMContext &Context) {
325282
std::vector<const Type*> params;
326283
params.reserve(4);
327284
params.push_back(TypeBuilder<A1, cross>::get(Context));
@@ -338,12 +295,6 @@ template<typename R, typename A1, typename A2, typename A3, typename A4,
338295
class TypeBuilder<R(A1, A2, A3, A4, A5), cross> {
339296
public:
340297
static const FunctionType *get(LLVMContext &Context) {
341-
static const FunctionType *const result = create(Context);
342-
return result;
343-
}
344-
345-
private:
346-
static const FunctionType *create(LLVMContext &Context) {
347298
std::vector<const Type*> params;
348299
params.reserve(5);
349300
params.push_back(TypeBuilder<A1, cross>::get(Context));
@@ -359,25 +310,13 @@ class TypeBuilder<R(A1, A2, A3, A4, A5), cross> {
359310
template<typename R, bool cross> class TypeBuilder<R(...), cross> {
360311
public:
361312
static const FunctionType *get(LLVMContext &Context) {
362-
static const FunctionType *const result = create(Context);
363-
return result;
364-
}
365-
366-
private:
367-
static const FunctionType *create(LLVMContext &Context) {
368313
return FunctionType::get(TypeBuilder<R, cross>::get(Context), true);
369314
}
370315
};
371316
template<typename R, typename A1, bool cross>
372317
class TypeBuilder<R(A1, ...), cross> {
373318
public:
374319
static const FunctionType *get(LLVMContext &Context) {
375-
static const FunctionType *const result = create(Context);
376-
return result;
377-
}
378-
379-
private:
380-
static const FunctionType *create(LLVMContext &Context) {
381320
std::vector<const Type*> params;
382321
params.reserve(1);
383322
params.push_back(TypeBuilder<A1, cross>::get(Context));
@@ -388,12 +327,6 @@ template<typename R, typename A1, typename A2, bool cross>
388327
class TypeBuilder<R(A1, A2, ...), cross> {
389328
public:
390329
static const FunctionType *get(LLVMContext &Context) {
391-
static const FunctionType *const result = create(Context);
392-
return result;
393-
}
394-
395-
private:
396-
static const FunctionType *create(LLVMContext &Context) {
397330
std::vector<const Type*> params;
398331
params.reserve(2);
399332
params.push_back(TypeBuilder<A1, cross>::get(Context));
@@ -406,12 +339,6 @@ template<typename R, typename A1, typename A2, typename A3, bool cross>
406339
class TypeBuilder<R(A1, A2, A3, ...), cross> {
407340
public:
408341
static const FunctionType *get(LLVMContext &Context) {
409-
static const FunctionType *const result = create(Context);
410-
return result;
411-
}
412-
413-
private:
414-
static const FunctionType *create(LLVMContext &Context) {
415342
std::vector<const Type*> params;
416343
params.reserve(3);
417344
params.push_back(TypeBuilder<A1, cross>::get(Context));
@@ -427,12 +354,6 @@ template<typename R, typename A1, typename A2, typename A3, typename A4,
427354
class TypeBuilder<R(A1, A2, A3, A4, ...), cross> {
428355
public:
429356
static const FunctionType *get(LLVMContext &Context) {
430-
static const FunctionType *const result = create(Context);
431-
return result;
432-
}
433-
434-
private:
435-
static const FunctionType *create(LLVMContext &Context) {
436357
std::vector<const Type*> params;
437358
params.reserve(4);
438359
params.push_back(TypeBuilder<A1, cross>::get(Context));
@@ -449,12 +370,6 @@ template<typename R, typename A1, typename A2, typename A3, typename A4,
449370
class TypeBuilder<R(A1, A2, A3, A4, A5, ...), cross> {
450371
public:
451372
static const FunctionType *get(LLVMContext &Context) {
452-
static const FunctionType *const result = create(Context);
453-
return result;
454-
}
455-
456-
private:
457-
static const FunctionType *create(LLVMContext &Context) {
458373
std::vector<const Type*> params;
459374
params.reserve(5);
460375
params.push_back(TypeBuilder<A1, cross>::get(Context));

llvm/unittests/Support/TypeBuilderTest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,18 @@ TEST(TypeBuilderTest, Functions) {
147147
false>::get(getGlobalContext())));
148148
}
149149

150+
TEST(TypeBuilderTest, Context) {
151+
// We used to cache TypeBuilder results in static local variables. This
152+
// produced the same type for different contexts, which of course broke
153+
// things.
154+
LLVMContext context1;
155+
EXPECT_EQ(&context1,
156+
&(TypeBuilder<types::i<1>, true>::get(context1))->getContext());
157+
LLVMContext context2;
158+
EXPECT_EQ(&context2,
159+
&(TypeBuilder<types::i<1>, true>::get(context2))->getContext());
160+
}
161+
150162
class MyType {
151163
int a;
152164
int *b;

0 commit comments

Comments
 (0)