46 questions
0
votes
0
answers
52
views
How does LLVM IR map to RISCV Assembly Code
I'm new to LLVM Backend development and I'm discovering some of its working by modifying an existing backend, compiling the llvm-project and observing the changes brought to the binaries and their ...
1
vote
0
answers
268
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 {
%...
0
votes
1
answer
624
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
...
2
votes
0
answers
559
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 ...
1
vote
1
answer
919
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, ...
2
votes
0
answers
623
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 ...
1
vote
0
answers
286
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
-...
15
votes
1
answer
802
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(...
7
votes
1
answer
664
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 {
...
261
votes
2
answers
24k
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 ...
422
votes
1
answer
54k
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 ...
575
votes
6
answers
42k
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 ...
2
votes
3
answers
669
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;
}
...
10
votes
1
answer
564
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 ...
1
vote
1
answer
148
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 ...
35
votes
3
answers
20k
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 more ...
16
votes
4
answers
6k
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: ...
6
votes
4
answers
8k
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 ...
11
votes
3
answers
2k
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 ...
142
votes
1
answer
36k
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 ...
12
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 ...
21
votes
2
answers
4k
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 ...
13
votes
2
answers
4k
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 ...