Skip to content

Commit f4b733c

Browse files
authored
Merge pull request #7916 from swiftlang/artemcm/ExplicitBuildClangTargetIRGen
Parameterize Initialization of `clang::CodeGenerator` on a `TargetInfo` instance which may differ from the one in the `ASTContext`
2 parents d1d2bfa + 4c63c4c commit f4b733c

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
@@ -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,14 @@ class ASTConsumer {
4647
/// ASTContext.
4748
virtual void Initialize(ASTContext &Context) {}
4849

50+
/// Initialize - This is called to initialize the consumer, providing the
51+
/// ASTContext. 'CodeGenTargetInfo' specifies the code-generation configuration
52+
/// for this compilation instance, which may differ from the one carried
53+
/// by the Context itself only in the OS Version number -
54+
/// for example when type-checking must be performed against an epoch OS version
55+
/// while code-generation must run according to the user-specified OS version.
56+
virtual void Initialize(ASTContext &Context, const TargetInfo &CodeGenTargetInfo) {}
57+
4958
/// HandleTopLevelDecl - Handle the specified top-level declaration. This is
5059
/// called by the parser to process every top-level Decl*.
5160
///

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,14 @@ CodeGenModule::CodeGenModule(ASTContext &C,
328328
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
329329
const HeaderSearchOptions &HSO,
330330
const PreprocessorOptions &PPO,
331-
const CodeGenOptions &CGO, llvm::Module &M,
331+
const CodeGenOptions &CGO,
332+
const TargetInfo &CGTI,
333+
llvm::Module &M,
332334
DiagnosticsEngine &diags,
333335
CoverageSourceInfo *CoverageInfo)
334336
: Context(C), LangOpts(C.getLangOpts()), FS(FS), HeaderSearchOpts(HSO),
335337
PreprocessorOpts(PPO), CodeGenOpts(CGO), TheModule(M), Diags(diags),
336-
Target(C.getTargetInfo()), ABI(createCXXABI(*this)),
338+
Target(CGTI), ABI(createCXXABI(*this)),
337339
VMContext(M.getContext()), Types(*this), VTables(*this),
338340
SanitizerMD(new SanitizerMetadata(*this)) {
339341

@@ -348,19 +350,19 @@ CodeGenModule::CodeGenModule(ASTContext &C,
348350
BFloatTy = llvm::Type::getBFloatTy(LLVMContext);
349351
FloatTy = llvm::Type::getFloatTy(LLVMContext);
350352
DoubleTy = llvm::Type::getDoubleTy(LLVMContext);
351-
PointerWidthInBits = C.getTargetInfo().getPointerWidth(LangAS::Default);
353+
PointerWidthInBits = Target.getPointerWidth(LangAS::Default);
352354
PointerAlignInBytes =
353-
C.toCharUnitsFromBits(C.getTargetInfo().getPointerAlign(LangAS::Default))
355+
C.toCharUnitsFromBits(Target.getPointerAlign(LangAS::Default))
354356
.getQuantity();
355357
SizeSizeInBytes =
356-
C.toCharUnitsFromBits(C.getTargetInfo().getMaxPointerWidth()).getQuantity();
358+
C.toCharUnitsFromBits(Target.getMaxPointerWidth()).getQuantity();
357359
IntAlignInBytes =
358-
C.toCharUnitsFromBits(C.getTargetInfo().getIntAlign()).getQuantity();
360+
C.toCharUnitsFromBits(Target.getIntAlign()).getQuantity();
359361
CharTy =
360-
llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getCharWidth());
361-
IntTy = llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getIntWidth());
362+
llvm::IntegerType::get(LLVMContext, Target.getCharWidth());
363+
IntTy = llvm::IntegerType::get(LLVMContext, Target.getIntWidth());
362364
IntPtrTy = llvm::IntegerType::get(LLVMContext,
363-
C.getTargetInfo().getMaxPointerWidth());
365+
Target.getMaxPointerWidth());
364366
Int8PtrTy = Int8Ty->getPointerTo(0);
365367
Int8PtrPtrTy = Int8PtrTy->getPointerTo(0);
366368
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
@@ -616,7 +616,9 @@ class CodeGenModule : public CodeGenTypeCache {
616616
CodeGenModule(ASTContext &C, IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
617617
const HeaderSearchOptions &headersearchopts,
618618
const PreprocessorOptions &ppopts,
619-
const CodeGenOptions &CodeGenOpts, llvm::Module &M,
619+
const CodeGenOptions &CodeGenOpts,
620+
const TargetInfo &CodeGenTargetInfo,
621+
llvm::Module &M,
620622
DiagnosticsEngine &Diags,
621623
CoverageSourceInfo *CoverageInfo = nullptr);
622624

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)