News for hackers. Stuff that matters.
Re:GCC Rust: GCC Front-End for Rust by steveklabnik on Sunday January 10, 2021 @01:17PM (#25717611)
IMHO, "great deal" is overemphasizing it. There are some things, yes, but they are mostly smaller, more niche details that are even probably things we'd choose ourselves in many cases.

That being said, there's also cases where Rust does not have LLVM semantics, and that can cause bugs. Some famous examples being the loop optimization miscompilation, and &mut T currently not being marked noalias.

Re:GCC Rust: GCC Front-End for Rust by masklinn on Sunday January 10, 2021 @01:27PM (#25717758)
> Some famous examples being the loop optimization miscompilation, and &mut T currently not being marked noalias.

I don't think that's a case where Rust "doesn't have LLVM semantics" since the miscompilation was reproduced in standard C. Rather that rust is actively and ubiquitously leveraging otherwise rarely-exercised LLVM features, revealing a bunch of bugs (either leftovers or breakages) in them.

Re:GCC Rust: GCC Front-End for Rust by steveklabnik on Sunday January 10, 2021 @01:40PM (#25717901)
The latter, you're right, I did make a mistake here. I was thinking of it as "Rust doesn't just do whatever LLVM does, see, we have semantics and LLVM compiles them wrong, so this is an example of that" but I forgot that actually, we do specify "this follows what LLVM does" currently, and the miscompilation is just a plain bug. Extra embarrassing because IIRC I was the one who made the PR saying that.

The former though is an area of significant divergence between Rust and C++ semantics, and LLVM directly following C++ semantics.

Re:GCC Rust: GCC Front-End for Rust by masklinn on Sunday January 10, 2021 @01:48PM (#25718019)
> The former though is an area of significant divergence between Rust and C++ semantics, and LLVM blindly following C++ semantics.

Ah I must have missed that, I thought you were talking about only one thing (since IIRC the noalias miscompilation is due to loop unrolling?)

Re:GCC Rust: GCC Front-End for Rust by jdoerfert on Sunday January 10, 2021 @05:56PM (#25721175)
FWIW, `noalias`, aka. `__restrict__`, generally works fine but the inliner translates it into `noalias` metadata which is, still, ambiguous and also not handled properly in some cases. Loop unrolling is one of them but that will be fixed with https://reviews.llvm.org/D92887, a general problem is the ambiguity itself.

There is a major effort to revamp the `noalias`/`restrict` handling going on for a while now. It takes quite long because it is hard and complex and we want to get it right.

In case you are interested, here is the new design https://reviews.llvm.org/differential/changeset/?ref=2170825 here the overall code changes currently considered https://reviews.llvm.org/D69542 and here you can find information on our monthly LLVM Alias Analysis call https://docs.google.com/document/d/1ybwEKDVtIbhIhK50qYtwKsL5...

Re:GCC Rust: GCC Front-End for Rust by steveklabnik on Sunday January 10, 2021 @02:20PM (#25718510)
I don't know the root cause of the noalias miscompilation, the thing I was referring to is https://github.com/rust-lang/rust/issues/28728

TL;DR: C++ says that an infinite loop with no side effects is UB, Rust does not. Empty loops in Rust will disappear entirely when they should really loop forever.

(I edited my comment slightly because, re-reading, "blindly" sounds too negative.)

Re:GCC Rust: GCC Front-End for Rust by davidhyde on Sunday January 10, 2021 @04:45PM (#25720293)
Great point! If anyone is curious about how to write an “empty” loop that is not compiled away, I've seen this common implementation that doesn’t even need the standard library:

loop {

core::sync::atomic::compiler_fence( core::sync::atomic::Ordering::SeqCst);

}

Re:GCC Rust: GCC Front-End for Rust by jdoerfert on Sunday January 10, 2021 @06:00PM (#25721229)
Since https://reviews.llvm.org/D86841, ~ Aug 2020, Clang is able to reason about the forward progress guarantees of the input. For the LLVM-IR handling see https://reviews.llvm.org/D86233. While we are still adding more fine-grained support of different language standards and corner cases, e.g., constant loop condition in C vs. C++, you should be able to tell LLVM a loop is allowed to loop forever w/o side-effect. In fact, that is the new default. That said, LLVM still removes calls to functions without side effect unconditionally. We are working on that, i.a., https://reviews.llvm.org/D94106.
Re:GCC Rust: GCC Front-End for Rust by volta87 on Sunday January 10, 2021 @04:40PM (#25720237)
Notice that in C and C++ loops that might not terminate are UB, while in Rust they are just infinite loops.

So there is a significant difference in semantics between Rust and C/C++ here.

Re:GCC Rust: GCC Front-End for Rust by saagarjha on Sunday January 10, 2021 @05:24PM (#25720760)
C and C++ actually differ in semantics here; C allows infinite loops that are controlled by a constant expression.
Re:GCC Rust: GCC Front-End for Rust by jdoerfert on Sunday January 10, 2021 @06:06PM (#25721292)
Generally, yes. Though, as always, these things are hard to summarize in one sentence and depend on the standard version (pre/post C++11). This post might shed some light on the options we are considering for Clang right now: https://reviews.llvm.org/D94367#2489090
Re:GCC Rust: GCC Front-End for Rust by souprock on Monday January 11, 2021 @06:34AM (#25727578)
Just what is gained by failing to loop forever? It seems like a really low-value optimization. Nearly always, the optimization would violate the programmer's intent or it would change an ordinary bug into a confusing bug. An infinite loop is not very many bytes on any processor.

The standard may allow the generation of insane code, but a decent-quality compiler does not do so. The standard ought to be fixed.

Re:GCC Rust: GCC Front-End for Rust by gpderetta on Monday January 11, 2021 @06:44AM (#25727670)
The infinite loop being UB was added because it prevents some important code motion optimizations and makes it hard to reason about the memory model. You can find the details on the papers leading to the C++11 memory model.
Re:GCC Rust: GCC Front-End for Rust by volta87 on Monday January 11, 2021 @10:10AM (#25729866)
> The infinite loop being UB was added because it prevents some important code motion optimizations

Which ones?

If this optimizations are so important, how come Rust was designed in such a way to make them impossible? Also, how does this fit, e.g., the benchmark game results which show that Rust is faster than C for all benchmarks considered there ?

Re:GCC Rust: GCC Front-End for Rust by gpderetta on Tuesday January 12, 2021 @01:10PM (#25750843)
Store sinking for example, which is unsafe unless the loop is guaranteed to terminate.

C does not have this guarantee (at least not in all cases). Also rust is compiled with the llvm backend, so my understanding is that in practice rust assumes that loops terminate. See:

https://github.com/rust-lang/rust/issues/28728

There are llvm directives that can be added to prevent the optimization, but they are rejected by the rust maintainers exactly because they would cause performance regressions.

Re:GCC Rust: GCC Front-End for Rust by souprock on Monday January 11, 2021 @06:56AM (#25727763)
Uh, so what?

Letting the compiler assume that a "switch" without a default will never go there is a great optimization too. Why not put that in the standard?

Actually, this is worse. Letting the loop be UB is like letting a true "if" be UB. Just never mind the code actually written; surely it doesn't mean what it says.

Re:GCC Rust: GCC Front-End for Rust by gpderetta on Tuesday January 12, 2021 @01:12PM (#25750881)
> Letting the compiler assume that a "switch" without a default will never go there is a great optimization too. Why not put that in the standard?

that's actually the case already. A missing default is UB if the switch condition does not match any of the cases. And C compilers already optimize accordingly.

Re:GCC Rust: GCC Front-End for Rust by Dylan16807 on Sunday January 10, 2021 @10:19PM (#25723958)
Loops that do nothing and don't terminate.

If your loop infinitely writes the number 70 to an atomic int, it's fine.

Re:GCC Rust: GCC Front-End for Rust by volta87 on Monday January 11, 2021 @03:01AM (#25726039)
If by "nothing" you mean "nothing but looping", then yes, you are right. Any loop that "just loops" is doing something by definition: looping.
Re:GCC Rust: GCC Front-End for Rust by Dylan16807 on Monday January 11, 2021 @12:07PM (#25731913)
It depends on your perspective. Normally when I talk about what a loop "does", I am talking purely about the contents of the loop.