Skip to content

Commit 70093bc

Browse files
committed
[Heavy] Add variable name mapping to NbdlWriter
1 parent bcd75d8 commit 70093bc

File tree

1 file changed

+45
-29
lines changed

1 file changed

+45
-29
lines changed

heavy/lib/NbdlWriter.cpp

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
#include "llvm/Support/Casting.h"
14+
#include <llvm/ADT/Twine.h>
15+
#include <llvm/Support/Casting.h>
16+
#include <memory>
17+
#include <string>
1518

1619
namespace {
1720
class NbdlWriter {
1821
// Map mlir values to names within a function scope.
19-
using ValueMapTy = llvm::ScopedHashTable<mlir::Value, std::string>;
22+
// (FIXME The unique_ptr is probably unnecessary.)
23+
using ValueMapTy = llvm::ScopedHashTable<mlir::Value,
24+
std::unique_ptr<std::string>>;
2025
using Scope = typename ValueMapTy::ScopeTy;
2126

2227
ValueMapTy ValueMap;
@@ -53,19 +58,27 @@ class NbdlWriter {
5358
Err = Context.CreateError(Loc, Str, heavy::Value(Op));
5459
}
5560

56-
void setLocalVar(mlir::Value V, std::string&& Name) {
61+
llvm::StringRef GetLocalVar(mlir::Value V) {
62+
std::unique_ptr<std::string> name = ValueMap.lookup(V);
63+
return llvm::StringRef(*name);
64+
}
65+
66+
llvm::StringRef SetLocalVar(mlir::Value V, llvm::Twine Name)
5767
assert(ValueMap.count(Name) == 0 && "no shadowing variable names");
58-
ValueMap.insert(V, std::move(Name));
68+
auto NameStr = std::make_unique<std::string>(new std::string(Name.str()));
69+
auto NameStrRef = llvm::StringRef(*NameStr);
70+
ValueMap.insert(V, std::move(NameStr));
71+
return NameStrRef;
5972
}
6073

61-
void setLocalVar(mlir::Value) {
62-
// Create anonymous name.
63-
std::string AnonName = "anon_TODO";
64-
setLocalVar(mlir::Value, std::move(AnonName));
74+
llvm::StringRef SetLocalVar(mlir::Value V, llvm::StringRef Name,
75+
unsigned Num) {
76+
return SetLocalVar(V, llvm::Twine(Name, Num));
6577
}
6678

67-
void tetLocalVar(mlir::Value, llvm::StringRef Name) {
68-
// TODO
79+
llvm::StringRef SetLocalVar(mlir::Value V) {
80+
// Create anonymous name for variable.
81+
return SetLocalVar(V, "anon_", CurrentAnonVarCount++);
6982
}
7083

7184
void Visit(mlir::Operation* Op) {
@@ -170,40 +183,43 @@ class NbdlWriter {
170183
// Write the function name.
171184
OS << ' ' << Name << '(';
172185

186+
mlir::Region& BodyRegion = Op.getBody();
187+
if (BodyRegion.empty())
188+
return SetError("empty function body", Op);
189+
190+
Scope(ValueMap);
191+
mlir::Block* EntryBlock = BodyRegion.begin();
173192
// Write the parameter list.
174193
{
175194
unsigned I = 0;
176-
for (mlir::Type ParamType : FT.getInputs()) {
177-
VisitType(ParamType);
178-
// TODO handle LocalVarNames and add them to scope.
179-
OS << "arg_" << I;
195+
llvm::interleaveComma(EntryBlock.getArguments(), OS,
196+
[&](mlir::Operand const& Operand) {
197+
OS << SetLocalVar(Arg, "arg_", I)
198+
}, OS);
199+
for (mlir::BlockArgument Arg : EntryBlock.getArguments()) {
200+
VisitType(Arg.getType());
201+
OS << SetLocalVar(Arg, "arg_", I);
180202
++I;
181203
}
182204
}
183205

184206
OS << ") { ";
185207

186-
// Print the body.
187-
mlir::Region& BodyRegion = Op.getBody();
188-
if (BodyRegion.empty())
189-
return SetError("empty function body", Op);
190-
// Assume there is only one block.
191-
mlir::Block* Block = BodyRegion.begin();
192-
for (mlir::Operation* Operation : Block)
208+
// Print the body assuming a single block.
209+
for (mlir::Operation* Operation : EntryBlock)
193210
Visit(Operation);
194211

195212
OS << "}";
196213
}
197214

198215
Visit(GetOp Op) {
199-
llvm::StringRef ResultVarName = setLocalVar(Op.getResult());
200-
OS << "decltype(auto) " <<
201-
ResultVarName <<
202-
"nbdl::get(" <<
203-
getLocalVar(Op.getState()) <<
204-
", " <<
205-
getLocalVar(Op.getKey()) <<
206-
");";
216+
OS << "decltype(auto) "
217+
<< SetLocalVar(Op.getResult())
218+
<< "nbdl::get("
219+
<< GetLocalVar(Op.getState())
220+
<< ", "
221+
<< GetLocalVar(Op.getKey())
222+
<< ");";
207223
}
208224

209225
/************************************

0 commit comments

Comments
 (0)