Live data from Hacker News

C++ Insights – See your source code with the eyes of a compiler

github.com

31–40 of 70 posts

Re: C++ Insights – See your source code with the eyes of a compiler

#31

Earlier quoted context omitted.

Thanks to another contributor there is a vim extensions: https://github.com/Freed-Wu/cppinsights.vim

Now do Emacs. 2 minutes od searching didn't hit anything :)

Sorry, I don't know about an Emacs plugin. All the plugins/extensions I'm aware of are listed in the Readme.md: https://github.com/andreasfertig/cppinsights/#c-insights--vi...

I'm happy to add an entry for Emacs once somebody develops a plugin for that editor.

Re: C++ Insights – See your source code with the eyes of a compiler

#32

Honestly, from the title, I expected something that gave actual 'insights' into the compiler, rather than the code. For example, indicating applied and potential (missed) optimizations. But seeing what it was, I was definitely not unpleasantly surprised :) Being able to cut through abstraction is very nice and quite important when you want to understand WHAT you are writing from the machine's perspective (or even in…

> I love using Haskell, but have no idea what kind of machine code GHC spits out at the end

There is GHC's core, an indermediate representation of your code (mostly "just" desugared Haskell), which is comparable to Andreas' C++ Insights. Which you can get by passing `-ddump-simpl` with additional flags to disable displaying everything.

A list of other internal GHC data to dump is https://downloads.haskell.org/~ghc/7.8.4/docs/html/users_gui...

But getting an idea what assembly is finally generated is still hard (at least for mé). Or, to rephrase, to know if "everything" is finally unpacked and strict or not, and if not: why not.

Btw. IMHO still the best book to read (of course not up-to-date, but to get the basic concepts of GHC) is https://www.microsoft.com/en-us/research/publication/the-imp... https://www.microsoft.com/en-us/research/uploads/prod/1987/0...

Re: C++ Insights – See your source code with the eyes of a compiler

#33
post #8

Honestly, from the title, I expected something that gave actual 'insights' into the compiler, rather than the code. For example, indicating applied and potential (missed) optimizations. But seeing what it was, I was definitely not unpleasantly surprised :) Being able to cut through abstraction is very nice and quite important when you want to understand WHAT you are writing from the machine's perspective (or even in…

compiler explorer/clang can do this. https://clang.llvm.org/docs/UsersManual.html#options-to-emit... Gcc also has similar options

Ooh nice, I completely forgot that that existed.

When you say the compiler explorer can do it, do you mean that it will 'inline' the output in a similar way to the assembly i.e. you can view the results 'within' the source code?

Re: C++ Insights – See your source code with the eyes of a compiler

#34

Earlier quoted context omitted.

Don't debug -O3, or -O2 for that matter. Debug a separate debug build without optimizations, or don't heavily optimize your release build if you want to debug that (you know that obviously). Even with optimizations disabled, debugging C++ can be quite painful. Only add a little template stuff to the source code, and a few "zero-cost" methods like indexing operators or cast operators, and it quickly becomes painful to…

You need to be able debug optimized builds. That's a requirement of the real world if you're a software engineer. Just learn how to use gdb, it's quite capable.

The complaint above isn't that gdb is hard, it's that the binary with debug info doesn't correlate very well with the source code after optimisations.

Bugs tend to disappear on me when I change the optimiser flags or run the thing under gdb though. I've also had a program valgrind-clean that segfaults when run without valgrind. It's a confusing world out there.

Re: C++ Insights – See your source code with the eyes of a compiler

#35
post #27

Earlier quoted context omitted.

Can Just My Code help if it is precisely my code that I want to skip over? I believe it does not -- IIUC it works based on modules i.e. the DLL that contains the machine code. IOW it doesn't even help if I move the "zero-cost" stuff to a separate source file -- templates and other stuff that have to be instanciated or inlined won't be separable from the code that I want to debug based on the module they're running in…

Well, if we are going down tiny details about what to skip, there are lots of C preprocessor stuff, standard library functions implementated in crazy ways for ISO/POSIX compliance, and own abstractions/functions, that I might also want to skip over.

Well, don't go down that path. Stop bringing decreasingly relevant points. You are too argumentative.

"standard library functions implemented in crazy ways" is what I though about once in my life, when writing a toy compiler whose output was linked to libc. Such "crazy functions" do exist but but apart from libc having little relevance in this context it's not at all like debugging a line my_arr[i] = make_arr_elem(). That is a reasonably looking line but can easily contain two more function calls than is immediately visible, and it's very very tiring in actual practice to step through such code. The only solution I've found is to be careful to use very little such magic.

The general way that C is written in practice is that you don't have 3 function calls per line. So even if you have lots abstractions and macros in place that you would like to ignore (which can happen, but it's more rare than common) you can still skip over them (e.g. F10 in VS).

Re: C++ Insights – See your source code with the eyes of a compiler

#36

Earlier quoted context omitted.

Can Just My Code help if it is precisely my code that I want to skip over? I believe it does not -- IIUC it works based on modules i.e. the DLL that contains the machine code. IOW it doesn't even help if I move the "zero-cost" stuff to a separate source file -- templates and other stuff that have to be instanciated or inlined won't be separable from the code that I want to debug based on the module they're running in…

[[gnu:: artificial]]

cool!

Re: C++ Insights – See your source code with the eyes of a compiler

#37

Honestly, from the title, I expected something that gave actual 'insights' into the compiler, rather than the code. For example, indicating applied and potential (missed) optimizations. But seeing what it was, I was definitely not unpleasantly surprised :) Being able to cut through abstraction is very nice and quite important when you want to understand WHAT you are writing from the machine's perspective (or even in…

Yeah, I like this, but the tool I wish existed the most is some kind of pointer aliasing checker. Something that would tell me when I've violated strict aliasing or, conversely, when I've forgotten to put in a restrict and now the compiler is reloading things from memory unnecessarily.

Re: C++ Insights – See your source code with the eyes of a compiler

#38
post #14
post #2

I wonder how helpful it is for g++? Myself and my ancient h/w and s/w engineering friends have been having a discussion recently about how the modern C++ language features are generating code that is not readily debugged at the source level. The compiler has always created assembly that implements the feature described by teh high level language, but histoirically these have had a relatively one-to-one relationship w…

Even C doesn't map any longer when taking into consideration -O3, UB, vector units, and compiler replaced standard library calls. I miss the part of what a modern debug lacks.

> Even C doesn't map any longer when taking into consideration -O3, UB, vector units, and compiler replaced standard library calls.

I've found it maps orders of magnitude better than C++ under the same optimisation levels.

Re: C++ Insights – See your source code with the eyes of a compiler

#39

I wish there was a way to show the code that is generated when using co_return or co_await e.g. https://cppinsights.io/lnk?code=Ly8gaHR0cHM6Ly93d3cuc2NzLnN0...

Sometimes, wishes come true! Just open the drop-down to select the C++ standard. Below, there is a section "More Transformations". Enable "Show coroutine transformation", and you will get the transformation you're looking for: https://cppinsights.io/s/4d8c3fc1 Disclaimer: I'm the author.

This is very helpful, thx.

Re: C++ Insights – See your source code with the eyes of a compiler

#40
post #14

Earlier quoted context omitted.

Even C doesn't map any longer when taking into consideration -O3, UB, vector units, and compiler replaced standard library calls. I miss the part of what a modern debug lacks.

Don't debug -O3, or -O2 for that matter. Debug a separate debug build without optimizations, or don't heavily optimize your release build if you want to debug that (you know that obviously). Even with optimizations disabled, debugging C++ can be quite painful. Only add a little template stuff to the source code, and a few "zero-cost" methods like indexing operators or cast operators, and it quickly becomes painful to…

Extremely bad advice. There is no point in debugging something that bears no relation to the software you release, and compiler optimizations is the only reason to use C or C++ in the first place.
Post reply on HN