Chris Lattner, the founder and chief architect of LLVM and the creator of the Swift programming language, and Tim Davis, former head of AI projects at Google such as TensorFlow and JAX, introduced a new programming language called Mojo. This language combines ease of use for research development and rapid prototyping with suitability for creating high-performance end products. The first goal is achieved through a familiar Python-like syntax, while the second is accomplished through the ability to compile to machine code, safe memory management mechanisms, and support for hardware acceleration techniques.
The project is focused on applications in machine learning, but it is presented as a general-purpose language that extends Python's capabilities with system programming and is suitable for a wide range of tasks. For example, the language can be applied in areas such as high-performance computing, data processing, and transformation. An interesting feature of Mojo is the ability to use the emoji symbol "🔥" as a file extension for code files (e.g., "helloworld.🔥"), in addition to the text extension ".mojo."
Currently, the language is under intensive development and only an online interface is available for testing. Separate builds for running on local systems are promised to be published later, after feedback is received about the interactive web environment. The source codes of the compiler, JIT, and other related project components are planned to be released after the internal architecture design is completed (the model of developing a working prototype behind closed doors is reminiscent of the initial stages of LLVM, Clang, and Swift development). Since Mojo's syntax is based on Python and its type system is close to C/C++, there's a plan to develop tools to facilitate the translation of existing C/C++ and Python projects to Mojo, as well as to develop hybrid projects that combine code written in Python and Mojo.
The project is designed to leverage existing hardware resources in heterogeneous systems for computation. For instance, GPU, specialized accelerators for machine learning, and vector processor instructions (SIMD) can be utilized for running applications in the Mojo language and parallelizing computations. The development of a separate subset of the Python language is mentioned as a reason, rather than optimizing the existing CPython, focusing on compilation, integrating systems programming capabilities, and utilizing a fundamentally different internal architecture that allows executing code on GPUs and various hardware accelerators. The developers of Mojo aim to maintain compatibility with CPython as much as possible.
Mojo can be used both in an interpreted mode with JIT and for compilation into executable files (AOT, ahead-of-time). The compiler incorporates modern technologies for automatic optimization, caching, and distributed compilation. Source code written in Mojo is transformed into low-level intermediate code MLIR (Multi-Level Intermediate Representation), developed by the LLVM project, which provides additional optimization opportunities for processing data flow graphs. The compiler allows for the generation of machine code using various backends that support MLIR.
Utilizing additional hardware mechanisms to accelerate computations enables achieving performance that outstrips applications written in C/C++ during intensive computations. For example, when testing an application that generates multiple Mandelbrot sets, a compiled application in the Mojo language running in the AWS cloud (r7iz.metal-16xl) was found to be 6 times faster than the C++ implementation (0.03 seconds versus 0.20 seconds), and also 35,000 times faster than the application in Python using the standard CPython 3.10.9 (0.03 seconds versus 1027 seconds) and 1500 times faster when using PYPY (0.03 seconds versus 46.1 seconds).
When evaluating performance in the area of machine learning task solutions, the AI stack Modular Inference Engine, written in Mojo, proved to be three times faster than the TensorFlow-based solution on an Intel processor when processing a language model, 6.4 times faster when performing a recommendation model, and 2.1 times faster when working with visual information processing models. When using AMD processors, the advantage of Mojo was 3.2, 5, and 2.2 times, while for ARM processors, it was 5.3, 7.5, and 1.7 times, respectively. The PyTorch-based solution lagged behind Mojo by 1.4, 1.1, and 1.5 times on Intel CPU, by 2.1, 1.2, and 1.5 times on AMD CPU, and 4, 4.3, and 1.3 times on ARM CPU.

The language supports static typing and tools for safe low-level memory manipulation, similar to Rust, such as tracking the lifetime of references and variable borrowing checks (borrow checker). In addition to safe pointer manipulation, the language also provides low-level capabilities, such as direct memory access in unsafe mode using the Pointer type, calling individual SIMD instructions, or accessing hardware extensions like TensorCores and AMX.

To simplify the separation of classical and optimized Python code for functions with explicit type definitions for all variables, it has been proposed to use a separate keyword 'fn' instead of 'def'. Similarly, for classes, when static data packing in memory during compilation (as in C) is necessary, the type 'struct' can be used instead of 'class'. Simple importing of modules from C/C++ languages is also possible; for example, to import the cos function from the math library, one can specify 'from "math.h" import cos'.
Source: opennet.ru
