LLVM for Rust -- what's the status?


#1

I’m quite new to LLVM. I know there’s a wrapper for LLVM in Rust. I wonder, how much does it wrap? Is it a true Rust LLVM compiler like Clang or what?

I just want to write all code in Rust, not in C++. Can re-write this C++ code for LLVM completely in Rust?

for (auto& B : F) {
   for (auto& I : B) {
     if (auto* op = dyn_cast<BinaryOperator>(&I)) {
      // Insert at the point where the instruction `op` appears.
      IRBuilder<> builder(op);        

      // Make a multiply with the same operands as `op`.
      Value* lhs = op->getOperand(0);
      Value* rhs = op->getOperand(1);
      Value* mul = builder.CreateMul(lhs, rhs);

       // Everywhere the old instruction was used as an operand, use our
       // new multiply instruction instead.
       for (auto& U : op->uses()) {
          User* user = U.getUser();  // A User is anything with operands.
          user->setOperand(U.getOperandNo(), mul);       
       }
        
       // We modified the code.
       return true;     
     }   
   } 
}

#2

The Rust compiler is a front end for LLVM, just like clang is. It takes rust source code and compilers it to the LLVM IR, then passes that to LLVM to optimize and compile to the targeted machine code. You seem to be asking about using LLVM’s API to add a pass to LLVM. This is very different from what rustc and clang do, and I don’t know of any Rust wrappers for LLVM’s API. So no, I don’t think you can write this in Rust right now (unless you write the wrapper for the LLVM API yourself).


#3

Not sure if it covers all of your scope but https://github.com/gereeter/llvm-safe is available.