Skip to content

Capture constness on elaborated types#14

Open
slburson wants to merge 2 commits into
borodust:masterfrom
slburson:const-elaborated-types
Open

Capture constness on elaborated types#14
slburson wants to merge 2 commits into
borodust:masterfrom
slburson:const-elaborated-types

Conversation

@slburson

Copy link
Copy Markdown

I started trying to compile the generated adapter, and the first problem I ran into was that Claw was generating getter/setter functions for global variables declared const.

For example, source essentially like this (greatly simplified):

struct Symbol {
    explicit constexpr Symbol(int uniq) : value(uniq) {}
    constexpr operator int() const noexcept { return value; }
private:
    int value;
};

constexpr Symbol view_copy(42);

produced this output:

__CLAW_API void __claw_set_c10WprimWview_copy(struct c10::Symbol* __claw_value_) {
// /home/gyro/Repos/Claw-Torch/libtorch/include/ATen/core/interned_strings.h:353:1

c10::prim::view_copy = *__claw_value_;
}

__CLAW_API struct c10::Symbol* __claw_get_c10WprimWview_copy() {
// /home/gyro/Repos/Claw-Torch/libtorch/include/ATen/core/interned_strings.h:353:1

return (struct c10::Symbol*)&c10::prim::view_copy;
}

I traced the problem to these lines near the beginning of resect_type_create:

        case CXType_Elaborated:
            return resect_type_create(visit_context, context, clang_Type_getNamedType(clang_type));

I wondered whether the constness was set on the elaborated type, and was therefore being lost, and this proved to be the case. Working out a fix took some time and experimentation, but I finally came up with something I'm happy with.

(I suspect this fixes the bug that you wrote claw.resect::ensure-const-type-if-needed to work around, but I haven't checked.)

The idea is that we need to use the elaborated type as the registration key so that we can safely mark the type const once it is built. This requires moving the resect_find_type call to the top, and replacing the recursive call with (a) noting whether the elaborated type was const, and (b) unwrapping it for the benefit of subsequent processing.

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.

1 participant