From c6a8b0707db8e914727f32a899cb8c004e499f45 Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Mon, 8 May 2023 13:33:28 +1000 Subject: [PATCH] Port part of `edg-l/melior-next` (#98) - [edg-l/melior-next](https://github.com/edg-l/melior-next) --- LICENSE | 2 +- src/dialect/llvm/type.rs | 15 +++++++++++++ src/ir/location.rs | 6 +++--- src/ir/type/type_like.rs | 46 +++++++++++++++++++++++++++++++++++++--- 4 files changed, 62 insertions(+), 7 deletions(-) diff --git a/LICENSE b/LICENSE index d538f22826..e7b6b389e3 100644 --- a/LICENSE +++ b/LICENSE @@ -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. diff --git a/src/dialect/llvm/type.rs b/src/dialect/llvm/type.rs index e1bd6602c1..3ba13576f6 100644 --- a/src/dialect/llvm/type.rs +++ b/src/dialect/llvm/type.rs @@ -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)) } @@ -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(); diff --git a/src/ir/location.rs b/src/ir/location.rs index bc00e9606f..a9c4fbd928 100644 --- a/src/ir/location.rs +++ b/src/ir/location.rs @@ -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(), @@ -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(), @@ -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())) } } diff --git a/src/ir/type/type_like.rs b/src/ir/type/type_like.rs index e642dc6393..c766cf8445 100644 --- a/src/ir/type/type_like.rs +++ b/src/ir/type/type_like.rs @@ -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. @@ -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 { + 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()) } @@ -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();