Skip to content

Commit 4550af2

Browse files
artemcmbnbarham
authored andcommitted
Parameterize Initialization of 'clang::CodeGenerator' on a TargetInfo instance which may differ from the one in the ASTContext
As per swiftlang/swift#65930, Swift compiler's built-in Clang instance may require to perform type-checking against one OS version and compilation/code-generation against an earlier version. This change allows Swift to configure it's built-in Clang code-generator with a custom 'TargetInfo'. Part of rdar://113712186 (cherry picked from commit 9894e7a)
1 parent 9160119 commit 4550af2

File tree

5 files changed

+40
-17
lines changed

5 files changed

+40
-17
lines changed

clang/include/clang/AST/ASTConsumer.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ namespace clang {
2727
class VarDecl;
2828
class FunctionDecl;
2929
class ImportDecl;
30+
class TargetInfo;
3031

3132
/// ASTConsumer - This is an abstract interface that should be implemented by
3233
/// clients that read ASTs. This abstraction layer allows the client to be
@@ -47,6 +48,14 @@ class ASTConsumer {
4748
/// ASTContext.
4849
virtual void Initialize(ASTContext &Context) {}
4950

51+
/// Initialize - This is called to initialize the consumer, providing the
52+
/// ASTContext. 'CodeGenTargetInfo' specifies the code-generation configuration
53+
/// for this compilation instance, which may differ from the one carried
54+
/// by the Context itself only in the OS Version number -
55+
/// for example when type-checking must be performed against an epoch OS version
56+
/// while code-generation must run according to the user-specified OS version.
57+
virtual void Initialize(ASTContext &Context, const TargetInfo &CodeGenTargetInfo) {}
58+
5059
/// HandleTopLevelDecl - Handle the specified top-level declaration. This is
5160
/// called by the parser to process every top-level Decl*.
5261
///

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -335,12 +335,14 @@ CodeGenModule::CodeGenModule(ASTContext &C,
335335
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
336336
const HeaderSearchOptions &HSO,
337337
const PreprocessorOptions &PPO,
338-
const CodeGenOptions &CGO, llvm::Module &M,
338+
const CodeGenOptions &CGO,
339+
const TargetInfo &CGTI,
340+
llvm::Module &M,
339341
DiagnosticsEngine &diags,
340342
CoverageSourceInfo *CoverageInfo)
341343
: Context(C), LangOpts(C.getLangOpts()), FS(FS), HeaderSearchOpts(HSO),
342344
PreprocessorOpts(PPO), CodeGenOpts(CGO), TheModule(M), Diags(diags),
343-
Target(C.getTargetInfo()), ABI(createCXXABI(*this)),
345+
Target(CGTI), ABI(createCXXABI(*this)),
344346
VMContext(M.getContext()), VTables(*this), StackHandler(diags),
345347
SanitizerMD(new SanitizerMetadata(*this)),
346348
AtomicOpts(Target.getAtomicOpts()) {
@@ -357,19 +359,19 @@ CodeGenModule::CodeGenModule(ASTContext &C,
357359
BFloatTy = llvm::Type::getBFloatTy(LLVMContext);
358360
FloatTy = llvm::Type::getFloatTy(LLVMContext);
359361
DoubleTy = llvm::Type::getDoubleTy(LLVMContext);
360-
PointerWidthInBits = C.getTargetInfo().getPointerWidth(LangAS::Default);
362+
PointerWidthInBits = Target.getPointerWidth(LangAS::Default);
361363
PointerAlignInBytes =
362-
C.toCharUnitsFromBits(C.getTargetInfo().getPointerAlign(LangAS::Default))
364+
C.toCharUnitsFromBits(Target.getPointerAlign(LangAS::Default))
363365
.getQuantity();
364366
SizeSizeInBytes =
365-
C.toCharUnitsFromBits(C.getTargetInfo().getMaxPointerWidth()).getQuantity();
367+
C.toCharUnitsFromBits(Target.getMaxPointerWidth()).getQuantity();
366368
IntAlignInBytes =
367-
C.toCharUnitsFromBits(C.getTargetInfo().getIntAlign()).getQuantity();
369+
C.toCharUnitsFromBits(Target.getIntAlign()).getQuantity();
368370
CharTy =
369-
llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getCharWidth());
370-
IntTy = llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getIntWidth());
371+
llvm::IntegerType::get(LLVMContext, Target.getCharWidth());
372+
IntTy = llvm::IntegerType::get(LLVMContext, Target.getIntWidth());
371373
IntPtrTy = llvm::IntegerType::get(LLVMContext,
372-
C.getTargetInfo().getMaxPointerWidth());
374+
Target.getMaxPointerWidth());
373375
Int8PtrTy = llvm::PointerType::get(LLVMContext,
374376
C.getTargetAddressSpace(LangAS::Default));
375377
const llvm::DataLayout &DL = M.getDataLayout();

clang/lib/CodeGen/CodeGenModule.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,9 @@ class CodeGenModule : public CodeGenTypeCache {
690690
CodeGenModule(ASTContext &C, IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
691691
const HeaderSearchOptions &headersearchopts,
692692
const PreprocessorOptions &ppopts,
693-
const CodeGenOptions &CodeGenOpts, llvm::Module &M,
693+
const CodeGenOptions &CodeGenOpts,
694+
const TargetInfo &CodeGenTargetInfo,
695+
llvm::Module &M,
694696
DiagnosticsEngine &Diags,
695697
CoverageSourceInfo *CoverageInfo = nullptr);
696698

clang/lib/CodeGen/ModuleBuilder.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,21 +149,26 @@ namespace {
149149
}
150150

151151
void Initialize(ASTContext &Context) override {
152+
Initialize(Context, Context.getTargetInfo());
153+
}
154+
155+
void Initialize(ASTContext &Context, const TargetInfo &CodeGenTargetInfo) override {
152156
Ctx = &Context;
153157

154-
M->setTargetTriple(Ctx->getTargetInfo().getTriple());
155-
M->setDataLayout(Ctx->getTargetInfo().getDataLayoutString());
156-
const auto &SDKVersion = Ctx->getTargetInfo().getSDKVersion();
158+
M->setTargetTriple(CodeGenTargetInfo.getTriple());
159+
M->setDataLayout(CodeGenTargetInfo.getDataLayoutString());
160+
const auto &SDKVersion = CodeGenTargetInfo.getSDKVersion();
157161
if (!SDKVersion.empty())
158162
M->setSDKVersion(SDKVersion);
159-
if (const auto *TVT = Ctx->getTargetInfo().getDarwinTargetVariantTriple())
163+
if (const auto *TVT = CodeGenTargetInfo.getDarwinTargetVariantTriple())
160164
M->setDarwinTargetVariantTriple(TVT->getTriple());
161165
if (auto TVSDKVersion =
162-
Ctx->getTargetInfo().getDarwinTargetVariantSDKVersion())
166+
CodeGenTargetInfo.getDarwinTargetVariantSDKVersion())
163167
M->setDarwinTargetVariantSDKVersion(*TVSDKVersion);
164168
Builder.reset(new CodeGen::CodeGenModule(Context, FS, HeaderSearchOpts,
165169
PreprocessorOpts, CodeGenOpts,
166-
*M, Diags, CoverageInfo));
170+
CodeGenTargetInfo, *M,
171+
Diags, CoverageInfo));
167172

168173
for (auto &&Lib : CodeGenOpts.DependentLibraries)
169174
Builder->AddDependentLib(Lib);

clang/lib/CodeGen/ObjectFilePCHContainerWriter.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,19 @@ class PCHContainerGenerator : public ASTConsumer {
174174
~PCHContainerGenerator() override = default;
175175

176176
void Initialize(ASTContext &Context) override {
177+
Initialize(Context, Context.getTargetInfo());
178+
}
179+
180+
void Initialize(ASTContext &Context, const TargetInfo &CodeGenTargetInfo) override {
177181
assert(!Ctx && "initialized multiple times");
178182

179183
Ctx = &Context;
180184
VMContext.reset(new llvm::LLVMContext());
181185
M.reset(new llvm::Module(MainFileName, *VMContext));
182186
M->setDataLayout(Ctx->getTargetInfo().getDataLayoutString());
183187
Builder.reset(new CodeGen::CodeGenModule(
184-
*Ctx, FS, HeaderSearchOpts, PreprocessorOpts, CodeGenOpts, *M, Diags));
188+
*Ctx, FS, HeaderSearchOpts, PreprocessorOpts, CodeGenOpts,
189+
CodeGenTargetInfo, *M, Diags));
185190

186191
// Prepare CGDebugInfo to emit debug info for a clang module.
187192
auto *DI = Builder->getModuleDebugInfo();

0 commit comments

Comments
 (0)