Skip to content

Commit 67acad7

Browse files
authored
Merge pull request swiftlang#41118 from ktoso/wip-sil-bb-names
[SIL] Allow giving debug names to SILBasicBlocks
2 parents 377ca35 + d7ba6c6 commit 67acad7

File tree

6 files changed

+47
-0
lines changed

6 files changed

+47
-0
lines changed

include/swift/SIL/SILBasicBlock.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ public SwiftObjectHeader {
107107
/// debug output.
108108
int getDebugID() const;
109109

110+
void setDebugName(llvm::StringRef name);
111+
Optional<llvm::StringRef> getDebugName() const;
112+
110113
SILFunction *getParent() { return Parent; }
111114
const SILFunction *getParent() const { return Parent; }
112115

include/swift/SIL/SILModule.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ class SILModule {
163163
llvm::ilist<SILDifferentiabilityWitness>;
164164
using CoverageMapCollectionType =
165165
llvm::MapVector<StringRef, SILCoverageMap *>;
166+
using BasicBlockNameMapType =
167+
llvm::DenseMap<const SILBasicBlock *, std::string>;
166168

167169
enum class LinkingMode : uint8_t {
168170
/// Link functions with non-public linkage. Used by the mandatory pipeline.
@@ -360,6 +362,10 @@ class SILModule {
360362
/// Action to be executed for serializing the SILModule.
361363
ActionCallback SerializeSILAction;
362364

365+
#if NDEBUG
366+
BasicBlockNameMapType basicBlockNames;
367+
#endif
368+
363369
SILModule(llvm::PointerUnion<FileUnit *, ModuleDecl *> context,
364370
Lowering::TypeConverter &TC, const SILOptions &Options);
365371

@@ -447,6 +453,23 @@ class SILModule {
447453
void setSerialized() { serialized = true; }
448454
bool isSerialized() const { return serialized; }
449455

456+
void setBasicBlockName(const SILBasicBlock *block, StringRef name) {
457+
#if NDEBUG
458+
basicBlockNames[block] = name.str();
459+
#endif
460+
}
461+
Optional<StringRef> getBasicBlockName(const SILBasicBlock *block) {
462+
#if NDEBUG
463+
auto Known = basicBlockNames.find(block);
464+
if (Known == basicBlockNames.end())
465+
return None;
466+
467+
return StringRef(Known->second);
468+
#else
469+
return None;
470+
#endif
471+
}
472+
450473
/// Serialize a SIL module using the configured SerializeSILAction.
451474
void serialize();
452475

lib/SIL/IR/SILBasicBlock.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ int SILBasicBlock::getDebugID() const {
6767
llvm_unreachable("block not in function's block list");
6868
}
6969

70+
void SILBasicBlock::setDebugName(llvm::StringRef name) {
71+
getModule().setBasicBlockName(this, name);
72+
}
73+
74+
Optional<llvm::StringRef> SILBasicBlock::getDebugName() const {
75+
return getModule().getBasicBlockName(this);
76+
}
77+
7078
SILModule &SILBasicBlock::getModule() const {
7179
return getParent()->getModule();
7280
}

lib/SIL/IR/SILPrinter.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,12 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
791791
// the block header.
792792
printBlockArgumentUses(BB);
793793

794+
// If the basic block has a name available, print it as well
795+
auto debugName = BB->getDebugName();
796+
if (debugName.hasValue()) {
797+
*this << "// " << debugName.getValue() << '\n';
798+
}
799+
794800
// Then print the name of our block, the arguments, and the block colon.
795801
*this << Ctx.getID(BB);
796802
printBlockArguments(BB);

lib/SILGen/SILGenFunction.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,7 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
818818
/// are created by different emissions; it's just a little
819819
/// counter-intuitive within a single emission.)
820820
SILBasicBlock *createBasicBlock();
821+
SILBasicBlock *createBasicBlock(llvm::StringRef debugName);
821822
SILBasicBlock *createBasicBlockAfter(SILBasicBlock *afterBB);
822823
SILBasicBlock *createBasicBlockBefore(SILBasicBlock *beforeBB);
823824

lib/SILGen/SILGenStmt.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ SILBasicBlock *SILGenFunction::createBasicBlock() {
5858
}
5959
}
6060

61+
SILBasicBlock *SILGenFunction::createBasicBlock(llvm::StringRef debugName) {
62+
auto block = createBasicBlock();
63+
block->setDebugName(debugName);
64+
return block;
65+
}
66+
6167
SILBasicBlock *SILGenFunction::createBasicBlock(FunctionSection section) {
6268
switch (section) {
6369
case FunctionSection::Ordinary: {

0 commit comments

Comments
 (0)