Skip to content
This repository was archived by the owner on Dec 12, 2024. It is now read-only.

Commit 68547d0

Browse files
author
Simon Camphausen
authored
Start label counting at 1 for every function (#111)
Before this labels were named based on a module wide counter.
1 parent f95a624 commit 68547d0

3 files changed

Lines changed: 57 additions & 6 deletions

File tree

‎include/emitc/Target/Cpp.h‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,19 @@ struct CppEmitter {
124124
/// RAII helper function to manage entering/exiting C++ scopes.
125125
struct Scope {
126126
Scope(CppEmitter &emitter)
127-
: mapperScope(emitter.valMapper), emitter(emitter) {
127+
: valMapperScope(emitter.valMapper),
128+
blockMapperScope(emitter.blockMapper), emitter(emitter) {
128129
emitter.valueInScopeCount.push(emitter.valueInScopeCount.top());
130+
emitter.labelInScopeCount.push(emitter.labelInScopeCount.top());
131+
}
132+
~Scope() {
133+
emitter.valueInScopeCount.pop();
134+
emitter.labelInScopeCount.pop();
129135
}
130-
~Scope() { emitter.valueInScopeCount.pop(); }
131136

132137
private:
133-
llvm::ScopedHashTableScope<Value, std::string> mapperScope;
138+
llvm::ScopedHashTableScope<Value, std::string> valMapperScope;
139+
llvm::ScopedHashTableScope<Block *, std::string> blockMapperScope;
134140
CppEmitter &emitter;
135141
};
136142

@@ -151,7 +157,7 @@ struct CppEmitter {
151157

152158
private:
153159
using ValMapper = llvm::ScopedHashTable<Value, std::string>;
154-
using BlockMapper = llvm::DenseMap<Block *, std::string>;
160+
using BlockMapper = llvm::ScopedHashTable<Block *, std::string>;
155161

156162
/// Output stream to emit to.
157163
raw_ostream &os;
@@ -171,6 +177,7 @@ struct CppEmitter {
171177
/// The number of values in the current scope. This is used to declare the
172178
/// names of values in a scope.
173179
std::stack<int64_t> valueInScopeCount;
180+
std::stack<int64_t> labelInScopeCount;
174181
};
175182

176183
/// Translates the given operation to C++ code. The operation or operations in

‎lib/Target/Cpp/TranslateToCpp.cpp‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,7 @@ CppEmitter::CppEmitter(raw_ostream &os, bool restrictToC,
534534
: os(os), restrictToC(restrictToC),
535535
forwardDeclareVariables(forwardDeclareVariables) {
536536
valueInScopeCount.push(0);
537+
labelInScopeCount.push(0);
537538
}
538539

539540
/// Return the existing or a new name for a Value.
@@ -546,8 +547,8 @@ StringRef CppEmitter::getOrCreateName(Value val) {
546547
/// Return the existing or a new name for a Block.
547548
StringRef CppEmitter::getOrCreateName(Block &block) {
548549
if (!blockMapper.count(&block))
549-
blockMapper.try_emplace(&block, formatv("label{0}", blockMapper.size()));
550-
return blockMapper[&block];
550+
blockMapper.insert(&block, formatv("label{0}", ++labelInScopeCount.top()));
551+
return *blockMapper.begin(&block);
551552
}
552553

553554
bool CppEmitter::mapToSigned(IntegerType::SignednessSemantics val) {

‎test/Target/control_flow.mlir‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,46 @@ func @simple(i64, i1) -> i64 {
7070
// CPP-FWDDECL-NEXT: [[BB4]]:
7171
// CPP-FWDDECL-NEXT: [[V0]] = add([[D]], [[E]]);
7272
// CPP-FWDDECL-NEXT: return [[V0]];
73+
74+
75+
func @block_labels0() {
76+
^bb1:
77+
br ^bb2
78+
^bb2:
79+
return
80+
}
81+
// C-FWDDECL: void block_labels0() {
82+
// C-FWDDECL-NEXT: label1:
83+
// C-FWDDECL-NEXT: goto label2;
84+
// C-FWDDECL-NEXT: label2:
85+
// C-FWDDECL-NEXT: return;
86+
// C-FWDDECL-NEXT: }
87+
88+
// CPP-FWDDECL: void block_labels0() {
89+
// CPP-FWDDECL-NEXT: label1:
90+
// CPP-FWDDECL-NEXT: goto label2;
91+
// CPP-FWDDECL-NEXT: label2:
92+
// CPP-FWDDECL-NEXT: return;
93+
// CPP-FWDDECL-NEXT: }
94+
95+
96+
// Repeat the same function to make sure the names of the block labels get reset.
97+
func @block_labels1() {
98+
^bb1:
99+
br ^bb2
100+
^bb2:
101+
return
102+
}
103+
// C-FWDDECL: void block_labels1() {
104+
// C-FWDDECL-NEXT: label1:
105+
// C-FWDDECL-NEXT: goto label2;
106+
// C-FWDDECL-NEXT: label2:
107+
// C-FWDDECL-NEXT: return;
108+
// C-FWDDECL-NEXT: }
109+
110+
// CPP-FWDDECL: void block_labels1() {
111+
// CPP-FWDDECL-NEXT: label1:
112+
// CPP-FWDDECL-NEXT: goto label2;
113+
// CPP-FWDDECL-NEXT: label2:
114+
// CPP-FWDDECL-NEXT: return;
115+
// CPP-FWDDECL-NEXT: }

0 commit comments

Comments
 (0)