Skip to content

Implement Builtin.freeze for integer and integer-vector types. #73519

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

Merged
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
2 changes: 2 additions & 0 deletions include/swift/AST/Builtins.def
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ BUILTIN_UNARY_OPERATION(FNeg, "fneg", "n", FloatOrVector)
BUILTIN_UNARY_OPERATION(AssumeNonNegative, "assumeNonNegative", "n", Integer)
// It only works on i1.
BUILTIN_UNARY_OPERATION(AssumeTrue, "assume", "", Integer)
// Converts poison/undef to an indeterminate but valid value.
BUILTIN_UNARY_OPERATION(Freeze, "freeze", "n", IntegerOrVector)

// Binary predicates have type (T,T) -> i1 or (T, T) -> Vector<i1> for scalars
// and vectors, respectively.
Expand Down
3 changes: 3 additions & 0 deletions lib/IRGen/GenBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,9 @@ void irgen::emitBuiltinCall(IRGenFunction &IGF, const BuiltinInfo &Builtin,
// Don't generate any code for the builtin.
return out.add(v);
}
if (Builtin.ID == BuiltinValueKind::Freeze) {
return out.add(IGF.Builder.CreateFreeze(args.claimNext()));
}

if (Builtin.ID == BuiltinValueKind::AllocRaw) {
auto size = args.claimNext();
Expand Down
1 change: 1 addition & 0 deletions lib/SIL/IR/OperandOwnership.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,7 @@ BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, GenericFRem)
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, FSub)
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, GenericFSub)
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, Fence)
BUILTIN_OPERAND_OWNERSHIP(TrivialUse, Freeze)
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, Ifdef)
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, GetObjCTypeEncoding)
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, ICMP_EQ)
Expand Down
1 change: 1 addition & 0 deletions lib/SIL/IR/ValueOwnership.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ CONSTANT_OWNERSHIP_BUILTIN(None, FRem)
CONSTANT_OWNERSHIP_BUILTIN(None, GenericFRem)
CONSTANT_OWNERSHIP_BUILTIN(None, FSub)
CONSTANT_OWNERSHIP_BUILTIN(None, GenericFSub)
CONSTANT_OWNERSHIP_BUILTIN(None, Freeze)
CONSTANT_OWNERSHIP_BUILTIN(None, ICMP_EQ)
CONSTANT_OWNERSHIP_BUILTIN(None, ICMP_NE)
CONSTANT_OWNERSHIP_BUILTIN(None, ICMP_SGE)
Expand Down
52 changes: 52 additions & 0 deletions test/IRGen/builtin_freeze.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// RUN: %target-swift-frontend -O -module-name builtin_freeze -enable-experimental-feature BuiltinModule -primary-file %s -emit-ir -o - | %FileCheck %s --check-prefix=CHECK

import Builtin

func fptosi(_ x: Float) -> Int32 {
Int32(Builtin.fptosi_FPIEEE32_Int32(x._value))
// CHECK: fptosi float %{{.+}} to i32
}

func fptosiWithFreeze(_ x: Float) -> Int32 {
Int32(Builtin.freeze_Int32(Builtin.fptosi_FPIEEE32_Int32(x._value)))
// CHECK: fptosi float %{{.+}} to i32
// CHECK-NEXT: freeze i32 %{{.+}}
}

func yuck() -> Int32 {
fptosi(0x1.0p32)
// CHECK: poison
}

func yum() -> Int32 {
fptosiWithFreeze(0x1.0p32)
// CHECK-NOT: poison
}

func fptosi(_ x: SIMD2<Float>) -> SIMD2<Int32> {
let maybePoison = Builtin.fptosi_Vec2xFPIEEE32_Vec2xInt32(x._storage._value)
var result = SIMD2<Int32>()
result._storage._value = maybePoison
return result
// CHECK: fptosi <2 x float> %{{.+}} to <2 x i32>
}

func fptosiWithFreeze(_ x: SIMD2<Float>) -> SIMD2<Int32> {
let maybePoison = Builtin.fptosi_Vec2xFPIEEE32_Vec2xInt32(x._storage._value)
let frozen = Builtin.freeze_Vec2xInt32(maybePoison)
var result = SIMD2<Int32>()
result._storage._value = frozen
return result
// CHECK: fptosi <2 x float> %{{.+}} to <2 x i32>
// CHECK-NEXT: freeze <2 x i32> %{{.+}}
}

func doubleYuck(_ x: SIMD2<Float>) -> SIMD2<Int32> {
fptosi(SIMD2<Float>(repeating: 0x1.0p32))
// CHECK: poison
}

func DoubleYum(_ x: SIMD2<Float>) -> SIMD2<Int32> {
fptosiWithFreeze(SIMD2<Float>(repeating: 0x1.0p32))
// CHECK-NOT: poison
}