Skip to content

Commit 65c308d

Browse files
committed
Specialized isempty functions.
Prevents having to iterate values if size is unknown.
1 parent 595ade3 commit 65c308d

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

src/core/basicblock.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,7 @@ end
6161
Base.last(iter::BasicBlockInstructionSet) =
6262
Instruction(API.LLVMGetLastInstruction(blockref(iter.bb)))
6363

64+
Base.isempty(iter::BasicBlockInstructionSet) =
65+
API.LLVMGetLastInstruction(blockref(iter.bb)) == C_NULL
66+
6467
Base.IteratorSize(::BasicBlockInstructionSet) = Base.SizeUnknown()

src/core/function.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ end
9494
Base.last(iter::FunctionParameterSet) =
9595
Argument(API.LLVMGetLastParam(ref(iter.f)))
9696

97+
Base.isempty(iter::FunctionParameterSet) =
98+
API.LLVMGetLastParam(ref(iter.f)) == C_NULL
99+
97100
Base.length(iter::FunctionParameterSet) = API.LLVMCountParams(ref(iter.f))
98101

99102
# NOTE: optimized `collect`
@@ -122,6 +125,9 @@ end
122125
Base.last(iter::FunctionBlockSet) =
123126
BasicBlock(API.LLVMGetLastBasicBlock(ref(iter.f)))
124127

128+
Base.isempty(iter::FunctionBlockSet) =
129+
API.LLVMGetLastBasicBlock(ref(iter.f)) == C_NULL
130+
125131
Base.length(iter::FunctionBlockSet) = API.LLVMCountBasicBlocks(ref(iter.f))
126132

127133
# NOTE: optimized `collect`

src/core/module.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ end
123123
Base.last(iter::ModuleGlobalSet) =
124124
GlobalVariable(API.LLVMGetLastGlobal(ref(iter.mod)))
125125

126+
Base.isempty(iter::ModuleGlobalSet) =
127+
API.LLVMGetLastGlobal(ref(iter.mod)) == C_NULL
128+
126129
Base.IteratorSize(::ModuleGlobalSet) = Base.SizeUnknown()
127130

128131
# partial associative interface
@@ -157,6 +160,9 @@ end
157160
Base.last(iter::ModuleFunctionSet) =
158161
Function(API.LLVMGetLastFunction(ref(iter.mod)))
159162

163+
Base.isempty(iter::ModuleFunctionSet) =
164+
API.LLVMGetLastFunction(ref(iter.mod)) == C_NULL
165+
160166
Base.IteratorSize(::ModuleFunctionSet) = Base.SizeUnknown()
161167

162168
# partial associative interface

test/core.jl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,9 @@ end
411411
# global variables
412412
Context() do ctx
413413
LLVM.Module("SomeModule", ctx) do mod
414+
@test isempty(globals(mod))
414415
gv = GlobalVariable(mod, LLVM.Int32Type(ctx), "SomeGlobal")
416+
@test !isempty(globals(mod))
415417

416418
show(devnull, gv)
417419

@@ -600,11 +602,14 @@ Context() do ctx
600602
LLVM.Module("SomeModule", ctx) do mod
601603
st = LLVM.StructType("SomeType", ctx)
602604
ft = LLVM.FunctionType(st, [st])
605+
@test isempty(functions(mod))
603606

604607
dummyfn = LLVM.Function(mod, "SomeFunction", ft)
605608
let fns = functions(mod)
606609
@test eltype(fns) == LLVM.Function
607610

611+
@test !isempty(fns)
612+
608613
@test first(fns) == dummyfn
609614
@test last(fns) == dummyfn
610615

@@ -738,12 +743,14 @@ Context() do ctx
738743
LLVM.Module("SomeModule", ctx) do mod
739744
ft = LLVM.FunctionType(LLVM.VoidType(ctx))
740745
fn = LLVM.Function(mod, "SomeFunction", ft)
746+
@test isempty(blocks(fn))
741747

742748
entrybb = BasicBlock(fn, "SomeBasicBlock")
743749
@test entry(fn) == entrybb
744750
let bbs = blocks(fn)
745751
@test eltype(bbs) == BasicBlock
746752

753+
@test !isempty(bbs)
747754
@test length(bbs) == 1
748755

749756
@test first(bbs) == entrybb
@@ -799,13 +806,15 @@ LLVM.Module("SomeModule", ctx) do mod
799806

800807
bb2 = BasicBlock(fn, "SomeOtherBasicBlock", ctx)
801808
@test LLVM.parent(bb2) == fn
809+
@test isempty(instructions(bb2))
802810

803811
bb1 = BasicBlock(bb2, "SomeBasicBlock", ctx)
804812
@test LLVM.parent(bb2) == fn
805813
position!(builder, bb1)
806814
brinst = br!(builder, bb2)
807815
position!(builder, bb2)
808816
retinst = ret!(builder)
817+
@test !isempty(instructions(bb2))
809818

810819
@test terminator(bb1) == brinst
811820
@test terminator(bb2) == retinst
@@ -835,8 +844,13 @@ end
835844
Context() do ctx
836845
Builder(ctx) do builder
837846
LLVM.Module("SomeModule", ctx) do mod
838-
ft = LLVM.FunctionType(LLVM.VoidType(ctx), [LLVM.Int1Type(ctx), LLVM.Int1Type(ctx)])
847+
ft = LLVM.FunctionType(LLVM.VoidType(ctx))
839848
fn = LLVM.Function(mod, "SomeFunction", ft)
849+
@test isempty(parameters(fn))
850+
851+
ft = LLVM.FunctionType(LLVM.VoidType(ctx), [LLVM.Int1Type(ctx), LLVM.Int1Type(ctx)])
852+
fn = LLVM.Function(mod, "SomeOtherFunction", ft)
853+
@test !isempty(parameters(fn))
840854

841855
bb1 = BasicBlock(fn, "entry", ctx)
842856
bb2 = BasicBlock(fn, "then", ctx)

0 commit comments

Comments
 (0)