Skip to content

Parameterize Initialization of clang::CodeGenerator on a TargetInfo instance which may differ from the one in the ASTContext #7916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions clang/include/clang/AST/ASTConsumer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -46,6 +47,14 @@ class ASTConsumer {
/// ASTContext.
virtual void Initialize(ASTContext &Context) {}

/// Initialize - This is called to initialize the consumer, providing the
/// ASTContext. 'CodeGenTargetInfo' specifies the code-generation configuration
/// for this compilation instance, which may differ from the one carried
/// by the Context itself only in the OS Version number -
/// for example when type-checking must be performed against an epoch OS version
/// while code-generation must run according to the user-specified OS version.
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*.
///
Expand Down
20 changes: 11 additions & 9 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,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)) {

Expand All @@ -348,19 +350,19 @@ 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());
Target.getMaxPointerWidth());
Int8PtrTy = Int8Ty->getPointerTo(0);
Int8PtrPtrTy = Int8PtrTy->getPointerTo(0);
const llvm::DataLayout &DL = M.getDataLayout();
Expand Down
4 changes: 3 additions & 1 deletion clang/lib/CodeGen/CodeGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,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);

Expand Down
17 changes: 11 additions & 6 deletions clang/lib/CodeGen/ModuleBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion clang/lib/CodeGen/ObjectFilePCHContainerOperations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,19 @@ class PCHContainerGenerator : public ASTConsumer {
~PCHContainerGenerator() override = default;

void Initialize(ASTContext &Context) override {
Initialize(Context, Context.getTargetInfo());
}

void Initialize(ASTContext &Context, const TargetInfo &CodeGenTargetInfo) override {
assert(!Ctx && "initialized multiple times");

Ctx = &Context;
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));
*Ctx, FS, HeaderSearchOpts, PreprocessorOpts, CodeGenOpts,
CodeGenTargetInfo, *M, Diags));

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