LLVM printing IR calls - llvm

I am following LLVM Kaleidoscope tutorial, 3rd chapter and it seems that additional information is being printed out on compiled code when an operation is run.
ready> 4+5;
Read top-level expression:
define double #0() {
entry:
ret double 9.000000e+00
}
But when I compile everything myself, i only get:
ready> 4+5;
ready> Parsed a top-level expr
I the code can be found on https://llvm.org/docs/tutorial/MyFirstLanguageFrontend/LangImpl03.html, I compile it the same way as in the tutorial.
clang++ -g -O3 toy.cpp `llvm-config-11 --cxxflags --ldflags --system-libs --libs core` -o toy
It seems that in example from the tutorial it prints Read top-level expression: but I get Parsed top-level expression:. I am very new to LLVM so apologies for a trivial question.

The reason you're getting a Parsed a top-level expr is because you most likely changed that string yourself.
If you visit the body for HandleTopLevelExpression, you see where the tutorial prints this message.
https://llvm.org/docs/tutorial/MyFirstLanguageFrontend/LangImpl03.html#full-code-listing
The printing of LLVM IR to stderr is done with various llvm::Value::print, called on the line below the one where it writes Read top-level expression.
To conclude, your code is missing parts from the code listing in the tutorial

Related

Object code generation for new RISCV instruction emitted by LLVM backend

From https://github.com/riscv/riscv-llvm,
Using the llvm-riscv is fairly simple to build a full executable
however you need riscv64-unknown-*-gcc to do the assembling and
linking. An example of compiling hello world:
$ clang -target riscv64 -mriscv=RV64IAMFD -S hello.c -o hello.S
$ riscv64-unknown-elf-gcc -o hello.riscv hello.S
My question is: if I change the LLVM backend and get it to emit a new instruction in the hello.S file, how will riscv64-unknown-elf-gcc know how to convert it into object code? Do I also need to make changes in riscv64-unknown-elf-gcc so that it knows the format of the new instruction?
riscv64-unknown-elf-gcc calls as, i.e. usually GNU as from the binutils to assemble assembly code (i.e. hello.S in your snippet) into executable machine code. Thus you would have to modify the binutils if you want to assemble a new instruction.

Fatal error in reader: makefile, unexpected end of line seen [duplicate]

I am having some issues with a makefile I am creating for a school project. I am compiling and assembling a C file and a SPARC assembly file (respectively) and linking them. I'm working in a Unix environment. Here is the makefile:
proj09.exe: proj09.driver.o proj09.support.o
<tab>gcc -Wall proj09.driver.o proj09.support.o -o proj09.exe
proj09.driver.o: proj09.driver.c /user/cse320/Projects/project09.support.h
<tab>gcc -Wall -c proj09.driver.c /user/cse320/Projects/project09.support.h
proj09.support.o: proj09.support.s
<tab>gcc -Wall proj09.support.s
When I try to make it, though, I get a reader error, specifically:
"Fatal error in reader: proj09.makefile, line 2: Unexpected end of line seen"
Now I know that usually this means that something is formatted incorrectly, but I have no idea what it could be in this case. Also, I am not 100% sure that this is the correct code for the makefile (specifically the assembling of my support.s file, and the linking of both files). I apologize if this is a repeat question, I looked through the archives beforehand and couldn't find anything of use. Any help would be greatly appreciated!
EDIT: I don't see why this would make a difference, but I am using gedit to actually write the code and then transferring the files into SSH for linking.
As Joachim told you, the lines should be indented by tab, not by spaces, so the second line should look like:
[TAB]gcc -Wall proj09.driver.o proj09.support.o -o proj09.exe[NEWLINE]
where [TAB] means TAB character.
Also there shouldn't be any spaces after the command. That's why I've put [NEWLINE] char.
Aside from the spaces and tabs, this doesn't generate an object file, shouldn't even compile (unless it has main()):
gcc -Wall proj09.support.s
You should use -c here too:
gcc -Wall -c proj09.support.s
Note: if you're working on Unix/Linux lose the .exe

lldb debugger cannot print structure contents

I've compiled a small program that uses asl_log, and when running in lldb, it failed to print the contents of a global variable from type 'aslclient', although i compiled in debug mode ('-g' flag).
perhaps you can tell me if this is related to the following bug, and how to workaround this problem
[lldb-dev] [Bug 16191] New: LLDB fails to evaluate expressions that
dereference a struct when inferior is built with recent Clang
the input from debugger :
(lldb) print log_asl_client
(aslclient) $5 = 0x0000000100200000
(lldb) print *log_asl_client
(lldb) print *log_asl_client
error: incomplete type '__asl_object_s' where a complete type is required
note: forward declaration of '__asl_object_s'
error: 1 errors parsing expression
my compilation command :
clang -g -c -Wall -DDEBUG=1 example.c -o example.o
clang example.o -o example
the code :
aslclient log_asl_client;
...
int main(int argc, char * const *argv) {
...
log_asl_client = asl_open(identity, facility, client_opts);
...
--> at this point i initiate the print command in debug mode.
the version i use :
clang --version
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.3.0
Thread model: posix
thanks,
The debug information has a record for the forward reference to __asl_object_s, but not for the full type. This isn't entirely surprising in this particular case, since the only appearance of __asl_object_s in the public header files on OS X is:
typedef struct __asl_object_s *asl_object_t;
so this is an opaque reference to the struct, and there isn't a real definition anywhere. Presumably __asl_object_s is a placeholder and the pointer gets cast to whatever it really is when it is used.
Anyway, the debugger isn't refusing to show you something, there's actually nothing there to see...

Using LLVM opt with built-in passes

I have successfully run llvm opt with my toy transformation pass but do not see how to use 'opt' with built-in transformation passes http://llvm.org/docs/Passes.html#introduction
I have an empty hi.c file
int main(){
}
For example, if I want to use -instcount pass,
opt -instcount hi.c
gives me strange error.
opt: hi.c:1:1: error: expected top-level entity
int main(){
^
Use opt -instcount hi.bc does not work neither, with
WARNING: You're attempting to print out a bitcode file.
This is inadvisable as it may cause display problems. If
you REALLY want to taste LLVM bitcode first-hand, you
can force output with the `-f' option.
If I use opt -inst-count -f hi.bc, the output is a messy bitcode.
Question: how should we use 'opt' with built-in transformation passes (those from the link above)? Thanks for your ideas. 'opt -help' says
opt [options] <input bitcode file>
but my example above 'opt -instcount hi.bc' does not work as expected (see above).
At first: opt only works on bitcode / readable LLVM IR files. So passing a .c file will never work.
You have to compile the .c file first with clang:
clang -emit-llvm in.c -o input.bc
The Warning you encounter says basicly everything:
WARNING: You're attempting to print out a bitcode file. This is
inadvisable as it may cause display problems. If you REALLY want to
taste LLVM bitcode first-hand, you can force output with the `-f'
option.
opt has as output the probably modified bitcode file and since you do not support an output file it will print it to stdout. That is why you get "messy" bitcode.
To use opt the way it should be you can use /dev/null to get rid of the output:
opt -inst-count input.bc -o /dev/null
or support an output file
opt -inst-count input.bc -o output.bc
or print the output as readable LLVM IR to stdout
opt -inst-count input.bc -S
or print the ouptut as readable LLVM IR file to disk
opt -inst-count input.bc -S -o output.ll

Reading gsettings from C++ program

I need to programmatically get the value of com.ubuntu.user-interface scale-factor from gsettings in my C++ program. Is there any elegant way to do this, instead of calling gsettings binary and parsing it's output?
There is a C++ binding to gsettings in glibmm. With it, reading a value from a schema can be done as shown below. Note that I do not have an Ubuntu system on which to test this, so specifics rely on a short look into the documentation that told me scale-factor is an integral value. With this in mind:
#include <giomm/settings.h>
#include <iostream>
int main() {
Glib::RefPtr<Gio::Settings> s = Gio::Settings::create("com.ubuntu.user-interface");
int i = s->get_int("scale-factor");
std::cout << i << std::endl;
}
See also here.
I can't post a comment to Wintermute answer because of low reputation so I post it here.
Newbe, like me, could have problem including <giomm/settings.h> (not found): a solution is to append to gcc compile command `pkg-config --cflags --libs glibmm-2.4 giomm-2.4` (with backticks)
If your source file is program.cc, you can compile it with:
g++ program.cc -o program `pkg-config --cflags --libs glibmm-2.4 giomm-2.4`
From here