Have a question regarding the content of an article about SIL

Hello, I am Kobe from South Korea, currently studying iOS and Swift.:man_bowing:t2:

I am writing this post because I have a question regarding the content of an article about SIL (Swift Intermediate Language) on a Korean tech blog, which seems different from my understanding.

The blog mentioned the following:

"SIL appears to be a language close to the static single assignment form, allowing only constants. Does this mean that 'var' in Swift is represented as 'let' in SIL? Initially, I thought so, but it turns out SIL is designed differently from Swift."

However, I have two pieces of evidence that lead me to believe otherwise:

1. Contradictory Results from Converting .swift to .sil Files

I created two test cases using variables and constants and converted a .swift file to a .sil file, which yielded results contrary to the blog post.

// MARK: - .swift
// first
let first: Int = 1 + 2
// second
var second: Int = 3 + 4

// MARK: - .sil
// first
sil_global hidden [let] @$s4main5firstSivp : $Int
// second
sil_global hidden @$s4main6secondSivp : $Int

// MARK: - rawSIL
@_hasStorage @_hasInitialValue let first: Int { get }
@_hasStorage @_hasInitialValue var second: Int { get set }

As seen in the code snippet above, SIL clearly differentiates between constants and variables based on the presence or absence of '[let]'.

I believe "Based on this code, SIL does distinguish between constants and variables."

2. Contradictory Information in Apple's Repository Swift Docs

A particular part of the blog post caught my attention:

"Does using 'var' in Swift change to 'let' in SIL? This was speculated, but apparently, SIL is designed differently from Swift."

My interpretation of this is:

"In Swift, var is used for mutable variables and let for immutable ones. However, this doesn't mean that a var in Swift code acts like a let in SIL. Rather, it means that the assignment and usage of variables in SIL could be handled in a manner close to SSA form."

But, if you refer to the first point in APPLE - SILGen, it states:

"Variables are represented not by strict SSA form but by loading and storing to mutable memory locations, similar to the initial alloca-heavy LLVM IR emitted by frontends like Clang. However, in Swift, variables are most commonly represented as reference-counted 'boxes', which can be retained, released, and captured by closures.”

I believe "Based on this documentation, SIL does not follow the SSA form."

Considering these two arguments and pieces of evidence, I hold a view contrary to the tech blog post.

I could be wrong, the blog could be wrong, or maybe a combination of both is correct.

Therefore, I seek your help and knowledge in this matter.

Thank you for taking the time to read my post, and I would greatly appreciate any insights into which knowledge is correct.

Sincerely from Korea,

Kobe, November 21, 2023.

This post is a question for Swift users around the world.


Hello, I am Kobe from South Korea, currently studying iOS and Swift.:man_bowing:t2:

I am writing this because I came across an article on a Korean tech blog about SIL (Swift Intermediate Language) that seems to contradict my understanding.

The blog stated, "SIL appears to be a language akin to the static single assignment form, allowing only constants. This raises the question, does 'var' in Swift get translated to 'let' in SIL? Initially, I thought so, but apparently, SIL is designed differently from Swift."

However, I have two pieces of evidence that suggest a contrary view:

1. Contradictory Results from Converting .swift Files to .sil

I created two test cases using variables and constants and converted them from .swift to .sil, which yielded results different from the blog post.

// MARK: - .swift
// first
let first: Int = 1 + 2
// second
var second: Int = 3 + 4

// MARK: - .sil
// first
sil_global hidden [let] @$s4main5firstSivp : $Int
// second
sil_global hidden @$s4main6secondSivp : $Int

As the code snippet shows, SIL clearly differentiates between constants and variables with the presence or absence of '[let]'.

I believe "Based on this code, SIL indeed distinguishes between constants and variables."

2. Contradictory Information in Apple's Repository Swift Docs

A specific part of the blog post caught my attention:

**"It was speculated whether using 'var' in Swift

translates to 'let' in SIL, but it turns out SIL is designed separately from Swift."**

My interpretation is as follows:

"In Swift, var is used for mutable variables, and let for immutable ones. However, this doesn't imply that a var in Swift code behaves like a let in SIL. Instead, it suggests that the assignment and usage of variables in SIL might be treated in a manner akin to the SSA form."

But, as per the first point in APPLE - SILGen, it is written:

"Variables are represented not strictly in SSA form but by loading and storing to mutable memory locations, similar to the initial LLVM IR with heavy use of alloca, emitted by frontends like Clang. In Swift, however, variables are most commonly represented as reference-counted 'boxes', which can be retained, released, and captured in closures.”

I believe "Based on this documentation, SIL does not adhere strictly to the SSA form."

With these two arguments and pieces of evidence, I stand in opposition to the tech blog's post.

I might be wrong, the blog might be wrong, or maybe a combination of both is correct.

Thus, I am seeking your help and knowledge in this matter.

Thank you for reading my post, and any insights into which understanding is correct would be greatly appreciated.

Sincerely from Korea,

Kobe, November 21, 2023.