Xcode 14 deprecates bitcode - but why? - ios

The bounty expires in 7 days. Answers to this question are eligible for a +50 reputation bounty.
mfaani wants to reward an existing answer.
Xcode 14 Beta release notes are out, all thanks to the annual WWDC.
And alas, the Bitcode is now deprecated, and you'll get a warning message if you attempt to enable it.
And I was wondering, why has this happened? Was there any downside to using Bitcode? Was it somehow painful for Apple to maintain it? And how will per-iPhone-model compilation operate now?

Bitccode is actually just the LLVM intermediate language. When you compile source code using the LLVM toolchain, source code is translated into an intermediate language, named Bitcode. This Bitcode is then analyzed, optimized and finally translated to CPU instructions for the desired target CPU.
The advantage of doing it that way is that all LLVM based frontends (like clang) only need to translate source code to Bitcode, from there on it works the same regardless the source language as the LLVM toolchain doesn't care if the Bitcode was generated from C, C++, Obj-C, Rust, Swift or any other source language; once there is Bitcode, the rest of the workflow is always the same.
One benefit of Bitcode is that you can later on generate instructions for another CPU without having to re-compile the original source code. E.g. I may compile a C code to Bitcode and have LLVM generate a running binary for x86 CPUs in the end. If I save the Bitcode, however, I can later on tell LLVM to also create a running binary for an ARM CPU from that Bitcode, without having to compile anything and without access to the original C code. And the generated ARM code will be as good as if I had compiled to ARM from the very start.
Without the Bitcode, I would have to convert x86 code to ARM code and such a translation produces way worse code as the original intent of the code is often lost in the final compilation step to CPU code, which also involves CPU specific optimizations that make no sense for other CPUs, whereas Bitcode retains the original intent pretty well and only performs optimization that all CPUs will benefit from.
Having the Bitcode of all apps allowed Apple to re-compile that Bitcode for a specific CPU, either to make an App compatible with a different kind of CPU or an entirely different architecture or just to benefit from better optimizations of newer compiler versions. E.g. if Apple had tomorrow shiped an iPhone that uses a RISC-V instead of an ARM CPU, all apps with Bitcode could have been re-compiled to RISC-V and would natively support that new CPU architecture despite the author of the app having never even heard of RISC-V.
I think that was the idea why Apple wanted all Apps in Bitcode format. But that approach had issues to begin with. One issue is that Bitcode is not a frozen format, LLVM updates it with every release and they do not guarantee full backward compatibility. Bitcode has never been intended to be a stable representation for permanent storage or archival. Another problem is that you cannot use assembly code as no Bitcode is emitted for assembly code. Also you cannot use pre-built third party libraries that come without Bitcode.
And last but not least: AFAIK Apple has never used any of the Bitcode advantages so far. Despite requiring all apps to contain Bitcode in the past, the apps also had to contain pre-build fat binaries for all supported CPUs and Apple would always only just ship that pre-build code. E.g. for iPhones you used to once have a 32 Bit ARMv7 and a 64 Bit ARM64 version, as well as the Bitcode and during app thinning, Apple would remove either the 32 Bit or the 64 Bit version, as well as the Bitcode, and then ship whats left over. Fine, but they could have done so also if no Bitcode was there. Bitcode is not required to thin out architectures of a fat binary!
Bitcode would be required to re-build for a different architecture but Apple has never done that. No 32 Bit app magically became 64 bit by Apple re-compiling the Bitcode. And no 64 bit only app was magically available for 32 bit systems as Apple re-compiled the Bitcode on demand. As a developer, I can assure you, the iOS App Store always delivered exactly the binary code that you have built and signed yourself and never any code that Apple has themselves created from the Bitcode, so nothing was server side optimized. Even when Apple switched from Intel to M1, no macOS app magically got converted to native ARM, despite that would have been possible for all x86 apps in the app store for that Apple had the Bitcode. Instead Apple still shipped the x86 version and let it run in Rosetta 2.
So imposing various disadvantages onto developers by forcing all code to be available as Bitcode and then not using any of the advantages Bitcode would give you kinda makes the whole thing pointless. And now that all platforms migrated to ARM64 and in a couple of years there won't even be fat binaries anymore (once x86 support for Mac has been dropped), what's the point of continuing with that stuff? I guess Apple took the chance to bury that idea once and for all. Even if they one day add RISC-V to their platforms, developers can still ship fat binaries containing ARM64 and RISC-V code at the same time. This concept works well enough, is way simpler, and has no downsides other than "bigger binaries" and that's something server side app thinning can fix, as during download only the code for the current platform needs to be included.

Apple Watch Series 3 was the last device to not support 64-bit. (i.e. i386 or armv7)
Apple has now stopped supporting the Apple Watch Series 3. [1] They would have been happy to drop support for bitcode.
[1] https://www.xda-developers.com/watchos-9-not-coming-apple-watch-series-3

xcode remove armv7/armv7s/i386 targets support. bitcode use for build different cpu targets. but now all devices might be arm64 . and now no more developer use this tech. so deprecated maybe a wise choice

Bitcode was always pointless, as even if you compiled bitcode to another architecture, there's high chance it won't actually work because the ABI is different. For example, when you compile C program, the libc headers actually are different for every architecture. I'm glad they are finally getting rid of it, as it caused more problems than solved. At most they could've done is re-optimize the binary for the same architecture, or similar enough architecture. There is also the problem of unwanted symbols leaking in bitcode builds, so you either have to rename/obfuscate those or get hit by collisions (big problem if you are a library/framework vendor).

Related

Will Apple re-run macros when using bitcode?

With bitcode, Apple say they can re-build my application on demand, whenever they think it is necessary.
If my source code contains preprocessor macros, when will those run?
When I build and archive my IPA locally? Or also when Apple re-builds the app?
I'm considering both custom macros, as well as built-in ones such as __DATE__ and __TIME__. Which date/time will it get if Apple re-builds the app in the app store?
No; Apple will have a 32 bit version and a 64 bit version for your app in bit code; that's all. All the #define's will be evaluated by your compiler and not be sent to Apple.
With what they have Apple can easily produce a new version of your app that will run on new versions of the ARM processor. They most likely could build a new version that would run on an Intel processor instead of an ARM processor (while their instruction set and implementation are very different, how these chips behave is actually very similar). Building a version for a little-endian PowerPC would be possible; a version for big-endian PowerPC would quite likely not work if your app includes code that needed to be different on a big-endian processor.
__DATE__ and __TIME__ would be the ones that were valid when you built the app.
The result of Apple building a new version for a new processor from bitcode should be exactly the same as if you had submitted the app with code for that processor. Obviously you don't have a compiler for ARM13 which will be introduced in 2022, but if Apple builds that version from your submitted bit code, it should be as if you had had that compiler today.

How 32bit architecture is differ from 64bit architecture mainly in form of app speed and memory management?

As per my knowledge, OS architecture is generally used to speed up our OS and adding new features with higher memory management but in IOS i am little bit confuse regarding architecture which we generally set in our app is as below
Architectures - Standard Architecture (armv7,arm64) Valid Architectures - armv7,arm64,armv7s.
Due to this, we are getting many warnings related to datatypes size and conversation because 64-bit architecture is the use of processors that have datapath widths, integer size, and memory address widths of 64 bits.
so my question is I want to understand what mechanism will work behind this while I generating IPA file for 32 bit supported architecture or 64 bit architecture (I know now after XCode-6 we will only build our app with 64-bit architecture with bitcode enabled in our app for thining our app size)
Can anyone help me with this to understand architecture mechanism, especially in IOS?
Your help will be appreciated.
There are two architecture settings in an iOS project:
Architectures
Valid Architectures
The list of Valid Architectures constrains the possible values in the Architectures list.
When building for debugging on a device, Xcode will only build for the architecture of the target device (which may be x86, for the simulator). If the target device is a 32bit architecture, you'll get a 32-bit build.
When building for some kind of release (ad-hoc or App Store), Xcode will build for all the architectures listed in the build setting's Architecture list. The app binary, along with any dynamically-loaded frameworks will have a slice for each architecture.
Original Link: http://iossupportmatrix.com/
To add on to what Avi said, I hope this picture will give you a better understanding of how OS are evolving. The more right you go, the more the OS can handle information (it can handle more RAM) and some application require more RAM to run. I wish I could give you more specific information about this but I wouldn't want to say something wrong.

iOS arm64 project with 32 bit third party library

As you may know, 1 February 2015 apple pushes us to build projects both 64 and 32 bit supports. However, our projects use third party library which does not include arm64 slice. Is there any way that i can add exception for 32 bit library to build project as arm64.
For example: iOS projects that use ARC system can make exception for single file which does not use ARC system by settings compiler flags with -fno-objc-arc.
Thanks
The answer is quite simple: unfortunately no. To have a 64-bit binary app you have to recompile all thirdy part libraries used in your project.
This mean that if you don't have the source code of these libraries and the authors didn't release a 64-bit version of the lib, your app will never been compliant for 64-bit requirements.

Is there a way to detect VFP/NEON/Thumb/... on iOS at runtime?

So it's fairly easy to figure out what kind of CPU an iOS device runs by querying sysctlbyname("hw.cpusubtype", ...), but there seems to be no obvious way to figure out what features the CPU actually has (think VFP, NEON, Thumb, ...). Can someone think of a way to do this?
Basically, what I need is something similar to getauxval(AT_HWCAP) on Linux/Android, which returns a bit mask of features supported by the CPU.
A few things to note:
The information must be retrieved at runtime from the OS. No preprocessor defines.
Fat binaries is not a solution. I really do need to know this stuff in an ARM v6 binary.
Thanks in advance!
sysctlbyname has “hw.optional.neon”. I do not see a name for VFP, except “hw.optional.vfp_shortvector”, which is a deprecated feature.
Do a matrix float multiplaction via accelerate.framework and measure the execution time. The difference will be huge enough between Neon and VFP driven math, you simply cannot miss.
Thumb is always there, and the presence of NEON means armv7= Thumb2.
First, consider carefully whether or not you really need to support armv6 binaries for iOS. According to published version share statistics, something like 98.5% of iOS devices are running iOS 5.0 or later, which does not support armv6 devices (armv6 binaries will still run on current iOS versions, obviously, but all new apps should really be targeting armv7; there’s basically zero reason for your customers to be shipping armv6 binaries for iOS today).
Similarly, your concerns about code size are misplaced. If you provide a fat library, and your customer builds an armv6 binary against it, only the armv6 bits of your library will be built into their application. Furthermore, code size is usually a nearly trivial fraction of application bundle size; most of the size of an application comes from other resources.
Ok. All that aside, if you really want to pursue this: VFP and thumb are supported on all iOS devices, so there’s no need to check for support. You can check for NEON and thumb-2 using the method that Eric Postpischil suggested (all armv7 iOS devices have NEON support, so availability of NEON coincides exactly with availability of thumb-2).

Is there a way to compile for ARM rather than Thumb in Xcode 4?

Apple is recommending to compiling for ARM rather than thumb if there are many floating point operations going on. My whole app is almost one big floating point operation.
Here's what they say in the iOS App Development Workflow Guide:
iOS devices support two instruction sets, ARM and Thumb. Xcode uses
Thumb instructions by default because using Thumb typically reduces
code size by about 35 percent relative to ARM. Applications that have
extensive floating-point code might perform better if they use ARM
instructions rather than Thumb. You can turn off Thumb for your
application, so it compiles for ARM, by setting the Compile for Thumb
build setting to No.
However, I cannot find any "Compile for Thumb" setting in my build settings. Did they rename it? Or is this unavailable now with Xcode 4?
First, the advice to not compile for the Thumb instruction set in order to improve floating point performance only really applies to the old ARMv6 devices.
ARMv7 hardware (iPhone 3G S and newer, including all iPads) uses the more efficient Thumb-2 instruction set, which does not suffer the same sort of floating point slowdowns. For ARMv7 builds, it is recommended in almost all cases that you build for Thumb. I provide a little more detail about this in my answer here.
This might be why this compiler setting is no longer exposed as a common option, because ARMv7 devices are the vast majority of iOS devices out there.
If you want to do this for just your ARMv6 builds, you can go to your build settings and mouse over the "Other C Flags" option. Click on the little plus button that appears to the right of this option and add a condition for the ARMv6 architecture. Do this again to create one for the ARMv7 architecture. Under the ARMv6 architecture, add the extra compiler flag of -mno-thumb (as Kevin suggests).
You should end up with something that looks like the following:
I do this in one of my applications, because I did see a performance boost on the older ARMv6 devices with that. However, another of my applications was slower when not building for Thumb on ARMv6, so you'll want to profile this first.
Additionally, there is currently a bug in the LLVM Compiler 3.0 that ships with Xcode 4.2 (which has since been fixed in 4.2.1, from what I hear) where floating point calculations are compiled wrong under Thumb for ARMv6. If you're using that particular version of Xcode, you'll need to do this for proper behavior on the older devices.
I don't know whether or not "Compile for Thumb" is supposed to exist in Xcode 4, but you can always add -mno-thumb to the Other C Flags build setting.
Regarding your original question: I have noticed that "Compile for Thumb" (under the section "Code Generation" of your "Project Build Settings") in Xcode 4.2.1 is only available if you are using LLVM GCC 4.2 (if set in "Compiler for C/C++/Objective-C")!
If compiling with Apple LLVM 3.0 then you will find no "Compile for Thumb" option. But - as Brad already said - you can still change the "Other C Flags" option to turn off Thumb mode.
Another interesting point: I am using the sqlite amalgamation source in my project (i need fts - full text search) and since compiling with LLVM 3.0 I had strange and rather random crashes on armv6 devices whenever accessing the database: as it turns out that was because of Thumb mode not disabled when compiling for armv6 devices.

Resources