MIR Serialization: Change MIR syntax - use custom syntax for MBBs.
This commit modifies the way the machine basic blocks are serialized - now the
machine basic blocks are serialized using a custom syntax instead of relying on
YAML primitives. Instead of using YAML mappings to represent the individual
machine basic blocks in a machine function's body, the new syntax uses a single
YAML block scalar which contains all of the machine basic blocks and
instructions for that function.
This is an example of a function's body that uses the old syntax:
body:
- id: 0
name: entry
instructions:
- '%eax = MOV32r0 implicit-def %eflags'
- 'RETQ %eax'
...
The same body is now written like this:
body: |
bb.0.entry:
%eax = MOV32r0 implicit-def %eflags
RETQ %eax
...
This syntax change is motivated by the fact that the bundled machine
instructions didn't map that well to the old syntax which was using a single
YAML sequence to store all of the machine instructions in a block. The bundled
machine instructions internally use flags like BundledPred and BundledSucc to
determine the bundles, and serializing them as MI flags using the old syntax
would have had a negative impact on the readability and the ease of editing
for MIR files. The new syntax allows me to serialize the bundled machine
instructions using a block construct without relying on the internal flags,
for example:
BUNDLE implicit-def dead %itstate, implicit-def %s1 ... {
t2IT 1, 24, implicit-def %itstate
%s1 = VMOVS killed %s0, 1, killed %cpsr, implicit killed %itstate
}
This commit also converts the MIR testcases to the new syntax. I developed
a script that can convert from the old syntax to the new one. I will post the
script on the llvm-commits mailing list in the thread for this commit.
llvm-svn: 244982
diff --git a/llvm/lib/CodeGen/MIRParser/MIRParser.cpp b/llvm/lib/CodeGen/MIRParser/MIRParser.cpp
index a9cbbc3..a0279fa 100644
--- a/llvm/lib/CodeGen/MIRParser/MIRParser.cpp
+++ b/llvm/lib/CodeGen/MIRParser/MIRParser.cpp
@@ -96,13 +96,6 @@
/// Return true if error occurred.
bool initializeMachineFunction(MachineFunction &MF);
- /// Initialize the machine basic block using it's YAML representation.
- ///
- /// Return true if an error occurred.
- bool initializeMachineBasicBlock(MachineFunction &MF, MachineBasicBlock &MBB,
- const yaml::MachineBasicBlock &YamlMBB,
- const PerFunctionMIParsingState &PFS);
-
bool initializeRegisterInfo(MachineFunction &MF,
const yaml::MachineFunction &YamlMF,
PerFunctionMIParsingState &PFS);
@@ -294,36 +287,15 @@
return true;
}
- const auto &F = *MF.getFunction();
- for (const auto &YamlMBB : YamlMF.BasicBlocks) {
- const BasicBlock *BB = nullptr;
- const yaml::StringValue &Name = YamlMBB.Name;
- const yaml::StringValue &IRBlock = YamlMBB.IRBlock;
- if (!Name.Value.empty()) {
- BB = dyn_cast_or_null<BasicBlock>(
- F.getValueSymbolTable().lookup(Name.Value));
- if (!BB)
- return error(Name.SourceRange.Start,
- Twine("basic block '") + Name.Value +
- "' is not defined in the function '" + MF.getName() +
- "'");
- }
- if (!IRBlock.Value.empty()) {
- // TODO: Report an error when both name and ir block are specified.
- SMDiagnostic Error;
- if (parseIRBlockReference(BB, SM, MF, IRBlock.Value, PFS, IRSlots, Error))
- return error(Error, IRBlock.SourceRange);
- }
- auto *MBB = MF.CreateMachineBasicBlock(BB);
- MF.insert(MF.end(), MBB);
- bool WasInserted =
- PFS.MBBSlots.insert(std::make_pair(YamlMBB.ID, MBB)).second;
- if (!WasInserted)
- return error(Twine("redefinition of machine basic block with id #") +
- Twine(YamlMBB.ID));
+ SMDiagnostic Error;
+ if (parseMachineBasicBlockDefinitions(MF, YamlMF.Body.Value.Value, PFS,
+ IRSlots, Error)) {
+ reportDiagnostic(
+ diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
+ return true;
}
- if (YamlMF.BasicBlocks.empty())
+ if (MF.empty())
return error(Twine("machine function '") + Twine(MF.getName()) +
"' requires at least one machine basic block in its body");
// Initialize the frame information after creating all the MBBs so that the
@@ -335,13 +307,13 @@
if (!YamlMF.JumpTableInfo.Entries.empty() &&
initializeJumpTableInfo(MF, YamlMF.JumpTableInfo, PFS))
return true;
- // Initialize the machine basic blocks after creating them all so that the
- // machine instructions parser can resolve the MBB references.
- unsigned I = 0;
- for (const auto &YamlMBB : YamlMF.BasicBlocks) {
- if (initializeMachineBasicBlock(MF, *MF.getBlockNumbered(I++), YamlMBB,
- PFS))
- return true;
+ // Parse the machine instructions after creating all of the MBBs so that the
+ // parser can resolve the MBB references.
+ if (parseMachineInstructions(MF, YamlMF.Body.Value.Value, PFS, IRSlots,
+ Error)) {
+ reportDiagnostic(
+ diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
+ return true;
}
inferRegisterInfo(MF, YamlMF);
// FIXME: This is a temporary workaround until the reserved registers can be
@@ -351,53 +323,6 @@
return false;
}
-bool MIRParserImpl::initializeMachineBasicBlock(
- MachineFunction &MF, MachineBasicBlock &MBB,
- const yaml::MachineBasicBlock &YamlMBB,
- const PerFunctionMIParsingState &PFS) {
- MBB.setAlignment(YamlMBB.Alignment);
- if (YamlMBB.AddressTaken)
- MBB.setHasAddressTaken();
- MBB.setIsLandingPad(YamlMBB.IsLandingPad);
- SMDiagnostic Error;
- // Parse the successors.
- const auto &Weights = YamlMBB.SuccessorWeights;
- bool HasWeights = !Weights.empty();
- if (HasWeights && Weights.size() != YamlMBB.Successors.size()) {
- bool IsFew = Weights.size() < YamlMBB.Successors.size();
- return error(IsFew ? Weights.back().SourceRange.End
- : Weights[YamlMBB.Successors.size()].SourceRange.Start,
- Twine("too ") + (IsFew ? "few" : "many") +
- " successor weights, expected " +
- Twine(YamlMBB.Successors.size()) + ", have " +
- Twine(Weights.size()));
- }
- size_t SuccessorIndex = 0;
- for (const auto &MBBSource : YamlMBB.Successors) {
- MachineBasicBlock *SuccMBB = nullptr;
- if (parseMBBReference(SuccMBB, MBBSource, MF, PFS))
- return true;
- // TODO: Report an error when adding the same successor more than once.
- MBB.addSuccessor(SuccMBB, HasWeights ? Weights[SuccessorIndex++].Value : 0);
- }
- // Parse the liveins.
- for (const auto &LiveInSource : YamlMBB.LiveIns) {
- unsigned Reg = 0;
- if (parseNamedRegisterReference(Reg, SM, MF, LiveInSource.Value, PFS,
- IRSlots, Error))
- return error(Error, LiveInSource.SourceRange);
- MBB.addLiveIn(Reg);
- }
- // Parse the instructions.
- for (const auto &MISource : YamlMBB.Instructions) {
- MachineInstr *MI = nullptr;
- if (parseMachineInstr(MI, SM, MF, MISource.Value, PFS, IRSlots, Error))
- return error(Error, MISource.SourceRange);
- MBB.insert(MBB.end(), MI);
- }
- return false;
-}
-
bool MIRParserImpl::initializeRegisterInfo(MachineFunction &MF,
const yaml::MachineFunction &YamlMF,
PerFunctionMIParsingState &PFS) {