Skip to content

Commit aa10b27

Browse files
authored
Merge pull request #9469 from swiftlang/20240723/artemcm/ExplicitBuildClangTargetIRGen
[stable/20240723 🍒] Parameterize Initialization of 'clang::CodeGenerator' on a TargetInfo instance which may differ from the one in the ASTContext
2 parents 3a9722b + 9894e7a commit aa10b27

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
@@ -337,12 +337,14 @@ CodeGenModule::CodeGenModule(ASTContext &C,
337337
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
338338
const HeaderSearchOptions &HSO,
339339
const PreprocessorOptions &PPO,
340-
const CodeGenOptions &CGO, llvm::Module &M,
340+
const CodeGenOptions &CGO,
341+
const TargetInfo &CGTI,
342+
llvm::Module &M,
341343
DiagnosticsEngine &diags,
342344
CoverageSourceInfo *CoverageInfo)
343345
: Context(C), LangOpts(C.getLangOpts()), FS(FS), HeaderSearchOpts(HSO),
344346
PreprocessorOpts(PPO), CodeGenOpts(CGO), TheModule(M), Diags(diags),
345-
Target(C.getTargetInfo()), ABI(createCXXABI(*this)),
347+
Target(CGTI), ABI(createCXXABI(*this)),
346348
VMContext(M.getContext()), VTables(*this),
347349
SanitizerMD(new SanitizerMetadata(*this)) {
348350

@@ -358,19 +360,19 @@ CodeGenModule::CodeGenModule(ASTContext &C,
358360
BFloatTy = llvm::Type::getBFloatTy(LLVMContext);
359361
FloatTy = llvm::Type::getFloatTy(LLVMContext);
360362
DoubleTy = llvm::Type::getDoubleTy(LLVMContext);
361-
PointerWidthInBits = C.getTargetInfo().getPointerWidth(LangAS::Default);
363+
PointerWidthInBits = Target.getPointerWidth(LangAS::Default);
362364
PointerAlignInBytes =
363-
C.toCharUnitsFromBits(C.getTargetInfo().getPointerAlign(LangAS::Default))
365+
C.toCharUnitsFromBits(Target.getPointerAlign(LangAS::Default))
364366
.getQuantity();
365367
SizeSizeInBytes =
366-
C.toCharUnitsFromBits(C.getTargetInfo().getMaxPointerWidth()).getQuantity();
368+
C.toCharUnitsFromBits(Target.getMaxPointerWidth()).getQuantity();
367369
IntAlignInBytes =
368-
C.toCharUnitsFromBits(C.getTargetInfo().getIntAlign()).getQuantity();
370+
C.toCharUnitsFromBits(Target.getIntAlign()).getQuantity();
369371
CharTy =
370-
llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getCharWidth());
371-
IntTy = llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getIntWidth());
372+
llvm::IntegerType::get(LLVMContext, Target.getCharWidth());
373+
IntTy = llvm::IntegerType::get(LLVMContext, Target.getIntWidth());
372374
IntPtrTy = llvm::IntegerType::get(LLVMContext,
373-
C.getTargetInfo().getMaxPointerWidth());
375+
Target.getMaxPointerWidth());
374376
Int8PtrTy = llvm::PointerType::get(LLVMContext,
375377
C.getTargetAddressSpace(LangAS::Default));
376378
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
@@ -636,7 +636,9 @@ class CodeGenModule : public CodeGenTypeCache {
636636
CodeGenModule(ASTContext &C, IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
637637
const HeaderSearchOptions &headersearchopts,
638638
const PreprocessorOptions &ppopts,
639-
const CodeGenOptions &CodeGenOpts, llvm::Module &M,
639+
const CodeGenOptions &CodeGenOpts,
640+
const TargetInfo &CodeGenTargetInfo,
641+
llvm::Module &M,
640642
DiagnosticsEngine &Diags,
641643
CoverageSourceInfo *CoverageInfo = nullptr);
642644

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().getTriple());
155-
M->setDataLayout(Ctx->getTargetInfo().getDataLayoutString());
156-
const auto &SDKVersion = Ctx->getTargetInfo().getSDKVersion();
158+
M->setTargetTriple(CodeGenTargetInfo.getTriple().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/ObjectFilePCHContainerOperations.cpp

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

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

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

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

0 commit comments

Comments
 (0)