Skip to content

Commit 04e8266

Browse files
committed
[NFC] 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'.
1 parent 281d716 commit 04e8266

File tree

5 files changed

+36
-27
lines changed

5 files changed

+36
-27
lines changed

clang/include/clang/AST/ASTConsumer.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace clang {
2626
class VarDecl;
2727
class FunctionDecl;
2828
class ImportDecl;
29+
class TargetInfo;
2930

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

50+
/// Initialize - This is called to initialize the consumer, providing the
51+
/// ASTContext.
52+
virtual void Initialize(ASTContext &Context, const TargetInfo &CodeGenTargetInfo) {}
53+
4954
/// HandleTopLevelDecl - Handle the specified top-level declaration. This is
5055
/// called by the parser to process every top-level Decl*.
5156
///

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -333,14 +333,13 @@ CodeGenModule::CodeGenModule(ASTContext &C,
333333
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
334334
const HeaderSearchOptions &HSO,
335335
const PreprocessorOptions &PPO,
336-
const CodeGenOptions &CGO, llvm::Module &M,
337-
DiagnosticsEngine &diags,
336+
const CodeGenOptions &CGO, const TargetInfo &CGTI,
337+
llvm::Module &M, DiagnosticsEngine &diags,
338338
CoverageSourceInfo *CoverageInfo)
339339
: Context(C), LangOpts(C.getLangOpts()), FS(FS), HeaderSearchOpts(HSO),
340340
PreprocessorOpts(PPO), CodeGenOpts(CGO), TheModule(M), Diags(diags),
341-
Target(C.getTargetInfo()), ABI(createCXXABI(*this)),
342-
VMContext(M.getContext()), Types(*this), VTables(*this),
343-
SanitizerMD(new SanitizerMetadata(*this)) {
341+
Target(CGTI), ABI(createCXXABI(*this)), VMContext(M.getContext()),
342+
Types(*this), VTables(*this), SanitizerMD(new SanitizerMetadata(*this)) {
344343

345344
// Initialize the type cache.
346345
llvm::LLVMContext &LLVMContext = M.getContext();
@@ -353,20 +352,18 @@ CodeGenModule::CodeGenModule(ASTContext &C,
353352
BFloatTy = llvm::Type::getBFloatTy(LLVMContext);
354353
FloatTy = llvm::Type::getFloatTy(LLVMContext);
355354
DoubleTy = llvm::Type::getDoubleTy(LLVMContext);
356-
PointerWidthInBits = C.getTargetInfo().getPointerWidth(LangAS::Default);
355+
PointerWidthInBits = Target.getPointerWidth(LangAS::Default);
357356
PointerAlignInBytes =
358-
C.toCharUnitsFromBits(C.getTargetInfo().getPointerAlign(LangAS::Default))
357+
C.toCharUnitsFromBits(Target.getPointerAlign(LangAS::Default))
359358
.getQuantity();
360359
SizeSizeInBytes =
361-
C.toCharUnitsFromBits(C.getTargetInfo().getMaxPointerWidth()).getQuantity();
362-
IntAlignInBytes =
363-
C.toCharUnitsFromBits(C.getTargetInfo().getIntAlign()).getQuantity();
364-
CharTy =
365-
llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getCharWidth());
366-
IntTy = llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getIntWidth());
367-
IntPtrTy = llvm::IntegerType::get(LLVMContext,
368-
C.getTargetInfo().getMaxPointerWidth());
369-
Int8PtrTy = llvm::PointerType::get(LLVMContext, 0);
360+
C.toCharUnitsFromBits(Target.getMaxPointerWidth()).getQuantity();
361+
IntAlignInBytes = C.toCharUnitsFromBits(Target.getIntAlign()).getQuantity();
362+
CharTy = llvm::IntegerType::get(LLVMContext, Target.getCharWidth());
363+
IntTy = llvm::IntegerType::get(LLVMContext, Target.getIntWidth());
364+
IntPtrTy = llvm::IntegerType::get(LLVMContext, Target.getMaxPointerWidth());
365+
Int8PtrTy = Int8Ty->getPointerTo(0);
366+
Int8PtrPtrTy = Int8PtrTy->getPointerTo(0);
370367
const llvm::DataLayout &DL = M.getDataLayout();
371368
AllocaInt8PtrTy =
372369
llvm::PointerType::get(LLVMContext, DL.getAllocaAddrSpace());

clang/lib/CodeGen/CodeGenModule.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,8 @@ class CodeGenModule : public CodeGenTypeCache {
613613
CodeGenModule(ASTContext &C, IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
614614
const HeaderSearchOptions &headersearchopts,
615615
const PreprocessorOptions &ppopts,
616-
const CodeGenOptions &CodeGenOpts, llvm::Module &M,
616+
const CodeGenOptions &CodeGenOpts,
617+
const TargetInfo &CodeGenTargetInfo, llvm::Module &M,
617618
DiagnosticsEngine &Diags,
618619
CoverageSourceInfo *CoverageInfo = nullptr);
619620

clang/lib/CodeGen/ModuleBuilder.cpp

Lines changed: 13 additions & 8 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,
156+
const TargetInfo &CodeGenTargetInfo) override {
152157
Ctx = &Context;
153158

154-
M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
155-
M->setDataLayout(Ctx->getTargetInfo().getDataLayoutString());
156-
const auto &SDKVersion = Ctx->getTargetInfo().getSDKVersion();
159+
M->setTargetTriple(CodeGenTargetInfo.getTriple().getTriple());
160+
M->setDataLayout(CodeGenTargetInfo.getDataLayoutString());
161+
const auto &SDKVersion = CodeGenTargetInfo.getSDKVersion();
157162
if (!SDKVersion.empty())
158163
M->setSDKVersion(SDKVersion);
159-
if (const auto *TVT = Ctx->getTargetInfo().getDarwinTargetVariantTriple())
164+
if (const auto *TVT = CodeGenTargetInfo.getDarwinTargetVariantTriple())
160165
M->setDarwinTargetVariantTriple(TVT->getTriple());
161166
if (auto TVSDKVersion =
162-
Ctx->getTargetInfo().getDarwinTargetVariantSDKVersion())
167+
CodeGenTargetInfo.getDarwinTargetVariantSDKVersion())
163168
M->setDarwinTargetVariantSDKVersion(*TVSDKVersion);
164-
Builder.reset(new CodeGen::CodeGenModule(Context, FS, HeaderSearchOpts,
165-
PreprocessorOpts, CodeGenOpts,
166-
*M, Diags, CoverageInfo));
169+
Builder.reset(new CodeGen::CodeGenModule(
170+
Context, FS, HeaderSearchOpts, PreprocessorOpts, CodeGenOpts,
171+
CodeGenTargetInfo, *M, Diags, CoverageInfo));
167172

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

clang/lib/CodeGen/ObjectFilePCHContainerOperations.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,9 @@ class PCHContainerGenerator : public ASTConsumer {
177177
VMContext.reset(new llvm::LLVMContext());
178178
M.reset(new llvm::Module(MainFileName, *VMContext));
179179
M->setDataLayout(Ctx->getTargetInfo().getDataLayoutString());
180-
Builder.reset(new CodeGen::CodeGenModule(
181-
*Ctx, FS, HeaderSearchOpts, PreprocessorOpts, CodeGenOpts, *M, Diags));
180+
Builder.reset(new CodeGen::CodeGenModule(*Ctx, FS, HeaderSearchOpts,
181+
PreprocessorOpts, CodeGenOpts,
182+
Ctx->getTargetInfo(), *M, Diags));
182183

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

0 commit comments

Comments
 (0)