aboutsummaryrefslogtreecommitdiff
path: root/backend/lib/MIR/MIRDocumentParser.cpp
blob: 08627d5264b067347564bcb50774805c2b563b67 (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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// SPDX-License-Identifier: GPL-3.0-or-later

#include "MIRDocumentParser.h"

#include "MIRDocumentReferenceValidation.h"
#include "MIRMetadataTextParser.h"
#include "MIRParserUtilities.h"
#include "rcc/CodeGen/MachineBasicBlock.h"
#include "rcc/CodeGen/MachineConstantPool.h"
#include "rcc/CodeGen/MachineFrameInfo.h"
#include "rcc/CodeGen/MachineFunction.h"
#include "rcc/CodeGen/MachineFunctionAttributes.h"
#include "rcc/CodeGen/MachineFunctionProperties.h"
#include "rcc/CodeGen/MachineFunctionRecords.h"
#include "rcc/CodeGen/MachineInstr.h"
#include "rcc/CodeGen/MachineJumpTableInfo.h"
#include "rcc/CodeGen/MachineMetadata.h"
#include "rcc/CodeGen/MachineRegisterInfo.h"
#include "rcc/CodeGen/MachineVerifier.h"
#include "rcc/CodeGen/TargetDescription.h"
#include "rcc/CodeGen/TargetMIRTextInfo.h"
#include "rcc/CodeGen/TargetRegisterInfo.h"
#include "rcc/MIR/LLVMConstantSyntax.h"

#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <initializer_list>
#include <limits>
#include <optional>
#include <ranges>
#include <span>
#include <string>
#include <string_view>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

namespace rcc::mir {

using parser_detail::parseBool;
using parser_detail::parsePrefixedID;
using parser_detail::parseSigned;
using parser_detail::parseUnsigned;
using parser_detail::parseUnsigned32;
using parser_detail::trim;

bool MIRDocumentParser::fail(const TextRange &Range, std::string Message, std::string Category) {
  if (Result.Diagnostics.empty()) {
    Result.Diagnostics.push_back(TextDiagnostic{
        .Severity = DiagnosticSeverity::Error,
        .Category = std::move(Category),
        .Message = std::move(Message),
        .SourceName = std::string(Result.Source.name()),
        .Range = Range,
    });
  }
  return true;
}

bool MIRDocumentParser::importError(const TextRange &Range, const Error &Failure,
                                    std::string Category) {
  return fail(Range, std::string(Failure.message()), std::move(Category));
}

const yaml::Node *MIRDocumentParser::mappingValue(const yaml::Node &Mapping,
                                                  std::string_view Key) const noexcept {
  const auto Value = Mapping.mappingValue(Key);
  return Value ? node(*Value) : nullptr;
}

std::string_view MIRDocumentParser::effectiveDataLayout() const noexcept {
  if (!Options.DataLayout.empty())
    return Options.DataLayout;
  if (ActiveDocument != nullptr) {
    if (const EmbeddedIRReferenceIndex *Embedded = ActiveDocument->embeddedIR()) {
      if (const auto DataLayout = Embedded->dataLayout())
        return *DataLayout;
    }
  }
  return {};
}

LLVMConstantSyntaxOptions MIRDocumentParser::pointerLayoutOptions() const noexcept {
  return {
      .DataLayout = effectiveDataLayout(),
      .NamedTypes = {},
  };
}

bool MIRDocumentParser::requireKind(const yaml::Node &Node, yaml::NodeKind Kind,
                                    std::string_view Description) {
  if (Node.kind() == Kind)
    return false;
  return fail(Node.range(), std::string(Description) + " has the wrong YAML node kind");
}

bool MIRDocumentParser::validateKeys(const yaml::Node &Mapping,
                                     std::span<const std::string_view> Keys,
                                     std::string_view Description) {
  for (const yaml::MappingEntry &Entry : Mapping.mapping()) {
    if (std::ranges::find(Keys, Entry.Key) == Keys.end()) {
      return fail(Entry.KeyRange,
                  "unknown " + std::string(Description) + " key '" + Entry.Key + "'");
    }
  }
  return false;
}

bool MIRDocumentParser::scalar(const yaml::Node &Node, std::string_view &Value,
                               std::string_view Description) {
  if (requireKind(Node, yaml::NodeKind::Scalar, Description))
    return true;
  Value = Node.scalar();
  return false;
}

bool MIRDocumentParser::boolean(const yaml::Node &Node, bool &Value) {
  std::string_view Text;
  if (scalar(Node, Text, "boolean") || !parseBool(Text, Value))
    return fail(Node.range(), "expected 'true' or 'false'");
  return false;
}

bool MIRDocumentParser::unsignedValue(const yaml::Node &Node, std::uint64_t &Value,
                                      std::string_view Description) {
  std::string_view Text;
  if (scalar(Node, Text, Description) || !parseUnsigned(Text, Value))
    return fail(Node.range(), "expected " + std::string(Description));
  return false;
}

bool MIRDocumentParser::signedValue(const yaml::Node &Node, std::int64_t &Value,
                                    std::string_view Description) {
  std::string_view Text;
  if (scalar(Node, Text, Description) || !parseSigned(Text, Value))
    return fail(Node.range(), "expected " + std::string(Description));
  return false;
}

bool MIRDocumentParser::alignment(const yaml::Node &Node, Alignment &Value, bool AllowZero) {
  std::uint64_t Bytes = 0;
  if (unsignedValue(Node, Bytes, "an unsigned alignment"))
    return true;
  if (Bytes == 0 && AllowZero) {
    Value = Alignment{};
    return false;
  }
  auto Parsed = Alignment::create(Bytes);
  if (!Parsed)
    return importError(Node.range(), Parsed.error());
  Value = *Parsed;
  return false;
}

bool MIRDocumentParser::physicalRegister(std::string_view Text, const TextRange &Range,
                                         PhysicalRegister &Reg) {
  Text = trim(Text);
  if (!Text.starts_with('$') || Text.size() == 1)
    return fail(Range, "physical register must use the '$name' MIR spelling");
  const PhysicalRegisterDesc *Description = Target.registers().physicalRegister(Text.substr(1));
  if (Description == nullptr)
    return fail(Range, "unknown physical register '" + std::string(Text) + "'");
  Reg = Description->Reg;
  return false;
}

bool MIRDocumentParser::physicalRegister(const yaml::Node &Node, PhysicalRegister &Reg) {
  std::string_view Text;
  return scalar(Node, Text, "physical register") || physicalRegister(Text, Node.range(), Reg);
}

bool MIRDocumentParser::virtualRegister(std::string_view Text, const TextRange &Range,
                                        MachineFunction &Function, MIParserContext &Context,
                                        Register &Reg) {
  const auto ID = parsePrefixedID(Text, "%");
  if (!ID)
    return fail(Range, "virtual register must use the '%N' MIR spelling");
  auto Found =
      std::ranges::find_if(Context.VirtualRegisters,
                           [ID](const MIVirtualRegisterSlot &Slot) { return Slot.TextID == *ID; });
  if (Found == Context.VirtualRegisters.end()) {
    auto Created = Function.registerInfo().createVirtualRegister();
    if (!Created)
      return importError(Range, Created.error());
    Context.VirtualRegisters.push_back({*ID, *Created});
    Reg = *Created;
    return false;
  }
  Reg = Found->Reg;
  return false;
}

MIRDocumentParseResult MIRDocumentParser::run() {
  yaml::ParseResult YAMLResult = yaml::parse(std::string(Result.Source.name()),
                                             std::string(Result.Source.text()), Options.YAML);
  if (!YAMLResult.succeeded()) {
    const auto Diagnostics = YAMLResult.diagnostics();
    Result.Diagnostics.assign(Diagnostics.begin(), Diagnostics.end());
    return std::move(Result);
  }
  Stream = YAMLResult.stream();
  if (Stream == nullptr || Stream->documents().empty()) {
    static_cast<void>(fail(Result.Source.range(0, 0), "MIR stream contains no YAML documents"));
    return std::move(Result);
  }

  MIRDocument Staging;
  ActiveDocument = &Staging;
  const auto IsEmptyDocument = [this](std::size_t Index) {
    const yaml::Node *Root = node(Stream->documents()[Index].Root);
    return Root != nullptr && Root->kind() == yaml::NodeKind::Null &&
           Root->range().Begin.Offset == Root->range().End.Offset;
  };
  std::size_t FirstContent = 0;
  while (FirstContent < Stream->documents().size() && IsEmptyDocument(FirstContent))
    ++FirstContent;
  if (FirstContent == Stream->documents().size()) {
    static_cast<void>(fail(Stream->documents().front().Range,
                           "MIR stream must contain at least one machine function document"));
    return std::move(Result);
  }

  std::size_t FirstFunction = FirstContent;
  const yaml::Node *FirstRoot = node(Stream->documents()[FirstContent].Root);
  if (FirstRoot != nullptr && FirstRoot->kind() == yaml::NodeKind::Scalar) {
    EmbeddedIRIndexResult Embedded = indexEmbeddedIR(
        std::string(Result.Source.name()) + ":embedded-ir", std::string(FirstRoot->scalar()));
    if (!Embedded.succeeded()) {
      const auto Diagnostics = Embedded.diagnostics();
      if (Diagnostics.empty()) {
        static_cast<void>(
            fail(FirstRoot->range(), "embedded LLVM IR indexing failed", "mir-embedded-ir"));
      } else {
        const TextDiagnostic &Diagnostic = Diagnostics.front();
        const TextRange Range = scalarDiagnosticRange(*FirstRoot, Diagnostic.Range);
        static_cast<void>(fail(Range, Diagnostic.Message, Diagnostic.Category));
      }
      return std::move(Result);
    }
    Staging.EmbeddedIR = std::move(Embedded).takeIndex();
    FirstFunction = FirstContent + 1;
  }

  if (!effectiveDataLayout().empty()) {
    auto PointerSize = llvmPointerSizeInBits(0, pointerLayoutOptions());
    if (!PointerSize) {
      static_cast<void>(importError(Stream->documents()[FirstContent].Range, PointerSize.error(),
                                    "mir-data-layout"));
      return std::move(Result);
    }
  }

  bool SawFunction = false;
  for (std::size_t Index = FirstFunction; Index < Stream->documents().size(); ++Index) {
    if (IsEmptyDocument(Index))
      continue;
    const yaml::Node *Root = node(Stream->documents()[Index].Root);
    if (Root == nullptr || requireKind(*Root, yaml::NodeKind::Mapping, "machine function") ||
        parseFunction(*Root, Staging)) {
      return std::move(Result);
    }
    SawFunction = true;
  }
  if (!SawFunction) {
    static_cast<void>(fail(Stream->documents()[FirstContent].Range,
                           "MIR stream must contain at least one machine function document"));
    return std::move(Result);
  }

  auto UnitResult = Staging.Unit.verify();
  if (!UnitResult) {
    static_cast<void>(
        fail(Stream->documents().back().Range, "parsed compilation unit failed verification: " +
                                                   std::string(UnitResult.error().message())));
    return std::move(Result);
  }
  ActiveDocument = nullptr;
  Result.Document = std::move(Staging);
  return std::move(Result);
}

bool MIRDocumentParser::parseFunction(const yaml::Node &Root, MIRDocument &Document) {
  constexpr std::array Keys{
      std::string_view{"name"},
      std::string_view{"alignment"},
      std::string_view{"exposesReturnsTwice"},
      std::string_view{"legalized"},
      std::string_view{"regBankSelected"},
      std::string_view{"selected"},
      std::string_view{"failedISel"},
      std::string_view{"tracksRegLiveness"},
      std::string_view{"hasWinCFI"},
      std::string_view{"noPhis"},
      std::string_view{"isSSA"},
      std::string_view{"noVRegs"},
      std::string_view{"hasFakeUses"},
      std::string_view{"callsEHReturn"},
      std::string_view{"callsUnwindInit"},
      std::string_view{"hasEHContTarget"},
      std::string_view{"hasEHScopes"},
      std::string_view{"hasEHFunclets"},
      std::string_view{"isOutlined"},
      std::string_view{"debugInstrRef"},
      std::string_view{"failsVerification"},
      std::string_view{"tracksDebugUserValues"},
      std::string_view{"registers"},
      std::string_view{"liveins"},
      std::string_view{"calleeSavedRegisters"},
      std::string_view{"frameInfo"},
      std::string_view{"fixedStack"},
      std::string_view{"stack"},
      std::string_view{"entry_values"},
      std::string_view{"callSites"},
      std::string_view{"debugValueSubstitutions"},
      std::string_view{"constants"},
      std::string_view{"machineFunctionInfo"},
      std::string_view{"jumpTable"},
      std::string_view{"machineMetadataNodes"},
      std::string_view{"calledGlobals"},
      std::string_view{"prefetch-targets"},
      std::string_view{"body"},
  };
  const yaml::Node *NameNode = mappingValue(Root, "name");
  if (NameNode == nullptr)
    return fail(Root.range(), "missing required key 'name'");
  if (validateKeys(Root, Keys, "machine function"))
    return true;
  std::string_view Name;
  if (scalar(*NameNode, Name, "machine function name"))
    return true;

  const EmbeddedIRFunction *IRFunction = nullptr;
  if (Document.EmbeddedIR) {
    if (Name.empty()) {
      std::size_t AnonymousOrdinal = 0;
      for (std::size_t Index = 0; Index < Document.Unit.functionCount(); ++Index) {
        const MachineFunction *Existing = Document.Unit.functionAt(Index);
        if (Existing != nullptr && Existing->name().empty())
          ++AnonymousOrdinal;
      }
      IRFunction = Document.EmbeddedIR->unnamedFunction(AnonymousOrdinal);
    } else {
      IRFunction = Document.EmbeddedIR->function(Name);
    }
    if (IRFunction == nullptr)
      return fail(NameNode->range(),
                  "function '" + std::string(Name) + "' isn't defined in the provided LLVM IR");
  }

  if (!Name.empty() && Document.Unit.function(Name) != nullptr)
    return fail(NameNode->range(), "redefinition of machine function '" + std::string(Name) + "'");

  auto Created = Document.Unit.createMachineFunction(std::string(Name));
  if (!Created)
    return importError(NameNode->range(), Created.error());
  MachineFunction &Function = **Created;
  ActiveFunction = &Function;

  MIRFunctionDocumentInfo DocumentInfo{
      .Function = Function.stableID(),
      .NoPHIs = std::nullopt,
      .IsSSA = std::nullopt,
      .NoVRegs = std::nullopt,
      .HasFakeUses = std::nullopt,
  };
  MIParserContext Context;
  if (IRFunction != nullptr) {
    Context.HasIRBlock = [IRFunction](std::string_view BlockName) {
      return IRFunction->block(BlockName) != nullptr;
    };
  }

  std::vector<PendingJumpTable> PendingJumpTables;
  if (parseFunctionScalars(Root, Function, DocumentInfo) ||
      parseMetadata(Root, Function, Context) || parseRegisters(Root, Function, Context) ||
      parseStackObjects(Root, Function, Context) || parseConstants(Root, Function, Context) ||
      reserveJumpTables(Root, Function, Context, PendingJumpTables) ||
      parseBody(Root, Function, Context) || defineJumpTables(Function, PendingJumpTables) ||
      parseFrameState(Root, Function, Context) ||
      recomputeProperties(Root, Function, DocumentInfo) || parseTargetState(Root, Function) ||
      parseFunctionRecords(Root, Function) || validateDocumentReferences(Function)) {
    return true;
  }

  if (Options.VerifyMachineFunctions) {
    auto Verified = MachineVerifier::verify(Function, Target);
    if (!Verified)
      return importError(Root.range(), Verified.error(), "mir-verifier");
  }
  Document.FunctionInfo.push_back(DocumentInfo);
  ActiveFunction = nullptr;
  return false;
}

} // namespace rcc::mir