Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Codegen] Implement proper type resolver #72

Open
edg-l opened this issue Jan 11, 2024 · 1 comment
Open

[Codegen] Implement proper type resolver #72

edg-l opened this issue Jan 11, 2024 · 1 comment
Labels
enhancement New feature or request
Milestone

Comments

@edg-l
Copy link
Collaborator

edg-l commented Jan 11, 2024

Right now we resolve types by name in a simple way, it probably needs enhancing and be done in a non stringly way.

Related code:

fn resolve_type(
&self,
context: &'c MeliorContext,
name: &str,
) -> Result<Type<'c>, Box<dyn Error>> {
Ok(match name {
"u64" | "i64" => IntegerType::new(context, 64).into(),
"u32" | "i32" => IntegerType::new(context, 32).into(),
"u16" | "i16" => IntegerType::new(context, 16).into(),
"u8" | "i8" => IntegerType::new(context, 8).into(),
"bool" => IntegerType::new(context, 1).into(),
_ => todo!("custom type lookup"),
})
}
fn resolve_type_spec(
&self,
context: &'c MeliorContext,
spec: &TypeSpec,
) -> Result<Type<'c>, Box<dyn Error>> {
Ok(match spec {
TypeSpec::Simple { name } => self.resolve_type(context, &name.name)?,
TypeSpec::Generic {
name,
type_params: _,
} => self.resolve_type(context, &name.name)?,
})
}
}

@edg-l edg-l added the enhancement New feature or request label Jan 11, 2024
@igaray igaray added this to the 0.03 Aluminum milestone Jan 18, 2024
@JulianGCalderon
Copy link
Contributor

The function has been renamed to compile_type and a type Enum is used instead of a string. I think we should close this issue as resolved. @igaray

fn compile_type<'c>(ctx: ModuleCodegenCtx<'c>, ty: &Ty) -> Type<'c> {
match &ty.kind {
concrete_ir::TyKind::Unit => Type::none(ctx.ctx.mlir_context),
concrete_ir::TyKind::Bool => IntegerType::new(ctx.ctx.mlir_context, 1).into(),
concrete_ir::TyKind::Char => IntegerType::new(ctx.ctx.mlir_context, 32).into(),
concrete_ir::TyKind::Int(int_ty) => match int_ty {
concrete_ir::IntTy::I8 => IntegerType::new(ctx.ctx.mlir_context, 8).into(),
concrete_ir::IntTy::I16 => IntegerType::new(ctx.ctx.mlir_context, 16).into(),
concrete_ir::IntTy::I32 => IntegerType::new(ctx.ctx.mlir_context, 32).into(),
concrete_ir::IntTy::I64 => IntegerType::new(ctx.ctx.mlir_context, 64).into(),
concrete_ir::IntTy::I128 => IntegerType::new(ctx.ctx.mlir_context, 128).into(),
},
concrete_ir::TyKind::Uint(uint_ty) => match uint_ty {
concrete_ir::UintTy::U8 => IntegerType::new(ctx.ctx.mlir_context, 8).into(),
concrete_ir::UintTy::U16 => IntegerType::new(ctx.ctx.mlir_context, 16).into(),
concrete_ir::UintTy::U32 => IntegerType::new(ctx.ctx.mlir_context, 32).into(),
concrete_ir::UintTy::U64 => IntegerType::new(ctx.ctx.mlir_context, 64).into(),
concrete_ir::UintTy::U128 => IntegerType::new(ctx.ctx.mlir_context, 128).into(),
},
concrete_ir::TyKind::Float(float_ty) => match float_ty {
concrete_ir::FloatTy::F32 => Type::float32(ctx.ctx.mlir_context),
concrete_ir::FloatTy::F64 => Type::float64(ctx.ctx.mlir_context),
},
concrete_ir::TyKind::String => todo!(),
concrete_ir::TyKind::Array(inner_type, length) => {
let inner_type = compile_type(ctx, inner_type);
let length = match length.data {
concrete_ir::ConstKind::Value(ValueTree::Leaf(ConstValue::U64(length))) => length,
_ => unimplemented!(),
};
melior::dialect::llvm::r#type::array(inner_type, length as u32)
}
concrete_ir::TyKind::Ref(_inner_ty, _) | concrete_ir::TyKind::Ptr(_inner_ty, _) => {
llvm::r#type::pointer(ctx.ctx.mlir_context, 0)
}
concrete_ir::TyKind::Param { .. } => todo!(),
concrete_ir::TyKind::Struct { id, generics: _ } => {
let body = ctx.ctx.program.structs.get(id).unwrap();
let mut fields = Vec::new();
for field in &body.variants {
let ty = compile_type(ctx, &field.ty);
fields.push(ty);
}
let ty = melior::dialect::llvm::r#type::r#struct(ctx.ctx.mlir_context, &fields, false);
ty
}
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants