Support for number, array, and dictionary constant literal optimization #136

Open
opened 2026-07-19 17:37:05 +02:00 by fang-ling · 4 comments

A recent Clang PR adds support for emitting ObjC literal expressions — @(number), @[], and @{} — as compile-time constant data structures instead of runtime message lookup calls, when the target runtime supports it.

As with constant string literals (-fconstant-string-class=), the backing classes for the other literal kinds can be customized via:

-fconstant-array-class=
-fconstant-dictionary-class=
-fconstant-integer-number-class=
-fconstant-float-number-class=
-fconstant-double-number-class=

The feature is gated behind -fno-objc-constant-literals and is automatically disabled for the fragile ABI and GNU runtimes, since it relies on ObjCRuntime::hasConstantLiteralClasses() in Clang, which currently only recognizes Apple's runtimes.

Question / request: Would it make sense for the ObjFW runtime to support this? Concretely, that would mean getting Clang to recognize ObjFW as a runtime with constant literal support, and having ObjFW provide the corresponding backing classes (something like OFConstantArray, OFConstantDictionary, OFConstantNumber, or similar) so the optimization can actually kick in.

Is this something you'd be open to supporting?

A recent [Clang PR](https://github.com/llvm/llvm-project/pull/185130) adds support for emitting ObjC literal expressions — `@(number)`, `@[]`, and `@{}` — as compile-time constant data structures instead of runtime message lookup calls, when the target runtime supports it. As with constant string literals (`-fconstant-string-class=`), the backing classes for the other literal kinds can be customized via: ``` -fconstant-array-class= -fconstant-dictionary-class= -fconstant-integer-number-class= -fconstant-float-number-class= -fconstant-double-number-class= ``` The feature is gated behind `-fno-objc-constant-literals` and is automatically disabled for the fragile ABI and GNU runtimes, since it relies on `ObjCRuntime::hasConstantLiteralClasses()` in Clang, which currently only recognizes Apple's runtimes. **Question / request**: Would it make sense for the ObjFW runtime to support this? Concretely, that would mean getting Clang to recognize ObjFW as a runtime with constant literal support, and having ObjFW provide the corresponding backing classes (something like `OFConstantArray`, `OFConstantDictionary`, `OFConstantNumber`, or similar) so the optimization can actually kick in. Is this something you'd be open to supporting?

Please tell me if implementing this will also help fixing #56. Thank you all.

Please tell me if implementing this will also help fixing https://git.nil.im/ObjFW/ObjFW/issues/56. Thank you all.
Owner

This was in the proprietary Apple Clang for a while, so it's great to see that it also made it into the open source Clang.

I'm not sure how I feel about needing to specify every class separately. On the one hand, this is better than how it was done previously, where -fobjc-runtime=apple implied Foundation. OTOH, I think a -fobjc-framework=objfw flag would be better, as the framework and the runtime are different things that are not necessarily the same.

That said, this is probably a good idea, as it's right now the best way to avoid the hacks needed so far because Foundation is hardcoded. So as a side effect it will probably also address #56.

This was in the proprietary Apple Clang for a while, so it's great to see that it also made it into the open source Clang. I'm not sure how I feel about needing to specify every class separately. On the one hand, this is better than how it was done previously, where `-fobjc-runtime=apple` implied Foundation. OTOH, I think a `-fobjc-framework=objfw` flag would be better, as the framework and the runtime are different things that are not necessarily the same. That said, this is probably a good idea, as it's right now the best way to avoid the hacks needed so far because Foundation is hardcoded. So as a side effect it will probably also address #56.
Author

Yes, the -fobjc-framework=objfw flag sounds like a good choice for setting the default framework to something other than Foundation, while still allowing each class to be specified individually. That flexibility is especially useful for those who want to use their own String, Number, Array, or Dictionary classes along with the ObjFW runtime.

As for #56, unlike constant string literals, this optimization may not fully address the issue, since there are cases where boxed or collection literals cannot be compile-time constants. For example, @(pow(a, b)) or @[[[MyObject alloc] init], /* ... */].

Perhaps with -fobjc-framework=objfw, we could also make the previously hard-coded NSString, NSNumber, NSArray, and NSDictionary classes configurable.

Yes, the `-fobjc-framework=objfw` flag sounds like a good choice for setting the default framework to something other than Foundation, while still allowing each class to be specified individually. That flexibility is especially useful for those who want to use their own `String`, `Number`, `Array`, or `Dictionary` classes along with the ObjFW runtime. As for #56, unlike constant string literals, this optimization may not fully address the issue, since there are cases where boxed or collection literals cannot be compile-time constants. For example, `@(pow(a, b))` or `@[[[MyObject alloc] init], /* ... */]`. Perhaps with `-fobjc-framework=objfw`, we could also make the previously hard-coded `NSString`, `NSNumber`, `NSArray`, and `NSDictionary` classes configurable.
Owner

Yes, that would be the idea, as the current hack is fragile and doesn't work anymore with modern C++, as modern C++ implies ObjC modules in Clang, which are incompatible with the hack as it requires defines.

Yes, that would be the idea, as the current hack is fragile and doesn't work anymore with modern C++, as modern C++ implies ObjC modules in Clang, which are incompatible with the hack as it requires defines.
Sign in to join this conversation.
No milestone
No project
No assignees
3 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
ObjFW/ObjFW#136
No description provided.