Skip to content

Commit e1b74a3

Browse files
committed
Object-based iteration for module flags.
1 parent cfa2462 commit e1b74a3

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

src/core/instructions.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ predicate_real(inst::Instruction) = API.LLVMGetFCmpPredicate(ref(inst))
4545

4646

4747
## metadata iteration
48+
# TODO: doesn't actually iterate, since we can't list the available keys
4849

4950
@enum(MD, MD_dbg = 0,
5051
MD_tbaa = 1,
@@ -72,8 +73,6 @@ predicate_real(inst::Instruction) = API.LLVMGetFCmpPredicate(ref(inst))
7273

7374
export InstructionMetadataDict
7475

75-
# doesn't print, because we don't have length. we can't iterate keys?
76-
7776
struct InstructionMetadataDict <: AbstractDict{MD,MetadataAsValue}
7877
inst::Instruction
7978
end

src/core/module.jl

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,30 @@ function Base.getindex(iter::ModuleFunctionSet, name::String)
177177
return Function(objref)
178178
end
179179

180-
# flags
180+
181+
## module flag iteration
182+
# TODO: doesn't actually iterate, since we can't list the available keys
183+
181184
if libllvm_version >= v"8.0"
182-
addflag!(mod::Module, behavior, key, val) = LLVM.API.LLVMAddModuleFlag(ref(mod), behavior, key, length(key), LLVM.ref(Metadata(val)))
185+
186+
export flags
187+
188+
struct ModuleFlagDict <: AbstractDict{String,Metadata}
189+
mod::Module
190+
end
191+
192+
flags(mod::Module) = ModuleFlagDict(mod)
193+
194+
Base.haskey(iter::ModuleFlagDict, name::String) =
195+
API.LLVMGetModuleFlag(ref(iter.mod), name, length(name)) != C_NULL
196+
197+
function Base.getindex(iter::ModuleFlagDict, name::String)
198+
objref = API.LLVMGetModuleFlag(ref(iter.mod), name, length(name))
199+
objref == C_NULL && throw(KeyError(name))
200+
return Metadata(objref)
201+
end
202+
203+
Base.push!(iter::ModuleFlagDict, behavior::LLVM.API.LLVMModuleFlagBehavior, name::String, val::Metadata) =
204+
API.LLVMAddModuleFlag(ref(iter.mod), behavior, name, length(name), ref(val))
205+
183206
end

test/core.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,19 @@ LLVM.Module("SomeModule", ctx) do mod
528528
dummyLayout = "e-p:64:64:64"
529529
datalayout!(mod, dummyLayout)
530530
@test convert(String, datalayout(mod)) == dummyLayout
531+
532+
if LLVM.version() >= v"8.0"
533+
md = Metadata(ConstantInt(42))
534+
535+
mod_flags = flags(mod)
536+
push!(mod_flags, LLVM.API.LLVMModuleFlagBehaviorError, "foobar", md)
537+
538+
@test occursin("!llvm.module.flags = !{!0}", sprint(io->show(io,mod)))
539+
@test occursin(r"!0 = !\{i\d+ 1, !\"foobar\", i\d+ 42\}", sprint(io->show(io,mod)))
540+
541+
@test mod_flags["foobar"] == md
542+
@test_throws KeyError mod_flags["foobaz"]
543+
end
531544
end
532545
end
533546

0 commit comments

Comments
 (0)