Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
647 changes: 645 additions & 2 deletions melior/src/dialect/llvm.rs

Large diffs are not rendered by default.

64 changes: 64 additions & 0 deletions melior/src/dialect/llvm/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,67 @@ pub fn linkage(context: &Context, linkage: Linkage) -> Attribute<'_> {
};
Attribute::parse(context, &format!("#llvm.linkage<{linkage}>")).unwrap()
}

// https://github.com/llvm/llvm-project/blob/600eeed51f538adc5f43c8223a57608e73aba31f/mlir/include/mlir/Dialect/LLVMIR/LLVMEnums.td#L542-L558
/// LLVM float comparison predicates.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u64)]
pub enum FCmpPredicate {
/// Always returns false.
False = 0,
/// Ordered and equal.
Oeq = 1,
/// Ordered and greater than.
Ogt = 2,
/// Ordered and greater than or equal.
Oge = 3,
/// Ordered and less than.
Olt = 4,
/// Ordered and less than or equal.
Ole = 5,
/// Ordered and not equal.
One = 6,
/// Ordered (no NaNs).
Ord = 7,
/// Unordered or equal.
Ueq = 8,
/// Unordered or greater than.
Ugt = 9,
/// Unordered or greater than or equal.
Uge = 10,
/// Unordered or less than.
Ult = 11,
/// Unordered or less than or equal.
Ule = 12,
/// Unordered or not equal.
Une = 13,
/// Unordered (either NaNs).
Uno = 14,
/// Always returns true.
True = 15,
}
/// LLVM integer comparison predicates.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u64)]
pub enum ICmpPredicate {
/// Equal
Eq = 0,
/// Not equal
Ne = 1,
/// Signed less than
Slt = 2,
/// Signed less than or equal
Sle = 3,
/// Signed greater than
Sgt = 4,
/// Signed greater than or equal
Sge = 5,
/// Unsigned less than
Ult = 6,
/// Unsigned less than or equal
Ule = 7,
/// Unsigned greater than
Ugt = 8,
/// Unsigned greater than or equal
Uge = 9,
}
41 changes: 41 additions & 0 deletions melior/src/dialect/llvm/global.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use crate::{
dialect::llvm::attributes::Linkage,
ir::{Attribute, Region},
};

pub enum GlobalValue<'c> {
/// A global variable.
Value(Attribute<'c>),
/// A global variable with a constant initializer.
Constant(Attribute<'c>),
/// A global variable with a constant initializer and a body.
Complex(Region<'c>),
}

#[derive(Default)]
pub struct GlobalVariableOptions {
pub(crate) addr_space: Option<i32>,
pub(crate) alignment: Option<i64>,
pub(crate) linkage: Option<Linkage>,
}

impl GlobalVariableOptions {
pub fn new() -> Self {
Self::default()
}

pub fn addr_space(mut self, addr_space: i32) -> Self {
self.addr_space = Some(addr_space);
self
}

pub fn alignment(mut self, alignment: i64) -> Self {
self.alignment = Some(alignment);
self
}

pub fn linkage(mut self, linkage: Linkage) -> Self {
self.linkage = Some(linkage);
self
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: melior/src/dialect/llvm.rs
expression: module.as_operation()
---
module {
llvm.mlir.global internal constant @my_global(42 : i64) {addr_space = 0 : i32, alignment = 1 : i64} : i64
llvm.func @get_global_addr() -> !llvm.ptr {
%0 = llvm.mlir.addressof @my_global : !llvm.ptr
llvm.return %0 : !llvm.ptr
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: melior/src/dialect/llvm.rs
expression: module.as_operation()
---
module {
llvm.func @int_bits_to_float(%arg0: i32) -> f32 {
%0 = llvm.bitcast %arg0 : i32 to f32
llvm.return %0 : f32
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: melior/src/dialect/llvm.rs
expression: module.as_operation()
---
module {
llvm.func @add_one(%arg0: i32) -> i32 {
%0 = llvm.mlir.constant(1 : i32) : i32
%1 = arith.addi %arg0, %0 : i32
llvm.return %1 : i32
}
llvm.func @caller(%arg0: i32) -> i32 {
%0 = llvm.call @add_one(%arg0) : (i32) -> i32
llvm.return %0 : i32
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: melior/src/dialect/llvm.rs
expression: module.as_operation()
---
module {
llvm.func @use_intrinsic(%arg0: i32) -> i32 {
%0 = llvm.call_intrinsic "llvm.bitreverse.i32"(%arg0) : (i32) -> i32
llvm.return %0 : i32
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: melior/src/dialect/llvm.rs
expression: module.as_operation()
---
module {
llvm.func @get_constant() -> i32 {
%0 = llvm.mlir.constant(42 : i32) : i32
llvm.return %0 : i32
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: melior/src/dialect/llvm.rs
expression: module.as_operation()
---
module {
llvm.mlir.global internal constant @const_global(42 : i32) {addr_space = 0 : i32, alignment = 4 : i64} : i32
llvm.mlir.global external @mutable_global(123 : i32) {addr_space = 0 : i32, alignment = 8 : i64} : i32
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: melior/src/dialect/llvm.rs
expression: module.as_operation()
---
module {
llvm.func @foo(%arg0: i64, %arg1: i64) -> i1 {
%0 = llvm.icmp "eq" %arg0, %arg1 : i64
llvm.return %0 : i1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: melior/src/dialect/llvm.rs
expression: module.as_operation()
---
module {
llvm.func @call_fn_ptr(%arg0: !llvm.ptr, %arg1: i32) -> i32 {
%0 = llvm.call %arg0(%arg1) : !llvm.ptr, (i32) -> i32
llvm.return %0 : i32
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: melior/src/dialect/llvm.rs
expression: module.as_operation()
---
module {
llvm.func @func_with_unreachable() {
llvm.unreachable
}
}
Loading