LLVM: add linker directives to LLVM IR - llvm

How can I add linker directives found in a COFF (MSVC object file) with LLVM using LLVM IR? Or is there another tool that can add these directives?

LLVM 5.0
!llvm.linker.options = !{ !0 }
!0 = {!"/DEFAULTLIB:flang.lib", !"/DEFAULTLIB:flangrti.lib", !"/DEFAULTLIB:ompstub.lib"}
LLVM 4.0
!llvm.module.flags = {!0}
!0 = !{i32 6, !"Linker Options", !1}
!1 = !{!2, !3}
!2 = !{!"/DEFAULTLIB:libcmtd.lib"}
!3 = !{!"/DEFAULTLIB:oldnames.lib"}

Related

Clang adds noinline attribute to all functions when emitting LLVM IR

Consider the following simple function:
int foo() { return 42; }
Compiling this to LLVM via clang -emit-llvm -S foo.cpp produces the following module:
; ModuleID = 'foo.cpp'
source_filename = "foo.cpp"
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx10.13.0"
; Function Attrs: noinline nounwind ssp uwtable
define i32 #_Z3foov() #0 {
ret i32 42
}
attributes #0 = { noinline nounwind ssp uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="penryn" "target-features"="+cx16,+fxsr,+mmx,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
!llvm.module.flags = !{!0}
!llvm.ident = !{!1}
!0 = !{i32 1, !"PIC Level", i32 2}
!1 = !{!"Apple LLVM version 9.0.0 (clang-900.0.37)"}
Why is the foo function declared as noinline? The flag is not added if an optimization level (other than -O0) is specified, but I would like to avoid that.
Is there another way / flag?
With -O0, you can't enable inlining globally, judging from Clang's source code
(Frontend\CompilerInvocation.cpp):
// At O0 we want to fully disable inlining outside of cases marked with
// 'alwaysinline' that are required for correctness.
Opts.setInlining((Opts.OptimizationLevel == 0)
? CodeGenOptions::OnlyAlwaysInlining
: CodeGenOptions::NormalInlining);
Depending on your requirements, you may:
Use -O1, which is the closest to -O0.
Use -O1 in conjuction with disabling of optimization flags that it enables. See the following answer for optimization flags enabled with -O1: Clang optimization levels
Apply always_inline attribute selectively on functions that should be inlined.
For example: int __attribute__((always_inline)) foo() { return 42; }

what llvm store instruction pattern do i need?

im trying to make an llvm backend and i dont know what i need to fix this error
LLVM ERROR: Cannot select: t5: ch = store<ST4[%retval]> t0, Constant:i32<0>, FrameIndex:i64<0>, undef:i64
this is the ir im trying to process
define i32 #main() #0 {
%retval = alloca i32, align 4
store i32 0, i32* %retval, align 4
ret i32 0
}
but i don't know what dag pattern i need to be able to match it.
a tablegen file that contains some of the instructions my arch supports is here https://github.com/jfmherokiller/customllvm/blob/master/llvm/lib/Target/ZCPU/zcpuInstr.td
i just figured out the issue i was looking at the issue wrong
store<ST4[%retval]> t0, Constant:i32<0>, FrameIndex:i64<0>, undef:i64
can be expessed in function form as store(Constant:i32<0>,FrameIndex:i64<0>) or store constant i32 0 in
stack frame index 0.
The information i wasnt getting was that FrameIndex:i64<0> directly related to this line in TargetSelectionDAG.td def frameindex :SDNode<"ISD::FrameIndex",SDTPtrLeaf, [],"FrameIndexSDNode">;
so FrameIndex = frameindex

Using Clang and LLVM

I am compiling this:
int main(){
}
With clang, using this command line:
clang++.exe -S -o %OUTFILE%.clang -emit-llvm %INFILE% -I. -I%INCLUDE_PATH% -std=c++14 -ftemplate-depth=1000
Which gives me llvm byte-code.
Then I use llc like so, to convert the byte-code into c code:
llc "%IN_FILE%.clang" -march=c -o foo.c
And get this error:
error: unterminated attribute group
attributes #0 = { norecurse nounwind uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" }
What I am doing wrong?
This is what clang++ is giving me:
; ModuleID = 'C:\Users\Owner\Documents\C++\SVN\prototypeInd\util\StaticBigInt.cpp'
target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-windows-msvc18.0.0"
; Function Attrs: norecurse nounwind readnone uwtable
define i32 #main() #0 {
entry:
ret i32 0
}
attributes #0 = { norecurse nounwind readnone uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" }
!llvm.module.flags = !{!0}
!llvm.ident = !{!1}
!0 = !{i32 1, !"PIC Level", i32 2}
!1 = !{!"clang version 3.8.0 (branches/release_38)"}
Note:
I am using clang version 3.8 and llc version 3.4
When you run a command such as:
clang -S -emit-llvm ...
Then the compiler emits not an IR in a bitcode form, but human readable representation.
It makes sense if all tools you use have the same versions, or if you just want to inspect the output manually.
However, the human readable IR may be incompatible with old tools.
In this case I can recommend to use bitcode directly (note that there is no -S parameter anymore):
clang -emit-llvm
C backend in LLVM was removed several years ago. It seems that you're trying to feed LLVM IR from the recent LLVM version to llc from old LLVM. This is certainly not supported - the IR is not compatible between the versions.

why it is asking for a token?

I've written a very simple llvm IR code. However when I try to run it through llc, I get the following error:
llc: add_test.ll:10:16: error: expected value token
%r = load i32, i32* %retval
^
Here is the code:
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
; Function Attrs: nounwind uwtable
define i32 #main() #0 {
entry:
%retval = alloca i32, align 4
store i32 0, i32* %retval
%r = load i32, i32* %retval
ret i32 0
}
attributes #0 = { nounwind uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
!llvm.ident = !{!0}
!0 = metadata !{metadata !"clang version 3.5.0 "}
The command that i'm running is llc add-test.ll
Does anybody know what could be the problem?
The syntax for load (among others) was changed in LLVM version 3.7. The syntax you're using is the new one. Since you're using version 3.5, you need to use the old syntax, which is:
%r = load i32* %retval
In other words you only specify the type of the parameter, not of the result.
I assume the problem occurred because you're using the current version of the documentation while using an old version of LLVM. The documentation for LLVM 3.5.0 can be found here.

how to use llvm intrinsics #llvm.read_register?

I noticed that llvm.read_register() could read the value of stack pointer, as well as llvm.write_register() could set the value of stack pointer. I add main function to the stackpointer.ll which could be found in the llvm src:
;stackpointer.ll
define i32 #get_stack() nounwind {
 %sp = call i32 #llvm.read_register.i32(metadata !0)
 ret i32 %sp
}
declare i32 #llvm.read_register.i32(metadata) nounwind
!0 = metadata !{metadata !"sp\00"}
define i32 #main() {
 %1 = call i32 #get_stack()
 ret i32 %1
}
I tested on an armv7 board running ubuntu 11.04:
lli stackpointer.ll
then, I get a stack dump:
ARMCodeEmitter::emitPseudoInstruction
UNREACHABLE executed at ARMCodeEmitter.cpp:847!
Stack dump:
0.  Program arguments: lli stackpointer.ll
1.  Running pass 'ARM Machine Code Emitter' on function '#main'
Aborted
I also tried llc:
llc stackpointer.ll -o stackpointer.s
The error messege:
Can't get register for value!
UNREACHABLE executed at ARMCodeEmitter.cpp:1183!
Stack dump:
0.  Program arguments: llc stackpointer.ll -o stackpointer.s
1.  Running pass 'Function Pass Manager' on moulude 'stackpointer.ll'
2.  Running pass 'ARM Instruction Selection' on function '#get_stack'
Aborted
I also tried on x86-64 platform, it didn't work. What is the correct way to use these intrinsics?
My lli didn't like your metadata definition.
I cnagned your
!0 = metadata !{metadata !"sp\00"}
to
!0 = !{!"sp\00"}
And it worked. (Well, since I'm on x86-64, I have also changed everywhere i32 to i64 and sp to rsp).
Plus there were bad whitespace symbols in your formatting, but I think it might be due to StackOverflow/html or something).