Skip to content

Commit 71e675b

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

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

lib/gc/Transforms/MemRefToCPURuntime.cpp

Lines changed: 21 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,26 @@ 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+
auto aliases = analysis.resolveReverse(deallocMemref);
106+
Value topAllocMemref = allocStack.back()->getResults().front();
107+
if (aliases.find(topAllocMemref) != aliases.end()) {
108+
allocStack.pop_back();
109+
} else {
110+
noTransformOps.insert(op);
111+
for (int i = allocStack.size() - 1; i >= 0; --i) {
112+
Operation *curAlloc = allocStack[i];
113+
if (aliases.find(curAlloc->getResults().front()) !=
114+
aliases.end()) {
115+
noTransformOps.insert(curAlloc);
116+
allocStack.erase(allocStack.begin() + i);
117+
}
118+
}
119+
}
99120
}
100121
});
101122
});

0 commit comments

Comments
 (0)