-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
[TIR] Fix segfaults from ordering of Let/Assert in MakePackedAPI #16543
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -155,6 +155,11 @@ void ArgBinder::BindDLTensor(const Buffer& buffer, const PrimExpr& device_type, | |
const DataType tvm_shape_type = DataType::ShapeIndex(); | ||
const DataType tvm_ndim_type = DataType::Int(32); | ||
const Stmt nop = Evaluate(0); | ||
|
||
init_nest_.emplace_back(AssertStmt( | ||
!Call(DataType::Bool(), builtin::isnullptr(), {handle}), | ||
tvm::tir::StringImm(arg_name + " is expected to have non-NULL DLTensor* pointer"), nop)); | ||
|
||
// dimension checks | ||
PrimExpr v_ndim = TVMArrayGet(tvm_ndim_type, handle, builtin::kArrNDim); | ||
|
||
|
@@ -173,7 +178,7 @@ void ArgBinder::BindDLTensor(const Buffer& buffer, const PrimExpr& device_type, | |
std::ostringstream ndim_err_msg; | ||
ndim_err_msg << arg_name << ".ndim is expected to equal " << buffer->shape.size(); | ||
auto msg = tvm::tir::StringImm(ndim_err_msg.str()); | ||
asserts_.emplace_back(AssertStmt(a_ndim == v_ndim, msg, nop)); | ||
init_nest_.emplace_back(AssertStmt(a_ndim == v_ndim, msg, nop)); | ||
// type checks | ||
std::ostringstream type_err_msg; | ||
type_err_msg << arg_name << ".dtype is expected to be " << buffer->dtype; | ||
|
@@ -186,18 +191,8 @@ void ArgBinder::BindDLTensor(const Buffer& buffer, const PrimExpr& device_type, | |
if (!(buffer->dtype == DataType::Int(1) || buffer->dtype == DataType::Int(4) || | ||
buffer->dtype == DataType::UInt(4))) { | ||
auto type_msg = tvm::tir::StringImm(type_err_msg.str()); | ||
asserts_.emplace_back(AssertStmt(a_ndim == v_ndim, msg, nop)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was this just a duplicate? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup. The buffer's dimensionality is checked earlier, so this is entirely a duplicate check on the dimensionality. |
||
asserts_.emplace_back(AssertStmt(cond, type_msg, nop)); | ||
} | ||
// data field | ||
if (Bind_(buffer->data, TVMArrayGet(DataType::Handle(), handle, builtin::kArrData), | ||
arg_name + ".data", true)) { | ||
Var vptr(buffer->data); | ||
def_handle_dtype_.Set(vptr, tir::TypeAnnotation(buffer->dtype)); | ||
// mark alignment of external bufs | ||
init_nest_.emplace_back(AttrStmt(vptr, tir::attr::storage_alignment, | ||
IntImm(DataType::Int(32), buffer->data_alignment), nop)); | ||
} | ||
|
||
// shape field | ||
Buffer buf_shape = decl_buffer({IntImm(DataType::Int(32), buffer->shape.size())}, tvm_shape_type, | ||
|
@@ -243,7 +238,7 @@ void ArgBinder::BindDLTensor(const Buffer& buffer, const PrimExpr& device_type, | |
foldl([](PrimExpr a, PrimExpr b, Span span) { return logical_and(a, b, span); }, | ||
const_true(1), conds), | ||
stride_msg, Evaluate(0)); | ||
check = IfThenElse(Not(v_strides_is_null), check, Stmt()); | ||
check = IfThenElse(Not(v_strides_is_null), check); | ||
asserts_.emplace_back(SeqStmt({check, Evaluate(0)})); | ||
} | ||
} else if (buffer->buffer_type == kAutoBroadcast) { | ||
|
@@ -300,6 +295,33 @@ void ArgBinder::BindDLTensor(const Buffer& buffer, const PrimExpr& device_type, | |
arg_name + ".device_type", true); | ||
Bind_(device_id, TVMArrayGet(DataType::Int(32), handle, builtin::kArrDeviceId), | ||
arg_name + ".device_id", true); | ||
|
||
// Data field. Because the validation of the data field may depend | ||
// on a dynamic size defined by the other DLTensor* parameters, this | ||
// field must be generated last. | ||
if (Bind_(buffer->data, TVMArrayGet(DataType::Handle(), handle, builtin::kArrData), | ||
arg_name + ".data", true)) { | ||
Var vptr(buffer->data); | ||
|
||
// Check if the data pointer is NULL. This check is skipped for | ||
// size-0 arrays, since CUDA provides a NULL pointer for size-zero | ||
// allocations. | ||
auto alloc_size = [&]() -> PrimExpr { | ||
PrimExpr product = IntImm(buffer->DefaultIndexType(), 1); | ||
for (const auto& dim : buffer->shape) { | ||
product *= dim; | ||
} | ||
return product; | ||
}(); | ||
asserts_.emplace_back(AssertStmt( | ||
alloc_size == 0 || !Call(DataType::Bool(), builtin::isnullptr(), {vptr}), | ||
tvm::tir::StringImm(arg_name + " is expected to have non-NULL data pointer"), nop)); | ||
|
||
def_handle_dtype_.Set(vptr, tir::TypeAnnotation(buffer->dtype)); | ||
// mark alignment of external bufs | ||
init_nest_.emplace_back(AttrStmt(vptr, tir::attr::storage_alignment, | ||
IntImm(DataType::Int(32), buffer->data_alignment), nop)); | ||
} | ||
} | ||
|
||
} // namespace tir | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Wonder how this came up. Just for readability?
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.
Oh, that's really weird. I'm guessing it was from bouncing over to the PR branch of #16183, which touched a number of the FFI bindings. I've removed this delta from the PR.