From ed9a4cb667b2db47d34af1b14ea7e169d0c7207f Mon Sep 17 00:00:00 2001 From: Julian P Samaroo Date: Mon, 8 Mar 2021 10:44:38 -0600 Subject: [PATCH] fixup! support for DIBuilder on LLVM8 --- src/LLVM.jl | 2 +- src/dibuilder.jl | 66 ++++++++++++++++++++++++++--------------------- test/dibuilder.jl | 23 +++++++++++++---- test/runtests.jl | 4 +-- 4 files changed, 58 insertions(+), 37 deletions(-) diff --git a/src/LLVM.jl b/src/LLVM.jl index dab4cbca..ee0907e6 100644 --- a/src/LLVM.jl +++ b/src/LLVM.jl @@ -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 diff --git a/src/dibuilder.jl b/src/dibuilder.jl index 53c6c267..4dff3dd9 100644 --- a/src/dibuilder.jl +++ b/src/dibuilder.jl @@ -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)), @@ -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 @@ -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) ) @@ -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) @@ -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), @@ -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), @@ -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), @@ -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 diff --git a/test/dibuilder.jl b/test/dibuilder.jl index 9dac330d..e8e7c647 100644 --- a/test/dibuilder.jl +++ b/test/dibuilder.jl @@ -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)]) @@ -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 @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index a375ee36..d4331b8b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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