Skip to content

Commit 78612a6

Browse files
committed
Add GlobalValue type getter.
1 parent ba5c2e5 commit 78612a6

File tree

6 files changed

+17
-3
lines changed

6 files changed

+17
-3
lines changed

deps/LLVMExtra/include/LLVMExtra.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ void LLVMExtraGetNamedMetadataOperands2(LLVMNamedMDNodeRef NMD, LLVMMetadataRef
8484
void LLVMExtraAddNamedMetadataOperand2(LLVMNamedMDNodeRef NMD, LLVMMetadataRef Val);
8585

8686
LLVMTypeRef LLVMGetFunctionType(LLVMValueRef Fn);
87+
LLVMTypeRef LLVMGetGlobalValueType(LLVMValueRef Fn);
8788

8889
#if LLVM_VERSION_MAJOR >= 12
8990
void LLVMAddCFGSimplificationPass2(LLVMPassManagerRef PM,

deps/LLVMExtra/lib/llvm-api.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,8 +567,12 @@ LLVMValueRef LLVMConstDataArray(LLVMTypeRef ElementTy, const void *Data, unsigne
567567
return wrap(ConstantDataArray::getRaw(S, NumElements, unwrap(ElementTy)));
568568
}
569569

570-
LLVMTypeRef LLVMGetFunctionType(LLVMValueRef Fn) {
570+
LLVMTypeRef LLVMGetFunctionType(LLVMValueRef Fn) {
571571
auto Ftype = unwrap<Function>(Fn)->getFunctionType();
572572
return wrap(Ftype);
573573
}
574574

575+
LLVMTypeRef LLVMGetGlobalValueType(LLVMValueRef GV) {
576+
auto Ftype = unwrap<GlobalValue>(GV)->getValueType();
577+
return wrap(Ftype);
578+
}

lib/libLLVM_extra.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,10 @@ function LLVMGetFunctionType(Fn)
425425
ccall((:LLVMGetFunctionType,libLLVMExtra), LLVMTypeRef, (LLVMValueRef,), Fn)
426426
end
427427

428+
function LLVMGetGlobalValueType(Fn)
429+
ccall((:LLVMGetGlobalValueType,libLLVMExtra), LLVMTypeRef, (LLVMValueRef,), Fn)
430+
end
431+
428432
function LLVMGetBuilderContext(B)
429433
ccall((:LLVMGetBuilderContext, libLLVMExtra), LLVMContextRef, (LLVMBuilderRef,), B)
430434
end

src/core/value/constant.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ InlineAsm(typ::FunctionType, asm::String, constraints::String,
579579

580580
abstract type GlobalValue <: Constant end
581581

582-
export GlobalValue,
582+
export GlobalValue, global_value_type,
583583
isdeclaration,
584584
linkage, linkage!,
585585
section, section!,
@@ -590,6 +590,8 @@ export GlobalValue,
590590

591591
parent(val::GlobalValue) = Module(API.LLVMGetGlobalParent(val))
592592

593+
global_value_type(val::GlobalValue) = LLVMType(API.LLVMGetGlobalValueType(val))
594+
593595
isdeclaration(val::GlobalValue) = convert(Core.Bool, API.LLVMIsDeclaration(val))
594596

595597
linkage(val::GlobalValue) = API.LLVMGetLinkage(val)

src/interop/base.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function Base.convert(::Type{LLVMType}, typ::Type; ctx::Context,
109109
llvmtyp = let
110110
mod = parse(LLVM.Module, buf; ctx)
111111
gv = first(globals(mod))
112-
eltype(value_type(gv))
112+
global_value_type(gv)
113113
end
114114
end
115115

test/core.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,10 @@ end
815815
@dispose ctx=Context() mod=LLVM.Module("SomeModule"; ctx) begin
816816
gv = GlobalVariable(mod, LLVM.Int32Type(ctx), "SomeGlobal", 1)
817817

818+
@test value_type(gv) isa LLVM.PointerType
818819
@test addrspace(value_type(gv)) == 1
820+
821+
@test global_value_type(gv) == LLVM.Int32Type(ctx)
819822
end
820823

821824
end

0 commit comments

Comments
 (0)