Skip to content

Commit 1343e68

Browse files
authored
[C API] Add function to create ConstantRange attributes to C API (#90505)
1 parent f16e234 commit 1343e68

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

llvm/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ Changes to the C API
164164
* Added ``LLVMAtomicRMWBinOpUIncWrap`` and ``LLVMAtomicRMWBinOpUDecWrap`` to
165165
``LLVMAtomicRMWBinOp`` enum for AtomicRMW instructions.
166166

167+
* Added ``LLVMCreateConstantRangeAttribute`` function for creating ConstantRange Attributes.
168+
167169
Changes to the CodeGen infrastructure
168170
-------------------------------------
169171

llvm/include/llvm-c/Core.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,18 @@ LLVMAttributeRef LLVMCreateTypeAttribute(LLVMContextRef C, unsigned KindID,
673673
*/
674674
LLVMTypeRef LLVMGetTypeAttributeValue(LLVMAttributeRef A);
675675

676+
/**
677+
* Create a ConstantRange attribute.
678+
*
679+
* LowerWords and UpperWords need to be NumBits divided by 64 rounded up
680+
* elements long.
681+
*/
682+
LLVMAttributeRef LLVMCreateConstantRangeAttribute(LLVMContextRef C,
683+
unsigned KindID,
684+
unsigned NumBits,
685+
const uint64_t LowerWords[],
686+
const uint64_t UpperWords[]);
687+
676688
/**
677689
* Create a string attribute.
678690
*/

llvm/lib/IR/Core.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "llvm-c/Core.h"
1515
#include "llvm/IR/Attributes.h"
1616
#include "llvm/IR/BasicBlock.h"
17+
#include "llvm/IR/ConstantRange.h"
1718
#include "llvm/IR/Constants.h"
1819
#include "llvm/IR/DebugInfoMetadata.h"
1920
#include "llvm/IR/DerivedTypes.h"
@@ -33,6 +34,7 @@
3334
#include "llvm/Support/ErrorHandling.h"
3435
#include "llvm/Support/FileSystem.h"
3536
#include "llvm/Support/ManagedStatic.h"
37+
#include "llvm/Support/MathExtras.h"
3638
#include "llvm/Support/MemoryBuffer.h"
3739
#include "llvm/Support/Threading.h"
3840
#include "llvm/Support/raw_ostream.h"
@@ -178,6 +180,20 @@ LLVMTypeRef LLVMGetTypeAttributeValue(LLVMAttributeRef A) {
178180
return wrap(Attr.getValueAsType());
179181
}
180182

183+
LLVMAttributeRef LLVMCreateConstantRangeAttribute(LLVMContextRef C,
184+
unsigned KindID,
185+
unsigned NumBits,
186+
const uint64_t LowerWords[],
187+
const uint64_t UpperWords[]) {
188+
auto &Ctx = *unwrap(C);
189+
auto AttrKind = (Attribute::AttrKind)KindID;
190+
unsigned NumWords = divideCeil(NumBits, 64);
191+
return wrap(Attribute::get(
192+
Ctx, AttrKind,
193+
ConstantRange(APInt(NumBits, ArrayRef(LowerWords, NumWords)),
194+
APInt(NumBits, ArrayRef(UpperWords, NumWords)))));
195+
}
196+
181197
LLVMAttributeRef LLVMCreateStringAttribute(LLVMContextRef C,
182198
const char *K, unsigned KLength,
183199
const char *V, unsigned VLength) {

llvm/unittests/IR/AttributesTest.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "llvm/IR/Attributes.h"
10+
#include "llvm-c/Core.h"
1011
#include "llvm/AsmParser/Parser.h"
1112
#include "llvm/IR/AttributeMask.h"
13+
#include "llvm/IR/ConstantRange.h"
1214
#include "llvm/IR/DerivedTypes.h"
1315
#include "llvm/IR/InstrTypes.h"
1416
#include "llvm/IR/LLVMContext.h"
@@ -308,4 +310,34 @@ TEST(Attributes, RemoveParamAttributes) {
308310
EXPECT_EQ(AL.getNumAttrSets(), 0U);
309311
}
310312

313+
TEST(Attributes, ConstantRangeAttributeCAPI) {
314+
LLVMContext C;
315+
{
316+
const unsigned NumBits = 8;
317+
const uint64_t LowerWords[] = {0};
318+
const uint64_t UpperWords[] = {42};
319+
320+
ConstantRange Range(APInt(NumBits, ArrayRef(LowerWords)),
321+
APInt(NumBits, ArrayRef(UpperWords)));
322+
323+
Attribute RangeAttr = Attribute::get(C, Attribute::Range, Range);
324+
auto OutAttr = unwrap(LLVMCreateConstantRangeAttribute(
325+
wrap(&C), Attribute::Range, NumBits, LowerWords, UpperWords));
326+
EXPECT_EQ(OutAttr, RangeAttr);
327+
}
328+
{
329+
const unsigned NumBits = 128;
330+
const uint64_t LowerWords[] = {1, 1};
331+
const uint64_t UpperWords[] = {42, 42};
332+
333+
ConstantRange Range(APInt(NumBits, ArrayRef(LowerWords)),
334+
APInt(NumBits, ArrayRef(UpperWords)));
335+
336+
Attribute RangeAttr = Attribute::get(C, Attribute::Range, Range);
337+
auto OutAttr = unwrap(LLVMCreateConstantRangeAttribute(
338+
wrap(&C), Attribute::Range, NumBits, LowerWords, UpperWords));
339+
EXPECT_EQ(OutAttr, RangeAttr);
340+
}
341+
}
342+
311343
} // end anonymous namespace

0 commit comments

Comments
 (0)