Skip to content

[C API] Add function to create ConstantRange attributes to C API #90505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 4, 2024
Merged
2 changes: 2 additions & 0 deletions llvm/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ Changes to the C API
* Added ``LLVMAtomicRMWBinOpUIncWrap`` and ``LLVMAtomicRMWBinOpUDecWrap`` to
``LLVMAtomicRMWBinOp`` enum for AtomicRMW instructions.

* Added ``LLVMCreateConstantRangeAttribute`` function for creating ConstantRange Attributes.

Changes to the CodeGen infrastructure
-------------------------------------

Expand Down
12 changes: 12 additions & 0 deletions llvm/include/llvm-c/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,18 @@ LLVMAttributeRef LLVMCreateTypeAttribute(LLVMContextRef C, unsigned KindID,
*/
LLVMTypeRef LLVMGetTypeAttributeValue(LLVMAttributeRef A);

/**
* Create a ConstantRange attribute.
*
* LowerWords and UpperWords need to be NumBits divided by 64 rounded up
* elements long.
*/
LLVMAttributeRef LLVMCreateConstantRangeAttribute(LLVMContextRef C,
unsigned KindID,
unsigned NumBits,
const uint64_t LowerWords[],
const uint64_t UpperWords[]);

/**
* Create a string attribute.
*/
Expand Down
16 changes: 16 additions & 0 deletions llvm/lib/IR/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "llvm-c/Core.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/ConstantRange.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/DerivedTypes.h"
Expand All @@ -33,6 +34,7 @@
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Threading.h"
#include "llvm/Support/raw_ostream.h"
Expand Down Expand Up @@ -178,6 +180,20 @@ LLVMTypeRef LLVMGetTypeAttributeValue(LLVMAttributeRef A) {
return wrap(Attr.getValueAsType());
}

LLVMAttributeRef LLVMCreateConstantRangeAttribute(LLVMContextRef C,
unsigned KindID,
unsigned NumBits,
const uint64_t LowerWords[],
const uint64_t UpperWords[]) {
auto &Ctx = *unwrap(C);
auto AttrKind = (Attribute::AttrKind)KindID;
unsigned NumWords = divideCeil(NumBits, 64);
return wrap(Attribute::get(
Ctx, AttrKind,
ConstantRange(APInt(NumBits, ArrayRef(LowerWords, NumWords)),
APInt(NumBits, ArrayRef(UpperWords, NumWords)))));
}

LLVMAttributeRef LLVMCreateStringAttribute(LLVMContextRef C,
const char *K, unsigned KLength,
const char *V, unsigned VLength) {
Expand Down
32 changes: 32 additions & 0 deletions llvm/unittests/IR/AttributesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
//===----------------------------------------------------------------------===//

#include "llvm/IR/Attributes.h"
#include "llvm-c/Core.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/AttributeMask.h"
#include "llvm/IR/ConstantRange.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/LLVMContext.h"
Expand Down Expand Up @@ -308,4 +310,34 @@ TEST(Attributes, RemoveParamAttributes) {
EXPECT_EQ(AL.getNumAttrSets(), 0U);
}

TEST(Attributes, ConstantRangeAttributeCAPI) {
LLVMContext C;
{
const unsigned NumBits = 8;
const uint64_t LowerWords[] = {0};
const uint64_t UpperWords[] = {42};

ConstantRange Range(APInt(NumBits, ArrayRef(LowerWords)),
APInt(NumBits, ArrayRef(UpperWords)));

Attribute RangeAttr = Attribute::get(C, Attribute::Range, Range);
auto OutAttr = unwrap(LLVMCreateConstantRangeAttribute(
wrap(&C), Attribute::Range, NumBits, LowerWords, UpperWords));
EXPECT_EQ(OutAttr, RangeAttr);
}
{
const unsigned NumBits = 128;
const uint64_t LowerWords[] = {1, 1};
const uint64_t UpperWords[] = {42, 42};

ConstantRange Range(APInt(NumBits, ArrayRef(LowerWords)),
APInt(NumBits, ArrayRef(UpperWords)));

Attribute RangeAttr = Attribute::get(C, Attribute::Range, Range);
auto OutAttr = unwrap(LLVMCreateConstantRangeAttribute(
wrap(&C), Attribute::Range, NumBits, LowerWords, UpperWords));
EXPECT_EQ(OutAttr, RangeAttr);
}
}

} // end anonymous namespace