https://github.com/artemcm updated https://github.com/llvm/llvm-project/pull/88977
>From 04e826688a504e141f3407567616bcf5cce9e2cc Mon Sep 17 00:00:00 2001 From: Artem Chikin <achi...@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 | 29 +++++++++---------- clang/lib/CodeGen/CodeGenModule.h | 3 +- clang/lib/CodeGen/ModuleBuilder.cpp | 21 +++++++++----- .../ObjectFilePCHContainerOperations.cpp | 5 ++-- 5 files changed, 36 insertions(+), 27 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..8380b71ababe5b 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -333,14 +333,13 @@ CodeGenModule::CodeGenModule(ASTContext &C, IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS, const HeaderSearchOptions &HSO, const PreprocessorOptions &PPO, - const CodeGenOptions &CGO, llvm::Module &M, - DiagnosticsEngine &diags, + 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)), - VMContext(M.getContext()), Types(*this), VTables(*this), - SanitizerMD(new SanitizerMetadata(*this)) { + Target(CGTI), ABI(createCXXABI(*this)), VMContext(M.getContext()), + Types(*this), VTables(*this), SanitizerMD(new SanitizerMetadata(*this)) { // Initialize the type cache. llvm::LLVMContext &LLVMContext = M.getContext(); @@ -353,20 +352,18 @@ 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(); - IntAlignInBytes = - C.toCharUnitsFromBits(C.getTargetInfo().getIntAlign()).getQuantity(); - CharTy = - llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getCharWidth()); - IntTy = llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getIntWidth()); - IntPtrTy = llvm::IntegerType::get(LLVMContext, - C.getTargetInfo().getMaxPointerWidth()); - Int8PtrTy = llvm::PointerType::get(LLVMContext, 0); + C.toCharUnitsFromBits(Target.getMaxPointerWidth()).getQuantity(); + IntAlignInBytes = C.toCharUnitsFromBits(Target.getIntAlign()).getQuantity(); + CharTy = llvm::IntegerType::get(LLVMContext, Target.getCharWidth()); + IntTy = llvm::IntegerType::get(LLVMContext, Target.getIntWidth()); + IntPtrTy = llvm::IntegerType::get(LLVMContext, 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..796efca160c6d5 100644 --- a/clang/lib/CodeGen/CodeGenModule.h +++ b/clang/lib/CodeGen/CodeGenModule.h @@ -613,7 +613,8 @@ 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..e66482e925575d 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)); + Builder.reset(new CodeGen::CodeGenModule( + Context, FS, HeaderSearchOpts, PreprocessorOpts, CodeGenOpts, + 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..752c5d0495a999 100644 --- a/clang/lib/CodeGen/ObjectFilePCHContainerOperations.cpp +++ b/clang/lib/CodeGen/ObjectFilePCHContainerOperations.cpp @@ -177,8 +177,9 @@ class PCHContainerGenerator : public ASTConsumer { VMContext.reset(new llvm::LLVMContext()); M.reset(new llvm::Module(MainFileName, *VMContext)); M->setDataLayout(Ctx->getTargetInfo().getDataLayoutString()); - Builder.reset(new CodeGen::CodeGenModule( - *Ctx, FS, HeaderSearchOpts, PreprocessorOpts, CodeGenOpts, *M, Diags)); + Builder.reset(new CodeGen::CodeGenModule(*Ctx, FS, HeaderSearchOpts, + PreprocessorOpts, CodeGenOpts, + Ctx->getTargetInfo(), *M, Diags)); // Prepare CGDebugInfo to emit debug info for a clang module. auto *DI = Builder->getModuleDebugInfo(); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits