Skip to content

Commit 555b6e8

Browse files
committed
[interop] refactor the IR details provider to avoid the visitor in IRGen
1 parent 36d2533 commit 555b6e8

File tree

7 files changed

+97
-128
lines changed

7 files changed

+97
-128
lines changed

include/swift/IRGen/SwiftIRDetailsCollector.h renamed to include/swift/IRGen/SwiftIRDetailsProvider.h

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,27 @@ class NominalTypeDecl;
2323
class IRGenOptions;
2424
class SILModule;
2525

26+
class IRDetailsProviderImpl;
27+
2628
/// An abstract interface provided to \c IRDetailsVisitor that provides access
2729
/// to the queries that can be performed on declarations to get their various
2830
/// ABI details.
2931
class IRDetailsProvider {
3032
public:
31-
virtual ~IRDetailsProvider() {}
33+
IRDetailsProvider(const IRGenOptions &opts, SILModule &SILMod);
34+
~IRDetailsProvider();
3235

3336
using SizeType = uint64_t;
3437

3538
/// Returns the size and alignment for the given type, or \c None if the type
3639
/// is not a fixed layout type.
37-
virtual llvm::Optional<std::pair<SizeType, SizeType>>
38-
getTypeSizeAlignment(const NominalTypeDecl *TD) = 0;
39-
};
40+
llvm::Optional<std::pair<SizeType, SizeType>>
41+
getTypeSizeAlignment(const NominalTypeDecl *TD);
4042

41-
/// A visitor interface that should be implemented by clients that want to
42-
/// collect information about ABI of Swift declaration, e.g. the layout of
43-
/// types.
44-
class IRDetailsVisitor {
45-
public:
46-
virtual ~IRDetailsVisitor() {}
47-
virtual void visitTypeDecl(const NominalTypeDecl *TD,
48-
IRDetailsProvider &irDetails) = 0;
43+
private:
44+
std::unique_ptr<IRDetailsProviderImpl> impl;
4945
};
5046

51-
/// Gathers ABI details for Swift declarations by inspecting the LLVM IR that is
52-
/// going to be generated for the given SIL module.
53-
///
54-
/// The clients should provide a custom \c IRDetailsVisitor implementation to
55-
/// collect the details they're interested in.
56-
void collectSwiftIRDetails(const IRGenOptions &opts, SILModule &SILMod,
57-
IRDetailsVisitor &visitor);
58-
5947
} // namespace swift
6048

6149
#endif

lib/IRGen/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ add_swift_host_library(swiftIRGen STATIC
5555
MetadataRequest.cpp
5656
Outlining.cpp
5757
StructLayout.cpp
58-
SwiftIRDetailsCollector.cpp
58+
SwiftIRDetailsProvider.cpp
5959
SwiftTargetInfo.cpp
6060
TypeLayout.cpp
6161
TypeLayoutDumper.cpp

lib/IRGen/SwiftIRDetailsCollector.cpp

Lines changed: 0 additions & 88 deletions
This file was deleted.

lib/IRGen/SwiftIRDetailsProvider.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//===--- SwiftIRDetailsCollector.cpp - Get ABI details for decls *- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "swift/IRGen/SwiftIRDetailsProvider.h"
14+
#include "FixedTypeInfo.h"
15+
#include "GenType.h"
16+
#include "IRGen.h"
17+
#include "IRGenModule.h"
18+
19+
#include "swift/AST/ASTContext.h"
20+
#include "swift/AST/ASTMangler.h"
21+
#include "swift/AST/IRGenOptions.h"
22+
#include "swift/AST/Types.h"
23+
#include "swift/SIL/SILModule.h"
24+
25+
#include <vector>
26+
27+
using namespace swift;
28+
using namespace irgen;
29+
30+
namespace swift {
31+
32+
class IRDetailsProviderImpl {
33+
public:
34+
using SizeType = IRDetailsProvider::SizeType;
35+
36+
IRDetailsProviderImpl(const IRGenOptions &opts, SILModule &SILMod)
37+
: IRGen(opts, SILMod), IGM(IRGen, IRGen.createTargetMachine()) {}
38+
39+
llvm::Optional<std::pair<SizeType, SizeType>>
40+
getTypeSizeAlignment(const NominalTypeDecl *TD) {
41+
auto *TI = &IGM.getTypeInfoForUnlowered(TD->getDeclaredTypeInContext());
42+
auto *fixedTI = dyn_cast<FixedTypeInfo>(TI);
43+
if (!fixedTI)
44+
return None;
45+
return std::make_pair(fixedTI->getFixedSize().getValue(),
46+
fixedTI->getFixedAlignment().getValue());
47+
}
48+
49+
private:
50+
IRGenerator IRGen;
51+
IRGenModule IGM;
52+
};
53+
54+
} // namespace swift
55+
56+
IRDetailsProvider::IRDetailsProvider(const IRGenOptions &opts,
57+
SILModule &SILMod)
58+
: impl(std::make_unique<IRDetailsProviderImpl>(opts, SILMod)) {}
59+
60+
IRDetailsProvider::~IRDetailsProvider() {}
61+
62+
llvm::Optional<
63+
std::pair<IRDetailsProvider::SizeType, IRDetailsProvider::SizeType>>
64+
IRDetailsProvider::getTypeSizeAlignment(const NominalTypeDecl *TD) {
65+
return impl->getTypeSizeAlignment(TD);
66+
}

lib/PrintAsClang/ExposedDeclFilter.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ namespace clang_translation {
2323

2424
/// Returns true if the given declaration should be exposed in the generated
2525
/// Objective-C/C/C++ header, as depending on the specified output language.
26-
bool shouldExpose(const ValueDecl *VD, OutputLanguageMode outputLang,
27-
AccessLevel minRequiredAccess) {
26+
inline bool shouldExpose(const ValueDecl *VD, OutputLanguageMode outputLang,
27+
AccessLevel minRequiredAccess) {
2828
return !VD->isInvalid() &&
2929
(outputLang == OutputLanguageMode::Cxx
3030
? cxx_translation::isVisibleToCxx(VD, minRequiredAccess)
@@ -35,7 +35,7 @@ bool shouldExpose(const ValueDecl *VD, OutputLanguageMode outputLang,
3535
/// Returns true if the given declaration should be exposed in generated
3636
/// Objective-C/C/C++ header, regardless of what language the declaration is
3737
/// exposed to.
38-
bool shouldExpose(const ValueDecl *VD, AccessLevel minRequiredAccess) {
38+
inline bool shouldExpose(const ValueDecl *VD, AccessLevel minRequiredAccess) {
3939
return shouldExpose(VD, OutputLanguageMode::ObjC, minRequiredAccess) ||
4040
shouldExpose(VD, OutputLanguageMode::Cxx, minRequiredAccess);
4141
}

lib/PrintAsClang/SwiftToClangInteropContext.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212

1313
#include "swift/PrintAsClang/SwiftToClangInteropContext.h"
1414

15+
#include "ExposedDeclFilter.h"
1516
#include "SwiftToClangInteropContextImpl.h"
1617

1718
#include "swift/AST/Decl.h"
18-
#include "swift/IRGen/SwiftIRDetailsCollector.h"
19+
#include "swift/IRGen/SwiftIRDetailsProvider.h"
20+
#include "swift/SIL/SILModule.h"
1921
#include "llvm/ADT/DenseMap.h"
2022

2123
using namespace swift;
@@ -27,20 +29,21 @@ SwiftToClangInteropContext::~SwiftToClangInteropContext() {}
2729

2830
void SwiftToClangInteropContext::collectSwiftIRGenDetails(
2931
const IRGenOptions &opts, SILModule &SILMod) {
30-
class IRABICollector : public IRDetailsVisitor {
31-
public:
32-
void visitTypeDecl(const NominalTypeDecl *TD,
33-
IRDetailsProvider &irDetails) override {
34-
if (const auto *SD = dyn_cast<StructDecl>(TD)) {
35-
if (auto typeInfo = irDetails.getTypeSizeAlignment(TD))
36-
impl.addStructDeclInfo(SD, {typeInfo->first, typeInfo->second});
37-
}
38-
}
32+
ModuleDecl &M = *SILMod.getSwiftModule();
33+
34+
// Gather the declarations that are exposed to clang.
35+
SmallVector<Decl *, 64> decls;
36+
M.getTopLevelDecls(decls);
3937

40-
IRABICollector(SwiftToClangInteropContextImpl &impl) : impl(impl) {}
38+
IRDetailsProvider irDetails(opts, SILMod);
4139

42-
SwiftToClangInteropContextImpl &impl;
43-
};
44-
IRABICollector visitor(*impl);
45-
collectSwiftIRDetails(opts, SILMod, visitor);
40+
for (auto decl : decls) {
41+
const auto *VD = dyn_cast<ValueDecl>(decl);
42+
if (!VD || !clang_translation::shouldExpose(VD, AccessLevel::Public))
43+
continue;
44+
if (const auto *SD = dyn_cast<StructDecl>(VD)) {
45+
if (auto typeInfo = irDetails.getTypeSizeAlignment(SD))
46+
impl->addStructDeclInfo(SD, {typeInfo->first, typeInfo->second});
47+
}
48+
}
4649
}

lib/PrintAsClang/SwiftToClangInteropContextImpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#ifndef SWIFT_PRINTASCLANG_SWIFTTOCLANGINTEROPCONTEXTIMPL_H
1414
#define SWIFT_PRINTASCLANG_SWIFTTOCLANGINTEROPCONTEXTIMPL_H
1515

16-
#include "swift/IRGen/SwiftIRDetailsCollector.h"
16+
#include "swift/IRGen/SwiftIRDetailsProvider.h"
1717
#include "llvm/ADT/DenseMap.h"
1818
#include "llvm/ADT/Optional.h"
1919
#include <stdint.h>

0 commit comments

Comments
 (0)