Skip to content

Commit

Permalink
Merge pull request #21 from lambdaclass/progress
Browse files Browse the repository at this point in the history
improvements
  • Loading branch information
edg-l authored Dec 10, 2024
2 parents 389d7f2 + c507f7d commit 06d791d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
22 changes: 15 additions & 7 deletions docs/src/mlir_basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ module.body().append_operation(operation)

## Operation

An operation is an instruction. It holds regions, which themselves hold blocks. It also has attributes, operands and results.
An operation is an instruction. It can hold regions, which themselves hold blocks. It also has attributes, operands, results and succesors.

- Attributes are like configuration parameters for the operation.
- Operands are the inputs, values.
- Results are the result values the operation produces, it can be 1 or more.
- Successors are blocks to branch into.

### Types

Expand All @@ -64,14 +65,15 @@ let my_u64: Type<'c> = IntegerType::new(context, 64).into();

### Attributes

Most operations accept or require attributes. For example the `func.func` operation requires a `StringAttribute` to define the function name, or a `TypeAttribute` to define the function type.
Most operations accept or require attributes. For example the `func.func` operation requires a `StringAttribute` to define the function name, some other operations may require a `TypeAttribute` to pass type information for example.

```rust
let my_type_attr: Attribute<'c> =
TypeAttribute::new(IntegerType::new(context, 64).into()).into();
```

In melior there are 4 ways to create a operation: Using ODS, using a method from the `dialect` melior rust module or using the operation builder.
In melior there are 4 ways to create a operation: Using ods, using
a method from the `dialect` melior rust module or using the operation builder.

### ODS

Expand All @@ -82,7 +84,10 @@ With ODS:
```rust
use melior::dialect::ods;

let my_alloca = block.append_operation(ods::llvm::alloca(context, res, array_size, elem_type, location).into());
let my_alloca = block.append_operation(
ods::llvm::alloca(context, res, array_size,
elem_type, location).into()
);
// Get the returned ptr
let ptr: Value<'c> = my_alloca.result(0).unwrap().into();
```
Expand All @@ -93,7 +98,8 @@ This is a handcrafted API, so it may miss a lot of operations:

```rust
let my_alloca = block.append_operation(
melior::dialect::llvm::alloca(context, array_size, ptr_type, location, extra_options)
melior::dialect::llvm::alloca(context, array_size, ptr_type,
location, extra_options)
);
// Get the returned ptr
let ptr: Value<'c> = my_alloca.result(0).unwrap().into();
Expand All @@ -120,7 +126,7 @@ Some frequently used operations, mainly those in the llvm, arith and builtin dia

## Region

A region holds one or multiple blocks. It depends on the operation whether there are 1 or more regions.
A region holds one or multiple blocks. It depends on the operation whether there are 0 or more regions.

Usually multiple regions are used in higher level dialects, like SCF, which has while and for constructs. The CF dialect instead works
with blocks.
Expand All @@ -141,7 +147,9 @@ let func_op = func::func(context, name, r#type, region, attributes, location);

## Block

A block holds a sequence of operations. Control flow can only happen within the isolated operations but control returns always to the next operation within the block. A block must always have a terminator, that is an operation that has the Terminator Trait. This is usually operations that do branching like `cf.br` or that diverge `llvm.unreachable`
A block holds a sequence of operations. Control flow can only happen within the isolated operations but control returns always to the next operation within the block.

A block must always have a terminator, that is a operation that has the Terminator Trait. This is usually operations that do branching like `cf.br` or that diverge `llvm.unreachable`

```rust
// To create a block we must pass the arguments it accepts, it is an array of a tuple of (Type, Location)
Expand Down
1 change: 1 addition & 0 deletions src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub fn compile_program(program: &Program, optlevel: OptLevel, out_name: &Path) {
link_binary(&[out_obj], out_name).unwrap();
}

#[cfg(test)]
pub fn compile_program_jit(program: &Program) -> ExecutionEngine {
// We need a registry to hold all the dialects
let registry = DialectRegistry::new();
Expand Down

0 comments on commit 06d791d

Please sign in to comment.