Skip to content

Commit 0f83b65

Browse files
authored
[cxx-interop] Remove impl for constexpr members (#60154)
* Remove impl for constexpr members * Readd some tets * Only remove import of static constexpr
1 parent 2f78f5a commit 0f83b65

File tree

7 files changed

+9
-153
lines changed

7 files changed

+9
-153
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3273,49 +3273,8 @@ namespace {
32733273
return nullptr;
32743274
}
32753275

3276-
AccessorDecl *tryCreateConstexprAccessor(const clang::VarDecl *clangVar,
3277-
VarDecl *swiftVar) {
3278-
assert(clangVar->isConstexpr());
3279-
clangVar->evaluateValue();
3280-
auto evaluated = clangVar->getEvaluatedValue();
3281-
if (!evaluated)
3282-
return nullptr;
3283-
3284-
// If we have a constexpr var with an evaluated value, try to create an
3285-
// accessor for it. If we can remove all references to the global (which
3286-
// we should be able to do for constexprs) then we can remove the global
3287-
// entirely.
3288-
auto accessor = AccessorDecl::create(
3289-
Impl.SwiftContext, SourceLoc(), SourceLoc(), AccessorKind::Get,
3290-
swiftVar, SourceLoc(), StaticSpellingKind::KeywordStatic,
3291-
/*Async=*/false, /*AsyncLoc=*/SourceLoc(),
3292-
/*throws=*/false, SourceLoc(), /*genericParams*/ nullptr,
3293-
ParameterList::createEmpty(Impl.SwiftContext), swiftVar->getType(),
3294-
swiftVar->getDeclContext());
3295-
Expr *value = nullptr;
3296-
// TODO: add non-numeric types.
3297-
if (evaluated->isInt()) {
3298-
value = IntegerLiteralExpr::createFromUnsigned(
3299-
Impl.SwiftContext,
3300-
static_cast<unsigned>(
3301-
clangVar->getEvaluatedValue()->getInt().getZExtValue()));
3302-
} else if (evaluated->isFloat()) {
3303-
auto floatStr = evaluated->getAsString(Impl.getClangASTContext(),
3304-
clangVar->getType());
3305-
auto floatIdent = Impl.SwiftContext.getIdentifier(floatStr);
3306-
value = new (Impl.SwiftContext)
3307-
FloatLiteralExpr(floatIdent.str(), SourceLoc());
3308-
}
3309-
if (!value)
3310-
return nullptr;
3311-
auto returnStmt = new (Impl.SwiftContext) ReturnStmt(SourceLoc(), value);
3312-
auto body = BraceStmt::create(Impl.SwiftContext, SourceLoc(),
3313-
{returnStmt}, SourceLoc());
3314-
accessor->setBodyParsed(body);
3315-
return accessor;
3316-
}
3317-
33183276
Decl *VisitVarDecl(const clang::VarDecl *decl) {
3277+
33193278
// Variables are imported as... variables.
33203279
ImportedName importedName;
33213280
Optional<ImportedName> correctSwiftName;
@@ -3362,6 +3321,10 @@ namespace {
33623321
if (dc->isTypeContext())
33633322
isStatic = true;
33643323

3324+
// For now we don't import static constexpr
3325+
if (isStatic && decl->isConstexpr())
3326+
return nullptr;
3327+
33653328
auto introducer = Impl.shouldImportGlobalAsLet(decl->getType())
33663329
? VarDecl::Introducer::Let
33673330
: VarDecl::Introducer::Var;
@@ -3385,11 +3348,6 @@ namespace {
33853348
if (correctSwiftName)
33863349
markAsVariant(result, *correctSwiftName);
33873350

3388-
// For constexpr vars, we can create an accessor with a numeric literal.
3389-
if (decl->isConstexpr())
3390-
if (auto acc = tryCreateConstexprAccessor(decl, result))
3391-
result->setAccessors(SourceLoc(), {acc}, SourceLoc());
3392-
33933351
return result;
33943352
}
33953353

test/Interop/Cxx/static/constexpr-static-member-var.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,4 @@ import StdlibUnittest
1111

1212
var ConstexprStaticMemberVarTestSuite = TestSuite("ConstexprStaticMemberVarTestSuite")
1313

14-
ConstexprStaticMemberVarTestSuite.test("constexpr-static-member") {
15-
expectEqual(139, WithConstexprStaticMember.definedInline)
16-
}
17-
1814
runAllTests()

test/Interop/Cxx/static/static-member-var-irgen.swift

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,6 @@
33
// CHECK: @{{_ZN16WithStaticMember12staticMemberE|"\?staticMember@WithStaticMember@@2HA"}} = external {{(dso_local )?}}global i32, align 4
44
// CHECK: @{{_ZN26WithIncompleteStaticMember10selfMemberE|"\?selfMember@WithIncompleteStaticMember@@2V1@A"}} = {{external|linkonce_odr}} {{(dso_local )?}}global %class.WithIncompleteStaticMember, align 4
55

6-
//TODO: This test uses only values of static const members, so it does not need
7-
//to depend on external definitions. However, our code generation pattern loads
8-
//the value dynamically. Instead, we should inline known constants. That would
9-
//allow Swift code to even read the value of WithIncompleteStaticMember::notDefined.
10-
// CHECK: @{{_ZN21WithConstStaticMember7definedE|"\?defined@WithConstStaticMember@@2HB"}} = {{available_externally|linkonce_odr}} {{(dso_local )?}}constant i32 48, {{(comdat, )?}}align 4
11-
// CHECK: @{{_ZN21WithConstStaticMember16definedOutOfLineE|"\?definedOutOfLine@WithConstStaticMember@@2HB"}} = external {{(dso_local )?}}constant i32, align 4
12-
13-
// Make sure we remove constexpr globals after all uses have been inlined.
14-
// CHECK-NOT: _ZN25WithConstexprStaticMember13definedInlineE
15-
// CHECK-NOT: ?definedInline@WithConstexprStaticMember@@2HB
16-
// CHECK-NOT: @_ZN25WithConstexprStaticMember20definedInlineWithArgE
17-
// CHECK-NOT: @_ZN25WithConstexprStaticMember18definedInlineFloatE
18-
// CHECK-NOT: @_ZN25WithConstexprStaticMember23definedInlineFromMethodE
19-
206
import StaticMemberVar
217

228
public func readStaticMember() -> CInt {
@@ -54,55 +40,17 @@ public func writeSelfMember(_ m: WithIncompleteStaticMember) {
5440
// public func readNotDefinedConstMember() -> CInt {
5541
// return WithConstStaticMember.notDefined
5642
// }
57-
5843
public func readDefinedConstMember() -> CInt {
5944
return WithConstStaticMember.defined
6045
}
6146

6247
// CHECK: define {{(protected |dllexport )?}}swiftcc i32 @"$s4main22readDefinedConstMembers5Int32VyF"() #0
6348
// CHECK: [[VALUE:%.*]] = load i32, i32* getelementptr inbounds (%Ts5Int32V, %Ts5Int32V* bitcast (i32* @{{_ZN21WithConstStaticMember7definedE|"\?defined@WithConstStaticMember@@2HB"}} to %Ts5Int32V*), i32 0, i32 0), align 4
6449
// CHECK: ret i32 [[VALUE]]
65-
6650
public func readDefinedOutOfLineConstMember() -> CInt {
6751
return WithConstStaticMember.definedOutOfLine
6852
}
6953

7054
// CHECK: define {{(protected |dllexport )?}}swiftcc i32 @"$s4main31readDefinedOutOfLineConstMembers5Int32VyF"() #0
7155
// CHECK: [[VALUE:%.*]] = load i32, i32* getelementptr inbounds (%Ts5Int32V, %Ts5Int32V* bitcast (i32* @{{_ZN21WithConstStaticMember16definedOutOfLineE|"\?definedOutOfLine@WithConstStaticMember@@2HB"}} to %Ts5Int32V*), i32 0, i32 0), align 4
72-
// CHECK: ret i32 [[VALUE]]
73-
74-
public func readConstexprStaticIntMembers() {
75-
let x = WithConstexprStaticMember.definedInline
76-
let y = WithConstexprStaticMember.definedInlineWithArg
77-
}
78-
79-
// CHECK-LABEL: define {{(protected |dllexport )?}}swiftcc void @"$s4main29readConstexprStaticIntMembersyyF"()
80-
// CHECK: call swiftcc i32 @"$sSo25WithConstexprStaticMemberV13definedInlines5Int32VvgZ"()
81-
// CHECK: call swiftcc i32 @"$sSo25WithConstexprStaticMemberV013definedInlineA3Args5Int32VvgZ"()
82-
// CHECK: ret void
83-
84-
// CHECK-LABEL: define linkonce_odr {{.*}}swiftcc i32 @"$sSo25WithConstexprStaticMemberV13definedInlines5Int32VvgZ"()
85-
// CHECK-NEXT: entry
86-
// CHECK-NEXT: ret i32 139
87-
88-
// CHECK-LABEL: define linkonce_odr {{.*}}swiftcc i32 @"$sSo25WithConstexprStaticMemberV013definedInlineA3Args5Int32VvgZ"()
89-
// CHECK-NEXT: entry
90-
// CHECK-NEXT: ret i32 42
91-
92-
public func readConstexprStaticFloatMembers() {
93-
let x = WithConstexprStaticMember.definedInlineFloat
94-
let y = WithConstexprStaticMember.definedInlineFromMethod
95-
}
96-
97-
// CHECK-LABEL: define {{(protected |dllexport )?}}swiftcc void @"$s4main31readConstexprStaticFloatMembersyyF"()
98-
// CHECK: call swiftcc float @"$sSo25WithConstexprStaticMemberV18definedInlineFloatSfvgZ"()
99-
// CHECK: call swiftcc float @"$sSo25WithConstexprStaticMemberV23definedInlineFromMethodSfvgZ"()
100-
// CHECK: ret void
101-
102-
// CHECK-LABEL: define linkonce_odr {{.*}}swiftcc float @"$sSo25WithConstexprStaticMemberV18definedInlineFloatSfvgZ"()
103-
// CHECK-NEXT: entry
104-
// CHECK-NEXT: ret float 1.390000e+02
105-
106-
// CHECK-LABEL: define linkonce_odr {{.*}}swiftcc float @"$sSo25WithConstexprStaticMemberV23definedInlineFromMethodSfvgZ"()
107-
// CHECK-NEXT: entry
108-
// CHECK-NEXT: ret float 4.200000e+01
56+
// CHECK: ret i32 [[VALUE]]

test/Interop/Cxx/static/static-member-var-silgen.swift

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44
// CHECK: sil_global public_external @{{_ZN16WithStaticMember12staticMemberE|\?staticMember@WithStaticMember@@2HA}} : $Int32
55
// CHECK: // clang name: WithIncompleteStaticMember::selfMember
66
// CHECK: sil_global public_external @{{_ZN26WithIncompleteStaticMember10selfMemberE|\?selfMember@WithIncompleteStaticMember@@2V1@A}} : $WithIncompleteStaticMember
7-
// CHECK: // clang name: WithConstStaticMember::defined
8-
// CHECK: sil_global public_external [let] @{{_ZN21WithConstStaticMember7definedE|\?defined@WithConstStaticMember@@2HB}} : $Int32
9-
// CHECK: // clang name: WithConstStaticMember::definedOutOfLine
10-
// CHECK: sil_global public_external [let] @{{_ZN21WithConstStaticMember16definedOutOfLineE|\?definedOutOfLine@WithConstStaticMember@@2HB}} : $Int32
11-
127
import StaticMemberVar
138

149
func readStaticMember() -> CInt {
@@ -49,12 +44,10 @@ func writeSelfMember(_ m: WithIncompleteStaticMember) {
4944
// CHECK: [[ADDR:%.*]] = global_addr @{{_ZN26WithIncompleteStaticMember10selfMemberE|\?selfMember@WithIncompleteStaticMember@@2V1@A}} : $*WithIncompleteStaticMember
5045
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [dynamic] [[ADDR]] : $*WithIncompleteStaticMember
5146
// CHECK: store %0 to [[ACCESS]] : $*WithIncompleteStaticMember
52-
5347
//TODO fix undefined reference to `WithConstStaticMember::notDefined`.
5448
// func readNotDefinedConstMember() -> CInt {
5549
// return WithConstStaticMember.notDefined
5650
// }
57-
5851
func readDefinedConstMember() -> CInt {
5952
return WithConstStaticMember.defined
6053
}
@@ -63,25 +56,6 @@ func readDefinedConstMember() -> CInt {
6356
// CHECK: [[ADDR:%.*]] = global_addr @{{_ZN21WithConstStaticMember7definedE|\?defined@WithConstStaticMember@@2HB}} : $*Int32
6457
// CHECK: [[VALUE:%.*]] = load [[ADDR]] : $*Int32
6558
// CHECK: return [[VALUE]] : $Int32
66-
6759
func readDefinedOutOfLineConstMember() -> CInt {
6860
return WithConstStaticMember.definedOutOfLine
69-
}
70-
71-
// CHECK: sil hidden @$s4main25readConstexprStaticMembers5Int32VyF : $@convention(thin) () -> Int32
72-
// CHECK: [[META:%.*]] = metatype $@thin WithConstexprStaticMember.Type
73-
// CHECK: [[ACC:%.*]] = function_ref @$sSo25WithConstexprStaticMemberV13definedInlines5Int32VvgZ : $@convention(method) (@thin WithConstexprStaticMember.Type) -> Int32
74-
// CHECK: [[OUT:%.*]] = apply [[ACC]]([[META]]) : $@convention(method) (@thin WithConstexprStaticMember.Type) -> Int32
75-
// CHECK: return [[OUT]] : $Int32
76-
// CHECK-LABEL: end sil function '$s4main25readConstexprStaticMembers5Int32VyF'
77-
78-
// Make sure we also generate the accessor with a numeric literal.
79-
// CHECK-LABEL: sil shared @$sSo25WithConstexprStaticMemberV13definedInlines5Int32VvgZ : $@convention(method) (@thin WithConstexprStaticMember.Type) -> Int32
80-
// CHECK: [[IL:%.*]] = integer_literal $Builtin.Int32, 139
81-
// CHECK: [[OUT:%.*]] = struct $Int32 ([[IL]] : $Builtin.Int32)
82-
// CHECK: return [[OUT]] : $Int32
83-
// CHECK-LABEL: end sil function '$sSo25WithConstexprStaticMemberV13definedInlines5Int32VvgZ'
84-
85-
func readConstexprStaticMember() -> CInt {
86-
return WithConstexprStaticMember.definedInline
87-
}
61+
}

test/Interop/Cxx/static/static-var-irgen.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,9 @@ import StaticVar
44

55
public func initStaticVars() -> CInt {
66
return staticVar + staticVarInit + staticVarInlineInit + staticConst + staticConstInit
7-
+ staticConstInlineInit + staticConstexpr + staticNonTrivial.val + staticConstNonTrivial.val
8-
+ staticConstexprNonTrivial.val
7+
+ staticConstInlineInit + staticNonTrivial.val + staticConstNonTrivial.val
98
}
109

11-
// Constexpr vars should be inlined and removed.
12-
// CHECK-NOT: ?staticConstexpr
13-
// CHECK-NOT: _ZL15staticConstexpr
14-
1510
// CHECK: @{{_ZL9staticVar|staticVar}} = internal global i32 2, align 4
1611
// CHECK: @{{_ZL13staticVarInit|staticVarInit}} = internal global i32 0, align 4
1712
// CHECK: @{{_ZL19staticVarInlineInit|staticVarInlineInit}} = internal global i32 0, align 4
@@ -20,7 +15,6 @@ public func initStaticVars() -> CInt {
2015
// CHECK: @{{_ZL21staticConstInlineInit|staticConstInlineInit}} = internal global i32 0, align 4
2116
// CHECK: @{{_ZL16staticNonTrivial|staticNonTrivial}} = internal global %class.NonTrivial zeroinitializer, align 4
2217
// CHECK: @{{_ZL21staticConstNonTrivial|staticConstNonTrivial}} = internal global %class.NonTrivial zeroinitializer, align 4
23-
// CHECK: @{{_ZL25staticConstexprNonTrivial|staticConstexprNonTrivial}} = internal constant %class.NonTrivial { i32 8192 }, align 4
2418

2519
// CHECK: define internal void @{{__cxx_global_var_init|"\?\?__EstaticVarInit@@YAXXZ"}}()
2620
// CHECK: %call = call i32 @{{_Z13makeStaticVarv|"\?makeStaticVar@@YAHXZ"}}()

test/Interop/Cxx/static/static-var-silgen.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@ import StaticVar
44

55
func initStaticVars() -> CInt {
66
return staticVar + staticVarInit + staticVarInlineInit + staticConst + staticConstInit
7-
+ staticConstInlineInit + staticConstexpr + staticNonTrivial.val + staticConstNonTrivial.val
8-
+ staticConstexprNonTrivial.val
7+
+ staticConstInlineInit + staticNonTrivial.val + staticConstNonTrivial.val
98
}
109

11-
// Constexpr globals should be inlined and removed.
12-
// CHECK-NOT: sil_global public_external [let] @staticConstexpr : $Int32
13-
1410
// CHECK: // clang name: staticVar
1511
// CHECK: sil_global public_external @staticVar : $Int32
1612
// CHECK: // clang name: staticVarInit
@@ -27,8 +23,6 @@ func initStaticVars() -> CInt {
2723
// CHECK: sil_global public_external @staticNonTrivial : $NonTrivial
2824
// CHECK: // clang name: staticConstNonTrivial
2925
// CHECK: sil_global public_external [let] @staticConstNonTrivial : $NonTrivial
30-
// CHECK: // clang name: staticConstexprNonTrivial
31-
// CHECK: sil_global public_external [let] @staticConstexprNonTrivial : $NonTrivial
3226

3327
func readStaticVar() -> CInt {
3428
return staticVar

test/Interop/Cxx/static/static-var.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ StaticVarTestSuite.test("static-const-int-init") {
4747
expectEqual(128, staticConstInit)
4848
}
4949

50-
StaticVarTestSuite.test("static-constexpr-int") {
51-
expectEqual(32, staticConstexpr)
52-
}
53-
5450
StaticVarTestSuite.test("static-non-trivial") {
5551
expectEqual(1024, staticNonTrivial.val)
5652
}
@@ -75,8 +71,4 @@ StaticVarTestSuite.test("static-const-non-trivial") {
7571
expectEqual(2048, staticConstNonTrivial.val)
7672
}
7773

78-
StaticVarTestSuite.test("static-constexpr-non-trivial") {
79-
expectEqual(8192, staticConstexprNonTrivial.val)
80-
}
81-
8274
runAllTests()

0 commit comments

Comments
 (0)