Skip to content

Commit c6cdbcd

Browse files
authored
Merge pull request #335 from maleadt/tb/value_type
Rename llvmtype to value_type, remove llvmeltype.
2 parents 1205905 + 78612a6 commit c6cdbcd

File tree

11 files changed

+49
-36
lines changed

11 files changed

+49
-36
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.jl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,12 @@ end
3939

4040
## general APIs
4141

42-
export llvmtype, llvmeltype, name, name!, replace_uses!, replace_metadata_uses!, isconstant, isundef, ispoison, context
42+
export value_type, name, name!, replace_uses!, replace_metadata_uses!, isconstant, isundef, ispoison, context
4343

44-
llvmtype(val::Value) = LLVMType(API.LLVMTypeOf(val))
45-
llvmeltype(val::Value) = eltype(llvmtype(val))
44+
value_type(val::Value) = LLVMType(API.LLVMTypeOf(val))
4645

4746
# defer size queries to the LLVM type (where we'll error)
48-
Base.sizeof(val::Value) = sizeof(llvmtype(val))
47+
Base.sizeof(val::Value) = sizeof(value_type(val))
4948

5049
name(val::Value) = unsafe_string(API.LLVMGetValueName(val))
5150
name!(val::Value, name::String) = API.LLVMSetValueName(val, name)
@@ -58,13 +57,13 @@ end
5857
replace_uses!(old::Value, new::Value) = API.LLVMReplaceAllUsesWith(old, new)
5958

6059
function replace_metadata_uses!(old::Value, new::Value)
61-
if llvmtype(old) == llvmtype(new)
60+
if value_type(old) == value_type(new)
6261
API.LLVMReplaceAllMetadataUsesWith(old, new)
6362
else
6463
# NOTE: LLVM does not support replacing values of different types, either using
6564
# regular RAUW or only on metadata. The latter should probably be supported.
6665
# Instead, we replace by a bitcast to the old type.
67-
compat_new = const_bitcast(new, llvmtype(old))
66+
compat_new = const_bitcast(new, value_type(old))
6867
replace_metadata_uses!(old, compat_new)
6968

7069
# the above is often invalid, e.g. for module-level metadata identifying functions.

src/core/value/constant.jl

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ abstract type ConstantDataSequential <: Constant end
133133
# but cannot support that as explained above).
134134

135135
# array interface
136-
Base.eltype(cda::ConstantDataSequential) = llvmeltype(cda)
137-
Base.length(cda::ConstantDataSequential) = length(llvmtype(cda))
136+
Base.eltype(cda::ConstantDataSequential) = eltype(value_type(cda))
137+
Base.length(cda::ConstantDataSequential) = length(value_type(cda))
138138
Base.size(cda::ConstantDataSequential) = (length(cda),)
139139
function Base.getindex(cda::ConstantDataSequential, idx::Integer)
140140
@boundscheck 1 <= idx <= length(cda) || throw(BoundsError(cda, idx))
@@ -191,7 +191,7 @@ register(ConstantAggregateZero, API.LLVMConstantAggregateZeroValueKind)
191191
# array interface
192192
# FIXME: can we reuse the ::ConstantArray functionality with ConstantAggregateZero values?
193193
# probably works fine if we just get rid of the refcheck
194-
Base.eltype(caz::ConstantAggregateZero) = llvmeltype(caz)
194+
Base.eltype(caz::ConstantAggregateZero) = eltype(value_type(caz))
195195
Base.size(caz::ConstantAggregateZero) = (0,)
196196
Base.length(caz::ConstantAggregateZero) = 0
197197
Base.axes(caz::ConstantAggregateZero) = (Base.OneTo(0),)
@@ -215,15 +215,15 @@ register(ConstantArray, API.LLVMConstantArrayValueKind)
215215

216216
# generic constructor taking an array of constants
217217
function ConstantArray(typ::LLVMType, data::AbstractArray{T,N}=T[]) where {T<:Constant,N}
218-
@assert all(x->x==typ, llvmtype.(data))
218+
@assert all(x->x==typ, value_type.(data))
219219

220220
if N == 1
221221
# XXX: this can return a ConstDataArray (presumably as an optimization?)
222222
return Value(API.LLVMConstArray(typ, Array(data), length(data)))
223223
end
224224

225225
ca_vec = map(x->ConstantArray(typ, x), eachslice(data, dims=1))
226-
ca_typ = llvmtype(first(ca_vec))
226+
ca_typ = value_type(first(ca_vec))
227227

228228
return ConstantArray(API.LLVMConstArray(ca_typ, ca_vec, length(ca_vec)))
229229
end
@@ -252,10 +252,10 @@ function Base.collect(ca::ConstantArray)
252252
end
253253

254254
# array interface
255-
Base.eltype(ca::ConstantArray) = llvmeltype(ca)
255+
Base.eltype(ca::ConstantArray) = eltype(value_type(ca))
256256
function Base.size(ca::ConstantArray)
257257
dims = Int[]
258-
typ = llvmtype(ca)
258+
typ = value_type(ca)
259259
while typ isa ArrayType
260260
push!(dims, length(typ))
261261
typ = eltype(typ)
@@ -326,13 +326,13 @@ function ConstantStruct(value::T, name=String(nameof(T)); ctx::Context,
326326
ConstantStruct(constants; ctx, packed)
327327
elseif haskey(types(ctx), name)
328328
typ = types(ctx)[name]
329-
if collect(elements(typ)) != llvmtype.(constants)
330-
throw(ArgumentError("Cannot create struct $name {$(join(llvmtype.(constants), ", "))} as it is already defined in this context as {$(join(elements(typ), ", "))}."))
329+
if collect(elements(typ)) != value_type.(constants)
330+
throw(ArgumentError("Cannot create struct $name {$(join(value_type.(constants), ", "))} as it is already defined in this context as {$(join(elements(typ), ", "))}."))
331331
end
332332
ConstantStruct(typ, constants)
333333
else
334334
typ = StructType(name; ctx)
335-
elements!(typ, llvmtype.(constants))
335+
elements!(typ, value_type.(constants))
336336
ConstantStruct(typ, constants)
337337
end
338338
end
@@ -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-
llvmeltype(gv)
112+
global_value_type(gv)
113113
end
114114
end
115115

src/irbuilder.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ debuglocation!(builder::Builder, inst::Instruction) =
5555
## build methods
5656

5757
# TODO/IDEAS:
58-
# - dynamic dispatch based on `llvmtype` (eg. disambiguating `add!` and `fadd!`)
58+
# - dynamic dispatch based on `value_type` (eg. disambiguating `add!` and `fadd!`)
5959

6060
# NOTE: the return values for these operations are, according to the C API, always a Value.
6161
# however, the C++ API learns us that we can be more strict.

src/utils.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function clone(f::Function; value_map::Dict{Value,Value}=Dict{Value,Value}())
5050
# the VMap. If so, we need to not add the arguments to the arg ty vector
5151
for arg in parameters(f)
5252
if !in(arg, keys(value_map)) # Haven't mapped the argument to anything yet?
53-
push!(argtypes, llvmtype(arg))
53+
push!(argtypes, value_type(arg))
5454
end
5555
end
5656

test/core.jl

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ end
228228

229229
show(devnull, val)
230230

231-
@test llvmtype(val) == LLVM.PointerType(typ)
231+
@test value_type(val) == LLVM.PointerType(typ)
232232
@test_throws ErrorException sizeof(val)
233233
@test name(val) == "foo"
234234
@test !isconstant(val)
@@ -367,7 +367,7 @@ end
367367
end
368368
let
369369
constval = ConstantInt(false; ctx)
370-
@test llvmtype(constval) == LLVM.Int1Type(ctx)
370+
@test value_type(constval) == LLVM.Int1Type(ctx)
371371
@test !convert(Bool, constval)
372372

373373
constval = ConstantInt(true; ctx)
@@ -459,7 +459,7 @@ end
459459
let
460460
test_struct = TestStruct(true, -99, 1.5)
461461
constant_struct = ConstantStruct(test_struct; ctx, anonymous=true)
462-
constant_struct_type = llvmtype(constant_struct)
462+
constant_struct_type = value_type(constant_struct)
463463

464464
@test constant_struct_type isa LLVM.StructType
465465
@test context(constant_struct) == ctx
@@ -479,7 +479,7 @@ end
479479
let
480480
test_struct = TestStruct(false, 52, -2.5)
481481
constant_struct = ConstantStruct(test_struct; ctx)
482-
constant_struct_type = llvmtype(constant_struct)
482+
constant_struct_type = value_type(constant_struct)
483483

484484
@test constant_struct_type isa LLVM.StructType
485485

@@ -499,7 +499,7 @@ end
499499
let
500500
test_struct = TestSingleton()
501501
constant_struct = ConstantStruct(test_struct; ctx)
502-
constant_struct_type = llvmtype(constant_struct)
502+
constant_struct_type = value_type(constant_struct)
503503

504504
@test isempty(operands(constant_struct))
505505
end
@@ -517,7 +517,7 @@ end
517517
eltyp = LLVM.Int32Type(ctx)
518518
cda = ConstantDataArray(eltyp, vec)
519519
@test cda isa ConstantDataArray
520-
@test llvmtype(cda) == LLVM.ArrayType(eltyp, 4)
520+
@test value_type(cda) == LLVM.ArrayType(eltyp, 4)
521521
@test collect(cda) == ConstantInt.(vec; ctx)
522522
end
523523

@@ -697,7 +697,7 @@ end
697697
ce = const_ptrtoint(ptr, LLVM.Int32Type(ctx))::LLVM.Constant
698698
@check_ir ce "i32 0"
699699

700-
ce = const_inttoptr(ce, llvmtype(ptr))::LLVM.Constant
700+
ce = const_inttoptr(ce, value_type(ptr))::LLVM.Constant
701701
if supports_typed_ptrs
702702
@check_ir ce "i32* null"
703703
else
@@ -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 addrspace(llvmtype(gv)) == 1
818+
@test value_type(gv) isa LLVM.PointerType
819+
@test addrspace(value_type(gv)) == 1
820+
821+
@test global_value_type(gv) == LLVM.Int32Type(ctx)
819822
end
820823

821824
end
@@ -1165,7 +1168,7 @@ end
11651168
@test fn isa LLVM.Function
11661169

11671170
if supports_typed_ptrs
1168-
@test llvmeltype(fn) == ft
1171+
@test eltype(value_type(fn)) == ft
11691172
end
11701173
@test isintrinsic(fn)
11711174

@@ -1192,7 +1195,7 @@ end
11921195
fn = LLVM.Function(mod, intr, [LLVM.DoubleType(ctx)])
11931196
@test fn isa LLVM.Function
11941197
if supports_typed_ptrs
1195-
@test llvmeltype(fn) == ft
1198+
@test eltype(value_type(fn)) == ft
11961199
end
11971200
@test isintrinsic(fn)
11981201

test/instructions.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@
273273
@check_ir bitcastinst "bitcast i32 %0 to float"
274274
if supports_typed_ptrs
275275
i32ptr1 = parameters(fn)[5]
276-
i32ptr1typ = llvmtype(i32ptr1)
276+
i32ptr1typ = value_type(i32ptr1)
277277
i32ptr1typ2 = LLVM.PointerType(eltype(i32ptr1typ), 2)
278278
addrspacecastinst = addrspacecast!(builder, i32ptr1, i32ptr1typ2)
279279
@check_ir addrspacecastinst "addrspacecast i32* %4 to i32 addrspace(2)*"

0 commit comments

Comments
 (0)