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

Various fixes to static-dimensioned Buffer #6589

Merged
merged 2 commits into from
Jan 27, 2022

Conversation

steven-johnson
Copy link
Contributor

@steven-johnson steven-johnson commented Jan 27, 2022

More details that should have been in #6574:

  • Buffer needed to make some methods constexpr, and also had a broken return value for as<>()
  • Various templated methods needed to add int Dims as a parameter
  • Generator::add_input() and add_output() needed specializations for static-buffer types
  • sliced() and embedded() should use the default values for InClassDimStorage
  • halide_image_io should use a named constant

- Buffer needed to make some methods constexpr, and also had a broken return value for `as<>()`
- Various templated methods needed to add `int Dims` as a parameter
- Generator::add_input() and add_output() needed specializations for static-buffer types
- sliced() and embedded() should use the default values for InClassDimStorage
- halide_image_io should use a named constant
src/Generator.h Outdated
typename std::enable_if<T::has_static_halide_type>::type * = nullptr>
typename std::enable_if<!std::is_arithmetic<T>::value && !std::is_same<T, Halide::Func>::value>::type * = nullptr>
GeneratorInput<T> *add_input(const std::string &name, const Type &t, int dimensions) {
static_assert(!T::has_static_dimensions, "You can only call this version of add_input() for a Buffer<T, D> where T is void or omitted .");
Copy link
Member

Choose a reason for hiding this comment

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

These static assert conditions seem to be the same on both lines

Copy link
Contributor Author

Choose a reason for hiding this comment

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

whoops, first should be has_static_halide_type

src/Generator.h Outdated
template<typename T,
typename std::enable_if<!std::is_arithmetic<T>::value && !std::is_same<T, Halide::Func>::value>::type * = nullptr>
GeneratorInput<T> *add_input(const std::string &name) {
static_assert(T::has_static_dimensions, "You can only call this version of add_input() for a Buffer<T, D> where T is not void.");
Copy link
Member

Choose a reason for hiding this comment

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

Same comment here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ditto

src/Generator.h Outdated
GeneratorOutput<T> *add_output(const std::string &name, const Type &t, int dimensions) {
static_assert(!T::has_static_dimensions, "You can only call this version of add_output() for a Buffer<T, D> where T is void or omitted .");
Copy link
Member

Choose a reason for hiding this comment

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

and here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ditto

src/Generator.h Outdated
template<typename T,
typename std::enable_if<!std::is_arithmetic<T>::value && !std::is_same<T, Halide::Func>::value>::type * = nullptr>
GeneratorOutput<T> *add_output(const std::string &name) {
static_assert(T::has_static_dimensions, "You can only call this version of add_output() for a Buffer<T, D> where T is not void.");
Copy link
Member

Choose a reason for hiding this comment

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

and here, unless I'm really not understanding something

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ditto

@@ -2088,31 +2090,32 @@ struct ImageTypeConversion {
// as documentation and a backstop against breakage.
static_assert(!ImageType::has_static_halide_type,
"This variant of convert_image() requires a dynamically-typed image");
constexpr int DimsUnconstrained = Internal::DimsUnconstrained;
Copy link
Member

Choose a reason for hiding this comment

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

Would "AnyDims" be a better name than "DimsUnconstrained"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unconstrained is the term we use in HalideBuffer.h so that's what I used here.

Copy link
Member

Choose a reason for hiding this comment

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

I know, I was reconsidering the name in general. Seeing it in usage makes it seem clunky.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, gotcha. I think I originally called it "DynamicDims" and @zvookin suggested "DimsUnconstrained". "AnyDims" is fine with me too and definitely terser. I'll make the change everywhere if you like.

@steven-johnson
Copy link
Contributor Author

PTAL

@@ -2615,7 +2615,7 @@ HALIDE_NO_USER_CODE_INLINE T evaluate_may_gpu(const Expr &e) {
Func f;
f() = e;
Internal::schedule_scalar(f);
Buffer<T> im = f.realize();
Buffer<T, 0> im = f.realize();
Copy link
Member

Choose a reason for hiding this comment

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

This is just a minor stack space optimization, right? I don't see that this zero makes much of a difference because the dims array is never accessed. I have no objection to adding it, I just want to make sure I understand what's happening.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct, it's just a tiny optimization.

(On that note: the code for Buffer currently always allocates space for at least one halide_dimension_t, even for zero-dim buffers, on the assumption that there is probably sloppy code out there that assumes that buffer.dim always points to something valid...)

@steven-johnson steven-johnson merged commit d27866e into master Jan 27, 2022
@steven-johnson steven-johnson deleted the srj/static-buffer-fixes branch January 27, 2022 17:46
jrprice pushed a commit to jrprice/Halide that referenced this pull request Feb 4, 2022
* Various fixes to static-dimensioned Buffer

- Buffer needed to make some methods constexpr, and also had a broken return value for `as<>()`
- Various templated methods needed to add `int Dims` as a parameter
- Generator::add_input() and add_output() needed specializations for static-buffer types
- sliced() and embedded() should use the default values for InClassDimStorage
- halide_image_io should use a named constant

* Update Generator.h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants