1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright (c) 2020-2021 Thomas Kramer.
// SPDX-FileCopyrightText: 2022 Thomas Kramer <code@tkramer.ch>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

//! Traits for simple placement engines that care about standard-cells only and cannot handle obstructions.

pub use libreda_db::iron_shapes::prelude::{Point, SimplePolygon};
pub use libreda_db::prelude as db;
pub use libreda_db::prelude::traits::*;
pub use libreda_db::prelude::{SInt, TryCastCoord, UInt, WindingNumber};
use std::collections::{HashMap, HashSet};

use crate::db::{L2NBase, SimpleTransform};
use crate::place::mixed_size_placer::{MixedSizePlacer, PlacementError};
use crate::place::placement_problem::PlacementProblem;
use log;

/// Traits for simple placement engines that care about standard-cells only and  cannot handle obstructions.
pub trait SimpleStdCellPlacer<N: NetlistBase> {
    /// Get the name of the placement engine.
    fn name(&self) -> &str;

    /// Find the rough positions of all cell instances inside `cell`.
    ///
    /// # Parameters
    ///
    /// * `netlist`: The netlist holding the connectivity information.
    /// * `cell_id`: The ID of the cell containing all the standard-cell instances to be placed.
    /// * `core_area`: The region where the cells are allowed to be placed.
    /// * `initial_positions`: Initial positions of the cells. For movable instances this can serve as a hint for the optimization, fixed instances must have a position defined.
    /// * `fixed_instances`: A set of instances that have fixed positions and cannot be moved.
    /// * `cell_outlines`: Rectangular outline shapes of the cells.
    ///
    /// # Returns
    ///
    /// Returns a `HashMap` which maps cell instances to positions.
    fn find_cell_positions_impl(
        &self,
        netlist: &N,
        top_cell: &N::CellId,
        core_area: &SimplePolygon<db::Coord>,
        initial_positions: &HashMap<N::CellInstId, Point<db::Coord>>,
        fixed_instances: &HashSet<N::CellInstId>,
        cell_outlines: &HashMap<N::CellId, db::Rect<db::Coord>>,
        net_weights: &HashMap<N::NetId, f64>,
    ) -> HashMap<N::CellInstId, Point<db::Coord>>;

    /// Calls `find_cell_positions_impl()` before and after doing some sanity checks.
    fn find_cell_positions(
        &self,
        netlist: &N,
        cell_id: &N::CellId,
        core_area: &SimplePolygon<db::Coord>,
        initial_positions: &HashMap<N::CellInstId, Point<db::Coord>>,
        fixed_instances: &HashSet<N::CellInstId>,
        cell_outlines: &HashMap<N::CellId, db::Rect<db::Coord>>,
        net_weights: &HashMap<N::NetId, f64>,
    ) -> HashMap<N::CellInstId, Point<SInt>> {
        // Debug output.
        log::debug!("Running placer '{}'.", self.name());
        log::debug!("Number of cells: {}", netlist.num_child_instances(cell_id));
        log::debug!("Number of initial positions: {}", initial_positions.len());
        log::debug!("Number of fixed cells: {}", fixed_instances.len());

        // TODO: Sanity checks.

        // Assert that all fixed instances are given a position.
        let num_without_position = fixed_instances
            .iter()
            .filter(|i| !initial_positions.contains_key(i))
            .count();
        assert_eq!(
            num_without_position, 0,
            "Some fixed instances have no position."
        );

        log::info!("Run placement engine.");
        let positions = self.find_cell_positions_impl(
            netlist,
            cell_id,
            core_area,
            initial_positions,
            fixed_instances,
            cell_outlines,
            net_weights,
        );
        log::debug!("Placement engine done.");

        // Check that the positions actually lie in the core area.
        let core_area: SimplePolygon<i64> = core_area.cast(); // Convert to i64 to avoid overflows.
        let num_outliers = positions
            .iter()
            .filter(|(idx, p)| {
                !fixed_instances.contains(idx) && !core_area.contains_point(p.cast())
            })
            .count();
        if num_outliers > 0 {
            log::warn!(
                "{} cells were placed outside of the core area.",
                num_outliers
            );
        }

        positions
    }
}

/// Adapt a [`SimpleStdCellPlacer`] to expose the [`MixedSizePlacer`] API.
pub struct PlacerAdapter<P> {
    placer: P,
}

impl<P> PlacerAdapter<P> {
    /// Wrap a [`SimpleStdCellPlacer`] to expose the [`MixedSizePlacer`] API.
    pub fn new(simple_stcell_placer: P) -> Self {
        PlacerAdapter {
            placer: simple_stcell_placer,
        }
    }
}

impl<P: SimpleStdCellPlacer<C>, C: L2NBase<Coord = db::Coord>> MixedSizePlacer<C>
    for PlacerAdapter<P>
{
    fn name(&self) -> &str {
        self.placer.name()
    }

    fn find_cell_positions_impl(
        &self,
        placement_problem: &dyn PlacementProblem<C>,
    ) -> Result<HashMap<C::CellInstId, SimpleTransform<C::Coord>>, PlacementError> {
        // Extract arguments from `placement_problem` such that they can be passed to
        // `SimpleStdCellPlacer`.

        let netlist = placement_problem.fused_layout_netlist();
        let top_cell = placement_problem.top_cell();

        // Extract the core area.
        let core_area = {
            let placement_region = placement_problem.placement_region();
            if placement_region.len() != 1 {
                return Err(PlacementError::Other(format!(
                    "Expect exactly one polygon marking the placement region. Found {}.",
                    placement_region.len()
                )));
            }
            let placement_region = &placement_region[0];
            placement_region.to_simple_polygon()
        };

        let all_instances = netlist.each_cell_instance_vec(&top_cell);

        // Extract initial positions.
        let initial_positions = all_instances
            .iter()
            .map(|inst| {
                let pos = placement_problem.initial_position(inst);
                (inst.clone(), pos.transform_point(Point::zero()))
            })
            .collect();
        // Get set of fixed instances.
        let fixed_instances = placement_problem.get_fixed_instances();

        // Extract cell outlines.
        let cell_outlines = netlist
            .each_cell_dependency(&top_cell)
            .filter_map(|cell| {
                placement_problem
                    .cell_outline(&cell)
                    .map(|outline| (cell, outline))
            })
            .collect();

        // Fetch all net weights.
        let net_weights: HashMap<C::NetId, f64> = netlist
            .each_internal_net(&top_cell)
            .map(|net| (net.clone(), placement_problem.net_weight(&net)))
            .collect();

        // Call the placer.
        let displacements = self.placer.find_cell_positions(
            netlist,
            &top_cell,
            &core_area,
            &initial_positions,
            &fixed_instances,
            &cell_outlines,
            &net_weights,
        );

        // Convert point locations into transforms (translations).
        let transforms = displacements
            .into_iter()
            .map(|(inst, displacement)| (inst, SimpleTransform::translate(displacement)))
            .collect();

        Ok(transforms)
    }
}