|
11 | 11 | // |
12 | 12 | //===----------------------------------------------------------------------===// |
13 | 13 |
|
14 | | -#include "llvm/Support/Casting.h" |
| 14 | +#include <llvm/ADT/Twine.h> |
| 15 | +#include <llvm/Support/Casting.h> |
| 16 | +#include <memory> |
| 17 | +#include <string> |
15 | 18 |
|
16 | 19 | namespace { |
17 | 20 | class NbdlWriter { |
18 | 21 | // 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>>; |
20 | 25 | using Scope = typename ValueMapTy::ScopeTy; |
21 | 26 |
|
22 | 27 | ValueMapTy ValueMap; |
@@ -53,19 +58,27 @@ class NbdlWriter { |
53 | 58 | Err = Context.CreateError(Loc, Str, heavy::Value(Op)); |
54 | 59 | } |
55 | 60 |
|
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) |
57 | 67 | 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; |
59 | 72 | } |
60 | 73 |
|
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)); |
65 | 77 | } |
66 | 78 |
|
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++); |
69 | 82 | } |
70 | 83 |
|
71 | 84 | void Visit(mlir::Operation* Op) { |
@@ -170,40 +183,43 @@ class NbdlWriter { |
170 | 183 | // Write the function name. |
171 | 184 | OS << ' ' << Name << '('; |
172 | 185 |
|
| 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(); |
173 | 192 | // Write the parameter list. |
174 | 193 | { |
175 | 194 | 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); |
180 | 202 | ++I; |
181 | 203 | } |
182 | 204 | } |
183 | 205 |
|
184 | 206 | OS << ") { "; |
185 | 207 |
|
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) |
193 | 210 | Visit(Operation); |
194 | 211 |
|
195 | 212 | OS << "}"; |
196 | 213 | } |
197 | 214 |
|
198 | 215 | 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 | + << ");"; |
207 | 223 | } |
208 | 224 |
|
209 | 225 | /************************************ |
|
0 commit comments