Skip to content
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

Rustify PackedFunc & Friends #2969

Merged
merged 5 commits into from
Apr 7, 2019
Merged

Rustify PackedFunc & Friends #2969

merged 5 commits into from
Apr 7, 2019

Conversation

nhynes
Copy link
Member

@nhynes nhynes commented Apr 5, 2019

No description provided.

@nhynes nhynes requested a review from tqchen April 5, 2019 06:44
@nhynes nhynes marked this pull request as ready for review April 5, 2019 06:44
@nhynes
Copy link
Member Author

nhynes commented Apr 5, 2019

cc @ehsanmok for review

Aren't PRs that have more red than green the best?

@nhynes nhynes force-pushed the rust-next branch 3 times, most recently from 801da1f to 6698f72 Compare April 5, 2019 08:03
Copy link
Contributor

@ehsanmok ehsanmok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job! I really like the enum POD repr. Here're some small comments:

rust/common/src/packed_func.rs Show resolved Hide resolved
rust/common/src/packed_func.rs Show resolved Hide resolved
rust/common/src/packed_func.rs Show resolved Hide resolved
pub value: TVMValue,
pub type_code: i64,
/// Constructs a derivative of a TVMPodValue.
macro_rules! TVMPODValue {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat :)

@@ -195,11 +195,11 @@ impl<'a, 'm> Builder<'a, 'm> {
let (mut values, mut type_codes): (Vec<ffi::TVMValue>, Vec<ffi::TVMTypeCode>) = self
.arg_buf
.iter()
.map(|tvm_arg| (tvm_arg.value, tvm_arg.type_code as ffi::TVMTypeCode))
.map(|arg| arg.clone().into_tvm_value())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be without clone after into_iter or something?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that would require

- type PackedFunc = Fn(&[TVMArgValue]) -> TVMRetValue;
+ type PackedFunc = Fn(Vec<TVMArgValue>) -> TVMRetValue;

and the TVM module is surely not going to know how to Drop a Vec (nor can the frontend or runtime Drop a C++ std::vector).

I think that a better solution might be .as_tvm_value(), but that adds a lot of boilerplate code and it's not totally clear that rustc doesn't just optimize away the clone.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, as_tvm_value seems correct. I guess I'll just make the macros more macro-y to eliminate the repetitive code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could eliminate the allocation on the rust side doing something like:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=8d4048c7235d7e7bf8074f1514a07350

This wouldn't be able to go through Builder though.

Also, mapping a slice of tuples to a pair of slices is actually surprisingly hard to do with macros. You could avoid doing it if you're able to just call into() twice for each macro, or else you can do some advanced macro magic to map over the slice.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be addressed later.

@ehsanmok
Copy link
Contributor

ehsanmok commented Apr 5, 2019

@nhynes @tqchen LGTM

@kazimuth
Copy link
Contributor

kazimuth commented Apr 5, 2019

e: see my review

@tqchen
Copy link
Member

tqchen commented Apr 5, 2019

@kazimuth
Copy link
Contributor

kazimuth commented Apr 5, 2019

@tqchen sure

@nhynes
Copy link
Member Author

nhynes commented Apr 5, 2019

I replaced into_tvm_value with to_tvm_value which takes &self. It copies the primitives and pointers, but this is actually correct since something needs to retain ownership of the strings and bytearrays.

@ehsanmok
Copy link
Contributor

ehsanmok commented Apr 5, 2019

right! @nhynes if I remember correctly, into_tvm_value was necessary before esp. for box but it should be possible according to your changes now.

@nhynes
Copy link
Member Author

nhynes commented Apr 5, 2019

It's no longer into because ffi::TVMValue doesn't own its contents. If, for instance, the frontend did into_tvm_value, there would be no guarantee that the pointed-to data wasn't dropped.

@nhynes nhynes mentioned this pull request Apr 6, 2019
@ehsanmok
Copy link
Contributor

ehsanmok commented Apr 7, 2019

@tqchen Could you merge this sooner please? I'm working on a feature which requires these fundamental changes and don't want to resolve too many conflicts.

@tqchen
Copy link
Member

tqchen commented Apr 7, 2019

awaiting approval from @kazimuth

@@ -195,11 +195,11 @@ impl<'a, 'm> Builder<'a, 'm> {
let (mut values, mut type_codes): (Vec<ffi::TVMValue>, Vec<ffi::TVMTypeCode>) = self
.arg_buf
.iter()
.map(|tvm_arg| (tvm_arg.value, tvm_arg.type_code as ffi::TVMTypeCode))
.map(|arg| arg.clone().into_tvm_value())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be addressed later.

@kazimuth
Copy link
Contributor

kazimuth commented Apr 7, 2019

Done @tqchen

@tqchen tqchen merged commit 14a0ecb into apache:master Apr 7, 2019
@tqchen
Copy link
Member

tqchen commented Apr 7, 2019

THanks, @nhynes , @kazimuth @ehsanmok , this is now merged

wweic pushed a commit to wweic/tvm that referenced this pull request Apr 8, 2019
wweic pushed a commit to wweic/tvm that referenced this pull request Apr 10, 2019
wweic pushed a commit to neo-ai/tvm that referenced this pull request Apr 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants