Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions deps/LLVMExtra/include/LLVMExtra.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ void LLVMDestroyConstant(LLVMValueRef Const);
LLVMTypeRef LLVMGetFunctionType(LLVMValueRef Fn);
LLVMTypeRef LLVMGetGlobalValueType(LLVMValueRef Fn);

// Attribute type detection (ConstantRange/ConstantRangeList kinds not exposed in C API)
#if LLVM_VERSION_MAJOR >= 19
LLVMBool LLVMIsConstantRangeAttribute(LLVMAttributeRef A);
#endif
#if LLVM_VERSION_MAJOR >= 20
LLVMBool LLVMIsConstantRangeListAttribute(LLVMAttributeRef A);
#endif

// Bug fixes
#if LLVM_VERSION_MAJOR < 20 // llvm/llvm-project#105521
void LLVMSetInitializer2(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal);
Expand Down
21 changes: 21 additions & 0 deletions deps/LLVMExtra/lib/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,27 @@ LLVMTypeRef LLVMGetGlobalValueType(LLVMValueRef GV) {
}


//
// Attribute type detection
//

#if LLVM_VERSION_MAJOR >= 19

LLVMBool LLVMIsConstantRangeAttribute(LLVMAttributeRef A) {
return unwrap(A).isConstantRangeAttribute();
}

#endif

#if LLVM_VERSION_MAJOR >= 20

LLVMBool LLVMIsConstantRangeListAttribute(LLVMAttributeRef A) {
return unwrap(A).isConstantRangeListAttribute();
}

#endif


//
// Bug fixes
//
Expand Down
4 changes: 4 additions & 0 deletions lib/17/libLLVM_extra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ function LLVMGetGlobalValueType(Fn)
ccall((:LLVMGetGlobalValueType, libLLVMExtra), LLVMTypeRef, (LLVMValueRef,), Fn)
end

function LLVMIsConstantRangeAttribute(A)
ccall((:LLVMIsConstantRangeAttribute, libLLVMExtra), LLVMBool, (LLVMAttributeRef,), A)
end

function LLVMSetInitializer2(GlobalVar, ConstantVal)
ccall((:LLVMSetInitializer2, libLLVMExtra), Cvoid, (LLVMValueRef, LLVMValueRef), GlobalVar, ConstantVal)
end
Expand Down
4 changes: 4 additions & 0 deletions lib/18/libLLVM_extra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ function LLVMGetGlobalValueType(Fn)
ccall((:LLVMGetGlobalValueType, libLLVMExtra), LLVMTypeRef, (LLVMValueRef,), Fn)
end

function LLVMIsConstantRangeAttribute(A)
ccall((:LLVMIsConstantRangeAttribute, libLLVMExtra), LLVMBool, (LLVMAttributeRef,), A)
end

function LLVMSetInitializer2(GlobalVar, ConstantVal)
ccall((:LLVMSetInitializer2, libLLVMExtra), Cvoid, (LLVMValueRef, LLVMValueRef), GlobalVar, ConstantVal)
end
Expand Down
4 changes: 4 additions & 0 deletions lib/19/libLLVM_extra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ function LLVMGetGlobalValueType(Fn)
ccall((:LLVMGetGlobalValueType, libLLVMExtra), LLVMTypeRef, (LLVMValueRef,), Fn)
end

function LLVMIsConstantRangeAttribute(A)
ccall((:LLVMIsConstantRangeAttribute, libLLVMExtra), LLVMBool, (LLVMAttributeRef,), A)
end

function LLVMSetInitializer2(GlobalVar, ConstantVal)
ccall((:LLVMSetInitializer2, libLLVMExtra), Cvoid, (LLVMValueRef, LLVMValueRef), GlobalVar, ConstantVal)
end
Expand Down
8 changes: 8 additions & 0 deletions lib/20/libLLVM_extra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ function LLVMGetGlobalValueType(Fn)
ccall((:LLVMGetGlobalValueType, libLLVMExtra), LLVMTypeRef, (LLVMValueRef,), Fn)
end

function LLVMIsConstantRangeAttribute(A)
ccall((:LLVMIsConstantRangeAttribute, libLLVMExtra), LLVMBool, (LLVMAttributeRef,), A)
end

function LLVMIsConstantRangeListAttribute(A)
ccall((:LLVMIsConstantRangeListAttribute, libLLVMExtra), LLVMBool, (LLVMAttributeRef,), A)
end

function LLVMGetMDString2(MD, Length)
ccall((:LLVMGetMDString2, libLLVMExtra), Cstring, (LLVMMetadataRef, Ptr{Cuint}), MD, Length)
end
Expand Down
8 changes: 8 additions & 0 deletions lib/21/libLLVM_extra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ function LLVMGetGlobalValueType(Fn)
ccall((:LLVMGetGlobalValueType, libLLVMExtra), LLVMTypeRef, (LLVMValueRef,), Fn)
end

function LLVMIsConstantRangeAttribute(A)
ccall((:LLVMIsConstantRangeAttribute, libLLVMExtra), LLVMBool, (LLVMAttributeRef,), A)
end

function LLVMIsConstantRangeListAttribute(A)
ccall((:LLVMIsConstantRangeListAttribute, libLLVMExtra), LLVMBool, (LLVMAttributeRef,), A)
end

function LLVMGetMDString2(MD, Length)
ccall((:LLVMGetMDString2, libLLVMExtra), Cstring, (LLVMMetadataRef, Ptr{Cuint}), MD, Length)
end
Expand Down
35 changes: 35 additions & 0 deletions src/core/attributes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

export Attribute,
EnumAttribute, StringAttribute, TypeAttribute,
ConstantRangeAttribute, ConstantRangeListAttribute,
kind, value

abstract type Attribute end
Expand All @@ -21,6 +22,16 @@ end
ref::API.LLVMAttributeRef
end

# ConstantRange attribute kind (e.g. `range`, introduced in LLVM 17).
@checked struct ConstantRangeAttribute <: Attribute
ref::API.LLVMAttributeRef
end

# ConstantRangeList attribute kind (e.g. `initializes`, introduced in LLVM 20).
@checked struct ConstantRangeListAttribute <: Attribute
ref::API.LLVMAttributeRef
end

# TODO: make the identify mechanism flexible enough to cover cases like this one,
# and not only Value and Type

Expand All @@ -33,6 +44,12 @@ function Attribute(ref::API.LLVMAttributeRef)
elseif Bool(API.LLVMIsTypeAttribute(ref))
return TypeAttribute(ref)
else
@static if version() >= v"20"
Bool(API.LLVMIsConstantRangeAttribute(ref)) && return ConstantRangeAttribute(ref)
Bool(API.LLVMIsConstantRangeListAttribute(ref)) && return ConstantRangeListAttribute(ref)
elseif version() >= v"19"
Bool(API.LLVMIsConstantRangeAttribute(ref)) && return ConstantRangeAttribute(ref)
end
error("unknown attribute kind")
end
end
Expand Down Expand Up @@ -87,3 +104,21 @@ kind(attr::TypeAttribute) = API.LLVMGetEnumAttributeKind(attr)
function value(attr::TypeAttribute)
return LLVMType(API.LLVMGetTypeAttributeValue(attr))
end

## constant range attribute

if version() >= v"19"
function ConstantRangeAttribute(kind::String, nbits::Integer,
lower::Vector{UInt64}, upper::Vector{UInt64})
enum_kind = API.LLVMGetEnumAttributeKindForName(kind, Csize_t(length(kind)))
return ConstantRangeAttribute(
API.LLVMCreateConstantRangeAttribute(context(), enum_kind, Cuint(nbits),
lower, upper))
end
end

kind(attr::ConstantRangeAttribute) = API.LLVMGetEnumAttributeKind(attr)

## constant range list attribute

kind(attr::ConstantRangeListAttribute) = API.LLVMGetEnumAttributeKind(attr)
6 changes: 6 additions & 0 deletions src/core/function.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ Base.delete!(iter::FunctionAttrSet, attr::EnumAttribute) =
Base.delete!(iter::FunctionAttrSet, attr::TypeAttribute) =
API.LLVMRemoveEnumAttributeAtIndex(iter.f, iter.idx, kind(attr))

Base.delete!(iter::FunctionAttrSet, attr::ConstantRangeAttribute) =
API.LLVMRemoveEnumAttributeAtIndex(iter.f, iter.idx, kind(attr))

Base.delete!(iter::FunctionAttrSet, attr::ConstantRangeListAttribute) =
API.LLVMRemoveEnumAttributeAtIndex(iter.f, iter.idx, kind(attr))

function Base.delete!(iter::FunctionAttrSet, attr::StringAttribute)
k = kind(attr)
API.LLVMRemoveStringAttributeAtIndex(iter.f, iter.idx, k, length(k))
Expand Down
6 changes: 6 additions & 0 deletions src/core/instructions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,12 @@ Base.delete!(iter::CallSiteAttrSet, attr::LLVM.EnumAttribute) =
Base.delete!(iter::CallSiteAttrSet, attr::LLVM.TypeAttribute) =
LLVM.API.LLVMRemoveCallSiteEnumAttribute(iter.instr, iter.idx, kind(attr))

Base.delete!(iter::CallSiteAttrSet, attr::LLVM.ConstantRangeAttribute) =
LLVM.API.LLVMRemoveCallSiteEnumAttribute(iter.instr, iter.idx, kind(attr))

Base.delete!(iter::CallSiteAttrSet, attr::LLVM.ConstantRangeListAttribute) =
LLVM.API.LLVMRemoveCallSiteEnumAttribute(iter.instr, iter.idx, kind(attr))

function Base.delete!(iter::CallSiteAttrSet, attr::LLVM.StringAttribute)
k = kind(attr)
return LLVM.API.LLVMRemoveCallSiteStringAttribute(iter.instr, iter.idx, k, length(k))
Expand Down
11 changes: 11 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,17 @@ end
delete!(instr_attrs, instr_attr)
@test length(instr_attrs) == 0
end

if LLVM.version() >= v"19"
let attr = ConstantRangeAttribute("range", 32, UInt64[0], UInt64[100])
@test attr isa ConstantRangeAttribute
@test kind(attr) != 0
push!(return_attributes(fn), attr)
collected = collect(return_attributes(fn))
@test any(a -> a isa ConstantRangeAttribute, collected)
delete!(return_attributes(fn), attr)
end
end
end

for i in 1:length(parameters(fn))
Expand Down
Loading