[clang] [NFC] Parameterize Initialization of `clang::CodeGenerator` on a `TargetInfo` instance which may differ from the one in the `ASTContext` (PR #88977)
Artem Chikin via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 16 12:59:37 PDT 2024
https://github.com/artemcm created https://github.com/llvm/llvm-project/pull/88977
A client's Clang instance may require to perform type-checking against one OS version and compilation/code-generation against an earlier version. This change allows such clients to configure their built-in Clang code-generator with a custom `TargetInfo`.
>From c86ee8d6fc2b0d3995f0dee4e7bc206ef6e05c8b Mon Sep 17 00:00:00 2001
From: Artem Chikin <achikin at apple.com>
Date: Wed, 20 Dec 2023 10:56:42 -0800
Subject: [PATCH] [NFC] Parameterize Initialization of 'clang::CodeGenerator'
on a TargetInfo instance which may differ from the one in the ASTContext
As per https://github.com/apple/swift/pull/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'.
---
clang/include/clang/AST/ASTConsumer.h | 5 ++++
clang/lib/CodeGen/CodeGenModule.cpp | 23 +++++++++++--------
clang/lib/CodeGen/CodeGenModule.h | 4 +++-
clang/lib/CodeGen/ModuleBuilder.cpp | 17 +++++++++-----
.../ObjectFilePCHContainerOperations.cpp | 3 ++-
5 files changed, 34 insertions(+), 18 deletions(-)
diff --git a/clang/include/clang/AST/ASTConsumer.h b/clang/include/clang/AST/ASTConsumer.h
index ebcd8059284d8d..774d19565e57b0 100644
--- a/clang/include/clang/AST/ASTConsumer.h
+++ b/clang/include/clang/AST/ASTConsumer.h
@@ -26,6 +26,7 @@ namespace clang {
class VarDecl;
class FunctionDecl;
class ImportDecl;
+ class TargetInfo;
/// ASTConsumer - This is an abstract interface that should be implemented by
/// clients that read ASTs. This abstraction layer allows the client to be
@@ -46,6 +47,10 @@ class ASTConsumer {
/// ASTContext.
virtual void Initialize(ASTContext &Context) {}
+ /// Initialize - This is called to initialize the consumer, providing the
+ /// ASTContext.
+ virtual void Initialize(ASTContext &Context, const TargetInfo &CodeGenTargetInfo) {}
+
/// HandleTopLevelDecl - Handle the specified top-level declaration. This is
/// called by the parser to process every top-level Decl*.
///
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 0c447b20cef40d..52ff65c0986931 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -333,12 +333,14 @@ CodeGenModule::CodeGenModule(ASTContext &C,
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
const HeaderSearchOptions &HSO,
const PreprocessorOptions &PPO,
- const CodeGenOptions &CGO, llvm::Module &M,
+ const CodeGenOptions &CGO,
+ const TargetInfo &CGTI,
+ llvm::Module &M,
DiagnosticsEngine &diags,
CoverageSourceInfo *CoverageInfo)
: Context(C), LangOpts(C.getLangOpts()), FS(FS), HeaderSearchOpts(HSO),
PreprocessorOpts(PPO), CodeGenOpts(CGO), TheModule(M), Diags(diags),
- Target(C.getTargetInfo()), ABI(createCXXABI(*this)),
+ Target(CGTI), ABI(createCXXABI(*this)),
VMContext(M.getContext()), Types(*this), VTables(*this),
SanitizerMD(new SanitizerMetadata(*this)) {
@@ -353,20 +355,21 @@ CodeGenModule::CodeGenModule(ASTContext &C,
BFloatTy = llvm::Type::getBFloatTy(LLVMContext);
FloatTy = llvm::Type::getFloatTy(LLVMContext);
DoubleTy = llvm::Type::getDoubleTy(LLVMContext);
- PointerWidthInBits = C.getTargetInfo().getPointerWidth(LangAS::Default);
+ PointerWidthInBits = Target.getPointerWidth(LangAS::Default);
PointerAlignInBytes =
- C.toCharUnitsFromBits(C.getTargetInfo().getPointerAlign(LangAS::Default))
+ C.toCharUnitsFromBits(Target.getPointerAlign(LangAS::Default))
.getQuantity();
SizeSizeInBytes =
- C.toCharUnitsFromBits(C.getTargetInfo().getMaxPointerWidth()).getQuantity();
+ C.toCharUnitsFromBits(Target.getMaxPointerWidth()).getQuantity();
IntAlignInBytes =
- C.toCharUnitsFromBits(C.getTargetInfo().getIntAlign()).getQuantity();
+ C.toCharUnitsFromBits(Target.getIntAlign()).getQuantity();
CharTy =
- llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getCharWidth());
- IntTy = llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getIntWidth());
+ llvm::IntegerType::get(LLVMContext, Target.getCharWidth());
+ IntTy = llvm::IntegerType::get(LLVMContext, Target.getIntWidth());
IntPtrTy = llvm::IntegerType::get(LLVMContext,
- C.getTargetInfo().getMaxPointerWidth());
- Int8PtrTy = llvm::PointerType::get(LLVMContext, 0);
+ Target.getMaxPointerWidth());
+ Int8PtrTy = Int8Ty->getPointerTo(0);
+ Int8PtrPtrTy = Int8PtrTy->getPointerTo(0);
const llvm::DataLayout &DL = M.getDataLayout();
AllocaInt8PtrTy =
llvm::PointerType::get(LLVMContext, DL.getAllocaAddrSpace());
diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h
index 1cc447765e2c97..7721941ce370af 100644
--- a/clang/lib/CodeGen/CodeGenModule.h
+++ b/clang/lib/CodeGen/CodeGenModule.h
@@ -613,7 +613,9 @@ class CodeGenModule : public CodeGenTypeCache {
CodeGenModule(ASTContext &C, IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
const HeaderSearchOptions &headersearchopts,
const PreprocessorOptions &ppopts,
- const CodeGenOptions &CodeGenOpts, llvm::Module &M,
+ const CodeGenOptions &CodeGenOpts,
+ const TargetInfo &CodeGenTargetInfo,
+ llvm::Module &M,
DiagnosticsEngine &Diags,
CoverageSourceInfo *CoverageInfo = nullptr);
diff --git a/clang/lib/CodeGen/ModuleBuilder.cpp b/clang/lib/CodeGen/ModuleBuilder.cpp
index df85295cfb2e29..adcbe4c0881297 100644
--- a/clang/lib/CodeGen/ModuleBuilder.cpp
+++ b/clang/lib/CodeGen/ModuleBuilder.cpp
@@ -149,21 +149,26 @@ namespace {
}
void Initialize(ASTContext &Context) override {
+ Initialize(Context, Context.getTargetInfo());
+ }
+
+ void Initialize(ASTContext &Context, const TargetInfo &CodeGenTargetInfo) override {
Ctx = &Context;
- M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
- M->setDataLayout(Ctx->getTargetInfo().getDataLayoutString());
- const auto &SDKVersion = Ctx->getTargetInfo().getSDKVersion();
+ M->setTargetTriple(CodeGenTargetInfo.getTriple().getTriple());
+ M->setDataLayout(CodeGenTargetInfo.getDataLayoutString());
+ const auto &SDKVersion = CodeGenTargetInfo.getSDKVersion();
if (!SDKVersion.empty())
M->setSDKVersion(SDKVersion);
- if (const auto *TVT = Ctx->getTargetInfo().getDarwinTargetVariantTriple())
+ if (const auto *TVT = CodeGenTargetInfo.getDarwinTargetVariantTriple())
M->setDarwinTargetVariantTriple(TVT->getTriple());
if (auto TVSDKVersion =
- Ctx->getTargetInfo().getDarwinTargetVariantSDKVersion())
+ CodeGenTargetInfo.getDarwinTargetVariantSDKVersion())
M->setDarwinTargetVariantSDKVersion(*TVSDKVersion);
Builder.reset(new CodeGen::CodeGenModule(Context, FS, HeaderSearchOpts,
PreprocessorOpts, CodeGenOpts,
- *M, Diags, CoverageInfo));
+ CodeGenTargetInfo, *M,
+ Diags, CoverageInfo));
for (auto &&Lib : CodeGenOpts.DependentLibraries)
Builder->AddDependentLib(Lib);
diff --git a/clang/lib/CodeGen/ObjectFilePCHContainerOperations.cpp b/clang/lib/CodeGen/ObjectFilePCHContainerOperations.cpp
index ee543e40b46099..3487d778848527 100644
--- a/clang/lib/CodeGen/ObjectFilePCHContainerOperations.cpp
+++ b/clang/lib/CodeGen/ObjectFilePCHContainerOperations.cpp
@@ -178,7 +178,8 @@ class PCHContainerGenerator : public ASTConsumer {
M.reset(new llvm::Module(MainFileName, *VMContext));
M->setDataLayout(Ctx->getTargetInfo().getDataLayoutString());
Builder.reset(new CodeGen::CodeGenModule(
- *Ctx, FS, HeaderSearchOpts, PreprocessorOpts, CodeGenOpts, *M, Diags));
+ *Ctx, FS, HeaderSearchOpts, PreprocessorOpts, CodeGenOpts, Ctx->getTargetInfo(),
+ *M, Diags));
// Prepare CGDebugInfo to emit debug info for a clang module.
auto *DI = Builder->getModuleDebugInfo();
More information about the cfe-commits
mailing list