Skip to content

Commit 7de1cda

Browse files
author
ZhangYan
committed
fallback memref transformation when alloc / dealloc not in FILO fashion
1 parent 9094fa6 commit 7de1cda

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

lib/gc/Transforms/MemRefToCPURuntime.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ struct ConvertMemRefToCPURuntime
7777
auto *ctx = &getContext();
7878
// Create a local set to store operations that should not be transformed.
7979
llvm::SmallSet<Operation *, 16> noTransformOps;
80+
llvm::SmallVector<Operation *, 16> allocStack;
8081

8182
// Walk through the module to find func::FuncOp instances.
8283
getOperation()->walk([&](func::FuncOp funcOp) {
@@ -96,6 +97,24 @@ struct ConvertMemRefToCPURuntime
9697
}
9798
}
9899
}
100+
} else if (isa<memref::AllocOp>(op)) {
101+
allocStack.push_back(op);
102+
} else if (isa<memref::DeallocOp>(op)) {
103+
// fallback alloc / dealloc not in FILO fashion
104+
Value deallocMemref = op->getOperands().front();
105+
Value topAllocMemref = allocStack.back()->getResults().front();
106+
if (deallocMemref == topAllocMemref) {
107+
allocStack.pop_back();
108+
} else {
109+
noTransformOps.insert(op);
110+
for (int i = allocStack.size() - 1; i >= 0; --i) {
111+
Operation *curAlloc = allocStack[i];
112+
if (deallocMemref == curAlloc->getResults().front()) {
113+
noTransformOps.insert(curAlloc);
114+
allocStack.erase(allocStack.begin() + i);
115+
}
116+
}
117+
}
99118
}
100119
});
101120
});

0 commit comments

Comments
 (0)