Skip to content

Commit c3f2e19

Browse files
committed
Fix AttrSet struct for LLVM 3.1 and 3.2.
Use a map instead of a vector as the attribute index may get as high as 0xFFFFFFFF (LLVM 3.1), apparently for function attributes.
1 parent 7bbcdc1 commit c3f2e19

File tree

4 files changed

+51
-41
lines changed

4 files changed

+51
-41
lines changed

gen/attributes.cpp

+29-21
Original file line numberDiff line numberDiff line change
@@ -60,48 +60,56 @@ AttrBuilder& AttrBuilder::remove(A attribute) {
6060
}
6161

6262

63-
void AttrSet::reserve(size_t length) {
64-
entries.reserve(length);
65-
}
66-
67-
AttrSet& AttrSet::add(size_t index, const AttrBuilder& builder) {
68-
if (builder.hasAttributes()) {
69-
if (index >= entries.size())
70-
entries.resize(index + 1);
63+
AttrSet& AttrSet::add(unsigned index, AttrBuilder builder) {
64+
if (builder.hasAttributes())
7165
entries[index] = builder;
72-
}
7366
return *this;
7467
}
7568

7669
#if LDC_LLVM_VER >= 303
7770

7871
llvm::AttributeSet AttrSet::toNativeSet() const {
7972
llvm::AttributeSet set;
80-
for (size_t i = 0; i < entries.size(); ++i) {
81-
if (entries[i].hasAttributes()) {
82-
AttrBuilder a = entries[i];
83-
set = set.addAttributes(gIR->context(), i, llvm::AttributeSet::get(gIR->context(), i, a.attrs));
84-
}
73+
74+
typedef std::map<unsigned, AttrBuilder>::const_iterator I;
75+
for (I it = entries.begin(); it != entries.end(); ++it) {
76+
unsigned index = it->first;
77+
AttrBuilder builder = it->second;
78+
if (!builder.hasAttributes())
79+
continue;
80+
81+
llvm::AttributeSet as = llvm::AttributeSet::get(gIR->context(),
82+
index, builder.attrs);
83+
set = set.addAttributes(gIR->context(), index, as);
8584
}
85+
8686
return set;
8787
}
8888

8989
#else
9090

9191
llvm::AttrListPtr AttrSet::toNativeSet() const {
92+
if (entries.empty())
93+
return llvm::AttrListPtr();
94+
9295
std::vector<llvm::AttributeWithIndex> attrsWithIndex;
9396
attrsWithIndex.reserve(entries.size());
94-
for (size_t i = 0; i < entries.size(); ++i) {
95-
if (entries[i].hasAttributes()) {
97+
98+
typedef std::map<unsigned, AttrBuilder>::const_iterator I;
99+
for (I it = entries.begin(); it != entries.end(); ++it) {
100+
unsigned index = it->first;
101+
AttrBuilder builder = it->second;
102+
if (!builder.hasAttributes())
103+
continue;
104+
96105
#if LDC_LLVM_VER == 302
97-
AttrBuilder a = entries[i];
98-
attrsWithIndex.push_back(llvm::AttributeWithIndex::get(i,
99-
llvm::Attributes::get(gIR->context(), a.attrs)));
106+
attrsWithIndex.push_back(llvm::AttributeWithIndex::get(index,
107+
llvm::Attributes::get(gIR->context(), builder.attrs)));
100108
#else
101-
attrsWithIndex.push_back(llvm::AttributeWithIndex::get(i, entries[i].attrs));
109+
attrsWithIndex.push_back(llvm::AttributeWithIndex::get(index, builder.attrs));
102110
#endif
103-
}
104111
}
112+
105113
#if LDC_LLVM_VER == 302
106114
return llvm::AttrListPtr::get(gIR->context(), llvm::ArrayRef<llvm::AttributeWithIndex>(attrsWithIndex));
107115
#else

gen/attributes.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#include "gen/llvm.h"
1414

15-
#include <vector>
15+
#include <map>
1616

1717
struct AttrBuilder
1818
{
@@ -42,10 +42,9 @@ struct AttrBuilder
4242

4343
struct AttrSet
4444
{
45-
std::vector<AttrBuilder> entries;
45+
std::map<unsigned, AttrBuilder> entries;
4646

47-
void reserve(size_t length);
48-
AttrSet& add(size_t index, const AttrBuilder& builder);
47+
AttrSet& add(unsigned index, AttrBuilder builder);
4948

5049
#if LDC_LLVM_VER >= 303
5150
llvm::AttributeSet toNativeSet() const;

gen/functions.cpp

+19-14
Original file line numberDiff line numberDiff line change
@@ -538,11 +538,11 @@ static void set_param_attrs(TypeFunction* f, llvm::Function* func, FuncDeclarati
538538
{
539539
assert(Parameter::getNth(f->parameters, k));
540540

541-
AttrBuilder a = irFty.args[k]->attrs;
542-
if (a.hasAttributes())
541+
AttrBuilder builder = irFty.args[k]->attrs;
542+
if (builder.hasAttributes())
543543
{
544544
unsigned i = idx + (irFty.reverseParams ? n-k-1 : k);
545-
llvm::AttributeSet as = llvm::AttributeSet::get(gIR->context(), i, a.attrs);
545+
llvm::AttributeSet as = llvm::AttributeSet::get(gIR->context(), i, builder.attrs);
546546
newAttrs = newAttrs.addAttributes(gIR->context(), i, as);
547547
}
548548
}
@@ -554,14 +554,15 @@ static void set_param_attrs(TypeFunction* f, llvm::Function* func, FuncDeclarati
554554
static void set_param_attrs(TypeFunction* f, llvm::Function* func, FuncDeclaration* fdecl)
555555
{
556556
IrFuncTy &irFty = getIrFunc(fdecl)->irFty;
557-
AttrSet attrs;
557+
AttrSet set;
558558

559559
int idx = 0;
560560

561561
// handle implicit args
562562
#define ADD_PA(X) \
563563
if (irFty.X) { \
564-
attrs.add(idx, irFty.X->attrs); \
564+
if (irFty.X->attrs.hasAttributes()) \
565+
set.add(idx, irFty.X->attrs); \
565566
idx++; \
566567
}
567568

@@ -579,25 +580,29 @@ static void set_param_attrs(TypeFunction* f, llvm::Function* func, FuncDeclarati
579580
{
580581
assert(Parameter::getNth(f->parameters, k));
581582

582-
unsigned i = idx + (irFty.reverseParams ? n-k-1 : k);
583-
attrs.add(i, irFty.args[k]->attrs);
583+
AttrBuilder builder = irFty.args[k]->attrs;
584+
if (builder.hasAttributes())
585+
{
586+
unsigned i = idx + (irFty.reverseParams ? n-k-1 : k);
587+
set.add(i, builder);
588+
}
584589
}
585590

586591
// Merge in any old attributes (attributes for the function itself are
587592
// also stored in a list slot).
588593
llvm::AttrListPtr oldAttrs = func->getAttributes();
589-
for (size_t i = 0; i < oldAttrs.getNumSlots(); ++i) {
590-
llvm::AttributeWithIndex curr = oldAttrs.getSlot(i);
591-
if (curr.Index >= attrs.entries.size())
592-
attrs.entries.resize(curr.Index + 1);
594+
for (unsigned i = 0; i < oldAttrs.getNumSlots(); ++i)
595+
{
596+
const llvm::AttributeWithIndex& curr = oldAttrs.getSlot(i);
597+
AttrBuilder& builder = set.entries[curr.Index];
593598
#if LDC_LLVM_VER == 302
594-
attrs.entries[curr.Index].attrs.addAttributes(curr.Attrs);
599+
builder.attrs.addAttributes(curr.Attrs);
595600
#else
596-
attrs.entries[curr.Index].attrs |= curr.Attrs;
601+
builder.attrs |= curr.Attrs;
597602
#endif
598603
}
599604

600-
func->setAttributes(attrs.toNativeSet());
605+
func->setAttributes(set.toNativeSet());
601606
}
602607
#endif
603608

gen/tocall.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,6 @@ DValue* DtoCallFunction(Loc& loc, Type* resulttype, DValue* fnval, Expressions*
294294

295295
// parameter attributes
296296
AttrSet attrs;
297-
// return attributes + attributes for max 3 implicit args (sret, context, _arguments) & all explicit args
298-
attrs.reserve(1 + 3 + n_arguments);
299297

300298
// return attrs
301299
attrs.add(0, irFty.ret->attrs);

0 commit comments

Comments
 (0)