Skip to content

Commit

Permalink
fixup! support for DIBuilder on LLVM8
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsamaroo committed Mar 8, 2021
1 parent 8df75a7 commit ed9a4cb
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/LLVM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ include("ir.jl")
include("bitcode.jl")
include("transform.jl")
include("debuginfo.jl")
if libllvm_version >= v"8.0.0"
if Base.libllvm_version >= v"8.0.0"
include("dibuilder.jl")
end

Expand Down
66 changes: 37 additions & 29 deletions src/dibuilder.jl
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
export DIBuilder, DICompileUnit, DILexicalBlock, DIFunction
export DIBuilder, DIFile, DICompileUnit, DILexicalBlock, DIFunction

@checked struct DIBuilder
ref::API.LLVMDIBuilderRef
end
reftype(::Type{DIBuilder}) = API.DILLVMDIBuilderRef

# LLVMCreateDIBuilderDisallowUnresolved
DIBuilder(mod::Module) = DIBuilder(API.LLVMCreateDIBuilder(ref(mod)))
DIBuilder(mod::Module) = DIBuilder(API.LLVMCreateDIBuilder(mod))

dispose(builder::DIBuilder) = API.LLVMDisposeDIBuilder(ref(builder))
finalize(builder::DIBuilder) = API.LLVMDIBuilderFinalize(ref(builder))
dispose(builder::DIBuilder) = API.LLVMDisposeDIBuilder(builder)
finalize(builder::DIBuilder) = API.LLVMDIBuilderFinalize(builder)

struct DICompileUnit
Base.unsafe_convert(::Type{API.LLVMDIBuilderRef}, builder::DIBuilder) = builder.ref

struct DIFile
file::String
dir::String
end

struct DICompileUnit
file::Metadata
language::API.LLVMDWARFSourceLanguage
producer::String
sysroot::String
sdk::String
flags::String
optimized::Core.Bool
version::Int
end

function compileunit!(builder::DIBuilder, cu::DICompileUnit)
file = file!(builder, cu.file, cu.dir)
md = API.LLVMDIBuilderCreateCompileUnit(
ref(builder),
builder,
cu.language,
ref(file),
cu.file,
cu.producer, convert(Csize_t, length(cu.producer)),
cu.optimized ? LLVM.True : LLVM.False,
cu.flags, convert(Csize_t, length(cu.flags)),
Expand All @@ -36,15 +42,17 @@ function compileunit!(builder::DIBuilder, cu::DICompileUnit)
#=DWOId=# 0,
#=SplitDebugInlining=# LLVM.True,
#=DebugInfoForProfiling=# LLVM.False,
cu.sysroot, convert(Csize_t, length(cu.sysroot)),
cu.sdk, convert(Csize_t, length(cu.sdk)),
)
return Metadata(md)
end

function file!(builder::DIBuilder, filename, directory)
function file!(builder::DIBuilder, file::DIFile)
md = API.LLVMDIBuilderCreateFile(
ref(builder),
filename, convert(Csize_t, length(filename)),
directory, convert(Csize_t, length(directory))
builder,
file.file, convert(Csize_t, length(file.file)),
file.dir, convert(Csize_t, length(file.dir))
)
return Metadata(md)
end
Expand All @@ -56,10 +64,10 @@ struct DILexicalBlock
end

function lexicalblock!(builder::DIBuilder, scope::Metadata, block::DILexicalBlock)
md = API.LLVMDIBuilerCreateLexicalBlock(
ref(builder),
ref(scope),
ref(block.file),
md = API.LLVMDIBuilderCreateLexicalBlock(
builder,
scope,
block.file,
convert(Cuint, block.line),
convert(Cuint, block.column)
)
Expand All @@ -68,9 +76,9 @@ end

function lexicalblock!(builder::DIBuilder, scope::Metadata, file::Metadata, discriminator)
md = API.LLVMDIBuilderCreateLexicalBlockFile(
ref(builder),
ref(scope),
ref(file),
builder,
scope,
file,
convert(Cuint, discriminator)
)
Metadata(md)
Expand All @@ -91,13 +99,13 @@ end

function subprogram!(builder::DIBuilder, scope::Metadata, f::DIFunction)
md = API.LLVMDIBuilderCreateFunction(
ref(builder),
ref(scope),
builder,
scope,
f.name, convert(Csize_t, length(f.name)),
f.linkageName, convert(Csize_t, length(f.linkageName)),
ref(f.file),
f.file,
f.line,
ref(f.type),
f.type,
f.localToUnit ? LLVM.True : LLVM.False,
f.isDefinition ? LLVM.True : LLVM.False,
convert(Cuint, f.scopeLine),
Expand All @@ -111,7 +119,7 @@ end

function basictype!(builder::DIBuilder, name, size, encoding)
md = LLVM.API.LLVMDIBuilderCreateBasicType(
ref(builder),
builder,
name,
convert(Csize_t, length(name)),
convert(UInt64, size),
Expand All @@ -123,8 +131,8 @@ end

function pointertype!(builder::DIBuilder, pointee::Metadata, size, as, align=0, name="")
md = LLVM.API.LLVMDIBuilderCreatePointerType(
ref(builder),
ref(pointee),
builder,
pointee,
convert(UInt64, size),
convert(UInt32, align),
convert(Cuint, as),
Expand All @@ -137,8 +145,8 @@ end
function subroutinetype!(builder::DIBuilder, file::Metadata, rettype, paramtypes...)
params = collect(ref(x) for x in (rettype, paramtypes...))
md = LLVM.API.LLVMDIBuilderCreateSubroutineType(
ref(builder),
ref(file),
builder,
file,
params,
length(params),
LLVM.API.LLVMDIFlagZero
Expand Down
23 changes: 18 additions & 5 deletions test/dibuilder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ end
Context() do ctx
LLVM.Module("SomeModule", ctx) do mod
dibuilder = DIBuilder(mod)
file = LLVM.file!(dibuilder, "test.jl", "src")
cu = LLVM.compileunit!(dibuilder, LLVM.API.LLVMDWARFSourceLanguageJulia, file, "LLVM.jl Tests", LLVM.False,
"", UInt32(0), "", LLVM.API.LLVMDWARFEmissionFull, UInt32(4), LLVM.False, LLVM.False)
file = DIFile("test.jl", "src")
file_md = LLVM.file!(dibuilder, file)
cu = DICompileUnit(file_md, LLVM.API.LLVMDWARFSourceLanguageJulia, "LLVM.jl Tests", "/", "LLVM.jl", "", false, 4)
cu_md = LLVM.compileunit!(dibuilder, cu)

Builder(ctx) do builder
ft = LLVM.FunctionType(LLVM.VoidType(ctx), [LLVM.Int32Type(ctx)])
Expand All @@ -21,8 +22,9 @@ LLVM.Module("SomeModule", ctx) do mod
entrybb = BasicBlock(fn, "entry")
position!(builder, entrybb)

md = LLVM.location!(ctx, UInt32(1), UInt32(1), cu) # could also be file
debuglocation!(builder, LLVM.MetadataAsValue(LLVM.API.LLVMMetadataAsValue(LLVM.ref(ctx), md)))
block = DILexicalBlock(file_md, 1, 1)
block_md = LLVM.lexicalblock!(dibuilder, file_md, block) # could also be file
debuglocation!(builder, LLVM.MetadataAsValue(LLVM.API.LLVMMetadataAsValue(ctx, block_md)))
ret!(builder)
end

Expand All @@ -32,8 +34,19 @@ LLVM.Module("SomeModule", ctx) do mod
fun = functions(mod)["SomeFunction"]
bb = entry(fun)
inst = first(instructions(bb))
@test !isempty(metadata(inst))
inst_str = sprint(io->Base.show(io, inst))
@test occursin("!dbg", inst_str)
@show mod inst

@test !isempty(metadata(inst))
mod_str = sprint(io->Base.show(io, mod))
@test occursin("!llvm.dbg.cu", mod_str)
@test occursin("!DICompileUnit", mod_str)
@test occursin("!DIFile", mod_str)
@test occursin("!DILexicalBlock", mod_str)
@test !occursin("scope: null", mod_str)

strip_debuginfo!(mod)
@test isempty(metadata(inst))
end
Expand Down
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ end
if LLVM.version() >= v"8.0.0"
include("dibuilder.jl")
end

#=
include("Kaleidoscope.jl")
include("examples.jl")
include("interop.jl")

=#
end

0 comments on commit ed9a4cb

Please sign in to comment.