Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 065c539

Browse files
committed
Merging r276236 and r276237:
------------------------------------------------------------------------ r276236 | deadalnix | 2016-07-20 21:25:06 -0700 (Wed, 20 Jul 2016) | 9 lines Expose AttributeSetNode, use it to provide aggregate getter for attribute in the C API. Summary: See D19181 for context. Reviewers: whitequark, Wallbraker, jyknight, echristo, bkramer, void Subscribers: mehdi_amini Differential Revision: http://reviews.llvm.org/D21265 ------------------------------------------------------------------------ ------------------------------------------------------------------------ r276237 | deadalnix | 2016-07-20 21:31:38 -0700 (Wed, 20 Jul 2016) | 1 line Add missing import to fix the build ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_39@276663 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 2adc29e commit 065c539

File tree

5 files changed

+134
-68
lines changed

5 files changed

+134
-68
lines changed

include/llvm-c/Core.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,6 +2014,9 @@ void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA);
20142014

20152015
void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
20162016
LLVMAttributeRef A);
2017+
unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx);
2018+
void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2019+
LLVMAttributeRef *Attrs);
20172020
LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F,
20182021
LLVMAttributeIndex Idx,
20192022
unsigned KindID);
@@ -2600,6 +2603,9 @@ void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
26002603

26012604
void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
26022605
LLVMAttributeRef A);
2606+
unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C, LLVMAttributeIndex Idx);
2607+
void LLVMGetCallSiteAttributes(LLVMValueRef C, LLVMAttributeIndex Idx,
2608+
LLVMAttributeRef *Attrs);
26032609
LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C,
26042610
LLVMAttributeIndex Idx,
26052611
unsigned KindID);

include/llvm/IR/Attributes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ class AttributeSet {
210210
private:
211211
friend class AttrBuilder;
212212
friend class AttributeSetImpl;
213+
friend class AttributeSetNode;
213214
template <typename Ty> friend struct DenseMapInfo;
214215

215216
/// \brief The attributes that we are managing. This can be null to represent

lib/IR/AttributeImpl.h

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
#include "llvm/ADT/FoldingSet.h"
2020
#include "llvm/ADT/Optional.h"
2121
#include "llvm/IR/Attributes.h"
22+
#include "AttributeSetNode.h"
2223
#include "llvm/Support/DataTypes.h"
23-
#include "llvm/Support/TrailingObjects.h"
2424
#include <climits>
2525
#include <string>
2626

@@ -142,73 +142,6 @@ class StringAttributeImpl : public AttributeImpl {
142142
StringRef getStringValue() const { return Val; }
143143
};
144144

145-
//===----------------------------------------------------------------------===//
146-
/// \class
147-
/// \brief This class represents a group of attributes that apply to one
148-
/// element: function, return type, or parameter.
149-
class AttributeSetNode final
150-
: public FoldingSetNode,
151-
private TrailingObjects<AttributeSetNode, Attribute> {
152-
friend TrailingObjects;
153-
154-
unsigned NumAttrs; ///< Number of attributes in this node.
155-
/// Bitset with a bit for each available attribute Attribute::AttrKind.
156-
uint64_t AvailableAttrs;
157-
158-
AttributeSetNode(ArrayRef<Attribute> Attrs)
159-
: NumAttrs(Attrs.size()), AvailableAttrs(0) {
160-
static_assert(Attribute::EndAttrKinds <= sizeof(AvailableAttrs) * CHAR_BIT,
161-
"Too many attributes for AvailableAttrs");
162-
// There's memory after the node where we can store the entries in.
163-
std::copy(Attrs.begin(), Attrs.end(), getTrailingObjects<Attribute>());
164-
165-
for (Attribute I : *this) {
166-
if (!I.isStringAttribute()) {
167-
AvailableAttrs |= ((uint64_t)1) << I.getKindAsEnum();
168-
}
169-
}
170-
}
171-
172-
// AttributesSetNode is uniqued, these should not be publicly available.
173-
void operator=(const AttributeSetNode &) = delete;
174-
AttributeSetNode(const AttributeSetNode &) = delete;
175-
public:
176-
void operator delete(void *p) { ::operator delete(p); }
177-
178-
static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
179-
180-
/// \brief Return the number of attributes this AttributeSet contains.
181-
unsigned getNumAttributes() const { return NumAttrs; }
182-
183-
bool hasAttribute(Attribute::AttrKind Kind) const {
184-
return AvailableAttrs & ((uint64_t)1) << Kind;
185-
}
186-
bool hasAttribute(StringRef Kind) const;
187-
bool hasAttributes() const { return NumAttrs != 0; }
188-
189-
Attribute getAttribute(Attribute::AttrKind Kind) const;
190-
Attribute getAttribute(StringRef Kind) const;
191-
192-
unsigned getAlignment() const;
193-
unsigned getStackAlignment() const;
194-
uint64_t getDereferenceableBytes() const;
195-
uint64_t getDereferenceableOrNullBytes() const;
196-
std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
197-
std::string getAsString(bool InAttrGrp) const;
198-
199-
typedef const Attribute *iterator;
200-
iterator begin() const { return getTrailingObjects<Attribute>(); }
201-
iterator end() const { return begin() + NumAttrs; }
202-
203-
void Profile(FoldingSetNodeID &ID) const {
204-
Profile(ID, makeArrayRef(begin(), end()));
205-
}
206-
static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
207-
for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
208-
AttrList[I].Profile(ID);
209-
}
210-
};
211-
212145
typedef std::pair<unsigned, AttributeSetNode *> IndexAttrPair;
213146

214147
//===----------------------------------------------------------------------===//

lib/IR/AttributeSetNode.h

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//===-- AttributeSetNode.h - AttributeSet Internal Node ---------*- C++ -*-===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
///
10+
/// \file
11+
/// \brief This file defines the node class used internally by AttributeSet.
12+
///
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef LLVM_IR_ATTRIBUTESETNODE_H
16+
#define LLVM_IR_ATTRIBUTESETNODE_H
17+
18+
#include "llvm/ADT/FoldingSet.h"
19+
#include "llvm/IR/Attributes.h"
20+
#include "llvm/Support/TrailingObjects.h"
21+
#include <climits>
22+
23+
namespace llvm {
24+
25+
//===----------------------------------------------------------------------===//
26+
/// \class
27+
/// \brief This class represents a group of attributes that apply to one
28+
/// element: function, return type, or parameter.
29+
class AttributeSetNode final
30+
: public FoldingSetNode,
31+
private TrailingObjects<AttributeSetNode, Attribute> {
32+
friend TrailingObjects;
33+
34+
unsigned NumAttrs; ///< Number of attributes in this node.
35+
/// Bitset with a bit for each available attribute Attribute::AttrKind.
36+
uint64_t AvailableAttrs;
37+
38+
AttributeSetNode(ArrayRef<Attribute> Attrs)
39+
: NumAttrs(Attrs.size()), AvailableAttrs(0) {
40+
static_assert(Attribute::EndAttrKinds <= sizeof(AvailableAttrs) * CHAR_BIT,
41+
"Too many attributes for AvailableAttrs");
42+
// There's memory after the node where we can store the entries in.
43+
std::copy(Attrs.begin(), Attrs.end(), getTrailingObjects<Attribute>());
44+
45+
for (Attribute I : *this) {
46+
if (!I.isStringAttribute()) {
47+
AvailableAttrs |= ((uint64_t)1) << I.getKindAsEnum();
48+
}
49+
}
50+
}
51+
52+
// AttributesSetNode is uniqued, these should not be publicly available.
53+
void operator=(const AttributeSetNode &) = delete;
54+
AttributeSetNode(const AttributeSetNode &) = delete;
55+
public:
56+
void operator delete(void *p) { ::operator delete(p); }
57+
58+
static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
59+
60+
static AttributeSetNode *get(AttributeSet AS, unsigned Index) {
61+
return AS.getAttributes(Index);
62+
}
63+
64+
/// \brief Return the number of attributes this AttributeSet contains.
65+
unsigned getNumAttributes() const { return NumAttrs; }
66+
67+
bool hasAttribute(Attribute::AttrKind Kind) const {
68+
return AvailableAttrs & ((uint64_t)1) << Kind;
69+
}
70+
bool hasAttribute(StringRef Kind) const;
71+
bool hasAttributes() const { return NumAttrs != 0; }
72+
73+
Attribute getAttribute(Attribute::AttrKind Kind) const;
74+
Attribute getAttribute(StringRef Kind) const;
75+
76+
unsigned getAlignment() const;
77+
unsigned getStackAlignment() const;
78+
uint64_t getDereferenceableBytes() const;
79+
uint64_t getDereferenceableOrNullBytes() const;
80+
std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
81+
std::string getAsString(bool InAttrGrp) const;
82+
83+
typedef const Attribute *iterator;
84+
iterator begin() const { return getTrailingObjects<Attribute>(); }
85+
iterator end() const { return begin() + NumAttrs; }
86+
87+
void Profile(FoldingSetNodeID &ID) const {
88+
Profile(ID, makeArrayRef(begin(), end()));
89+
}
90+
static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
91+
for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
92+
AttrList[I].Profile(ID);
93+
}
94+
};
95+
96+
} // end llvm namespace
97+
98+
#endif

lib/IR/Core.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "llvm/ADT/StringSwitch.h"
1717
#include "llvm/Bitcode/ReaderWriter.h"
1818
#include "llvm/IR/Attributes.h"
19+
#include "AttributeSetNode.h"
1920
#include "llvm/IR/CallSite.h"
2021
#include "llvm/IR/Constants.h"
2122
#include "llvm/IR/DerivedTypes.h"
@@ -1844,6 +1845,18 @@ void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
18441845
unwrap<Function>(F)->addAttribute(Idx, unwrap(A));
18451846
}
18461847

1848+
unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx) {
1849+
auto *ASN = AttributeSetNode::get(unwrap<Function>(F)->getAttributes(), Idx);
1850+
return ASN->getNumAttributes();
1851+
}
1852+
1853+
void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1854+
LLVMAttributeRef *Attrs) {
1855+
auto *ASN = AttributeSetNode::get(unwrap<Function>(F)->getAttributes(), Idx);
1856+
for (auto A: make_range(ASN->begin(), ASN->end()))
1857+
*Attrs++ = wrap(A);
1858+
}
1859+
18471860
LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F,
18481861
LLVMAttributeIndex Idx,
18491862
unsigned KindID) {
@@ -2216,6 +2229,21 @@ void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
22162229
CallSite(unwrap<Instruction>(C)).addAttribute(Idx, unwrap(A));
22172230
}
22182231

2232+
unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C,
2233+
LLVMAttributeIndex Idx) {
2234+
auto CS = CallSite(unwrap<Instruction>(C));
2235+
auto *ASN = AttributeSetNode::get(CS.getAttributes(), Idx);
2236+
return ASN->getNumAttributes();
2237+
}
2238+
2239+
void LLVMGetCallSiteAttributes(LLVMValueRef C, LLVMAttributeIndex Idx,
2240+
LLVMAttributeRef *Attrs) {
2241+
auto CS = CallSite(unwrap<Instruction>(C));
2242+
auto *ASN = AttributeSetNode::get(CS.getAttributes(), Idx);
2243+
for (auto A: make_range(ASN->begin(), ASN->end()))
2244+
*Attrs++ = wrap(A);
2245+
}
2246+
22192247
LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C,
22202248
LLVMAttributeIndex Idx,
22212249
unsigned KindID) {

0 commit comments

Comments
 (0)