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
6// Without llvm-sys, we should be able to build without std.
7#![cfg_attr(not(feature = "llvm-sys"), no_std)]
8
9extern crate alloc;
10
11// A static check to ensure that `std` on pliron-llvm pulls in pliron with `std`.
12#[cfg(feature = "std")]
13const _: () = assert!(
14    pliron::std_deps::STD_ENABLED,
15    "pliron-llvm's `std` feature must forward to `pliron/std`"
16);
17
18use pliron::{
19    builtin::ops::ModuleOp,
20    context::Context,
21    derive::{op_interface, type_interface},
22    irbuild::dialect_conversion::{DialectConversionRewriter, OperandsInfo},
23    op::Op,
24    opts::{
25        constants::sccp::SCCPPass, dce::DCEPass, mem2reg::Mem2RegPass,
26        simplify_cfg::SimplifyCFGPass,
27    },
28    pass::{NestedOpsPass, OpPass, Passes},
29    result::Result,
30    r#type::{Type, TypeHandle},
31};
32
33use crate::{builtin_to_llvm::builtin_to_llvm_pass, ops::FuncOp};
34
35pub mod attributes;
36pub mod builtin_to_llvm;
37pub mod function_call_utils;
38pub mod interface_impls;
39pub mod llvm_attrs;
40pub mod metadata;
41pub mod op_interfaces;
42pub mod ops;
43pub mod types;
44
45#[cfg(feature = "llvm-sys")]
46pub mod data_layout;
47#[cfg(feature = "llvm-sys")]
48pub mod from_llvm_ir;
49#[cfg(feature = "llvm-sys")]
50pub mod llvm_attrs_conversions;
51#[cfg(feature = "llvm-sys")]
52pub mod llvm_sys;
53#[cfg(feature = "llvm-sys")]
54pub mod metadata_conversions;
55#[cfg(feature = "llvm-sys")]
56pub mod to_llvm_ir;
57
58/// Interface for rewriting to LLVM dialect.
59#[op_interface]
60pub trait ToLLVMDialect {
61    /// Rewrite [self] to LLVM dialect.
62    fn rewrite(
63        &self,
64        ctx: &mut Context,
65        rewriter: &mut DialectConversionRewriter,
66        operands_info: &OperandsInfo,
67    ) -> Result<()>;
68
69    fn verify(_op: &dyn Op, _ctx: &Context) -> Result<()>
70    where
71        Self: Sized,
72    {
73        Ok(())
74    }
75}
76
77/// Interface for converting to an LLVM type.
78#[type_interface]
79pub trait ToLLVMType {
80    /// Convert [self] to an LLVM type.
81    fn convert(&self, ctx: &Context) -> Result<TypeHandle>;
82
83    fn verify(_ty: &dyn Type, _ctx: &Context) -> Result<()>
84    where
85        Self: Sized,
86    {
87        Ok(())
88    }
89}
90
91/// Append -O1 passes to the given list of passes.
92pub fn append_o1_passes(module_passes: &mut OpPass<ModuleOp, Passes>) {
93    let mut passes = Passes::default();
94    passes.add_pass(OpPass::<FuncOp, Mem2RegPass>::default());
95    passes.add_pass(OpPass::<FuncOp, SCCPPass>::default());
96    passes.add_pass(OpPass::<FuncOp, SimplifyCFGPass>::default());
97    passes.add_pass(OpPass::<FuncOp, DCEPass>::default());
98
99    module_passes.add_pass(NestedOpsPass::new(passes));
100    // Optimizations may introduce builtin ops that need to be converted to LLVM ops
101    module_passes.add_pass(builtin_to_llvm_pass());
102}