llvm.org GIT mirror llvm / 2603664
MIR Serialization: Serialize the list of machine basic blocks with simple attributes. This commit implements the initial serialization of machine basic blocks in a machine function. Only the simple, scalar MBB attributes are serialized. The reference to LLVM IR's basic block is preserved when that basic block has a name. Reviewers: Duncan P. N. Exon Smith Differential Revision: http://reviews.llvm.org/D10465 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@240145 91177308-0d34-0410-b5e6-96231b3b80d8 Alex Lorenz 5 years ago
4 changed file(s) with 124 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
1919
2020 #include "llvm/ADT/StringRef.h"
2121 #include "llvm/Support/YAMLTraits.h"
22 #include
23
24 namespace llvm {
25 namespace yaml {
26
27 struct MachineBasicBlock {
28 std::string Name;
29 unsigned Alignment = 0;
30 bool IsLandingPad = false;
31 bool AddressTaken = false;
32 // TODO: Serialize the successors and liveins.
33 // TODO: Serialize machine instructions.
34 };
35
36 template <> struct MappingTraits {
37 static void mapping(IO &YamlIO, MachineBasicBlock &MBB) {
38 YamlIO.mapOptional("name", MBB.Name,
39 std::string()); // Don't print out an empty name.
40 YamlIO.mapOptional("alignment", MBB.Alignment);
41 YamlIO.mapOptional("isLandingPad", MBB.IsLandingPad);
42 YamlIO.mapOptional("addressTaken", MBB.AddressTaken);
43 }
44 };
45
46 } // end namespace yaml
47 } // end namespace llvm
48
49 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::MachineBasicBlock)
2250
2351 namespace llvm {
2452 namespace yaml {
2856 unsigned Alignment = 0;
2957 bool ExposesReturnsTwice = false;
3058 bool HasInlineAsm = false;
59
60 std::vector BasicBlocks;
3161 };
3262
3363 template <> struct MappingTraits {
3666 YamlIO.mapOptional("alignment", MF.Alignment);
3767 YamlIO.mapOptional("exposesReturnsTwice", MF.ExposesReturnsTwice);
3868 YamlIO.mapOptional("hasInlineAsm", MF.HasInlineAsm);
69 YamlIO.mapOptional("body", MF.BasicBlocks);
3970 }
4071 };
4172
1818 #include "llvm/AsmParser/Parser.h"
1919 #include "llvm/CodeGen/MachineFunction.h"
2020 #include "llvm/CodeGen/MIRYamlMapping.h"
21 #include "llvm/IR/BasicBlock.h"
2122 #include "llvm/IR/DiagnosticInfo.h"
2223 #include "llvm/IR/Instructions.h"
2324 #include "llvm/IR/LLVMContext.h"
2425 #include "llvm/IR/Module.h"
26 #include "llvm/IR/ValueSymbolTable.h"
2527 #include "llvm/Support/LineIterator.h"
2628 #include "llvm/Support/SMLoc.h"
2729 #include "llvm/Support/SourceMgr.h"
7274 ///
7375 /// Return true if error occurred.
7476 bool initializeMachineFunction(MachineFunction &MF);
77
78 /// Initialize the machine basic block using it's YAML representation.
79 ///
80 /// Return true if an error occurred.
81 bool initializeMachineBasicBlock(MachineBasicBlock &MBB,
82 const yaml::MachineBasicBlock &YamlMBB);
7583
7684 private:
7785 /// Return a MIR diagnostic converted from an LLVM assembly diagnostic.
197205 MF.setAlignment(YamlMF.Alignment);
198206 MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);
199207 MF.setHasInlineAsm(YamlMF.HasInlineAsm);
208 const auto &F = *MF.getFunction();
209 for (const auto &YamlMBB : YamlMF.BasicBlocks) {
210 const BasicBlock *BB = nullptr;
211 if (!YamlMBB.Name.empty()) {
212 BB = dyn_cast_or_null(
213 F.getValueSymbolTable().lookup(YamlMBB.Name));
214 // TODO: Report an error if a basic block isn't found.
215 }
216 auto *MBB = MF.CreateMachineBasicBlock(BB);
217 MF.insert(MF.end(), MBB);
218 if (initializeMachineBasicBlock(*MBB, YamlMBB))
219 return true;
220 }
221 return false;
222 }
223
224 bool MIRParserImpl::initializeMachineBasicBlock(
225 MachineBasicBlock &MBB, const yaml::MachineBasicBlock &YamlMBB) {
226 MBB.setAlignment(YamlMBB.Alignment);
227 if (YamlMBB.AddressTaken)
228 MBB.setHasAddressTaken();
229 MBB.setIsLandingPad(YamlMBB.IsLandingPad);
200230 return false;
201231 }
202232
1515 #include "llvm/ADT/STLExtras.h"
1616 #include "llvm/CodeGen/MachineFunction.h"
1717 #include "llvm/CodeGen/MIRYamlMapping.h"
18 #include "llvm/IR/BasicBlock.h"
1819 #include "llvm/IR/Module.h"
1920 #include "llvm/Support/MemoryBuffer.h"
2021 #include "llvm/Support/raw_ostream.h"
3334 MIRPrinter(raw_ostream &OS) : OS(OS) {}
3435
3536 void print(const MachineFunction &MF);
37
38 void convert(yaml::MachineBasicBlock &YamlMBB, const MachineBasicBlock &MBB);
3639 };
3740
3841 } // end anonymous namespace
6063 YamlMF.Alignment = MF.getAlignment();
6164 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
6265 YamlMF.HasInlineAsm = MF.hasInlineAsm();
66 for (const auto &MBB : MF) {
67 yaml::MachineBasicBlock YamlMBB;
68 convert(YamlMBB, MBB);
69 YamlMF.BasicBlocks.push_back(YamlMBB);
70 }
6371 yaml::Output Out(OS);
6472 Out << YamlMF;
73 }
74
75 void MIRPrinter::convert(yaml::MachineBasicBlock &YamlMBB,
76 const MachineBasicBlock &MBB) {
77 // TODO: Serialize unnamed BB references.
78 if (const auto *BB = MBB.getBasicBlock())
79 YamlMBB.Name = BB->hasName() ? BB->getName() : "";
80 else
81 YamlMBB.Name = "";
82 YamlMBB.Alignment = MBB.getAlignment();
83 YamlMBB.AddressTaken = MBB.hasAddressTaken();
84 YamlMBB.IsLandingPad = MBB.isLandingPad();
6585 }
6686
6787 void llvm::printMIR(raw_ostream &OS, const Module &M) {
0 # RUN: llc -start-after branch-folder -stop-after branch-folder -o /dev/null %s | FileCheck %s
1 # This test ensures that the MIR parser parses machine functions correctly.
2
3 --- |
4
5 define i32 @foo() {
6 entry:
7 ret i32 0
8 }
9
10 define i32 @bar() {
11 start:
12 ret i32 0
13 }
14
15 ...
16 ---
17 # CHECK: name: foo
18 # CHECK: body:
19 # CHECK-NEXT: - name: entry
20 # CHECK-NEXT: alignment: 0
21 # CHECK-NEXT: isLandingPad: false
22 # CHECK-NEXT: addressTaken: false
23 name: foo
24 body:
25 - name: entry
26 ...
27 ---
28 # CHECK: name: bar
29 # CHECK: body:
30 # CHECK-NEXT: - name: start
31 # CHECK-NEXT: alignment: 4
32 # CHECK-NEXT: isLandingPad: false
33 # CHECK-NEXT: addressTaken: false
34 # CHECK-NEXT: - alignment: 0
35 # CHECK-NEXT: isLandingPad: false
36 # CHECK-NEXT: addressTaken: true
37 name: bar
38 body:
39 - name: start
40 alignment: 4
41 - addressTaken: true
42 ...