Skip to content

Commit

Permalink
Ensure built-in wrappers have the expected alignment
Browse files Browse the repository at this point in the history
This commit is implementing a hack to make sure built-in wrappers
(e.g., `Int8`) have the alignment of the value they wrap.
  • Loading branch information
kyouko-taiga committed Oct 5, 2023
1 parent 253fa8e commit 0ad8afc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
13 changes: 10 additions & 3 deletions Sources/CodeGen/LLVM/ConcreteTypeLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@ struct ConcreteTypeLayout {
///
/// - Requires: `l.type` is representable in LLVM.
init(_ l: AbstractTypeLayout, definedIn ir: IR.Program, forUseIn m: inout LLVM.Module) {
if let u = BuiltinType(l.type) {
self.init(of: u, forUseIn: &m)
} else {
switch l.type.base {
case let t as BuiltinType:
self.init(of: t, forUseIn: &m)

case is ProductType where l.properties.count == 1:
// Note: Hack to get the correct alignment of built-in wrappers until we implement a proper
// layout algorithm (#1067).
self.init(of: l.properties[0].type, definedIn: ir, forUseIn: &m)

default:
let u = ir.llvm(l.type, in: &m)
self.init(size: m.layout.storageSize(of: u), alignment: m.layout.preferredAlignment(of: u))
}
Expand Down
3 changes: 3 additions & 0 deletions Tests/EndToEndTests/TestCases/MemoryLayout.hylo
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ type Vector2 {
public fun main() {
let x = Hylo.MemoryLayout<Vector2>.stride()
precondition(x == 16)

precondition(Hylo.MemoryLayout<Int8>.alignment() == 1)
precondition(Hylo.MemoryLayout<Int32>.alignment() == 4)
}

0 comments on commit 0ad8afc

Please sign in to comment.