Skip to content

Commit

Permalink
Port part of edg-l/melior-next (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
raviqqe committed May 8, 2023
1 parent 6967147 commit c6a8b07
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 7 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@

END OF TERMS AND CONDITIONS

Copyright 2022 Yota Toyama, LLVM Team
Copyright 2022 Yota Toyama, Edgar Luque, LLVM Team

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
15 changes: 15 additions & 0 deletions src/dialect/llvm/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ pub fn function<'c>(
}
}

/// Creates an LLVM opaque pointer type.
pub fn opaque_pointer(context: &Context) -> Type {
Type::parse(context, "!llvm.ptr").unwrap()
}

/// Creates an LLVM pointer type.
pub fn pointer(r#type: Type, address_space: u32) -> Type {
unsafe { Type::from_raw(mlirLLVMPointerTypeGet(r#type.to_raw(), address_space)) }
Expand Down Expand Up @@ -69,6 +74,16 @@ mod tests {
context
}

#[test]
fn opaque_pointer() {
let context = create_context();

assert_eq!(
super::opaque_pointer(&context),
Type::parse(&context, "!llvm.ptr").unwrap()
);
}

#[test]
fn pointer() {
let context = create_context();
Expand Down
6 changes: 3 additions & 3 deletions src/ir/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl<'c> Location<'c> {
}

/// Creates a fused location.
pub fn fused(context: &Context, locations: &[Self], attribute: Attribute) -> Self {
pub fn fused(context: &'c Context, locations: &[Self], attribute: Attribute) -> Self {
unsafe {
Self::from_raw(mlirLocationFusedGet(
context.to_raw(),
Expand All @@ -47,7 +47,7 @@ impl<'c> Location<'c> {
}

/// Creates a name location.
pub fn name(context: &Context, name: &str, child: Location) -> Self {
pub fn name(context: &'c Context, name: &str, child: Location) -> Self {
unsafe {
Self::from_raw(mlirLocationNameGet(
context.to_raw(),
Expand All @@ -58,7 +58,7 @@ impl<'c> Location<'c> {
}

/// Creates an unknown location.
pub fn unknown(context: &Context) -> Self {
pub fn unknown(context: &'c Context) -> Self {
unsafe { Self::from_raw(mlirLocationUnknownGet(context.to_raw())) }
}

Expand Down
46 changes: 43 additions & 3 deletions src/ir/type/type_like.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::Id;
use crate::context::ContextRef;
use mlir_sys::{
mlirTypeDump, mlirTypeGetContext, mlirTypeGetTypeID, mlirTypeIsABF16, mlirTypeIsAF16,
mlirTypeIsAF32, mlirTypeIsAF64, mlirTypeIsAFunction, mlirTypeIsAMemRef, mlirTypeIsATuple,
mlirTypeIsAVector, MlirType,
mlirIntegerTypeGetWidth, mlirTypeDump, mlirTypeGetContext, mlirTypeGetTypeID, mlirTypeIsABF16,
mlirTypeIsAF16, mlirTypeIsAF32, mlirTypeIsAF64, mlirTypeIsAFunction, mlirTypeIsAIndex,
mlirTypeIsAInteger, mlirTypeIsAMemRef, mlirTypeIsATuple, mlirTypeIsAVector, MlirType,
};

/// Trait for type-like types.
Expand All @@ -21,6 +21,25 @@ pub trait TypeLike<'c> {
unsafe { Id::from_raw(mlirTypeGetTypeID(self.to_raw())) }
}

/// Returns `true` if a type is integer.
fn is_integer(&self) -> bool {
unsafe { mlirTypeIsAInteger(self.to_raw()) }
}

/// Gets a bit width of an integer type.
fn get_width(&self) -> Option<usize> {
if self.is_integer() {
Some(unsafe { mlirIntegerTypeGetWidth(self.to_raw()) } as usize)
} else {
None
}
}

/// Returns `true` if a type is index.
fn is_index(&self) -> bool {
unsafe { mlirTypeIsAIndex(self.to_raw()) }
}

/// Returns `true` if a type is bfloat16.
fn is_bfloat16(&self) -> bool {
unsafe { mlirTypeIsABF16(self.to_raw()) }
Expand Down Expand Up @@ -87,6 +106,27 @@ mod tests {
assert_eq!(Type::index(&context).id(), Type::index(&context).id());
}

#[test]
fn is_integer() {
let context = Context::new();

assert!(Type::integer(&context, 64).is_integer());
}

#[test]
fn get_width() {
let context = Context::new();

assert_eq!(Type::integer(&context, 64).get_width(), Some(64));
}

#[test]
fn is_index() {
let context = Context::new();

assert!(Type::index(&context).is_index());
}

#[test]
fn is_bfloat16() {
let context = Context::new();
Expand Down

0 comments on commit c6a8b07

Please sign in to comment.