This Will Eventually Replace C++

This Will Eventually Replace C++

Not so long ago, I wanted to make an algorithm run on a particular GPU. So I spent about a month setting up a special C++ compiler, tuning it up with its special flags, debugging and optimizing the GPU code.

Not so long ago, I wanted to make an algorithm run on a particular GPU. So I spent about a month setting up a special C++ compiler, tuning it up with its special flags, debugging and optimizing the GPU code. I was very pleased in the end since not only I managed to make it work, but the resulting implementation, due to all the optimizations, boiled down to ~400 lines of GPU’s native machine code.

Yey!

Except, when you look at it from another angle, if I were writing in assembly from the start, 400 lines of code in one month doesn’t seem like a noticeable achievement.

We used to write GPU shaders in assembly not so long ago. In 2006, I was translating some from assembly to GLSL for maintainability reasons. Maintainability, and rapid development in general, was the king back then. Now, however, with cloud-based SaaS getting its popularity, the focus shifts from rapid development to optimization.

Desktop software brings money from sales, and software sales because of its features. You do more features in a unit of time — you make more money.

Software as a service is slightly different. You charge for the service, but you also pay for the run time. The faster your software runs, the more you save. The more you save — the cheaper you can sell your service while maintaining a profit. The cheaper your service is — the more clients you attract. The fastest code wins the market.

Compilers were made to automate tedious assembly programming. To make development faster. Not the code. Sure, compilers can do a lot of optimizations by themselves, but it’s more or less auxiliary to their main function which is saving cost on coding.

As the market model changes, so change the priorities. As far as it goes, it wouldn’t take long before we start translating GLSL shaders back to the assembly.

Unless something new replaces compilers as a technology.

I think, not only C++ in particular, but optimizing compilers in general overstayed their welcome. They come from the 1950s and although every particular compiler advanced greatly over the years, the core of the technology remained mostly unchallenged.

NASA, Public domain, via Wikimedia Commons

But what exactly would we want to challenge? What is a compiler?

I’d like to propose a mental model. It might feel alien at first but hear me out.

An optimizing compiler is a constrained minimization problem solver.

The target function, the thing we want to minimize is the run time of the resulting machine code.

The *constraints *are our source code. We used to see it as text, as some language construct. But essentially it’s just a set of constraints. We don’t care about how exactly it is executed, how exactly the machine code will look, we only want that, if there is 2+2 in the code, there will be 4 in the output.

And the mathematical space, where we’re looking for the minimum, is the space of all the possible valid machine code.

Of course, you can’t just compile a program by running some gradient descent algorithm in this space. The constraints are too many, the space is too large, none of the classical minimization algorithms are realistic in this space. So compilers use a special strategy to keep within constraints. They translate code from one representation to another and only “travel” the optimization space sparingly within the current representation.

This “travel” means that in every given representation, for instance, one of GCC intermediate languages, or LLVM intermediate representation, or even the machine code at the back end, the code is altered to run faster while maintaining some level of semantic equivalency.

E. g. Intel Atom Z has a very small fetch frame. It’s only 8 bytes. This means that given the average size of Intel instruction, it can’t normally take more than one instruction a fetch. GCC knows that. It rearranges instructions so the longer ones are followed with the shortest ones and now more pairs are fetched together. The program is semantically the same but it runs faster.

Sometimes you have to sacrifice some of the equivalency for performance. Arithmetic optimizations in floating-point numbers are such an example. Floating-point numbers are not strictly associative meaning that (a+b)+c ≠ a+(b+c). They are “almost associative” but due to the way they manage the computational error, they aren’t. So if you want to rearrange your instructions to make code faster, you have to give up some of the equivalency strictness.

software-engineering cpp future

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Software Developer vs Software Engineer — Differences: Bogus or Real?

In this article, see if there are any differences between software developers and software engineers. What you’re about to read mostly revolves around my personal thoughts, deductions, and offbeat imagination. If you have different sentiments, add them in the comment section, and let’s dispute! So, today’s topic…

Science and Engineering in Software Architecture

Science and Engineering in Software Architecture - We are passing through tough times, “The moment everything changed” with COVID-19, quarentine, people trying adapt their day to day…

Deploy a white label futures trading software to boost your position

Avail white label cryptocurrency exchange software solutions from Infinite Block Tech, that is quick to launch and cost-effective. The software supports multiple cryptocurrencies, including major fiat currencies, integrated with a multi-currency wallet ,and supports both web and mobile versions.

Custom Software vs Off-the-shelf Software: How to select a better one for your business?

Custom Software or Off-the-shelf software, the question in mind for many business personnel. Read this blog to get help to make right decision that will benefit your business.

Buy cryptos with fiat currency instantly using our White label futures trading software

Avail white label cryptocurrency exchange software solutions from Infinite Block Tech, that is quick to launch and cost-effective. The software supports multiple cryptocurrencies, including major fiat currencies, integrated with a multi-currency wallet ,and supports both web and mobile versions.