News for hackers. Stuff that matters.
Re:GCC Rust: GCC Front-End for Rust by Blikkentrekker on Sunday January 10, 2021 @12:47PM (#25717218)
Is a great deal of actual Rust behavior not fairly intimately tied to LLVM? As far as I know it leaks various LLVM details and much of the documentation about various functions documents them as being little more than a thin wrapper to various LLVM internals.
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.
Re:GCC Rust: GCC Front-End for Rust by dagmx on Sunday January 10, 2021 @01:05PM (#25717442)
Potentially but there are alternative backends like cranelift so it's unlikely anything major is tied to just LLVM anymore
Re:GCC Rust: GCC Front-End for Rust by chrisseaton on Sunday January 10, 2021 @12:48PM (#25717236)
I'd find that very surprising and interesting - I can't imagine which semantics could leak from LLVM into a language specification. Can you give some examples?
Re:GCC Rust: GCC Front-End for Rust by Blikkentrekker on Sunday January 10, 2021 @01:00PM (#25717377)
For instance `std::ptr::offset` which is apparently a trivial wrapper around some LLVM internal: https://llvm.org/docs/LangRef.html#getelementptr-instruction

I'm not sure as to what capacity GCC has a similar instruction but I heard that LLVM's using of signed integers here is apparently nonstandard and what lead to Rust's decisions for vectors to be limited to a certain size: https://doc.rust-lang.org/nomicon/vec-alloc.html

Re:GCC Rust: GCC Front-End for Rust by stouset on Sunday January 10, 2021 @01:06PM (#25717447)
I don’t know why you’re italicizing LLVM, gcc, and Rust, but I (and perhaps others) find it relatively jarring as my brain automatically parses it as emphasis even though it clearly isn’t intended to be.

I only bring it up because it pretty drastically harms readability (for me at least, and to a degree I frankly find surprising). Just thought you may want to know.

Re:GCC Rust: GCC Front-End for Rust by balnaphone on Sunday January 10, 2021 @01:39PM (#25717896)
This is common nomenclature to enhance readability in fields that have a large number of neologisms and proper nouns, such as some technical domains, medicine, investment banking, and so on. It's particularly useful when names are in the same language as the base language (e.g. in English "Peter" is easily recognized as a name, but "better fish" means something in the language, but can also be used as a proper noun, so using italics for proper nouns serves to disambiguate the intended meaning).
Re:GCC Rust: GCC Front-End for Rust by tssva on Sunday January 10, 2021 @01:56PM (#25718133)
Maybe he is used to writing in MLA style which calls for the titles of software to be italicized just as books, movies, etc are.
Re:GCC Rust: GCC Front-End for Rust by Blikkentrekker on Sunday January 10, 2021 @02:11PM (#25718374)
This is indeed a point of contention among different styles of whether software titles should be italicized. Though the position that they should uniformly be is the minority one, it is certainly used by some outlets with considerable weight, and I believe it to be the consistent one if titles of books and films are too.

Some style guides make the even more inconsistent distinction that only titles of video games be italicized, but titles of “application software” not be.

I suppose that on H.N., where such software is frequently mentioned, it does stand out more.

Re:GCC Rust: GCC Front-End for Rust by unanswered on Sunday January 10, 2021 @02:55PM (#25718933)
The video games example seems consistent to me; we regularly italicize the names of plays (for example) but not the names of tools and businesses.
Re:GCC Rust: GCC Front-End for Rust by Blikkentrekker on Sunday January 10, 2021 @03:26PM (#25719284)
Because tools and businesses are not titles that qualify for intellectual property, a piece of software is.

I do not italicize Google the company either, but I do that of Google the search engine.

The titles of scientific research papers are also typically italicized.

Re:GCC Rust: GCC Front-End for Rust by samb1729 on Sunday January 10, 2021 @01:36PM (#25717862)
Looking at their commenting history, they have a habit of italicising really often, so it's not specific to this topic. It does indeed make a lot of their comments annoying to read without obviously adding any value.
Re:GCC Rust: GCC Front-End for Rust by codetrotter on Sunday January 10, 2021 @02:15PM (#25718427)
> It does indeed make a lot of their comments annoying to read without obviously adding any value

I’m going to assume that you mean that the italics don’t add value, and not their comments as a whole. I think your comment could be read either way. Anyways, even so others have since pointed out reasons why the might be used to doing so.

Re:GCC Rust: GCC Front-End for Rust by chrisseaton on Sunday January 10, 2021 @01:14PM (#25717564)
getelementptr is just some pointer arithmetic - all backends can do that.

And using signed integers is unusual yes, but again all backends (obviously) also support signed integers.

Re:GCC Rust: GCC Front-End for Rust by steveklabnik on Sunday January 10, 2021 @01:41PM (#25717923)
IMHO, "Just some pointer arithmetic" is selling it a bit short. There's a lot of stuff there around provenance that matters, that is, there's a reason this is an intrinsic and not just a subtraction after casting both to an int.

(I agree on the signed integer thing though.)

Re:GCC Rust: GCC Front-End for Rust by CUViper on Sunday January 10, 2021 @01:08PM (#25717475)
Glibc malloc also limits the maximum according to ptrdiff_t, which is effectively the same as Rust isize.
Re:GCC Rust: GCC Front-End for Rust by monocasa on Sunday January 10, 2021 @12:52PM (#25717273)
I don't think much of the stable functionality exposes llvm internals. Part of stabilizing is removing the llvm leaks into the language.