Skip to content

Commit d4d5228

Browse files
committed
Allow constant folding IntToPtr builtin (make it isValidInStaticInitializerOfGlobal)
1 parent f53be47 commit d4d5228

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

SwiftCompilerSources/Sources/SIL/GlobalVariable.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ extension Instruction {
8282
return type.isBuiltinInteger || type.isBuiltinFloat
8383
case .PtrToInt:
8484
return bi.operands[0].value is StringLiteralInst
85+
case .IntToPtr:
86+
return bi.operands[0].value is IntegerLiteralInst
8587
case .StringObjectOr:
8688
// The first operand can be a string literal (i.e. a pointer), but the
8789
// second operand must be a constant. This enables creating a

lib/IRGen/GenConstant.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ Explosion irgen::emitConstantValue(IRGenModule &IGM, SILValue operand,
302302
auto *ptr = emitConstantValue(IGM, args[0]).claimNextConstant();
303303
return llvm::ConstantExpr::getPtrToInt(ptr, IGM.IntPtrTy);
304304
}
305+
case BuiltinValueKind::IntToPtr: {
306+
auto *num = emitConstantValue(IGM, args[0]).claimNextConstant();
307+
return llvm::ConstantExpr::getIntToPtr(num, IGM.Int8PtrTy);
308+
}
305309
case BuiltinValueKind::ZExtOrBitCast: {
306310
auto *val = emitConstantValue(IGM, args[0]).claimNextConstant();
307311
return llvm::ConstantExpr::getZExtOrBitCast(val,

test/SILOptimizer/init_static_globals.sil

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ sil_global private @g1_token : $Builtin.Word
6666
sil_global [let] @g2 : $Int32
6767
sil_global private @g2_token : $Builtin.Word
6868

69+
// CHECK-LABEL: sil_global [let] @g3 : $Optional<UnsafeMutablePointer<Int>>
70+
sil_global [let] @g3 : $Optional<UnsafeMutablePointer<Int>>
71+
sil_global private @g3_token : $Builtin.Word
72+
73+
// CHECK-LABEL: sil_global [let] @g4 : $Optional<UnsafeMutablePointer<Int>>
74+
sil_global [let] @g4 : $Optional<UnsafeMutablePointer<Int>>
75+
sil_global private @g4_token : $Builtin.Word
76+
6977

7078
// CHECK-LABEL: sil [global_init_once_fn] [ossa] @globalinit_trivialglobal_func :
7179
// CHECK-NOT: alloc_global
@@ -151,3 +159,33 @@ bb0:
151159
return %6 : $()
152160
}
153161

162+
// CHECK-LABEL: sil [global_init_once_fn] [ossa] @globalinit_enum :
163+
// CHECK-NOT: alloc_global
164+
// CHECK-NOT: store
165+
// CHECK: } // end sil function 'globalinit_enum'
166+
sil [global_init_once_fn] [ossa] @globalinit_enum : $@convention(c) () -> () {
167+
bb0:
168+
alloc_global @g3
169+
%2 = global_addr @g3 : $*Optional<UnsafeMutablePointer<Int>>
170+
%3 = enum $Optional<UnsafeMutablePointer<Int>>, #Optional.none!enumelt
171+
store %3 to [trivial] %2 : $*Optional<UnsafeMutablePointer<Int>>
172+
%5 = tuple ()
173+
return %5 : $()
174+
}
175+
176+
// CHECK-LABEL: sil [global_init_once_fn] [ossa] @globalinit_enum_inttoptr :
177+
// CHECK-NOT: alloc_global
178+
// CHECK-NOT: store
179+
// CHECK: } // end sil function 'globalinit_enum_inttoptr'
180+
sil [global_init_once_fn] [ossa] @globalinit_enum_inttoptr : $@convention(c) () -> () {
181+
bb0:
182+
alloc_global @g4
183+
%2 = global_addr @g4 : $*Optional<UnsafeMutablePointer<Int>>
184+
%4 = integer_literal $Builtin.Word, 1111638594
185+
%5 = builtin "inttoptr_Word"(%4 : $Builtin.Word) : $Builtin.RawPointer
186+
%6 = struct $UnsafeMutablePointer<Int> (%5 : $Builtin.RawPointer)
187+
%7 = enum $Optional<UnsafeMutablePointer<Int>>, #Optional.some!enumelt, %6 : $UnsafeMutablePointer<Int>
188+
store %7 to [trivial] %2 : $*Optional<UnsafeMutablePointer<Int>>
189+
%10 = tuple ()
190+
return %10 : $()
191+
}

0 commit comments

Comments
 (0)