llvm Won't generate debug line information - c++

I have the following code:
llvmbuilder->SetCurrentDebugLocation(llvm::DILocation::get(*llvmctx, 0, mypos, llvmsub));
Where llvmsub is constructed at each function definition beginning like so:
llvmsub = llvm::DISubprogram::getDistinct(*llvmctx, llvmcu->getFile(), currfunc->identifier, currfunc->linkage, llvmcu->getFile(), 0, nullptr, 0, nullptr, 0, 0, llvm::DINode::DIFlags::FlagZero, llvm::DISubprogram::toSPFlags(false, true, false), llvmcu);
Where currfunc->identifier, currfunc->linkage are strings - for my C function and llvmcu is defined as follows, in the beginning of my Translation Unit:
llvmcu = llvmdibuilder->createCompileUnit(llvm::dwarf::DW_LANG_C, llvmdibuilder->createFile(std::string{modulename}, ""), "regularc", false, "", 0);
I also set version like so:
mainmodule->addModuleFlag(llvm::Module::Warning, "Debug Info Version",
llvm::DEBUG_METADATA_VERSION);
The issue is that after generation (which looks like this):
0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "regularc", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
!1 = !DIFile(filename: "combined.pp_1", directory: "")
!2 = !{i32 2, !"Debug Info Version", i32 3}
!3 = !DILocation(line: 0, column: 49179, scope: !4)
!4 = distinct !DISubprogram(name: "checkkeyb", scope: !1, file: !1, spFlags: DISPFlagDefinition, unit: !0)
!5 = !DILocation(line: 0, column: 49215, scope: !4)
!6 = !DILocation(line: 0, column: 49227, scope: !4)
!7 = !DILocation(line: 0, column: 49241, scope: !4)
!8 = !DILocation(line: 0, column: 49255, scope: !4)
!9 = !DILocation(line: 0, column: 49288, scope: !4)
(The columns are so long because I actually store file offsets)
After I compiler with llc and then with clang -g I get no debug line information in lldb:
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x30)
* frame #0: 0x0000000100126cb0 libSDL-1.2.0.dylib`SDL_SetColorKey + 36
frame #1: 0x000000010000b444 a.out`vgainit + 76
frame #2: 0x000000010000f354 a.out`maininit + 28
frame #3: 0x000000010000fd48 a.out`main + 20
frame #4: 0x00000001ac887e50 dyld`start + 2544

Related

Google Sheets Formula help: IF reference cell is BLANK, then cell is BLANK

OK the solution should be simple... like =IF(AG3 = "", "")
but I am unable to add the clause to my current formula as seen below: Any suggestions?
=IF(
IF(AF3 <> "y",
SUM(IFNA(VLOOKUP($AG3, RICS_TimeClocks!Q$3:U, 4, 0), 0),
IFNA(VLOOKUP($AG3, RICS_TimeClocks!V$3:Z, 4, 0), 0))
,"0")
= "0", "", SUM(IFNA(VLOOKUP($AG3, RICS_TimeClocks!Q$3:U, 4, 0), 0),
IFNA(VLOOKUP($AG3, RICS_TimeClocks!V$3:Z, 4, 0), 0)))
Let's say your current formula is "FORMULA", you would have to do the following:
=IF(AG3="",,FORMULA)
Now replace FORMULA with your actual formula and you get
=IF(AG3="",,IF(
IF(AF3 <> "y",
SUM(IFNA(VLOOKUP($AG3, RICS_TimeClocks!Q$3:U, 4, 0), 0),
IFNA(VLOOKUP($AG3, RICS_TimeClocks!V$3:Z, 4, 0), 0))
,"0")
= "0", "", SUM(IFNA(VLOOKUP($AG3, RICS_TimeClocks!Q$3:U, 4, 0), 0),
IFNA(VLOOKUP($AG3, RICS_TimeClocks!V$3:Z, 4, 0), 0))))

Wrong number of arguments to IF

With the formula below, I keep receiving the error "Wrong number of arguments to IF. Expected between 2 and 3 arguments, but received 5 arguments."
Where have I gone wrong? It works up until a certain point and then the code throw up the above error... Am i missing a comma or bracket somewhere?
Any help greatly appreciated :)
=arrayformula(If(G5 <> "",if(H5 = "Y",if(C6 = "Sub-Total", ttime($M$3,vlookup(B5,Lookup!$A:$I,3,0)),if(AND(C7 = "Sub-Total",B6 <>"",B5 <>""),ttime($M$3,vlookup(B6,Lookup!$A:$I,3,0),vlookup(B5,Lookup!$A:$I,3,0)),if(AND(C8 = "Sub-Total",B7 <> "", B6 <> "",B5 <>""),ttime($M$3,vlookup(B7,Lookup!$A:$I,3,0),vlookup(B6,Lookup!$A:$I,3,0),vlookup(B5,Lookup!$A:$I,3,0)),if(AND(C9 = "Sub-Total",B8 <> "", B7 <> "",B6 <>"",B5 <> ""),ttime($M$3,vlookup(B8,Lookup!$A:$I,3,0),vlookup(B7,Lookup!$A:$I,3,0),vlookup(B6,Lookup!$A:$I,3,0)),vlookup(B5,Lookup!$A:$I,3,0)),if(AND(C10 = "Sub-Total",B9 <> "", B8 <> "",B7 <>"",B6 <> "",B5 <> ""),ttime($M$3,vlookup(B9,Lookup!$A:$I,3,0),vlookup(B8,Lookup!$A:$I,3,0),vlookup(B7,Lookup!$A:$I,3,0)),vlookup(B6,Lookup!$A:$I,3,0)),vlookup(B5,Lookup!$A:$I,3,0))))+IF(C6 = "sub-total","",if(B5 <> "", G6 * 3,""))+IF(C6 = "sub-total","",if(B5 <> "", "15","")),"Not Despatched"),""))
try:
=ARRAYFORMULA(
IF(G5<>"",
IF(H5="Y",
IF(C6="Sub-Total", TTIME($M$3, VLOOKUP(B5, Lookup!$A:$I, 3, 0)),
IF(AND(C7="Sub-Total", B6<>"", B5<>""), TTIME($M$3, VLOOKUP(B6, Lookup!$A:$I, 3, 0),
VLOOKUP(B5, Lookup!$A:$I, 3, 0)),
IF(AND(C8="Sub-Total", B7<>"", B6<>"", B5<>""), TTIME($M$3, VLOOKUP(B7, Lookup!$A:$I, 3, 0),
VLOOKUP(B6, Lookup!$A:$I, 3, 0),
VLOOKUP(B5, Lookup!$A:$I, 3, 0)),
IF(AND(C9="Sub-Total", B8<>"", B7<>"", B6<>"", B5<>""), TTIME($M$3, VLOOKUP(B8, Lookup!$A:$I, 3, 0),
VLOOKUP(B7, Lookup!$A:$I, 3, 0),
VLOOKUP(B6, Lookup!$A:$I, 3, 0),
VLOOKUP(B5, Lookup!$A:$I, 3, 0)),
IF(AND(C10="Sub-Total", B9<>"", B8<>"", B7<>"", B6<>"", B5<>""), TTIME($M$3, VLOOKUP(B9, Lookup!$A:$I, 3, 0),
VLOOKUP(B8, Lookup!$A:$I, 3, 0),
VLOOKUP(B7, Lookup!$A:$I, 3, 0),
VLOOKUP(B6, Lookup!$A:$I, 3, 0),
VLOOKUP(B5, Lookup!$A:$I, 3, 0)))))))+
IF(C6="sub-total",,
IF(B5<>"", G6*3, ))+
IF(C6="sub-total",,
IF(B5<>"", "15", )), "Not Despatched"), ))

LLVM IR: expose variables to GDB?

I am writing a custom programming language. I am generating LLVM IR as an intermediate (via LLVMlite), and I want to expose variables to GDB. This is an example of the generated IR:
; ModuleID = "tests/debuginfo.xan"
source_filename = "debuginfo.xan"
target triple = "x86_64-unknown-linux-gnu"
target datalayout = ""
define void #"main"(i32 %".1", i8** %".2") !dbg !10
{
entry:
%"$argc" = alloca i32
store i32 %".1", i32* %"$argc"
%"$argv" = alloca i8**
store i8** %".2", i8*** %"$argv"
%"$a" = alloca i32
call void #"llvm.dbg.declare"(metadata i32* %"$a", metadata !12, metadata !13), !dbg !14
call void #"llvm.dbg.value"(metadata i32 0, metadata !12, metadata !13), !dbg !15
store i32 0, i32* %"$a"
ret void
}
declare i32 #"printf"(i8* %".1", ...)
declare void #"llvm.dbg.declare"(metadata %".1", metadata %".2", metadata %".3") nounwind readnone
declare void #"llvm.dbg.value"(metadata %".1", metadata %".2", metadata %".3") nounwind readnone
#"NULL" = internal constant i8* inttoptr (i32 0 to i8*)
!llvm.dbg.cu = !{ !2 }
!llvm.module.flags = !{ !3, !4, !5 }
!llvm.ident = !{ !6 }
!gen-by = !{ !16 }
!0 = !DIFile(directory: "/home/proc-daemon/Dropbox/Xanathar/xanathar/tests", filename: "debuginfo.xan")
!1 = !{ }
!2 = distinct !DICompileUnit(emissionKind: FullDebug, enums: !1, file: !0, isOptimized: false, language: DW_LANG_Python, producer: "Xanathar v. a0.0.1", runtimeVersion: 0)
!3 = !{ i32 2, !"Dwarf Version", i32 4 }
!4 = !{ i32 2, !"Debug Info Version", i32 3 }
!5 = !{ i32 1, !"wchar_size", i32 4 }
!6 = !{ !"Xanathar a0.0.1" }
!7 = !DIDerivedType(baseType: null, size: 64, tag: DW_TAG_pointer_type)
!8 = !{ !7 }
!9 = !DISubroutineType(types: !8)
!10 = distinct !DISubprogram(file: !0, isDefinition: true, isLocal: false, isOptimized: false, name: "main", scope: !0, scopeLine: 1, type: !9, unit: !2, variables: !1)
!11 = !DIBasicType(encoding: DW_ATE_signed, name: "int", size: 4)
!12 = !DILocalVariable(file: !0, line: 1, name: "a", scope: !10, type: !11)
!13 = !DIExpression()
!14 = !DILocation(column: 1, line: 1, scope: !10)
!15 = !DILocation(column: 1, line: 2, scope: !10)
!16 = !{ !"Xanathar" }
I removed the previous edits. The generation code can be found here. As you can see, I have a variable $a that I am trying to declare with llvm.dbg.declare. However, although objdump --sym lists debug info (pastebin), gdb gives No locals. upon running info locals. What is the correct way to export variables? How could I generate that with LLVMlite?
Here is the compilation code:
def compile(self, name, so):
sys.stderr.write('---COMPILE---\n')
if not so:
command = 'clang {0} -g -fstandalone-debug -O0 ' + name + '.ll '
else:
command = 'clang {0} -g -fstandalone-debug -O0 -shared -undefined dynamic_lookup ' + name + '.ll '
command = command.format(self.flags)
for i in self.LOADED_MODULES:
if i["type"] == 'LINKED_SO':
command += os.path.abspath(i["FILE"]) + ' '
command = command + '-o ' + name + ('.so' if so else '.o')
# print(command)
os.system(command)
Here is the LLVM confirmation code:
def _compile_ir(self, builder):
"""
Compile the LLVM IR string with the given engine.
The compiled module object is returned.
"""
# Create a LLVM module object from the IR
self.builder = builder
self.builder.ret_void()
self.module.add_named_metadata("gen-by", ["Xanathar"])
llvm_ir = str(self.module)
try:
mod = self.binding.parse_assembly(llvm_ir)
except RuntimeError as e:
sys.stderr.write("[ERR] LLVM parsing error!\n")
sys.stderr.write(str(e))
if "expected instruction opcode" in str(e):
sys.stderr.write("\nDid you forget to return from a function?")
exit(1)
mod = 0 # Otherwise PyCharm complains about mod's usage below
mod.verify()
# Now add the module and make sure it is ready for execution
self.engine.add_module(mod)
self.engine.finalize_object()
self.engine.run_static_constructors()
return mod
(After this, the module is written to a file)
EDIT 5 (or 6, idk): As Chirag Patel suggested, I added a new debug statement to the ret instruction. Then, this happened.
(gdb) r
Starting program: /home/proc-daemon/Dropbox/Xanathar/xanathar/tests/debuginfo.xan.o
Breakpoint 1, main () at debuginfo.xan:1
1 declare $a as int32;
(gdb) info locals
Segmentation fault (core dumped)

How to determine if a function parameter is annotated?

I'm annotating function parameters as shown below with the label bar.
int foo (char* s __attribute__((annotate("bar")))) {
...
}
Next, I am running a function pass. How can I determine if a given function argument is annotated with the label bar?
You will have to read the llvm.var.annotation and llvm.dbg.declare intrinsics.
More specifically, here is the llvm-ir generated by your code above:
#.str = private unnamed_addr constant [4 x i8] c"bar\00", section "llvm.metadata"
#.str.1 = private unnamed_addr constant [75 x i8] c"/tmp/compiler-explorer-compiler117030-12962-1rhu4lb.ojfaiz4cxr/example.cpp\00", section "llvm.metadata"
; Function Attrs: nounwind uwtable
define i32 #foo(char*)(i8*) #0 !dbg !6 {
%2 = alloca i8*, align 8
store i8* %0, i8** %2, align 8
call void #llvm.dbg.declare(metadata i8** %2, metadata !12, metadata !13), !dbg !14
%3 = bitcast i8** %2 to i8*
call void #llvm.var.annotation(i8* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* #.str, i32 0, i32 0), i8* getelementptr inbounds ([75 x i8], [75 x i8]* #.str.1, i32 0, i32 0), i32 1)
ret i32 0, !dbg !15
}
!6 = distinct !DISubprogram(name: "foo", linkageName: "foo(char*)", scope: !1, file: !1, line: 1, type: !7, isLocal: false, isDefinition: true, scopeLine: 1, flags: DIFlagPrototyped, isOptimized: false, unit: !0, variables: !2)
!7 = !DISubroutineType(types: !8)
!8 = !{!9, !10}
!9 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed)
!10 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !11, size: 64, align: 64)
!11 = !DIBasicType(name: "char", size: 8, align: 8, encoding: DW_ATE_signed_char)
!12 = !DILocalVariable(name: "s", arg: 1, scope: !6, file: !1, line: 1, type: !10)
The dbg.declare instruction tells you that %2 is actually the first parameter of the function (named s).
%3 is a bitcast of %2, so basically an alias.
And the the llvm.var.annotation instruction tells you that %2 is annotated with the constant string #str, which value is "bar".

LLVM tutorial 3.6 linker error when trying to compile output from chapter 8

I am working through the llvm tutorial:
http://llvm.org/releases/3.6.0/docs/tutorial/index.html
The code from chapter 8 compiles just fine and emits the IR however I am unable to compile the emitted IR. The code listing is copy and pasted to reduce the chances that I have a typo in my code somewhere. The only thing I have modified is the build command since llvm-config defaults to an older version.
build command:
clang++ -g -O3 toy.cpp `llvm-config-3.6 --cxxflags --ldflags --system-libs --libs core mcjit native` -o toy
to run it (I removed the & from the original command since that seems to throw an error):
./toy < programs/basic.ks | clang-3.6 -x ir -
The output is listed below:
; ModuleID = 'my cool jit'
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
declare double #printd(double)
define double #main() {
entry:
%calltmp = call double #printd(double 4.200000e+01), !dbg !14
ret double %calltmp, !dbg !14
}
!llvm.module.flags = !{!0, !1}
!llvm.dbg.cu = !{!2}
!0 = !{i32 2, !"Debug Info Version", i32 2}
!1 = !{i32 2, !"Dwarf Version", i32 2}
!2 = !{!"0x11\002\00Kaleidoscope Compiler\000\00\000\00\001", !3, !4, !4, !5, !4, !4} ; [ DW_TAG_compile_unit ] [./fib.ks] [DW_LANG_C]
!3 = !{!"fib.ks", !"."}
!4 = !{}
!5 = !{!6, !11}
!6 = !{!"0x2e\00printd\00printd\00\001\000\001\000\000\00256\000\001", !3, !7, !8, null, double (double)* #printd, null, null, !4} ; [ DW_TAG_subprogram ] [line 1] [def] [printd]
!7 = !{!"0x29", !3} ; [ DW_TAG_file_type ] [./fib.ks]
!8 = !{!"0x15\00\000\000\000\000\000\000", null, null, null, !9, null, null, null} ; [ DW_TAG_subroutine_type ] [line 0, size 0, align 0, offset 0] [from ]
!9 = !{!10, !10}
!10 = !{!"0x24\00double\000\0064\0064\000\000\004", null, null} ; [ DW_TAG_base_type ] [double] [line 0, size 64, align 64, offset 0, enc DW_ATE_float]
!11 = !{!"0x2e\00main\00main\00\002\000\001\000\000\00256\000\002", !3, !7, !12, null, double ()* #main, null, null, !4} ; [ DW_TAG_subprogram ] [line 2] [def] [main]
!12 = !{!"0x15\00\000\000\000\000\000\000", null, null, null, !13, null, null, null} ; [ DW_TAG_subroutine_type ] [line 0, size 0, align 0, offset 0] [from ]
!13 = !{!10}
!14 = !MDLocation(line: 2, column: 8, scope: !11)
warning: overriding the module target triple with x86_64-apple-macosx10.10.0
1 warning generated.
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The example code prints out the IR using the dump() method, which prints to stderr. You should redirect stderr to stdout before piping, like this:
./toy < programs/basic.ks 2>&1 | clang-3.6 -x ir -