[clang] ca6bbe0 - [OpenMP] Make clang argument handling for the new driver more generic
Joseph Huber via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 29 06:14:57 PDT 2022
Author: Joseph Huber
Date: 2022-04-29T09:14:35-04:00
New Revision: ca6bbe008512c9dc6a1ac242466a9d42288daff8
URL: https://github.com/llvm/llvm-project/commit/ca6bbe008512c9dc6a1ac242466a9d42288daff8
DIFF: https://github.com/llvm/llvm-project/commit/ca6bbe008512c9dc6a1ac242466a9d42288daff8.diff
LOG: [OpenMP] Make clang argument handling for the new driver more generic
In preparation for accepting other offloading kinds with the new driver,
this patch makes the way we handle offloading actions more generic. A
new field to get the associated device action's toolchain is used rather
than manually iterating a list. This makes building the arguments easier
and makes sure that we doin't rely on any implicit ordering.
Reviewed By: yaxunl
Differential Revision: https://reviews.llvm.org/D123313
Added:
Modified:
clang/include/clang/Driver/Action.h
clang/lib/Driver/Action.cpp
clang/lib/Driver/ToolChains/Clang.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Driver/Action.h b/clang/include/clang/Driver/Action.h
index 36410150c2797..f8a96619c4f61 100644
--- a/clang/include/clang/Driver/Action.h
+++ b/clang/include/clang/Driver/Action.h
@@ -128,6 +128,9 @@ class Action {
/// The Offloading architecture associated with this action.
const char *OffloadingArch = nullptr;
+ /// The Offloading toolchain associated with this device action.
+ const ToolChain *OffloadingToolChain = nullptr;
+
Action(ActionClass Kind, types::ID Type) : Action(Kind, ActionList(), Type) {}
Action(ActionClass Kind, Action *Input, types::ID Type)
: Action(Kind, ActionList({Input}), Type) {}
@@ -184,7 +187,8 @@ class Action {
/// Set the device offload info of this action and propagate it to its
/// dependences.
- void propagateDeviceOffloadInfo(OffloadKind OKind, const char *OArch);
+ void propagateDeviceOffloadInfo(OffloadKind OKind, const char *OArch,
+ const ToolChain *OToolChain);
/// Append the host offload info of this action and propagate it to its
/// dependences.
@@ -205,10 +209,13 @@ class Action {
OffloadKind getOffloadingDeviceKind() const { return OffloadingDeviceKind; }
const char *getOffloadingArch() const { return OffloadingArch; }
+ const ToolChain *getOffloadingToolChain() const {
+ return OffloadingToolChain;
+ }
/// Check if this action have any offload kinds. Note that host offload kinds
/// are only set if the action is a dependence to a host offload action.
- bool isHostOffloading(OffloadKind OKind) const {
+ bool isHostOffloading(unsigned int OKind) const {
return ActiveOffloadKindMask & OKind;
}
bool isDeviceOffloading(OffloadKind OKind) const {
diff --git a/clang/lib/Driver/Action.cpp b/clang/lib/Driver/Action.cpp
index 21691c4ac4b98..8f4403ff42b65 100644
--- a/clang/lib/Driver/Action.cpp
+++ b/clang/lib/Driver/Action.cpp
@@ -54,7 +54,8 @@ const char *Action::getClassName(ActionClass AC) {
llvm_unreachable("invalid class");
}
-void Action::propagateDeviceOffloadInfo(OffloadKind OKind, const char *OArch) {
+void Action::propagateDeviceOffloadInfo(OffloadKind OKind, const char *OArch,
+ const ToolChain *OToolChain) {
// Offload action set its own kinds on their dependences.
if (Kind == OffloadClass)
return;
@@ -67,9 +68,10 @@ void Action::propagateDeviceOffloadInfo(OffloadKind OKind, const char *OArch) {
assert(!ActiveOffloadKindMask && "Setting a device kind in a host action??");
OffloadingDeviceKind = OKind;
OffloadingArch = OArch;
+ OffloadingToolChain = OToolChain;
for (auto *A : Inputs)
- A->propagateDeviceOffloadInfo(OffloadingDeviceKind, OArch);
+ A->propagateDeviceOffloadInfo(OffloadingDeviceKind, OArch, OToolChain);
}
void Action::propagateHostOffloadInfo(unsigned OKinds, const char *OArch) {
@@ -91,7 +93,8 @@ void Action::propagateOffloadInfo(const Action *A) {
propagateHostOffloadInfo(HK, A->getOffloadingArch());
else
propagateDeviceOffloadInfo(A->getOffloadingDeviceKind(),
- A->getOffloadingArch());
+ A->getOffloadingArch(),
+ A->getOffloadingToolChain());
}
std::string Action::getOffloadingKindPrefix() const {
@@ -192,6 +195,7 @@ OffloadAction::OffloadAction(const DeviceDependences &DDeps, types::ID Ty)
DevToolChains(DDeps.getToolChains()) {
auto &OKinds = DDeps.getOffloadKinds();
auto &BArchs = DDeps.getBoundArchs();
+ auto &OTCs = DDeps.getToolChains();
// If all inputs agree on the same kind, use it also for this action.
if (llvm::all_of(OKinds, [&](OffloadKind K) { return K == OKinds.front(); }))
@@ -203,7 +207,7 @@ OffloadAction::OffloadAction(const DeviceDependences &DDeps, types::ID Ty)
// Propagate info to the dependencies.
for (unsigned i = 0, e = getInputs().size(); i != e; ++i)
- getInputs()[i]->propagateDeviceOffloadInfo(OKinds[i], BArchs[i]);
+ getInputs()[i]->propagateDeviceOffloadInfo(OKinds[i], BArchs[i], OTCs[i]);
}
OffloadAction::OffloadAction(const HostDependence &HDep,
@@ -222,7 +226,8 @@ OffloadAction::OffloadAction(const HostDependence &HDep,
if (auto *A = DDeps.getActions()[i]) {
getInputs().push_back(A);
A->propagateDeviceOffloadInfo(DDeps.getOffloadKinds()[i],
- DDeps.getBoundArchs()[i]);
+ DDeps.getBoundArchs()[i],
+ DDeps.getToolChains()[i]);
}
}
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 05034ab15f402..3fc8dd0ecf24d 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -4384,11 +4384,13 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
bool IsHIP = JA.isOffloading(Action::OFK_HIP);
bool IsHIPDevice = JA.isDeviceOffloading(Action::OFK_HIP);
bool IsOpenMPDevice = JA.isDeviceOffloading(Action::OFK_OpenMP);
- bool IsOpenMPHost = JA.isHostOffloading(Action::OFK_OpenMP);
bool IsHeaderModulePrecompile = isa<HeaderModulePrecompileJobAction>(JA);
bool IsExtractAPI = isa<ExtractAPIJobAction>(JA);
bool IsDeviceOffloadAction = !(JA.isDeviceOffloading(Action::OFK_None) ||
JA.isDeviceOffloading(Action::OFK_Host));
+ bool IsHostOffloadingAction =
+ JA.isHostOffloading(Action::OFK_OpenMP) &&
+ !Args.hasArg(options::OPT_fno_openmp_new_driver);
bool IsUsingLTO = D.isUsingLTO(IsDeviceOffloadAction);
auto LTOMode = D.getLTOMode(IsDeviceOffloadAction);
@@ -4415,7 +4417,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
InputInfoList ModuleHeaderInputs;
InputInfoList ExtractAPIInputs;
- InputInfoList OpenMPHostInputs;
+ InputInfoList HostOffloadingInputs;
const InputInfo *CudaDeviceInput = nullptr;
const InputInfo *OpenMPDeviceInput = nullptr;
for (const InputInfo &I : Inputs) {
@@ -4438,12 +4440,12 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
<< types::getTypeName(ExpectedInputType);
}
ExtractAPIInputs.push_back(I);
+ } else if (IsHostOffloadingAction) {
+ HostOffloadingInputs.push_back(I);
} else if ((IsCuda || IsHIP) && !CudaDeviceInput) {
CudaDeviceInput = &I;
} else if (IsOpenMPDevice && !OpenMPDeviceInput) {
OpenMPDeviceInput = &I;
- } else if (IsOpenMPHost) {
- OpenMPHostInputs.push_back(I);
} else {
llvm_unreachable("unexpectedly given multiple inputs");
}
@@ -6948,24 +6950,23 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
}
}
- // Host-side OpenMP offloading recieves the device object files and embeds it
- // in a named section including the associated target triple and architecture.
- if (IsOpenMPHost && !OpenMPHostInputs.empty()) {
- auto InputFile = OpenMPHostInputs.begin();
- auto OpenMPTCs = C.getOffloadToolChains<Action::OFK_OpenMP>();
- for (auto TI = OpenMPTCs.first, TE = OpenMPTCs.second; TI != TE;
- ++TI, ++InputFile) {
- const ToolChain *TC = TI->second;
- const ArgList &TCArgs = C.getArgsForToolChain(TC, "", Action::OFK_OpenMP);
- StringRef File =
- C.getArgs().MakeArgString(TC->getInputFilename(*InputFile));
+ // Host-side offloading recieves the device object files and embeds it in a
+ // named section including the associated target triple and architecture.
+ for (const InputInfo Input : HostOffloadingInputs) {
+ const Action *OffloadAction = Input.getAction();
+ const ToolChain *TC = OffloadAction->getOffloadingToolChain();
+ const ArgList &TCArgs =
+ C.getArgsForToolChain(TC, OffloadAction->getOffloadingArch(),
+ OffloadAction->getOffloadingDeviceKind());
+ StringRef File = C.getArgs().MakeArgString(TC->getInputFilename(Input));
+ StringRef Arch = (OffloadAction->getOffloadingArch())
+ ? OffloadAction->getOffloadingArch()
+ : TCArgs.getLastArgValue(options::OPT_march_EQ);
- CmdArgs.push_back(
- Args.MakeArgString("-fembed-offload-object=" + File + "," +
- Action::GetOffloadKindName(Action::OFK_OpenMP) +
- "," + TC->getTripleString() + "," +
- TCArgs.getLastArgValue(options::OPT_march_EQ)));
- }
+ CmdArgs.push_back(Args.MakeArgString(
+ "-fembed-offload-object=" + File + "," +
+ Action::GetOffloadKindName(OffloadAction->getOffloadingDeviceKind()) +
+ "," + TC->getTripleString() + "," + Arch));
}
if (Triple.isAMDGPU()) {
More information about the cfe-commits
mailing list