Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate IR for pointer conversions to sink addresses #1021

Merged
merged 5 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Library/Hylo/Core/PointerToMutable.hylo
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,15 @@ public conformance PointerToMutable: Equatable {
}

}

public extension PointerToMutable where Pointee: Movable {

/// Returns the value at the address represented by `self`, leaving the storage at this address
/// uninitialized.
///
/// Note: This method should be deprecated once nonmutating subscripts are implemented.
public fun unsafe_pointee() -> Pointee {
return base as* (remote sink Pointee)
}

}
36 changes: 28 additions & 8 deletions Sources/IR/Emitter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,7 @@ struct Emitter {
case .down:
emitStore(downcast: e, to: storage)
case .pointerConversion:
unreachable("pointer to address conversion evaluates to a lvalue")
emitStore(pointerConversion: e, to: storage)
}
}

Expand Down Expand Up @@ -1161,6 +1161,21 @@ struct Emitter {
UNIMPLEMENTED()
}

/// Inserts the IR for storing the value of `e` to `storage`.
private mutating func emitStore(pointerConversion e: CastExpr.ID, to storage: Operand) {
let x0 = emitLValue(pointerConversion: e)

// Consuming a pointee requires a conformance to `Movable`.
let target = RemoteType(canonical(program[e].type))!
let movable = program.ast.movableTrait
if !program.conforms(target.bareType, to: movable, in: insertionScope!) {
report(.error(module.type(of: x0).ast, doesNotConformTo: movable, at: ast[e].site))
return
}

emitMove([.inout, .set], x0, to: storage, at: ast[e].site)
}

/// Inserts the IR for storing the value of `e` to `storage`.
private mutating func emitStore(_ e: ConditionalExpr.ID, to storage: Operand) {
let (success, failure) = emitTest(condition: ast[e].condition, in: AnyScopeID(e))
Expand Down Expand Up @@ -2072,18 +2087,23 @@ struct Emitter {
private mutating func emitLValue(_ e: CastExpr.ID) -> Operand {
switch ast[e].direction {
case .pointerConversion:
let x0 = emitLValue(ast[e].left)
let x1 = insert(module.makeAccess(.sink, from: x0, at: ast[e].site))!
let x2 = insert(module.makeLoad(x1, at: ast[e].site))!
insert(module.makeEndAccess(x1, at: ast[e].site))
let target = RemoteType(canonical(program[e].type))!
return insert(module.makePointerToAddress(x2, to: target, at: ast[e].site))!

return emitLValue(pointerConversion: e)
default:
UNIMPLEMENTED()
}
}

/// Inserts the IR for lvalue `e`.
private mutating func emitLValue(pointerConversion e: CastExpr.ID) -> Operand {
let x0 = emitLValue(ast[e].left)
let x1 = insert(module.makeAccess(.sink, from: x0, at: ast[e].site))!
let x2 = insert(module.makeLoad(x1, at: ast[e].site))!
insert(module.makeEndAccess(x1, at: ast[e].site))

let target = RemoteType(canonical(program[e].type))!
return insert(module.makePointerToAddress(x2, to: target, at: ast[e].site))!
}

/// Inserts the IR for lvalue `e`.
private mutating func emitLValue(_ e: InoutExpr.ID) -> Operand {
emitLValue(ast[e].subject)
Expand Down
10 changes: 9 additions & 1 deletion Sources/IR/Mangling/Mangler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,17 @@ struct Mangler {
/// Writes the mangled representation of `c` to `output`.
private mutating func write(constraint c: WhereClause.ConstraintExpr, to output: inout Output) {
switch c {
case .conformance, .value:
case .value:
UNIMPLEMENTED()

case .conformance(let lhs, let rhs):
write(operator: .conformanceConstraint, to: &output)
mangle(type: program[lhs].type, to: &output)
write(integer: rhs.count, to: &output)
for t in rhs {
mangle(type: program[t].type, to: &output)
}

case .equality(let lhs, let rhs):
write(operator: .equalityConstraint, to: &output)
mangle(type: program[lhs].type, to: &output)
Expand Down
8 changes: 2 additions & 6 deletions Sources/IR/Operands/Instruction/PointerToAddress.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,10 @@ extension Module {

/// Creates a `pointer_to_address` anchored at `site` that converts `source`, which is a
/// built-in pointer value, to an address of type `target`.
///
/// - Requires: `target.access` is `.let`, `.inout`, or `.set`
func makePointerToAddress(
_ source: Operand,
to target: RemoteType,
at site: SourceRange
_ source: Operand, to target: RemoteType, at site: SourceRange
) -> PointerToAddress {
precondition(AccessEffectSet([.let, .inout, .set]).contains(target.access))
precondition(target.access != .yielded)
return .init(source: source, target: target, site: site)
}

Expand Down
9 changes: 9 additions & 0 deletions Tests/LibraryTests/TestCases/PointerToMutableTests.hylo
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,16 @@ fun test_advance() {
precondition(!(f == e))
}

fun test_pointee() {
let p = PointerToMutable<Int>.allocate(count: 1)
p.unsafe_initialize_pointee(fun (_ i: set Int) -> Void { &i = 42 })
let y = p.unsafe_pointee()
precondition(42 == y)
p.deallocate()
}

public fun main() {
test_advance_by_bytes()
test_advance()
test_pointee()
}