-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathMemoryDependentAnalyzer.cpp
More file actions
221 lines (189 loc) · 6.33 KB
/
MemoryDependentAnalyzer.cpp
File metadata and controls
221 lines (189 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include "PTO/Transforms/InsertSync/MemoryDependentAnalyzer.h"
#include "PTO/Transforms/InsertSync/InsertSyncDebug.h"
#include "mlir/Interfaces/ViewLikeInterface.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "llvm/Support/Debug.h"
#define DEBUG_TYPE "pto-inject-sync"
using namespace mlir;
using namespace mlir::pto;
static bool isTraceEnabled() {
return isInsertSyncDebugEnabled(InsertSyncDebugLevel::Trace);
}
// [Debug] 打印 Value 详细信息
static void printValueDebug(const char* tag, Value v) {
if (!isTraceEnabled())
return;
llvm::errs() << tag << ": ";
if (!v) {
llvm::errs() << "NULL\n";
return;
}
if (auto *op = v.getDefiningOp()) {
llvm::errs() << "OpResult defined by [" << op->getName() << "]";
} else {
llvm::errs() << "BlockArgument";
}
llvm::errs() << " | Type: " << v.getType() << "\n";
}
// [Fix & Debug] 增强版 GetRealRoot
static Value GetRealRoot(Value v) {
const bool trace = isTraceEnabled();
if (trace) {
llvm::errs() << " [Trace] GetRealRoot Start:\n";
printValueDebug(" Current", v);
}
int depth = 0;
const int maxDepth = 20;
while (v && depth++ < maxDepth) {
Operation *defOp = v.getDefiningOp();
if (!defOp) {
if (trace)
llvm::errs() << " -> Reached BlockArgument. Stop.\n";
break;
}
if (auto op = dyn_cast<memref::CollapseShapeOp>(defOp)) {
if (trace)
llvm::errs() << " -> Hit CollapseShapeOp. Peel off.\n";
v = op.getSrc();
continue;
}
if (auto op = dyn_cast<memref::ExpandShapeOp>(defOp)) {
if (trace)
llvm::errs() << " -> Hit ExpandShapeOp. Peel off.\n";
v = op.getSrc();
continue;
}
if (auto op = dyn_cast<memref::ViewOp>(defOp)) {
if (trace)
llvm::errs() << " -> Hit ViewOp. Peel off.\n";
v = op.getSource();
continue;
}
if (auto view = dyn_cast<ViewLikeOpInterface>(defOp)) {
if (trace)
llvm::errs() << " -> Hit ViewLikeInterface. Peel off.\n";
v = view.getViewSource();
continue;
}
if (auto cast = dyn_cast<memref::CastOp>(defOp)) {
v = cast.getSource();
continue;
}
if (auto reCast = dyn_cast<memref::ReinterpretCastOp>(defOp)) {
v = reCast.getSource();
continue;
}
if (trace) {
llvm::errs() << " -> Hit Alloc/Other [" << defOp->getName()
<< "]. Stop.\n";
}
break;
}
return v;
}
bool MemoryDependentAnalyzer::DepBetween(
const SmallVector<const BaseMemInfo *> &a,
const SmallVector<const BaseMemInfo *> &b,
DepBaseMemInfoPairVec &depBaseMemInfosVec) {
// [Debug Log] 关键入口信息
if (isTraceEnabled()) {
llvm::errs() << "\n[DepBetween] Checking dependency...\n";
llvm::errs() << " Vec A Size: " << a.size() << "\n";
llvm::errs() << " Vec B Size: " << b.size() << "\n";
}
bool hasAlias = false;
for (auto &i : a) {
for (auto &j : b) {
if (MemAlias(i, j)) {
depBaseMemInfosVec.push_back(std::make_pair(i, j));
hasAlias = true;
}
}
}
return hasAlias;
}
bool MemoryDependentAnalyzer::MemAlias(const BaseMemInfo *a,
const BaseMemInfo *b) {
pto::AddressSpace as = a->scope;
pto::AddressSpace bs = b->scope;
// [Debug Log] 打印比较对象
if (isTraceEnabled()) {
llvm::errs() << " [MemAlias Check]\n";
printValueDebug(" Root A", a->rootBuffer);
printValueDebug(" Root B", b->rootBuffer);
llvm::errs() << " Scope A: " << (int)as << ", Scope B: " << (int)bs
<< "\n";
}
if (as != bs) {
if (isTraceEnabled())
llvm::errs() << " -> Scope Mismatch. False.\n";
return false;
}
// 1. GM 内存
if (as == pto::AddressSpace::GM) {
return isGMBufferOverlap(a, b);
}
// 2. Local Memory (UB/L1)
if (a->rootBuffer == b->rootBuffer) {
if (a->unknownRange || b->unknownRange) return true;
if (a->baseAddresses.empty() || b->baseAddresses.empty()) return true;
if (a->allocateSize == 0 || b->allocateSize == 0) return true;
return isBufferAddressRangeOverlap(a, b);
}
// 2.2 深层比较:穿透 View
Value realRootA = GetRealRoot(a->rootBuffer);
Value realRootB = GetRealRoot(b->rootBuffer);
if (isTraceEnabled()) {
llvm::errs() << " [Deep Check] Surface Roots differ. Digging deeper...\n";
printValueDebug(" Real Root A", realRootA);
printValueDebug(" Real Root B", realRootB);
}
if (realRootA == realRootB && realRootA != nullptr) {
if (isTraceEnabled())
llvm::errs() << " -> MATCH! Real roots are the same.\n";
return true;
} else {
if (isTraceEnabled())
llvm::errs() << " -> Mismatch. Real roots differ.\n";
}
return false;
}
bool MemoryDependentAnalyzer::isGMBufferOverlap(const BaseMemInfo *a,
const BaseMemInfo *b) {
if (a->rootBuffer != b->rootBuffer) {
Value realRootA = GetRealRoot(a->rootBuffer);
Value realRootB = GetRealRoot(b->rootBuffer);
if (realRootA != realRootB) {
return false;
}
return true;
}
if (a->unknownRange || b->unknownRange) return true;
if (a->baseAddresses.empty() || b->baseAddresses.empty()) return true;
if (a->allocateSize == 0 || b->allocateSize == 0) return true;
return isBufferAddressRangeOverlap(a, b);
}
bool MemoryDependentAnalyzer::isBufferAddressRangeOverlap(
const BaseMemInfo *a, const BaseMemInfo *b) {
int aBaseAddressesSize = static_cast<int>(a->baseAddresses.size());
int bBaseAddressesSize = static_cast<int>(b->baseAddresses.size());
for (int i = 0; i < aBaseAddressesSize; i++) {
for (int j = 0; j < bBaseAddressesSize; j++) {
if (isBufferOverlap(a, b, i, j)) {
return true;
}
}
}
return false;
}
bool MemoryDependentAnalyzer::isBufferOverlap(const BaseMemInfo *a,
const BaseMemInfo *b, int aIndex,
int bIndex) {
uint64_t aStart = a->baseAddresses[aIndex];
uint64_t bStart = b->baseAddresses[bIndex];
uint64_t aEnd = aStart + a->allocateSize;
uint64_t bEnd = bStart + b->allocateSize;
uint64_t maxStart = std::max(aStart, bStart);
uint64_t minEnd = std::min(aEnd, bEnd);
return maxStart < minEnd;
}