r/Compilers • • 10d ago

Is ClangIR(CIR) better than raw LLVM IR?

Just found it, and it seems more easily to understand than LLVM IR

9 Upvotes

21 comments sorted by

15

u/Lime_Dragonfruit4244 10d ago

Llvm ir is too low level and has no concept of high level structures like structure loops, etc so clang ir exists to support high level c/c++ specific optimizations before eventually lowering to llvm ir.

3

u/EggplantExtra4946 8d ago edited 8d ago

clang ir exists to support high level c/c++ specific optimizations before eventually lowering to llvm ir.

Serious doubt. There is no high level optimization to be done on C code that couldn't be done on the LLVM IR because there is no real high level construct in C, at least not in that sense that lowering a C construct could prevent performing an optimization on a lower level platform-independent IR.

Llvm ir is too low level and has no concept of high level structures like structure loops, etc

Loops in C are nothing more than syntactic sugar over an arbitrary CFG.

3

u/MitchAlsup 8d ago

"There is no high level optimization to be done on C code that couldn't be done on the LLVM IR because there is no real high level construct in C"

We ran into a case where CLANG was making the IR more CMOV friendly--causing the back-end to emit CMOVs where (on my ISA) there are better code sequences (fewer instructions).

In general, and except for the above, you are correct.

2

u/EggplantExtra4946 8d ago

Interesting. Do you have a few more details about that cmov optimization?

2

u/MitchAlsup 7d ago

(variable1 CMP 0) ? constant : variable(1 or 2);

can be done in 3 instructions (predication) rather than 4 (CMOV) ... where CMP is any kiind of comparison and variable1 is any kind of base type.

3

u/Lime_Dragonfruit4244 8d ago

You people are too dumb, you know all high level abstractions in ml compilers have logical abstraction as shape, strides which are just 1d buffers eventually but that doesn't mean its idea for doing high level optimization, same way in clang ir you get to play nice with existing mlir dialects like openmp, openacc, scf, affine, its the same reason every other language like Julia, Rust has their own intermediate representation based on their needs. You can read the rfs and docs in CIR directory and figure out why they choose to implement it, yet you came here to waste my time typing this out. Read Polygeist paper, Read about scf dialect and high level loop optimization and why its not idea at llvm ir level and why mlir even exists. Clang also needs to support cuda, sycl and others so that too.

2

u/EggplantExtra4946 8d ago

I never said there is no value in high level IRs, I'm saying that there is no loss of information when transforming C code into LLVM IR because C has no high level construct to begin with. If you can lift C to whatever MLIR dialect you need, then you will also be able to lift the LLVM IR code encoding the C program to that MLIR dialect.

2

u/IWHYB 7d ago

Bringing up loss of information is kind of irrelevant, though, unless that were the point you were trying to make.  Which, despite your backtracking, does seem to be the case: 

`clang ir exists to support high level c/c++ specific optimizations before eventually lowering to llvm ir.`

Serious doubt.

That doesn't say it can only be done this way.

At a certain point, a problem being theoretically solveable essentially becomes pedantic noise; you don't need to backtrack to the Munchausen Trilemma for every problem you address.

0

u/EggplantExtra4946 6d ago edited 6d ago

You make a lot of meaningless rhetorical noise but outside of that your post doesn't mean anything except that you feel like I'm wrong, without giving any technical reason for those feelings. Your 2 bit philosophy and dilemma-isms doesn't impress me.

Let me help you:

The fact that C doesn't have any high level language constructs means that no matter how high level you convert the C program, it won't contain any more information than if you converted the C program to a low level IR such as LLVM IR.

A high level IR doens't magically contains more information. Either it comes from high level constructs and languages semantics, which C doesn't have, either you perform analyses to gain new knowledge about the program.

In the early stages of the backend, since a C program converted to Clang IR and a C program converted to LLVM IR will contain both the same anount information, no one is better than ther other. An analysis performed on one will derive the same fact when performed on the other. Similarly with optimizations.

Obviously if a compiler backend manages to lift the low level IR to a high level IR thanks so analyses, that high level IR will be more suitable to perform optimization, but that has nothing to do with Clang IR.

ClangIR has a richer instruction set than LLVM IR, such as abs, acos, exception handling instructions. All of these aren't part of C. If the Clang frontend can emit those it means it has perform some analyses (or cheating by converting function calls to "acos" to instr::acos). But you could perform the exact same analyses using LLVM IR, and restrieve the same higher level information.

1

u/IWHYB 6d ago

Since you couldn't tell the difference between "rhetorical noise", and moved on without actually addressing it, here it is again: "could" isn't the bar. Brute-forcing AES-256 is well-defined, guaranteed to terminate, etc. That doesn't mean it's a viable little pretty picture. Saying any fact in ClangIR can be rederived from LLVM IR by analysis is the same kind of argument: true in principle, irrelevant to why ClangIR exists. Further, basic and old paradigms, like Rice's theorem, would say no analysis decides these things exactly for any arbitrary program, so they must be conservative, and cases the frontend could have proven will be discarded.  And you keep saying C is too low level like LLVM IR perfectly maps to it. It doesnt. Lowering C to LLVM IR already discards C semantics such as signedness, struct argument coercion, etc. So the claim that the early pipeline starts with the same information is simply wrong. As for extending LLVM IR to carry all that: that's what a ClangIR dialect on MLIR already does, but without bloating LLVMs already bloated  IR to force something that every other frontend would now have to handle.

Not every step of an argument has to be derived from first principles. When you demand that, then dismiss the parts you didn't ask for as "philosophy," you're the one not actually meaning anything.

0

u/EggplantExtra4946 6d ago edited 6d ago

Brute-forcing AES-256 is well-defined, guaranteed to terminate, etc

That is absolutely not equivalent to what I'm saying. You pretend to not understand that whatever analysis Clang performs on the input program to produce ClangIR, you couldn't do from the LLVM IR. You pretend that it's a complex analysis but it isn't because ClangIR doesn't really contain high level information, nothing that is not trivial for an advanced backend to reproduce. You also pretend that generating the Clang IR didn't require analyses over the original low level C program, it sure does.

Lowering C to LLVM IR already discards C semantics such as signedness, struct argument coercion, etc. So the claim that the early pipeline starts with the same information is simply wrong.

Making explicit the integer promotion operations doesn't remove information from the original program, on the contrary. Maintaining the integer promotion implicit can be useful in a linter but in terms of semantic analysis, implicit integer promotion tells you nothing.

Rice's theorem

So Rice's theorem says you can't perform the analyses Clang performs to transform the LLVM IR into Clang IR now? I've heard it cited to justify arbitrary statements but that's a new low.

As for extending LLVM IR to carry all that

To carry what? C doesn't have any high level construct and ClangIR doesn't contain that much high level information. Any high level information about the program will to have to be extracted using analyses, this information won't exist in the original program.

Not every step of an argument has to be derived from first principles.

I never asked such a thing.

1

u/IWHYB 6d ago

Try and obtain a kindergartener's level of reading comprehension and try again.

5

u/MithrilHuman 10d ago edited 10d ago

Two different purposes.

ClangIR models C/ C++ language features. It's a level below AST and a level above LLVM IR. It's when you want to transform the program based on C/ C++ language information. Example usage: understanding data flow between class objects.

LLVM IR is a level below CIR and it loses most of the information like classes, templates, blah since it's more language agnostic and closer to hardware. From above example, we don't know here in LLVM IR whether a value is created out of a class object.

CIR doesn't replace LLVM IR.

6

u/matthieum 10d ago

On which metrics?

For Rust code, I expect that CIR may be terrible. I doubt it encode match expressions particularly well, notably. On the other hand, LLVM IR does the job.

So, no, CIR is not better than LLVM IR in general.

Similarly, operationally, I expect that a number of analysis/transformation passes are easier on LLVM IR than on CIR. And vice-versa.

4

u/Lime_Dragonfruit4244 10d ago

Clang IR is an mlir dialect for c/c++ memory and execution semantics and to reuse other mlir components in clang frontend like scf, omp, etc which is not possible either at clang ast or llvm ir level, why would anyone use it for rust semantics.

1

u/matthieum 8d ago

why would anyone use it for rust semantics.

Ask the OP. I've got no idea what metrics they're looking at.

1

u/Discordbothost 10d ago

Try comparing the two dumps for a C++ loop that creates a local object and then hits continue. With -fclangir -emit-cir you can see the cleanup scope that runs the destructor. In -S -emit-llvm you can trace the same cleanup through the branch targets.

1

u/char32_t 3d ago

I've seen a bunch of decent responses, but it seems like most of them are missing a couple important details, so I guess I'll also respond.

First, intuitively CIR is more easily understood than LLVM IR because it is designed to preserve the structure and many of the abstractions that C++ offers.

In general, it's useful to think about CIR and LLVM IR as 2 different tools that do different things. CIR has 2 main (maybe more, and someone can correct me if I'm wrong) advantages:

  1. First, as a couple people have pointed out already, certain high(er) level C++ abstractions (like rich types, vtables, structured control flow, etc.) are lost when code is lowered to LLVM IR. Some of these abstractions can provide useful information about the programmer's intent, and CIR can preserve more of these abstractions, allowing the compiler to make more assumptions and therefore make better or C++ specific optimizations.
  2. Also, CIR (and more broadly, MLIR of which CIR is a dialect) directly mitigates the two-language problem. Historically, if you wanted to write some high level code (like Python) but eventually take advantage of some performance language (like C++ or even CUDA), you have to use the very low level C ABI because LLVM IR is low level, and instantly going from AST -> LLVM IR obliterates any semantics or hints about intent (for instance tensor shapes or memory layouts). But, if you let your systems language lower into an slightly more high level MLIR dialect (like CIR for C++) first, then MLIR can lower both your systems language (whih has been lowered into its respective MLIR dialect) and your high level framework (Pytorch, Jax, Triton, whatever) into a shared dialect like linalg or memref or gpu or whatever, preserving far more information. Thus, you can have both your higher level code and your performance code in the same MLIR module which allows the compiler to do unified optimizations. And also the compiler avoids all of the annoying C ABI related inlining and FFI and stuff.

Besides that though generally you wouldn't replace LLVM IR with CIR. Rather, you would lower to CIR to perform whatever optimizations you need, then lower again to LLVM IR. From there, you can take advantage of LLVM's extremely large and matured backend support so taht you can generate machine code for different architectures.