Skip to content

Commit d412dbe

Browse files
oToToTsam-mccall
authored andcommitted
[clang][NFC] Extract Target and AuxTarget creation in CompilerInstance to new function
As @sammccall mentioned in [[ https://reviews.llvm.org/D97109 | D97109 ]], I've extract the logic of creating Target and AuxTarget into a new function called `createTargetAndAuxTarget`. Since there are many similar code in clang or other related tools, consolidating them into a single function may help others to maintain the logic handling target related things. Reviewed By: sammccall Differential Revision: https://reviews.llvm.org/D97493
1 parent 2a42c75 commit d412dbe

File tree

2 files changed

+52
-43
lines changed

2 files changed

+52
-43
lines changed

clang/include/clang/Frontend/CompilerInstance.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,9 @@ class CompilerInstance : public ModuleLoader {
382382
/// Replace the current AuxTarget.
383383
void setAuxTarget(TargetInfo *Value);
384384

385+
// Create Target and AuxTarget based on current options
386+
bool createTarget();
387+
385388
/// }
386389
/// @name Virtual File System
387390
/// {

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,54 @@ void CompilerInstance::setVerboseOutputStream(std::unique_ptr<raw_ostream> Value
9797
void CompilerInstance::setTarget(TargetInfo *Value) { Target = Value; }
9898
void CompilerInstance::setAuxTarget(TargetInfo *Value) { AuxTarget = Value; }
9999

100+
bool CompilerInstance::createTarget() {
101+
// Create the target instance.
102+
setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(),
103+
getInvocation().TargetOpts));
104+
if (!hasTarget())
105+
return false;
106+
107+
// Create TargetInfo for the other side of CUDA/OpenMP/SYCL compilation.
108+
if ((getLangOpts().CUDA || getLangOpts().OpenMPIsDevice ||
109+
getLangOpts().SYCLIsDevice) &&
110+
!getFrontendOpts().AuxTriple.empty()) {
111+
auto TO = std::make_shared<TargetOptions>();
112+
TO->Triple = llvm::Triple::normalize(getFrontendOpts().AuxTriple);
113+
if (getFrontendOpts().AuxTargetCPU)
114+
TO->CPU = getFrontendOpts().AuxTargetCPU.getValue();
115+
if (getFrontendOpts().AuxTargetFeatures)
116+
TO->FeaturesAsWritten = getFrontendOpts().AuxTargetFeatures.getValue();
117+
TO->HostTriple = getTarget().getTriple().str();
118+
setAuxTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), TO));
119+
}
120+
121+
if (!getTarget().hasStrictFP() && !getLangOpts().ExpStrictFP) {
122+
if (getLangOpts().getFPRoundingMode() !=
123+
llvm::RoundingMode::NearestTiesToEven) {
124+
getDiagnostics().Report(diag::warn_fe_backend_unsupported_fp_rounding);
125+
getLangOpts().setFPRoundingMode(llvm::RoundingMode::NearestTiesToEven);
126+
}
127+
if (getLangOpts().getFPExceptionMode() != LangOptions::FPE_Ignore) {
128+
getDiagnostics().Report(diag::warn_fe_backend_unsupported_fp_exceptions);
129+
getLangOpts().setFPExceptionMode(LangOptions::FPE_Ignore);
130+
}
131+
// FIXME: can we disable FEnvAccess?
132+
}
133+
134+
// Inform the target of the language options.
135+
// FIXME: We shouldn't need to do this, the target should be immutable once
136+
// created. This complexity should be lifted elsewhere.
137+
getTarget().adjust(getLangOpts());
138+
139+
// Adjust target options based on codegen options.
140+
getTarget().adjustTargetOptions(getCodeGenOpts(), getTargetOpts());
141+
142+
if (auto *Aux = getAuxTarget())
143+
getTarget().setAuxTarget(Aux);
144+
145+
return true;
146+
}
147+
100148
llvm::vfs::FileSystem &CompilerInstance::getVirtualFileSystem() const {
101149
return getFileManager().getVirtualFileSystem();
102150
}
@@ -878,51 +926,9 @@ bool CompilerInstance::ExecuteAction(FrontendAction &Act) {
878926
if (!Act.PrepareToExecute(*this))
879927
return false;
880928

881-
// Create the target instance.
882-
setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(),
883-
getInvocation().TargetOpts));
884-
if (!hasTarget())
929+
if (!createTarget())
885930
return false;
886931

887-
// Create TargetInfo for the other side of CUDA/OpenMP/SYCL compilation.
888-
if ((getLangOpts().CUDA || getLangOpts().OpenMPIsDevice ||
889-
getLangOpts().SYCLIsDevice) &&
890-
!getFrontendOpts().AuxTriple.empty()) {
891-
auto TO = std::make_shared<TargetOptions>();
892-
TO->Triple = llvm::Triple::normalize(getFrontendOpts().AuxTriple);
893-
if (getFrontendOpts().AuxTargetCPU)
894-
TO->CPU = getFrontendOpts().AuxTargetCPU.getValue();
895-
if (getFrontendOpts().AuxTargetFeatures)
896-
TO->FeaturesAsWritten = getFrontendOpts().AuxTargetFeatures.getValue();
897-
TO->HostTriple = getTarget().getTriple().str();
898-
setAuxTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), TO));
899-
}
900-
901-
if (!getTarget().hasStrictFP() && !getLangOpts().ExpStrictFP) {
902-
if (getLangOpts().getFPRoundingMode() !=
903-
llvm::RoundingMode::NearestTiesToEven) {
904-
getDiagnostics().Report(diag::warn_fe_backend_unsupported_fp_rounding);
905-
getLangOpts().setFPRoundingMode(llvm::RoundingMode::NearestTiesToEven);
906-
}
907-
if (getLangOpts().getFPExceptionMode() != LangOptions::FPE_Ignore) {
908-
getDiagnostics().Report(diag::warn_fe_backend_unsupported_fp_exceptions);
909-
getLangOpts().setFPExceptionMode(LangOptions::FPE_Ignore);
910-
}
911-
// FIXME: can we disable FEnvAccess?
912-
}
913-
914-
// Inform the target of the language options.
915-
//
916-
// FIXME: We shouldn't need to do this, the target should be immutable once
917-
// created. This complexity should be lifted elsewhere.
918-
getTarget().adjust(getLangOpts());
919-
920-
// Adjust target options based on codegen options.
921-
getTarget().adjustTargetOptions(getCodeGenOpts(), getTargetOpts());
922-
923-
if (auto *Aux = getAuxTarget())
924-
getTarget().setAuxTarget(Aux);
925-
926932
// rewriter project will change target built-in bool type from its default.
927933
if (getFrontendOpts().ProgramAction == frontend::RewriteObjC)
928934
getTarget().noSignedCharForObjCBool();

0 commit comments

Comments
 (0)