-
Notifications
You must be signed in to change notification settings - Fork 13.7k
initial implementation of the darwin_objc unstable feature #145660
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
base: master
Are you sure you want to change the base?
Conversation
Some changes occurred in compiler/rustc_attr_parsing Some changes occurred in compiler/rustc_codegen_ssa Some changes occurred in compiler/rustc_passes/src/check_attr.rs Some changes occurred in compiler/rustc_hir/src/attrs |
This comment has been minimized.
This comment has been minimized.
I looked through the attribute changes and they look good to me. I can't judge the rest. @tmandry feel free to approve in both our names once you agree with the rest :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looked through it, it seems to match what I tried to do with the objc2
macros those years back (which is not to say that it is correct, I had no idea what I was doing back then).
Also, it would be helpful with more comments, and a few codegen tests - I get why you haven't done that yet, it's bothersome while we're still somewhat discussing what the best approach is.
Note: I'm still a bit unsure that this is actually the correct approach going forward, I suspect there might be more value in the future from something like the define_in_every_cgu_used
you first proposed?
At the very least we'll also need some way to get protocol references, we might also need metaclass references, and possibly more if we're to support static NSString and support fully statically declared classes.
But I think it's fine to move forwards with this in the current state, then we can always figure that sort of stuff out well before stabilisation.
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I added |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
☔ The latest upstream changes (presumably #145728) made this pull request unmergeable. Please resolve the merge conflicts. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
☔ The latest upstream changes (presumably #145773) made this pull request unmergeable. Please resolve the merge conflicts. |
This comment has been minimized.
This comment has been minimized.
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gave it another review after I got around to actually building and playing with it.
The RefCell
on compiler_used_statics
is fine.
Also, if you could update the PR description to motivate the feature that'd be nice as well (it'll be recorded in Git history).
tests/codegen-llvm/darwin-objc.rs
Outdated
extern crate minicore; | ||
use minicore::*; | ||
|
||
#[no_mangle] | ||
pub fn get_class() -> *mut () { | ||
unsafe extern "C" { | ||
#[rustc_objc_class = "MyClass"] | ||
safe static VAL: *mut (); | ||
} | ||
VAL | ||
} | ||
|
||
#[no_mangle] | ||
pub fn get_selector() -> *mut () { | ||
unsafe extern "C" { | ||
#[rustc_objc_selector = "myMethod"] | ||
safe static VAL: *mut (); | ||
} | ||
VAL | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would be interested in the following extra assembly tests:
- Using the same class/selector twice in the same codegen unit.
- A test with two codegen units, to ensure that it is the codegen unit that ends up using these that has the references, and that the other doesn't.
- Using one of these in a cross-crate compilation setting, with inlining.
- Possibly also where the crate that defines the static is compiled as a dylib?
- An assertion that not using any of these means that the Objective-C image/module info isn't output either.
And maybe a "practical" test that uses the macros and tests that things actually links and runs, something like:
//! Run `[NSObject class]` using `core::os::darwin` helpers.
#![feature(darwin_objc)]
use std::mem::transmute;
use std::os::darwin::objc::{Class, SEL, class, selector};
#[link(name = "objc", kind = "dylib")]
unsafe extern "C-unwind" {
unsafe fn objc_msgSend();
}
#[link(name = "Foundation", kind = "framework")]
unsafe extern "C" {}
fn main() {
let msg_send_fn = unsafe {
transmute::<
unsafe extern "C-unwind" fn(),
unsafe extern "C-unwind" fn(Class, SEL) -> Class,
>(objc_msgSend)
};
let cls = class!("NSObject");
let runtime_class = unsafe { msg_send_fn(cls, selector!("class")) };
assert_eq!(cls, runtime_class);
}
See also the compile tests that objc2
has (I'm not attached to those specifically, but I at least thought those things were interesting to test back when I experimented with it).
self.compiler_used_statics.borrow_mut().push(global); | ||
} | ||
|
||
fn define_objc_classname(&self, classname: &str) -> &'ll Value { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you wouldn't mind, I think it'd be useful to have some links to Clang's implementation of the functions you've added in this file - that would make it easier to verify that we're doing things correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also would be nice to document Clang's -fno-objc-msgsend-selector-stubs
, which makes verification easier.
llvm::set_linkage(g, llvm::Linkage::PrivateLinkage); | ||
llvm::set_section(g, c"__OBJC,__module_info,regular,no_dead_strip"); | ||
|
||
self.add_compiler_used_global(g); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Testing with:
// clang test_objc_c.m -emit-llvm -S -O -fno-objc-msgsend-selector-stubs
#import <Foundation/Foundation.h>
Class foo(void) {
return [NSObject foo];
}
And:
// rustc +stage1 test_objc.rs --emit=llvm-ir -O
#![feature(darwin_objc)]
#![crate_type = "lib"]
use std::mem::transmute;
use std::os::darwin::objc::{Class, SEL, class, selector};
#[link(name = "objc", kind = "dylib")]
unsafe extern "C-unwind" {
unsafe fn objc_msgSend();
}
#[link(name = "Foundation", kind = "framework")]
unsafe extern "C" {}
#[no_mangle]
pub fn foo() -> Class {
let msg_send_fn = unsafe {
transmute::<
unsafe extern "C-unwind" fn(),
unsafe extern "C-unwind" fn(Class, SEL) -> Class,
>(objc_msgSend)
};
let cls = class!("NSObject");
unsafe { msg_send_fn(cls, selector!("foo")) }
}
I'm finding the following discrepancies:
@OBJC_CLASSLIST_REFERENCES_$_
andOBJC_SELECTOR_REFERENCES_
hasno_dead_strip
in the link section (see also macOS: Why are Objective-C sections marked withno_dead_strip
? llvm/llvm-project#114111, I'd be fine with linking to that as to why we don't do that as well).@OBJC_SELECTOR_REFERENCES_
has theexternally_initialized
attribute.- When loading the
@OBJC_SELECTOR_REFERENCES_
symbol, the!invariant.load
metadata attribute is used. - The Rust side uses
!noundef
a lot (probably fine).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- I had noticed the
no_dead_strip
discrepancy as well, found the unresolved LLVM issue you linked, and decided to take objc2's approach and exclude them. But as you note in that issue, "in practice the linker seems to remove it anyhow". So, in the spirit of just matching LLVM when we don't understand, I've added it back. - Last I checked, the LLVM Rust API we use doesn't currently expose the ability to set
externally_initialized
. But my understanding is it's only relevant for code with global initializers which Rust doesn't have. But just in-case it matters in the future, I'll take another stab at adding it. invariant.load
is just an optimization but might be nice to have. I'll look into adding it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
externally_initialized
was easy to add. Looking into invariant.load
now.
#[rustc_objc_class = $classname] | ||
safe static VAL: $crate::os::darwin::objc::Class; | ||
} | ||
VAL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just calling to attention that this unconditionally loads the value in the static, which I guess is fine, but may also be unnecessarily restrictive?
For example, I can't do:
struct ThreadSafe<T>(T);
unsafe impl<T> Sync for ThreadSafe<T> {}
static CLASSES: ThreadSafe<&[&'static Class]> = ThreadSafe(&[&class!("NSObject"), &class!("NSString")]);
Which I think I'd otherwise be able to do with an extern static
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's actually a feature of this design. In one of the Zulip threads, the concern was raised that an extern static with multiple definitions, even if de-deuplicated at static link time, wouldn't be de-duplicated across dylib boundaries. The macro was written such that only the value of the static is accessible and never its identity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, right. Could you document this somewhere in the implementation of the macro? And add a UI test for some of the behaviour that we want to disallow.
… reduce duplication
…ose the static value
Tracking issue: #145496
This feature makes it possible to reference Objective-C classes and selectors using the same ABI used by native Objective-C on Apple/Darwin platforms. Without it, Rust code interacting with Objective-C must resort to loading classes and selectors using costly string-based lookups at runtime. With it, these references can be loaded efficiently at dynamic load time.
r? @tmandry