Skip to content

Commit 6ea52da

Browse files
Refactor GenContext cleanup logic to use stack-allocated structures and improve memory management (#204)
1 parent d36c838 commit 6ea52da

2 files changed

Lines changed: 20 additions & 33 deletions

File tree

tslang/include/TypeScript/MLIRLogic/MLIRGenContext.h

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,12 @@ struct GenContext
7575
receiverFuncType = mlir::Type();
7676
}
7777

78-
// TODO: you are using "theModule.getBody()->clear();", do you need this hack anymore?
78+
// erases the IR collected during a dummy run (cleanup lists, dummy funcOp) and detaches from it;
79+
// the lists, passResult and state are owned by the stack frame that set them, not by GenContext
7980
void clean()
8081
{
8182
if (cleanUps)
82-
{
83+
{
8384
for (auto op : *cleanUps)
8485
{
8586
op->dropAllDefinedValueUses();
@@ -88,7 +89,7 @@ struct GenContext
8889
op->erase();
8990
}
9091

91-
delete cleanUps;
92+
cleanUps->clear();
9293
cleanUps = nullptr;
9394
}
9495

@@ -102,30 +103,16 @@ struct GenContext
102103
op->erase();
103104
}
104105

105-
delete cleanUpOps;
106-
cleanUpOps = nullptr;
106+
cleanUpOps->clear();
107+
cleanUpOps = nullptr;
107108
}
108109

109-
if (passResult)
110-
{
111-
delete passResult;
112-
passResult = nullptr;
113-
}
114-
115-
cleanState();
110+
passResult = nullptr;
111+
state = nullptr;
116112

117113
cleanFuncOp();
118114
}
119115

120-
void cleanState()
121-
{
122-
if (state)
123-
{
124-
delete state;
125-
state = nullptr;
126-
}
127-
}
128-
129116
void cleanFuncOp()
130117
{
131118
if (funcOp)

tslang/lib/TypeScript/MLIRGen.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -764,9 +764,6 @@ class MLIRGenImpl
764764
genContextPartial.dummyRun = true;
765765
genContextPartial.rootContext = &genContextPartial;
766766
genContextPartial.postponedMessages = &postponedMessages;
767-
// TODO: no need to clean up here as whole module will be removed
768-
//genContextPartial.cleanUps = new mlir::SmallVector<mlir::Block *>();
769-
//genContextPartial.cleanUpOps = new mlir::SmallVector<mlir::Operation *>();
770767

771768
for (auto includeFile : includeFiles)
772769
{
@@ -781,8 +778,6 @@ class MLIRGenImpl
781778

782779
auto notResolved = processStatements(module->statements, genContextPartial);
783780

784-
genContextPartial.clean();
785-
786781
// clean up: erase only the ops this discovery pass added, preserving any content that
787782
// was already generated before a nested discovery (see preExistingOps above).
788783
clearTempModule();
@@ -5557,16 +5552,22 @@ class MLIRGenImpl
55575552
llvm::ScopedHashTableScope<StringRef, VariableDeclarationDOM::TypePtr>
55585553
fullNameGlobalsMapScope(fullNameGlobalsMap);
55595554

5555+
// owned here; GenContext borrows pointers to them (see GenContext::clean)
5556+
SmallVector<mlir::Block *> cleanUpsList;
5557+
SmallVector<mlir::Operation *> cleanUpOpsList;
5558+
PassResult passResultData;
5559+
int discoverState = 1;
5560+
55605561
GenContext genContextWithPassResult{};
55615562
genContextWithPassResult.funcOp = dummyFuncOp;
55625563
genContextWithPassResult.thisType = genContext.thisType;
55635564
genContextWithPassResult.thisClassType = genContext.thisClassType;
55645565
genContextWithPassResult.allowPartialResolve = true;
55655566
genContextWithPassResult.dummyRun = true;
5566-
genContextWithPassResult.cleanUps = new SmallVector<mlir::Block *>();
5567-
genContextWithPassResult.cleanUpOps = new SmallVector<mlir::Operation *>();
5568-
genContextWithPassResult.passResult = new PassResult();
5569-
genContextWithPassResult.state = new int(1);
5567+
genContextWithPassResult.cleanUps = &cleanUpsList;
5568+
genContextWithPassResult.cleanUpOps = &cleanUpOpsList;
5569+
genContextWithPassResult.passResult = &passResultData;
5570+
genContextWithPassResult.state = &discoverState;
55705571
genContextWithPassResult.allocateVarsInContextThis =
55715572
(functionLikeDeclarationBaseAST->internalFlags & InternalFlags::VarsInObjectContext) ==
55725573
InternalFlags::VarsInObjectContext;
@@ -6091,7 +6092,8 @@ class MLIRGenImpl
60916092
auto funcGenContext = GenContext(funcDeclGenContext);
60926093
funcGenContext.clearScopeVars();
60936094
funcGenContext.funcOp = funcOp;
6094-
funcGenContext.state = new int(1);
6095+
int funcState = 1;
6096+
funcGenContext.state = &funcState;
60956097
// if funcGenContext.passResult is null and allocateVarsInContextThis is true, this type should contain fully
60966098
// defined object with local variables as fields
60976099
funcGenContext.allocateVarsInContextThis =
@@ -6124,8 +6126,6 @@ class MLIRGenImpl
61246126
functionLikeDeclarationBaseAST, funcProto->getNameWithoutNamespace(), funcOp, funcProto, funcGenContext);
61256127
}
61266128

6127-
funcGenContext.cleanState();
6128-
61296129
if (mlir::failed(resultFromBody))
61306130
{
61316131
return {mlir::failure(), funcOp, "", false};

0 commit comments

Comments
 (0)