Questions tagged [llvm-codegen]

Questions about how LLVM generates machine code from LLVM-IR or high-level languages. If you are asking about a specific CPU architecture, please also add that corresponding tag (e.g. `x86`).

Filter by
Sorted by
Tagged with
0 votes
0 answers
36 views

how to integrate new custom backend in the LLVM

Is there any good understable resources for a beginner, which will demonstrate clearly (exact path) from scratch how to integrate new custom backend from LLVM-IR? I have read Creating an LLVM Backend ...
pralay das's user avatar
1 vote
0 answers
137 views

Selection DAG from LLVM IR?

I have fetched LLVM-IR via clang -S -emit-llvm demo.c where demo.c is as follows int demo(int a, int b){ int c = a+b; return c; } The IR looks like define dso_local i32 @demo(i32 %0, i32 %1) #0 { %...
pralay das's user avatar
0 votes
1 answer
280 views

LLVM C-API Lifecycles of core objects

I've started playing with LLVM, making a pet language. I'm using the C-API. I have a parser and basic AST, but I am at a bit of a road block with LLVM. The following is a minified version of my code ...
Zachary Vander Klippe's user avatar
0 votes
1 answer
427 views

x86_64 incorrect calling convention when calling function

I'm relatively new to LLVM, and I'm attempting to generate LLVM IR that calls a C function (growDictionary). This is on x86_64 Linux, using llvm 12: $ llc-12 --version Ubuntu LLVM version 12.0.1 ...
Tudor Bosman's user avatar
4 votes
1 answer
300 views

Why does clang emit a 32-bit float ps instruction for the absolute value of a 64-bit double?

Why is clang turning fabs(double) into vandps instead of vandpd (like GCC does)? Example from Compiler Explorer: #include <math.h> double float_abs(double x) { return fabs(x); } clang 12....
soc's user avatar
  • 28.1k
1 vote
0 answers
348 views

Emitting Object Code In Memory With LLVM's C API

I'm using the LLVM-C API for a compiler project and need to emit object code from IR to an in memory buffer. I'm aware the JIT can do this, but the resulting code will be executed many times with ...
muke's user avatar
  • 324
3 votes
0 answers
508 views

disable use of cmov in clang

From the command line it is possible to tell clang to disable use of some features, for example -mno-mmx or -mno-popcnt. However, while cmov is a feature tracked by llvm, there is no -mno-cmov for ...
PluckyBird's user avatar
1 vote
1 answer
614 views

LLVM-C creating object file results in: "TargetMachine can't emit a file of this type"

Trying to generate a very simple object file with LLVM-C. Unfortunately I'm still stuck on "TargetMachine can't emit a file of this type" tried reordering code and various things for CPU (x64-64, ...
Mirks's user avatar
  • 116
1 vote
2 answers
427 views

Statically scheduling OOO processors

The LLVM MISched instruction scheduler uses declarative TableGen descriptions of the processor functional units, pipelines and latencies. Imagine trying to determine the equivalent of the coding ...
Olsonist's user avatar
  • 2,140
1 vote
0 answers
485 views

LLVM creating executable code from C/C++ builder

I have get an example llvm code from here. This code has some problems that I fixed them too. At this point, all it does is to dump the translated IR code. What I am after is to create an executable ...
ar2015's user avatar
  • 5,718
2 votes
0 answers
34 views

What are code models and how do they affect code generation? [duplicate]

In rustc, there are 4 code models. You can see the list by typing rustc --print code-models: Available code models: small kernel medium large What do they mean?
William Taylor's user avatar
1 vote
0 answers
211 views

Error invoking LLVM CodeGen pass using clang: Trying to construct TargetPassConfig without a target machine

I have been trying to invoke my out-of-tree LLVM Function pass using clang (opt is not an option. Works fine with opt btw): clang -std=c99 -m64 -c -o file.o -DSPEC -DNDEBUG -Ispec_qsort -...
sk10's user avatar
  • 73
10 votes
1 answer
5k views

Is signed integer overflow in safe Rust in release mode considered as undefined behavior?

Rust treats signed integer overflow differently in debug and release mode. When it happens, Rust panics in debug mode while silently performs two's complement wrapping in release mode. As far as I ...
Zhiyao's user avatar
  • 4,222
15 votes
1 answer
683 views

Why does LLVM appear to ignore Rust's assume intrinsic?

LLVM appears to ignore core::intrinsics::assume(..) calls. They do end up in the bytecode, but don't change the resulting machine code. For example take the following (nonsensical) code: pub fn one(...
llogiq's user avatar
  • 14k
11 votes
3 answers
426 views

Why does LLVM allocate a redundant variable?

Here's a simple C file with an enum definition and a main function: enum days {MON, TUE, WED, THU}; int main() { enum days d; d = WED; return 0; } It transpiles to the following LLVM IR:...
macleginn's user avatar
  • 331
7 votes
1 answer
551 views

Why isn't there a branch prediction failure penalty in this Rust code?

I've written this very simple Rust function: fn iterate(nums: &Box<[i32]>) -> i32 { let mut total = 0; let len = nums.len(); for i in 0..len { if nums[i] > 0 { ...
Dathan's user avatar
  • 7,306
1 vote
0 answers
457 views

Get Size of LLVM Type / "Dereference" Type

This question is quite similar to this one, but I am not realy sure how to get the size in the following situation: I have a Pointer type, e.g. i32*. Now I would like to get the size of the 'pointed-...
mame98's user avatar
  • 1,311
259 votes
2 answers
23k views

Why is there a large performance impact when looping over an array with 240 or more elements?

When running a sum loop over an array in Rust, I noticed a huge performance drop when CAPACITY >= 240. CAPACITY = 239 is about 80 times faster. Is there special compilation optimization Rust is ...
Guy Korland's user avatar
  • 9,138
185 votes
4 answers
32k views

How does Rust's 128-bit integer `i128` work on a 64-bit system?

Rust has 128-bit integers, these are denoted with the data type i128 (and u128 for unsigned ints): let a: i128 = 170141183460469231731687303715884105727; How does Rust make these i128 values work on ...
ruohola's user avatar
  • 22.3k
407 votes
1 answer
52k views

Why does the Rust compiler not optimize code assuming that two mutable references cannot alias?

As far as I know, reference/pointer aliasing can hinder the compiler's ability to generate optimized code, since they must ensure the generated binary behaves correctly in the case where the two ...
Zhiyao's user avatar
  • 4,222
12 votes
1 answer
410 views

Can compilers (specifically rustc) really simplify triangle-summation to avoid a loop? How?

On page 322 of Programming Rust by Blandy and Orendorff is this claim: ...Rust...recognizes that there's a simpler way to sum the numbers from one to n: the sum is always equal to n * (n+1) / 2. ...
Kyle Strand's user avatar
571 votes
6 answers
40k views

Does the C++ standard allow for an uninitialized bool to crash a program?

I know that an "undefined behaviour" in C++ can pretty much allow the compiler to do anything it wants. However, I had a crash that surprised me, as I assumed that the code was safe enough. In this ...
Remz's user avatar
  • 3,378
12 votes
1 answer
3k views

Why does clang produce inefficient asm with -O0 (for this simple floating point sum)?

I am disassembling this code on llvm clang Apple LLVM version 8.0.0 (clang-800.0.42.1): int main() { float a=0.151234; float b=0.2; float c=a+b; printf("%f", c); } I compiled with no ...
Stefano Borini's user avatar
2 votes
3 answers
615 views

What optimization techniques are applied to Rust code that sums up a simple arithmetic sequence?

The code is naive: use std::time; fn main() { const NUM_LOOP: u64 = std::u64::MAX; let mut sum = 0u64; let now = time::Instant::now(); for i in 0..NUM_LOOP { sum += i; } ...
Sanhu Li's user avatar
  • 427
-6 votes
2 answers
268 views

Why is swapping elements of a []float64 in Go faster than swapping elements of a Vec<f64> in Rust?

I have two (equivalent?) programs, one in Go the other in Rust. The average execution time is: Go ~169ms Rust ~201ms Go package main import ( "fmt" "time" ) func main() { work := []...
Kyle's user avatar
  • 11
10 votes
1 answer
532 views

Why can the Rust compiler not optimize away the Err arm of Box::downcast?

I have a Box<dyn Any> and I know the underlying type so I want to optimize away the test in Box::downcast() (source). First I tried with std::hint::unreachable_unchecked(): pub unsafe fn ...
Tim Diekmann's user avatar
  • 7,953
2 votes
1 answer
1k views

What LLVM passes are performed on emitted LLVM IR?

If I compile with cargo rustc -- --emit=llvm-ir the compiler will emit LLVM IR. Here are the LLVM passes that Rust uses. What LLVM passes, if any, have been performed on the emitted IR? Is there any ...
HiDefender's user avatar
  • 2,158
1 vote
1 answer
141 views

How to map multiple same type loops under a function to the generated basic block in LLVM IR?

If the loops are of the different type then I can easily identify them with the name but if there are multiple same type loops (say 5 while loops), how can I identify what basic block in the LLVM IR ...
Sanjit Kumar Mishra's user avatar
15 votes
2 answers
6k views

LLVM opt mem2reg has no effect

I am currently playing around with LLVM and am trying to write a few optimizers to familiarize myself with opt and clang. I wrote a test.c file that is as follow: int foo(int aa, int bb, int cc){ ...
Brenda So's user avatar
  • 191
34 votes
3 answers
13k views

What do the optimization levels `-Os` and `-Oz` do in rustc?

Executing rustc -C help shows (among other things): -C opt-level=val -- optimize with possible levels 0-3, s, or z The levels 0 to 3 are fairly intuitive, I think: the higher the level, the ...
Lukas Kalbertodt's user avatar
15 votes
1 answer
2k views

Why doesn't the Rust optimizer remove those useless instructions (tested on Godbolt Compiler Explorer)?

I wanted to take a look at the assembly output for a tiny Rust function: pub fn double(n: u8) -> u8 { n + n } I used the Godbolt Compiler Explorer to generate and view the assembly (with the -...
Lukas Kalbertodt's user avatar
16 votes
4 answers
4k views

Why does this code generate much more assembly than equivalent C++/Clang? [closed]

I wrote a simple C++ function in order to check compiler optimization: bool f1(bool a, bool b) { return !a || (a && b); } After that I checked the equivalent in Rust: fn f1(a: bool, b: ...
Mariusz Jaskółka's user avatar
14 votes
2 answers
5k views

Can I force Rust to not optimize a single function?

I have a function where Rust's/LLVM's optimization fails and leads to a panic (in the release version), while the unoptimized code (debug version) just works fine. If I compare the generated assembly ...
Matthias's user avatar
  • 8,046
4 votes
4 answers
6k views

How to prevent function calls from being optimized away?

How can I ensure that a function with no side effects gets executed and doesn't get optimized away in stable Rust? Is there an attribute combination I could use, or must I call another function with ...
Doe's user avatar
  • 625
26 votes
1 answer
723 views

Is there a way to get Rust to treat pointers as non-aliasing, so it can mark them as `noalias` for LLVM's optimizer?

The following example of pointer aliasing: pub unsafe fn f(a: *mut i32, b: *mut i32, x: *const i32) { *a = *x; *b = *x; } compiles into the following assembly (with -C opt-level=s): example::f: ...
Cornstalks's user avatar
  • 37.4k
9 votes
3 answers
1k views

Which integer operations have higher performance alternate methods in Rust?

When writing integer functions in Rust which will run millions of times (think pixel processing), it's useful to use operations with the highest performance - similar to C/C++. While the reference ...
ideasman42's user avatar
  • 43.7k
29 votes
1 answer
4k views

Can Rust optimise away the bit-wise copy during move of an object someday?

Consider the snippet struct Foo { dummy: [u8; 65536], } fn bar(foo: Foo) { println!("{:p}", &foo) } fn main() { let o = Foo { dummy: [42u8; 65536] }; println!("{:p}", &o); ...
WiSaGaN's user avatar
  • 47.3k
132 votes
1 answer
31k views

When should inline be used in Rust?

Rust has an "inline" attribute that can be used in one of those three flavors: #[inline] #[inline(always)] #[inline(never)] When should they be used? In the Rust reference, we see an inline ...
WiSaGaN's user avatar
  • 47.3k
29 votes
1 answer
2k views

Why does my code run slower when I remove bounds checks?

I'm writing a linear algebra library in Rust. I have a function to get a reference to a matrix cell at a given row and column. This function starts with a pair of assertions that the row and column ...
Bradley Hardy's user avatar
13 votes
1 answer
1k views

Why empty functions aren't removed as dead code in LLVM IR?

Starting with this simple C program: void nothing(void) {} int main() { int i; for (i = 0; i < 10; ++i) { nothing(); } return 0; } My passes output as follows: Note: IR statements ...
Ahmed Ghoneim's user avatar
5 votes
1 answer
567 views

Setting a non-default rounding mode with Rust inline asm isn't respected by the LLVM optimizer?

I am working on a Rust crate which changes the rounding mode (+inf, -inf, nearest, or truncate). The functions that change the rounding mode are written using inline assembly: fn upward() { let ...
Houss_gc's user avatar
  • 739
21 votes
2 answers
3k views

How do I compile with "ffast-math"?

I'm trying to benchmark some Rust code, but I can't figure out how to set the "ffast-math" option. % rustc -C opt-level=3 -C llvm-args='-enable-unsafe-fp-math' unrolled.rs rustc: Unknown command line ...
yong's user avatar
  • 3,583
4 votes
1 answer
925 views

LLVM use of carry and zero flags

I'm starting to read LLVM docs and IR documentation. In common architectures, an asm cmp instruction "result" value is -at least- 3 bits long, let's say the first bit is the SIGN flag, the second bit ...
Lucio M. Tato's user avatar
13 votes
2 answers
3k views

C ABI with LLVM

I've got a compiler written with LLVM and I'm looking to up my ABI compliance. For example, I've found it hard to actually find specification documents for C ABI on Windows x86 or Linux. And the ones ...
Puppy's user avatar
  • 145k
27 votes
1 answer
15k views

LLVM's integer types

The LLVM language specifies integer types as iN, where N is the bit-width of the integer, and ranges from 1 to 2^23-1 (According to: http://llvm.org/docs/LangRef.html#integer-type) I have 2 questions:...
Ali J's user avatar
  • 342