Skip to content

Commit 3f04f1a

Browse files
better serialization
1 parent a6bfb38 commit 3f04f1a

File tree

9 files changed

+75
-22
lines changed

9 files changed

+75
-22
lines changed

lib/conversions/src/RLCToPython.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ class FunctionToPyFunction
242242
rewriter,
243243
getTypeConverter(),
244244
op.getUnmangledName(),
245-
op.getSymName(),
245+
op.getMangledName(),
246246
op.getArgNames(),
247247
op.getFunctionType());
248248
rewriter.eraseOp(op);
@@ -345,7 +345,7 @@ class ActionDeclToTNothing
345345
rewriter,
346346
getTypeConverter(),
347347
op.getUnmangledName(),
348-
mlir::rlc::mangledName(op.getSymName(), op.getFunctionType()),
348+
mlir::rlc::mangledName(op.getMangledName(), op.getFunctionType()),
349349
op.getArgNames(),
350350
op.getFunctionType());
351351

lib/dialect/src/Conversion.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ class FunctionRewriter
866866
realTypes.front().cast<mlir::LLVM::LLVMPointerType>().getElementType();
867867
rewriter.setInsertionPoint(op);
868868
auto newF = rewriter.create<mlir::LLVM::LLVMFuncOp>(
869-
op.getLoc(), op.getSymName(), fType, linkage);
869+
op.getLoc(), op.getMangledName(), fType, linkage);
870870
rewriter.cloneRegionBefore(
871871
op.getBody(), newF.getBody(), newF.getBody().begin());
872872
rewriter.eraseOp(op);
@@ -937,7 +937,7 @@ static mlir::LogicalResult flattenModule(mlir::ModuleOp op)
937937
{
938938
rewriter.setInsertionPoint(use->getOwner());
939939
auto ref = rewriter.create<mlir::rlc::Reference>(
940-
use->getOwner()->getLoc(), f.getFunctionType(), f.getSymName());
940+
use->getOwner()->getLoc(), f.getFunctionType(), f.getMangledName());
941941
use->set(ref);
942942
}
943943
}
@@ -947,13 +947,10 @@ static mlir::LogicalResult flattenModule(mlir::ModuleOp op)
947947
return res;
948948

949949
rewriter.setInsertionPoint(f);
950-
auto name = f.getName().str();
951-
f.setName("dc");
952950
auto newF = rewriter.create<mlir::rlc::FlatFunctionOp>(
953951
op.getLoc(),
954952
f.getFunctionType(),
955953
f.getUnmangledName(),
956-
rewriter.getStringAttr(name),
957954
f.getArgNamesAttr());
958955

959956
rewriter.cloneRegionBefore(

lib/dialect/src/Dialect.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,26 @@
44
#include "Dialect.inc"
55
#include "rlc/dialect/Types.hpp"
66

7+
class TypeAliasASMInterface: public mlir::OpAsmDialectInterface
8+
{
9+
public:
10+
using mlir::OpAsmDialectInterface::OpAsmDialectInterface;
11+
12+
AliasResult getAlias(mlir::Type type, llvm::raw_ostream& OS) const final
13+
{
14+
if (auto casted = type.dyn_cast<mlir::rlc::EntityType>())
15+
{
16+
OS << casted.getName();
17+
return AliasResult::FinalAlias;
18+
}
19+
20+
return AliasResult::NoAlias;
21+
}
22+
};
23+
724
void mlir::rlc::RLCDialect::initialize()
825
{
926
registerTypes();
1027
registerOperations();
28+
addInterfaces<TypeAliasASMInterface>();
1129
}

lib/dialect/src/Operations.td

+30-7
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ def RLC_DeclarationStatement : RLC_Dialect<"decl", [Symbol, DeclareOpInterfaceMe
175175

176176

177177
class RLC_FunctionBase<string mnemonic> :
178-
RLC_Dialect<mnemonic, [Symbol, CallableOpInterface, AutomaticAllocationScope, TypeCheckable]> {
178+
RLC_Dialect<mnemonic, [CallableOpInterface, AutomaticAllocationScope, TypeCheckable]> {
179179

180-
let arguments = (ins StrAttr:$unmangledName, SymbolNameAttr:$sym_name, StrArrayAttr:$argNames);
180+
let arguments = (ins StrAttr:$unmangledName, StrArrayAttr:$argNames);
181181
let description = [{
182182
expression.
183183
}];
@@ -191,17 +191,21 @@ RLC_Dialect<mnemonic, [Symbol, CallableOpInterface, AutomaticAllocationScope, Ty
191191

192192
let builders = [
193193
OpBuilder<(ins "StringRef":$name, "FunctionType":$type, "ArrayAttr":$argNames), [{
194-
build($_builder, $_state, type, name ,mangledName(name, type), argNames);
194+
build($_builder, $_state, type, name ,argNames);
195195
}]>];
196196

197197
let assemblyFormat = [{
198-
$unmangledName $argNames type($result) $sym_name $precondition $body attr-dict
198+
$unmangledName $argNames type($result) $precondition $body attr-dict
199199
}];
200-
let extraClassDeclaration = [{
200+
code extraBaseClassDeclaration = [{
201201
Region *getCallableRegion() {
202202
return isDeclaration() ? nullptr : &getBody();
203203
}
204204

205+
std::string getMangledName() {
206+
return rlc::mangledName(getUnmangledName(), getType());
207+
}
208+
205209
mlir::FunctionType getType() {
206210
return getResult().getType().cast<mlir::FunctionType>();
207211
}
@@ -228,7 +232,7 @@ RLC_Dialect<mnemonic, [Symbol, CallableOpInterface, AutomaticAllocationScope, Ty
228232
}
229233

230234
bool isInternal() {
231-
return getName().startswith("_");
235+
return getUnmangledName().startswith("_");
232236
}
233237

234238
bool isDeclaration() {
@@ -289,18 +293,37 @@ RLC_Dialect<mnemonic, [Symbol, CallableOpInterface, AutomaticAllocationScope, Ty
289293

290294
def RLC_FlatFunctionOp : RLC_FunctionBase<"flat_fun"> {
291295
let summary = "function.";
296+
let extraClassDeclaration = extraBaseClassDeclaration;
292297
}
293298

294299
def RLC_FunctionOp : RLC_FunctionBase<"fun"> {
295300
let summary = "function.";
301+
let extraClassDeclaration = extraBaseClassDeclaration;
296302
}
297303

298304
def RLC_ActionFunction : RLC_FunctionBase<"action_fun"> {
299305
let summary = "function.";
300306
let results = (outs FunctionType:$result, Variadic<FunctionType>:$actions);
301307
let assemblyFormat = [{
302-
$unmangledName $argNames type($result) type($actions) $sym_name $precondition $body attr-dict
308+
$unmangledName $argNames type($result) type($actions) $precondition $body attr-dict
303309
}];
310+
let extraClassDeclaration = extraBaseClassDeclaration # [{
311+
312+
size_t getSubActionsSize() {
313+
return getActions().size();
314+
}
315+
316+
mlir::FunctionType getSubActionType(size_t index) {
317+
return getActions().getTypes()[index].cast<mlir::FunctionType>();
318+
}
319+
320+
mlir::FunctionType getMainActionType() {
321+
return getResult().getType().cast<mlir::FunctionType>();
322+
}
323+
mlir::rlc::EntityType getEntityType() {
324+
return getMainActionType().getResult(0).cast<mlir::rlc::EntityType>();
325+
}
326+
}];
304327

305328
}
306329

lib/dialect/src/SymbolTable.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ mlir::rlc::ModuleBuilder::ModuleBuilder(mlir::ModuleOp op)
180180

181181
for (auto action : op.getOps<mlir::rlc::ActionFunction>())
182182
{
183-
auto type = types.getOne((action.getName() + "Entity").str());
183+
auto type = types.getOne((action.getUnmangledName() + "Entity").str());
184184
actionToActionType[action.getResult()] = type;
185185
actionTypeToAction[type] = action.getResult();
186186
values.add(

lib/dialect/src/TypeCheck.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static mlir::LogicalResult declareActionEntities(mlir::ModuleOp op)
5757
for (auto action : op.getOps<mlir::rlc::ActionFunction>())
5858
{
5959
auto type = mlir::rlc::EntityType::getIdentified(
60-
action.getContext(), (action.getName() + "Entity").str());
60+
action.getContext(), (action.getUnmangledName() + "Entity").str());
6161

6262
auto entity = rewriter.create<mlir::rlc::EntityDeclaration>(
6363
action.getLoc(),
@@ -154,7 +154,7 @@ static mlir::LogicalResult deduceFunctionTypes(mlir::ModuleOp op)
154154
builder.getConverter().convertType(fun.getFunctionType());
155155
if (deducedType == nullptr)
156156
{
157-
fun.emitRemark("in function declaration " + fun.getName());
157+
fun.emitRemark("in function declaration " + fun.getUnmangledName());
158158
return mlir::failure();
159159
}
160160
assert(deducedType.isa<mlir::FunctionType>());
@@ -510,7 +510,6 @@ static mlir::LogicalResult deduceActionTypes(mlir::ModuleOp op)
510510
actionType,
511511
generatedFunctions,
512512
fun.getUnmangledName(),
513-
fun.getSymName(),
514513
fun.getArgNames());
515514
newAction.getBody().takeBody(fun.getBody());
516515
newAction.getPrecondition().takeBody(fun.getPrecondition());

lib/parser/src/Parser.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,6 @@ Expected<mlir::rlc::FunctionOp> Parser::functionDefinition()
754754
location,
755755
mlir::FunctionType::get(ctx, argTypes, { retType }),
756756
builder.getStringAttr(nm),
757-
builder.getStringAttr(nm),
758757
builder.getStrArrayAttr(argName));
759758

760759
EXPECT(Token::Colons);
@@ -802,7 +801,6 @@ Expected<mlir::rlc::ActionFunction> Parser::actionDeclaration()
802801
location,
803802
mlir::FunctionType::get(ctx, argTypes, { retType }),
804803
builder.getStringAttr(nm),
805-
builder.getStringAttr(nm),
806804
builder.getStrArrayAttr(argName));
807805
}
808806

lib/parser/test/src/ParserTest.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ TEST(ParserTest, functionDefinition)
253253
if (!s)
254254
FAIL();
255255

256-
EXPECT_EQ(s->getName(), "a");
256+
EXPECT_EQ(s->getUnmangledName(), "a");
257257
EXPECT_EQ(s->getArgNames().size(), 0);
258258
}
259259

@@ -286,5 +286,5 @@ TEST(ParserTest, voidDeclaration)
286286
if (!s)
287287
FAIL();
288288

289-
EXPECT_EQ(s->getName(), "function");
289+
EXPECT_EQ(s->getUnmangledName(), "function");
290290
}

lib/python/src/Dialect.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,28 @@
55
#include "rlc/python/Operations.hpp"
66
#include "rlc/python/Types.hpp"
77

8+
class PythonTypeAliasASMInterface: public mlir::OpAsmDialectInterface
9+
{
10+
public:
11+
using mlir::OpAsmDialectInterface::OpAsmDialectInterface;
12+
13+
AliasResult getAlias(mlir::Type type, llvm::raw_ostream& OS) const final
14+
{
15+
if (auto casted = type.dyn_cast<mlir::rlc::python::CTypeStructType>())
16+
{
17+
OS << casted.getName();
18+
return AliasResult::FinalAlias;
19+
}
20+
21+
return AliasResult::NoAlias;
22+
}
23+
};
24+
825
void mlir::rlc::python::RLCPython::initialize()
926
{
1027
registerTypes();
1128
registerOperations();
29+
addInterfaces<PythonTypeAliasASMInterface>();
1230
}
1331

1432
namespace mlir::rlc::python

0 commit comments

Comments
 (0)