-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Insert TBAA pointer tag for AlignedPointer and dealloc_helper Indices #1196
base: main
Are you sure you want to change the base?
Conversation
Hello. You may have forgotten to update the changelog!
|
@tzunghanjuang please also test this in your branch which fails and report back here :) Thanks! |
Failing tests in #1167: All the error messages are sth like this. freeing without malloc %5 = tail call ptr @_mlir_memref_to_llvm_alloc(i64 %.idx) #7
freeing without malloc %5 = tail call ptr @_mlir_memref_to_llvm_alloc(i64 %.idx) #7
freeing without malloc %147 = extractvalue { ptr, ptr, i64, [1 x i64], [1 x i64] } %6, 0
freeing without malloc %7 = tail call ptr @_mlir_memref_to_llvm_alloc(i64 72) #7
freeing without malloc %12 = tail call ptr @_mlir_memref_to_llvm_alloc(i64 72) #7
freeing without malloc %30 = tail call ptr @_mlir_memref_to_llvm_alloc(i64 72) #7
freeing without malloc %35 = tail call ptr @_mlir_memref_to_llvm_alloc(i64 72) #7
free(): double free detected in tcache 2
[1] 784711 IOT instruction (core dumped) python a_test/test_tensor_measure.py |
@tzunghanjuang , you are saying that even with this PR, these tests in your other PR are still failing? |
@erick-xanadu Yes. These still fails with this PR. |
// Index can be used as a pointer. | ||
if (isa<IndexType>(baseType) && (isa<LLVM::StoreOp>(newOp) || isa<LLVM::LoadOp>(newOp))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if we should always consider index as a pointer. Rather, I would walk backwards and if the index comes from an op that we know represents a pointer (like memref.extract_aligned_pointer
), we change the TBAA to pointer.
Always considering index to be a pointer sounds like it could produce a lot of errors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure if there is any way to memerize if a memref consider from memref.extract_aligned_pointer
. Maybe we need to modify the mlir side to add this information?
Edit: I added a helper method that recursively tracks prevNode until it encounters memref.extract_aligned_pointer
.
I think a prerequisite for merging this PR be that it solves the issue in your other PR. @tzunghanjuang |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #1196 +/- ##
==========================================
- Coverage 97.93% 97.93% -0.01%
==========================================
Files 76 77 +1
Lines 10893 10921 +28
Branches 970 971 +1
==========================================
+ Hits 10668 10695 +27
Misses 179 179
- Partials 46 47 +1 ☔ View full report in Codecov by Sentry. |
The double free issue might be related to this EnzymeAD/Enzyme#2029. |
Index
(with Load or Store)(isFromExtractAlignedPointerAsIndexOp(currentOp) || isInsideDeallocHelper(currentOp))) { | ||
tag = tree->getTag("any pointer"); | ||
} | ||
else { | ||
isInsideDeallocHelper(currentOp); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the idea behind the isInsideDeallocHelper
function? Is isFromExtractAlignedPointerAsIndexOp
not sufficient?
Also the second call to isInsideDeallocHelper
doesn't seem to do anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The deallocation pass inserts delloc_helper
function and it does not come with any ExtractAlignedPointerAsIndexOp
.
The index pointers (like %66
at the below) are fed to dealloc_helper (%21
). So the store and load inside dealloc_helper
should be added with the pointer tag.
%66 = call ptr @_mlir_memref_to_llvm_alloc(i64 ptrtoint (ptr getelementptr (i1, ptr null, i32 1) to i64))
call void @dealloc_helper(ptr %54, ptr %54, i64 0, i64 3, i64 1, ptr %56, ptr %56, i64 0, i64 1, i64 1, ptr %55, ptr %55, i64 0, i64 3, i64 1, ptr %65, ptr %65, i64 0, i64 3, i64 1, ptr %66, ptr %66, i64 0, i64 1, i64 1)
...
define void @dealloc_helper(ptr %0, ptr %1, i64 %2, i64 %3, i64 %4, ptr %5, ptr %6, i64 %7, i64 %8, i64 %9, ptr %10, ptr %11, i64 %12, i64 %13, i64 %14, ptr %15, ptr %16, i64 %17, i64 %18, i64 %19, ptr %20, ptr %21, i64 %22, i64 %23, i64 %24) {
...
45: ; preds = %42
%46 = getelementptr inbounds i1, ptr %21, i64 %43
store i1 false, ptr %46, align 1, !tbaa !6
%47 = add i64 %43, 1
br label %42
...
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this dealloc_helper
handles index pointers, so we should insert pointer tags inside it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, so actually we would need to follow all uses of the buffers (memrefs) where the index-typed pointers are stored into. Tricky indeed.
tag = tree->getTag("int"); | ||
// Index can be used as a pointer. | ||
if (isa<IndexType>(baseType) && | ||
(isFromExtractAlignedPointerAsIndexOp(currentOp) || isInsideDeallocHelper(currentOp))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you extract the logic isFromExtractAlignedPointerAsIndexOp(currentOp) || isInsideDeallocHelper(currentOp)
from set tags? You should be able to determine the type before the setting it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I have moved this logic out.
@@ -72,7 +108,7 @@ struct MemrefLoadTBAARewritePattern : public ConvertOpToLLVMPattern<memref::Load | |||
loadOp.getNontemporal()); | |||
|
|||
if (isAnyOf<IndexType, IntegerType, FloatType, MemRefType>(baseType)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can add the a function that check that is index and isFromExtractAlignedPointerAsIndexOp(currentOp) || isInsideDeallocHelper(currentOp)
then you return ptr or index and do not need to change the settag
Context:
Update the if-condition in TBAAPatterns so that it can insert pointer tag to
Index
. (Index
can be treated as Pointer.)We check if the index comes from
ExtractAlignedPointerAsIndexOp
or is located indealloc_helper
created by the deallocation pass. However, even with this fix, we still getdouble free
error related Enzyme (#1167 (comment)).Trace:
The following example has a integer tag (!tbaa !4) for Index. It should be changed to the pointer tag (!tbaa !6).
Otherwise, this error will be thrown.