Clang dump ast but exclude imported files? - clang

Use clang to dump a objective-c file, but it will print all the imported files, so many useless info, is it possible just log out the class or method defined in this class?
The command is like this:
clang -x objective-c -Xclang -ast-dump -fsyntax-only -fobjc-arc main.m

Related

How to compile resource files into a custom framework or static library or Mach-O file?

How to compile resource files into a custom framework or static library (meaning into a Mach-O file)? Thanks for any tips.
Assuming by "resource files" you mean arbitrary binary files, you can include them into any binary by creating an assembly file (e.g. resources.S) like so:
.section __RESOURCE,__file1
_symbol_file1:
.incbin "file1.zip"
.no_dead_strip _symbol_file1
.section __WHATEVER,__file2
_symbol_file2:
.incbin "file2.bin"
.no_dead_strip _symbol_file2
// etc, you get the pattern
If you don't have a project in which you can include this file already, then you can turn it into an iOS arm64 framework by itself like so:
mkdir MyResources.framework
xcrun -sdk iphoneos clang -arch arm64 -shared -o MyResources.framework/MyResources resources.S

clang target option for riscv64 doesn't work

I'm trying to use clang to make IR for riscv64.
When I use llc --version, it shows many targets including riscv64.
But when I use the following command:
clang -target riscv64 hello.c
clang -target riscv64-unknown-linux hello.c
It shows clang-4.0: error: unknown target triple 'riscv64-unknown-linux', please use -triple or -arch
I'm not using ucb-bar's llvm for riscv. I'm using riscv from upstream of llvm.org.
Did I do something wrong or do I have to do something before building llvm?
Try elf instead of linux.
clang -target riscv32-unknown-elf
clang -target riscv64-unknown-elf
Edit: I created this repo for everyone interested in using RISC-V with LLVM.

Convert LLVM IR to higher optimization level

I have compiled the C code to LLVM IR code with -O0 optimization.
How can I convert this -O0 LLVM IR code to the -O3 LLVM IR code "without the C code"?
I have tried below:
clang -O3 -S -emit-llvm O0.ll -o O3.ll
and
opt -O3 -S O0.ll -o O3.ll
but the output is still -O0 level.
Thank you.
I'm not sure when the change happened (I think it's LLVM 3.9.0 and on), but when you compile to bitcode functions get annotated with the optnone attribute and further optimizations are not performed.
Have a look for a relevant SO discussion here.
What is suggested is to do this:
clang -emit-llvm -O1 -mllvm -disable-llvm-optzns -disable-llvm-passes foo.c -o foo.bc
For LLVM 3.8.0 and earlier (I think) what you have already been doing is enough.
So, once you obtain that bitcode (without the optnone) argument, you can use opt as you've already been doing.

How to create executable and IR at the same time

I would like to create both the executable and LLVM IR using clang at the same time. Is there a way to do that?
I'm currently using
clang -flto -Wl,-plugin-opt=also-emit-llvm -o foo foo.c
and get the error
clang: error: unknown argument: '-plugin-opt=also-emit-llvm'
with a -v invocation, I see that
/usr/bin/ld: unrecognized option '-plugin'
You can do both quite easily. Take a look at the tool:
https://github.com/SRI-CSL/whole-program-llvm

How to make clang stop before a specified LLVM pass and dump the LLVM IR

How do I run clang and have it stop just before a pass, say loop-vectorize, and dump the IR to an .ll file that can be later fed to opt?
opt has a -stop-after= option, but Clang seems to be missing the equivalent option. Here is a failed attempt with Clang 3.7.0rc2:
$ ../build/bin/clang -O2 -mllvm -stop-after=loop-vectorize a.cpp
clang (LLVM option parsing): Unknown command line argument '-stop-after=loop-vectorize'. Try: 'clang (LLVM option parsing) -help'
clang (LLVM option parsing): Did you mean '-print-after=loop-vectorize'?
I've also tried running clang -O0 -emit-llvm -S and then running opt -O2, but the results were different than running clang -O2 directly.
I'm not aware of any way to stop after a specific pass when compiling with Clang, but instead I can offer a hopefully helpful alternative.
First, to address opt and Clang producing different IR files, it may be helpful to compare the pass lists for clang -O2 and opt -O2 manually. This can be done for both by passing -debug-pass=Arguments. When running Clang you will need -mllvm to pass the argument along.
Having done this myself it appears that a different set of passes are being run for each of them but I would suggest confirming for yourself.
To address your initial question, you might simply copy the list of passes run during -O2 only up through loop-vectorize and simply run opt manually passing it the reduced list.

Resources