Skip to main content

pliron_llvm/
lib.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) The pliron contributors
3
4//! LLVM Dialect for [pliron]
5
6use pliron::{
7    builtin::ops::ModuleOp,
8    context::Context,
9    derive::{op_interface, type_interface},
10    irbuild::dialect_conversion::{DialectConversionRewriter, OperandsInfo},
11    op::Op,
12    opts::{
13        constants::sccp::SCCPPass, dce::DCEPass, mem2reg::Mem2RegPass,
14        simplify_cfg::SimplifyCFGPass,
15    },
16    pass::{NestedOpsPass, OpPass, Passes},
17    result::Result,
18    r#type::{Type, TypeHandle},
19};
20
21use crate::{builtin_to_llvm::builtin_to_llvm_pass, ops::FuncOp};
22
23pub mod attributes;
24pub mod builtin_to_llvm;
25pub mod function_call_utils;
26pub mod interface_impls;
27pub mod op_interfaces;
28pub mod ops;
29pub mod types;
30
31#[cfg(feature = "llvm-sys")]
32pub mod from_llvm_ir;
33#[cfg(feature = "llvm-sys")]
34pub mod llvm_sys;
35#[cfg(feature = "llvm-sys")]
36pub mod to_llvm_ir;
37
38/// Interface for rewriting to LLVM dialect.
39#[op_interface]
40pub trait ToLLVMDialect {
41    /// Rewrite [self] to LLVM dialect.
42    fn rewrite(
43        &self,
44        ctx: &mut Context,
45        rewriter: &mut DialectConversionRewriter,
46        operands_info: &OperandsInfo,
47    ) -> Result<()>;
48
49    fn verify(_op: &dyn Op, _ctx: &Context) -> Result<()>
50    where
51        Self: Sized,
52    {
53        Ok(())
54    }
55}
56
57/// Interface for converting to an LLVM type.
58#[type_interface]
59pub trait ToLLVMType {
60    /// Convert [self] to an LLVM type.
61    fn convert(&self, ctx: &Context) -> Result<TypeHandle>;
62
63    fn verify(_ty: &dyn Type, _ctx: &Context) -> Result<()>
64    where
65        Self: Sized,
66    {
67        Ok(())
68    }
69}
70
71/// Append -O1 passes to the given list of passes.
72pub fn append_o1_passes(module_passes: &mut OpPass<ModuleOp, Passes>) {
73    let mut passes = Passes::default();
74    passes.add_pass(OpPass::<FuncOp, Mem2RegPass>::default());
75    passes.add_pass(OpPass::<FuncOp, SCCPPass>::default());
76    passes.add_pass(OpPass::<FuncOp, SimplifyCFGPass>::default());
77    passes.add_pass(OpPass::<FuncOp, DCEPass>::default());
78
79    module_passes.add_pass(NestedOpsPass::new(passes));
80    // Optimizations may introduce builtin ops that need to be converted to LLVM ops
81    module_passes.add_pass(builtin_to_llvm_pass());
82}