aboutsummaryrefslogtreecommitdiff
path: root/backend/lib/Tooling/BackendDriver.cpp
blob: 59323354085a8e9f679ecf369cf0faa73e86c533 (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
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
// SPDX-License-Identifier: GPL-3.0-or-later

#include "rcc/Tooling/BackendDriver.h"

#include "rcc/CodeGen/MIRPrinter.h"
#include "rcc/CodeGen/MachineFunction.h"
#include "rcc/Target/TargetMachine.h"

#include <cstddef>
#include <cstdint>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

namespace rcc::tooling {
namespace {

[[nodiscard]] BackendStage outputStage(ArtifactKind Output) noexcept {
  switch (Output) {
  case ArtifactKind::PreLegalizerMIR:
    return BackendStage::PreLegalizerMIR;
  case ArtifactKind::LegalizedMIR:
    return BackendStage::LegalizedMIR;
  case ArtifactKind::RegisterBankMIR:
    return BackendStage::RegisterBankMIR;
  case ArtifactKind::SelectedMIR:
    return BackendStage::SelectedMIR;
  case ArtifactKind::PostSSAMIR:
    return BackendStage::PostSSAMIR;
  case ArtifactKind::PostRegisterAllocationMIR:
    return BackendStage::PostRegisterAllocationMIR;
  case ArtifactKind::FinalMIR:
  case ArtifactKind::Assembly:
  case ArtifactKind::RelocatableObject:
    return BackendStage::FinalMIR;
  }
  return BackendStage::FinalMIR;
}

[[nodiscard]] Expected<std::vector<std::uint8_t>> printMIR(const mir::MIRDocument &Document,
                                                           const TargetDescription &Target,
                                                           const BackendInvocation &Invocation,
                                                           bool Simplify) {
  mir::MIRDocumentPrinterOptions Options;
  Options.Mode = Simplify ? mir::MIPrintMode::Readable : mir::MIPrintMode::Complete;
  Options.EmitEmbeddedIR = true;
  Options.DataLayout = Invocation.DataLayout;
  auto Printed = mir::printMIRDocument(Document, Target, std::move(Options));
  if (!Printed)
    return std::unexpected(Printed.error());
  return std::vector<std::uint8_t>(Printed->begin(), Printed->end());
}

} // namespace

std::string_view BackendRunResult::text() const noexcept {
  if (Kind == ArtifactKind::RelocatableObject || Output.empty())
    return {};
  return {reinterpret_cast<const char *>(Output.data()), Output.size()};
}

Expected<BackendRunResult> runBackend(CanonicalMIR Input, const BackendInvocation &Invocation,
                                      ArtifactKind Output, PipelineControls Controls,
                                      MachinePassInstrumentation *Instrumentation) {
  auto TargetMachine = createTargetMachine(Invocation);
  if (!TargetMachine)
    return std::unexpected(TargetMachine.error());
  const TargetDescription &Target = (*TargetMachine)->targetDescription();
  if (Input.Document.unit().functionCount() == 0) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "backend input must contain at least one machine function");
  }

  BackendStage InitialStage = BackendStage::FinalMIR;
  for (std::size_t Index = 0; Index < Input.Document.unit().functionCount(); ++Index) {
    const MachineFunction *Function = Input.Document.unit().functionAt(Index);
    if (Function == nullptr)
      return makeUnexpected(ErrorCode::InternalError, "backend input contains a null function");
    const BackendStage Stage = stageForFunction(*Function);
    if (Index == 0 || isEarlierStage(Stage, InitialStage))
      InitialStage = Stage;
  }
  const BackendStage RequiredStage = outputStage(Output);
  if (isMIRArtifact(Output) && isEarlierStage(RequiredStage, InitialStage)) {
    return makeUnexpected(ErrorCode::InvalidInvocation,
                          "backend cannot reconstruct an earlier MIR stage from a later snapshot");
  }

  if (isMIRArtifact(Output)) {
    if (Controls.StopAtStage && *Controls.StopAtStage != RequiredStage) {
      return makeUnexpected(ErrorCode::InvalidInvocation,
                            "output stage conflicts with the explicit pipeline stop stage");
    }
    Controls.StopAtStage = RequiredStage;
  }

  const bool SimplifyMIR = Controls.SimplifyMIR;

  MachinePassInstrumentation LocalInstrumentation;
  MachinePassInstrumentation *ActiveInstrumentation =
      Instrumentation == nullptr ? &LocalInstrumentation : Instrumentation;
  auto Pipeline = runTargetPipeline(Input.Document, **TargetMachine, Invocation,
                                    std::move(Controls), ActiveInstrumentation);
  if (!Pipeline)
    return std::unexpected(Pipeline.error());

  std::vector<std::uint8_t> Bytes;
  if (isMIRArtifact(Output)) {
    auto Printed = printMIR(Input.Document, Target, Invocation, SimplifyMIR);
    if (!Printed)
      return std::unexpected(Printed.error());
    Bytes = std::move(*Printed);
  } else if (Output == ArtifactKind::Assembly) {
    auto Assembly = (*TargetMachine)->emitAssembly(Input.Document.unit());
    if (!Assembly)
      return std::unexpected(Assembly.error());
    Bytes.assign(Assembly->begin(), Assembly->end());
  } else {
    auto Object = (*TargetMachine)->emitObject(Input.Document.unit());
    if (!Object)
      return std::unexpected(Object.error());
    Bytes = std::move(*Object);
  }

  return BackendRunResult{
      .Kind = Output,
      .Output = std::move(Bytes),
      .Pipeline = std::move(*Pipeline),
      .Actions = ActiveInstrumentation->actionEvents(),
  };
}

} // namespace rcc::tooling