https://kotlinlang.org logo
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
benchmarks
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
confetti
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
lincheck
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by
Title
l

Landry Norris

02/18/2023, 10:57 PM
Would it be possible for K/N to have a pure bitcode target? From my very limited understanding of LLVM, it sounds like the main platform-specific parts of bitcode are in the form of linking external symbols, which may not be available on all platforms. It seems like it should be possible to compile and publish a library as raw bitcode, which would be helpful as new targets get added in the future.
It seems like something like this would be crucial to the eventual goal of custom cross-compilation. If we were able to customize the compilation, there likely won't be any published libraries that are compatible.
When I did some experimenting with the cli compiler a while back, I saw that there's a flag that produces raw bitcode, which we presumably could pass to a compatible LLVM, then link against a custom built K/N runtime, but I never tested this theory.
a

andylamax

02/18/2023, 11:12 PM
I am no expert, but aren't you talking about klib??
l

Landry Norris

02/18/2023, 11:14 PM
I believe the klib format packages bitcode, but it currently is tied to a specific target, such as iosArm64, linuxX64, etc. As far as I'm aware, there's not a way to build a klib that's just bitcode with no specific target and publish it. The idea being that any K/N target could consume this klib.
e

ephemient

02/18/2023, 11:25 PM
LLVM IR is not meant to be cross-platform. all sorts of platform-specific ABI such as integer sizes and calling conventions leak into it.
a

andylamax

02/18/2023, 11:26 PM
I get your point now. But while it is something I desire, having an abstract bitcode (one that will work for all targets) might be just too much abstraction as some platforms are really very different (i.e. JS and ios). How would you have bitcode that works same for both???
l

Landry Norris

02/18/2023, 11:30 PM
JS would not be covered by LLVM bitcode. I'm more so wondering if there's a good way to make sure libraries are compatible with future K/N targets. Right now, when a new target is added (like the new watch target in 1.8.0), libraries have to be updated to support them.
e

ephemient

02/18/2023, 11:30 PM
https://youtrack.jetbrains.com/issue/KT-52666 is perhaps what you are looking for
l

Landry Norris

02/18/2023, 11:31 PM
Since bitcode requires knowledge of calling conventions, pure bitcode may not be the solution
e

ephemient

02/18/2023, 11:31 PM
I believe the long-term plan is to ship the Kotlin compiler's IR
there is no existing IR that is suitable
l

Landry Norris

02/18/2023, 11:33 PM
I remember talk about a longer term goal of allowing custom cross compilation (I may be thinking of KT-43974). If this is added, I don't see how you'd be able to use existing libraries without some form of IR publishing.
t

Troels Lund

02/20/2023, 9:02 AM
The documentation gives you some answers here: https://kotlinlang.org/docs/native-libraries.html#library-format From the above, klibs contain both platform specific bitcode and platform independent Kotlin IR. The IR is generated from Kotlin source, while the bitcode is from any interop sources.
n

napperley

02/20/2023, 7:55 PM
Hypothetically if Kotlin Native had some Bare Metal targets (eg RP2040 - https://www.raspberrypi.com/documentation/microcontrollers/rp2040.html ), how would the bit code library stuff work on these targets? There is a reason the bit code is target specific.
e

ephemient

02/20/2023, 11:04 PM
not sure what the the issue with bare metal is. the common stdlib doesn't have I/O or other OS interaction. you need a memory allocator, but you can't run Kotlin without one, regardless of stdlib or not
l

Landry Norris

02/20/2023, 11:12 PM
I'd assume any type of 'target independent' IR would not allow you to depend on a library that defines just a set of targets (likely no okio or compose for the reasons mentioned above). It would mainly have value for libraries such as unit conversion, text processing, math, and other more abstract concepts. It would be nice to have these libraries without having to build them with new targets yourself.
n

napperley

02/21/2023, 12:14 AM
Isn't the K/N memory allocator separate from the Kotlin Std lib?
l

Landry Norris

02/21/2023, 12:16 AM
The memory allocator is part of the K/N runtime
n

napperley

02/21/2023, 12:16 AM
I thought that would be the case.
On the K/N side what is part of the Kotlin Std lib (as a summary)? Presumably there isn't anything in the Std lib that would prevent it being used with a Bare Metal target (as a show stopper). Would be good if a member of the Kotlin team can confirm this.
l

Landry Norris

02/21/2023, 12:20 AM
https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib I believe most of the stdlib is multiplatform now. K/N hasn't had its own stdlib in a little while.
n

napperley

02/21/2023, 12:25 AM
Is K/N's version of the Std lib the Common module version?
t

Troels Lund

02/21/2023, 12:21 PM
Not entirely. There are parts of the standard library that is only available in K/N, like <https:kotlin.native|kotlin.native>, kotlin.native.concurrent, and kotlin.native.ref. Most of these exist because the programming model has to be slightly different for native targets, but otherwise only expose functionality that is otherwise available in a slightly different form for other targets.
l

Landry Norris

02/21/2023, 2:18 PM
Is the kotlin.native package part of the stdlib? I'd assumed it was a separate endorsed library.
e

ephemient

02/21/2023, 2:20 PM
there are several platform-specific parts of stdlib. it's all in the docs.
for example, you can see that https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.system/ is available on JVM and native, but not JS
the colors tell you what's available where
n

napperley

02/22/2023, 12:31 AM
Are the K/N parts of the Std lib (kotlin.native, kotlin.native.concurrent, and kotlin.native.ref) OS agnostic (i.e. don't depend on an OS)?
t

Troels Lund

02/22/2023, 7:14 AM
They should behave the same on all K/N platforms, if that's what you're asking.
n

napperley

02/22/2023, 9:11 PM
Not exactly the answer I was after. Was wanting to know if any parts of the Kotlin Std lib depend on using an OS.
e

ephemient

02/22/2023, 9:12 PM
that is not relevant to the original topic anyway. if it's in stdlib, it's an external call from the klib regardless of how it's implemented
but just look at the APIs
l

Landry Norris

02/22/2023, 9:12 PM
If you're looking to run bare metal, it's going to be far from easy. The OS provides libunwind, libpthread, and several other hard to write libraries.
e

ephemient

02/22/2023, 9:13 PM
they presume an environment where dynamic memory allocation, atomics, and threads are possible
l

Landry Norris

02/22/2023, 9:14 PM
Kotlin 1.6 really only needed libunwind if you used the experimental stms gc, but that's sadly not the case anymore. Until they make a more minimal runtime, you need an OS.
e

ephemient

02/22/2023, 9:14 PM
do you count whatever supplies that as an OS? eh…
l

Landry Norris

02/22/2023, 9:14 PM
It's part of an OS.
e

ephemient

02/22/2023, 9:17 PM
it's certainly possible to implement those on raw UEFI
whether you count that as an OS or not is a bit arguable
n

napperley

02/22/2023, 9:18 PM
Do note that UEFI is only applicable to AMD/Intel based hardware.
l

Landry Norris

02/22/2023, 9:19 PM
Ironically it's more than anyone on the OSDev wiki ever does. It's hard to find a reference OS there with libunwind support. They always say to just use a 'real OS' at that point.
e

ephemient

02/22/2023, 9:19 PM
UEFI is part of ARM BBR as well
n

napperley

02/22/2023, 9:20 PM
The libunwind lib is only applicable to CPP.
e

ephemient

02/22/2023, 9:23 PM
the Kotlin/Native runtime uses C++, and needs some form of tracing over Kotlin frames as well
n

napperley

02/22/2023, 9:23 PM
What do you mean about Kotlin frames?
l

Landry Norris

02/22/2023, 9:24 PM
The symbols in libunwind are needed for K/N. I tried building a K/N binary for bare metal back in 1.6. I got close, but decided to wait until the new gc was stable.
Frames are part of the ABI. It's basically everything that gets pushed to the stack on a method call.
Return address, params, etc
e

ephemient

02/22/2023, 9:26 PM
I think it should be possible to replace libunwind with any other backtracer appropriate to the environment. I'm not sure if it can be stubbed out completely…
but that's also not part of the OS (unless you count SEH as part of the OS)
l

Landry Norris

02/22/2023, 9:27 PM
Some osdev examples stub out libunwind, but they often use languages where you can disable exceptions. The kotlin team has been very opposed to this idea.
n

napperley

02/22/2023, 9:27 PM
In the Kotlin code base there is an option to disable back traces on a per target basis which should get around the libunwind issue.
l

Landry Norris

02/22/2023, 9:27 PM
OS is often a hard term to define.
libunwind is also needed for exception support at all, not just getting the trace.
It may be possible to replace libunwind if you rebuild the K/N runtime (or just not use unwinding at all), but the official runtime needs the symbols for it.
Some day when I have time (and can focus on one side project), I'd like to make a more minimal runtime without every feature and try to have the compiler output bitcode, then use llc to compile to binary, but that'd be a lot of work.
n

napperley

02/22/2023, 9:38 PM
The libunwind lib appears to be standard across ALL Bare Metal ARM targets in the official ARM GCC toolchain (there are two files: unwind.h & unwind-arm-common.h).
e

ephemient

02/22/2023, 9:39 PM
libunwind and libgcc unwind are two different things
also there's the one in llvm
n

napperley

02/22/2023, 9:41 PM
Some of the K/N targets use a GCC based tool chain (eg the unofficial Zephyr based target - uses the official ARM GCC tool chain ).
By the way the libunwind lib is available for some Bare Metal ARM targets (eg ARMv6-m - https://reviews.llvm.org/D22292?id=63759 ).