Skip to content

Conversation

@jcmosc
Copy link
Contributor

@jcmosc jcmosc commented Jul 12, 2025

No description provided.

#include "OGBase.h"

typedef OG_OPTIONS(uint32_t, OGAttributeFlags) {
typedef OG_OPTIONS(uint8_t, OGAttributeFlags) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This gets stored as an 8-bit field in AG::Node.

Comment on lines 36 to 39
struct {
OGTypeID type_id;
const void *witness_table;
} body_conformance;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Kyle-Ye This doesn't appear in the swift section dump you provided, but I can see it being set in Ghidra's disassembly. I'm not sure if there is a difference in behaviour depending on version.

Copy link
Contributor

Choose a reason for hiding this comment

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

Do we miss something in swift section? Maybe we can try to use Ghidra and align those logic. cc @Mx-Iris

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm now on macOS Sequoia and the Swift section is reporting the structure is 56 bytes, which would exclude this field. But compatibility tests are confirming that it is definitely set.
https://github.com/jcmosc/Compute/blob/updating/Tests/ComputeTests/Shared/Attribute/AttributeTests.swift#L39

It's possible that it's not included in the attribute type struct, but instead part of a wrapper struct returned from the closure passed to AGGraphInternAttributeType.

@Kyle-Ye
Copy link
Contributor

Kyle-Ye commented Aug 2, 2025

Any reason why this is still work in progress? If you do not mind, I can help rebase and merge it now. cc @jcmosc

@Kyle-Ye Kyle-Ye marked this pull request as ready for review August 2, 2025 08:02
@Kyle-Ye Kyle-Ye force-pushed the update-public-interface branch from db8f5a7 to 0825d44 Compare August 2, 2025 08:10
@jcmosc jcmosc force-pushed the update-public-interface branch from 0825d44 to 504745e Compare August 2, 2025 15:10
@jcmosc jcmosc changed the title Update struct definitions Update public interface Aug 2, 2025
@jcmosc
Copy link
Contributor Author

jcmosc commented Aug 2, 2025

@Kyle-Ye I didn't see your comment yesterday! I would consider this ready to merge, though I realise it's become a big PR. Happy to break it up into smaller ones if you prefer.

@Kyle-Ye
Copy link
Contributor

Kyle-Ye commented Aug 3, 2025

@Kyle-Ye I didn't see your comment yesterday! I would consider this ready to merge, though I realise it's become a big PR. Happy to break it up into smaller ones if you prefer.

Merge this as it is or split it both seem fine to me.

Let's solve the final conflict and get it merged 🚀.

@jcmosc jcmosc force-pushed the update-public-interface branch 3 times, most recently from 059fc91 to 024fde2 Compare August 3, 2025 21:10
@jcmosc
Copy link
Contributor Author

jcmosc commented Aug 3, 2025

@Kyle-Ye rebased without conflicts :)

@Kyle-Ye
Copy link
Contributor

Kyle-Ye commented Aug 4, 2025

The compatibility tests is failing since we have not updated AG yet. I'll create a PR to update it.

Copy link
Contributor

@Kyle-Ye Kyle-Ye left a comment

Choose a reason for hiding this comment

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

98% change is LGTM.

1% change is NIT: Some missing OG_SWIFT_NAME can be added or a later PR.

1% change needs more discussion here: mainly Subgraph.Flags and Flags. (We can also merge it as it is after the AG repo bump if you do not have time recently and discuss it later.)

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should keep the API on AnyAttribute side and Attribute side in sync.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can't see the line number this comment is attached to in GitHub. I assume you're referring to source vs indirectSource?

I originally thought the accessor traps if the attribute is not indirect and wanted to call this behaviour out. But I realise now it actually just returns itself.

I'll revert it to source.

Comment on lines -55 to +62
@_silgen_name("OGSubgraphApply")
private func OGSubgraphApply(
_ graph: Subgraph,
flags: OGAttributeFlags,
callback: (AnyAttribute) -> Void
)

@_silgen_name("OGSubgraphAddObserver")
private func OGSubgraphAddObserver(
_ graph: Subgraph,
observer: () -> Void
) -> Int
// FIXME: migrate to use @_extern(c, "xx") in Swift 6
extension Subgraph {
@_silgen_name("OGSubgraphApply")
private static func apply(_ graph: Subgraph, flags: Subgraph.Flags, callback: (AnyAttribute) -> Void)

@_silgen_name("OGSubgraphAddObserver")
private static func addObserver(_ graph: Subgraph, observer: () -> Void) -> Int
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason to change this? Does it related with https://forums.swift.org/t/understanding-calling-conventions-passing-closure-from-swift-to-c-and-calling-it-from-c/79995?

If so, please add some comments context 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.

At the time this was the only way I could get the parameters to line up between C++ and Swift. But I was using @_extern(c) instead of @_silgen_name. I will experiment with @_silgen_name to see if it works without changes.

Comment on lines +14 to +17
typedef OG_OPTIONS(uint8_t, OGAttributeFlags) {
OGAttributeFlagsNone = 0,
OGAttributeFlagsAll = 0xFF,
} OG_SWIFT_NAME(OGSubgraphRef.Flags);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why we are removing the case for OGAttributeFlags and rename it to OGSubgraphRef.Flags

Copy link
Contributor Author

Choose a reason for hiding this comment

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

From OG's point of view, these flags have no semantics. They function more like a userInfo dictionary. It would be in OpenSwiftUI or another hypothetical client where the actual bits are assigned meaning.

In OG, these are only used by OGSubgraphIsDirty, OGSubgraphIntersects, OGSubgraphApply and OGSubgraphUpdate with the effect that those functions only consider attributes whose flags match the passed in mask (or all attributes if the passed in mask is 0x00).

As for the name, the JSON returned from OGGraphDescription includes these under the key "subgraph_flags".

OGAttributeTypeFlagsExternal = 1 << 4,
OGAttributeTypeFlagsAsyncThread = 1 << 5,
};
} OG_SWIFT_NAME(Flags);
Copy link
Contributor

Choose a reason for hiding this comment

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

The name should not be Flags.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am basing it off this section:

struct __C._AttributeType {
    var self_id: __C.Metadata
    var value_id: __C.Metadata
    var update: __C._AGClosureStorage
    var vtable: Swift.UnsafePointer<__C._AttributeVTable>
    var flags: __C.Flags
    var internal_offset: Swift.UInt32
    var value_layout: Swift.Optional<Swift.UnsafePointer<Swift.UInt8>>
}

Copy link
Contributor

Choose a reason for hiding this comment

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

If a type is XXYYZZ marks as NS_SWIFT_NAME(YY.ZZ), the name you will see is __C.ZZ.

So I suggest changing the name to OGAttributeType.Flags

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh I didn't know that. Sure I'll change it to OGAttributeType.Flags!

OGCounterQueryTypeCreatedNodes,
OGCounterQueryTypeSubgraphs,
OGCounterQueryTypeCreatedSubgraphs,
} OG_SWIFT_NAME(OGGraphRef.CounterQueryType);
Copy link
Contributor

Choose a reason for hiding this comment

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

Also change the enum to OGGraphCounterQueryType

OGTraceEventsNamed = 2,
OGTraceEventsDeadline = 3,
OGTraceEventsCompareFailed = 4,
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Swift name - TraceEvents

void (*_Nullable passed_deadline)(void *_Nullable context);

void (*_Nullable compare_failed)(void *_Nullable context, OGAttribute attribute, OGComparisonState comparisonState);
} OGTrace;
Copy link
Contributor

Choose a reason for hiding this comment

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

Swift name - Trace

OGValueStateMainRef = 1 << 5,
OGValueStateRequiresMainThread = 1 << 6,
OGValueStateSelfModified = 1 << 7,
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Swift name - ValueState

Copy link
Contributor

Choose a reason for hiding this comment

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

The same as I commented above. Why the cases is removed and use rawValue 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.

I agree the tests are much more verbose now. But I disagree it is a concern of OG to define what each flag mean for the same reasons above. What if we define an extension in the test target so the test at least is more readable?

Copy link
Contributor

Choose a reason for hiding this comment

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

Got it. I'll do it in the upstream then.

What if we define an extension in the test target so the test at least is more readable?

Yeah. Go ahead.

@Kyle-Ye
Copy link
Contributor

Kyle-Ye commented Aug 4, 2025

When we are ready with the final comment, here is the merge process @jcmosc :

  1. run Scripts/bump_ag_pr.sh update-public-interface to create a PR for DarwinPrivateFramework (Needs to merge the latest code from main branch to use the script)
  2. We merge that PR.
  3. We update Package.resovled in this repo.
  4. Pass the CI and merge this.

jcmosc added 5 commits August 11, 2025 03:50
# Conflicts:
#	Sources/OpenGraph_SPI/Graph/GraphDescription.mm
#	Sources/OpenGraph_SPI/include/OGGraphDescription.h

# Conflicts:
#	Sources/OpenGraphCxx/include/OpenGraph/OGAttributeType.h
#	Sources/OpenGraphCxx/include/OpenGraph/OGClosure.h
#	Sources/OpenGraphCxx/include/OpenGraph/OGValue.h
@Kyle-Ye Kyle-Ye force-pushed the update-public-interface branch from 37a0caa to a46e09f Compare August 10, 2025 19:50
@Kyle-Ye
Copy link
Contributor

Kyle-Ye commented Aug 10, 2025

@Kyle-Ye Kyle-Ye merged commit 62d1332 into OpenSwiftUIProject:main Aug 10, 2025
4 checks passed
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