Skip to content

Commit 7ea54c8

Browse files
committed
SILPrintContext: support for printing consistent block numbers.
1 parent 5cb302e commit 7ea54c8

File tree

3 files changed

+192
-113
lines changed

3 files changed

+192
-113
lines changed

include/swift/SIL/SILBasicBlock.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class SILFunction;
2828
class SILArgument;
2929
class SILPHIArgument;
3030
class SILFunctionArgument;
31+
class SILPrintContext;
3132

3233
class SILBasicBlock :
3334
public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock> {
@@ -335,6 +336,9 @@ public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock> {
335336
/// Pretty-print the SILBasicBlock with the designated stream.
336337
void print(llvm::raw_ostream &OS) const;
337338

339+
/// Pretty-print the SILBasicBlock with the designated stream and context.
340+
void print(llvm::raw_ostream &OS, SILPrintContext &Ctx) const;
341+
338342
void printAsOperand(raw_ostream &OS, bool PrintType = true);
339343

340344
/// getSublistAccess() - returns pointer to member of instruction list

include/swift/SIL/SILPrintContext.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#ifndef SWIFT_SIL_PRINTCONTEXT_H
1414
#define SWIFT_SIL_PRINTCONTEXT_H
1515

16+
#include "swift/SIL/SILDebugScope.h"
17+
#include "swift/SIL/SILValue.h"
1618
#include "llvm/ADT/DenseMap.h"
1719
#include "llvm/Support/raw_ostream.h"
1820

@@ -21,10 +23,42 @@ namespace swift {
2123
class SILDebugScope;
2224
class SILInstruction;
2325
class SILFunction;
26+
class SILBasicBlock;
2427

2528
/// Used as context for the SIL print functions.
2629
class SILPrintContext {
30+
public:
31+
struct ID {
32+
enum ID_Kind { SILBasicBlock, SILUndef, SSAValue } Kind;
33+
unsigned Number;
34+
35+
// A stable ordering of ID objects.
36+
bool operator<(ID Other) const {
37+
if (unsigned(Kind) < unsigned(Other.Kind))
38+
return true;
39+
if (Number < Other.Number)
40+
return true;
41+
return false;
42+
}
43+
44+
void print(raw_ostream &OS);
45+
};
46+
2747
protected:
48+
// Cache block and value identifiers for this function. This is useful in
49+
// general for identifying entities, not just emitting textual SIL.
50+
//
51+
// TODO: It would be more discplined for the caller to provide a function
52+
// context. That way it would be impossible for IDs to change meaning within
53+
// the caller's scope.
54+
struct SILPrintFunctionContext {
55+
const SILFunction *F = nullptr;
56+
llvm::DenseMap<const SILBasicBlock *, unsigned> BlocksToIDMap;
57+
llvm::DenseMap<const ValueBase *, unsigned> ValueToIDMap;
58+
};
59+
60+
SILPrintFunctionContext FuncCtx;
61+
2862
llvm::raw_ostream &OutStream;
2963

3064
llvm::DenseMap<const SILDebugScope *, unsigned> ScopeToIDMap;
@@ -42,6 +76,11 @@ class SILPrintContext {
4276

4377
virtual ~SILPrintContext();
4478

79+
SILPrintFunctionContext &getFuncContext(const SILFunction *F);
80+
81+
// Initialized block IDs from the order provided in `blocks`.
82+
void initBlockIDs(ArrayRef<const SILBasicBlock *> Blocks);
83+
4584
/// Returns the output stream for printing.
4685
llvm::raw_ostream &OS() const { return OutStream; }
4786

@@ -51,6 +90,10 @@ class SILPrintContext {
5190
/// Returns true if verbose SIL should be printed.
5291
bool printVerbose() const { return Verbose; }
5392

93+
SILPrintContext::ID getID(const SILBasicBlock *Block);
94+
95+
SILPrintContext::ID getID(SILValue V);
96+
5497
/// Returns true if the \p Scope has and ID assigned.
5598
bool hasScopeID(const SILDebugScope *Scope) const {
5699
return ScopeToIDMap.count(Scope) != 0;
@@ -74,6 +117,8 @@ class SILPrintContext {
74117
virtual void printInstructionCallBack(const SILInstruction *I);
75118
};
76119

120+
raw_ostream &operator<<(raw_ostream &OS, SILPrintContext::ID i);
121+
77122
} // end namespace swift
78123

79124
#endif /* SWIFT_SIL_PRINTCONTEXT_H */

0 commit comments

Comments
 (0)