MLIR (software)
≈ 6 min read
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
MLIR (Multi-Level Intermediate Representation) is an open-source compiler infrastructure project developed as a sub-project of the LLVM project. It provides a modular and extensible intermediate representation (IR) framework intended to facilitate the construction of domain-specific compilers and improve compilation for heterogeneous computing platforms. MLIR supports multiple abstraction levels in a single IR and introduces dialects, a mechanism for defining custom operations, types, and attributes tailored to specific domains. The name "Multi-Level Intermediate Representation" reflects the system’s ability to model computations at various abstraction levels and progressively lower them toward machine code. MLIR was originally developed in 2018 by Chris Lattner at Google, and publicly released as part of LLVM in 20191. It was designed to address challenges in building compilers for modern workloads such as machine learning, hardware acceleration, and high-level synthesis by providing reusable components and standardizing the representation of intermediate computations across different programming languages and hardware targets. MLIR is used in a range of systems including TensorFlow, Mojo, TPU-MLIR, and others. It is released under the Apache License 2.0 with LLVM exceptions and is maintained as part of the LLVM project.
MLIR (Multi-Level Intermediate Representation) is an open-source compiler infrastructure project developed as a sub-project of the LLVM project. It provides a modular and extensible intermediate representation (IR) framework intended to facilitate the construction of domain-specific compilers and improve compilation for heterogeneous computing platforms. MLIR supports multiple abstraction levels in a single IR and introduces dialects, a mechanism for defining custom operations, types, and attributes tailored to specific domains. The name "Multi-Level Intermediate Representation" reflects the system’s ability to model computations at various abstraction levels and progressively lower them toward machine code. MLIR was originally developed in 2018 by Chris Lattner at Google, and publicly released as part of LLVM in 2019. It was designed to address challenges in building compilers for modern workloads such as machine learning, hardware acceleration, and high-level synthesis by providing reusable components and standardizing the representation of intermediate computations across different programming languages and hardware targets. MLIR is used in a range of systems including TensorFlow, Mojo, TPU-MLIR, and others. It is released under the Apache License 2.0 with LLVM exceptions and is maintained as part of the LLVM project. %res:2 = "mydialect.morph"(%input#3) ({ ^bb0(%arg0: !mydialect loc("mysource.cc":10:8)): // nested operations }) { some.attribute = true, other_attribute = 1.5 } : (!mydialect ) -> (!mydialect , !mydialect ) loc(callsite("foo" at "mysource.cc":10:8)) This operation, named morph , belongs to the mydialect dialect. It takes one input operand ( %input#3 ) of type custom_type and produces two output values of type other_type . The operation includes two attributes- some.attribute and other_attribute -and contains a region with a single block ( ^bb0 ) that accepts one argument. The loc keyword specifies source-level location information, which can be used for debugging or diagnostic reporting. The syntax of operations, types and attributes can also be customized according to the user preferences by implementing proper parsing and printing functions within the operation definition. func.func @matrix_add(%arg0: memref , %arg1: memref ) -> memref { %result = memref.alloc() : memref affine.for %i = 0 to 10 { affine.for %j = 0 to 20 { %lhs = memref.load %arg0[%i, %j] : memref %rhs = memref.load %arg1[%i, %j] : memref %sum = arith.addf %lhs, %rhs : f32 memref.store %sum, %result[%i, %j] : memref } } func.return %result : memref } Although different dialects may be used to express similar computations, the level of abstraction and the intended compilation flow may vary. In the example above, the affine dialect enables polyhedral analysis and optimizations, while the memref and arith dialects express memory and arithmetic operations, respectively. let assemblyFormat = "attr-dict ($operands^ `:` type($operands))?"; map = affine_map (d0, d1)> module { func.func @avg(%arg0: memref , %arg1: memref ) -> memref { %alloc = memref.alloc() : memref %c0 = arith.constant 0 : index %c10 = arith.constant 10 : index %c1 = arith.constant 1 : index scf.for %arg2 = %c0 to %c10 step %c1 { %c0_0 = arith.constant 0 : index %c20 = arith.constant 20 : index %c1_1 = arith.constant 1 : index scf.for %arg3 = %c0_0 to %c20 step %c1_1 { %0 = memref.load %arg0[%arg2, %arg3] : memref %1 = memref.load %arg1[%arg2, %arg3] : memref %2 = arith.addf %0, %1 : f32 memref.store %2, %alloc[%arg2, %arg3] : memref } } return %alloc : memref } } Another possible strategy, however, could have been to use the gpu dialect to generate code for GPUs: map = affine_map (d0, d1)> module { func.func @avg(%arg0: memref , %arg1: memref ) -> memref { %alloc = memref.alloc() : memref %c0 = arith.constant 0 : index %c10 = arith.constant 10 : index %0 = arith.subi %c10, %c0 : index %c1 = arith.constant 1 : index %c0_0 = arith.constant 0 : index %c20 = arith.constant 20 : index %1 = arith.subi %c20, %c0_0 : index %c1_1 = arith.constant 1 : index %c1_2 = arith.constant 1 : index gpu.launch blocks(%arg2, %arg3, %arg4) in (%arg8 = %0, %arg9 = %c1_2, %arg10 = %c1_2) threads(%arg5, %arg6, %arg7) in (%arg11 = %1, %arg12 = %c1_2, %arg13 = %c1_2) { %2 = arith.addi %c0, %arg2 : index %3 = arith.addi %c0_0, %arg5 : index %4 = memref.load %arg0[%2, %3] : memref %5 = memref.load %arg1[%2, %3] : memref %6 = arith.addf %4, %5 : f32 memref.store %4, %alloc[%2, %3] : memref gpu.terminator } return %alloc : memref } }
Text derived from https://en.wikipedia.org/w/index.php?oldid=1378854463 under CC BY-SA 4.0; modifications have been made.