Skip to content

Commit 114af54

Browse files
committed
[Compile Time Values] Rebase on top of newly-added experimental feature for the '@const' attribute
1 parent b8ef3ff commit 114af54

20 files changed

+181
-176
lines changed

lib/SILOptimizer/Mandatory/DiagnoseUnknownCompileTimeValues.cpp

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ class DiagnoseUnknownCompileTimeValues : public SILModuleTransform {
5858
// compile-time known values
5959
verifyGlobals();
6060

61-
// Verify _const lets appearing as local variables
61+
// Verify @const lets appearing as local variables
6262
verifyLocals();
6363

64-
// For each function call, ensure arguments to _const parameters
64+
// For each function call, ensure arguments to @const parameters
6565
// are all compile-time known values
6666
verifyCallArguments();
6767
}
@@ -95,7 +95,7 @@ class DiagnoseUnknownCompileTimeValues : public SILModuleTransform {
9595
SILGlobalVariable &Global,
9696
VarDecl *Decl) {
9797
LLVM_DEBUG(llvm::dbgs()
98-
<< "_const static let " << Decl->getName().str().str()
98+
<< "@const static let " << Decl->getName().str().str()
9999
<< ": " << Decl->getTypeInContext().getString() << " = ";);
100100
auto StaticInitializerValue = Global.getStaticInitializerValue();
101101
assert(StaticInitializerValue && "Expected a static initializer");
@@ -115,7 +115,7 @@ class DiagnoseUnknownCompileTimeValues : public SILModuleTransform {
115115
void verifyInitializeOnceGlobal(ConstExprFunctionState &ConstExprState,
116116
SILGlobalVariable &Global, VarDecl *Decl) {
117117
LLVM_DEBUG(llvm::dbgs()
118-
<< "_const [init_once] let " << Decl->getName().str().str()
118+
<< "@const [init_once] let " << Decl->getName().str().str()
119119
<< ": " << Decl->getTypeInContext().getString() << " = ";);
120120
SILModule *M = getModule();
121121
for (SILFunction &Fn : *M) {
@@ -164,7 +164,7 @@ class DiagnoseUnknownCompileTimeValues : public SILModuleTransform {
164164
// statically with compile-time known values
165165
for (SILGlobalVariable &G : M->getSILGlobals()) {
166166
if (auto Decl = G.getDecl()) {
167-
if (Decl->getAttrs().getAttribute<CompileTimeConstAttr>()) {
167+
if (Decl->isConstVal()) {
168168
if (G.getStaticInitializerValue())
169169
verifyStaticallyInitializedGlobal(ConstExprState, G, Decl);
170170
else
@@ -176,17 +176,17 @@ class DiagnoseUnknownCompileTimeValues : public SILModuleTransform {
176176

177177
void verifyLocal(DebugValueInst *DBI) {
178178
auto Decl = DBI->getDecl();
179-
if (!Decl || !Decl->isCompileTimeConst())
179+
if (!Decl || !Decl->isConstVal())
180180
return;
181181

182182
auto Value = ConstExprState.getConstantValue(DBI->getOperand());
183183
LLVM_DEBUG(llvm::dbgs()
184-
<< "_const let " << Decl->getName().str().str() << ": "
184+
<< "@const let " << Decl->getName().str().str() << ": "
185185
<< Decl->getTypeInContext().getString() << " = ";);
186186
LLVM_DEBUG(printSymbolicValueValue(Value, Allocator););
187187
if (!Value.isConstant()) {
188188
getModule()->getASTContext().Diags.diagnose(
189-
Decl->getParentInitializer()->getStartLoc(),
189+
Decl->getStartLoc(),
190190
diag::require_const_arg_for_parameter);
191191
}
192192
}
@@ -200,7 +200,7 @@ class DiagnoseUnknownCompileTimeValues : public SILModuleTransform {
200200
}
201201

202202
void verifyCallArguments() {
203-
// Find all calls to functions which have _const parameters
203+
// Find all calls to functions which have @const parameters
204204
for (SILFunction &Fn : *getModule())
205205
for (SILBasicBlock &BB : Fn)
206206
for (SILInstruction &I : BB)
@@ -220,29 +220,31 @@ class DiagnoseUnknownCompileTimeValues : public SILModuleTransform {
220220
auto CalleeParameters = CalleeDecl->getParameters();
221221
auto ApplyArgRefs = Apply->getArguments();
222222

223-
// llvm::dbgs() << "\n-------------------------------------------\n";
224-
// llvm::dbgs() << "Apply: ";
225-
// Apply->dump();
226-
// llvm::dbgs() << CalleeDecl->getNameStr() << "\n";
227-
// llvm::dbgs() << "Apply Args: ";
228-
// llvm::dbgs() << ApplyArgRefs.size() << "\n";
229-
// llvm::dbgs() << "CalleeParameters: ";
230-
// llvm::dbgs() << CalleeParameters->size() << "\n";
231-
// llvm::dbgs() << "ArgumentOperandNumber: ";
232-
// llvm::dbgs() << Apply->getArgumentOperandNumber() << "\n";
223+
// LLVM_DEBUG({
224+
// llvm::dbgs() << "\n-------------------------------------------\n";
225+
// llvm::dbgs() << "Apply: ";
226+
// Apply->dump();
227+
// llvm::dbgs() << CalleeDecl->getNameStr() << "\n";
228+
// llvm::dbgs() << "Apply Args: ";
229+
// llvm::dbgs() << ApplyArgRefs.size() << "\n";
230+
// llvm::dbgs() << "CalleeParameters: ";
231+
// llvm::dbgs() << CalleeParameters->size() << "\n";
232+
// llvm::dbgs() << "ArgumentOperandNumber: ";
233+
// llvm::dbgs() << Apply->getArgumentOperandNumber() << "\n";
234+
// });
233235

234-
// TOOD: Needs work to correctly match params to args
236+
// (AC) TODO: Needs work to correctly match params to args
235237
bool hasConst = false;
236238
for (size_t i = 0; i < CalleeParameters->size(); ++i)
237-
if (CalleeParameters->get(i)->isCompileTimeConst())
239+
if (CalleeParameters->get(i)->isConstVal())
238240
hasConst = true;
239241

240242
if (hasConst) {
241243
for (size_t i = 0; i < CalleeParameters->size(); ++i) {
242244
auto CorrespondingArg = ApplyArgRefs[i];
243-
if (CalleeParameters->get(i)->isCompileTimeConst()) {
245+
if (CalleeParameters->get(i)->isConstVal()) {
244246
LLVM_DEBUG({
245-
llvm::dbgs() << "[" << CalleeDecl->getNameStr() << "] argument: ";
247+
llvm::dbgs() << "Argument of fn{" << CalleeDecl->getNameStr() << "} ";
246248
llvm::dbgs() << CalleeParameters->get(i)->getNameStr() << ": ";
247249
std::string typeName;
248250
llvm::raw_string_ostream out(typeName);

test/ConstValues/CImports.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Constant globals should "work" when referencing C-imported constants
2-
2+
// REQUIRES: swift_feature_CompileTimeValues
33
// RUN: %empty-directory(%t)
44
// RUN: split-file %s %t
55

6-
// RUN: %target-swift-frontend -emit-ir -primary-file %t/main.swift -parse-as-library -import-bridging-header %t/bridging_header.h
6+
// RUN: %target-swift-frontend -emit-ir -primary-file %t/main.swift -parse-as-library -import-bridging-header %t/bridging_header.h -enable-experimental-feature CompileTimeValues
77

88
//--- bridging_header.h
99

@@ -16,6 +16,6 @@ struct CStruct {
1616

1717
//--- main.swift
1818

19-
_const let constGlobal1: Int = Int(c_integer)
20-
_const let constGlobal2: Int = Int(c_long)
21-
_const let constGlobal3: Int = MemoryLayout<CStruct>.size
19+
@const let constGlobal1: Int = Int(c_integer)
20+
@const let constGlobal2: Int = Int(c_long)
21+
@const let constGlobal3: Int = MemoryLayout<CStruct>.size

test/ConstValues/Conditions.swift

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
// Constant globals on comparisons and conditions
2+
// REQUIRES: swift_feature_CompileTimeValues
3+
// RUN: %target-swift-frontend -emit-ir -pri mary-file %s -parse-as-library -enable-experimental-feature CompileTimeValues
24

3-
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library
5+
@const let constGlobal1: Int = true ? 1 : 0
6+
@const let constGlobal2: Int = (2 * 4 == 8) ? 1 : 0
7+
@const let constGlobal3: Int = (10 > 20) ? 10 : 20
8+
//@const let constGlobal4: Int = max(10, 20)
9+
@const let constGlobal5: Bool = 10 > 20
10+
@const let constGlobal6: Int = constGlobal5 ? 10 : 20
411

5-
_const let constGlobal1: Int = true ? 1 : 0
6-
_const let constGlobal2: Int = (2 * 4 == 8) ? 1 : 0
7-
_const let constGlobal3: Int = (10 > 20) ? 10 : 20
8-
//_const let constGlobal4: Int = max(10, 20)
9-
_const let constGlobal5: Bool = 10 > 20
10-
_const let constGlobal6: Int = constGlobal5 ? 10 : 20
12+
@const let number: UInt8 = 0xf0
1113

12-
_const let number: UInt8 = 0xf0
14+
@const let bit0: Bool = (number & 0b0000_0001) != 0
15+
@const let bit1: Bool = (number & 0b0000_0010) != 0
16+
@const let bit2: Bool = (number & 0b0000_0100) != 0
17+
@const let bit3: Bool = (number & 0b0000_1000) != 0
18+
@const let bit4: Bool = (number & 0b0001_0000) != 0
19+
@const let bit5: Bool = (number & 0b0010_0000) != 0
20+
@const let bit6: Bool = (number & 0b0100_0000) != 0
21+
@const let bit7: Bool = (number & 0b1000_0000) != 0
1322

14-
_const let bit0: Bool = (number & 0b0000_0001) != 0
15-
_const let bit1: Bool = (number & 0b0000_0010) != 0
16-
_const let bit2: Bool = (number & 0b0000_0100) != 0
17-
_const let bit3: Bool = (number & 0b0000_1000) != 0
18-
_const let bit4: Bool = (number & 0b0001_0000) != 0
19-
_const let bit5: Bool = (number & 0b0010_0000) != 0
20-
_const let bit6: Bool = (number & 0b0100_0000) != 0
21-
_const let bit7: Bool = (number & 0b1000_0000) != 0
23+
@const let bits0_0: UInt8 = ((bit0 ? 0b0000_0001 : 0b0000_0000) << 0)
24+
@const let bits0_1: UInt8 = bits0_0 | ((bit1 ? 0b0000_0001 : 0b0000_0000) << 1)
25+
@const let bits0_2: UInt8 = bits0_1 | ((bit2 ? 0b0000_0001 : 0b0000_0000) << 2)
26+
@const let bits0_3: UInt8 = bits0_2 | ((bit3 ? 0b0000_0001 : 0b0000_0000) << 3)
27+
@const let bits0_4: UInt8 = bits0_3 | ((bit4 ? 0b0000_0001 : 0b0000_0000) << 4)
28+
@const let bits0_5: UInt8 = bits0_4 | ((bit5 ? 0b0000_0001 : 0b0000_0000) << 5)
29+
@const let bits0_6: UInt8 = bits0_5 | ((bit6 ? 0b0000_0001 : 0b0000_0000) << 6)
30+
@const let bits0_7: UInt8 = bits0_6 | ((bit7 ? 0b0000_0001 : 0b0000_0000) << 7)
2231

23-
_const let bits0_0: UInt8 = ((bit0 ? 0b0000_0001 : 0b0000_0000) << 0)
24-
_const let bits0_1: UInt8 = bits0_0 | ((bit1 ? 0b0000_0001 : 0b0000_0000) << 1)
25-
_const let bits0_2: UInt8 = bits0_1 | ((bit2 ? 0b0000_0001 : 0b0000_0000) << 2)
26-
_const let bits0_3: UInt8 = bits0_2 | ((bit3 ? 0b0000_0001 : 0b0000_0000) << 3)
27-
_const let bits0_4: UInt8 = bits0_3 | ((bit4 ? 0b0000_0001 : 0b0000_0000) << 4)
28-
_const let bits0_5: UInt8 = bits0_4 | ((bit5 ? 0b0000_0001 : 0b0000_0000) << 5)
29-
_const let bits0_6: UInt8 = bits0_5 | ((bit6 ? 0b0000_0001 : 0b0000_0000) << 6)
30-
_const let bits0_7: UInt8 = bits0_6 | ((bit7 ? 0b0000_0001 : 0b0000_0000) << 7)
31-
32-
_const let numberBackIsRight: Bool = bits0_7 == 0xf0
32+
@const let numberBackIsRight: Bool = bits0_7 == 0xf0

test/ConstValues/DiagModules.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// Constant globals should "work" even when used across files in non-WMO builds.
2-
2+
// REQUIRES: swift_feature_CompileTimeValues
33
// RUN: %empty-directory(%t)
44
// RUN: split-file %s %t
55

6-
// RUN: %target-swift-frontend -emit-module -o %t/MyModule.swiftmodule %t/MyModule.swift -parse-as-library
7-
// RUN: %target-swift-frontend -emit-ir -I %t %t/Main.swift -verify
6+
// RUN: %target-swift-frontend -emit-module -o %t/MyModule.swiftmodule %t/MyModule.swift -parse-as-library -enable-experimental-feature CompileTimeValues
7+
// RUN: %target-swift-frontend -emit-ir -I %t %t/Main.swift -verify -enable-experimental-feature CompileTimeValues
88

99
//--- MyModule.swift
1010

@@ -16,5 +16,5 @@ public func foo() -> Int {
1616

1717
import MyModule
1818

19-
_const let constGlobal1: Int = foo()
20-
// expected-error@-1 {{_const let should be initialized with a compile-time value}}
19+
@const let constGlobal1: Int = foo()
20+
// expected-error@-1 {{@const let should be initialized with a compile-time value}}

test/ConstValues/DiagNotConst.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// Constant globals rejected for not being constant values
2+
// REQUIRES: swift_feature_CompileTimeValues
3+
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -verify -enable-experimental-feature CompileTimeValues
24

3-
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -verify
4-
5-
_const let a: Bool = Bool.random()
6-
// expected-error@-1 {{_const let should be initialized with a compile-time value}}
5+
@const let a: Bool = Bool.random()
6+
// expected-error@-1 {{@const let should be initialized with a compile-time value}}
77

88
func foo() -> Int {
99
return 42 * Int.random(in: 0 ..< 10)
1010
}
1111

12-
_const let b: Int = foo()
13-
// expected-error@-1 {{_const let should be initialized with a compile-time value}}
12+
@const let b: Int = foo()
13+
// expected-error@-1 {{@const let should be initialized with a compile-time value}}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Constant globals referencing other constant globals and forming a cycle
2+
// REQUIRES: swift_feature_CompileTimeValues
3+
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -verify -enable-experimental-feature CompileTimeValues
24

3-
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -verify
4-
5-
_const let a: Int = c
6-
_const let b: Int = a
7-
_const let c: Int = b
5+
@const let a: Int = c
6+
@const let b: Int = a
7+
@const let c: Int = b
88
// expected-error@-1 {{cycle in definitions of constant values}}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Constant globals on simple floating-point literals
2+
// REQUIRES: swift_feature_CompileTimeValues
3+
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -enable-experimental-feature CompileTimeValues
24

3-
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library
4-
5-
_const let constGlobal1 = 42.0
6-
_const let constGlobal2: Double = 42.0
7-
_const let constGlobal3: Float = 42.0
8-
_const let constGlobal4: Float16 = 42.0
5+
@const let constGlobal1 = 42.0
6+
@const let constGlobal2: Double = 42.0
7+
@const let constGlobal3: Float = 42.0
8+
@const let constGlobal4: Float16 = 42.0

test/ConstValues/FunctionTypes.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
// Constant globals on function types / function pointers
2-
3-
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library
2+
// REQUIRES: swift_feature_CompileTimeValues
3+
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -enable-experimental-feature CompileTimeValues
44

55
func foo_void_to_void() {}
66
func foo_int_to_int(x: Int) -> Int { return 42 }
77

8-
_const let constGlobalA1: ()->() = { }
9-
_const let constGlobalA2: @convention(c) ()->() = { }
10-
_const let constGlobalA3: @convention(thin) ()->() = { }
8+
@const let constGlobalA1: ()->() = { }
9+
@const let constGlobalA2: @convention(c) ()->() = { }
10+
@const let constGlobalA3: @convention(thin) ()->() = { }
1111

12-
_const let constGlobalB1: ()->() = foo_void_to_void
13-
_const let constGlobalB2: @convention(c) ()->() = foo_void_to_void
14-
_const let constGlobalB3: @convention(thin) ()->() = foo_void_to_void
12+
@const let constGlobalB1: ()->() = foo_void_to_void
13+
@const let constGlobalB2: @convention(c) ()->() = foo_void_to_void
14+
@const let constGlobalB3: @convention(thin) ()->() = foo_void_to_void
1515

16-
_const let constGlobalC1: (Int)->(Int) = { _ in return 42 }
17-
_const let constGlobalC2: @convention(c) (Int)->(Int) = { _ in return 42 }
18-
_const let constGlobalC3: @convention(thin) (Int)->(Int) = { _ in return 42 }
16+
@const let constGlobalC1: (Int)->(Int) = { _ in return 42 }
17+
@const let constGlobalC2: @convention(c) (Int)->(Int) = { _ in return 42 }
18+
@const let constGlobalC3: @convention(thin) (Int)->(Int) = { _ in return 42 }
1919

20-
_const let constGlobalD1: (Int)->(Int) = foo_int_to_int
21-
_const let constGlobalD2: @convention(c) (Int)->(Int) = foo_int_to_int
22-
_const let constGlobalD3: @convention(thin) (Int)->(Int) = foo_int_to_int
20+
@const let constGlobalD1: (Int)->(Int) = foo_int_to_int
21+
@const let constGlobalD2: @convention(c) (Int)->(Int) = foo_int_to_int
22+
@const let constGlobalD3: @convention(thin) (Int)->(Int) = foo_int_to_int

test/ConstValues/InlineArrays.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Constant globals on inline arrays
2+
// REQUIRES: swift_feature_CompileTimeValues
3+
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -disable-availability-checking -enable-experimental-feature CompileTimeValues
24

3-
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -disable-availability-checking
4-
5-
_const let constGlobal1: InlineArray = [1, 2, 3]
6-
_const let constGlobal2: InlineArray = [1.0, 2.0, 3.0]
7-
_const let constGlobal3: InlineArray = constGlobal1
8-
_const let constGlobal4: Int = ([1, 2, 3] as InlineArray).count
5+
@const let constGlobal1: InlineArray = [1, 2, 3]
6+
@const let constGlobal2: InlineArray = [1.0, 2.0, 3.0]
7+
@const let constGlobal3: InlineArray = constGlobal1
8+
@const let constGlobal4: Int = ([1, 2, 3] as InlineArray).count
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Constant globals on integer arithmetics
2+
// REQUIRES: swift_feature_CompileTimeValues
3+
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -enable-experimental-feature CompileTimeValues
24

3-
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library
4-
5-
_const let constGlobal1: Int = 42
6-
_const let constGlobal2: UInt = 42 + 42
7-
_const let constGlobal3: UInt = 42 * 42
8-
_const let constGlobal4: UInt = 42 - 42
9-
_const let constGlobal5: UInt = 42 / 42
10-
_const let constGlobal6: UInt = 42 % 2
11-
_const let constGlobal7: UInt = (42 % 2)
5+
@const let constGlobal1: Int = 42
6+
@const let constGlobal2: UInt = 42 + 42
7+
@const let constGlobal3: UInt = 42 * 42
8+
@const let constGlobal4: UInt = 42 - 42
9+
@const let constGlobal5: UInt = 42 / 42
10+
@const let constGlobal6: UInt = 42 % 2
11+
@const let constGlobal7: UInt = (42 % 2)

0 commit comments

Comments
 (0)