aboutsummaryrefslogtreecommitdiff
path: root/backend/include/rcc/CodeGen/PBQP/Solution.h
blob: 512eb570d4aba3856152b5db2c16851f10174e15 (plain)
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
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef RCC_CODEGEN_PBQP_SOLUTION_H
#define RCC_CODEGEN_PBQP_SOLUTION_H

#include "rcc/CodeGen/PBQP/Graph.h"
#include "rcc/Support/Error.h"

#include <cstddef>
#include <limits>
#include <optional>
#include <span>
#include <vector>

namespace rcc::pbqp {

class Solution final {
public:
  explicit Solution(std::size_t NodeCount)
      : Selections(NodeCount, std::numeric_limits<std::size_t>::max()) {}

  [[nodiscard]] Expected<void> setSelection(NodeID ID, std::size_t Selection) {
    if (!ID.isValid() || ID.value() >= Selections.size())
      return makeUnexpected(ErrorCode::InvalidArgument, "PBQP solution received an unknown node");
    Selections[ID.value()] = Selection;
    return {};
  }

  [[nodiscard]] std::optional<std::size_t> selection(NodeID ID) const noexcept {
    if (!ID.isValid() || ID.value() >= Selections.size() ||
        Selections[ID.value()] == std::numeric_limits<std::size_t>::max())
      return std::nullopt;
    return Selections[ID.value()];
  }

  [[nodiscard]] std::span<const std::size_t> selections() const noexcept {
    return Selections;
  }
  [[nodiscard]] Cost totalCost() const noexcept {
    return TotalCost;
  }
  void setTotalCost(Cost Value) noexcept {
    TotalCost = Value;
  }

  [[nodiscard]] Expected<void> verify(const Graph &Problem) const {
    if (Selections.size() != Problem.nodeCount())
      return makeUnexpected(ErrorCode::VerificationError, "PBQP solution has the wrong node count");
    Cost Total = 0;
    for (const Node &NodeValue : Problem.nodes()) {
      const auto Selection = selection(NodeValue.ID);
      if (!Selection)
        return makeUnexpected(ErrorCode::VerificationError,
                              "PBQP solution omits a valid node selection");
      const std::size_t SelectedOption = Selection.value();
      if (SelectedOption >= NodeValue.Costs.size())
        return makeUnexpected(ErrorCode::VerificationError,
                              "PBQP solution selects an invalid node option");
      Total = addCosts(Total, NodeValue.Costs[SelectedOption]);
    }
    for (const Edge &EdgeValue : Problem.edges()) {
      const auto FirstSelection = selection(EdgeValue.First);
      const auto SecondSelection = selection(EdgeValue.Second);
      if (!FirstSelection || !SecondSelection) {
        return makeUnexpected(ErrorCode::VerificationError,
                              "PBQP solution omits an edge endpoint selection");
      }
      Total = addCosts(Total, EdgeValue.Costs.at(FirstSelection.value(), SecondSelection.value()));
    }
    if (isInfinite(Total))
      return makeUnexpected(ErrorCode::RegisterAllocationFailed,
                            "PBQP problem has no finite solution");
    if (Total != TotalCost)
      return makeUnexpected(ErrorCode::VerificationError, "PBQP solution cost is inconsistent");
    return {};
  }

private:
  std::vector<std::size_t> Selections;
  Cost TotalCost = 0;
};

} // namespace rcc::pbqp

#endif // RCC_CODEGEN_PBQP_SOLUTION_H