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

Add workaround for the const-or-not user_context issue (#635) #7291

Merged
merged 7 commits into from
Jan 20, 2023

Conversation

steven-johnson
Copy link
Contributor

@steven-johnson steven-johnson commented Jan 18, 2023

For historical reasons, Halide-generated AOT code defines user_context as void const*, but expects all define_extern code with user_context usage to use void *. This usually isn't an issue, but turns into one if both the caller and callee of the define_extern() use the C++ backend, and a user_context is passed, and c_plus_plus_name_mangling is enabled, in which case we get link errors because of this dichotomy. Fixing this "correctly" (ie so that everything always uses identical types for user_context in all cases) will require a lot of downstream churn, so this is an ugly workaround:

If this is an external function, and it's a C++ implementation, and it uses user_context, then add a wrapper with void* ucon -> void const* ucon. In most cases this will be ignored (and probably dead-stripped), but in these cases it's critical.

(Note that we don't check to see if c_plus_plus_name_mangling is enabled, since that would have to be done on the caller side, and this is purely a callee-side fix.)

EDIT: amazingly, this is broken in the LLVM backend as well -- I find it extremely hard to believe that no one has ever attempted to wire an AOT-Halide-filter-with-ucon directly to define_extern() with c_plus_plus_name_mangling enabled before, but apparently that must be the case. Updating this PR to include a similar shim in the LLVM backend as well.

This is subtle: for historical reasons, Halide-generated AOT code defines user_context as `void const*`, but expects all define_extern code with user_context usage to use `void *`. This usually isn't an issue, but turns into one if both the caller and callee of the define_extern() use the C++ backend, *and* a user_context is passed, *and* c_plus_plus_name_mangling is enabled, in which case we get link errors because of this dichotomy. Fixing this "correctly" (ie so that everything always uses identical types for user_context in all cases) will require a *lot* of downstream churn, so this is an ugly workaround:

If this is an external function, and it's a C++ implementation, and it uses user_context, then add a wrapper with `void*` ucon -> `void const*` ucon. In most cases this will be ignored (and probably dead-stripped), but in these cases it's critical.

(Note that we don't check to see if c_plus_plus_name_mangling is enabled, since
that would have to be done on the caller side, and this is purely a callee-side fix.)
@steven-johnson
Copy link
Contributor Author

Looks like this is finally green (and also looks good inside Google).... PTAL

@abadams
Copy link
Member

abadams commented Jan 19, 2023

I'm ok with the code changes but I'd prefer if Zalman approved it because I'm not following the exact scenario that's giving you trouble. Is it calling a halide stage as an extern stage in another halide pipeline, but with the c backend and c++ mangling on?

@steven-johnson
Copy link
Contributor Author

Is it calling a halide stage as an extern stage in another halide pipeline, but with the c backend and c++ mangling on?

Yes, except it affects the LLVM backend too. (I am gobsmacked that we apparently never tested that scenario anyway -- even inside Google -- but that is indeed apparently the case.)

@@ -1487,7 +1487,7 @@ class ExternCallPrototypes : public IRGraphVisitor {
}

bool has_c_declarations() const {
return !c_externs.empty();
return !c_externs.empty() || !destructors.empty();
Copy link
Member

Choose a reason for hiding this comment

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

This looks like a general fix outside of what this PR is doing. If so, worth a separate PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, maybe -- it's a drive-by that was needed only because the changes to the nested_externs test caused a call to halide_device_free_as_destructor to be injected when building under host-cuda, but it failed to compile because the call in question had no prototype. I can try to make minor changes to pull this out separately.

@steven-johnson steven-johnson changed the title Add a "fix" for the const-or-not user_context issue Add workaround for the const-or-not user_context issue (#635) Jan 20, 2023
@steven-johnson steven-johnson merged commit c601e4e into main Jan 20, 2023
@steven-johnson steven-johnson deleted the srj/cpp-ucon-hack branch January 20, 2023 17:45
steven-johnson added a commit that referenced this pull request Jan 20, 2023
ardier pushed a commit to ardier/Halide-mutation that referenced this pull request Mar 3, 2024
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.

3 participants