Skip to content

Instantly share code, notes, and snippets.

@stuartcarnie
Created January 17, 2026 19:09
Show Gist options
  • Select an option

  • Save stuartcarnie/4af9775529dd091cf5213370b38876b6 to your computer and use it in GitHub Desktop.

Select an option

Save stuartcarnie/4af9775529dd091cf5213370b38876b6 to your computer and use it in GitHub Desktop.
metal-cpp: Objective-C vs C++

Summary

Shows differences in how code is generated when using metal-cpp (C++) vs Metal (Objective-C)

#include <Metal/Metal.hpp>
struct Foo {
MTL::Texture *tex;
~Foo() {
tex->release();
}
};
/* Type your code here, or load an example. */
MTL::Texture *newTexture(Foo *p_foo, MTL::Device *p_dev, MTL::TextureDescriptor *p_desc) {
MTL::Texture *tex = p_dev->newTexture(p_desc);
p_foo->tex = tex->retain();
return tex;
}
void foo_del(Foo *p_foo) {
delete p_foo;
}
; compiled with -std=c++17 -O3
newTexture(Foo*, MTL::Device*, MTL::TextureDescriptor*):
stp x20, x19, [sp, #-32]!
stp x29, x30, [sp, #16]
add x29, sp, #16
mov x8, x1
mov x19, x0
adrp x9, __ZN3MTL7Private8Selector28s_knewTextureWithDescriptor_E@GOTPAGE
ldr x9, [x9, __ZN3MTL7Private8Selector28s_knewTextureWithDescriptor_E@GOTPAGEOFF]
ldr x1, [x9]
mov x0, x8
bl _objc_msgSend
mov x20, x0
adrp x8, __ZN2NS7Private8Selector9s_kretainE@GOTPAGE
ldr x8, [x8, __ZN2NS7Private8Selector9s_kretainE@GOTPAGEOFF]
ldr x1, [x8]
bl _objc_msgSend
str x0, [x19]
mov x0, x20
ldp x29, x30, [sp, #16]
ldp x20, x19, [sp], #32
ret
foo_del(Foo*):
stp x20, x19, [sp, #-32]!
stp x29, x30, [sp, #16]
add x29, sp, #16
cbz x0, LBB1_3
mov x19, x0
ldr x0, [x0]
adrp x8, __ZN2NS7Private8Selector10s_kreleaseE@GOTPAGE
ldr x8, [x8, __ZN2NS7Private8Selector10s_kreleaseE@GOTPAGEOFF]
ldr x1, [x8]
bl _objc_msgSend
mov x0, x19
ldp x29, x30, [sp, #16]
ldp x20, x19, [sp], #32
b operator delete(void*)
LBB1_3:
ldp x29, x30, [sp, #16]
ldp x20, x19, [sp], #32
ret
bl ___clang_call_terminate
___clang_call_terminate:
stp x29, x30, [sp, #-16]!
mov x29, sp
bl ___cxa_begin_catch
bl std::terminate()
#import <Metal/Metal.h>
struct Foo {
id<MTLTexture> tex;
};
/* Type your code here, or load an example. */
id<MTLTexture> newTexture(Foo *p_foo, id<MTLDevice> p_dev, MTLTextureDescriptor *p_desc) {
id<MTLTexture> tex = [p_dev newTextureWithDescriptor:p_desc];
p_foo->tex = tex;
return tex;
}
void foo_del(Foo *p_foo) {
delete p_foo;
}
; compiled with -O3 -fobjc-arc
newTexture(Foo*, id<MTLDevice>, MTLTextureDescriptor*):
stp x22, x21, [sp, #-48]!
stp x20, x19, [sp, #16]
stp x29, x30, [sp, #32]
add x29, sp, #32
mov x21, x2
mov x20, x0
mov x0, x1
bl _objc_retain
mov x19, x0
mov x0, x21
bl _objc_retain
mov x21, x0
mov x0, x19
mov x2, x21
bl "_objc_msgSend$newTextureWithDescriptor:"
mov x22, x0
mov x0, x20
mov x1, x22
bl _objc_storeStrong
mov x0, x21
bl _objc_release
mov x0, x19
bl _objc_release
mov x0, x22
ldp x29, x30, [sp, #32]
ldp x20, x19, [sp, #16]
ldp x22, x21, [sp], #48
b _objc_autoreleaseReturnValue
mov x20, x0
mov x0, x21
bl _objc_release
mov x0, x19
bl _objc_release
mov x0, x20
bl __Unwind_Resume
foo_del(Foo*):
cbz x0, LBB1_2
stp x20, x19, [sp, #-32]!
stp x29, x30, [sp, #16]
add x29, sp, #16
ldr x8, [x0]
mov x19, x0
mov x0, x8
bl _objc_release
mov x0, x19
ldp x29, x30, [sp, #16]
ldp x20, x19, [sp], #32
b operator delete(void*)
LBB1_2:
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment